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

How to enable UARTE in nRF52840

Hi,

I need to use two uarts for the project requirement. UART0 is enabled and working successfully. And I am trying to initialize UARTE, but I am not getting any output on the pins if I probe. Here is my code:

        #define U_RXD ((1 << 5) | (3 & 0x1F))   //P1.03
        #define U_TXD ((1 << 5) | (4 & 0x1F))   //P1.04

        const  uint8_t buffer[4] = {0x41,0x42,0x43,0x44};
         NRF_UARTE_Type * u_reg;

         nrf_gpio_cfg_input(U_RXD,NRF_GPIO_PIN_NOPULL);
         nrf_gpio_cfg_output(U_TXD);
         nrf_gpio_pin_set(U_TXD);

        u_reg->CONFIG = NRF_UARTE_PARITY_EXCLUDED |  NRF_UARTE_HWFC_DISABLED;
        u_reg->PSEL.TXD = U_TXD;
        u_reg->PSEL.RXD = U_RXD;

   u_reg->BAUDRATE = NRF_UARTE_BAUDRATE_115200;
   u_reg->ENABLE = UARTE_ENABLE_ENABLE_Enabled;

    u_reg->TXD.PTR    = (uint32_t)buffer;
    u_reg->TXD.MAXCNT = 4;

     nrf_uarte_task_trigger(u_reg, NRF_UARTE_TASK_STARTTX);
      while(!nrf_uarte_event_check(u_reg, NRF_UARTE_EVENT_ENDTX) );

Help me if the configuration is wrong. Is it possible to use both UART0 and UARTE at a time?

  • Hi,

    The nRF52840 have 2 UART Peripherals. The first one can be configured to be used either with or without EasyDMA(either as UARTE0 or UART0), while the second one can only be configured with EasyDMA (UARTE1).


    I would strongly recommend you to you use the UART drivers in the SDK. You can find an example on how to use it in the folder SDK_folder\examples\peripheral\uart\. Remember to enable both UART instances in sdk_config.h if you want to use both peripherals. The latest SDK version can be downloaded from here.

  • The 2 UART Peripherals each have its separate instance. See the Instantiation table here.

  • Looks like app_uart_fifo only supports running one of the UART instances at the same time. I.e. it will always use the APP_UART_DRIVER_INSTANCE that is configured in sdk_config.h

    code snippet from app_uart_fifo.c:

    static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
    

    The underlying driver(nrf_drv_uart) supports multiple UART instances at the same time, but app_uart_fifo will need some modification to support running both UART instances at the same time.

  • Note that we have added a new library called serial that solves this problem.

    From release notes:

    Serial port library: Module for handling UART communication. It supports multibyte transfers, time-outs, and buffering of data. The module is a replacement for app_uart. See the usage example in examples/peripheral/serial

  • Hi Sigurd The serial sample code is a not good code to control 2 UART. Because the code will entry while loop in nrf_serial_read(); It means if I used nrf_serial_read(0,x,x); and nrf_serial_read(1,x,x);in this code., it will entry nrf_serial_read(0,x,x); and wait data from UARTE0. When Get data from UARTE0 then run nrf_serial_read(1,x,x); then wait data from UARTE1.

    If it wait data from UARTE0 but get data from UARTE1, this data will keep in buffer and cannot write to UARTE1 port because the code is still in while loop of nrf_serial_read(0,x,x).

    Is it right?

    Thanks. Atlas

Related