NRF52832 FreeRTOS+UARTE not worked

Hi everyone:

I want to use UARTE on my FreeRTOS, but when I used the example(examples/peripheral/libuarte) in rtos, something went wrong.

First of all, I tested the example, and it looks like good. When I send slowly, the uart log is here.

and when I send quickly(every 4ms), the uart log is that.

But when I use the UARTE in FreeRTOS, It looks OK when sending slowly, but when I send it every 4ms(I need this frequency), the result is here.

I don't know what's wrong.

Best regards, 

Lurn

Parents
  • Hi Lurn,

    There is not much to go on here. Can you elaborate a bit more? How exactly do you doing things in your implementation, and what exactly happens in the failing case? And what is the difference between the failing case (with FreeRTOS) and without it?

    Generally FreeRTOS does not interfere directly with UART, but it is tempting to think that there could be some issue with priorities, perhaps the application now is doing more or for some reason is not able to service UART related interrupts fast enough, but I don't know enough of your application to be more specific.

  • Sorry for my unclear description.

    I use the example uarte code in FreeRTOS, just modify here

        nrf_libuarte_async_config_t nrf_libuarte_async_config = {
                .tx_pin     = TX_PIN_NUMBER,
                .rx_pin     = RX_PIN_NUMBER,
                .baudrate   = NRF_UARTE_BAUDRATE_921600,
                .parity     = NRF_UARTE_PARITY_EXCLUDED,
                .hwfc       = NRF_UARTE_HWFC_DISABLED,
                .timeout_us = 2,
                .int_prio   = APP_IRQ_PRIORITY_HIGH,
                .pullup_rx  = 1
        };

    I change the int_prio from APP_IRQ_PRIORITY_LOW to APP_IRQ_PRIORITY_HIGH.

    the question is when I use FreeRTOS,when I'm done sending the data, I should receive the data immediately, the log should like that,  (the ->◇ is send data, <- Diamonds is receive data).

    But now, I get a wrong state, I only receive some data after I send it several times

    BR,

    Lurn

  • Hi Lurn,

    That is good news. Thanks for letting me know.

    Regarding _rx_buf_cnt and _rx_buf_size these are created using the Block allocator library. There is no hard limit on these sizes, other than the available RAM.

  • Sorry, this problem was solved only when I send 32 bytes, because I modify the rx buffer size to 32, so when I received 32bytes the buffer is full, then the rx_pool will be free.

    When I send less than 32 bytes of data, it still like this.

    1. When the sender sending data slowly, about 25 times per second, logs looks like that.

    After receiving several pieces of data, one of it was split into two parts

    So, I try to free the rx pool every time

    void nrf_libuarte_async_rx_free(const nrf_libuarte_async_t * const p_libuarte, uint8_t * p_data, size_t length)
    {
        p_libuarte->p_ctrl_blk->rx_free_cnt += length;
        if (p_libuarte->p_ctrl_blk->rx_free_cnt == p_libuarte->rx_buf_size)
        {
            p_data -= (p_libuarte->p_ctrl_blk->rx_free_cnt - length);
            p_libuarte->p_ctrl_blk->rx_free_cnt = 0;
            nrf_balloc_free(p_libuarte->p_rx_pool, p_data);
    
            p_libuarte->p_ctrl_blk->alloc_cnt--;
            if (p_libuarte->p_ctrl_blk->alloc_cnt<0)
            {
                NRF_LOG_ERROR("Freeing more RX buffers than allocated.");
                APP_ERROR_CHECK_BOOL(false);
            }
            NRF_LOG_INFO("Freeing full buffer 0x%08X, %d, (currently allocated:%d).",p_data, length, p_libuarte->p_ctrl_blk->alloc_cnt);
    
            if (p_libuarte->p_ctrl_blk->rx_halted)
            {
                bool ret = rx_buffer_schedule(p_libuarte);
                ASSERT(ret);
                p_libuarte->p_ctrl_blk->rx_halted = false;
            }
        }
        else if (p_libuarte->p_ctrl_blk->rx_free_cnt > p_libuarte->rx_buf_size)
        {
            NRF_LOG_ERROR("Unexpected RX free input parameter.");
            APP_ERROR_CHECK_BOOL(false);
        }
        else
        {
            NRF_LOG_INFO("Freeing partial buffer: 0x%08X, length:%d", p_data, length);
        }
    
    }

     I modified the if criteria(change to 1 make sure the code can enter here every time), but it didn't work even make my program crash. It running to "Freeing more RX buffers than allocated."

    So, maybe you can give me a batter solution.

    Another question, when I sending data by uarte, something went wrong.

    you can see that, when I send 5A 0C 03 01 01 for test, I can receive it correctly. then I modify the data in data[3] and data[4], the RTT print is OK, but the uart print is wrong. when i increase the data length, I found that the data from data[3] to data[31] are all 0.

    Also, when I only send data by uart, the data format going run. I init the test_data, then in my uart task, only have the tx code

        ...
        for(int i = 0; i<32; i++)
        {
            test_data[i] = 0xA0+i;
        }
        ...
        
        nrf_libuarte_async_tx(&libuarte, test_data, sizeof(test_data));

    but the uart print like this

    BR,

    Lurn

  • Hi Einar,

    The uart init and event_handler is here.

    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 0, 0, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 32, 12);
    
    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:
                UART_PRINTF("NRF_LIBUARTE_ASYNC_EVT_ERROR = %d.", p_evt->data.errorsrc);
                break;
            case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
                memcpy(data_array, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                flag = 1;
                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:
                break;
            default:
                UART_PRINTF("Unexpected LibUarte Event = %d.", p_evt->type);
                break;
        }
    }
    
    void uart_init()
    {
        uint8_t err_code;
        nrf_libuarte_async_config_t nrf_libuarte_async_config = {
                .tx_pin     = TX_PIN_NUMBER,
                .rx_pin     = RX_PIN_NUMBER,
                .baudrate   = NRF_UARTE_BAUDRATE_921600,
                .parity     = NRF_UARTE_PARITY_EXCLUDED,
                .hwfc       = NRF_UARTE_HWFC_DISABLED,
                .timeout_us = 600,
                .int_prio   = APP_IRQ_PRIORITY_HIGH,
                .pullup_rx  = true
        };
    
        err_code = nrf_libuarte_async_init(&libuarte, &nrf_libuarte_async_config, uart_event_handler, (void *)&libuarte);
        if(err_code != NRF_SUCCESS)
        {
            UART_PRINTF("nrf_libuarte_async_init err_code = %d.", err_code);
        }
        APP_ERROR_CHECK(err_code);
    
        nrf_libuarte_async_enable(&libuarte);
    
        // err_code = nrf_libuarte_async_tx(&libuarte, text, text_size);
        // UART_PRINTF("nrf_libuarte_async_tx err_code = %d.", err_code);
        // APP_ERROR_CHECK(err_code);
    }

    Any idea what the problem could be?

    Best regards,

    Lurn

  • Ok, the question is I set the memcpy(data_array, 0, sizeof(data_array)) too early, that the uart data wasn't send done, so the data was be set to 0.

    but, how to handle the error format?

    BR,

    Lurn

  • Hi Lurn,

    Lurn_Z said:
    Ok, the question is I set the memcpy(data_array, 0, sizeof(data_array)) too early, that the uart data wasn't send done, so the data was be set to 0.

    No, the data is available when you get the event, so copying them immediately afterwards, and freeing the buffer is OK.

    Lurn_Z said:
    but, how to handle the error format?

    Which error?

    From what I understand, the issue here is that you get the data in chunks. That is something you need to handle though, as fragmentation can happen for many different reasons. So I would suggest that you implement a form of filtering on a higher level. UART is typically just a stream of data, so most likely there is something in the data that tells you what logical units they represent? Perhaps a new-line character, or perhaps the data is in TLV format, or something else?

Reply
  • Hi Lurn,

    Lurn_Z said:
    Ok, the question is I set the memcpy(data_array, 0, sizeof(data_array)) too early, that the uart data wasn't send done, so the data was be set to 0.

    No, the data is available when you get the event, so copying them immediately afterwards, and freeing the buffer is OK.

    Lurn_Z said:
    but, how to handle the error format?

    Which error?

    From what I understand, the issue here is that you get the data in chunks. That is something you need to handle though, as fragmentation can happen for many different reasons. So I would suggest that you implement a form of filtering on a higher level. UART is typically just a stream of data, so most likely there is something in the data that tells you what logical units they represent? Perhaps a new-line character, or perhaps the data is in TLV format, or something else?

Children
Related