openharmony 系统移植最核心的步骤是内核的移植,内核的稳定是一切子系统稳定的基础,上一篇我们讲述了内核启动原理,以及 vendor、board 的开发配置,本文将介绍 soc 层级的移植适配流程。
soc 适配
soc 配置芯片层级编译依赖库,包括 cmsis、hal(硬件抽象层)等,这里包含操作总线、串口、时钟、寄存等库函数。
①创建对应的文件目录结构
目录名称按照芯片厂家、芯片型号来创建,比如 st 公司下的 stm32f4xx 系列芯片。
配置文件内容如下:
device/soc/st/stm32f4xx/kconfig.liteos_m.defconfig.seriesif soc_series_stm32f4xxrsource kconfig.liteos_m.defconfig.stm32f4xxconfig soc_seriesstringdefault stm32f4xxendifdevice/soc/st/stm32f4xx/kconfig.liteos_m.defconfig.stm32f4xxconfig socstringdefault stm32f4xxdepends on soc_stm32f4xxdevice/soc/st/stm32f4xx/kconfig.liteos_m.seriesconfig soc_series_stm32f4xxbool stmicroelectronics stm32f4xx seriesselect arch_armselect soc_company_stmicroelectronicsselect cpu_cortex_m4helpenable support for stmicroelectronics stm32f4xx seriesdevice/soc/st/stm32f4xx/kconfig.liteos_m.socchoiceprompt stmicroelectronics stm32f4xx series socdepends on soc_series_stm32f4xxconfig soc_stm32f407bool soc stm32f407endchoicedevice/soc/st/kconfig.liteos_m.defconfigrsource */kconfig.liteos_m.defconfig.seriesdevice/soc/st/kconfig.liteos_m.defconfigrsource */kconfig.liteos_m.seriesdevice/soc/st/kconfig.liteos_m.socconfig soc_company_stmicroelectronicsboolif soc_company_stmicroelectronicsconfig soc_companydefault strsource */kconfig.liteos_m.socendif # soc_company_stmicroelectronicsdevice/soc/st/build.gnif (ohos_kernel_type == liteos_m) {import(//kernel/liteos_m/liteos.gni)module_name = get_path_info(rebase_path(.), name)module_group(module_name) {modules = [ stm32f4xx ]}}device/soc/st/stm32f4xx/build.gnif (ohos_kernel_type == liteos_m) {import(//kernel/liteos_m/liteos.gni)module_name = get_path_info(rebase_path(.), name)module_group(module_name) {modules = [ liteos_m, sdk ]}} ②移植 hal 库函数等文件
对于 stm32f407 我们可以使用官方的 stm32cubemx 生成对应的标准的 hal 库函数文件。
选择 access to mcu selector:
勾选 arm cortex-m4→stm32f4→stm32f407zgtx:
填写工程名称,选择工程保存路径,选择 makefile 作为编译工具,点击 generate code 生成工程代码。
使用 vscode 打开目录,我们得到如下工程:
回顾一下之前讲过的系统启动的流程:
hal 初始化
系统时钟初始化
系统初始化
系统启动
接下来我们将 hal 库函数文件及芯片头文件迁移到 oh 代码中,文件路径如下:
将 drivers 中的 cmsis、stm32f4xx_hal_driver 复制到 /device/soc/st/stm32f4xx/sdk/drivers 中。
③修改系统编译配置文件
使用 oh 的 gn 以及 config 文件配置系统编译流程以及包依赖关系,涉及到的配置文件如下:
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-gccuse_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 = [ //utils/native/lite/include ]# board adapter dir for ohos components.board_adapter_dir = 这里的核心工作就是将原有的makefile编译文件翻译成oh的config.gni,可以看到有很多的编译参数以及宏变量定义。修改编译依赖文件build.gndevice/board/alientek/explorer/liteos_m/build.gnimport(//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(//kernel/liteos_m/liteos.gni)module_name = stm32f4xx_sdkkernel_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 = [//device/board/alientek/explorer/liteos_m/inc]}#指定全局头文件搜索路径config(public) {include_dirs = [drivers/stm32f4xx_hal_driver/inc,drivers/cmsis/device/st/stm32f4xx/include,]} ④改造 main 函数,拉起系统内核
int main(void){hal_init(); /* 初始化hal库 */sys_stm32_clock_init(336, 8, 2, 7); /* 初始化时钟频率168mhz */delay_init(168); /* 延时初始化 */printf(hal、系统始终初始化完毕,开始启动系统...);runtask();}void runtask(){unsigned int ret;ret = los_kernelinit(); // 初始化liteos系统if (ret != los_ok){printf(liteos kernel init failed! error: 0x%x, ret);}else{los_start(); // 启动系统}...} 编译与烧录 使用 hb 工具进行编译,hb set 选择编译目标,hb build -f 执行编译。
日志输出 explorer build success 表示编译成功。 编译过程中可能会遇到缺少某些结构体或者函数的定义,需要细心排查,注意宏定义是否打开。 stm32f407 开发板支持串口和 st-link 烧录方式,但 oh 编译出来的是 bin 文件,bin 无法直接通过串口烧录。
需要用到 st-link 工具进行烧录,烧录时需要指定 flash,开始地址:0x08000000,大小:0x100000。
开始烧录:
点亮开发板:
总结
本文主要讲述了 openharmony 内核的 soc 代码移植。因为 stm32f407 的架构和基础 sdk 都是官方开源的,所以移植工作也较为容易。 但在实际工作中如果遇到未开源的芯片,那么需要模组或芯片厂商提供技术支持才可完成移植工作。 到这里瘦设备 oh 适配的最核心工作已完成,希望能对热爱 openharmony 的小伙伴有所帮助。
关注我们
行业方案|半导体行业SRM供应商管理解决方案
虚拟技术在医疗健康领域中有着哪些前景
凯翔MegaBric分布式存储商用实践
什么是ETF,对比特币价格有何影响
PoE应用正在不断开发 满足发展的需求
浅析OpenHarmony内核SoC层级的移植适配流程
苹果放出iOS 10.3最新测试版:16G用户福利!至少省2GB存储空间!
意法半导体升级NanoEdge™ AI Studio,简化物联网产品和工业设备的机器学习软件开发
新趋势:iOS10等新闻App让媒体们尝到甜头,全都上头版头条!
华进二期以及先进封装材料验证实验室情况
首款第二代5G手机带节奏“核弹级爆品”点燃5G
荣耀9和小米6区别对比评测:小米6和华为荣耀9到底该怎么选?从性能、配置、系统、优缺点分析
插座的零线和灯的零线可以共用吗
电磁特性的测量方法——网络参数法
华为台式机、显示器曝光,有望搭载AMD处理器
东京工业大学开发AI预测系统 可预测0.5秒后的动作
指针式万用表故障常用维修方法
profinet转rs485连接三项多功能电力仪表配置案例
运动蓝牙耳机什么牌子好,运动无线蓝牙耳机排名
印度斯特里特电力斩获巴西6个新输电项目,价值达10亿美元