Skip to content

Commit 94b72e7

Browse files
Mierunskicarlescufi
authored andcommitted
drivers: nrf: Add UART and UARTE configure function.
Add UART configure function for UART and UARTE shims. Signed-off-by: Mieszko Mierunski <[email protected]>
1 parent 9643ed6 commit 94b72e7

File tree

2 files changed

+224
-67
lines changed

2 files changed

+224
-67
lines changed

drivers/serial/uart_nrfx_uart.c

Lines changed: 114 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@
1616
static NRF_UART_Type *const uart0_addr =
1717
(NRF_UART_Type *)DT_NORDIC_NRF_UART_UART_0_BASE_ADDRESS;
1818

19+
/* Device data structure */
20+
struct uart_nrfx_data {
21+
struct uart_config uart_config;
22+
};
23+
24+
/**
25+
* @brief Structure for UART configuration.
26+
*/
27+
struct uart_nrfx_config {
28+
bool rts_cts_pins_set;
29+
};
30+
31+
static inline struct uart_nrfx_data *get_dev_data(struct device *dev)
32+
{
33+
return dev->driver_data;
34+
}
35+
36+
static inline const struct uart_nrfx_config *get_dev_config(struct device *dev)
37+
{
38+
return dev->config->config_info;
39+
}
40+
1941
#ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
2042

2143
static uart_irq_callback_user_data_t irq_callback; /**< Callback function pointer */
@@ -216,6 +238,62 @@ static int uart_nrfx_err_check(struct device *dev)
216238
return error;
217239
}
218240

241+
static int uart_nrfx_configure(struct device *dev,
242+
const struct uart_config *cfg)
243+
{
244+
nrf_uart_parity_t parity;
245+
nrf_uart_hwfc_t hwfc;
246+
247+
if (cfg->stop_bits != UART_CFG_STOP_BITS_1) {
248+
return -ENOTSUP;
249+
}
250+
251+
if (cfg->data_bits != UART_CFG_DATA_BITS_8) {
252+
return -ENOTSUP;
253+
}
254+
255+
switch (cfg->flow_ctrl) {
256+
case UART_CFG_FLOW_CTRL_NONE:
257+
hwfc = NRF_UART_HWFC_DISABLED;
258+
break;
259+
case UART_CFG_FLOW_CTRL_RTS_CTS:
260+
if (get_dev_config(dev)->rts_cts_pins_set) {
261+
hwfc = NRF_UART_HWFC_ENABLED;
262+
} else {
263+
return -ENOTSUP;
264+
}
265+
break;
266+
default:
267+
return -ENOTSUP;
268+
}
269+
270+
switch (cfg->parity) {
271+
case UART_CFG_PARITY_NONE:
272+
parity = NRF_UART_PARITY_EXCLUDED;
273+
break;
274+
case UART_CFG_PARITY_EVEN:
275+
parity = NRF_UART_PARITY_INCLUDED;
276+
break;
277+
default:
278+
return -ENOTSUP;
279+
}
280+
281+
if (baudrate_set(dev, cfg->baudrate) != 0) {
282+
return -ENOTSUP;
283+
}
284+
285+
nrf_uart_configure(uart0_addr, parity, hwfc);
286+
287+
get_dev_data(dev)->uart_config = *cfg;
288+
289+
return 0;
290+
}
291+
292+
static int uart_nrfx_config_get(struct device *dev, struct uart_config *cfg)
293+
{
294+
*cfg = get_dev_data(dev)->uart_config;
295+
return 0;
296+
}
219297

220298
#ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
221299

