六、打印信息
在最后会打印出如下信息:
In: serial
Out: serial
Err: serial
这说明串口初始化完成。
七、为什么要使用devlist,std_device[]?
为了更灵活地实现标准IO重定向,任何可以作为标准IO的设备,如USB键盘,LCD屏,串口等都可以对应一个device_t的结构体变量,只需要实现getc和putc等函数,就能加入到devlist列表中去,也就可以被assign为标准IO设备stdo_device中去。如函数 int console_assign (int file, char *devname); /* Assign the console 重定向标准输入输出*/这个函数功能就是把名为devname的设备重定向为标准IO文件file(stdin,stdout,stderr)。其执行过程是在devlist中查找devname的设备,返回这个设备的device_t指针,并把指针值赋给stdo_device[file]。- int console_assign (int file, char *devname)
- {
- int flag, i;
- /* Check for valid file */
- switch (file) {
- case stdin:
- flag = DEV_FLAGS_INPUT;
- break;
- case stdout:
- case stderr:
- flag = DEV_FLAGS_OUTPUT;
- break;
- default:
- return -1;
- }
- /* Check for valid device name */
- for (i = 1; i <= ListNumItems (devlist); i++) {
- device_t *dev = ListGetPtrToItem (devlist, i);
- if (strcmp (devname, dev->name) == 0) {
- if (dev->flags & flag)
- return console_setfile (file, dev);
- return -1;
- }
- }
- return -1;
- }
- static int console_setfile (int file, device_t * dev)
- {
- int error = 0;
- if (dev == NULL)
- return -1;
- switch (file) {
- case stdin:
- case stdout:
- case stderr:
- /* Start new device */
- if (dev->start) {
- error = dev->start ();
- /* If it's not started dont use it */
- if (error < 0)
- break;
- }
- /* Assign the new device (leaving the existing one started) */
- stdio_devices[file] = dev;//这里是关键
- /*
- * Update monitor functions
- * (to use the console stuff by other applications)
- */
- switch (file) {
- case stdin:
- gd->jt[XF_getc] = dev->getc;
- gd->jt[XF_tstc] = dev->tstc;
- break;
- case stdout:
- gd->jt[XF_putc] = dev->putc;
- gd->jt[XF_puts] = dev->puts;
- gd->jt[XF_printf] = printf;
- break;
- }
- break;
- default: /* Invalid file ID */
- error = -1;
- }
- return error;
- }
