Skip to content

Commit

Permalink
stm32 : move tx_enable pin cfg for uart_stream to own function (#25)
Browse files Browse the repository at this point in the history
* stm32 : move tx_enable pin cfg for uart_stream to own function

Also add rx_enable and pin polarity options.
This is needed for RS485 where the receive enable
pin must be disabled while transmitting (in half-duplex mode)
and also has inverted polarity.

---------

Co-authored-by: Per Knytt <[email protected]>
  • Loading branch information
pekenator1 and Per Knytt authored Dec 3, 2024
1 parent 1f68559 commit 4a1d049
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 18 deletions.
29 changes: 21 additions & 8 deletions src/platform/stm32/stm32_uart_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "stm32_uart_stream.h"


static void
stm32_uart_update_cr1(stm32_uart_stream_t *u)
{
Expand Down Expand Up @@ -104,7 +103,10 @@ stm32_uart_write(stream_t *s, const void *buf, size_t size, int flags)
u->tx_busy = 1;

if(u->tx_enable != GPIO_UNUSED && !u->tx_enabled) {
gpio_set_output(u->tx_enable, 1);
gpio_set_output(u->tx_enable, !u->tx_pol_invert);
if(u->rx_enable != GPIO_UNUSED)
gpio_set_output(u->rx_enable, u->rx_pol_invert);

u->tx_enabled = 1;
}

Expand Down Expand Up @@ -199,14 +201,17 @@ uart_irq(void *arg)
}
}

