Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/periph/uart: add periph_uart_tx_ondemand feature #20108

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpu/sam0_common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ config CPU_COMMON_SAM0
select HAS_PERIPH_UART_NONBLOCKING
select HAS_PERIPH_UART_RECONFIGURE
select HAS_PERIPH_UART_RXSTART_IRQ
select HAS_PERIPH_UART_TX_ONDEMAND
select HAS_PERIPH_WDT
select HAS_PERIPH_WDT_CB
select HAS_PERIPH_WDT_WARNING_PERIOD
Expand Down
1 change: 1 addition & 0 deletions cpu/sam0_common/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ FEATURES_PROVIDED += periph_uart_modecfg
FEATURES_PROVIDED += periph_uart_nonblocking
FEATURES_PROVIDED += periph_uart_reconfigure
FEATURES_PROVIDED += periph_uart_rxstart_irq
FEATURES_PROVIDED += periph_uart_tx_ondemand
FEATURES_PROVIDED += periph_wdt periph_wdt_cb periph_wdt_warning_period

FEATURES_CONFLICT += periph_rtc:periph_rtt
Expand Down
1 change: 1 addition & 0 deletions cpu/sam0_common/include/periph_cpu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ typedef enum {
UART_FLAG_NONE = 0x0, /**< No flags set */
UART_FLAG_RUN_STANDBY = 0x1, /**< run SERCOM in standby mode */
UART_FLAG_WAKEUP = 0x2, /**< wake from sleep on receive */
UART_FLAG_TX_ONDEMAND = 0x4, /**< Only enable TX pin on demand */
} uart_flag_t;

#ifndef DOXYGEN
Expand Down
19 changes: 18 additions & 1 deletion cpu/sam0_common/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ static void _set_baud(uart_t uart, uint32_t baudrate, uint32_t f_src)
#endif
}

void uart_enable_tx(uart_t uart)
{
/* configure RX pin */
if (uart_config[uart].tx_pin != GPIO_UNDEF) {
gpio_init_mux(uart_config[uart].tx_pin, uart_config[uart].mux);
}
}

void uart_disable_tx(uart_t uart)
{
/* configure RX pin */
if (uart_config[uart].tx_pin != GPIO_UNDEF) {
gpio_init_mux(uart_config[uart].tx_pin, GPIO_MUX_A);
}
}

static void _configure_pins(uart_t uart)
{
/* configure RX pin */
Expand All @@ -138,7 +154,8 @@ static void _configure_pins(uart_t uart)
}

/* configure TX pin */
if (uart_config[uart].tx_pin != GPIO_UNDEF) {
if (uart_config[uart].tx_pin != GPIO_UNDEF &&
!(uart_config[uart].flags & UART_FLAG_TX_ONDEMAND)) {
gpio_set(uart_config[uart].tx_pin);
gpio_init(uart_config[uart].tx_pin, GPIO_OUT);
gpio_init_mux(uart_config[uart].tx_pin, uart_config[uart].mux);
Expand Down
9 changes: 8 additions & 1 deletion drivers/dose/dose.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@
signal = state_transit_send(ctx, signal);
ctx->state = DOSE_STATE_SEND;
break;

default:
DEBUG("dose state(): unexpected state transition (STATE=0x%02x SIGNAL=0x%02x)\n", ctx->state, signal);

Check warning on line 321 in drivers/dose/dose.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
/* fall-through */
case DOSE_STATE_RECV + DOSE_SIGNAL_SEND:
signal = DOSE_SIGNAL_NONE;
}
} while (signal != DOSE_SIGNAL_NONE);
Expand Down Expand Up @@ -357,7 +358,7 @@
state(dev, DOSE_SIGNAL_ZTIMER);
break;
default:
;

Check warning on line 361 in drivers/dose/dose.c

View workflow job for this annotation

GitHub Actions / static-tests

semicolon is isolated from other tokens
}
}

Expand Down Expand Up @@ -503,6 +504,9 @@

static inline void _send_start(dose_t *ctx)
{
#ifdef MODULE_PERIPH_UART_TX_ONDEMAND
uart_enable_tx(ctx->uart);
#endif
#ifdef MODULE_PERIPH_UART_COLLISION
uart_collision_detect_enable(ctx->uart);
#else
Expand All @@ -512,6 +516,9 @@

static inline void _send_done(dose_t *ctx, bool collision)
{
#ifdef MODULE_PERIPH_UART_TX_ONDEMAND
uart_disable_tx(ctx->uart);
#endif
#ifdef MODULE_PERIPH_UART_COLLISION
uart_collision_detect_disable(ctx->uart);
if (collision) {
Expand Down
18 changes: 18 additions & 0 deletions drivers/include/periph/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ void uart_poweron(uart_t uart);
*/
void uart_poweroff(uart_t uart);

/**
* @brief Enable the TX line one the given UART
*
* @note requires the `periph_uart_tx_ondemand` feature
*
* @param[in] uart the UART device start TX on
*/
void uart_enable_tx(uart_t uart);

/**
* @brief Disable the TX line one the given UART
*
* @note requires the `periph_uart_tx_ondemand` feature
*
* @param[in] uart the UART device to stop TX on
*/
void uart_disable_tx(uart_t uart);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions drivers/soft_uart/include/soft_uart_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef SOFT_UART_PARAMS_H
#define SOFT_UART_PARAMS_H

#include "board.h"
Copy link
Contributor Author

@benpicco benpicco Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exactly related to this PR, but without it SOFT_UART_PARAM_xx set in board.h would not get applied.

I first thought of using the software UART to invert the line polarity, but then I found I can use a real UART instead if I re-configure the TX pin.

#include "soft_uart.h"
#include "macros/units.h"
#include "kernel_defines.h"
Expand Down
5 changes: 5 additions & 0 deletions kconfigs/Kconfig.features
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@ config HAS_PERIPH_UART_MODECFG
help
Indicates that the UART peripheral allows mode configuration.

config HAS_PERIPH_UART_TX_ONDEMAND
bool
help
Indicates that the UART peripheral can enable the TX line on demmand.

config HAS_PERIPH_UART_NONBLOCKING
bool
help
Expand Down
Loading