This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF51822 and SPI Slave Example - Confused How This Works And How To Use It

Hello, I am going over this example here: infocenter.nordicsemi.com/index.jsp

I am a bit confused how this works. My intention is to create a slave, so a master can send different commands (e.g. command indicating that a data transfer is to follow, a query command where the slave returns data, etc.). The slave needs to be performing tasks in the meantime.

So if the master sends a command, how would I then compare it, and then send the corresponding data back? It seems like this library is set up for a direct exchange of data? Would I just take a look at the m_rx_buff after the spis_xfer_done flag was set, then prepare the m_tx_buf with the corresponding command data to send back? I also see a function called memset() and couldn't figure out how this is supposed to work.

What is the best way to perform other tasks while the slave is waiting for commands followed by data xfer?

Thanks and any help/advice is greatly appreciated.

Parents
  • I have not used SPIS, but I can answer some general C programming questions

    The memset is used to clear the rx buffer memory, by filling it with zeros

    If you look at the code the tx buffer seems to be initialised to contain the word, "Nordic"

    Personally, I find the tx buffer initialisation a bit confusing, as you are likely to need to send back something based on the data you receive, rather than a static string. (which I presume is copied into the tx buffer memory (array)

    In terms of SPIS...

    Looking at the docs (e.f. for SDK 12.3), there only seems to be an event when the transfer is complete.

    infocenter.nordicsemi.com/index.jsp

    I presume this is when the master clears the SS / CS pin, but its not clear from the docs, so you may want to see when this event is triggered.

    Edit.

    I updated the link and converted this to an answer

    BTW. If you look there are 2 other events, but neither are much use to you.

    Nordic may pitch in with a better answer, but I'd advise that you look at the driver code for the SPIS in components\drivers_nrf\spi_slave

    As there is a IRQ function

    static void spis_irq_handler(NRF_SPIS_Type * p_spis, spis_cb_t * p_cb)
    

    So you could try logging if other events are triggered internally e.g. ideally you need one per byte of data received.

    One thing however, that I can see if problematic, is getting the code to respond fast enough even if you get an event to say a byte has arrived, you will not have time to put the necessary bytes into the tx buffer, because SPI can run at over 2M bits per second.

    i.e 250,000 bytes per second.

    So that means that a byte would arrive every 4uS

    And the next "bit" of data would need to be available in 0.5uS, which only gives you 8 clock cycles to populate the next byte of the TX buffer.

    I would say that even using assembler this is potentially impossible.

    If you can slow down the SPI clock to 125khz which is the spec minimum it would give you 128 clock cycles, so you would potentially be able to update it fast enough

    But even then, there could be issues of the SPIS hardware needing a longer time to preload the byte

Reply
  • I have not used SPIS, but I can answer some general C programming questions

    The memset is used to clear the rx buffer memory, by filling it with zeros

    If you look at the code the tx buffer seems to be initialised to contain the word, "Nordic"

    Personally, I find the tx buffer initialisation a bit confusing, as you are likely to need to send back something based on the data you receive, rather than a static string. (which I presume is copied into the tx buffer memory (array)

    In terms of SPIS...

    Looking at the docs (e.f. for SDK 12.3), there only seems to be an event when the transfer is complete.

    infocenter.nordicsemi.com/index.jsp

    I presume this is when the master clears the SS / CS pin, but its not clear from the docs, so you may want to see when this event is triggered.

    Edit.

    I updated the link and converted this to an answer

    BTW. If you look there are 2 other events, but neither are much use to you.

    Nordic may pitch in with a better answer, but I'd advise that you look at the driver code for the SPIS in components\drivers_nrf\spi_slave

    As there is a IRQ function

    static void spis_irq_handler(NRF_SPIS_Type * p_spis, spis_cb_t * p_cb)
    

    So you could try logging if other events are triggered internally e.g. ideally you need one per byte of data received.

    One thing however, that I can see if problematic, is getting the code to respond fast enough even if you get an event to say a byte has arrived, you will not have time to put the necessary bytes into the tx buffer, because SPI can run at over 2M bits per second.

    i.e 250,000 bytes per second.

    So that means that a byte would arrive every 4uS

    And the next "bit" of data would need to be available in 0.5uS, which only gives you 8 clock cycles to populate the next byte of the TX buffer.

    I would say that even using assembler this is potentially impossible.

    If you can slow down the SPI clock to 125khz which is the spec minimum it would give you 128 clock cycles, so you would potentially be able to update it fast enough

    But even then, there could be issues of the SPIS hardware needing a longer time to preload the byte

Children
No Data
Related