一、前言
rt_console_set_device被rt_hw_board_init调用。
#define RT_CONSOLE_DEVICE_NAME "uart1"
#ifdef RT_USING_CONSOLE
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
二、rt_console_set_device函数分析
rt_device_t rt_console_set_device(const char *name)
{
rt_device_t new, old;
/* save old device */
old = _console_device;
/* find new console device */
new = rt_device_find(name);
if (new != RT_NULL)
{
if (_console_device != RT_NULL)
{
/* close old console device */
rt_device_close(_console_device);
}
/* set new console device */
rt_device_open(new, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
_console_device = new;
}
return old;
}
函数作用:
1、查找名字为"uart1"设备
2、找到以后,打开设备
三、rt_device_find函数分析
rt_device_t rt_device_find(const char *name)
{
struct rt_object *object;
struct rt_list_node *node;
struct rt_object_information *information;
/* enter critical */
if (rt_thread_self() != RT_NULL)
rt_enter_critical();
/* try to find device object */
information = rt_object_get_information(RT_Object_Class_Device);
RT_ASSERT(information != RT_NULL);
for (node = information->object_list.next;
node != &(information->object_list);
node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
{
/* leave critical */
if (rt_thread_self() != RT_NULL)
rt_exit_critical();
return (rt_device_t)object;
}
}
/* leave critical */
if (rt_thread_self() != RT_NULL)
rt_exit_critical();
/* not found */
return RT_NULL;
}
struct rt_object_information *
rt_object_get_information(enum rt_object_class_type type)
{


208

被折叠的 条评论
为什么被折叠?



