上面这张图是U-Boot中串口设备驱动的流程,从寄存器级别的设置到最后终端信息的输出。下面我们详细讲解每一个步骤。
该函数设置了gd->bd->bi_baudrate。
static int init_baudrate (void) { char tmp[64]; /* long enough for environment variables */ int i = getenv_r ("baudrate", tmp, sizeof (tmp)); gd->bd->bi_baudrate = gd->baudrate = (i > 0) ? (int) simple_strtoul (tmp, NULL, 10) : CONFIG_BAUDRATE; //#define CONFIG_BAUDRATE 115200 定义在/include/configs/smdk2410.c中 //如果环境中没有保存,则使用宏定义的参数 return (0); }
UART控制器的初始化。
void serial_setbrg (void) { S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR); int i; unsigned int reg = 0; /* value is calculated so : (int)(PCLK/16./baudrate) -1 */ reg = get_PCLK() / (16 * gd->baudrate) - 1; /* FIFO enable, Tx/Rx FIFO clear */ uart->UFCON = 0x07; uart->UMCON = 0x0; /* Normal,No parity,1 stop,8 bit */ uart->ULCON = 0x3; /* * tx=level,rx=edge,disable timeout int.,enable rx error int., * normal,interrupt or polling */ uart->UCON = 0x245; uart->UBRDIV = reg; #ifdef CONFIG_HWFLOW uart->UMCON = 0x1; /* RTS up */ #endif for (i = 0; i < 100; i++); } /* * Initialise the serial port with the given baudrate. The settings * are always 8 data bits, no parity, 1 stop bit, no start bits. * */ int serial_init (void) { serial_setbrg ();//UART寄存器设置 return (0); }
控制台的前期初始化。
int console_init_f (void) { gd->have_console = 1; #ifdef CONFIG_SILENT_CONSOLE if (getenv("silent") != NULL) gd->flags |= GD_FLG_SILENT; #endif return (0); }
这一部分在前面的文章已经分析过了,这里就不在叙述了。
控制台后期初始化。查看环境参数stdin,stdout,stderr中对标准IO的指定的设备名称,如果没有知道再按照环境指定的名称搜索devlist,将搜到的设备指针赋给标准IO数组stdio_devices[]。
int console_init_r (void) { char *stdinname, *stdoutname, *stderrname; device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL; #ifdef CFG_CONSOLE_ENV_OVERWRITE int i; #endif /* CFG_CONSOLE_ENV_OVERWRITE */ /* set default handlers at first 设置跳转表*/ gd->jt[XF_getc] = serial_getc; gd->jt[XF_tstc] = serial_tstc; gd->jt[XF_putc] = serial_putc; gd->jt[XF_puts] = serial_puts; gd->jt[XF_printf] = serial_printf; /* stdin stdout and stderr are in environment 查看环境参数stdin,stdout,stderr中对标准IO的指定的设备名称*/ /* scan for it */ stdinname = getenv ("stdin"); stdoutname = getenv ("stdout"); stderrname = getenv ("stderr"); if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */ inputdev = search_device (DEV_FLAGS_INPUT, stdinname); outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname); errdev = search_device (DEV_FLAGS_OUTPUT, stderrname); } /* if the devices are overwritten or not found, use default device 按指定的名称搜索设备*/ if (inputdev == NULL) { inputdev = search_device (DEV_FLAGS_INPUT, "serial"); } if (outputdev == NULL) { outputdev = search_device (DEV_FLAGS_OUTPUT, "serial"); } if (errdev == NULL) { errdev = search_device (DEV_FLAGS_OUTPUT, "serial"); } /* Initializes output console first将搜到的设备指针赋给标准IO数组stdio_devices[],在下面会将为什么要这样做 */ if (outputdev != NULL) { console_setfile (stdout, outputdev); } if (errdev != NULL) { console_setfile (stderr, errdev); } if (inputdev != NULL) { console_setfile (stdin, inputdev); } gd->flags |= GD_FLG_DEVINIT; /* device initialization completed,到此串口设备才初始化完成,这个标志会影响getc等函数 */ #ifndef CFG_CONSOLE_INFO_QUIET /* Print information 打印信息*/ puts ("In: "); if (stdio_devices[stdin] == NULL) { puts ("No input devices available!\n"); } else { printf ("%s\n", stdio_devices[stdin]->name); } puts ("Out: "); if (stdio_devices[stdout] == NULL) { puts ("No output devices available!\n"); } else { printf ("%s\n", stdio_devices[stdout]->name); } puts ("Err: "); if (stdio_devices[stderr] == NULL) { puts ("No error devices available!\n"); } else { printf ("%s\n", stdio_devices[stderr]->name); } #endif /* CFG_CONSOLE_INFO_QUIET */ #ifdef CFG_CONSOLE_ENV_OVERWRITE /* set the environment variables (will overwrite previous env settings) */ for (i = 0; i < 3; i++) { setenv (stdio_names[i], stdio_devices[i]->name); } #endif /* CFG_CONSOLE_ENV_OVERWRITE */ #if 0 /* If nothing usable installed, use only the initial console */ if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL)) return (0); #endif return (0); }
在最后会打印出如下信息:
In: serial
Out: serial
Err: serial
这说明串口初始化完成。
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; }该函数是调用console_setfile设置stdo_device[]完成重定向输入输出。
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; }