Skip to content

Commit 717c119

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 717c119

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

drivers/serial/uart_native_tty.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,74 @@ 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+
native_tty_read_bottom_cfg(fd, &bottom_cfg);
147+
if (rc != 0) {
148+
return 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+
200+
133201
/*
134202
* @brief Output a character towards the serial port
135203
*
@@ -177,6 +245,21 @@ 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+
262+
180263
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
181264
static int native_tty_uart_fifo_fill(const struct device *dev,
182265
const uint8_t *tx_data,
@@ -375,6 +458,7 @@ static DEVICE_API(uart, native_tty_uart_driver_api) = {
375458
.poll_in = native_tty_uart_poll_in,
376459
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
377460
.configure = native_tty_configure,
461+
.config_get = native_tty_config_get,
378462
#endif
379463
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
380464
.fifo_fill = native_tty_uart_fifo_fill,

drivers/serial/uart_native_tty_bottom.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,34 @@ 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+
* @return 0 on success, -1 on error
100+
*/
101+
static inline int native_tty_baud_speed_get(const struct termios *ter, uint32_t *baudrate)
102+
{
103+
speed_t ispeed = cfgetispeed(ter);
104+
speed_t ospeed = cfgetospeed(ter);
105+
106+
if (ispeed != ospeed) {
107+
ERROR("Input and output baud rates differ: %d vs %d\n", ispeed, ospeed);
108+
}
109+
110+
for (int i = 0; i < ARRAY_SIZE(baudrate_lut); i++) {
111+
if (baudrate_lut[i].termios_baudrate == ispeed) {
112+
*baudrate = baudrate_lut[i].baudrate;
113+
return 0;
114+
}
115+
}
116+
117+
ERROR("Unsupported termios baudrate: %d\n", ispeed);
118+
}
119+
120+
93121
/**
94122
* @brief Set parity setting in the termios structure
95123
*
@@ -117,6 +145,27 @@ static inline void native_tty_baud_parity_set(struct termios *ter,
117145
}
118146
}
119147

148+
/**
149+
* @brief Get the parity setting from the termios structure
150+
*
151+
* @param ter
152+
* @param parity
153+
*
154+
* @return 0 on success
155+
*/
156+
static inline int native_tty_baud_parity_get(const struct termios *ter,
157+
enum native_tty_bottom_parity *parity)
158+
{
159+
if ((ter->c_cflag & PARENB) == 0) {
160+
*parity = NTB_PARITY_NONE;
161+
} else if (ter->c_cflag & PARODD) {
162+
*parity = NTB_PARITY_ODD;
163+
} else {
164+
*parity = NTB_PARITY_EVEN;
165+
}
166+
return 0;
167+
}
168+
120169
/**
121170
* @brief Set the number of stop bits in the termios structure
122171
*
@@ -140,6 +189,22 @@ static inline void native_tty_stop_bits_set(struct termios *ter,
140189
}
141190
}
142191

192+
/**
193+
* @brief Get the number of stop bits from the termios structure
194+
*
195+
* @param ter
196+
* @param stop_bits
197+
*
198+
* @return 0 on success
199+
*/
200+
static inline int native_tty_stop_bits_get(const struct termios *ter,
201+
enum native_tty_bottom_stop_bits *stop_bits)
202+
{
203+
*stop_bits = (ter->c_cflag & CSTOPB) ? NTB_STOP_BITS_2 : NTB_STOP_BITS_1;
204+
return 0;
205+
}
206+
207+
143208
/**
144209
* @brief Set the number of data bits in the termios structure
145210
*
@@ -175,6 +240,37 @@ static inline void native_tty_data_bits_set(struct termios *ter,
175240
ter->c_cflag |= data_bits_to_set;
176241
}
177242

243+
/**
244+
* @brief Get the number of data bits from the termios structure
245+
*
246+
* @param ter
247+
* @param data_bits
248+
*
249+
* @return 0 on success, -1 if unsupported
250+
*/
251+
static inline int native_tty_data_bits_get(const struct termios *ter,
252+
enum native_tty_bottom_data_bits *data_bits)
253+
{
254+
switch (ter->c_cflag & CSIZE) {
255+
case CS5:
256+
*data_bits = NTB_DATA_BITS_5;
257+
break;
258+
case CS6:
259+
*data_bits = NTB_DATA_BITS_6;
260+
break;
261+
case CS7:
262+
*data_bits = NTB_DATA_BITS_7;
263+
break;
264+
case CS8:
265+
*data_bits = NTB_DATA_BITS_8;
266+
break;
267+
default:
268+
ERROR("Unsupported data bits setting in termios.\n");
269+
}
270+
return 0;
271+
}
272+
273+
178274
int native_tty_poll_bottom(int fd)
179275
{
180276
struct pollfd pfd = { .fd = fd, .events = POLLIN };
@@ -252,3 +348,42 @@ int native_tty_configure_bottom(int fd, struct native_tty_bottom_cfg *cfg)
252348

253349
return 0;
254350
}
351+
352+
int native_tty_read_bottom_cfg(int fd, struct native_tty_bottom_cfg *cfg)
353+
{
354+
struct termios ter;
355+
int rc = 0;
356+
357+
rc = tcgetattr(fd, &ter);
358+
if (rc != 0) {
359+
int err = 0;
360+
361+
err = errno;
362+
WARN("Could not read terminal driver settings: %s\n", strerror(err));
363+
return -err;
364+
}
365+
366+
rc = native_tty_baud_speed_get(&ter, &cfg->baudrate);
367+
if (rc != 0) {
368+
return rc;
369+
}
370+
371+
rc = native_tty_baud_parity_get(&ter, &cfg->parity);
372+
if (rc != 0) {
373+
return rc;
374+
}
375+
376+
rc = native_tty_data_bits_get(&ter, &cfg->data_bits);
377+
if (rc != 0) {
378+
return rc;
379+
}
380+
381+
rc = native_tty_stop_bits_get(&ter, &cfg->stop_bits);
382+
if (rc != 0) {
383+
return rc;
384+
}
385+
386+
cfg->flow_ctrl = NTB_FLOW_CTRL_NONE;
387+
388+
return 0;
389+
}

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)