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

  • here is no data loss, though - only that you got two events for what I assume conceptually was one transaction

    Yes, I think so, the data is not lost, just became two parts.

    You will get data when there is a timeout (packet boundary) or when the Rx buffer is full.

    I think it's really about Rx buffer, cause when I change the buffer size from 256 to 64 the data was be split earlier.

    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 0, 0, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 64, 3);

    But, the rx buffer was cleared everytime I received data.

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

    Do you have any other solution?

    And I have another question that I need a frequency of 500 times per second, but when I test the application,I don't think it can reach this frequency. Because I get the logs in 9 second, it just 260 times data. Also I will check the sender frequency, and I hope you can give me some suggestions.

    BR,

    Lurn

  • After my test, Here is 3 questions.

    I send 32 bytes, the data head is A5 C5, and print 34 bytes so the last two data should be 0.

    (uint8_t data_array[256] = {0};)

    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

    2. When the sender sending data quickly, (250 times per second), data will be lost, Only 30 pieces of data are received per second.

    3. When the sender sending data quickly, (250 times per second), logs looks like that.

    There is an exception in the data format,

    Hope you can give me some suggestions.

    BR,

    Lurn

  • Hi Lurn,

    I am not able to pinpoint what is happening in your application. However, as we have discussed before, you get the Rx events either for timeout (interpreted as a packet boundary) or a full buffer. This means that you are not guaranteed to only get events for a packet boundary, and if that is something you need, you need an additional layer of "filtering" to handle that.

    It is difficult to know what happens in your application. It could be that data is not handled in a timely manner because there are too much delays / CPU usage of other parts. Do you see the same issue if you comment out most of the other code/threads, so that you are only receiving UART data? If not, then that would point to this being related. And if so you may need to consider optimizing other parts in order to resolve this issue.

    Einar

  • Hi Einar,

    I think this problem has been solved.

    I modified the buffer size, buffer number and time_out to find the values that work for me.

    Now, it is running ok.

    Thanks a lot!

    But I also have a question, in this macro

    #define NRF_LIBUARTE_ASYNC_DEFINE(_name, _uarte_idx, _timer0_idx,\
                                      _rtc1_idx, _timer1_idx,\
                                      _rx_buf_size, _rx_buf_cnt)\
                                      ....

    How many _rx_buf_cnt I can set at most? and what is the maximum _rx_buf_size?

    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.

Reply Children
  • 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

Related