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

ble_app_beacon example stucking after once advertise.

Hello, 

I successfully merge example libuarte and ble_app_beacon and built .

My problem started after it receive 48 bytes of data  from sensor via uart ,This is my area of doubt .

In nrf connect android app I am able to see data but it is not changing .

For verification I try to debug and in debug terminal also I found it is advertising once.

for reference ,I am adding debug terminal snapshot below.

In debug terminal one can clearly see that it is running but not going to update function.

I am attaching my uart read and advertise update function below.

void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_evt)
{
    nrf_libuarte_async_t * p_libuarte = (nrf_libuarte_async_t *)context;

    
    ret_code_t ret;

    switch (p_evt->type)
    {
        case NRF_LIBUARTE_ASYNC_EVT_ERROR:
            bsp_board_led_invert(0);
            break;
        case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
            
            strcat(rx_buffer,p_evt->data.rxtx.p_data);
            
            nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
            
            break;
        
        default:
            break;
    }
}

static void adv_update_timeout_handler(void * p_context)
{
   
    UNUSED_PARAMETER(p_context);

    uint32_t err_code;
    static uint32_t major_value;
    static uint32_t minor_value;
    static uint32_t raw_value;
    static uint8_t raw_1,raw_2,raw_3,raw_4,raw_5;
    static uint8_t val,val1,val2,val3,val4,val5,val6,val7;

    uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;

    m_adv_data.adv_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;

    /* Increment the Beacon's Major value as a test to demonstrate that we can update payload
       while advertising */
    raw_1=rx_buffer[2]-48;
    raw_2=rx_buffer[3]-48;
    raw_3=rx_buffer[4]-48;
    raw_4=rx_buffer[5]-48;
    raw_5=rx_buffer[6]-48;
    raw_value = ((raw_1*10000)+(raw_2*1000)+(raw_3*100)+(raw_4*10)+raw_5);
    raw_value = raw_value/8;    

    val=rx_buffer[22]-48;
    val1=rx_buffer[23]-48;
    val2=rx_buffer[24]-48;
    val3=rx_buffer[25]-48;
    major_value = ((val*1000)+(val1*100)+(val2*10)+val3);
    //major_value = (major_value/16);
    val4=rx_buffer[10]-48;
    val5=rx_buffer[11]-48;
    val6=rx_buffer[12]-48;
    val7=rx_buffer[13]-48;
    minor_value = ((val4*1000)+(val5*100)+(val6*10)+val7);
    //minor_value = minor_value/128;
    
    m_beacon_info[index++] = MSB_16(raw_value);
    m_beacon_info[index++] = LSB_16(raw_value);

    m_beacon_info[index++] = MSB_16(major_value);
    m_beacon_info[index++] = LSB_16(major_value);

    m_beacon_info[index++] = MSB_16(minor_value);
    m_beacon_info[index++] = LSB_16(minor_value);

    /* Swap adv data buffer - from API doc: "In order to update advertising
    data while advertising,new advertising buffers must be provided" */
    m_adv_data.adv_data.p_data =
        (m_adv_data.adv_data.p_data == m_enc_advdata[0]) ? m_enc_advdata[1] : m_enc_advdata[0];


    err_code = ble_advdata_encode(&m_adv_data_params, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, NULL);
    APP_ERROR_CHECK(err_code);
    //memset(rx_buffer,0,sizeof(rx_buffer));
    NRF_LOG_INFO("Advertising payload updated. New Major value is 0x%x", major_value);

}

Thanks in advance for your time and efforts.

Ram

  • Hello,

    Would you mind uploading the whole project? It would be easier to figure out what is wrong if I could debug it here.

    Thanks,

    Vidar

  • Hello Vidar,

    Would you mind uploading the whole project? It would be easier to figure out what is wrong if I could debug it here.

    Sure ,I am attaching the .zip file of the project.

    ble_app_beacon_sensor data.zip

    I have strongly doubt on UART_Handler  please review 1st this part because my sensor giving 48 bytes of data in every seconds.

    Thanks,

    Ram

  • Hello Ram,

    I found 2 problems with your code.

    1. strcat will just keep appending characters to your rx_buffer until it eventually overflows if you are not null terminating your string. You could try something like this to prevent it from happening:

    void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_evt)
    {
        nrf_libuarte_async_t * p_libuarte = (nrf_libuarte_async_t *)context;
    
        uint16_t string_len = strlen(rx_buffer);
        uint16_t buf_len = sizeof(rx_buffer);
        uint16_t bytes_left = (buf_len - string_len);
    
        ret_code_t ret;
    
        switch (p_evt->type)
        {
            case NRF_LIBUARTE_ASYNC_EVT_ERROR:
                bsp_board_led_invert(0);
                break;
            case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
    
                if (string_len < buf_len)
                {
                    strncat(rx_buffer, p_evt->data.rxtx.p_data, bytes_left);
                    NRF_LOG_INFO("Bytes received: 0x%x", p_evt->data.rxtx.length)
                    NRF_LOG_INFO("Bytes appended to buffer 0x%x", ((bytes_left) <= p_evt->data.rxtx.length)
                                                                 ? bytes_left
                                                                 : p_evt->data.rxtx.length);
                }
                else
                {
                    NRF_LOG_INFO("rx_buffer is full");
                }
    
                nrf_libuarte_async_rx_free(
                    p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
    
    
                break;
    
            default:
                break;
        }
    }

    2. manuf_specific_data in advertising_init must be placed in static memory:

    static void advertising_init(void)
    {
        uint32_t err_code;
        uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    
        static ble_advdata_manuf_data_t manuf_specific_data;

  • Hello Vidar,

    Thanks for your valuable reply .

    But still there is problem happening with the code.

    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 1, 2, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 51, 3);

    From my sensor site sometime extra byte of data received ,thats where problem started.

    I am thinking if I define 

    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 1, 2, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 51, 3); to 

    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 1, 2, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 51, 1);

    Then the rx_buffer will not recieve extra byte data and the pointer is set to zero by 

    nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);

    But it is showing error in the time of build.

    What is your suggesstion.

    Thanks,

    Ram

  • Hi Ram,

    You need to find a way to handle/ignore this extra byte in your code. Reducing the dma buffer will not solve this problem. Also, libuarte requires a minimum of 2 buffers for double buffering. That is why you get the compile error when you only provide a single buffer.

    Best regards,

    Vidar

Related