@@ -414,14 +492,8 @@ static int uart_nrfx_init(struct device *dev)
414492
nrf_uart_txrx_pins_set(uart0_addr,
415493
DT_NORDIC_NRF_UART_UART_0_TX_PIN,
416494
DT_NORDIC_NRF_UART_UART_0_RX_PIN);
417-
418-
#ifdef CONFIG_UART_0_NRF_FLOW_CONTROL
419-
#ifndef DT_NORDIC_NRF_UART_UART_0_RTS_PIN
420-
#error Flow control for UART0 is enabled, but RTS pin is not defined.
421-
#endif
422-
#ifndef DT_NORDIC_NRF_UART_UART_0_CTS_PIN
423-
#error Flow control for UART0 is enabled, but CTS pin is not defined.
424-
#endif
495+
#if defined(DT_NORDIC_NRF_UART_UART_0_RTS_PIN) && \
496+
defined(DT_NORDIC_NRF_UART_UART_0_CTS_PIN)
425497
/* Setting default height state of the RTS PIN to avoid glitches
426498
* on the line during peripheral activation/deactivation.
427499
*/
@@ -434,22 +506,10 @@ static int uart_nrfx_init(struct device *dev)
434506
nrf_uart_hwfc_pins_set(uart0_addr,
435507
DT_NORDIC_NRF_UART_UART_0_RTS_PIN,
436508
DT_NORDIC_NRF_UART_UART_0_CTS_PIN);
437-
#endif /* CONFIG_UART_0_NRF_FLOW_CONTROL */
438-
439-
nrf_uart_configure(uart0_addr,
440-
#ifdef CONFIG_UART_0_NRF_PARITY_BIT
441-
NRF_UART_PARITY_INCLUDED,
442-
#else
443-
NRF_UART_PARITY_EXCLUDED,
444-
#endif /* CONFIG_UART_0_NRF_PARITY_BIT */
445-
#ifdef CONFIG_UART_0_NRF_FLOW_CONTROL
446-
NRF_UART_HWFC_ENABLED);
447-
#else
448-
NRF_UART_HWFC_DISABLED);
449-
#endif /* CONFIG_UART_0_NRF_PARITY_BIT */
509+
#endif
450510

451-
/* Set baud rate */
452-
err = baudrate_set(dev, DT_NORDIC_NRF_UART_UART_0_CURRENT_SPEED);
511+
/* Set initial configuration */
512+
err = uart_nrfx_configure(dev, &get_dev_data(dev)->uart_config);
453513
if (err) {
454514
return err;
455515
}
@@ -488,6 +548,8 @@ static const struct uart_driver_api uart_nrfx_uart_driver_api = {
488548
.poll_in = uart_nrfx_poll_in,
489549
.poll_out = uart_nrfx_poll_out,
490550
.err_check = uart_nrfx_err_check,
551+
.configure = uart_nrfx_configure,
552+
.config_get = uart_nrfx_config_get,
491553
#ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
492554
.fifo_fill = uart_nrfx_fifo_fill,
493555
.fifo_read = uart_nrfx_fifo_read,
@@ -542,12 +604,39 @@ static int uart_nrfx_pm_control(struct device *dev,
542604
}
543605
#endif /* CONFIG_DEVICE_POWER_MANAGEMENT */
544606

607+
static struct uart_nrfx_data uart_nrfx_uart0_data = {
608+
.uart_config = {
609+
.stop_bits = UART_CFG_STOP_BITS_1,
610+
.data_bits = UART_CFG_DATA_BITS_8,
611+
.baudrate = DT_NORDIC_NRF_UART_UART_0_CURRENT_SPEED,
612+
#ifdef CONFIG_UART_0_NRF_PARITY_BIT
613+
.parity = UART_CFG_PARITY_EVEN,
614+
#else
615+
.parity = UART_CFG_PARITY_NONE,
616+
#endif /* CONFIG_UART_0_NRF_PARITY_BIT */
617+
#ifdef CONFIG_UART_0_NRF_FLOW_CONTROL
618+
.flow_ctrl = UART_CFG_FLOW_CTRL_RTS_CTS,
619+
#else
620+
.flow_ctrl = UART_CFG_FLOW_CTRL_NONE,
621+
#endif /* CONFIG_UART_0_NRF_FLOW_CONTROL */
622+
}
623+
};
624+
625+
static const struct uart_nrfx_config uart_nrfx_uart0_config = {
626+
#if defined(DT_NORDIC_NRF_UART_UART_0_RTS_PIN) && \
627+
defined(DT_NORDIC_NRF_UART_UART_0_CTS_PIN)
628+
.rts_cts_pins_set = true,
629+
#else
630+
.rts_cts_pins_set = false,
631+
#endif
632+
};
633+
545634
DEVICE_DEFINE(uart_nrfx_uart0,
546635
DT_NORDIC_NRF_UART_UART_0_LABEL,
547636
uart_nrfx_init,
548637
uart_nrfx_pm_control,
549-
NULL,
550-
NULL,
638+
&uart_nrfx_uart0_data,
639+
&uart_nrfx_uart0_config,
551640
/* Initialize UART device before UART console. */
552641
PRE_KERNEL_1,
553642
CONFIG_KERNEL_INIT_PRIORITY_DEVICE,

0 commit comments

Comments
 (0)