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

Connection timeout at central

Hi,

I'm using the example "ble_central/ble_app_uart_c" of nRF5_SDK_17.0.2. Connection to peripheral workes fine.

If the central re-starts while a connection is established, the peripheral get a "disconnect" event and wait for a new connection.

If the peripheral re-starts while a connection is established, the central get no "disconnect" event and runs into fatal error.

So, how to set up central, to handle a "disconnect" by timeout?

Thanks for helping!

Parents
  • Hello,

    I'm using the example "ble_central/ble_app_uart_c" of nRF5_SDK_17.0.2.

    Have you made any changes to the central example? If so, what changes have you made?

    the central get no "disconnect" event and runs into fatal error.

    That sounds strange - the central should resume scanning for a peripheral device when a link is broken.
    Could you make sure to have DEBUG defined in your preprocessor defines, like shown in the included image?

    This will make a detailed error message be outputted to your logger whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK. Please do this, and let me know what this error message says when the device resets.

    Looking forward to resolving this issue together!

    Best regards,
    Karl

  • Hello Karl,

    thank you for your soon response. First the error message:

    <info> app_timer: RTC: initialized.
    <info> app: BLE UART central example started.
    <info> app: Connecting to target 340D3750ACE0
    <info> app: ATT MTU exchange completed.
    <info> app: Ble NUS max data length set to 0xF4(244)
    <info> app: Discovery complete.
    <info> app: Connected to device with Nordic UART Service.
    <error> nrf_ble_gq: SD GATT procedure (1) failed on connection handle 0 with error: 0x00000013.
    <error> app: ERROR 19 [NRF_ERROR_RESOURCES] at C:\Projekte\MyCompany\0288_TT_BLE_Modul\nRF5_SDK_17.0.2_d674dde\examples\ble_central\ble_app_uart_c\main.c:123
    PC at: 0x0002F57D
    <error> app: End of error report

    I have made some "small" changes in main.c:

    • set ECHOBACK_BLE_UART_DATA to 0
    • in void uart_event_handle(app_uart_evt_t * p_event):
      Check connection state befor sending data
          if (m_ble_nus_c.conn_handle != BLE_CONN_HANDLE_INVALID)
          {
              switch (p_event->evt_type)
              {
                  case APP_UART_DATA_READY:
                      ...
              }
          }

    Here is some background information:

    The BLE module is connected to our µC (STM32) via UART. This STM32 sends a heartbeat every 250ms via uart -> BLE to the peripheral - indipending of a BLE connection state. If there is no connection established, the central runs into a fatal error. Therefore we have checked the connection handle before sending the data via BLE.

  • Hi Karl,

    we had different problems.

    One of them was that our controller sends data cyclically - regardless of whether there is a BLE connection or not. When we debug the BLE module, the situation arises that the BLE stack is not yet fully set up while the STM32 is already trying to send data via UART. This leads to an APP_ERROR_HANDLER.

    The disconnect event is generally recognized. However, an APP_ERROR_HANDLER was previously executed. We have to intercept this troubleshooting in a different way than has been done up to now. When we do this, it works to disconnect and reconnect the two devices.

    It may be that our queue is too small, but we currently cannot simply increase it.

    Many thanks for your support!

    Regards,

    Andi

  • Hello again Andi,

    Andi_Frueh said:
    the BLE stack is not yet fully set up while the STM32 is already trying to send data via UART. This leads to an APP_ERROR_HANDLER.

    The initialization and enabling of the BLE stack does not take very long, and the nRF52833 can still receive over UART regardless of the BLE stack status. Which error is generated, and from which function, when the STM32 sends data too soon?

    Andi_Frueh said:
    The disconnect event is generally recognized. However, an APP_ERROR_HANDLER was previously executed. We have to intercept this troubleshooting in a different way than has been done up to now. When we do this, it works to disconnect and reconnect the two devices.

    I am sorry, but I do not understand what you mean to say here fully. Are you saying that you do not see the DISCONNECTED event when a APP_ERROR_HANDLER execution has just finished? Have you implemented specific error handling in the APP_ERROR_HANDLER, or are you using the default which is to reset the device?
    Please elaborate on what you mean with this paragraph so I may better advice you on how to resolve the issue. 

    Andi_Frueh said:
    It may be that our queue is too small, but we currently cannot simply increase it.

    I do not understand what you mean by this. Why can you simply not increase the queue, if this is what is causing your NRF_ERROR_RESOURCES?
    The only alternative to increasing the queue to avoid the NRF_ERROR_RESOURCES would be to queue notifications less frequently. For example, only after receiving a BLE_GATTS_EVT_HVN_TX_COMPLETE event.

    Best regards,
    Karl

  • Hello Karl,

    sorry for the late response.

    The initialization and enabling of the BLE stack does not take very long, and the nRF52833 can still receive over UART regardless of the BLE stack status. Which error is generated, and from which function, when the STM32 sends data too soon?

    In Debug-Build I get following error message:

    In line 305 of main.c is the following function:

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
        static uint16_t index = 0;
        uint32_t ret_val;
    
        switch (p_event->evt_type)
        {
            /**@snippet [Handling data from UART] */
            case APP_UART_DATA_READY:
                ...
                break;
    
            /**@snippet [Handling data from UART] */
            case APP_UART_COMMUNICATION_ERROR:
                index = 0;
                NRF_LOG_ERROR("Communication error occurred while handling UART.");
    
    /* line 305 */
                APP_ERROR_HANDLER(p_event->data.error_communication);
    
                break;
    
            case APP_UART_FIFO_ERROR:
                index = 0;
                NRF_LOG_ERROR("Error occurred in FIFO module used by UART.");
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                break;
        }
    }
    

    Whe I stopp the debugger before entering the APP_ERROR_HANDLER, the call stack changes to

    Since the error occurs while function "ble_stack_init()" is running, I suspected that the problem was in the area of initialization.

    I am sorry, but I do not understand what you mean to say here fully. Are you saying that you do not see the DISCONNECTED event when a APP_ERROR_HANDLER execution has just finished?

    If I reset the peripheral while there is a BLE connection and the cyclic heartbeat is being sent, I get the following error:

    Since we use the default APP_ERROR_HANDLER, application stopps and has to be reset! How should we handle this error?

    I do not understand what you mean by this. Why can you simply not increase the queue, if this is what is causing your NRF_ERROR_RESOURCES?

    The project we are working on is a safety application. The only message sent via BLE is a heartbeat (every 250 ms). If this heartbeat does not occur, the machine must be switched off immediately.

    If there is a corresponding error, the Central no longer sends a heartbeat and thus wants to force shutdown of the machine.

    If we handle messages in a queue, heartbeats could accumulate in the event of a disturbed transmission and lead to a delayed shutdown. That would be unacceptable!

    Kind regards,
    Andi

  • Hello Andi,

    Andi_Frueh said:
    sorry for the late response.

    No problem at all - we continue whenever you have the time, no worries.

    Andi_Frueh said:
    In line 305 of main.c is the following function:

    Exactly which APP_ERROR_CHECK is at line 305?
    Could it here be that you have unitialized the UART peripheral while not in a connection without having disabled related UART functionality in the application, or similar?

    Andi_Frueh said:
    If I reset the peripheral while there is a BLE connection and the cyclic heartbeat is being sent, I get the following error:

    Which function returned the NRF_ERROR_RESOURCES passed to the APP_ERROR_CHECK on line 119?

    Andi_Frueh said:
    Since we use the default APP_ERROR_HANDLER, application stopps and has to be reset! How should we handle this error?

    You could implement specific error handling for specific errors. The default error handling is to reset the device - no matter which error has occurred - this is not always necessary, but depends on your application. For example when the NRF_ERROR_RESOURCES is returned by the sd_ble_gatts_hvx call, that only means that the queue for notifications to send is already full. This is therefore then not a good reason to reset the application.
    In this case, a more fitting error handling would be to note which data was not successfully queued, and instead try to queue it again later, after having received a hvn tx event (notification successfully sent event). This way, you make sure not to restart the device unnecessarily, and not to loose any data that is not immediately queued, for example.

    Andi_Frueh said:

    The project we are working on is a safety application. The only message sent via BLE is a heartbeat (every 250 ms). If this heartbeat does not occur, the machine must be switched off immediately.

    If there is a corresponding error, the Central no longer sends a heartbeat and thus wants to force shutdown of the machine.

    If we handle messages in a queue, heartbeats could accumulate in the event of a disturbed transmission and lead to a delayed shutdown. That would be unacceptable!

    Thank you for elaborating on the requirements and constraints of the project - this makes it much easier for me to understand your issues and help you resolve them.

    Best regards,
    Karl

  • Hi Karl,

    Exactly which APP_ERROR_CHECK is at line 305?

    Could it here be that you have unitialized the UART peripheral while not in a connection without having disabled related UART functionality in the application, or similar?

    Since call stack shows, that the program counter has started in function ble_stack_init, uart would be already initialized:

    int main(void)
    {
        // Initialize.
        log_init();
        timer_init();
        uart_init();
        buttons_leds_init();
        db_discovery_init();
        power_management_init();
        ble_stack_init();
        gatt_init();
        nus_c_init();
        scan_init();
    
        // Start execution.
        printf("BLE UART central example started.\r\n");
        NRF_LOG_INFO("BLE UART central example started.");
        scan_start();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    

    Which function returned the NRF_ERROR_RESOURCES passed to the APP_ERROR_CHECK on line 119?

    in file nrf_ble_gq.c the function sd_ble_gattc_write returnesthe NRF_ERROR_RESOURCES.

    Could you handle with that informations?

    Best regards,
    Andi

Reply
  • Hi Karl,

    Exactly which APP_ERROR_CHECK is at line 305?

    Could it here be that you have unitialized the UART peripheral while not in a connection without having disabled related UART functionality in the application, or similar?

    Since call stack shows, that the program counter has started in function ble_stack_init, uart would be already initialized:

    int main(void)
    {
        // Initialize.
        log_init();
        timer_init();
        uart_init();
        buttons_leds_init();
        db_discovery_init();
        power_management_init();
        ble_stack_init();
        gatt_init();
        nus_c_init();
        scan_init();
    
        // Start execution.
        printf("BLE UART central example started.\r\n");
        NRF_LOG_INFO("BLE UART central example started.");
        scan_start();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    

    Which function returned the NRF_ERROR_RESOURCES passed to the APP_ERROR_CHECK on line 119?

    in file nrf_ble_gq.c the function sd_ble_gattc_write returnesthe NRF_ERROR_RESOURCES.

    Could you handle with that informations?

    Best regards,
    Andi

Children
  • Hello again, Andi

    Andi_Frueh said:
    In Debug-Build I get following error message:

    Thank you for clarifying.

    There is a issue with the logging of the UART communication error in the default error handler. It assumes that the error code is a standard nRF error code, but in fact the error code you get in case of a APP_UART_COMMUNICATION_ERROR is the content of the ERRORSRC register. So value 1 is not NRF_ERROR_SVC_HANDLER_MISSING, but rather an overrun error (1, i.e. the least significant bit is the OVERRUN field in ERRORSRC: "A start bit is received while the previous data still lies in RXD").

    It would seem that for some reason you are not able to process the data fast enough in some cases. Perhaps you get a interrupt at a "bad time", or something else. The best way to handle this is probably to use flow control if possible.

    Andi_Frueh said:

    in file nrf_ble_gq.c the function sd_ble_gattc_write returnesthe NRF_ERROR_RESOURCES.

    Could you handle with that informations?

    You can check returned error messages against the function API to read why this particular error message was returned.
    In the case of sd_ble_gattc_write, the description for the NRF_ERROR_RESOURCES reads:

    NRF_ERROR_RESOURCES Too many writes without responses queued. Wait for a BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE event and retry.

    It seems to me that you are queueing the writes faster than they are being written, leading to an overflow of the queued writes buffer.
    Please read the note section of the function's API to see how you may use the CMD_TX_COMPLETE event to queue more write without responses.
    How fast are you queueing writes, compared to how fast they are being sent? What connection interval are you using?

    Best regards,
    Karl

  • Hello again,

    APP_UART_COMMUNICATION_ERROR

    When this error occurs, I see in call stack, that the initialization of application code isn't finished. The application code is still handling the function ble_stack_init(). While executing this function, nrf_sdh_enable_request() will be called, where a critical section ist entered and exited. While exit this critical section, the UARTE0_UART0_IRQHandler() is called immideatelly, which ends in the APP_UART_COMMUNICATION_ERROR.

    Even if the dedicated STM32 sends its 250ms heartbeat via UART while the Nordic chip is starting its firmware (start debug session): how can it be that an overflow occurs immediately at this point every time? How can I prevent this overflow?

    It seems to me that you are queueing the writes faster than they are being written, leading to an overflow of the queued writes buffer.
    Thisis quite possible. The Central needs approx. 4 seconds until it detects that the connection to the Peripheral has been broken. During this time the heartbeat continues to be sent every 250ms.
    Is it possible that the interplay between the
    • heartbeat cycle (250ms),
    • the duration for detecting the disconnection (approx. 4 Sec.) and
    • the size of the message queue (?)

    has been selected as "unfavorable"? Can I adjust disconnection time?

  • Hi Karl,

    we tried something to find out the interacting of

    • heartbeat cycle
    • disconnection time and
    • queue size.
    We found the place where the dosconnection time can be set. If we reduce this time (from 4000ms to 1000ms), a disconnection will be recognized before the queue overflows.
    Unfortunately we have not found the place where to adjust the size of the queue. Can this also be found in the sdk_config?
  • Hello again,

    the other issue with the APP_UART_COMMUNICATION_ERROR while initialization seems also to be fixed (by a workaround): We have changed the order of the initialization sequence and have moved uart_init() behind the ble_stack_init().

    While doing so, we have no APP_UART_COMMUNICATION_ERROR  anymore.

    While initialization the uart interrupts are already enabled, although the BLE stack has not yet been initialized. This obviously leads to this error.

    What better solution here than just changing the initialization order?

  • Hello again Andi,

    Thank you for your patience.

    Andi_Frueh said:
    We found the place where the dosconnection time can be set. If we reduce this time (from 4000ms to 1000ms), a disconnection will be recognized before the queue overflows.

    Yes, the connection timeout can be adjusted to fit the specific application. 
    You should also consider the environment that the device will be working in when configuring this, because packet loss or corruption happens more frequently in a 2.4 GHz noisy environment, or in an environment in which the peripheral and central moves behind obstacles and further away from each other. What connection interval are you using currently?
    The connection interval is what determines how often there is scheduled communication between the two devices, so for example if you need your central to stop a machine as fast as possible if a heartbeat is not received you can likely set the connection timeout even lower if you are using a low connection interval, such as 7.5 ms.

    Andi_Frueh said:
    Unfortunately we have not found the place where to adjust the size of the queue. Can this also be found in the sdk_config?

    The size of the queue, are you here talking about the UARTE RX buffer, or the hvn_tx_queue_size?

    Andi_Frueh said:
    What better solution here than just changing the initialization order?

    Is it a possibility for you to use flow control?
    This should negate this issue all together.
    Alternatively, perhaps your STM could wait until it receives some kind of go-ahead signal before it starts sending the heartbeats. This could for example be a start command sent over UART, or similar.

    Best regards,
    Karl

Related