1# 移植内核 2 3 4## 移植芯片架构 5 6芯片架构的移植是内核移植的基础,在OpenHarmony中芯片架构移植是可选过程,如果当前OpenHarmony已经支持对应芯片架构则不需要移植操作,在“liteos_m/arch”目录下可看到当前已经支持的架构,如表1: 7 8 **表1** OpenHarmony已支持的架构 9 10| **系列** | **型号** | 11| -------- | -------- | 12| arm | arm9<br/>cortex-m3<br/>cortex-m4<br/>cortex-m7<br/>cortex-m33 | 13| csky | v2 | 14| risc-v | nuclei<br/>riscv32 | 15| xtensa | lx6 | 16 17 18如果当前OpenHarmony尚未支持对应芯片架构,则需要芯片厂商自行适配,arch/include目录包含了通用的芯片架构适配所需要实现的函数。部分芯片架构代码由汇编实现,而汇编代码会因编译器的不同而不同,因此在具体的芯片架构下,还包含使用不同编译器(iar、keil、gcc等)编译的架构代码。 19 20 21 22``` 23kernel/liteos_m/arch # 不同版本路径有差异。 24├── arm # arm系列。 25│ ├── arm9 26│ ├── cortex-m3 27│ ├── cortex-m33 28│ │ ├── gcc # 使用gcc编译器编译的架构代码。 29│ │ └── iar # 使用iar编译器编译的架构代码。 30│ ├── cortex-m4 31│ ├── cortex-m7 32├── csky # csky系列。 33├── include # 包含通用的芯片架构所需要实现的函数。 34│ ├── los_arch.h # 定义芯片架构初始化所需要的函数。 35│ ├── los_atomic.h # 定义芯片架构所需要实现的原子操作函数。 36│ ├── los_context.h # 定义芯片架构所需要实现的任务上下文相关函数。 37│ ├── los_interrupt.h # 定义芯片架构所需要实现的中断和异常相关的函数。 38│ └── los_timer.h # 定义芯片架构所需要实现的系统时钟相关的函数。 39├── risc-v # risc-v系列。 40│ ├── nuclei 41│ └── riscv32 42└── xtensa # xtensa系列。 43 └── lx6 44``` 45 46 47## 移植芯片厂商SDK 48 49编译框架搭建完成后,需要将芯片厂商的SDK加入OpenHarmony编译框架,从而可以编译出带SDK的烧录文件(此时编译出的是不带系统的裸机工程),以便OpenHarmony可以调用SDK中的接口。通过以下步骤将厂商SDK加入OpenHarmony编译框架中: 50 511. 将芯片厂商sdk置于device目录下合适的位置,SDK的编译脚本/镜像打包脚本整合进编译框架中。 52 53 参考编译脚本:“device/MyDeviceCompany/MyBoard/BUILD.gn” 54 55 56 ``` 57 import("//build/lite/config/component/lite_component.gni") 58 59 executable("OHOS_Image.elf") { # 生成可执行程序。 60 libs = [ 61 "xxx/xxx/libxxx.a", # 链接厂商闭源静态库方法一。 62 ] 63 asmflags = [ # 汇编编译参数。 64 "", 65 ] 66 ldflags = [ 67 "-T./xxx/xxx/xxx.ld", # 链接脚本文件。 68 "-Lxxx/xxx/", # 指定厂商静态库路径。 69 "-lxxx", # 链接厂商闭源静态库方法二。 70 "-Wl,--whole-archive", 71 "-lmodule_xxx", 72 "-Wl,--no-whole-archive", 73 ] 74 deps = [ 75 "//build/lite:ohos", # 依赖OpenHarmony静态库编译完成,链接OpenHarmony编译出来的静态库。 76 ":sdk", # 依赖厂商源码静态库编译完成,链接厂商源码生成的静态库。 77 ] 78 } 79 80 copy("prebuilt") { # 准备镜像生成工具等,一般把镜像生成工具拷贝到out目录。 81 sources = [ ] # 复制的源文件。 82 outputs = [ ] # 复制的目标文件。 83 } 84 static_library("sdk") { 85 sources = [ ] # 添加厂商源码编译成静态库。 86 include_dirs = [ ] # 厂商源码包含头文件路径。 87 } 88 build_ext_component("image") { # 调用shell命令,生成可烧写镜像文件 。 89 exec_path = rebase_path(root_out_dir) #指定shell命令执行目录。 90 objcopy = "arm-none-eabi-objcopy" 91 objdump = "arm-none-eabi-objdump" 92 command = "$objcopy -O binary OHOS_Image.elf OHOS_Image.bin" 93 command += " && sh -c '$objdump -t OHOS_Image.elf | sort > OHOS_Image.sym.sorted'" 94 command += " && sh -c '$objdump -d OHOS_Image.elf > OHOS_Image.asm'" 95 deps = [ 96 ":prebuilt", # 无需准备镜像生成工具等可以删除此依赖。 97 ":OHOS_Image.elf", # 依赖elf文件的生成。 98 ] 99 } 100 group("MyBoard") { # MyBoard与当前路径名称一致。 101 } 102 ``` 103 104 105 **图1** 目标的依赖执行顺序 106 107  108 1092. 自定义芯片厂“target_config.h”文件。 110 111 厂商应在“device/MyDeviceCompany/MyBoard”下合适位置创建内核配置文件“target_config.h”,并根据芯片的硬件资源修改参数(具体参数介绍详见表2target_config.h文件主要配置项)。 112 113 参考文件路径:“device/hisilicon/hispark_pegasus/sdk_liteos/platform/os/Huawei_LiteOS/targets/hi3861v100/include/target_config.h” 114 115 >  **说明:** 116 > 117 > 1. 若已有的配置项不能满足需求,可查看“kernel/liteos_m/kernel/include/los_config.h”,其为liteos_m内核的全量配置文件。 118 > 2. “target_config.h”文件中出现的配置将会覆盖“los_config.h”中的配置。 119 120 **表2** target_config.h文件主要配置项 121 122 | 配置项 | 说明 | 参考值 | 123 | -------- | -------- | -------- | 124 | OS_SYS_CLOCK | 系统时钟。 | 40000000UL | 125 | LOSCFG_BASE_CORE_TICK_PER_SECOND | 操作系统节拍的时钟周期。 | 100UL | 126 | LOSCFG_BASE_CORE_TICK_HW_TIME | 定时器裁剪的外部配置项。 | YES | 127 | LOSCFG_PLATFORM_HWI | 是否采用接管中断的方式。 | YES | 128 | LOSCFG_BASE_CORE_TSK_LIMIT | 支持的最大任务个数(除去空闲任务)。 | 32 | 129 | LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE | 空闲任务的堆栈大小。 | 0x180UL | 130 | LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE | 指定默认的任务栈大小,任务栈的大小按 8 字节大小对齐。 | 0x1000UL | 131 | LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE | 表示任务最小需要的堆栈大小。 | ALIGN(0x180, 4) | 132 | LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT | 具有相同优先级任务的最长执行时间。 | 2 | 133 | LOSCFG_BASE_IPC_SEM_LIMIT | 最大支持信号量的个数。 | 100 | 134 | LOSCFG_BASE_IPC_MUX_LIMIT | 最大支持互斥量的个数。 | 64 | 135 | LOSCFG_BASE_IPC_QUEUE_LIMIT | 最大支持消息队列量的个数。 | 64 | 136 | LOSCFG_BASE_CORE_SWTMR_LIMIT | 支持的最大软件定时器数量,而不是可用的软件定时器数量。 | 80 | 137 | LOSCFG_BASE_MEM_NODE_SIZE_CHECK | 配置内存节点大小检查。 | NO | 138 | LOSCFG_PLATFORM_EXC | 异常模块配置项。 | YES | 139 | LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT | 是否使用OS默认的中断。 | NO | 140 1413. 修改内核中断。 142 143 内核提供了两种中断修改方式: 144 145 1. 使用厂商默认中断。 146 147 将“target_config.h”中的宏"LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT"置为NO (0),但需要在xxx.s启动文件中作以下修改: 148 149 - PendSV_Handler:厂商sdk自带中断入口函数,需要替换为OpenHarmony的接口HalPendSV; 150 - SysTick_Handler:厂商sdk自带时钟中断入口函数,需要替换为OpenHarmony的接口OsTickHandler。 151 152 2. 系统初始化时重定向中断。 153 154 将“target_config.h”中的宏"LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT"和"LOSCFG_PLATFORM_HWI"置为YES (1)。 155 156 >  **说明:** 157 > 重定向后的中断向量表g_hwiForm需要根据arch手册要求进行字节对齐,通常0x200字节对齐。 158 159 160## 添加内核子系统 161 162添加完内核子系统后,可以编译出带有系统的工程。通过以下步骤添加内核子系统: 163 1641. 在“config.json”中添加内核子系统。 165 166 路径:“vendor/MyVendorCompany/MyProduct/config.json” 167 168 修改如下: 169 170 ``` 171 { 172 "subsystem": "kernel", # 添加内核子系统 173 "components": [ 174 { 175 "component": "liteos_m", "features":[""] 176 } 177 ] 178 }, 179 ``` 180 1812. 开启/关闭内核特性。 182 183 轻量级系统的内核提供了一些特性,此步骤将指导如何查看、开启/关闭这些特性。 184 185 内核特性:liteos_m提供了包括文件系统、backtrace在内的一系列内核特性开关。 186 187 路径:“kernel/liteos_m/BUILD.gn” 188 189 190 ``` 191 declare_args() { 192 enable_ohos_kernel_liteos_m_cppsupport = true # cpp支持。 193 enable_ohos_kernel_liteos_m_cpup = true # CPU占用率支持。 194 enable_ohos_kernel_liteos_m_exchook = true # 异常处理支持。 195 enable_ohos_kernel_liteos_m_kal = true # kal接口支持。 196 enable_ohos_kernel_liteos_m_fs = true # 文件系统支持。 197 enable_ohos_kernel_liteos_m_backtrace = true # backtrace支持。 198 } 199 group("kernel") { 200 deps = [ 201 "components/bounds_checking_function:sec", 202 "kernel:kernel", 203 "utils:utils", 204 ] 205 if (enable_ohos_kernel_liteos_m_cppsupport == true) { 206 deps += [ "components/cppsupport:cppsupport" ] # 如果内核特性true,则会加入相应的代码进行编译。 207 } 208 …… 209 if (enable_ohos_kernel_liteos_m_kal == true) { 210 deps += [ "kal:kal" ] 211 } 212 } 213 ``` 214 215 特性:可以选择cmsis接口或者posix接口支持。 216 217 路径:“kernel/liteos_m/kal/BUILD.gn” 218 219 220 ``` 221 declare_args() { 222 enable_ohos_kernel_liteos_m_cmsis = true # cmsis支持。 223 enable_ohos_kernel_liteos_m_posix = true # posix支持。 224 } 225 static_library("kal") { 226 sources = [ "kal.c" ] 227 if (enable_ohos_kernel_liteos_m_cmsis == true) { 228 deps += [ "cmsis/" ] # 如果cmsis enable,加入cmsis目录编译。 229 } 230 if (enable_ohos_kernel_liteos_m_posix == true) { 231 deps += [ "posix/" ] # 如果posix enable,加入posix目录编译。 232 } 233 } 234 ``` 235 236 特性:可以选择fatfs支持。 237 238 路径:“kernel/liteos_m/components/fs/BUILD.gn” 239 240 241 ``` 242 declare_args() { 243 enable_ohos_kernel_liteos_m_fatfs = true # fatfs支持 244 } 245 group("fs") { 246 deps = [] 247 if (enable_ohos_kernel_liteos_m_fatfs == true) { 248 deps += [ "fatfs:fatfs" ] 249 } 250 } 251 ``` 252 253 >  **说明:** 254 > 内核特性开关可以在具体产品模组中配置。例如关闭fs和cppsupport特性。 255 > 256 > “vendor/MyVendorCompany/MyProduct/config.json” 257 > 258 > 259 > ``` 260 > "subsystem": "kernel", 261 > "components": [ 262 > { 263 > "component": "liteos_m", 264 > "features":["enable_ohos_kernel_liteos_m_fs = false", 265 > "enable_ohos_kernel_liteos_m_cppsupport = false"] 266 > } 267 > ] 268 > } 269 > ``` 270