Skip to content

Commit db5efa6

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 db5efa6

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-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: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ 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+
return -1;
109+
}
110+
111+
for (int i = 0; i < ARRAY_SIZE(baudrate_lut); i++) {
112+
if (baudrate_lut[i].termios_baudrate == ispeed) {
113+
*baudrate = baudrate_lut[i].baudrate;
114+
return 0;
115+
}
116+
}
117+
118+
ERROR("Unsupported termios baudrate: %d\n", ispeed);
119+
return -1;
120+
}
121+
122+
93123
/**
94124
* @brief Set parity setting in the termios structure
95125
*
@@ -117,6 +147,27 @@ static inline void native_tty_baud_parity_set(struct termios *ter,
117147
}
118148
}
119149

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

194+
/**
195+
* @brief Get the number of stop bits from the termios structure
196+
*
197+
* @param ter
198+
* @param stop_bits
199+
*
200+
* @return 0 on success
201+
*/
202+
static inline int native_tty_stop_bits_get(const struct termios *ter,
203+
enum native_tty_bottom_stop_bits *stop_bits)
204+
{
205+
*stop_bits = (ter->c_cflag & CSTOPB) ? NTB_STOP_BITS_2 : NTB_STOP_BITS_1;
206+
return 0;
207+
}
208+
209+
143210
/**
144211
* @brief Set the number of data bits in the termios structure
145212
*
@@ -175,6 +242,38 @@ static inline void native_tty_data_bits_set(struct termios *ter,
175242
ter->c_cflag |= data_bits_to_set;
176243
}
177244

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

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

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)