1# Small System STM32MP1 SoC Porting Case 2 3This document describes how to port small development boards with screens based on the [BearPi-HM Micro development board](https://gitee.com/openharmony/device_board_bearpi) of the `STM32MP157` SoC from STMicroelectronics, so as to adapt components such as `ace_engine_lite`, `graphic_ui`, `aafwk_lite`, `appexecfwk_lite`, and `HDF` to the `OpenHarmony LiteOS-A` kernel. The porting architecture uses the solution where `Board` and `SoC` are separated. 4 5## Compilation and Building 6 7### Directory Planning 8 9This solution designs the directory structure using the [board and SoC decoupling idea](https://gitee.com/openharmony-sig/sig-content/blob/master/devboard/docs/board-soc-arch-design.md), and plans the SoC adaptation directory as follows: 10 11``` 12device 13├── board --- Board vendor directory 14│ └── bearpi --- Board vendor: BearPi 15│ └── bearpi_hm_micro --- Board name: bearpi_hm_micro 16└── soc --- SoC vendor directory 17 └── common --- Directory storing common HDF drivers. 18 └── st --- SoC vendor: STMicroelectronics 19 └── stm32mp1xx --- SoC series: stm32mp1xx 20``` 21 22The planned product demo directory is as follows: 23 24``` 25vendor 26└── bearpi --- Vendor of the product demo, which is BearPi here 27 └── bearpi_hm_micro --- Product name: bearpi_hm_micro development board 28``` 29 30### Precompilation Adaptation 31 32Before porting, you need to perform precompilation adaptation. 33 34The precompilation adaptation process mainly uses the `hb set` command to set environment variables such as the project root directory, board directory, product directory, and board vendor name, to make preparations for compilation. 35 36The procedure is as follows: 37 381. Add the `config.json` file to the `vendor/bearpi/bearpi_hm_micro` directory to describe board and kernel information used by the product demo. The sample code is as follows: 39 40 ``` 41 { 42 "product_name": "bearpi_hm_micro", --- Product name displayed when the **hb set** command is used. 43 "version": "3.0", --- Version of the system, which can be 1.0, 2.0, or 3.0. 44 "type": "small", --- System type, which can be mini, small, or standard. 45 "ohos_version": "OpenHarmony 3.0", --- OpenHarmony system version. 46 "device_company": "bearpi", --- Board vendor name, which is used to find the /device/board/bearpi directory during compilation. 47 "board": "bearpi_hm_micro", --- Board name, which is used to find the /device/board/bearpi/bearpi_hm_micro directory during compilation. 48 "kernel_type": "liteos_a", --- Kernel type. OpenHarmony supports multiple kernels, and one board may adapt to multiple kernels. Therefore, you need to specify a kernel for compilation. 49 "kernel_version": "", --- Kernel version. One board may adapt to multiple Linux kernel versions. Therefore, you need to specify a kernel version for compilation. 50 "subsystems": [ ] --- Subsystem to be built. 51 } 52 ``` 53 542. Add the `config.gni` file to the `device/board/bearpi/bearpi_hm_micro/liteos_a` directory to describe board and kernel information used by the product demo. The sample code is as follows: 55 56 ``` 57 # Kernel type, e.g. "linux", "liteos_a", "liteos_m". 58 kernel_type = "liteos_a" 59 60 # Kernel version. 61 kernel_version = "" 62 63 # Board CPU type, e.g. "cortex-a7", "riscv32". 64 board_cpu = "cortex-a7" 65 66 # Board arch, e.g. "armv7-a", "rv32imac". 67 board_arch = "" 68 69 # Toolchain name used for system compiling. 70 # E.g. gcc-arm-none-eabi, arm-linux-harmonyeabi-gcc, ohos-clang, riscv32-unknown-elf. 71 # Note: The default toolchain is "ohos-clang". It's not mandatory if you use the default toolchain. 72 board_toolchain = "" 73 74 # The toolchain path installed, it's not mandatory if you have added toolchain path to your ~/.bashrc. 75 board_toolchain_path = "" 76 77 # Compiler prefix. 78 board_toolchain_prefix = "" 79 80 # Compiler type, "gcc" or "clang". 81 board_toolchain_type = "clang" 82 83 # Board related common compile flags. 84 board_cflags = [ 85 "-mfloat-abi=softfp", 86 "-mfpu=neon-vfpv4", 87 ] 88 board_cxx_flags = [ 89 "-mfloat-abi=softfp", 90 "-mfpu=neon-vfpv4", 91 ] 92 board_ld_flags = [] 93 94 # Board related headfiles search path. 95 board_include_dirs = [] 96 97 # Board adapter dir for OHOS components. 98 board_adapter_dir = "//device/board/bearpi/bearpi_hm_micro/hal" 99 100 # Sysroot path. 101 board_configed_sysroot = "" 102 103 # Board storage type, it used for file system generation. 104 storage_type = "emmc" 105 ``` 106 1073. Verify that the `hb set` configuration is correct. The `hb set` configuration is correct if the output information is as shown in the following figure. 108 109 Run `hb set`, enter the project root directory, and press **Enter**. The `hb` command will traverse all `config.json` files in the `//vendor/<product_company>/<product_name>` directory and list all available product compilation options. In `config.json`, `product_name` is used to display the product name, and `device_company` and `board` are used to associate the `//device/board/<device_company>/<board>` directory and match the `<any_dir_name>/config.gni` file. If multiple files are matched, the board adapts to multiple kernels. Then, `kernel_type` and `kernel_version` in `config.json` can be used to uniquely match `kernel_type` and `kernel_version` in `config.gni`, thus determining the board with a kernel to be compiled. 110 111 ![hb set](figures/bearpi_hm_micro_hb_set.png) 112 113 After you select a product and press **Enter**, a **ohos_config.json** file will be generated in the root directory. The file will list the product information to be compiled. You can also run the `hb env` command to view the selected precompilation environment variables. 114 115 ![hb env](figures/bearpi_hm_micro_hb_env.png) 116 117 118 119## Kernel Porting 120 121Kernel porting requires `LiteOS-A Kconfig` adaptation, `gn` compilation and building, and minimum kernel startup adaptation. 122 123For details about the porting procedure, see [LiteOS-A kernel porting](porting-smallchip-kernel-a.md). 124### Kconfig Adaptation 1251. Add the chip, product name, and vendor name configurations to **//device/board/bearpi/bearpi_hm_micro/liteos_a/drivers/Kconfig**. 126 ``` 127 source "../../device/soc/st/common/platform/Kconfig" 128 129 config PLATFORM 130 string 131 default "stm32mp157" if PLATFORM_STM32MP157 132 133 config PRODUCT_NAME 134 string "product name" 135 default "bearpi_hm_micro" if PRODUCT_BEARPI_HM_MICRO 136 137 config DEVICE_COMPANY 138 string "vendor name" 139 default "st" if PLATFORM_STM32MP157 140 141 config TEE_ENABLE 142 bool "Enable TEE" 143 default n 144 depends on PLATFORM_STM32MP157 145 help 146 Enable teeos in platform 147 ``` 1482. Add driver configurations to **//device/soc/st/common/platform/Kconfig**. 149 ``` 150 config DRIVERS_MMC 151 depends on DRIVERS 152 bool "Enable MMC" 153 default y 154 depends on DRIVERS && FS_VFS 155 help 156 Answer Y to enable LiteOS support MMC driver. 157 158 config DRIVERS_EMMC 159 depends on DRIVERS_MMC && PLATFORM_STM32MP157 160 bool "Enable MMC0 support eMMC type" 161 162 config DRIVERS_HI3881 163 bool "Enable Hi3881 Host driver" 164 default n 165 depends on DRIVERS_HDF_WIFI 166 help 167 Answer Y to enable Hi3881 Host driver. 168 config HW_RANDOM_ENABLE 169 depends on DRIVERS_RANDOM 170 bool "Select hw random" 171 default y 172 help 173 Answer Y to select hw random. 174 ``` 1753. Enable related configurations in **//vendor/bearpi/bearpi_hm_micro/kernel_configs/debug_tee.config**. 176 ``` 177 ... 178 LOSCFG_PLATFORM="stm32mp157" 179 LOSCFG_PRODUCT_NAME="bearpi_hm_micro" 180 LOSCFG_DEVICE_COMPANY="st" 181 # LOSCFG_PLATFORM_HI3516DV300 is not set 182 # LOSCFG_PLATFORM_HI3518EV300 is not set 183 # LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7 is not set 184 LOSCFG_PLATFORM_STM32MP157=y 185 LOSCFG_PRODUCT_BEARPI_HM_MICRO=y 186 LOSCFG_BOARD_CONFIG_PATH="device/board/bearpi/bearpi_hm_micro/liteos_a/board" 187 LOSCFG_TEE_ENABLE=y 188 ... 189 ``` 190### GN Build Adaptation 1911. Create **BUILD.gn** in **//device/board/bearpi/bearpi_hm_micro/liteos_a** and add the following code. This module depends on **board**, **drivers**, and **hdf_config**. 192 ``` 193 cmd = "if [ -f $product_path/hdf_config/BUILD.gn ]; then echo true; else echo false; fi" 194 HAVE_PRODUCT_CONFIG = 195 exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") 196 197 group("liteos_a") { 198 deps = [ 199 "board", 200 "drivers", 201 ] 202 if (HAVE_PRODUCT_CONFIG) { 203 deps += [ "$product_path/hdf_config" ] 204 } else { 205 deps += [ "hdf_config" ] 206 } 207 } 208 209 config("public") { 210 configs = [ 211 "board:public", 212 "drivers:public", 213 ] 214 } 215 ``` 2162. Create **BUILD.gn** in **//device/board/bearpi/bearpi_hm_micro/liteos_a/board** and add the following code: Compile **os_adapt.c** kernel startup code into the system. 217 ``` 218 import("//kernel/liteos_a/liteos.gni") 219 220 module_name = "bsp_config" 221 222 kernel_module(module_name) { 223 sources = [] 224 if (defined(LOSCFG_PLATFORM_ADAPT)) { 225 sources += [ "os_adapt/os_adapt.c" ] 226 } 227 } 228 229 config("public") { 230 include_dirs = [ "." ] 231 include_dirs += [ "include" ] 232 include_dirs += [ "$LITEOSTOPDIR/drivers/block/disk/include" ] 233 include_dirs += 234 [ "$LITEOSTOPDIR/../../drivers/adapter/khdf/liteos/osal/include" ] 235 } 236 ``` 2373. Create **BUILD.gn** in **//device/board/bearpi/bearpi_hm_micro/liteos_a/drivers**, add the following code, and compile the HDF driver under **device/soc/st/common/platform** into the system: 238 ``` 239 import("//drivers/adapter/khdf/liteos/hdf.gni") 240 241 group("drivers") { 242 public_deps = [ "//device/soc/st/common/platform:drivers" ] 243 } 244 245 config("public") { 246 configs = [ "//device/soc/st/common/platform:public" ] 247 } 248 249 ``` 2504. Create **BUILD.gn** in **//vendor/bearpi/bearpi_hm_micro/hdf_config**, add the following code, and compile the HCS configuration file into the system: 251 ``` 252 module_switch = defined(LOSCFG_DRIVERS_HDF) && !defined(LOSCFG_DRIVERS_HDF_TEST) 253 module_name = "libhdf_config" 254 hdf_driver(module_name) { 255 hcs_sources = [ "hdf.hcs" ] 256 } 257 258 group("hdf_config") { 259 public_deps = [ ":$module_name" ] 260 deps = [ 261 "hdf_test", 262 "hdf_test/hcs_macro_test", 263 ] 264 } 265 ``` 266### Kernel Startup Adaptation 2671. Add the following kernel startup code to **//device/board/bearpi/bearpi_hm_micro/liteos_a/board/os_adapt.c**. For details, see [LiteOS-A kernel porting](porting-smallchip-kernel-a.md). 268 ``` 269 ... 270 void SystemInit(void) 271 { 272 #ifdef LOSCFG_DRIVERS_RANDOM 273 dprintf("dev random init ...\n"); 274 Mp1xxRngInit(); 275 #endif 276 #ifdef LOSCFG_DRIVERS_MEM 277 dprintf("mem dev init ...\n"); 278 extern int mem_dev_register(void); 279 mem_dev_register(); 280 #endif 281 282 dprintf("Date:%s.\n", __DATE__); 283 dprintf("Time:%s.\n", __TIME__); 284 285 #ifdef LOSCFG_DRIVERS_HDF 286 dprintf("DeviceManagerStart start ...\n"); 287 if (DeviceManagerStart()) { 288 PRINT_ERR("No drivers need load by hdf manager!"); 289 } 290 dprintf("DeviceManagerStart end ...\n"); 291 #endif 292 net_init(); 293 294 #ifdef LOSCFG_PLATFORM_ROOTFS 295 dprintf("OsMountRootfs start ...\n"); 296 if (LOS_GetCmdLine()) { 297 dprintf("get cmdline error!\n"); 298 } 299 if (LOS_ParseBootargs()) { 300 dprintf("parse bootargs error!\n"); 301 } 302 if (OsMountRootfs()) { 303 dprintf("mount rootfs error!\n"); 304 } 305 dprintf("OsMountRootfs end ...\n"); 306 #endif 307 308 dprintf("Before PLATFORM_UART ...\n"); 309 310 #ifdef LOSCFG_DRIVERS_HDF_PLATFORM_UART 311 if (virtual_serial_init(TTY_DEVICE) != 0) { 312 PRINT_ERR("virtual_serial_init failed"); 313 } 314 if (system_console_init(SERIAL) != 0) { 315 PRINT_ERR("system_console_init failed\n"); 316 } 317 #endif 318 319 dprintf("After PLATFORM_UART ...\n"); 320 321 if (OsUserInitProcess()) { 322 PRINT_ERR("Create user init process failed!\n"); 323 return; 324 } 325 dprintf("cat log shell end\n"); 326 return; 327 } 328 ... 329 ``` 330 331 332## Board-Level OS Porting 333 334### Porting the HDF Driver for the SoC Platform 335 336Driver adaptation files are stored in `device/soc/st/common/platform` and are loaded using the `HDF` mechanism. This section uses GPIO driver adaptation as an example. 337 3381. Describe the building adaptation of the stm32mp1xx `gpio` driver in the `//device/soc/st/common/platform/gpio/BUILD.gn` file. The sample code is as follows: 339 340 ``` 341 module_switch = defined(LOSCFG_DRIVERS_HDF_PLATFORM_GPIO) --- If the GPIO configuration switch of the HDF is enabled, the following is built: 342 module_name = get_path_info(rebase_path("."), "name") 343 344 hdf_driver("hdf_gpio") { 345 sources = [ "stm32mp1_gpio.c" ] --- GPIO driver source file 346 include_dirs = [ --- Dependent .h path 347 "." , 348 "../stm32mp1xx_hal/STM32MP1xx_HAL_Driver/Inc", 349 ] 350 } 351 ``` 352 3532. Describe the source code adaptation of the stm32mp1xx `gpio` driver in the `//device/soc/st/common/platform/gpio/stm32mp1_gpio.c` file. 354 First, load the basic driver adaptation framework based on the `HDF` driver framework of `OpenHarmony`, as shown below: 355 356 ``` 357 struct HdfDriverEntry g_GpioDriverEntry = { 358 .moduleVersion = 1, 359 .moduleName = "HDF_PLATFORM_GPIO", 360 .Bind = GpioDriverBind, 361 .Init = GpioDriverInit, 362 .Release = GpioDriverRelease, 363 }; 364 HDF_INIT(g_GpioDriverEntry); --- Load the GPIO driver using HDF_INIT. 365 ``` 3663. Add the GPIO hardware description file **gpio_config.hcs** to **//device/soc/st/stm32mp1xx/sdk_liteos/hdf_config/gpio**, and add private configuration information of the driver to the file. 367 ``` 368 root { 369 platform { 370 gpio_config { 371 controller_0x50002000 { 372 match_attr = "st_stm32mp1_gpio"; 373 groupNum = 11; 374 bitNum = 16; 375 gpioRegBase = 0x50002000; 376 gpioRegStep = 0x1000; 377 irqRegBase = 0x5000D000; 378 irqRegStep = 0x400; 379 } 380 } 381 } 382 } 383 ``` 3844. Configure the product load driver. All device information of the product is defined in the source code file **//vendor/bearpi/bearpi_hm_micro/hdf_config/device_info/device_info.hcs**. 385 386 Add the platform driver to the host of the platform. 387 388 > **NOTE** 389 > 390 > **moduleName** must be the same as that defined in the driver file, and **deviceMatchAttr** must be the same as **match_attr** defined in the driver's private configuration information file **gpio_config.hcs**. 391 392 393 ``` 394 root { 395 ... 396 platform :: host { 397 device_gpio :: device { 398 device0 :: deviceNode { 399 policy = 2; 400 priority = 10; 401 permission = 0644; 402 moduleName = "HDF_PLATFORM_GPIO_MANAGER"; 403 serviceName = "HDF_PLATFORM_GPIO_MANAGER"; 404 } 405 device1 :: deviceNode { 406 policy = 0; 407 priority = 10; 408 permission = 0644; 409 moduleName = "HDF_PLATFORM_GPIO"; 410 serviceName = "HDF_PLATFORM_GPIO"; 411 deviceMatchAttr = "st_stm32mp1_gpio"; 412 } 413 } 414 } 415 } 416 ``` 4175. Complete driver code. Configuration information in **gpio_config.hcs** will be loaded in **GpioDriverInit**. 418 ``` 419 static int32_t GpioDriverInit(struct HdfDeviceObject *device) 420 { 421 422 int32_t ret; 423 struct Mp1xxGpioCntlr *stm32gpio = &g_Mp1xxGpioCntlr; 424 425 dprintf("%s: Enter", __func__); 426 if (device == NULL || device->property == NULL) { 427 HDF_LOGE("%s: device or property NULL!", __func__); 428 return HDF_ERR_INVALID_OBJECT; 429 } 430 // Obtain property data. 431 ret = Mp1xxGpioReadDrs(stm32gpio, device->property); 432 if (ret != HDF_SUCCESS) { 433 HDF_LOGE("%s: get gpio device resource fail:%d", __func__, ret); 434 return ret; 435 } 436 ... 437 } 438 ``` 439 440### OpenHarmony Subsystem Adaptation 441 442To adapt `OpenHarmony` subsystems, you only need to add related subsystems and components to `config.json`, so that the components can be included in the compilation by the compilation system. 443 444#### Startup Subsystem Adaptation 445 446For the startup subsystem, adapt the `bootstrap_lite`, `syspara_lite`, `appspawn_lite`, and `init` components. Add the corresponding configuration items to the `vendor/bearpi/bearpi_hm_micro/config.json` file, as shown below: 447 448``` 449 { 450 "subsystem": "startup", 451 "components": [ 452 { "component": "syspara_lite", "features":[] }, 453 { "component": "bootstrap_lite", "features":[] }, 454 { "component": "appspawn_lite", "features":[] }, 455 { "component": "init", "features":[] } 456 ] 457 }, 458``` 459The system will be started according to startup configurations in the **//vendor/bearpi/bearpi_hm_micro/init_configs** file. 460 461 462#### DFX Subsystem Adaptation 463 464To adapt the `DFX` subsystem, you need to add the `hilog_featured_lite` and `hidumper_lite` components to the `config.json` file. 465 466``` 467 { 468 "subsystem": "hiviewdfx", 469 "components": [ 470 { "component": "hilog_featured_lite", "features":[] }, 471 { "component": "hidumper_lite", "features":[] } 472 ] 473 }, 474``` 475 476#### System Service Management Subsystem Adaptation 477 478To adapt the system service management subsystem, you need to add the `samgr_lite`, `safwk_lite`, and `dmsfwk_lite` components to the `config.json` file. 479 480``` 481 { 482 "subsystem": "distributed_schedule", 483 "components": [ 484 { "component": "samgr_lite", "features":[] }, 485 { "component": "safwk_lite", "features":[] }, 486 { "component": "dmsfwk_lite", "features":[] } 487 ] 488 }, 489``` 490 491#### Security Subsystem Adaptation 492 493To adapt the security subsystem, you need to add the `permission_lite`, `appverify`, `device_auth`, and `huks` components to the `config.json` file. 494 495``` 496 { 497 "subsystem": "security", 498 "components": [ 499 { "component": "permission_lite", "features":[] }, 500 { "component": "appverify", "features":[] }, 501 { "component": "device_auth", "features":[] }, 502 { "component": "huks", "features": 503 [ 504 "huks_config_file = \"hks_config_small.h\"" 505 ] 506 } 507 ] 508 }, 509``` 510 511 512#### utils Subsystem Adaptation 513 514To adapt the utils subsystem, you need to add the `kv_store` and `os_dump` components to the `config.json` file. 515 516``` 517 { 518 "subsystem": "utils", 519 "components": [ 520 { "component": "kv_store", "features":[] }, 521 { "component": "os_dump", "features":[] } 522 ] 523 }, 524``` 525 526#### Graphics Subsystem Adaptation 527 528To adapt the graphics subsystem, you need to add the `graphic_utils` component to the `config.json` file. 529 530``` 531 { 532 "subsystem": "graphic", 533 "components": [ 534 { "component": "graphic_utils", 535 "features": [ "enable_ohos_graphic_utils_product_config = true" 536 ] 537 }, 538 { "component": "graphic_hals", "features":[] }, 539 { "component": "ui", "features":[ "enable_graphic_font = true","enable_video_component=false"] }, 540 { "component": "surface", "features":[] }, 541 { "component": "wms", "features":[] } 542 ] 543 }, 544``` 545 546For details about `graphic` configuration, see `//vendor/bearpi/bearpi_hm_micro/graphic_config/product_graphic_lite_config.h`. 547 548 549#### ACE Subsystem Adaptation 550 551To adapt the ACE subsystem, you need to add the `ace_engine_lite` component to the `config.json` file. 552 553``` 554 { 555 "subsystem": "ace", 556 "components": [ 557 { 558 "component": "ace_engine_lite", 559 "features": [ 560 "enable_ohos_ace_engine_lite_product_config = true" 561 ] 562 } 563 ] 564 }, 565``` 566For details about `ace_engine_lite` configuration, see `//vendor/bearpi/bearpi_hm_micro/ace_lite_config/product_acelite_config.h`. 567 568#### aafwk Subsystem Adaptation 569 570To adapt the aafwk subsystem, you need to add the `aafwk_lite` component to the `config.json` file. 571 572``` 573 { 574 "subsystem": "aafwk", 575 "components": [ 576 { 577 "component": "aafwk_lite", 578 "features": [ 579 "enable_ohos_appexecfwk_feature_ability = true" --- The FA feature is supported, that is, the graphics capability is included. 580 ] 581 } 582 ] 583 }, 584``` 585 586 587#### appexecfwk Subsystem Adaptation 588 589To adapt the appexecfwk subsystem, you need to add the `appexecfwk_lite` component to the `config.json` file. 590 591``` 592 { 593 "subsystem": "appexecfwk", 594 "components": [ 595 { 596 "component": "appexecfwk_lite" 597 } 598 ] 599 }, 600``` 601