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

UARTE isn't working

Hi. I'm writing my own libraries for nRF52832 and I can't get UARTE to work.

Here is my code.

I'm using pins 6 and 8 as the UART example.

I wrote same code for UART (without DMA) and it works fine!

uart.c file

#include <string.h>
#include <stdbool.h>
#include "nrf52.h"
#include "uart.h"

static volatile uint8_t tx_buffer[128];

void uart_set_baudrate(uint32_t val)
{
    NRF_UARTE0->BAUDRATE = val;
}

void uart_configure(uint32_t parity, uint32_t hwfc)
{
    NRF_UARTE0->CONFIG = (parity << UART_CONFIG_PARITY_Pos) | (hwfc << UART_CONFIG_HWFC_Pos);
}

void uart_set_pins(uint32_t txpin, uint32_t rxpin)
{
    NRF_UARTE0->PSEL.RXD = rxpin;
    NRF_UARTE0->PSEL.TXD = txpin;
}

void uart_enable(void)
{
    NRF_UARTE0->ENABLE = UART_ENABLE_ENABLE_Enabled;
}

void uart_init(void)
{
    uart_set_baudrate(UART_BAUDRATE_BAUDRATE_Baud115200);
    uart_configure(UART_CONFIG_PARITY_Excluded, UART_CONFIG_HWFC_Disabled);
    uart_set_pins(6, 8);
    uart_enable();
}

void uart_write_string(uint8_t * str, int size)
{
    memcpy(tx_buffer, str, size);
    NRF_UARTE0->TXD.PTR = (uint32_t)((uint8_t *)tx_buffer);
    NRF_UARTE0->TXD.MAXCNT = size;
    NRF_UARTE0->TASKS_STARTTX = 1;
}

main.c

int main(void)
{
    clock_init();
    rtc_init();

    gpio_out_set(6);
    gpio_config_output(6);
    gpio_config_input(8, GPIO_PIN_CNF_PULL_Disabled);
    
    uart_init();
    uart_write_string("Hello world\r\n", 14);
    radio_init();
    
    __enable_irq();

    while (true)
    {

    }
}

I don't get anything on the terminal.

Parents
  • Try this code then compare with yours, for example start without flow control - like this code - and incrementally add features. Some of the steps here are not strictly necessary, but this works.

    static NRF_UARTE_Type *pUART = NRF_UARTE0;
    #define UART_TX 6
    #define UART_RX 8
    
    static void UartTransmit(uint8_t *pMsg, uint16_t MsgLength)
    {
       // Disable the UARTE
       pUART->ENABLE = 0;
    
       // Configure UARTE with no flow control, no parity bit and 230400 baud rate
       pUART->CONFIG = 0;
       pUART->BAUDRATE = NRF_UART_BAUDRATE_230400;
    
       // Select TX and RX pin default disconnected mode to avoid sending break indication on disabling uart
       nrf_gpio_cfg(UART_RX, NRF_GPIO_PIN_DIR_INPUT,  NRF_GPIO_PIN_INPUT_CONNECT,    NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
       nrf_gpio_cfg(UART_TX, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    
       // Select TX and RX pins
       pUART->PSEL.TXD = UART_TX;
       pUART->PSEL.RXD = UART_RX;
    
       // Disable all interrupts and clear events
       pUART->INTENCLR = 0xFFFFFFFFUL;
       pUART->EVENTS_TXSTARTED = 0;
       pUART->EVENTS_TXSTOPPED = 0;
       pUART->EVENTS_RXSTARTED = 0;
       pUART->EVENTS_ENDTX = 0;
       pUART->EVENTS_ENDRX = 0;
    
       // Enable the UART, note 0x04 for UART and 0x08 for UARTE with DMA
       pUART->ENABLE = 0x08;
    
       // Configure transmit buffer and start the transfer
       pUART->TXD.MAXCNT = MsgLength;
       pUART->TXD.PTR    = (uint32_t)pMsg;
       pUART->TASKS_STARTTX = 1;
       __DSB();
       // Wait until the transfer start event is indicated
       while (pUART->EVENTS_TXSTARTED == 0) ;
    
       // Wait until the transfer is complete
       while (pUART->EVENTS_ENDTX == 0) ;
    
       // Stop the UART TX
       pUART->TASKS_STOPTX = 1;
       __DSB();
       // Wait until we receive the stopped event
       while (pUART->EVENTS_TXSTOPPED == 0) ;
    
       // Disable the UARTE
       pUART->ENABLE = 0;
    
       // De-Select TX and RX pins
       pUART->PSEL.TXD = 0x80000000;
       pUART->PSEL.RXD = 0x80000000;
    }

    This code enables the UARTE0, transmits a string, then disables UARTE0 and releases the port pins so they can be used by other devices - such as (say) remote wakeup

Reply
  • Try this code then compare with yours, for example start without flow control - like this code - and incrementally add features. Some of the steps here are not strictly necessary, but this works.

    static NRF_UARTE_Type *pUART = NRF_UARTE0;
    #define UART_TX 6
    #define UART_RX 8
    
    static void UartTransmit(uint8_t *pMsg, uint16_t MsgLength)
    {
       // Disable the UARTE
       pUART->ENABLE = 0;
    
       // Configure UARTE with no flow control, no parity bit and 230400 baud rate
       pUART->CONFIG = 0;
       pUART->BAUDRATE = NRF_UART_BAUDRATE_230400;
    
       // Select TX and RX pin default disconnected mode to avoid sending break indication on disabling uart
       nrf_gpio_cfg(UART_RX, NRF_GPIO_PIN_DIR_INPUT,  NRF_GPIO_PIN_INPUT_CONNECT,    NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
       nrf_gpio_cfg(UART_TX, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_H0H1, NRF_GPIO_PIN_NOSENSE);
    
       // Select TX and RX pins
       pUART->PSEL.TXD = UART_TX;
       pUART->PSEL.RXD = UART_RX;
    
       // Disable all interrupts and clear events
       pUART->INTENCLR = 0xFFFFFFFFUL;
       pUART->EVENTS_TXSTARTED = 0;
       pUART->EVENTS_TXSTOPPED = 0;
       pUART->EVENTS_RXSTARTED = 0;
       pUART->EVENTS_ENDTX = 0;
       pUART->EVENTS_ENDRX = 0;
    
       // Enable the UART, note 0x04 for UART and 0x08 for UARTE with DMA
       pUART->ENABLE = 0x08;
    
       // Configure transmit buffer and start the transfer
       pUART->TXD.MAXCNT = MsgLength;
       pUART->TXD.PTR    = (uint32_t)pMsg;
       pUART->TASKS_STARTTX = 1;
       __DSB();
       // Wait until the transfer start event is indicated
       while (pUART->EVENTS_TXSTARTED == 0) ;
    
       // Wait until the transfer is complete
       while (pUART->EVENTS_ENDTX == 0) ;
    
       // Stop the UART TX
       pUART->TASKS_STOPTX = 1;
       __DSB();
       // Wait until we receive the stopped event
       while (pUART->EVENTS_TXSTOPPED == 0) ;
    
       // Disable the UARTE
       pUART->ENABLE = 0;
    
       // De-Select TX and RX pins
       pUART->PSEL.TXD = 0x80000000;
       pUART->PSEL.RXD = 0x80000000;
    }

    This code enables the UARTE0, transmits a string, then disables UARTE0 and releases the port pins so they can be used by other devices - such as (say) remote wakeup

Children
Related