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

  • Hi Lurn,

    Can you share the actual modified example that you are testing, not just the snippet? That way I will not have to repeat what you have done, and also, will not end pt testing something which might not be exactly the same.

    That said though, you will get higher latency with FreeRTOS than with bare metal, so it could be that this simply is one of those (quite common) cases where you are trying to use UART without flow control and that simply does not work unless the nRF is always ready to receive data as it comes (which is often not the case for more complex applications). So the first thing I would do is to use flow control if possible.

  • ok, I will share you my main.c, sdk_config and uart.c

    it looks like a bit of a mess, because I delete some another tasks things.

    /**
     * Copyright (c) 2015 - 2021, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    /** @file
     * @defgroup blinky_example_main main.c
     * @{
     * @ingroup blinky_example_freertos
     *
     * @brief Blinky FreeRTOS Example Application main file.
     *
     * This file contains the source code for a sample application using FreeRTOS to blink LEDs.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    
    #include "FreeRTOS.h"
    #include "task.h"
    #include "timers.h"
    #include "bsp.h"
    #include "nordic_common.h"
    #include "nrf_drv_clock.h"
    #include "sdk_errors.h"
    #include "app_error.h"
    #include "thread.h"
    #include "nrf_power.h"
    #include "nrf_pwr_mgmt.h"
    #include "nrf_delay.h"
    
    
    #if LEDS_NUMBER <= 2
    #error "Board is not equipped with enough amount of LEDs"
    #endif
    
    #define TASK_DELAY        200           /**< Task delay. Delays a LED0 task for 200 ms */
    #define TIMER_PERIOD      1000          /**< Timer period. LED1 timer will expire after 1000 ms */
    
    #define BOOTLOADER_DFU_GPREGRET_MASK            (0xF8)      /**< Mask for GPGPREGRET bits used for the magic pattern written to GPREGRET register to signal between main app and DFU. */
    #define BOOTLOADER_DFU_GPREGRET                 (0xB0)      /**< Magic pattern written to GPREGRET register to signal between main app and DFU. The 3 lower bits are assumed to be used for signalling purposes.*/
    #define BOOTLOADER_DFU_START_BIT_MASK           (0x01)      /**< Bit mask to signal from main application to enter DFU mode using a buttonless service. */
    #define BOOTLOADER_DFU_START    (BOOTLOADER_DFU_GPREGRET | BOOTLOADER_DFU_START_BIT_MASK)      /**< Magic number to signal that bootloader should enter DFU mode because of signal from Buttonless DFU in main app.*/
    
    
    static void clocks_start( void )
    {
        NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_HFCLKSTART = 1;
    
        while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
    }
    
    #if 1
    static void enter_dfu(void)
    {
        uint8_t old_gpregret = nrf_power_gpregret_get();
        SEGGER_RTT_printf(0, "get the old_gpregret = %d", old_gpregret);
            nrf_delay_ms(5);
        uint8_t new_gpregret = old_gpregret & BOOTLOADER_DFU_START;
        SEGGER_RTT_printf(0, "new_gpregret = %d", new_gpregret);
            nrf_delay_ms(5);
        uint8_t temp = 0;
    
        nrf_power_gpregret_set(BOOTLOADER_DFU_START);
        while((temp & BOOTLOADER_DFU_START) != BOOTLOADER_DFU_START)
        {
            temp = nrf_power_gpregret_get();
            SEGGER_RTT_printf(0, "temp = %d", temp);
            nrf_delay_ms(5);
        }
        nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU);
        SEGGER_RTT_printf(0, "nrf_pwr_mgmt_shutdown NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU and system reset.");
            nrf_delay_ms(5);
        NVIC_SystemReset();
    }
    #endif
    
    int main(void)
    {
        ret_code_t err_code;
        SEGGER_RTT_printf(0, "Start application.");
            nrf_delay_ms(5);
        /* Initialize clock driver for better time accuracy in FREERTOS */
        err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        clocks_start();
        /* Configure LED-pins as outputs */
        bsp_board_init(BSP_INIT_LEDS);
    
        // enter_dfu();
    
        thread_main();
    
        /* Activate deep sleep mode */
        SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
    
        /* Start FreeRTOS scheduler. */
        vTaskStartScheduler();
    
        while (true)
        {
            /* FreeRTOS should not be here... FreeRTOS goes back to the start of stack
             * in vTaskStartScheduler function. */
        }
    }
    
    /**
     *@}
     **/
    

    6648.sdk_config.h

    /**
     * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    /** @file
     * @defgroup uart_example_main main.c
     * @{
     * @ingroup uart_example
     * @brief UART Example Application main file.
     *
     * This file contains the source code for a sample application using UART.
     *
     */
    
    #include "thread.h"
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    #include "bsp.h"
    #if defined (UART_PRESENT)
    #include "nrf_uart.h"
    #endif
    #if defined (UARTE_PRESENT)
    #include "nrf_uarte.h"
    #endif
    #include "uart.h"
    #include "nrf_drv_clock.h"
    #include "esb.h"
    #include "pairing_cmd.h"
    
    
    #define UART_PRINTF( ... )  SEGGER_RTT_printf(0, __VA_ARGS__)
    
    
    uint32_t app_timer_cnt_get(void)
    {
        return xTaskGetTickCount();
    }
    
    uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
                                        uint32_t ticks_from)
    {
        return ((ticks_to - ticks_from) & RTC_COUNTER_COUNTER_Msk);
    }
    
    uint8_t data_array[UART_RX_BUF_SIZE];
    static uint8_t index = 0;
    static uint8_t flag = 0;
    
    #include "nrf_libuarte_async.h"
    #include <bsp.h>
    #include "nrf_queue.h"
    
    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 0, 0, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 255, 3);
    
    static uint8_t text[] = "UART example started.\r\n Loopback:\r\n";
    static uint8_t text_size = sizeof(text);
    static volatile bool m_loopback_phase;
    
    typedef struct {
        uint8_t * p_data;
        uint32_t length;
    } buffer_t;
    
    NRF_QUEUE_DEF(buffer_t, m_buf_queue, 10, NRF_QUEUE_MODE_NO_OVERFLOW);
    
    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:
                ret = nrf_libuarte_async_tx(p_libuarte,p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                buffer_t buf = {
                    .p_data = p_evt->data.rxtx.p_data,
                    .length = p_evt->data.rxtx.length,
                };
                if (ret == NRF_ERROR_BUSY)
                {
    
                    ret = nrf_queue_push(&m_buf_queue, &buf);
                    APP_ERROR_CHECK(ret);
                }
                else
                {
                    APP_ERROR_CHECK(ret);
                }
                m_loopback_phase = true;
                uart_cmd_set(buf.p_data, buf.length);
                // memcpy(data_array, buf.p_data, buf.length);
                // flag = 1;
                break;
            case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
                if (m_loopback_phase)
                {
                    nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                    if (!nrf_queue_is_empty(&m_buf_queue))
                    {
                        buffer_t buf;
                        ret = nrf_queue_pop(&m_buf_queue, &buf);
                        APP_ERROR_CHECK(ret);
                        UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
                    }
                }
                break;
            default:
                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 = 0,
                .int_prio   = APP_IRQ_PRIORITY_HIGH,
                .pullup_rx  = 1
        };
    
        err_code = nrf_libuarte_async_init(&libuarte, &nrf_libuarte_async_config, uart_event_handler, (void *)&libuarte);
        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);
    }
    
    #endif
    
    /**
     * @brief Function for main application entry.
     */
    void uart_main(void)
    {
    	#if 0
        while (flag)
        {
            UART_PRINTF("data_array = %s", data_array);
            memset(&data_array, 0, UART_RX_BUF_SIZE);
            flag = 0;
        }
    	#endif
    }
    
    /** @} */
    

    /* FreeRTOS hearder */
    #include "thread.h"
    #include "uart.h"
    #include "nrf_delay.h"
    
    /**************************** task handler ********************************/
    /* 
     * create task handler
     * if operate self , value can be NULL。
     * 
     */
    // static TaskHandle_t AppTaskCreate_Handle;
    
    TaskHandle_t RunTimeStats_Task_Handle;
    TaskHandle_t Uart_Task_Handle;
    
    /********************************** core handler *********************************/
    /*
     * sem,queue,event,timer...
     * 
     */
    
    
    /******************************* Global variable ************************************/
    /*
     * Global variable
     */
    
    /******************************* Macro Define ***************************************/
    /*
     * Macro Define
     */
    /*
    ****************************************************************************
    *                              task name
    *****************************************************************************
    */
    #define RunTimeStatsTaskName                             "RunTimeStatsTask"
    #define Uart_TaskName                                    "Uart_Task"
    
    /*
    ****************************************************************************
    *                               size
    *****************************************************************************
    */
    #define RunTimeStatsTaskSize                                      ( 128 )
    #define Uart_TaskSize                                             ( 1024 )
    
    /*
    ****************************************************************************
    *                               prio
    *****************************************************************************
    */
    /*
    MAX prio in FreeRTOS
    
    #define configMAX_PRIORITIES                                                      ( 7 )
    */
    
    #define RunTimeStatsTaskPrio                                      ( 3 )
    #define Uart_TaskPrio                                             ( 5 )
    
    /*
    *****************************************************************************
    *                              handler
    *****************************************************************************
    */
    
    extern TaskHandle_t RunTimeStats_Task_Handle;                        // Run state
    extern TaskHandle_t Uart_Task_Handle;                                // Uart
    
    /*
    *************************************************************************
    *                             Funtion
    *************************************************************************
    
     /**********************************************************************
      * @ Fun  : RunTimeStatsTask
      * @ param   :   
      * @ retval  : null
      ********************************************************************/
    
    void RunTimeStatsTask(void)
    {
        while(1)
        {
            int val;
            SEGGER_RTT_printf(0, "TaskName\t\t\t\tPrio\t\t\t\tStackSize");
            val = uxTaskGetStackHighWaterMark(Uart_Task_Handle);
            SEGGER_RTT_printf(0, "%s\t\t\t\t%d\t\t\t\t%d", Uart_TaskName, Uart_TaskPrio, val);
            vTaskDelay(10000);
        }
    }
    
    /**********************************************************************
      * @ Fun  : Uart_task test
      * @ param   :   
      * @ retval  : null
      ********************************************************************/
    void Uart_Task(void* parameter)
    {
        uart_init();
    
        while(1)
        {
            uart_main();
            vTaskDelay(2);
        }
    }
    
    /*****************************************************************
      * @brief  HardWare_init
      * @param  null
      * @retval null
      * @note   init all hardware
      ****************************************************************/
    void HardWare_init(void)
    {
    
    }
    
    /*****************************************************************
      * @brief  main
      * @param  null
      * @retval null
      * @note   thread manager main
      ****************************************************************/
    int thread_main(void)
    {
        BaseType_t xReturn = pdPASS;
        int size = 0;
    
        HardWare_init();
    
        taskENTER_CRITICAL();
    
        xReturn = xTaskCreate((TaskFunction_t )Uart_Task,                 // enter fun
                            (const char*    )Uart_TaskName,               // name
                            (uint16_t       )Uart_TaskSize,               // size
                            (void*          )NULL,                        // enter param
                            (UBaseType_t    )Uart_TaskPrio,               // prio
                            (TaskHandle_t*  )&Uart_Task_Handle);          // task handle 
    
        SEGGER_RTT_printf(0, " xTaskCreate Uart_Task = %d\r\n", xReturn);
        if(pdPASS == xReturn)
            SEGGER_RTT_printf(0, "Uart_Task Create Done!\r\n");
    
        size = xPortGetFreeHeapSize();
        SEGGER_RTT_printf(0, "heap size = %d\r\n", size);
    
        taskEXIT_CRITICAL();
        if(pdPASS != xReturn)
            return -1;
        return pdPASS;
    }
    
    

    BR,

    Lurn

  • Maybe I've entered a misunderstanding, my device need receive 32bytes data per 4ms, and process it.

    So, for my test, I shouldn't print it by UART after receive data, because TX also spend time?

    I modified the way to print the log (it will be display by RTT now),but it still many wrong formats.

    logs like that, I send 32B data (ZZZ.....for test), Sometimes it is split into two parts

    Can you see something wrong with my code?
    The location I modified is here, maybe this modification is not appropriate.
    void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_evt)
    {
        ....
    
        switch (p_evt->type)
        {
            ...
            
            case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
                // ret = nrf_libuarte_async_tx(p_libuarte,p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                ret = 0;
                buffer_t buf = {
                    .p_data = p_evt->data.rxtx.p_data,
                    .length = p_evt->data.rxtx.length,
                };
                if (ret == NRF_ERROR_BUSY)
                {
    
                    ret = nrf_queue_push(&m_buf_queue, &buf);
                    APP_ERROR_CHECK(ret);
                }
                else
                {
                    APP_ERROR_CHECK(ret);
                }
                m_loopback_phase = true;
                memcpy(data_array, buf.p_data, buf.length);
                flag = 1;
                nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                break;
            
            ...
            
                break;
        }
    }
    BR,
    Lurn
  • you can see the application in examples/peripheral/blinky_freertos/pca10040/blank/arm5_no_packs.

    I removed a lot of things that were not related to UART. So, maybe it looks like a bit of a mess.
    BR,
    Lurn
  • maybe you need to modify the start address, because in another ticket I need program MBR, so I modify it to 0X1000.

Related