当前位置:首页 >休闲 >OpenHarmony瘦设备内核移植实战(三) 所以移植工作也较为容易

OpenHarmony瘦设备内核移植实战(三) 所以移植工作也较为容易

2024-06-29 07:19:00 [百科] 来源:避面尹邢网

OpenHarmony瘦设备内核移植实战(三)

作者:软通动力HOS 系统 OpenHarmony 本文主要讲述OpenHarmony内核的瘦设备实战SoC代码移植。因为STM32F407的内核架构和基础SDK都是官方开源的,所以移植工作也较为容易,移植但在实际工作中如果遇到未开源的瘦设备实战芯片,那么需要模组或芯片厂商提供技术支持才可完成移植工作。内核

想了解更多关于开源的移植内容,请访问:

OpenHarmony瘦设备内核移植实战(三) 所以移植工作也较为容易

51CTO 开源基础软件社区

OpenHarmony瘦设备内核移植实战(三) 所以移植工作也较为容易

https://ost.51cto.com

OpenHarmony瘦设备内核移植实战(三) 所以移植工作也较为容易

一、瘦设备实战背景

OpenHarmony系统移植最核心的内核步骤是内核的移植,内核的移植稳定是一切子系统稳定的基础,上一篇我们讲述了内核启动原理,瘦设备实战以及vendor、内核board的移植开发配置,本文将介绍SoC层级的瘦设备实战移植适配流程。

二、内核SoC适配

SoC配置芯片层级编译依赖库,移植包括CMSIS、HAL(硬件抽象层)等,这里包含操作总线、串口、时钟、寄存等库函数。

1、创建对应的文件目录结构

目录名称按照芯片厂家、芯片型号来创建,比如st公司下的stm32f4xx系列芯片。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

配置文件内容如下:

device/soc/st/stm32f4xx/Kconfig.liteos_m.defconfig.seriesif SOC_SERIES_STM32F4xxrsource "Kconfig.liteos_m.defconfig.stm32f4xx"config SOC_SERIESstringdefault "stm32f4xx"endifdevice/soc/st/stm32f4xx/Kconfig.liteos_m.defconfig.stm32f4xxconfig SOCstringdefault "stm32f4xx"depends on SOC_STM32F4xxdevice/soc/st/stm32f4xx/Kconfig.liteos_m.seriesconfig SOC_SERIES_STM32F4xxbool "STMicroelectronics STM32F4xx series"select ARCH_ARMselect SOC_COMPANY_STMICROELECTRONICSselect CPU_CORTEX_M4helpEnable support for STMicroelectronics STM32F4xx seriesdevice/soc/st/stm32f4xx/Kconfig.liteos_m.socchoiceprompt "STMicroelectronics STM32F4xx series SoC"depends on SOC_SERIES_STM32F4xxconfig SOC_STM32F407bool "SoC STM32F407"Endchoicedevice/soc/st/Kconfig.liteos_m.defconfigrsource "*/Kconfig.liteos_m.defconfig.series"device/soc/st/Kconfig.liteos_m.defconfigrsource "*/Kconfig.liteos_m.series"device/soc/st/Kconfig.liteos_m.socconfig SOC_COMPANY_STMICROELECTRONICSboolif SOC_COMPANY_STMICROELECTRONICSconfig SOC_COMPANYdefault "st"rsource "*/Kconfig.liteos_m.soc"endif # 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" ]}}

2、移植HAL库函数等文件

对于STM32F407我们可以使用官方的STM32CubeMX生成对应的标准的hal库函数文件。

(1)选择ACCESS TO MCU SELECTOR。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

(2)勾选Arm Cortex-M4 --> STM32F4 --> STM32F407ZGTx。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

(3)填写工程名称,选择工程保存路径,选择Makefile作为编译工具,点击GENERATE CODE生成工程代码。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

(4)使用vscode打开目录,我们得到如下工程。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

回顾一下之前讲过的系统启动的流程:

  • HAL初始化
  • 系统时钟初始化
  • 系统初始化
  • 系统启动

接下来我们将HAL库函数文件及芯片头文件迁移到OH代码中,文件路径如下:

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

将Drivers中的CMSIS、STM32F4xx_HAL_Driver复制到/device/soc/st/stm32f4xx/sdk/Drivers中。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

3、修改系统编译配置文件

使用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-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 = [ "//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_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 = ["//device/board/alientek/explorer/liteos_m/Inc"]}#指定全局头文件搜索路径config("public") { include_dirs = ["Drivers/STM32F4xx_HAL_Driver/Inc","Drivers/CMSIS/Device/ST/STM32F4xx/Include",]}

4、改造main函数,拉起系统内核

我们对device/board/alientek/explorer/liteos_m/Src/main.c文件进行如下编辑。

int main(void){ HAL_Init();                         /* 初始化HAL库 */sys_stm32_clock_init(336, 8, 2, 7); /* 初始化时钟频率168Mhz */delay_init(168);                    /* 延时初始化 */printf("hal、系统始终初始化完毕,开始启动系统...\n");RunTask();}void RunTask(){ unsigned int ret;ret = LOS_KernelInit();  // 初始化LiteOS系统if (ret != LOS_OK){ printf("Liteos kernel init failed! ERROR: 0x%x\n", ret);}else{ LOS_Start(); // 启动系统}...}

三、编译与烧录

使用hb工具进行编译,hb set选择编译目标,hb build -f执行编译。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

日志输出explorer build success表示编译成功。

编译过程中可能会遇到缺少某些结构体或者函数的定义,需要细心排查,注意宏定义是否打开。

STM32F407开发板支持串口和ST-LINK烧录方式,但OH编译出来的是bin文件,bin无法直接通过串口烧录。需要用到ST-LINK工具进行烧录,烧录时需要指定flash,开始地址:0x08000000,大小:0x100000。

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

开始烧录

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

点亮开发板

OpenHarmony瘦设备内核移植实战(三)-开源基础软件社区

四、总结

本文主要讲述了OpenHarmony内核的SoC代码移植。因为STM32F407的架构和基础SDK都是官方开源的,所以移植工作也较为容易,但在实际工作中如果遇到未开源的芯片,那么需要模组或芯片厂商提供技术支持才可完成移植工作。到这里瘦设备OH适配的最核心工作已完成,希望能对热爱OpenHarmony的小伙伴有所帮助。

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

责任编辑:jianghua 来源: 51CTO 开源基础软件社区 鸿蒙内核移植

(责任编辑:休闲)

    推荐文章
    热点阅读