Zephyr的Uart_console代码分析
代码路径:../zephyr/drivers/console/uart_console.c
先看初始化:初始化主要完成两项工作
1:获取绑定串口
2:重定向输出
/**
*
* @brief Install printk/stdout hook for UART console output
*
* @return N/A
*/
static void uart_console_hook_install(void)
{
__stdout_hook_install(console_out);
__printk_hook_install(console_out);
}
/**
*
* @brief Initialize one UART as the console/debug port
*
* @return 0 if successful, otherwise failed.
*/
static int uart_console_init(const struct device *arg)
{
ARG_UNUSED(arg);
/* Claim console device */
uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
uart_console_hook_install(); /*重定向输出,使其通过串口驱动输出*/
return 0;
}
/* UART console initializes after the UART device itself */
SYS_INIT(uart_console_init,
#if defined(CONFIG_USB_UART_CONSOLE)
APPLICATION,
#elif defined(CONFIG_EARLY_CONSOLE)
PRE_KERNEL_1,
#else
POST_KERNEL,
#endif
CONFIG_UART_CONSOLE_INIT_PRIORITY);
再看API:串口控制台模块提供唯一的对外API:uart_register_input(...)。也只有在调用该API后,控制台才真正的发挥作用。
void uart_register_input(struct k_fifo *avail, struct k_fifo *lines,
uint8_t (*completion)(char *str, uint8_t len))
{
avail_queue = avail;
lines_queue = lines;
completion_cb = completion;
console_input_init();
}
该函数主要完成两个功能:
1:将入参的两个struct k_fifo 类型变量与uart_console.c中的队列进行绑定。avail_queue、lines_queue是控制台管理的两个重要数据结构。
2:控制台输入的初始化,在该初始化中完成以下事情:
1:关闭串口的接收、发送中断。并重新注册串口的中断处理函数为:uart_console_isr(),这是该模块的内部中断处理函数。
2:清空uart硬件模块的fifio缓存。
3:重新是能接收中断,发送中断不再开放,发送模式改为poll mode(可追踪前面提到的重定向的接口实现)。