OpenHarmony 系統(tǒng)移植最核心的步驟是內(nèi)核的移植,內(nèi)核的穩(wěn)定是一切子系統(tǒng)穩(wěn)定的基礎(chǔ),上一篇我們講述了內(nèi)核啟動(dòng)原理,以及 vendor、board 的開發(fā)配置,本文將介紹 SoC 層級的移植適配流程。
SoC 適配
SoC 配置芯片層級編譯依賴庫,包括 CMSIS、HAL(硬件抽象層)等,這里包含操作總線、串口、時(shí)鐘、寄存等庫函數(shù)。
①創(chuàng)建對應(yīng)的文件目錄結(jié)構(gòu)
目錄名稱按照芯片廠家、芯片型號來創(chuàng)建,比如 st 公司下的 stm32f4xx 系列芯片。
配置文件內(nèi)容如下:
device/soc/st/stm32f4xx/Kconfig.liteos_m.defconfig.series if?SOC_SERIES_STM32F4xx rsource?"Kconfig.liteos_m.defconfig.stm32f4xx" config?SOC_SERIES string default?"stm32f4xx" endif device/soc/st/stm32f4xx/Kconfig.liteos_m.defconfig.stm32f4xx config?SOC string default?"stm32f4xx" depends?on?SOC_STM32F4xx device/soc/st/stm32f4xx/Kconfig.liteos_m.series config?SOC_SERIES_STM32F4xx bool?"STMicroelectronics?STM32F4xx?series" select?ARCH_ARM select?SOC_COMPANY_STMICROELECTRONICS select?CPU_CORTEX_M4 help Enable?support?for?STMicroelectronics?STM32F4xx?series device/soc/st/stm32f4xx/Kconfig.liteos_m.soc choice prompt?"STMicroelectronics?STM32F4xx?series?SoC" depends?on?SOC_SERIES_STM32F4xx config?SOC_STM32F407 bool?"SoC?STM32F407" Endchoice device/soc/st/Kconfig.liteos_m.defconfig rsource?"*/Kconfig.liteos_m.defconfig.series" device/soc/st/Kconfig.liteos_m.defconfig rsource?"*/Kconfig.liteos_m.series" device/soc/st/Kconfig.liteos_m.soc config?SOC_COMPANY_STMICROELECTRONICS bool if?SOC_COMPANY_STMICROELECTRONICS config?SOC_COMPANY default?"st" rsource?"*/Kconfig.liteos_m.soc" endif?#?SOC_COMPANY_STMICROELECTRONICS device/soc/st/BUILD.gn if?(ohos_kernel_type?==?"liteos_m")?{ import("http://kernel/liteos_m/liteos.gni") module_name?=?get_path_info(rebase_path("."),?"name") module_group(module_name)?{ modules?=?[?"stm32f4xx"?] } } device/soc/st/stm32f4xx/BUILD.gn if?(ohos_kernel_type?==?"liteos_m")?{ import("http://kernel/liteos_m/liteos.gni") module_name?=?get_path_info(rebase_path("."),?"name") module_group(module_name)?{ modules?=?[?"liteos_m",?"sdk"?] } }
②移植 HAL 庫函數(shù)等文件
對于 STM32F407 我們可以使用官方的 STM32CubeMX 生成對應(yīng)的標(biāo)準(zhǔn)的 hal 庫函數(shù)文件。
勾選 Arm Cortex-M4→STM32F4→STM32F407ZGTx:
填寫工程名稱,選擇工程保存路徑,選擇 Makefile 作為編譯工具,點(diǎn)擊 GENERATE CODE 生成工程代碼。
使用 vscode 打開目錄,我們得到如下工程:
回顧一下之前講過的系統(tǒng)啟動(dòng)的流程:
HAL 初始化
系統(tǒng)時(shí)鐘初始化
系統(tǒng)初始化
系統(tǒng)啟動(dòng)
接下來我們將 HAL 庫函數(shù)文件及芯片頭文件遷移到 OH 代碼中,文件路徑如下:
將 Drivers 中的 CMSIS、STM32F4xx_HAL_Driver 復(fù)制到 /device/soc/st/stm32f4xx/sdk/Drivers 中。
③修改系統(tǒng)編譯配置文件
使用 OH 的 gn 以及 config 文件配置系統(tǒng)編譯流程以及包依賴關(guān)系,涉及到的配置文件如下:
?
device/board/alientek/explorer/liteos_m/config.gni#?Kernel? type,?e.g.?"linux",?"liteos_a",?"liteos_m".kernel_type?=?"liteos_m"#?Kernel?version.kernel_version?=?"3.0.0"#?Board?CPU? type,?e.g.?"cortex-a7",?"riscv32".board_cpu?=?"cortex-m4"#?Board?arch,?e.g.?"armv7-a",?"rv32imac".board_arch?=?""#?Toolchain?name?used? for?system?compiling.#?E.g.?gcc-arm-none-eabi,?arm-linux-harmonyeabi-gcc,?ohos-clang,?riscv32-unknown-elf.#?Note:?The?default?toolchain?is?"ohos-clang".? It's?not?mandatory?if?you?use?the?default?toolchain.board_toolchain?=?"arm-none-eabi-gcc"use_board_toolchain?=?true#?The?toolchain?path?installed,?it's? not?mandatory? if?you?have?added?toolchain? path?to?your?~/.bashrc.board_toolchain_path?=?""# ?Compiler?prefix.board_toolchain_prefix?=?"arm-none-eabi-"#?Compiler? type,?"gcc"? or?"clang".board_toolchain_type?=?"gcc" #Debug?compiler?optimization?level?optionsboard_opt_flags?=?["-mcpu=cortex-m4","-mthumb","-mfpu=fpv4-sp-d16","-mfloat-abi=hard",]#? Board?related?common?compile?flags.board_cflags?=?["-Og","-Wall","-fdata-sections","-ffunction-sections","-DSTM32F407xx","-DHAL_UART_MODULE_ENABLED"] board_cflags?+=?board_opt_flagsboard_asmflags?=?["-Og","-Wall","-fdata-sections","-ffunction-sections",] board_asmflags?+=?board_opt_flagsboard_cxx_flags?=?board_cflagsboard_ld_flags?=?["-T${ohos_root_path}device/board/alientek/explorer/liteos_m/STM32F407ZGTx_FLASH.ld"] board_ld_flags?+=?board_opt_flags#?Board?related?headfiles?search? path.board_include_dirs?=?[?"http://utils/native/lite/include"?]#?Board?adapter?dir? for?OHOS?components.board_adapter_dir?=?""這里的核心工作就是將原有的Makefile編譯文件翻譯成OH的 config.gni,可以看到有很多的編譯參數(shù)以及宏變量定義。 修改編譯依賴文件BUILD.gndevice/board/alientek/explorer/liteos_m/BUILD.gnimport("http://kernel/liteos_m/liteos.gni") module_name?=?get_path_info(rebase_path("."),?"name") kernel_module(module_name)? {sources?=?["startup_stm32f407xx.s","Src/main.c","Src/delay.c","Src/led.c","Src/sys.c","Src/usart.c","Src/stm32f4xx_hal_msp.c"," Src/stm32f4xx_it.c","Src/system_stm32f4xx.c", ]include_dirs?=?[?"Inc",]}#?"-Wl,-T"?+?rebase_path("STM32F407ZGTx_FLASH.ld"), config("public")?{ldflags?=?["-Wl,-u_printf_float"]libs?=?["c","m","nosys",]} device/soc/st/stm32f4xx/sdk/BUILD.gnimport("http://kernel/liteos_m/liteos.gni") module_name?=?"stm32f4xx_sdk"kernel_module(module_name)? {asmflags?=?board_asmflagssources?=?["Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c"," Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c","Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c"," Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c"," Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c"," Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c","Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c"," Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c","Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c",] include_dirs?=?["http://device/board/alientek/explorer/liteos_m/Inc"]}#指定全局頭文件搜索路徑 config("public")?{include_dirs?=?["Drivers/STM32F4xx_HAL_Driver/Inc","Drivers/CMSIS/Device/ST/STM32F4xx/Include",]}
④改造 main 函數(shù),拉起系統(tǒng)內(nèi)核
我們對 device/board/alientek/explorer/liteos_m/Src/main.c 文件進(jìn)行如下編輯:
int?main(void) { HAL_Init();?????????????????????????/*?初始化HAL庫?*/ sys_stm32_clock_init(336,?8,?2,?7);?/*?初始化時(shí)鐘頻率168Mhz?*/ delay_init(168);????????????????????/*?延時(shí)初始化?*/ printf("hal、系統(tǒng)始終初始化完畢,開始啟動(dòng)系統(tǒng)... "); RunTask(); } void?RunTask() { unsigned?int?ret; ret?=?LOS_KernelInit();??//?初始化LiteOS系統(tǒng) if?(ret?!=?LOS_OK) { printf("Liteos?kernel?init?failed!?ERROR:?0x%x ",?ret); } else { LOS_Start();?//?啟動(dòng)系統(tǒng) } ... }? ??編譯與燒錄
使用 hb 工具進(jìn)行編譯,hb set 選擇編譯目標(biāo),hb build -f 執(zhí)行編譯。
日志輸出 explorer build success 表示編譯成功。 編譯過程中可能會遇到缺少某些結(jié)構(gòu)體或者函數(shù)的定義,需要細(xì)心排查,注意宏定義是否打開。 STM32F407 開發(fā)板支持串口和 ST-LINK 燒錄方式,但 OH 編譯出來的是 bin 文件,bin 無法直接通過串口燒錄。
需要用到 ST-LINK 工具進(jìn)行燒錄,燒錄時(shí)需要指定 flash,開始地址:0x08000000,大小:0x100000。
開始燒錄:
點(diǎn)亮開發(fā)板:
總結(jié)
本文主要講述了 OpenHarmony 內(nèi)核的 SoC 代碼移植。因?yàn)?STM32F407 的架構(gòu)和基礎(chǔ) SDK 都是官方開源的,所以移植工作也較為容易。 但在實(shí)際工作中如果遇到未開源的芯片,那么需要模組或芯片廠商提供技術(shù)支持才可完成移植工作。 到這里瘦設(shè)備 OH 適配的最核心工作已完成,希望能對熱愛 OpenHarmony 的小伙伴有所幫助。 ?
關(guān)注我們
編輯:黃飛
?
評論