此篇博客爲 SylixOS ARM BSP 編寫連載的第三篇,主要介紹 bspInit.c 文件具體實現。
bspInit.c 爲 BSP 操作系統初始化部分代碼,通常由 startup.S 初始完基本處理器參數後調用,下面以 S3C2440A 處理器爲例,逐塊介紹 bspInit.c 代碼。
SylixOS ARM BSP 第二篇中提到 startup.S 初始化完成會將會調用 bspInit() 函數,此函數用於初始化操作系統,並開始多任務調度。
int bspInit (void) { static __section(.noinit) CHAR cKernelHeap[6 * LW_CFG_MB_SIZE]; static __section(.noinit) CHAR cSystemHeap[6 * LW_CFG_MB_SIZE]; halModeInit(); debugChannelInit(0); API_KernelStartParam("ncpus=1 kdlog=no kderror=yes kfpu=no heapchk=yes"); API_KernelStart(usrStartup, cKernelHeap,sizeof(cKernelHeap), cSystemHeap,sizeof(cSystemHeap)); return (-1); }
代碼中定義的 cKernelHeap 與 cSystemHeap 爲 SylixOS 內核堆與系統堆內存位置( cKernelHeap 與 cSystemHeap 數組不需要被清零),這裏通過 static char 數字的方式開闢,__section(.noinit) 其中 __section() 是一個宏,原型在 sys/compiler.h 中定義:
#ifdef BSD /* bsd system use __attribute__ ((__section__(S))) not #S, so compiler bsd source MUST defined BSD */ # define __section(S) __attribute__ ((__section__(S))) #else # define __section(S) __attribute__ ((__section__(#S))) #endif
表示將被定義的對象放在某一個指定的段中,這裏的 __section(.noinit) 表示 cKernelHeap 與 cSystemHeap 數組放在 .noinit 段中(如果沒有指定,則默認在 .bss 段中),類似彙編中的 SECTION() 操作。
.noinit 段不需要在 startup.S 中沒有像 .bss 段一樣被清零,所以這樣的方法可以加快系統啓動時間。
halModeInit() 是目標機模式初始化函數,目前爲空函數,如有需要用戶可以加入自己的代碼,此代碼將會在操作系統初始化之前被調用。
debugChannelInit() 操作系統內部使用 bspDebugMsg() 作爲內部調試信息打印接口,debugChannelInit() 就是初始化這個打印接口,bspDebugMsg() 函數將在以後的 bspLib.c 博文中詳細說明。
API_KernelStartParam() 是系統內核函數,負責設置操作系統啓動參數,啓動參數爲一個字符串,格式爲【參數名=參數值】多個參數之間用空格隔開,SylixOS 目前支持的啓動參數有:
參數名 | 默認值 | 說明 |
ncpus | 1 | CPU 個數,例如單核處理器 ncpus=1 多核處理器 ncpus=CPU 數量,注意:ncpus 不得大於 LW_CFG_MAX_PROCESSORS |
dlog | no | 是否允許操作系統通過 bspDebugMsg() 函數打印調試信息,dlog=yes 代表允許,dlog=no 代表不允許 |
derror | yes | 是否允許操作系統通過 bspDebugMsg() 函數打印錯誤信息,參數與 dlog 相同 |
kfpu | no | 是否允許操作系統內核使用硬浮點運算器,強烈建議此參數爲 no |
heapchk | yes | 是否允許操作系統對所有內存堆操作進行內存訪問越界檢查,建議參數爲 yes |
varea | 0xC0000000 | 表示系統虛擬內存空間起始點,虛擬空間配置將在以後的 bspMap.h 博文中詳細說明。 |
vsize | 0x40000000 | |
hz | 100 | |
hhz | 100 | |
irate | 5 | |
hpsec | 1 |
(未完,待續)