if(sr & (1 << 6)) {
if(sr & (1 << 6)) { // Transmission complete
if(u->tx_enable != GPIO_UNUSED) {
gpio_set_output(u->tx_enable, 0);
gpio_set_output(u->tx_enable, u->tx_pol_invert);
if(u->rx_enable != GPIO_UNUSED)
gpio_set_output(u->rx_enable, !u->rx_pol_invert);

u->tx_enabled = 0;
}
}

if(sr & (1 << 7)) {
if(sr & (1 << 7)) { // TX reg empty
uint8_t avail = u->tx_fifo_wrptr - u->tx_fifo_rdptr;
if(avail == 0) {
u->tx_busy = 0;
Expand All @@ -232,15 +237,24 @@ stm32_uart_print_info(struct device *dev, struct stream *st)
u->rx_framing_error);
}

void
stm32_uart_set_io_ctrl(stream_t *s, gpio_t tx_enable, int tx_pol_invert,
gpio_t rx_enable, int rx_pol_invert)
{
stm32_uart_stream_t *u = (stm32_uart_stream_t *)s;
u->tx_enable = tx_enable;
u->tx_pol_invert = !!tx_pol_invert;
u->rx_enable = rx_enable;
u->rx_pol_invert = !!rx_pol_invert;
}

static const device_class_t stm32_uart_class = {
.dc_print_info = stm32_uart_print_info,
};

stm32_uart_stream_t *
stm32_uart_stream_init(stm32_uart_stream_t *u, int reg_base, int baudrate,
int clkid, int irq, uint8_t flags, gpio_t tx_enable,
const char *name)
int clkid, int irq, uint8_t flags, const char *name)
{
if(u == NULL)
u = calloc(1, sizeof(stm32_uart_stream_t));
Expand All @@ -253,7 +267,6 @@ stm32_uart_stream_init(stm32_uart_stream_t *u, int reg_base, int baudrate,

u->reg_base = reg_base;
u->flags = flags;
u->tx_enable = tx_enable;

const unsigned int freq = clk_get_freq(clkid);
const unsigned int bbr = (freq + baudrate - 1) / baudrate;
Expand Down
4 changes: 4 additions & 0 deletions src/platform/stm32/stm32_uart_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ typedef struct stm32_uart_stream {
uint8_t flags;

gpio_t tx_enable;
uint8_t tx_pol_invert;
uint8_t tx_enabled;

gpio_t rx_enable;
uint8_t rx_pol_invert;

uint8_t rx_fifo_rdptr;
uint8_t rx_fifo_wrptr;
uint8_t tx_fifo_rdptr;
Expand Down
9 changes: 6 additions & 3 deletions src/platform/stm32f4/stm32f4_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ typedef struct {
uint32_t rxdma;
} stm32f4_uart_config_t;

stream_t *stm32f4_uart_stream_init(struct stm32_uart_stream *uart,
int instance, int baudrate,
gpio_t tx, gpio_t rx, gpio_t tx_enable,
stream_t *stm32f4_uart_stream_init(struct stm32_uart_stream *uart, int instance,
int baudrate, gpio_t tx, gpio_t rx,
uint8_t flags, const char *name);

void stm32f4_uart_mbus_multidrop_create(unsigned int instance,
gpio_t tx, gpio_t rx, gpio_t txe,
const char *name);

void stm32f4_uart_set_io_ctrl(stream_t *uart, gpio_t tx_enable,
int tx_pol_invert, gpio_t rx_enable,
int rx_pol_invert);

const stm32f4_uart_config_t *stm32f4_uart_get_config(int index);
11 changes: 8 additions & 3 deletions src/platform/stm32f4/stm32f4_uart_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ stm32f4_uart_get_config(int index)

static stm32_uart_stream_t *uarts[6];

void
stm32f4_uart_set_io_ctrl(stream_t *s, gpio_t tx_enable, int tx_pol_invert,
gpio_t rx_enable, int rx_pol_invert)
{
stm32_uart_set_io_ctrl(s, tx_enable, tx_pol_invert, rx_enable, rx_pol_invert);
}

stream_t *
stm32f4_uart_stream_init(stm32_uart_stream_t *u, int instance, int baudrate,
gpio_t tx, gpio_t rx, gpio_t tx_enable, uint8_t flags,
const char *name)
gpio_t tx, gpio_t rx, uint8_t flags, const char *name)
{
const int index = instance - 1;
const stm32f4_uart_config_t *cfg = stm32f4_uart_get_config(index);
Expand All @@ -46,7 +52,6 @@ stm32f4_uart_stream_init(stm32_uart_stream_t *u, int instance, int baudrate,
cfg->clkid,
cfg->irq,
flags,
tx_enable,
name);

uarts[index] = u;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/stm32f405-feather/stm32f405-feather.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ board_init_console(void)
{
static stm32_uart_stream_t console;
stdio = stm32f4_uart_stream_init(&console, 6, 115200, GPIO_PC(6), GPIO_PC(7),
GPIO_UNUSED, UART_CTRLD_IS_PANIC, "console");
UART_CTRLD_IS_PANIC, "console");
}


Expand Down
2 changes: 1 addition & 1 deletion src/platform/stm32f407g-disc1/stm32f407g-disc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ board_init_console(void)
{
static stm32_uart_stream_t console;
stdio = stm32f4_uart_stream_init(&console, 2, 115200, GPIO_PA(2), GPIO_PA(3),
GPIO_UNUSED, UART_CTRLD_IS_PANIC, "console");
UART_CTRLD_IS_PANIC, "console");
}


Expand Down
2 changes: 1 addition & 1 deletion src/platform/stm32f439-nucleo144/stm32f439-nucleo144.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ board_init_console(void)
{
static stm32_uart_stream_t console;
stdio = stm32f4_uart_stream_init(&console, 3, 115200, GPIO_PD(8), GPIO_PD(9),
GPIO_UNUSED, UART_CTRLD_IS_PANIC, "console");
UART_CTRLD_IS_PANIC, "console");
}

static const uint8_t stm32f439_nucleo144_ethernet_gpios[] =
Expand Down
1 change: 0 additions & 1 deletion src/platform/stm32g0/stm32g0_uart_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ stm32g0_uart_stream_init(stm32_uart_stream_t *u, unsigned int instance,
cfg->clkid,
cfg->irq,
flags,
GPIO_UNUSED,
name);

uarts[instance - 1] = u;
Expand Down

0 comments on commit 4a1d049

Please sign in to comment.