[components/driver] update serial (#8567)

This commit is contained in:
fangjianzhou 2024-03-01 10:29:21 +08:00 committed by GitHub
parent 7eccdb471e
commit 00c6800e4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 229 additions and 9 deletions

View File

@ -12,6 +12,7 @@
#include <rtdevice.h> #include <rtdevice.h>
#include <drivers/platform.h> #include <drivers/platform.h>
#include <drivers/core/bus.h> #include <drivers/core/bus.h>
#include "../serial/serial_dm.h"
#define DBG_TAG "rtdm.ofw" #define DBG_TAG "rtdm.ofw"
#define DBG_LVL DBG_INFO #define DBG_LVL DBG_INFO
@ -61,7 +62,7 @@ struct rt_ofw_stub *rt_ofw_stub_probe_range(struct rt_ofw_node *np,
static const char *ofw_console_serial_find(char *dst_con, struct rt_ofw_node *np) static const char *ofw_console_serial_find(char *dst_con, struct rt_ofw_node *np)
{ {
rt_object_t rt_obj; rt_object_t rt_obj = RT_NULL;
const char *ofw_name = RT_NULL; const char *ofw_name = RT_NULL;
struct rt_serial_device *rt_serial = rt_ofw_data(np); struct rt_serial_device *rt_serial = rt_ofw_data(np);
@ -168,7 +169,7 @@ static const char *ofw_console_tty_find(char *dst_con, const char *con)
rt_err_t rt_ofw_console_setup(void) rt_err_t rt_ofw_console_setup(void)
{ {
rt_err_t err = -RT_ENOSYS; rt_err_t err = -RT_ENOSYS;
char con_name[RT_NAME_MAX]; char con_name[RT_NAME_MAX], *options = RT_NULL;
const char *ofw_name = RT_NULL, *stdout_path, *con; const char *ofw_name = RT_NULL, *stdout_path, *con;
/* chosen.console > chosen.stdout-path > RT_CONSOLE_DEVICE_NAME */ /* chosen.console > chosen.stdout-path > RT_CONSOLE_DEVICE_NAME */
@ -190,6 +191,18 @@ rt_err_t rt_ofw_console_setup(void)
if (ofw_name) if (ofw_name)
{ {
const char *ch = con;
while (*ch && *ch != ' ')
{
if (*ch++ == ',')
{
options = (char *)ch;
break;
}
}
err = RT_EOK; err = RT_EOK;
break; break;
} }
@ -230,6 +243,18 @@ rt_err_t rt_ofw_console_setup(void)
rt_console_set_device(con); rt_console_set_device(con);
if (options)
{
rt_device_t con_dev = rt_console_get_device();
if (con_dev)
{
struct serial_configure con_conf = serial_cfg_from_args(options);
rt_device_control(con_dev, RT_DEVICE_CTRL_CONFIG, &con_conf);
}
}
rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_COMPLETED); rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_COMPLETED);
LOG_I("Console: %s (%s)", con, ofw_name ? ofw_name : "<unknown>"); LOG_I("Console: %s (%s)", con, ofw_name ? ofw_name : "<unknown>");

View File

@ -3,12 +3,19 @@ from building import *
cwd = GetCurrentDir() cwd = GetCurrentDir()
CPPPATH = [cwd + '/../include'] CPPPATH = [cwd + '/../include']
group = [] group = []
if GetDepend(['RT_USING_SERIAL']): src = []
if GetDepend(['RT_USING_SERIAL_V2']):
src = Glob('serial_v2.c') if not GetDepend(['RT_USING_SERIAL']):
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SERIAL_V2'], CPPPATH = CPPPATH) Return('group')
else:
src = Glob('serial.c') if GetDepend(['RT_USING_SERIAL_V2']):
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SERIAL'], CPPPATH = CPPPATH) src += ['serial_v2.c']
else:
src += ['serial.c']
if GetDepend(['RT_USING_DM']):
src += ['serial_dm.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group') Return('group')

View File

@ -0,0 +1,154 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-16 GuEe-GUI first version
*/
#include <rtatomic.h>
#include "serial_dm.h"
int serial_dev_set_name(struct rt_serial_device *sdev)
{
int id = -1;
static int uid_min = -1;
static volatile rt_atomic_t uid = 0;
RT_ASSERT(sdev != RT_NULL);
#ifdef RT_USING_OFW
if (sdev->parent.ofw_node)
{
id = rt_ofw_get_alias_id(sdev->parent.ofw_node, "serial");
if (id < 0)
{
id = rt_ofw_get_alias_id(sdev->parent.ofw_node, "uart");
}
if (uid_min < 0)
{
uid_min = rt_ofw_get_alias_last_id("serial");
if (uid_min < 0)
{
uid_min = rt_ofw_get_alias_last_id("uart");
}
uid_min = uid_min < 0 ? 0 : (uid_min + 1);
rt_hw_atomic_store(&uid, uid_min);
}
}
#endif
if (id < 0)
{
id = (int)rt_hw_atomic_add(&uid, 1);
}
return rt_dm_dev_set_name(&sdev->parent, "uart%u", id);
}
void *serial_base_from_args(char *str)
{
rt_ubase_t base = 0;
while (*str && !(*str == 'x' || *str == 'X'))
{
++str;
}
++str;
/* The str may get from bootargs that we need check it */
while (*str)
{
if ((*str >= 'a' && *str <= 'f') || (*str >= 'A' && *str <= 'F'))
{
base = (base << 4) | (((*str | ' ') - 'a') + 10);
}
else if (*str >= '0' && *str <= '9')
{
base = (base << 4) | (*str - '0');
}
else break;
++str;
}
return (void *)base;
}
struct serial_configure serial_cfg_from_args(char *str)
{
struct serial_configure cfg = RT_SERIAL_CONFIG_DEFAULT;
/* Format baudrate/parity/bits/flow (BBBBPNF), Default is 115200n8 */
if (str && *str)
{
rt_uint32_t baudrate = 0;
/* BBBB is the speed */
while (*str && (*str >= '0' && *str <= '9'))
{
baudrate *= 10;
baudrate += *str - '0';
++str;
}
if (baudrate)
{
cfg.baud_rate = baudrate;
}
/* P is parity (n/o/e) */
switch (*str)
{
case 'n':
cfg.parity = PARITY_NONE;
break;
case 'o':
cfg.parity = PARITY_ODD;
break;
case 'e':
cfg.parity = PARITY_EVEN;
break;
default:
--str;
break;
}
++str;
/* N is number of bits */
if (*str && (*str >= '0' && *str <= '9'))
{
cfg.data_bits = *str - '0';
++str;
}
/* F is flow ontrol ('r' for RTS) */
if (*str)
{
cfg.flowcontrol = (*str == 'r' ? RT_SERIAL_FLOWCONTROL_CTSRTS : RT_SERIAL_FLOWCONTROL_NONE);
++str;
}
#ifdef RT_USING_OFW
if (*str == '\0')
{
const char earlycon_magic[] = { 'O', 'F', 'W', '\0' };
if (!rt_strcmp(++str, earlycon_magic))
{
/* Is OFW earlycon, we should ACK it */
rt_memset(str, 0, RT_ARRAY_SIZE(earlycon_magic));
}
}
#endif
}
return cfg;
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-16 GuEe-GUI first version
*/
#ifndef __SERIAL_DM_H__
#define __SERIAL_DM_H__
#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
int serial_dev_set_name(struct rt_serial_device *sdev);
void *serial_base_from_args(char *str);
struct serial_configure serial_cfg_from_args(char *str);
#define serial_for_each_args(arg, args) \
for (char *context = (arg = (typeof(arg))args, (void *)RT_NULL), \
*context_end = rt_strchrnul((char *)args, ' '); \
(arg = strtok_r(arg, ",", &context)) && arg < context_end; \
arg = RT_NULL)
#endif /* __SERIAL_DM_H__ */

View File

@ -519,3 +519,8 @@ rt_weak void rt_hw_secondary_cpu_idle_exec(void)
rt_hw_wfe(); rt_hw_wfe();
} }
#endif #endif
void rt_hw_console_output(const char *str)
{
rt_fdt_earlycon_output(str);
}