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

Help with m_aci_spi_transfer function in hal_aci_tl.cpp file [nrf8001]

hello everyone

I have some questions with the following function in the hal_aci_tl.cpp file

static bool m_aci_spi_transfer(hal_aci_data_t * data_to_send, hal_aci_data_t * received_data)

what is happening between line 9 and 13? Is the receiving packet an event on what has ben sent?

And why is the sending en receiving separated because on line 31 the rest of the packet fallows.

Any and all help is much appreciated

1 static bool m_aci_spi_transfer(hal_aci_data_t * data_to_send, hal_aci_data_t * received_data)
2 {
3  uint8_t byte_cnt;
4  uint8_t byte_sent_cnt;
5  uint8_t max_bytes;
6
7  m_aci_reqn_enable();
8
9  // Send length, receive header
10 byte_sent_cnt = 0;
11 received_data->status_byte = spi_readwrite(data_to_send->buffer[byte_sent_cnt++]);
12 // Send first byte, receive length from slave
13 received_data->buffer[0] = spi_readwrite(data_to_send->buffer[byte_sent_cnt++]);
14 if (0 == data_to_send->buffer[0])
15 {
16   max_bytes = received_data->buffer[0];
17 }
18 else
19 {
20   // Set the maximum to the biggest size. One command byte is already sent
21   max_bytes = (received_data->buffer[0] > (data_to_send->buffer[0] - 1))
22                                         ? received_data->buffer[0]
23                                         : (data_to_send->buffer[0] - 1);
24 }
25
26 if (max_bytes > HAL_ACI_MAX_LENGTH)
27 {
28   max_bytes = HAL_ACI_MAX_LENGTH;
29 }
30
31 // Transmit/receive the rest of the packet
32 for (byte_cnt = 0; byte_cnt < max_bytes; byte_cnt++)
33 {
34   received_data->buffer[byte_cnt+1] =  spi_readwrite(data_to_send->buffer[byte_sent_cnt++]);
35 }
36
37 // RDYN should follow the REQN line in approx 100ns
38 m_aci_reqn_disable();
39
40 return (max_bytes > 0);
41}
  • SPI is a bi-directional protocol , you can see it like a shift register i.e. when 1 byte is clocked over the MOSI from the master, the slave clocks 1 byte over MISO.

    Since we are now receiving messages from the SPI Slave and sending from the SPI master, we need to generate a SPI clock that is sufficient to get the largest of the message.

    To determine the larger of the messages (i.e. master to slave message or slave to master message) we need to get the length byte. The master to slave message has the length as the first byte. The slave to master message has the length as the second byte. This means 2 bytes need to clocked out from the slave before a decision can be made about the total number of clocks.

    This is done in lines 9 to 13 in the code snippet.

    See section 7 in the nRF8001 datasheet for the details about the message format and the SPI/ACI

  • Thank you for your quick response.

    In section 7.1.5 in the nRF8001 datasheet the second byte comming from the slave is indeed the length byte like you said. Doe you know the meaning of the first byte that is sent from the slave (received_data->status_byte).? In the datasheet section 7.1.5 they talk about an internal debug byte thats not being used by the application controller. Doe you know the meaning of this debug/status byte?

    thanks

Related