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

nRF52840 UART queries

Hello,

 

I am using nRF52840, SDK_16.0.0, S140 SoftDevice and Segger 4.16 for flashing the image. I am using ‘ble_app_blinky’.

 

1) In Product specification, it says 2 UARTE supported. Whereas in section 6.33.10 I am seeing only one instance of UART0, but in section 6.34.9 there are two instance of UARTE0 and UARTE1.

a) Without EasyDMA, is it only one UART supported ?

 

2) I need uart in my ble_app_blinky application. I have gone through ble_app_uart. For this I enabled NRF_LOG_BACKEND_RTT_ENABLED and disabled NRF_LOG_BACKEND_UART_ENABLED. Is this fine ?

 

3) For UART rx, ble_app_uart has uart_event_handle() to receive data. For UART Tx whether can I use app_uart_put() ?

     a) To cross check I want to type on Terminal. Once received on uart, I want to transmit the same data back to terminal. But when I call app_uart_put(), I am not seeing any print on Terminal.

     b) Even I am getting event APP_UART_TX_EMPTY (3) in default case, indicating "UART has completed transmission of all available data in the TX FIFO". But not output on Terminal. (I configured Terminal properly).

      

/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[UART_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
        {
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;
            
            NRF_LOG_DEBUG("D ");
            //NRF_LOG_HEXDUMP_DEBUG(data_array, index);
            
            app_uart_put(data_array[index]);
            
            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {

                index = 0;
            }
        }
        break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            NRF_LOG_DEBUG("DF %d", p_event->evt_type);
            break;
    }
}
/**@snippet [Handling the data received over UART] */


/**@brief  Function for initializing the UART module.
 */
/**@snippet [UART Initialization] */
void uart_init(void)
{
    uint32_t                     err_code;
    app_uart_comm_params_t const comm_params =
    {
        #if CT40_ACTUAL_BOARD
        .rx_pin_no    = RX_PIN_NUMBER_CT40,
        .tx_pin_no    = TX_PIN_NUMBER_CT40,
        #else
        .rx_pin_no    = RX_PIN_NUMBER,
        .tx_pin_no    = TX_PIN_NUMBER,
        #endif

        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
#if defined (UART_PRESENT)
        .baud_rate    = NRF_UART_BAUDRATE_115200
#else
        .baud_rate    = NRF_UARTE_BAUDRATE_115200
#endif
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);
    APP_ERROR_CHECK(err_code);
}
/**@snippet [UART Initialization] */

4) I am bit confused, whether can I use app_uart.c / app_uart_fifo.c or “Serial Port Library”. My requirements are:

a) I need both UART Tx & Rx

b) BLE should work simultaneously.

c) Device should go into low power mode.

 

 Thanks & Regards

Vishnu Beema

  • Hi, 
    I will try to answer your questions.

    1) Yes, you understand correctly. UART0 and UARTE0 share the same memory space and HW and can be used with or without EasyDMA. UARTE1 can only be used with EasyDMA.

    2) I don't understand, you say you need UART, but you have enabled RTT? Do you mean that you need our logger, and want to use RTT as the log backend, or do you need the UART to be available as part of the application, while RTT is only for logging? In both cases, it should be OK.

    3) app_uart_put() is to be the correct way to transmit data in this context.
    If you have a look in the nus_data_handler(ble_nus_evt_t * p_evt) you can see how app_uart_put() is used.
    It seems that you are not waiting the tx to finish, other example typically use app_uart_put() inside a while loop. You can se how this is done in e.g. the peripheral example of uart on (line 173).
    while (app_uart_put(cr) != NRF_SUCCESS);

    4) I understand the confusion, and share this confusion with you. However, the serial port library is only a wrapper around the same functionality as using the uart more directly as is done in the app_uart example. I don't think it matters which method you choose to use, however I find the syntax in the serial port libary to be a bit easier.

    a) All the mentioned examples, etc. are able to handle both rx and tx
    b) Many of our BLE examples use UART and BLE simultaneously, the app_ble_uart does this as well. 
    c) I will recommend to disable the UART peripheral whenever possible, and e.g. use a BLE message or a pin input to trigger that the device should start listening on rx.

  • Thank you for your inputs.

     

    1) Yes, I want RTT for debug logs and UART for serial communication with other board.

    2) app_uart_put() is working. Issue is with my code. I am increment index before printing. So it will be a null character and I will not be seeing.

     

    My further queries are:

     

    1) Need more inputs on the statement “UART peripheral whenever possible”.

    a) Assume BLE is advertising every 1 sec and to my understanding nRF will go to low power mode in-between Advertisement packets. But if UART is always enabled will not nRF chip go to low power mode in-between Advertisement packets.

    b) If UART is always enabled, whether controller will not go to low power mode and whether current consumption will be more.

    c) In ble_app_uart, how are you handling of enabling and disabling of UART. Is there any reference.

     

    Thanks & Regards

    Vishnu Beema

  • Hi, 

    From your followup questions:
    a) The device can enter low power modes regardless of the UART being enabled. Simplistic said low power mode is to halt the CPU and make it wait for an event to happen. The CPU will resume operation when an event occur, handle the event and enter the wait-for-event mode again. However, the UART consumes energy by being enabled. This applies to all peripherals.

    b) The CPU will enter low power mode, but the current consumption is higher than needed. This can easily be measured if you run e.g. the app_ble_uart example and do measurements with and without UART enabled. 

    c) The ble_app_uart example does not handle dynamic enabling and disabling of UART. However, when you know you system's needs you can dynamically turn the UART on and off whenever needed. The function app_uart_close() can be used to disable the UART. 

  • Hi,

    I my use case first Secure Serial bootloader is loaded and then my ble_app_blinky with UART application is loaded.

    1) Because of Secure serial bootloader whether serial uart is always enabled. Even I am not seeing calling nrfx_uarte_uninit().

    2) (Or) Can I assume when pin is pressed then only chip will enter into boot mode and once DFU done, device will reset, so no issue even if there is no initialization of uart.

    3) What will be the current with uart enabled. Whether it will be 3 to 4 mA of ideal current. Anyway I will test practically, but this is for my quick understanding.

    Thanks & Regards

    Vishnu Beema

  • Hi,

    1) UART is only enabled when the bootloader enters DFU mode, e.g. when the button is pressed

    2) When DFU is completed the device resets, and if no button is pressed after the reset, the bootloader validates the application, then starts the application.

    3)

    I did some quick measurements with the power profiler kit on the nRF52840.
    I modified the peripheral -> serial example to either enable uart, then enter sleep mode, or disable uart before entering sleep mode.

    In the attached pictures below you can see that the UART being enabled adds around 600 uA.
    With UART enabled ~630 uA
    With UART disabled ~18 uA


    Code changes:
    Add the following lines before the infinite loop in the example.

    //nrf_drv_uart_uninit(&serial_uart); // comment out to disable UART peripheral
    while(true)
    {
        sleep_handler(); // Enter sleep mode
    }

    Uart peripheral disabled, sleep mode

    Uart peripheral enabled, sleep mode

Related