跟着韦东山老师的视频走了好几遍uboot发现还是不能深刻领悟其中奥妙,就自己把细节的代码看起来,发现其中有不少地方C用的很灵活又恰到好处。摘录下来供自己和大家学习。
在Board.c文件中有这么一个用法
typedef int (init_fnc_t) (void); //定义一个函数类型
/************************在然后用这个函数类型定义了一个指针数组
在指针数组中存储的是函数地址
****************************/
init_fnc_t *init_sequence[] = {
cpu_init, /* basic cpu dependent setup */
board_init, /* basic board dependent setup */
interrupt_init,/* set up exceptions */
env_init, /* initialize environment */
init_baudrate,/* initialze baudrate settings */
serial_init, /* serial communications setup */
console_init_f,/* stage 1 init of console */
display_banner,/* say that we are here */
#if defined(CONFIG_DISPLAY_CPUINFO)
print_cpuinfo,/* display cpu info (and speed) */
#endif
#if defined(CONFIG_DISPLAY_BOARDINFO)
checkboard, /* display board info */
#endif
dram_init, /* configure available RAM banks */
display_dram_config,
NULL,
};
其中cpu_init,定义如下
int cpu_init (void)
{
.............................
return 0;
}
其它的都比较类似了,这里定义不是用的指针函数
把所有的都定义完了该使用起来了。在void start_armboot(void)函数中进行了调用
init_fnc_t **init_fnc_ptr; //首先定义了一个指向指针函数的指针
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) //然后用这个函数调用了数组中所有的函数,方便自己添加函数
{
if ((*init_fnc_ptr)() != 0)
{
hang ();
}
}
这个方法比较神奇 主函数不懂而只需变更数据中函数即可增加调用的函数。
另外typedef用法想脑补同学移步这里,应该说的上高级用法了
http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html