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

nRF51822 problems with spi slave

Hi, i'm trying to communicate between a msp-430f5529 (spi master) and a nrf51822 (spi slave). I have configured the MISO, MOSI, CS and SCLK of the nrf. I use the spi_slave_example for testing the communication.

When the spi transfer is completed it calls to the handle "static void spi_slave_event_handle(spi_slave_evt_t event)". As you know to the end of this function you have to set the buffer again (after you process the data that arrived) with "spi_slave_buffers_set" .I want set the new buffer data depending of the last transfer data, then i use a switch case for the first byte that arrives but this fails, i need to set the bufffer to the end of function spi_slave_event_handle(). I'm doing this:

     if (event.evt_type == SPI_SLAVE_XFER_DONE) 
     { 			
      SpiCommand = m_rx_buf[0];			
		switch (SpiCommand)
		{
			case 0x01:
				nrf_gpio_port_set(NRF_GPIO_PORT_SELECT_PORT1, 1);
                 spi_slave_buffers_set(m_tx_buf, m_rx_buf, sizeof(m_tx_buf), sizeof(m_rx_buf));
                 break;
            case 0x02: ...
                 break;
            default:
                 break;
        }             
       }

it doesn't work, the led doesn't turn on. But if i try this:

  if (event.evt_type == SPI_SLAVE_XFER_DONE) 
     { 			
      SpiCommand = m_rx_buf[0];			
		switch (SpiCommand)
		{
			case 0x01:
				nrf_gpio_port_set(NRF_GPIO_PORT_SELECT_PORT1, 1);
                 break;
            case 0x02: ...
                 break;
            default:
                 break;
        }             
         spi_slave_buffers_set(m_tx_buf, m_rx_buf, sizeof(m_tx_buf), sizeof(m_rx_buf));
       }

It works great, the led turn on. I have to put spi_slave_buffers_set() out of the switch at the end. Why? Any idea?

  • i forgot comment, i'm using softdevice at the same time. Is this the reason?

  • Hi

    The SPIS proceidure is that when the CSN line is set high by the master, which means end of SPI transmission/reception, the SPIS handler is called with event type SPI_SLAVE_XFER_DONE, which will make your switch statement execute. You must then call spi_slave_buffers_set in order for the SPIS handler to be called again when the CSN line is set high next time.

    My guess is that the first time the CSN is set high and the SPIS handler is executed, the value of m_rx_buf[0] is not 0x01, so the LEDs are not turned on and you will not set the buffers, making the SPIS handler not to be called next time the CSN line is set high, i.e. your SPI communication halts.

    However, when you put the spi_slave_buffers_set call outside your switch statement, your handler is also called next time CSN is set high, making your SPI communication to continue. Perhaps at some point the value of m_rx_buf[0] is 0x01 and the LEDs are turned on.

    Try to check if my theory is correct, report back your result.

  • Hi Stefan Thanks for reply, the problem is that i set the CSN line once, i'm testing to transfer only one byte "0x01" therefore i don't set high CSN (in the master) for a next time.

Related