1 /**
2 * @brief UART Driver for interacting with host serial ports
3 *
4 * @note Driver can open and send characters to the host serial ports (such as /dev/ttyUSB0 or
5 * /dev/ttyACM0). Only polling Uart API is implemented. Driver can be configured via devicetree,
6 * command line options or at runtime.
7 *
8 * To learn more see Native TTY section at:
9 * https://docs.zephyrproject.org/latest/boards/posix/native_sim/doc/index.html
10 * or
11 * ${ZEPHYR_BASE}/boards/posix/native_sim/doc/index.rst
12 *
13 * Copyright (c) 2023 Marko Sagadin
14 * SPDX-License-Identifier: Apache-2.0
15 */
16
17 #include <zephyr/device.h>
18 #include <zephyr/drivers/uart.h>
19 #include <zephyr/kernel.h>
20
21 #include <nsi_errno.h>
22 #include <nsi_tracing.h>
23
24 #include "cmdline.h"
25 #include "posix_native_task.h"
26 #include "uart_native_tty_bottom.h"
27 #include "nsi_host_trampolines.h"
28
29 #define WARN(...) nsi_print_warning(__VA_ARGS__)
30 #define ERROR(...) nsi_print_error_and_exit(__VA_ARGS__)
31
32 #define DT_DRV_COMPAT zephyr_native_tty_uart
33
34 struct native_tty_data {
35 /* File descriptor used for the tty device. */
36 int fd;
37 /* Absolute path to the tty device. */
38 char *serial_port;
39 /* Baudrate set from the command line. If UINT32_MAX, it was not set. */
40 int cmd_baudrate;
41 /* Serial port set from the command line. If NULL, it was not set. */
42 char *cmd_serial_port;
43 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
44 /* Emulated tx irq is enabled. */
45 bool tx_irq_enabled;
46 /* Emulated rx irq is enabled. */
47 bool rx_irq_enabled;
48 /* IRQ callback */
49 uart_irq_callback_user_data_t callback;
50 /* IRQ callback data */
51 void *cb_data;
52 /* Instance-specific RX thread. */
53 struct k_thread rx_thread;
54 /* RX thread stack. */
55 K_KERNEL_STACK_MEMBER(rx_stack, CONFIG_ARCH_POSIX_RECOMMENDED_STACK_SIZE);
56 #endif
57 };
58
59 struct native_tty_config {
60 struct uart_config uart_config;
61 };
62
63 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
64 #define NATIVE_TTY_INIT_LEVEL POST_KERNEL
65 #else
66 #define NATIVE_TTY_INIT_LEVEL PRE_KERNEL_1
67 #endif
68
69 /**
70 * @brief Convert from uart_config to native_tty_bottom_cfg eqvivalent struct
71 *
72 * @param bottom_cfg
73 * @param cfg
74 *
75 * @return 0 on success, negative errno otherwise.
76 */
native_tty_conv_to_bottom_cfg(struct native_tty_bottom_cfg * bottom_cfg,const struct uart_config * cfg)77 static int native_tty_conv_to_bottom_cfg(struct native_tty_bottom_cfg *bottom_cfg,
78 const struct uart_config *cfg)
79 {
80 bottom_cfg->baudrate = cfg->baudrate;
81
82 switch (cfg->parity) {
83 case UART_CFG_PARITY_NONE:
84 bottom_cfg->parity = NTB_PARITY_NONE;
85 break;
86 case UART_CFG_PARITY_ODD:
87 bottom_cfg->parity = NTB_PARITY_ODD;
88 break;
89 case UART_CFG_PARITY_EVEN:
90 bottom_cfg->parity = NTB_PARITY_EVEN;
91 break;
92 default:
93 return -ENOTSUP;
94 }
95
96 switch (cfg->stop_bits) {
97 case UART_CFG_STOP_BITS_1:
98 bottom_cfg->stop_bits = NTB_STOP_BITS_1;
99 break;
100 case UART_CFG_STOP_BITS_2:
101 bottom_cfg->stop_bits = NTB_STOP_BITS_2;
102 break;
103 default:
104 return -ENOTSUP;
105 }
106
107 switch (cfg->data_bits) {
108 case UART_CFG_DATA_BITS_5:
109 bottom_cfg->data_bits = NTB_DATA_BITS_5;
110 break;
111 case UART_CFG_DATA_BITS_6:
112 bottom_cfg->data_bits = NTB_DATA_BITS_6;
113 break;
114 case UART_CFG_DATA_BITS_7:
115 bottom_cfg->data_bits = NTB_DATA_BITS_7;
116 break;
117 case UART_CFG_DATA_BITS_8:
118 bottom_cfg->data_bits = NTB_DATA_BITS_8;
119 break;
120 default:
121 return -ENOTSUP;
122 }
123
124 if (cfg->flow_ctrl != UART_CFG_FLOW_CTRL_NONE) {
125 WARN("Could not set flow control, any kind of hw flow control is not supported.\n");
126 return -ENOTSUP;
127 }
128
129 bottom_cfg->flow_ctrl = NTB_FLOW_CTRL_NONE;
130
131 return 0;
132 }
133
134 /**
135 * @brief Convert from native_tty_bottom_cfg to uart_config
136 *
137 * @param bottom_cfg
138 * @param cfg
139 *
140 * @return 0 on success, negative errno otherwise.
141 */
native_tty_conv_from_bottom_cfg(int fd,struct uart_config * cfg)142 int native_tty_conv_from_bottom_cfg(int fd, struct uart_config *cfg)
143 {
144 struct native_tty_bottom_cfg bottom_cfg;
145 int rc = 0;
146
147 rc = native_tty_read_bottom_cfg(fd, &bottom_cfg);
148 if (rc != 0) {
149 return nsi_errno_from_mid(rc);
150 }
151
152 cfg->baudrate = bottom_cfg.baudrate;
153
154 switch (bottom_cfg.parity) {
155 case NTB_PARITY_NONE:
156 cfg->parity = UART_CFG_PARITY_NONE;
157 break;
158 case NTB_PARITY_ODD:
159 cfg->parity = UART_CFG_PARITY_ODD;
160 break;
161 case NTB_PARITY_EVEN:
162 cfg->parity = UART_CFG_PARITY_EVEN;
163 break;
164 default:
165 return -ENOTSUP;
166 }
167
168 switch (bottom_cfg.stop_bits) {
169 case NTB_STOP_BITS_1:
170 cfg->stop_bits = UART_CFG_STOP_BITS_1;
171 break;
172 case NTB_STOP_BITS_2:
173 cfg->stop_bits = UART_CFG_STOP_BITS_2;
174 break;
175 default:
176 return -ENOTSUP;
177 }
178
179 switch (bottom_cfg.data_bits) {
180 case NTB_DATA_BITS_5:
181 cfg->data_bits = UART_CFG_DATA_BITS_5;
182 break;
183 case NTB_DATA_BITS_6:
184 cfg->data_bits = UART_CFG_DATA_BITS_6;
185 break;
186 case NTB_DATA_BITS_7:
187 cfg->data_bits = UART_CFG_DATA_BITS_7;
188 break;
189 case NTB_DATA_BITS_8:
190 cfg->data_bits = UART_CFG_DATA_BITS_8;
191 break;
192 default:
193 return -ENOTSUP;
194 }
195
196 cfg->flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
197
198 return 0;
199 }
200
201 /*
202 * @brief Output a character towards the serial port
203 *
204 * @param dev UART device structure.
205 * @param out_char Character to send.
206 */
native_tty_uart_poll_out(const struct device * dev,unsigned char out_char)207 static void native_tty_uart_poll_out(const struct device *dev, unsigned char out_char)
208 {
209 struct native_tty_data *data = dev->data;
210
211 int ret = nsi_host_write(data->fd, &out_char, 1);
212
213 if (ret == -1) {
214 ERROR("Could not write to %s\n", data->serial_port);
215 }
216 }
217
218 /**
219 * @brief Poll the device for input.
220 *
221 * @param dev UART device structure.
222 * @param p_char Pointer to a character.
223 *
224 * @retval 0 If a character arrived.
225 * @retval -1 If no character was available to read.
226 */
native_tty_uart_poll_in(const struct device * dev,unsigned char * p_char)227 static int native_tty_uart_poll_in(const struct device *dev, unsigned char *p_char)
228 {
229 struct native_tty_data *data = dev->data;
230
231 return nsi_host_read(data->fd, p_char, 1) > 0 ? 0 : -1;
232 }
233
native_tty_configure(const struct device * dev,const struct uart_config * cfg)234 static int native_tty_configure(const struct device *dev, const struct uart_config *cfg)
235 {
236 int fd = ((struct native_tty_data *)dev->data)->fd;
237 struct native_tty_bottom_cfg bottom_cfg;
238
239 int rc = native_tty_conv_to_bottom_cfg(&bottom_cfg, cfg);
240 if (rc) {
241 WARN("Could not convert uart config to native tty bottom cfg\n");
242 return rc;
243 }
244
245 return native_tty_configure_bottom(fd, &bottom_cfg);
246 }
247
native_tty_config_get(const struct device * dev,struct uart_config * cfg)248 static int native_tty_config_get(const struct device *dev, struct uart_config *cfg)
249 {
250 int fd = ((struct native_tty_data *)dev->data)->fd;
251 int rc = 0;
252
253 rc = native_tty_conv_from_bottom_cfg(fd, cfg);
254 if (rc) {
255 WARN("Could not convert native tty bottom cfg to uart config\n");
256 return rc;
257 }
258
259 return 0;
260 }
261
262 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
native_tty_uart_fifo_fill(const struct device * dev,const uint8_t * tx_data,int size)263 static int native_tty_uart_fifo_fill(const struct device *dev,
264 const uint8_t *tx_data,
265 int size)
266 {
267 struct native_tty_data *data = dev->data;
268
269 return nsi_host_write(data->fd, (const void *)tx_data, size);
270 }
271
native_tty_uart_fifo_read(const struct device * dev,uint8_t * rx_data,const int size)272 static int native_tty_uart_fifo_read(const struct device *dev,
273 uint8_t *rx_data,
274 const int size)
275 {
276 struct native_tty_data *data = dev->data;
277
278 return nsi_host_read(data->fd, rx_data, size);
279 }
280
native_tty_uart_irq_tx_ready(const struct device * dev)281 static int native_tty_uart_irq_tx_ready(const struct device *dev)
282 {
283 struct native_tty_data *data = dev->data;
284
285 return data->tx_irq_enabled ? 1 : 0;
286 }
287
native_tty_uart_irq_tx_complete(const struct device * dev)288 static int native_tty_uart_irq_tx_complete(const struct device *dev)
289 {
290 ARG_UNUSED(dev);
291 return 1;
292 }
293
native_tty_uart_irq_tx_enable(const struct device * dev)294 static void native_tty_uart_irq_tx_enable(const struct device *dev)
295 {
296 struct native_tty_data *data = dev->data;
297
298 data->tx_irq_enabled = true;
299 }
300
native_tty_uart_irq_tx_disable(const struct device * dev)301 static void native_tty_uart_irq_tx_disable(const struct device *dev)
302 {
303 struct native_tty_data *data = dev->data;
304
305 data->tx_irq_enabled = false;
306 }
307
native_tty_uart_irq_rx_enable(const struct device * dev)308 static void native_tty_uart_irq_rx_enable(const struct device *dev)
309 {
310 struct native_tty_data *data = dev->data;
311
312 data->rx_irq_enabled = true;
313 }
314
native_tty_uart_irq_rx_disable(const struct device * dev)315 static void native_tty_uart_irq_rx_disable(const struct device *dev)
316 {
317 struct native_tty_data *data = dev->data;
318
319 data->rx_irq_enabled = false;
320 }
321
native_tty_uart_irq_rx_ready(const struct device * dev)322 static int native_tty_uart_irq_rx_ready(const struct device *dev)
323 {
324 struct native_tty_data *data = dev->data;
325
326 if (data->rx_irq_enabled && native_tty_poll_bottom(data->fd) == 1) {
327 return 1;
328 }
329 return 0;
330 }
331
native_tty_uart_irq_is_pending(const struct device * dev)332 static int native_tty_uart_irq_is_pending(const struct device *dev)
333 {
334 return native_tty_uart_irq_rx_ready(dev) ||
335 native_tty_uart_irq_tx_ready(dev);
336 }
337
native_tty_uart_irq_update(const struct device * dev)338 static int native_tty_uart_irq_update(const struct device *dev)
339 {
340 ARG_UNUSED(dev);
341 return 1;
342 }
343
native_tty_uart_irq_handler(const struct device * dev)344 static void native_tty_uart_irq_handler(const struct device *dev)
345 {
346 struct native_tty_data *data = dev->data;
347
348 if (data->callback) {
349 data->callback(dev, data->cb_data);
350 } else {
351 WARN("No callback!\n");
352 }
353 }
354
355 /*
356 * Emulate uart interrupts using a polling thread
357 */
native_tty_uart_irq_function(void * arg1,void * arg2,void * arg3)358 void native_tty_uart_irq_function(void *arg1, void *arg2, void *arg3)
359 {
360 ARG_UNUSED(arg2);
361 ARG_UNUSED(arg3);
362 struct device *dev = (struct device *)arg1;
363 struct native_tty_data *data = dev->data;
364
365 while (1) {
366 if (data->rx_irq_enabled) {
367 int ret = native_tty_poll_bottom(data->fd);
368
369 if (ret == 1) {
370 native_tty_uart_irq_handler(dev);
371 } else if (ret < 0) {
372 WARN("Poll returned error %d\n", ret);
373 } else {
374 k_sleep(K_MSEC(1));
375 }
376 }
377 if (data->tx_irq_enabled) {
378 native_tty_uart_irq_handler(dev);
379 }
380 if (data->tx_irq_enabled == false && data->rx_irq_enabled == false) {
381 k_sleep(K_MSEC(10));
382 }
383 }
384 }
385
native_tty_uart_irq_callback_set(const struct device * dev,uart_irq_callback_user_data_t cb,void * cb_data)386 static void native_tty_uart_irq_callback_set(const struct device *dev,
387 uart_irq_callback_user_data_t cb,
388 void *cb_data)
389 {
390 struct native_tty_data *data = dev->data;
391
392 data->callback = cb;
393 data->cb_data = cb_data;
394 }
395
native_tty_irq_init(const struct device * dev)396 static void native_tty_irq_init(const struct device *dev)
397 {
398 struct native_tty_data *data = dev->data;
399
400 /* Create a thread which will wait for data - replacement for IRQ */
401 k_thread_create(&data->rx_thread, data->rx_stack, K_KERNEL_STACK_SIZEOF(data->rx_stack),
402 native_tty_uart_irq_function,
403 (void *)dev, NULL, NULL,
404 K_HIGHEST_THREAD_PRIO, 0, K_NO_WAIT);
405 }
406 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
407
native_tty_serial_init(const struct device * dev)408 static int native_tty_serial_init(const struct device *dev)
409 {
410 struct native_tty_data *data = dev->data;
411 struct uart_config uart_config = ((struct native_tty_config *)dev->config)->uart_config;
412
413 /* Default value for cmd_serial_port is NULL, this is due to the set 's' type in
414 * command line opts. If it is anything else then it was configured via command
415 * line.
416 */
417 if (data->cmd_serial_port) {
418 data->serial_port = data->cmd_serial_port;
419 }
420
421 /* Default value for cmd_baudrate is UINT32_MAX, this is due to the set 'u' type in
422 * command line opts. If it is anything else then it was configured via command
423 * line.
424 */
425 if (data->cmd_baudrate != UINT32_MAX) {
426 uart_config.baudrate = data->cmd_baudrate;
427 }
428
429 /* Serial port needs to be set either in the devicetree or provided via command line
430 * opts, if that is not the case, then abort.
431 */
432 if (!data->serial_port) {
433 ERROR("%s: path to the serial port was not set.\n", dev->name);
434 }
435
436 /* Try to open a serial port as with read/write access, also prevent serial port
437 * from becoming the controlling terminal.
438 */
439
440 data->fd = native_tty_open_tty_bottom(data->serial_port);
441
442 if (native_tty_configure(dev, &uart_config)) {
443 ERROR("%s: could not configure serial port %s\n", dev->name, data->serial_port);
444 }
445
446 posix_print_trace("%s connected to the serial port: %s\n", dev->name, data->serial_port);
447
448 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
449 /* Start irq emulation thread */
450 native_tty_irq_init(dev);
451 #endif
452 return 0;
453 }
454
455 static DEVICE_API(uart, native_tty_uart_driver_api) = {
456 .poll_out = native_tty_uart_poll_out,
457 .poll_in = native_tty_uart_poll_in,
458 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
459 .configure = native_tty_configure,
460 .config_get = native_tty_config_get,
461 #endif
462 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
463 .fifo_fill = native_tty_uart_fifo_fill,
464 .fifo_read = native_tty_uart_fifo_read,
465 .irq_tx_enable = native_tty_uart_irq_tx_enable,
466 .irq_tx_disable = native_tty_uart_irq_tx_disable,
467 .irq_tx_ready = native_tty_uart_irq_tx_ready,
468 .irq_tx_complete = native_tty_uart_irq_tx_complete,
469 .irq_rx_enable = native_tty_uart_irq_rx_enable,
470 .irq_rx_disable = native_tty_uart_irq_rx_disable,
471 .irq_rx_ready = native_tty_uart_irq_rx_ready,
472 .irq_is_pending = native_tty_uart_irq_is_pending,
473 .irq_update = native_tty_uart_irq_update,
474 .irq_callback_set = native_tty_uart_irq_callback_set,
475 #endif
476 };
477
478 #define NATIVE_TTY_INSTANCE(inst) \
479 static const struct native_tty_config native_tty_##inst##_cfg = { \
480 .uart_config = \
481 { \
482 .data_bits = UART_CFG_DATA_BITS_8, \
483 .flow_ctrl = UART_CFG_FLOW_CTRL_NONE, \
484 .parity = UART_CFG_PARITY_NONE, \
485 .stop_bits = UART_CFG_STOP_BITS_1, \
486 .baudrate = DT_INST_PROP(inst, current_speed), \
487 }, \
488 }; \
489 \
490 static struct native_tty_data native_tty_##inst##_data = { \
491 .serial_port = DT_INST_PROP_OR(inst, serial_port, NULL), \
492 }; \
493 \
494 DEVICE_DT_INST_DEFINE(inst, native_tty_serial_init, NULL, &native_tty_##inst##_data, \
495 &native_tty_##inst##_cfg, NATIVE_TTY_INIT_LEVEL, \
496 CONFIG_SERIAL_INIT_PRIORITY, &native_tty_uart_driver_api);
497
498 DT_INST_FOREACH_STATUS_OKAY(NATIVE_TTY_INSTANCE);
499
500 #define INST_NAME(inst) DEVICE_DT_NAME(DT_DRV_INST(inst))
501
502 #define NATIVE_TTY_COMMAND_LINE_OPTS(inst) \
503 { \
504 .option = INST_NAME(inst) "_port", \
505 .name = "\"serial_port\"", \
506 .type = 's', \
507 .dest = &native_tty_##inst##_data.cmd_serial_port, \
508 .descript = "Set a serial port for " INST_NAME(inst) " uart device, " \
509 "overriding the one in devicetree.", \
510 }, \
511 { \
512 .option = INST_NAME(inst) "_baud", \
513 .name = "baudrate", \
514 .type = 'u', \
515 .dest = &native_tty_##inst##_data.cmd_baudrate, \
516 .descript = "Set a baudrate for " INST_NAME(inst) " device, overriding the " \
517 "baudrate of " STRINGIFY(DT_INST_PROP(inst, current_speed)) \
518 "set in the devicetree.", \
519 },
520
521 /**
522 * @brief Adds command line options for setting serial port and baud rate for each uart
523 * device.
524 */
native_tty_add_serial_options(void)525 static void native_tty_add_serial_options(void)
526 {
527 static struct args_struct_t opts[] = {
528 DT_INST_FOREACH_STATUS_OKAY(NATIVE_TTY_COMMAND_LINE_OPTS) ARG_TABLE_ENDMARKER};
529
530 native_add_command_line_opts(opts);
531 }
532
533 #define NATIVE_TTY_CLEANUP(inst) \
534 if (native_tty_##inst##_data.fd != 0) { \
535 nsi_host_close(native_tty_##inst##_data.fd); \
536 }
537
538 /**
539 * @brief Cleans up any open serial ports on the exit.
540 */
native_tty_cleanup_uart(void)541 static void native_tty_cleanup_uart(void)
542 {
543 DT_INST_FOREACH_STATUS_OKAY(NATIVE_TTY_CLEANUP);
544 }
545
546 NATIVE_TASK(native_tty_add_serial_options, PRE_BOOT_1, 11);
547 NATIVE_TASK(native_tty_cleanup_uart, ON_EXIT, 99);
548