Skip to content

Commit 758ab97

Browse files
committed
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 <vytautas@virvicius.dev>
1 parent f52d71c commit 758ab97

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

drivers/serial/uart_native_tty.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,73 @@ static int native_tty_conv_to_bottom_cfg(struct native_tty_bottom_cfg *bottom_cf
130130
return 0;
131131
}
132132

133+
/**
134+
* @brief Convert from native_tty_bottom_cfg to uart_config
135+
*
136+
* @param bottom_cfg
137+
* @param cfg
138+
*
139+
* @return 0 on success, negative errno otherwise.
140+
*/
141+
int native_tty_conv_from_bottom_cfg(int fd, struct uart_config *cfg)
142+
{
143+
struct native_tty_bottom_cfg bottom_cfg;
144+
int rc = 0;
145+
146+
rc = native_tty_read_bottom_cfg(fd, &bottom_cfg);
147+
if (rc != 0) {
148+
return nsi_errno_from_mid(rc);
149+
}
150+
151+
cfg->baudrate = bottom_cfg.baudrate;
152+
153+
switch (bottom_cfg.parity) {
154+
case NTB_PARITY_NONE:
155+
cfg->parity = UART_CFG_PARITY_NONE;
156+
break;
157+
case NTB_PARITY_ODD:
158+
cfg->parity = UART_CFG_PARITY_ODD;
159+
break;
160+
case NTB_PARITY_EVEN:
161+
cfg->parity = UART_CFG_PARITY_EVEN;
162+
break;
163+
default:
164+
return -ENOTSUP;
165+
}
166+
167+
switch (bottom_cfg.stop_bits) {
168+
case NTB_STOP_BITS_1:
169+
cfg->stop_bits = UART_CFG_STOP_BITS_1;
170+
break;
171+
case NTB_STOP_BITS_2:
172+
cfg->stop_bits = UART_CFG_STOP_BITS_2;
173+
break;
174+
default:
175+
return -ENOTSUP;
176+
}
177+
178+
switch (bottom_cfg.data_bits) {
179+
case NTB_DATA_BITS_5:
180+
cfg->data_bits = UART_CFG_DATA_BITS_5;
181+
break;
182+
case NTB_DATA_BITS_6:
183+
cfg->data_bits = UART_CFG_DATA_BITS_6;
184+
break;
185+
case NTB_DATA_BITS_7:
186+
cfg->data_bits = UART_CFG_DATA_BITS_7;
187+
break;
188+
case NTB_DATA_BITS_8:
189+
cfg->data_bits = UART_CFG_DATA_BITS_8;
190+
break;
191+
default:
192+
return -ENOTSUP;
193+
}
194+
195+
cfg->flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
196+
197+
return 0;
198+
}
199+
133200
/*
134201
* @brief Output a character towards the serial port
135202
*
@@ -177,6 +244,20 @@ static int native_tty_configure(const struct device *dev, const struct uart_conf
177244
return native_tty_configure_bottom(fd, &bottom_cfg);
178245
}
179246

247+
static int native_tty_config_get(const struct device *dev, struct uart_config *cfg)
248+
{
249+
int fd = ((struct native_tty_data *)dev->data)->fd;
250+
int rc = 0;
251+
252+
rc = native_tty_conv_from_bottom_cfg(fd, cfg);
253+
if (rc) {
254+
WARN("Could not convert native tty bottom cfg to uart config\n");
255+
return rc;
256+
}
257+
258+
return 0;
259+
}
260+
180261
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
181262
static int native_tty_uart_fifo_fill(const struct device *dev,
182263
const uint8_t *tx_data,
@@ -375,6 +456,7 @@ static DEVICE_API(uart, native_tty_uart_driver_api) = {
375456
.poll_in = native_tty_uart_poll_in,
376457
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
377458
.configure = native_tty_configure,
459+
.config_get = native_tty_config_get,
378460
#endif
379461
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
380462
.fifo_fill = native_tty_uart_fifo_fill,

drivers/serial/uart_native_tty_bottom.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ static inline void native_tty_baud_speed_set(struct termios *ter, int baudrate)
9090
ERROR("Could not set baudrate, as %d is not supported.\n", baudrate);
9191
}
9292

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

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

186+
/**
187+
* @brief Get the number of stop bits from the termios structure
188+
*
189+
* @param ter
190+
* @param stop_bits
191+
*/
192+
static inline void native_tty_stop_bits_get(const struct termios *ter,
193+
enum native_tty_bottom_stop_bits *stop_bits)
194+
{
195+
*stop_bits = (ter->c_cflag & CSTOPB) ? NTB_STOP_BITS_2 : NTB_STOP_BITS_1;
196+
}
197+
143198
/**
144199
* @brief Set the number of data bits in the termios structure
145200
*
@@ -175,6 +230,35 @@ static inline void native_tty_data_bits_set(struct termios *ter,
175230
ter->c_cflag |= data_bits_to_set;
176231
}
177232

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

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

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)