printk如何定向输出到UART的?
来源:互联网 发布:linux easybcd 编辑:程序博客网 时间:2024/06/08 16:47
我们通常在config里面配置这么一句“CONFIG_CMDLINE="console=tty0 console=ttyMT3,921600n1 root=/dev/ram"”,然后修改console的赋值就可以改变输出的位置,代码中如何实现的呢?
这里紧接着上文中的printk.c文件中,有这么一个函数先看一下:
/*
* Set up a list of consoles. Called from init/main.c
*/
拉风的注释直接告诉了我们这个函数被init/main.c文件中某个函数调用。
static int __init console_setup(char *str){char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */char *s, *options, *brl_options = NULL;int idx;#ifdef CONFIG_A11Y_BRAILLE_CONSOLEif (!memcmp(str, "brl,", 4)) {brl_options = "";str += 4;} else if (!memcmp(str, "brl=", 4)) {brl_options = str + 4;str = strchr(brl_options, ',');if (!str) {printk(KERN_ERR "need port name after brl=\n");return 1;}*(str++) = 0;}#endif/* * Decode str into name, index, options. */if (str[0] >= '0' && str[0] <= '9') {strcpy(buf, "ttyS");strncpy(buf + 4, str, sizeof(buf) - 5);} else {strncpy(buf, str, sizeof(buf) - 1);}buf[sizeof(buf) - 1] = 0;if ((options = strchr(str, ',')) != NULL)*(options++) = 0;#ifdef __sparc__if (!strcmp(str, "ttya"))strcpy(buf, "ttyS0");if (!strcmp(str, "ttyb"))strcpy(buf, "ttyS1");#endiffor (s = buf; *s; s++)if ((*s >= '0' && *s <= '9') || *s == ',')break;idx = simple_strtoul(s, NULL, 10);*s = 0;__add_preferred_console(buf, idx, options, brl_options);console_set_on_cmdline = 1;return 1;}
可以看到这里面有对字符串的解析,获得tty的name,index,options。加入链表是由下面的宏来做的。
__setup("console=", console_setup);
好像是不是缺少了点东东,在main.c中调用的的链表呢?在这里register_console()。
/* * The console driver calls this routine during kernel initialization * to register the console printing procedure with printk() and to * print any messages that were printed by the kernel before the * console driver was initialized. * * This can happen pretty early during the boot process (because of * early_printk) - sometimes before setup_arch() completes - be careful * of what kernel features are used - they may not be initialised yet. * * There are two types of consoles - bootconsoles (early_printk) and * "real" consoles (everything which is not a bootconsole) which are * handled differently. * - Any number of bootconsoles can be registered at any time. * - As soon as a "real" console is registered, all bootconsoles * will be unregistered automatically. * - Once a "real" console is registered, any attempt to register a * bootconsoles will be rejected */void register_console(struct console *newcon){............................/* *See if this console matches one we selected on *the command line. */ //看上面的注释我们也可以知道了command line中的定义在这里也起作用了for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];i++) {if (strcmp(console_cmdline[i].name, newcon->name) != 0)continue;if (newcon->index >= 0 && newcon->index != console_cmdline[i].index)continue;if (newcon->index < 0)newcon->index = console_cmdline[i].index;#ifdef CONFIG_A11Y_BRAILLE_CONSOLEif (console_cmdline[i].brl_options) {newcon->flags |= CON_BRL;braille_register_console(newcon,console_cmdline[i].index,console_cmdline[i].options,console_cmdline[i].brl_options);return;}#endifif (newcon->setup && newcon->setup(newcon, console_cmdline[i].options) != 0)/////在这里可以看到setup的过程了break;newcon->flags |= CON_ENABLED;newcon->index = console_cmdline[i].index;if (i == selected_console) {newcon->flags |= CON_CONSDEV;preferred_console = selected_console;}break;}if (!(newcon->flags & CON_ENABLED))//其他的console driver屏蔽掉return;........................................}
当然了,上面的函数在源码中的注释很是详细,如果需要看的,读源码是最佳的选择。
到了这里,so command line的tty端口的指定就到了这里了。
Have Fun!
- printk如何定向输出到UART的?
- Keil重定向printf到串口UART输出
- printk的定向问题
- 单片机的printf重定向到OLED/UART/SEGGER_RTT
- 如何将System.out的内容重定向输出到一个界面上的文本框中
- 如何将System.out的内容重定向输出到一个界面上的文本框中?
- python logging模块如何把不同内容定向到不同的输出
- Android中如何将原生程序向stdout和stderr的输出重定向到logcat
- 如何定向wxPython程序的输出
- 如何设定虚拟机的内核调试,以及把用户态调试器的输出重定向到内核调试输出
- C 如何将标准输出重定向到文件
- 【C++学习笔记】如何将输出定向到txt
- printf到printk的转变
- printf到printk的转变
- printf到printk的转变
- printf到printk的转变
- printf到printk的转变
- printk 输出
- Android权限设置android.permission完整列表
- 第二周2天13章多线程的锁的概念
- 使用调试钩子屏蔽全局钩子
- C#中隐藏(new)和方法重写(override)和重载(overload)的区别
- 一个简单的排序程序
- printk如何定向输出到UART的?
- MySQL配置文件my.cnf详解
- NoSQL学习笔记(二)之CAP理论
- 有关常量字符串和静态存储区
- 对话框控件的提示功能
- 运行及卸载第一个应用
- Menu菜单(二)————利用GridView实现自定义的OptionMenu
- VC-监视程序的编制
- 招投标过程---投标之述标 大杂烩