Skip to content

Commit

Permalink
cpu/nrf5x_common: fix uart_poweroff()
Browse files Browse the repository at this point in the history
Previously, uart_poweroff() and uart_poweron() were no-ops. This
replaces them with the logic to indeed power on and power off the UART
device.
  • Loading branch information
maribu committed May 6, 2024
1 parent a5996e2 commit a4bd18c
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion cpu/nrf5x_common/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @}
*/

#include <assert.h>
#include <stdint.h>
#include <string.h>

Expand Down Expand Up @@ -99,6 +100,39 @@ enum {
UART_ISR_NUMOF,
};

static inline void set_power(uart_t uart, bool value)
{
#ifdef UARTE_PRESENT
const uint32_t enable_mask = UARTE_ENABLE_ENABLE_Enabled;
const uint32_t disable_mask = UARTE_ENABLE_ENABLE_Disabled;
#else
const uint32_t enable_mask = UART_ENABLE_ENABLE_Enabled;
const uint32_t disable_mask = UART_ENABLE_ENABLE_Disabled;
#endif
UART_TYPE *dev = uart_config[uart].dev;

if (value) {
dev->ENABLE = enable_mask;
}
else {
dev->ENABLE = disable_mask;
}
#ifndef UARTE_PRESENT
dev->POWER = value;
#endif
}

static inline bool get_power(uart_t uart)
{
#ifdef UARTE_PRESENT
const uint32_t disable_mask = UARTE_ENABLE_ENABLE_Disabled;
#else
const uint32_t disable_mask = UART_ENABLE_ENABLE_Disabled;
#endif
UART_TYPE *dev = uart_config[uart].dev;
return dev->ENABLE != disable_mask;
}

int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
{
/* ensure the ISR names have been defined as needed */
Expand Down Expand Up @@ -206,7 +240,7 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
}

/* enable the UART device */
dev->ENABLE = ENABLE_ON;
set_power(uart, true);

#ifdef MODULE_PERIPH_UART_NONBLOCKING
/* set up the TX buffer */
Expand Down Expand Up @@ -242,6 +276,7 @@ void uart_poweron(uart_t uart)

if (isr_ctx[uart].rx_cb) {
uart_config[uart].dev->TASKS_STARTRX = 1;
set_power(uart, false);
}
}

Expand All @@ -250,6 +285,7 @@ void uart_poweroff(uart_t uart)
assume((unsigned)uart < UART_NUMOF);

uart_config[uart].dev->TASKS_STOPRX = 1;
set_power(uart, false);
}

/* Unify macro names across nRF51 (UART) and nRF52 and newer (UARTE) */
Expand Down Expand Up @@ -335,6 +371,10 @@ static void _write_buf(uart_t uart, const uint8_t *data, size_t len)
void uart_write(uart_t uart, const uint8_t *data, size_t len)
{
assume((unsigned)uart < UART_NUMOF);
if (!get_power(uart)) {
/* Device is powered down. Writing anyway would deadlock */
return;
}
#ifdef MODULE_PERIPH_UART_NONBLOCKING
for (size_t i = 0; i < len; i++) {
/* in IRQ or interrupts disabled */
Expand Down

0 comments on commit a4bd18c

Please sign in to comment.