Hi everybody!
I've plugged my NRF51422 as a slave on a STM32 through a SPI bus. I would like to be able to send to the NRF an unknown amount of data. I also need to read the bytes as they arrived in order to know how to handle the next ones and finally, I need to detect the end of a frame. I firstly thought using the CSN to detect the end of a frame, but it seems that the spi_slave_event_handle method is triggered only on that signal. So I need it to read each byte. My answer is: Is there an interrupt called for each byte received not using the CSN? Or is there an other way to read an unknown amount of data?
Here is the code I use for the moment. I didn't test everything, but the idea is here:
static void spi_slave_event_handle(spi_slave_evt_t event)
{
static int state = 0;
static int address;
static bool dataEnd = 0;
uint32_t err_code;
m_rx_buf[0] = m_rx_buf[1] = 0x00;
if (event.evt_type == SPI_SLAVE_BUFFERS_SET_DONE )
{
err_code = spi_slave_buffers_set(m_tx_buf, m_rx_buf, 1, 1);
switch(state)
{
case 0: // Basic state: send data
if(!m_rx_buf[0] == 0)
{
switch((m_rx_buf[0]&0x80) >> 7) // We read the R/W bit
{
case 0: // The STM32 need to read some data
address = m_rx_buf[0]&0x3F; // We read which data is needed
err_code = spi_slave_buffers_set(&m_registres[address], m_rx_buf, 1, 1);// We prepare the data
break;
case 1: // The STM32 will send us data
state = 1; // We go in the receive state for the next data
address = m_rx_buf[0]&0x3F;
err_code = spi_slave_buffers_set(m_tx_buf, m_rx_buf, 1, 1);// We prepare the data
break;
}
}
else // STM32 wait for data
{
address++; // We take the next data in our table
err_code = spi_slave_buffers_set(&m_registres[address], m_rx_buf, 1, 1); // We send it
}
break;
case 1: // Second state: receive data
if(!dataEnd) // dataEnd changing state when CSN is rised
{
m_registres[address] = m_rx_buf[0];
address++;
}
else
{
state = 0;
dataEnd = 0;
}
break;
}
}
}
Thanks a lot! :)