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

configure UART 1 by directly accessing the registers

I want to configure the UART1 (not the UART0), and I want to know if there have some examples that introducing the UART configuration by directing accessing the register. My code is below and I want to confirm this operation is correct.

void uart_init(){

//NRF_P0 --->P0.xx;NRF_P1---->P1.xx
NRF_P0->OUTSET = 1 << UART_TX_PIN;//Set UART pin number bits in GPIO port

// tx pin configured as output
NRF_P0->PIN_CNF[UART_TX_PIN] = \
((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);

// rx pin configured as input // 执行的就该函数是 nrf_gpio_cfg_input()-->nrf_gpio_cfg
NRF_P0->PIN_CNF[UART_RX_PIN] = \
((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);

// configure uart

NRF_UART0->BAUDRATE = (uint32_t)(UART_BAUDRATE_115200);
NRF_UART0->CONFIG =
(uint32_t)(UART_CONFIG_PARITY << UART_CONFIG_PARITY_POS)
| (uint32_t)(UART_CONFIG_HWFC << UART_CONFIG_HWFC_POS);
NRF_UART0->PSEL.RXD = (uint32_t)UART_RX_PIN;
NRF_UART0->PSEL.TXD = (uint32_t)UART_TX_PIN;

// enable UART rx done ready and tx done ready interrupts

NRF_UART0->INTENSET =
(uint32_t)(1<<UART_INTEN_RXDRDY_POS)
| (uint32_t)(1<<UART_INTEN_TXDRDY_POS);

// set priority and enable interrupt in NVIC
NVIC_SetPriority(UARTE0_UART0_IRQn, NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY);

NVIC->ISER[((uint32_t)UARTE0_UART0_IRQn)>>5] =
((uint32_t)1) << ( ((uint32_t)UARTE0_UART0_IRQn) & 0x1f);

// enable uart
NRF_UART0->ENABLE = (uint32_t)UART_ENABLE_ENABLE_Enabled;

// start to tx and rx
NRF_UART0->TASKS_STARTTX = (uint32_t)1;
NRF_UART0->TASKS_STARTRX = (uint32_t)1;

}

Related