Hot!PIC18F4550 USB and SPI

Author
Rhuan Correia
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2018/02/07 20:28:22
  • Location: 0
  • Status: offline
2018/02/14 13:10:39 (permalink)
0

PIC18F4550 USB and SPI

Hi everyone.

I'm controlling another device (slave) with PIC18F4550 (master) and I need to receive data from slave and save directly to SRAM memory to send the packets to the host via USB.

The slave sends 27 bytes per time. I'm using SPI_Read comand and saving the information into USB buffer. So, I pretend to use a while loop and send the packets when the size reachs 270 bytes.

I'm using MikroC.

1) Is it possible to use them (SPI and USB) at the same time?
 
2) If yes, I will allocate 270 bytes in the write_buffer variable and can the 27 bytes be written sequentially (27-to-27) in SRAM memory?

3) I will receive no data from PC USB. Is there any way to raise the packet to 540 bytes and use a small read_buffer variable?

Thanks in advance!   
 
Rhuan.
 



#1

3 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3327
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC18F4550 USB and SPI 2018/02/14 18:22:11 (permalink)
    0
    Yes but with qualifications - mainly on the USB side of things.
    The USB stack (at least the Microchip one - never used MikroC) needs to be called fairly frequently (about every mSec if I recall). It is interrupt driven as far as the hardware interface is concerned but does need care and attention every so often. (This applies to both the modes for the Microchip USB stack).
    It all depends on how the SPI is being driven. If it uses blocking functions and the exchanges are not occurring fast enough then you might not be able to get back to service the USB stack in time.
    On the other hand if you use the SPI peripheral through interrupts then your main loop can be left free to handle the USB stack and only add in the data from the SPI when it is already in the buffer.
    Only guessing for your exact situation but I've seen similar things happen before.
    Susan
    #2
    Rhuan Correia
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2018/02/07 20:28:22
    • Location: 0
    • Status: offline
    Re: PIC18F4550 USB and SPI 2018/02/15 10:13:37 (permalink)
    0
    Thanks for your support, Susan!

    I undestood your point and I agree with it, but I won't use real time SPI with USB. I will give some instructions to slave only in the power up. So, I'll wait for new data to be read, then I'll read continuously.
     
    My code has around 300 lines. So, I will explain as better I can below.

    function(SPI_Write);
    function(SPI_Read);
    SPI_Write(<hex configurations of slave>) //there are at least 20 instructions to send to slave
     
    void main ()
    if pin RB2 = 1; // indicates that new data from slave is ready to be read and it has 27 bytes of binary info
    SPI_Read = USB_write_buffer[270] // I would like to send 270 bytes packet to the host

    Can I save SPI received data directly into USB RAM? How can I do this? It's like a internal conversion of SPI-to-USB converter. 

    In this case, it is possible to save the bytes received via SPI into variables, and then write them in the HID_Write?
     
    Best regards,

    Rhuan.
    #3
    Aussie Susan
    Super Member
    • Total Posts : 3327
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC18F4550 USB and SPI 2018/02/15 18:42:23 (permalink)
    0
    Your use of two functions to read from and write to the SPI interface makes me thing that perhaps you don't understand that the SPI is a value exchange protocol - the master initiates each exchange but it will receive a value from the slave at the same time as it sends a value to it. Therefore you only need one functions - if you don't care what is received back then ignore the returned value; if you don't care what is sent then send a dummy value.
    The master has to initiate each exchange. When the slave raises the pin to say it has values, the master must then initiate 27 exchanges. (It is during this process that you may have timing issues if your SPI function is blocking.)
    Where you put the values you received from the slave is up to you, as is what values you pass on to the USB peripheral and where they come from. If you use the same program buffer then that might help. 
    However I have no idea of the USB stack that you are using and whether or not it uses an internal buffer, how you could access it and how you can initiate a USB transfer without passing a buffer pointer (on the assumption that you have already filled in some internal buffer).
    Personally, I would have my main loop servicing the USB peripheral as required. When I am told that the SPI slave has data, I would start an interrupt driven exchange that stores each value into a buffer in an ISR (triggering the next exchange until all 27 values have been exchanged) which then sets a flag for the main loop to perform the USB write.
    Susan
    #4
    Jump to:
    © 2018 APG vNext Commercial Version 4.5