Skip to content

rp2040: Bugfix UART initialisation: Mixup of Zephyr constants and rpi-hal constants #52348

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

Merged
Merged
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
106 changes: 58 additions & 48 deletions drivers/serial/uart_rpi_pico.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,60 @@ static void uart_rpi_poll_out(const struct device *dev, unsigned char c)
uart_hw->dr = c;
}

static int uart_rpi_set_format(const struct device *dev, const struct uart_config *cfg)
{
const struct uart_rpi_config *config = dev->config;
uart_inst_t * const uart_inst = config->uart_dev;
uart_parity_t parity = 0;
uint data_bits = 0;
uint stop_bits = 0;

switch (cfg->data_bits) {
case UART_CFG_DATA_BITS_5:
data_bits = 5;
break;
case UART_CFG_DATA_BITS_6:
data_bits = 6;
break;
case UART_CFG_DATA_BITS_7:
data_bits = 7;
break;
case UART_CFG_DATA_BITS_8:
data_bits = 8;
break;
default:
return -EINVAL;
}

switch (cfg->stop_bits) {
case UART_CFG_STOP_BITS_1:
stop_bits = 1;
break;
case UART_CFG_STOP_BITS_2:
stop_bits = 2;
break;
default:
return -EINVAL;
}

switch (cfg->parity) {
case UART_CFG_PARITY_NONE:
parity = UART_PARITY_NONE;
break;
case UART_CFG_PARITY_EVEN:
parity = UART_PARITY_EVEN;
break;
case UART_CFG_PARITY_ODD:
parity = UART_PARITY_ODD;
break;
default:
return -EINVAL;
}

uart_set_format(uart_inst, data_bits, stop_bits, parity);
return 0;
}

static int uart_rpi_init(const struct device *dev)
{
const struct uart_rpi_config *config = dev->config;
Expand Down Expand Up @@ -94,10 +148,7 @@ static int uart_rpi_init(const struct device *dev)
.parity = UART_CFG_PARITY_NONE,
.stop_bits = UART_CFG_STOP_BITS_1
};
uart_set_format(uart_inst,
data->uart_config.data_bits,
data->uart_config.stop_bits,
data->uart_config.parity);
uart_rpi_set_format(dev, &data->uart_config);
hw_clear_bits(&uart_hw->lcr_h, UART_UARTLCR_H_FEN_BITS);
uart_hw->dr = 0U;

Expand All @@ -121,58 +172,17 @@ static int uart_rpi_configure(const struct device *dev, const struct uart_config
const struct uart_rpi_config *config = dev->config;
uart_inst_t * const uart_inst = config->uart_dev;
struct uart_rpi_data *data = dev->data;
uart_parity_t parity = 0;
uint data_bits = 0;
uint stop_bits = 0;
uint baudrate = 0;

switch (cfg->data_bits) {
case UART_CFG_DATA_BITS_5:
data_bits = 5;
break;
case UART_CFG_DATA_BITS_6:
data_bits = 6;
break;
case UART_CFG_DATA_BITS_7:
data_bits = 7;
break;
case UART_CFG_DATA_BITS_8:
data_bits = 8;
break;
default:
return -EINVAL;
}

switch (cfg->stop_bits) {
case UART_CFG_STOP_BITS_1:
stop_bits = 1;
break;
case UART_CFG_STOP_BITS_2:
stop_bits = 2;
break;
default:
baudrate = uart_set_baudrate(uart_inst, cfg->baudrate);
if (baudrate == 0) {
return -EINVAL;
}

switch (cfg->parity) {
case UART_CFG_PARITY_NONE:
parity = UART_PARITY_NONE;
break;
case UART_CFG_PARITY_EVEN:
parity = UART_PARITY_EVEN;
break;
case UART_CFG_PARITY_ODD:
parity = UART_PARITY_ODD;
break;
default:
if (uart_rpi_set_format(dev, cfg) != 0) {
return -EINVAL;
}

baudrate = uart_set_baudrate(uart_inst, cfg->baudrate);
if (baudrate == 0) {
return -EINVAL;
}
uart_set_format(uart_inst, data_bits, stop_bits, parity);
data->uart_config = *cfg;
return 0;
}
Expand Down