Skip to content

Commit

Permalink
cpu/nrf5x/uart: run STOPTX task after finished tx
Browse files Browse the repository at this point in the history
This reduces power consumption for UARTs that are configured in tx-only mode.
  • Loading branch information
jue89 committed Nov 22, 2022
1 parent 97bc825 commit 9b4f684
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cpu/nrf5x_common/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
dev(uart)->ENABLE = UARTE_ENABLE_ENABLE_Enabled;
#else
NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
NRF_UART0->TASKS_STARTTX = 1;
#endif

#ifdef MODULE_PERIPH_UART_NONBLOCKING
Expand Down Expand Up @@ -261,6 +260,7 @@ static void _write_buf(uart_t uart, const uint8_t *data, size_t len)
/* wait for the end of transmission */
if (!IS_USED(MODULE_PERIPH_UART_NONBLOCKING)) {
while (dev(uart)->EVENTS_ENDTX == 0) {}
dev(uart)->TASKS_STOPTX = 1;
}
}

Expand Down Expand Up @@ -393,6 +393,8 @@ static inline void irq_handler(uart_t uart)
if (!tsrb_empty(&uart_tx_rb[uart])) {
tx_buf[uart] = tsrb_get_one(&uart_tx_rb[uart]);
_write_buf(uart, &tx_buf[uart], 1);
} else {
dev(uart)->TASKS_STOPTX = 1;
}
}
#endif
Expand All @@ -406,6 +408,8 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)
{
(void)uart;

NRF_UART0->TASKS_STARTTX = 1;

for (size_t i = 0; i < len; i++) {
/* This section of the function is not thread safe:
- another thread may mess up with the uart at the same time.
Expand All @@ -423,13 +427,14 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)
/* wait for any transmission to be done */
while (NRF_UART0->EVENTS_TXDRDY == 0) {}
}

NRF_UART0->TASKS_STOPTX = 1;
}

void uart_poweron(uart_t uart)
{
(void)uart;

NRF_UART0->TASKS_STARTTX = 1;
if (isr_ctx.rx_cb) {
NRF_UART0->TASKS_STARTRX = 1;
}
Expand All @@ -439,7 +444,7 @@ void uart_poweroff(uart_t uart)
{
(void)uart;

NRF_UART0->TASKS_SUSPEND;
NRF_UART0->TASKS_STOPRX = 1;
}

int uart_mode(uart_t uart, uart_data_bits_t data_bits, uart_parity_t parity,
Expand Down

0 comments on commit 9b4f684

Please sign in to comment.