@@ -29,6 +29,7 @@ struct serial_tty_context
2929
3030static struct rt_workqueue * _ttyworkq ; /* system work queue */
3131
32+ #ifndef RT_USING_DM
3233static rt_atomic_t _device_id_counter = 0 ;
3334
3435static long get_dec_digits (rt_ubase_t val )
@@ -49,17 +50,36 @@ static long get_dec_digits(rt_ubase_t val)
4950 }
5051 return result ;
5152}
53+ #endif
5254
53- static char * alloc_device_name (void )
55+ static char * alloc_device_name (struct rt_serial_device * serial )
5456{
5557 char * tty_dev_name ;
58+ #ifdef RT_USING_DM
59+ char * serial_name = serial -> parent .parent .name ;
60+ /*
61+ * if RT_USING_DM is defined, the name of the serial device
62+ * must be obtained using the serial_dev_set_name function,
63+ * and it should begin with "uart".
64+ */
65+ RT_ASSERT ((strlen (serial_name ) > strlen ("uart" )) && (strncmp (serial_name , "uart" , 4 ) == 0 ));
66+ long digits_len = (sizeof (TTY_NAME_PREFIX ) - 1 ) /* raw prefix */
67+ + strlen (serial_name + sizeof ("uart" ) - 1 ) /* suffix of serial device name*/
68+ + 1 ; /* tailing \0 */
69+
70+ tty_dev_name = rt_malloc (digits_len );
71+ if (tty_dev_name )
72+ rt_sprintf (tty_dev_name , "%s%s" , TTY_NAME_PREFIX , serial_name + sizeof ("uart" ) - 1 );
73+ #else
74+ RT_UNUSED (serial );
5675 unsigned int devid = rt_atomic_add (& _device_id_counter , 1 );
5776 long digits_len = (sizeof (TTY_NAME_PREFIX ) - 1 ) /* raw prefix */
5877 + get_dec_digits (devid ) + 1 ; /* tailing \0 */
5978
6079 tty_dev_name = rt_malloc (digits_len );
6180 if (tty_dev_name )
6281 rt_sprintf (tty_dev_name , "%s%u" , TTY_NAME_PREFIX , devid );
82+ #endif
6383 return tty_dev_name ;
6484}
6585
@@ -287,7 +307,8 @@ rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
287307 softc = rt_malloc (sizeof (struct serial_tty_context ));
288308 if (softc )
289309 {
290- dev_name = alloc_device_name ();
310+ dev_name = alloc_device_name (serial );
311+
291312 if (dev_name )
292313 {
293314 softc -> parent = serial ;
0 commit comments