Skip to content

Commit e1eaa0e

Browse files
vytvirnashif
authored andcommitted
drivers: serial: native_tty: config_get support
This commit adds config_get support for native_tty. This is helpful as some driver code (e.g. u_blox m8) will error out if they can't read the current configuration. Signed-off-by: Vytautas Virvičius <[email protected]>
1 parent 4675f17 commit e1eaa0e

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

drivers/serial/uart_native_tty.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <zephyr/drivers/uart.h>
1919
#include <zephyr/kernel.h>
2020

21+
#include <nsi_errno.h>
2122
#include <nsi_tracing.h>
2223

2324
#include "cmdline.h"
@@ -130,6 +131,73 @@ static int native_tty_conv_to_bottom_cfg(struct native_tty_bottom_cfg *bottom_cf
130131
return 0;
131132
}
132133

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+
*/
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+
133201
/*
134202
* @brief Output a character towards the serial port
135203
*
@@ -177,6 +245,20 @@ static int native_tty_configure(const struct device *dev, const struct uart_conf
177245
return native_tty_configure_bottom(fd, &bottom_cfg);
178246
}
179247

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+
180262
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
181263
static int native_tty_uart_fifo_fill(const struct device *dev,
182264
const uint8_t *tx_data,
@@ -375,6 +457,7 @@ static DEVICE_API(uart, native_tty_uart_driver_api) = {
375457
.poll_in = native_tty_uart_poll_in,
376458
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
377459
.configure = native_tty_configure,
460+
.config_get = native_tty_config_get,
378461
#endif
379462
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
380463
.fifo_fill = native_tty_uart_fifo_fill,

drivers/serial/uart_native_tty_bottom.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <termios.h>
1616
#include <unistd.h>
1717

18+
#include <nsi_errno.h>
1819
#include <nsi_tracing.h>
1920

2021
#define WARN(...) nsi_print_warning(__VA_ARGS__)
@@ -90,6 +91,31 @@ static inline void native_tty_baud_speed_set(struct termios *ter, int baudrate)
9091
ERROR("Could not set baudrate, as %d is not supported.\n", baudrate);
9192
}
9293

94+
/**
95+
* @brief Get the baud rate speed from the termios structure
96+
*
97+
* @param ter
98+
* @param baudrate
99+
*/
100+
static inline void native_tty_baud_speed_get(const struct termios *ter, uint32_t *baudrate)
101+
{
102+
speed_t ispeed = cfgetispeed(ter);
103+
speed_t ospeed = cfgetospeed(ter);
104+
105+
if (ispeed != ospeed) {
106+
ERROR("Input and output baud rates differ: %d vs %d\n", ispeed, ospeed);
107+
}
108+
109+
for (int i = 0; i < ARRAY_SIZE(baudrate_lut); i++) {
110+
if (baudrate_lut[i].termios_baudrate == ispeed) {
111+
*baudrate = baudrate_lut[i].baudrate;
112+
return;
113+
}
114+
}
115+
116+
ERROR("Unsupported termios baudrate: %d\n", ispeed);
117+
}
118+
93119
/**
94120
* @brief Set parity setting in the termios structure
95121
*
@@ -117,6 +143,24 @@ static inline void native_tty_baud_parity_set(struct termios *ter,
117143
}
118144
}
119145

146+
/**
147+
* @brief Get the parity setting from the termios structure
148+
*
149+
* @param ter
150+
* @param parity
151+
*/
152+
static inline void native_tty_baud_parity_get(const struct termios *ter,
153+
enum native_tty_bottom_parity *parity)
154+
{
155+
if ((ter->c_cflag & PARENB) == 0) {
156+
*parity = NTB_PARITY_NONE;
157+
} else if (ter->c_cflag & PARODD) {
158+
*parity = NTB_PARITY_ODD;
159+
} else {
160+
*parity = NTB_PARITY_EVEN;
161+
}
162+
}
163+
120164
/**
121165
* @brief Set the number of stop bits in the termios structure
122166
*
@@ -140,6 +184,18 @@ static inline void native_tty_stop_bits_set(struct termios *ter,
140184
}
141185
}
142186

187+
/**
188+
* @brief Get the number of stop bits from the termios structure
189+
*
190+
* @param ter
191+
* @param stop_bits
192+
*/
193+
static inline void native_tty_stop_bits_get(const struct termios *ter,
194+
enum native_tty_bottom_stop_bits *stop_bits)
195+
{
196+
*stop_bits = (ter->c_cflag & CSTOPB) ? NTB_STOP_BITS_2 : NTB_STOP_BITS_1;
197+
}
198+
143199
/**
144200
* @brief Set the number of data bits in the termios structure
145201
*
@@ -175,6 +231,33 @@ static inline void native_tty_data_bits_set(struct termios *ter,
175231
ter->c_cflag |= data_bits_to_set;
176232
}
177233

234+
/**
235+
* @brief Get the number of data bits from the termios structure
236+
*
237+
* @param ter
238+
* @param data_bits
239+
*/
240+
static inline void native_tty_data_bits_get(const struct termios *ter,
241+
enum native_tty_bottom_data_bits *data_bits)
242+
{
243+
switch (ter->c_cflag & CSIZE) {
244+
case CS5:
245+
*data_bits = NTB_DATA_BITS_5;
246+
break;
247+
case CS6:
248+
*data_bits = NTB_DATA_BITS_6;
249+
break;
250+
case CS7:
251+
*data_bits = NTB_DATA_BITS_7;
252+
break;
253+
case CS8:
254+
*data_bits = NTB_DATA_BITS_8;
255+
break;
256+
default:
257+
ERROR("Unsupported data bits setting in termios.\n");
258+
}
259+
}
260+
178261
int native_tty_poll_bottom(int fd)
179262
{
180263
struct pollfd pfd = { .fd = fd, .events = POLLIN };
@@ -252,3 +335,26 @@ int native_tty_configure_bottom(int fd, struct native_tty_bottom_cfg *cfg)
252335

253336
return 0;
254337
}
338+
339+
int native_tty_read_bottom_cfg(int fd, struct native_tty_bottom_cfg *cfg)
340+
{
341+
struct termios ter;
342+
int rc = 0;
343+
344+
rc = tcgetattr(fd, &ter);
345+
if (rc != 0) {
346+
int err = 0;
347+
348+
err = errno;
349+
WARN("Could not read terminal driver settings: %s\n", strerror(err));
350+
return -nsi_errno_to_mid(err);
351+
}
352+
353+
native_tty_baud_speed_get(&ter, &cfg->baudrate);
354+
native_tty_baud_parity_get(&ter, &cfg->parity);
355+
native_tty_data_bits_get(&ter, &cfg->data_bits);
356+
native_tty_stop_bits_get(&ter, &cfg->stop_bits);
357+
cfg->flow_ctrl = NTB_FLOW_CTRL_NONE;
358+
359+
return 0;
360+
}

drivers/serial/uart_native_tty_bottom.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ int native_tty_open_tty_bottom(const char *pathname);
8686
*/
8787
int native_tty_configure_bottom(int fd, struct native_tty_bottom_cfg *cfg);
8888

89+
/**
90+
* @brief Read bottom tty configuration
91+
*
92+
* @param fd
93+
* @param cfg
94+
*
95+
* @return 0 on success, negative value on error
96+
*/
97+
int native_tty_read_bottom_cfg(int fd, struct native_tty_bottom_cfg *cfg);
98+
8999
#ifdef __cplusplus
90100
}
91101
#endif

0 commit comments

Comments
 (0)