This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPI & Bluetooth best practices

Hi All,

I'm a little rusty on my embedded C and was wondering if I could get a few broad pointers on the best way to organize this research system I'm putting together.

The system is basic: I have an NRF52840 that talks to a custom IC via SPI and then sends whatever it reads out to an NRF52 dongle via bluetooth. If this was one way then it would be especially straight forward but unfortunately, the NRF52840 can also receive commands from the dongle (over bluetooth) that may or may not trigger specific SPI transactions. It seems to me that the best way to organize the NRF52840 (that's talking to the custom IC) as the peripheral and the dongle as the central device. This way the peripheral can use an interrupt to query the IC and then notify the central device with a new packet.

I'm having a little more trouble with the other main function though (dongle sending commands to the peripheral) though since it's really important that this bluetooth connection runs as fast as possible without dropping any packets.

Is it fair to have a main while loop on all my devices that spins (just checking if there's been an incoming command) when they're in their main interrupt-driven operating mode. Then if there has been a command, they break out of their interrupt-driven mode and work on whatever the command is? I worry that this constant spinning may burn unnecessary power and also may slow down the main BLE operation (which needs to run as fast as possible!). Are there other alternatives that may allow the bluetooth and spi to operate as fast as possible in parallel?

Thanks so much for any advice/help. If anything was unclear please let me know and I can clarify. I tried to distill everything to not be too confusing but I may have distilled too much.

- Ryan

Parents
  • Hi Karl, 

    This was incredibly helpful! I'll play with the SPIM peripheral/drivers and take a look at the SAADC example! 

    I have one more question regarding best practices - this time with my 'main mode of operation'. Eventually, I will have a timer triggering a SPI transaction every 500us. The resulting data from this SPI transaction will then be beamed via bluetooth.

    To make things simpler for now, I am leaving the SPI out and just using my 500us timer to increment a counter. I then have an if statement in my main while loop that sends the updated counter to another device over bluetooth. Is this the best way to organize fast transactions through bluetooth? I must be doing something wrong because I'm losing a lot of data (I suspect the bluetooth transactions are taking too long - or the cpu is getting halted for too long so the counter increments more than once between bluetooth transfers. I tried putting the ble_nus_data_send() command in my timer's interrupt handler but that just caused nrf52840 to hang and become unresponsive.

    I'm currently building off of the ble_app_uart examples.

    Thanks again for all of your help!

    Ryan

  • Karl!

    Hi Karl, 

    I've been able to get something to work but it still can't run at max speed. Per your suggestion, I've set up the Bluetooth transfer inside the timer's interrupt handler. I fixed one problem - I had a 'while (ble_ready == success)' gating the ble_nus_data_send - removing that helped things a little. I figure this is okay because I'm under the assumption the timer will always leave enough time for the ble_nus_data_send to do its thing. My timer's interrupt handler is below:

     

    static void trx_timer_handler(void * p_context)
    {
      // fire off a SPI transaction
    
      // For now, just send an incrementing counter over BLE
      if (test_counter == 15) {
        test_counter = 0;
      } else {
        test_counter = test_counter + 1;
      }
      
    
      //app_fifo_put(&EE_rx_fifo, test_counter);
    
      if ((ble_ready == NRF_SUCCESS)) { // & (app_fifo_get(&EE_rx_fifo, EE_transfer_buffer) == NRF_SUCCESS)){
        // uint16_t size2 = sprintf(m_tx_buffer, "%d \r\n", *EE_transfer_buffer);
        uint16_t size2 = sprintf(m_tx_buffer, "%d \r\n", test_counter);
        ble_ready = ble_nus_data_send(&m_nus, (uint8_t *) m_tx_buffer, &size2, m_conn_handle);
      }
    }

    This works if I slow the timer down - if it triggers once every 3ms I don't drop any packets. Unfortunately, when I speed the timer up to trigger every 500us (which is my target), I start "dropping" packets. I suspect this is because I'm effectively trying to queue notifications faster than I can send them.

    I'm using most of the default connection parameters from the ble_app_uart example! The only ones I changed were the min and max_conn_interval:

    #define MIN_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)             /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
    #define MAX_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)             /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */

    Are there any other connection parameters you think I should update to speed up the bluetooth speed? I can't find any BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT in the project or ble_gatt.h. Is this something I need to implement? Is there an example that implements this? I'm currently using NRF5 sdk 17 with the nrf52840 DK.

    As always, Karl, thank you so much for your help.

Reply
  • Karl!

    Hi Karl, 

    I've been able to get something to work but it still can't run at max speed. Per your suggestion, I've set up the Bluetooth transfer inside the timer's interrupt handler. I fixed one problem - I had a 'while (ble_ready == success)' gating the ble_nus_data_send - removing that helped things a little. I figure this is okay because I'm under the assumption the timer will always leave enough time for the ble_nus_data_send to do its thing. My timer's interrupt handler is below:

     

    static void trx_timer_handler(void * p_context)
    {
      // fire off a SPI transaction
    
      // For now, just send an incrementing counter over BLE
      if (test_counter == 15) {
        test_counter = 0;
      } else {
        test_counter = test_counter + 1;
      }
      
    
      //app_fifo_put(&EE_rx_fifo, test_counter);
    
      if ((ble_ready == NRF_SUCCESS)) { // & (app_fifo_get(&EE_rx_fifo, EE_transfer_buffer) == NRF_SUCCESS)){
        // uint16_t size2 = sprintf(m_tx_buffer, "%d \r\n", *EE_transfer_buffer);
        uint16_t size2 = sprintf(m_tx_buffer, "%d \r\n", test_counter);
        ble_ready = ble_nus_data_send(&m_nus, (uint8_t *) m_tx_buffer, &size2, m_conn_handle);
      }
    }

    This works if I slow the timer down - if it triggers once every 3ms I don't drop any packets. Unfortunately, when I speed the timer up to trigger every 500us (which is my target), I start "dropping" packets. I suspect this is because I'm effectively trying to queue notifications faster than I can send them.

    I'm using most of the default connection parameters from the ble_app_uart example! The only ones I changed were the min and max_conn_interval:

    #define MIN_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)             /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
    #define MAX_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)             /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */

    Are there any other connection parameters you think I should update to speed up the bluetooth speed? I can't find any BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT in the project or ble_gatt.h. Is this something I need to implement? Is there an example that implements this? I'm currently using NRF5 sdk 17 with the nrf52840 DK.

    As always, Karl, thank you so much for your help.

Children
  • Hello again,

    ryerye120 said:
    As always, Karl, thank you so much for your help.

    It is no problem at all, Ryan, I am happy to help!

    ryerye120 said:
    I'm under the assumption the timer will always leave enough time for the ble_nus_data_send to do its thing.

    The ble_nus_data_send function does not actually send the data directly, but it queues the notification for sending (placing it in the hvn queue). The queue is FIFO, and the notification will be sent as soon as there is room for it in an upcoming connection event.
    - This is why it is important that your queue size is at minimum large enough to hold all notifications you intend to generate over the span of your connection interval. I say at minimum because you also need to account for possible retransmission due to corrupted packets or otherwise lost packets (data is never lost in the link since the un-acknowledged packet will be retransmitted).

    ryerye120 said:
    I've set up the Bluetooth transfer inside the timer's interrupt handler.

    The ble_nus_data_send call could also happen as part of the SPI event handler, so that you do not have to wait in the timer interrupt for the SPI transaction to complete before you can queue the data for transfer, for instance.

    ryerye120 said:
    The only ones I changed were the min and max_conn_interval:

    This is indeed how you change the connection interval preferences of the peripheral - but keep in mind that it is always the central that actually determines what connection parameters will be used. The peripheral may only send connection parameter update requests in order to ask the central to change to parameters within its preferences, but the central is free to reject these requests.
    If the central rejects the peripheral can either accept the parameters used by the central, or terminate the link. Many applications such as nRF Connect for smartphone etc. will accommodate any preferences, but some native applications might not in order to save power on the central device. Certain phone OS's especially limit this, in order to avoid excess power consumption.

    ryerye120 said:
    I can't find any BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT in the project or ble_gatt.h.

    Does it not exist in your sdk_config.h file? The sdk_config is used to set all these types of defines and configurations for your project in the nRF5 SDK.

    ryerye120 said:
    Are there any other connection parameters you think I should update to speed up the bluetooth speed?

    You could take a look at the SoftDevice's throughput documentation for this - it details the parameters used to achieve the highest connection throughput. Have you calculated what throughput you will need for your application? Is there a constraint on the latency of the transfers?

    Best regards,
    Karl

  • Hello again, Ryan

    I just noticed that you do not immediately check the error code returned from ble_nus_data_send. I strongly recommend that you always pass the returned error code directly to an APP_ERROR_CHECK in order to be alerted to any possible failings by the function. There might be an unexpected condition or situation that makes a function call fail, and checking the returned error code is the only way you may know that this has happened.

    This is likely also the reason why you might be seeing 'lost data' - if the call to queue data for sending fails, and your application proceeds as if it succeeded, it will discard the data and move on to the next.
    The returned error codes will also let you know exactly how to remedy the issue (by looking up the returned error code in the function's API reference) if you have DEBUG defined in your preprocessor defines, like shown in the included image:

    This will make a detailed error message be outputted by your logger whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.

    Best regards,
    Karl

  • Hi Karl,

    The queue is FIFO, and the notification will be sent as soon as there is room for it in an upcoming connection event.

    Ahhh this makes a lot of sense. This seems like a likely issue because BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT isn't defined in my sdk_config.h either. I'm assuming this is a peripheral device thing since it has to do with notifications (which is what I'm working on right now).


    You could take a look at the SoftDevice's throughput documentation for this - it details the parameters used to achieve the highest connection throughput. Have you calculated what throughput you will need for your application? Is there a constraint on the latency of the transfers?

    Yes! Ultimately I'll need to hit about 1Mbps so my goal is to transfer about 500 bits every 500 microsec (which means I should be transferring about 1000 bits total in every millisecond -> 1000kbps). From my understanding/corroborated by the link, I need to make sure my central device sets the PHY to 2Mbps. It should be that way already - is there a way to ask the peripheral to print what the PHY setting is via a debug statement?


    I just noticed that you do not immediately check the error code returned from ble_nus_data_send. I strongly recommend that you always pass the returned error code directly to an APP_ERROR_CHECK in order to be alerted to any possible failings by the function.

    Ah great point! I've added an app error check and the pertinent code is now:

      if ((ble_ready == NRF_SUCCESS)) { // & (app_fifo_get(&EE_rx_fifo, EE_transfer_buffer) == NRF_SUCCESS)){
        // uint16_t size2 = sprintf(m_tx_buffer, "%d \r\n", *EE_transfer_buffer);
        uint16_t size2 = sprintf(m_tx_buffer, "%d \r\n", test_counter);
        ble_ready = ble_nus_data_send(&m_nus, (uint8_t *) m_tx_buffer, &size2, m_conn_handle);
        APP_ERROR_CHECK(ble_ready);
      }
     

    As you expected, the device starts to hang and I get thrown into an error. Unfortunately, I'm having a really hard time finding a stack trace that gives me more info. This is what I see in segger:

    I figure the real error I need to fix is 0x00730075 but I can't figure out what this error is. So far I've looked through sdk_errors.h, nrf_error.h, and google and haven't been able to understand. Clearly, I don't understand the error hierarchy - is there a more detailed guide somewhere describing these? I've been trying to go through the appropriate nrf52 s140 documentation infocenter but if anything I'm more confused.

    As a side note:

    The ble_nus_data_send call could also happen as part of the SPI event handler, so that you do not have to wait in the timer interrupt for the SPI transaction to complete before you can queue the data for transfer, for instance.

    That's encouraging to hear. When I add the SPI driver into this project, I'll shift everything just to the spi trx' interrupt handler!

  • ryerye120 said:
    From my understanding/corroborated by the link, I need to make sure my central device sets the PHY to 2Mbps. It should be that way already - is there a way to ask the peripheral to print what the PHY setting is via a debug statement?

    Yes, using the 2M PHY is important to achieve a high throughput.
    If you scan and advertise on the 2M PHY the connection will also use 2M PHY. If you wish you could print something in the PHY UPDATED event, in the case that this should change at any time. What are you currently using as your central?
    The best tool for debugging connectivity and throughput issues is the nRF Sniffer tool which lets you monitor on-air BLE traffic. Are you familiar with this tool already?
    In case you are not you can find its documentation here. It is a powerful tool to wield when developing with BLE.

    ryerye120 said:
    As you expected, the device starts to hang and I get thrown into an error. Unfortunately, I'm having a really hard time finding a stack trace that gives me more info. This is what I see in segger:

    Great - this is the first step in resolving the issue. Could you confirm for me that you've added the DEBUG define in the common build configuration, as mentioned earlier? I notice that your logger is not outputting a complete error message.

    ryerye120 said:
    That's encouraging to hear. When I add the SPI driver into this project, I'll shift everything just to the spi trx' interrupt handler!

    Great, I'm happy to hear that you found the comment encouraging! Slight smile

    Best regards,
    Karl

  • EDIT: sorry for the double post, I accidentally 'replied' to the wrong message so I copied my response to here so it's easier for you/others to follow.

    Hi Karl,

    What are you currently using as your central?

    I'm using an nrf52 dongle that's acting like a BLE<->USB pipe. I built this based off of the ble_uart_c example and took stuff from the usbd_ble_uart peripheral when needed. 

    The best tool for debugging connectivity and throughput issues is the nRF Sniffer tool which lets you monitor on-air BLE traffic. Are you familiar with this tool already?

    I've heard of it but haven't used it - I'll order another dev kit and set a sniffer up! I completely forgot this was a thing.

    Great - this is the first step in resolving the issue. Could you confirm for me that you've added the DEBUG define in the common build configuration, as mentioned earlier? I notice that your logger is not outputting a complete error message.

    Oh you were spot on - I added it to the release build config. This time I added it to the projects common build config and I'm getting a proper error out now.

    <error> app: ERROR 19 [NRF_ERROR_RESOURCES] at C:\Users\...\main.c:235
    PC at: 0x0002B1F5
    <error> app: End of error report

    Now after some googling I found this thread where someone seems to have the same problem and the issue is - you guessed it "Too many notifications queued."  For that user it seems to have boiled down to their notification queue being too small but while trying to learn how they've changed it I've come across a few new questions:

    • The linked thread implies that this queue size is part of the BLE stack. If that's the case the BLE 'stack' includes not just the BLE drivers but also the memory interface between the CPU & the drivers. Is that right? 
    • In ble_stack_init() (in main.c), there is nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start); The linked thread implies this sets a default queue size to  BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT (which is 1). Firstly, is there a max queue size? Ideally i'd just set it to the max using sd_ble_cfg_set() - BUT I can't find a single reference to sd_ble_cfg_set() in the existing solution. Do you have any pointers to where I may be able to find an example so I can safely call sd_ble_cfg_set() with a new queue size?
    • Now, assuming I get that set, in order to maximize my throughput, I can set my connection interval to as small as possible (7.5ms, right?), maximize my MTU (to 251, right?), set my PHY to 2M, and then set my total number of links to just 1 (so that I don't have to share bandwidth). Once I get a sniffer up and running, I'll hopefully be able to confirm that all of that is the case after trying to test it out.

    As always, thank you for your patience and help! I realize a lot of these are basic questions - I can't describe how appreciative I am of your time/effort.

    Kindest regards,

    Ryan

Related