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

Why does this ble_app_uart code crash if ble_nus_string_send() is not called?

I am running ble_uart_app in SDK_11 s130 on nRF51-dk

I am trying to detect certain pattern received in the UART Rx.

Here is my code.

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
            {
				//search for beginning bytes "AB," 
				if (data_array[0] == 'A' && data_array[1] == 'B' && data_array[2] == ',')
				{
					//process commands
                    //If below line is commented, firmware will crash when code enters here	
					//err_code = ble_nus_string_send(&m_nus, data_array, index);
				}
				else
				{
                	err_code = ble_nus_string_send(&m_nus, data_array, index);
				}
				
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                
                index = 0;
            }
            break;

To prevent the code from crashing, for some reason, ble_nus_string_send() has to be run all the time inside the if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN))) statement. Otherwise, code will crash. I am very puzzled. Why is it necessary to run ble_nus_string_send()?

Parents
  • well you haven't initialized err_code so it's random garbage going through that branch of the if. So ble_nus_send_string() doesn't need to be called, it just happens that a side effect of calling that sets err_code to a valid value and thus stops the APP_ERROR_CHECK() failing your code 2 lines later.

    And by the way 'code crashes' really isn't a very good description of the problem. That could mean you're in an error handler, or in the hardfault handler, or stuck in a loop or have a interrupt you're not handling. If you'd had a breakpoint in the error handler, which is constantly recommended, you'd have had a stack trace showing where you failed which I've assumed is in the APP_ERROR_CHECK() of err_code.

    I'd suggest turning your compiler warning level up, any decent compiler will warn about using uninitialised variables like that.

  • that rather depends whether you want to succeed or fail, since you want to succeed, probably NRF_SUCCESS. Or just move the test inside that the branch of the if which sets err_code instead of having it afterwards.

Reply Children
No Data
Related