summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzms123456 <[email protected]>2024-04-16 11:20:30 +0800
committerGitHub <[email protected]>2024-04-15 23:20:30 -0400
commita68b4df4d8867f76a077f610be793bfbd5c64cbc (patch)
tree629551ee37d83788bec836866f9bbcd1ed7ef4f4
parentbe699795f8a5076e671091fb904823c21adf7df4 (diff)
[component][driver][serial/tty]using serial name to name tty
当使用新device model时,serial的名字由系统分配,id也由系统分配,此时应根据系统分配的serial id来作为serial-tty的id
-rw-r--r--components/drivers/serial/serial_tty.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/components/drivers/serial/serial_tty.c b/components/drivers/serial/serial_tty.c
index c31aa8e3c6..1443cd1632 100644
--- a/components/drivers/serial/serial_tty.c
+++ b/components/drivers/serial/serial_tty.c
@@ -29,6 +29,7 @@ struct serial_tty_context
static struct rt_workqueue *_ttyworkq; /* system work queue */
+#ifndef RT_USING_DM
static rt_atomic_t _device_id_counter = 0;
static long get_dec_digits(rt_ubase_t val)
@@ -49,10 +50,28 @@ static long get_dec_digits(rt_ubase_t val)
}
return result;
}
+#endif
-static char *alloc_device_name(void)
+static char *alloc_device_name(struct rt_serial_device *serial)
{
char *tty_dev_name;
+#ifdef RT_USING_DM
+ char *serial_name = serial->parent.parent.name;
+ /*
+ * if RT_USING_DM is defined, the name of the serial device
+ * must be obtained using the serial_dev_set_name function,
+ * and it should begin with "uart".
+ */
+ RT_ASSERT((strlen(serial_name) > strlen("uart")) && (strncmp(serial_name, "uart", 4) == 0));
+ long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
+ + strlen(serial_name + sizeof("uart") - 1) /* suffix of serial device name*/
+ + 1; /* tailing \0 */
+
+ tty_dev_name = rt_malloc(digits_len);
+ if (tty_dev_name)
+ rt_sprintf(tty_dev_name, "%s%s", TTY_NAME_PREFIX, serial_name + sizeof("uart") - 1);
+#else
+ RT_UNUSED(serial);
unsigned int devid = rt_atomic_add(&_device_id_counter, 1);
long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
+ get_dec_digits(devid) + 1; /* tailing \0 */
@@ -60,6 +79,7 @@ static char *alloc_device_name(void)
tty_dev_name = rt_malloc(digits_len);
if (tty_dev_name)
rt_sprintf(tty_dev_name, "%s%u", TTY_NAME_PREFIX, devid);
+#endif
return tty_dev_name;
}
@@ -286,7 +306,8 @@ rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
softc = rt_malloc(sizeof(struct serial_tty_context));
if (softc)
{
- dev_name = alloc_device_name();
+ dev_name = alloc_device_name(serial);
+
if (dev_name)
{
softc->parent = serial;