9
9
#include <zephyr/drivers/reset.h>
10
10
#include <zephyr/drivers/pinctrl.h>
11
11
#include <zephyr/irq.h>
12
+ #include <string.h>
12
13
13
14
/* pico-sdk includes */
14
15
#include <hardware/uart.h>
@@ -26,7 +27,7 @@ struct uart_rpi_config {
26
27
};
27
28
28
29
struct uart_rpi_data {
29
- uint32_t baudrate ;
30
+ struct uart_config uart_config ;
30
31
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
31
32
uart_irq_callback_user_data_t irq_cb ;
32
33
void * irq_cb_data ;
@@ -64,7 +65,8 @@ static int uart_rpi_init(const struct device *dev)
64
65
uart_inst_t * const uart_inst = config -> uart_dev ;
65
66
uart_hw_t * const uart_hw = config -> uart_regs ;
66
67
struct uart_rpi_data * const data = dev -> data ;
67
- int ret , baudrate ;
68
+ uint baudrate ;
69
+ int ret ;
68
70
69
71
ret = pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_DEFAULT );
70
72
if (ret < 0 ) {
@@ -75,12 +77,27 @@ static int uart_rpi_init(const struct device *dev)
75
77
* uart_init() may be replaced by register based API once rpi-pico platform
76
78
* has a clock controller driver
77
79
*/
78
- baudrate = uart_init (uart_inst , data -> baudrate );
80
+ baudrate = uart_init (uart_inst , data -> uart_config . baudrate );
79
81
/* Check if baudrate adjustment returned by 'uart_init' function is a positive value */
80
- if (baudrate < = 0 ) {
82
+ if (baudrate = = 0 ) {
81
83
return - EINVAL ;
82
84
}
83
-
85
+ /*
86
+ * initialize uart_config with hardware reset values
87
+ * https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf#tab-registerlist_uart page:431
88
+ * data bits set default to 8 instaed of hardware reset 5 to increase compatibility.
89
+ */
90
+ data -> uart_config = (struct uart_config ){
91
+ .baudrate = baudrate ,
92
+ .data_bits = UART_CFG_DATA_BITS_8 ,
93
+ .flow_ctrl = UART_CFG_FLOW_CTRL_NONE ,
94
+ .parity = UART_CFG_PARITY_NONE ,
95
+ .stop_bits = UART_CFG_STOP_BITS_1
96
+ };
97
+ uart_set_format (uart_inst ,
98
+ data -> uart_config .data_bits ,
99
+ data -> uart_config .stop_bits ,
100
+ data -> uart_config .parity );
84
101
hw_clear_bits (& uart_hw -> lcr_h , UART_UARTLCR_H_FEN_BITS );
85
102
uart_hw -> dr = 0U ;
86
103
@@ -91,6 +108,75 @@ static int uart_rpi_init(const struct device *dev)
91
108
return 0 ;
92
109
}
93
110
111
+ static int uart_rpi_config_get (const struct device * dev , struct uart_config * cfg )
112
+ {
113
+ struct uart_rpi_data * data = dev -> data ;
114
+
115
+ memcpy (cfg , & data -> uart_config , sizeof (struct uart_config ));
116
+ return 0 ;
117
+ }
118
+
119
+ static int uart_rpi_configure (const struct device * dev , const struct uart_config * cfg )
120
+ {
121
+ const struct uart_rpi_config * config = dev -> config ;
122
+ uart_inst_t * const uart_inst = config -> uart_dev ;
123
+ struct uart_rpi_data * data = dev -> data ;
124
+ uart_parity_t parity = 0 ;
125
+ uint data_bits = 0 ;
126
+ uint stop_bits = 0 ;
127
+ uint baudrate = 0 ;
128
+
129
+ switch (cfg -> data_bits ) {
130
+ case UART_CFG_DATA_BITS_5 :
131
+ data_bits = 5 ;
132
+ break ;
133
+ case UART_CFG_DATA_BITS_6 :
134
+ data_bits = 6 ;
135
+ break ;
136
+ case UART_CFG_DATA_BITS_7 :
137
+ data_bits = 7 ;
138
+ break ;
139
+ case UART_CFG_DATA_BITS_8 :
140
+ data_bits = 8 ;
141
+ break ;
142
+ default :
143
+ return - EINVAL ;
144
+ }
145
+
146
+ switch (cfg -> stop_bits ) {
147
+ case UART_CFG_STOP_BITS_1 :
148
+ stop_bits = 1 ;
149
+ break ;
150
+ case UART_CFG_STOP_BITS_2 :
151
+ stop_bits = 2 ;
152
+ break ;
153
+ default :
154
+ return - EINVAL ;
155
+ }
156
+
157
+ switch (cfg -> parity ) {
158
+ case UART_CFG_PARITY_NONE :
159
+ parity = UART_PARITY_NONE ;
160
+ break ;
161
+ case UART_CFG_PARITY_EVEN :
162
+ parity = UART_PARITY_EVEN ;
163
+ break ;
164
+ case UART_CFG_PARITY_ODD :
165
+ parity = UART_PARITY_ODD ;
166
+ break ;
167
+ default :
168
+ return - EINVAL ;
169
+ }
170
+
171
+ baudrate = uart_set_baudrate (uart_inst , cfg -> baudrate );
172
+ if (baudrate == 0 ) {
173
+ return - EINVAL ;
174
+ }
175
+ uart_set_format (uart_inst , data_bits , stop_bits , parity );
176
+ data -> uart_config = * cfg ;
177
+ return 0 ;
178
+ }
179
+
94
180
static int uart_rpi_err_check (const struct device * dev )
95
181
{
96
182
const struct uart_rpi_config * const config = dev -> config ;
@@ -264,6 +350,10 @@ static const struct uart_driver_api uart_rpi_driver_api = {
264
350
.poll_in = uart_rpi_poll_in ,
265
351
.poll_out = uart_rpi_poll_out ,
266
352
.err_check = uart_rpi_err_check ,
353
+ #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
354
+ .configure = uart_rpi_configure ,
355
+ .config_get = uart_rpi_config_get ,
356
+ #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
267
357
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
268
358
.fifo_fill = uart_rpi_fifo_fill ,
269
359
.fifo_read = uart_rpi_fifo_read ,
@@ -310,7 +400,7 @@ static const struct uart_driver_api uart_rpi_driver_api = {
310
400
}; \
311
401
\
312
402
static struct uart_rpi_data uart##idx##_rpi_data = { \
313
- .baudrate = DT_INST_PROP(idx, current_speed), \
403
+ .uart_config. baudrate = DT_INST_PROP(idx, current_speed), \
314
404
}; \
315
405
\
316
406
DEVICE_DT_INST_DEFINE(idx, &uart_rpi_init, \
0 commit comments