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

Device hang in libuarte function when no debugger run

Hi,
Our platform is NRF-52833

Our project use RS485 bus (nordic's uart + a 485 chip) to communicate with other device.
And we choose the libuarte to be the uart handler to tranxfer data.
It works fine when device is in debugging run(segger, SWD).

But if running without debugging, device will hang after uart start to tranxfer data.
(I create a timer to print log(RTT) periodically to check if device alive or not)
Device can be recovered back by attaching on debugger.

It's hard to debug since this issue only happen in no debugger attached.
I tried the different baudrates but issue still happen.
I also tried to change the LF_CLK source from XTAL to RC and this issue still happen.
(Our device default use an external 32.768Khz XTAL)


Could anyone give me some suggestion?

The below is the main framework of my program:

Init:
	nrf_drv_clock_lfclk_request(NULL);
	
    nrf_libuarte_async_config_t nrf_libuarte_async_config = {
            .tx_pin     = UART_ERV_TX,
            .rx_pin     = UART_ERV_RX,
            .baudrate   = buadrate,
            .parity     = NRF_UARTE_PARITY_EXCLUDED,
            .hwfc       = NRF_UARTE_HWFC_DISABLED,
            .timeout_us = 100,
            .int_prio   = APP_IRQ_PRIORITY_LOW
    };
    err_code = nrf_libuarte_async_init(&libuarte, &nrf_libuarte_async_config, uart_event_handler, (void *)&libuarte);
    APP_ERROR_CHECK(err_code);
    
    nrf_libuarte_async_enable(&libuarte);


Handler:
void rs485_byte_send(uint8_t tx_byte)
{
    uint32_t err_code;
    uint8_t  data;

    data = tx_byte;
    is_byte_sent = false;
    err_code = nrf_libuarte_async_tx(&libuarte, &data, 1);
}

bool rs485_byte_available(uint8_t *data_register)
{
    bool data_available = false; /* return value */

    if (!FIFO_Empty(&Receive_Buffer)) {
        if (data_register) {
            *data_register = FIFO_Get(&Receive_Buffer);
        }
        data_available = true;
        //NRF_LOG_INFO("byte_available: %d, data=0x%x\n", Receive_Buffer.head-Receive_Buffer.tail, *data_register);
    }

    return data_available;
}

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:
            break;
        case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
            FIFO_Add(&Receive_Buffer, p_evt->data.rxtx.p_data,  p_evt->data.rxtx.length);
            NRF_LOG_INFO("byte push:%x %d!!\n", *(p_evt->data.rxtx.p_data), p_evt->data.rxtx.length);
            nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
            break;
        case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
            is_byte_sent = true;
            //NRF_LOG_INFO("byte tx done!!\n");
            break;
        default:
            break;
    }
}





thanks,

Jacob

Parents
  • Hello Jacob Lee,

    But if running without debugging, device will hang after uart start to tranxfer data.
    (I create a timer to print log(RTT) periodically to check if device alive or not)

    Could you make sure that you have DEBUG defined in your preprocessor defines, like shown in the included image?

    I also notice that you do not check the returned error codes in your code, such as in your rs485_byte_send function.
    You should always pass the returned error codes to an APP_ERROR_CHECK to see if the function call was successful. These checks are your only way of knowing whether a function call has failed (for whatever reason), or if your program can proceed as usual.
    If you have defined DEBUG as mentioned, any non NRF_SUCCESS returned error code will generate a detailed error message printed by your logger.

    Please add checks to all returned error codes and add the DEBUG define to preprocessor defines, and let me know if any error message is printed when you run the device without debugging.

    Looking forward to resolve this issue together!

    Best regards,
    Karl

Reply
  • Hello Jacob Lee,

    But if running without debugging, device will hang after uart start to tranxfer data.
    (I create a timer to print log(RTT) periodically to check if device alive or not)

    Could you make sure that you have DEBUG defined in your preprocessor defines, like shown in the included image?

    I also notice that you do not check the returned error codes in your code, such as in your rs485_byte_send function.
    You should always pass the returned error codes to an APP_ERROR_CHECK to see if the function call was successful. These checks are your only way of knowing whether a function call has failed (for whatever reason), or if your program can proceed as usual.
    If you have defined DEBUG as mentioned, any non NRF_SUCCESS returned error code will generate a detailed error message printed by your logger.

    Please add checks to all returned error codes and add the DEBUG define to preprocessor defines, and let me know if any error message is printed when you run the device without debugging.

    Looking forward to resolve this issue together!

    Best regards,
    Karl

Children
  • Hi Karl,

    Thanks for the reply.

    I think I found the root cause.

    I imported a library in out project and it use the fprintf(stdout..) to show message.

    I comment the all fprintf to solve this issue.

    So, the fprintf will make device hang if it's not in the debug mode.

     Maybe this issue can be related to this:

    devzone.nordicsemi.com/.../133776

  • Jacob Lee said:
    Thanks for the reply.

    No problem at all, I am happy to help!

    Jacob Lee said:
    I think I found the root cause.

    I am glad to hear that you have found the root cause of the issue. 

    Jacob Lee said:

    I comment the all fprintf to solve this issue.

    So, the fprintf will make device hang if it's not in the debug mode.

    Do you have RETARGET defined to 1 in your sdk_config?

    Did you also do as I recommended in my previous comment, and add error code checks to the returned error codes, and defined DEBUG in preprocessor defines?
    These two things will be very helpful during future development.

    If you issue is resolved, we may close this ticket. If so, please do not hesitate to open a new ticket if you should encounter any issues or question in the future! :) 

    Best regards,
    Karl

Related