OpenHarmony开发者论坛

标题: 【4】AT32F437 OpenHarmony轻量系统移植教程(1) [打印本页]

作者: 润开鸿_贾佳豪    时间: 昨天 10:16
标题: 【4】AT32F437 OpenHarmony轻量系统移植教程(1)
[md]开源地址:https://gitee.com/AT32437_OpenHarmony

## 1.学习本文档的意义

1.学习移植OpenHarmony轻量系统到AT32全系列mcu上,本文档移植的具体型号为AT32F437ZMT7

2.学习OpenHarmony轻量系统开发

## 2.移植前的准备工作

1.移植之前**必须**要先熟悉AT-START-F437开发板的使用,了解板子的裸机编程、板子裸机工程的默认调试串口。

2.获取到AT32F437的底层驱动源代码(Firmware Library)

### 2.0 熟悉AT32F437的裸机开发

1.雅特力提供了官方的开发工具 `AT32IDE`,下载地址:https://www.arterytek.com/cn/product/AT32F437.jsp#Resource

![image.png](https://dl-harmonyos.51cto.com/i ... rocess=image/resize,w_820,h_397)

当然AT32F437也支持了 `Keil`和 `IAR`这个两个开发工具,但是这里我们使用 `AT32IDE`就足够了。

2.具体使用 `AT32IDE`开发AT32F437的步骤请参考雅特力提供的使用手册,笔者在创建第一个裸机工程[437_led_printf](./0_使用雅特力AT32IDE开发裸机工程/437_led_printf/)时候发现默认的调试串口是PA9
![image.png](https://dl-harmonyos.51cto.com/i ... rocess=image/resize,w_820,h_277)

3.通过分析裸机工程获取到要移植涉及的at32f437相关文件,这些都是移植需要的文件

![image.png](https://dl-harmonyos.51cto.com/i ... rocess=image/resize,w_549,h_501)

### 2.1 安装源码对应的hb工具

> 使用什么版本的OpenHarmony的源码,就安装此版本源码对应的hb工具

```
前提条件
Linux服务器,Ubuntu16.04及以上64位系统版本。
Python 3.7.4及以上。
源码下载成功。
安装hb
在源码根目录下执行:

python3 -m pip install --user build/hb

执行hb help有相关帮助信息,有打印信息即表示安装成功,当前hb 主要提供了hb set,hb build,hb tool,hb env,hb clean五个选项。卸载方法:

python3 -m pip uninstall ohos-build
```

## 3.轻量系统AT32F437芯片移植案例

### 3.1 目录规划

AT32F437基于 `Cortex-M4`,移植架构采用Board与SoC分离方案,使用 `arm-none-eabi-gcc`工具链(在轻量系统源码中可以指定编译工具链路径,具体可以查看 https://gitee.com/AT32437_OpenHa ... 86_64-arm-none-eabi)

芯片适配目录规划为:

```
device
├── board              --- 单板厂商目录
│   └── artery_board   --- 单板厂商名字:雅特力
└── soc                --- SoC厂商目录
    └── artery         --- SoC厂商名称
          └── at32f4xx --- SoC Series名
```

产品样例目录规划为:

```
vendor
└── artery             --- 开发产品样例厂商目录
    └── AT-START-F437  --- 产品名字:AT-START-F437
```

### 3.2 预编译适配

预编译适配内容就是围绕hb set命令的适配,使工程能够通过该命令设置根目录、单板目录、产品目录、单板公司名等环境变量,为后续适配编译做准备。

具体的预编译适配步骤如下:

1.在vendor/artery/AT-START-F437目录下新增config.json文件,用于描述这个产品样例所使用的单板、内核等信息,描述信息可参考如下内容:

```
{
  "product_name": "AT-START-F437",      --- 用于hb set进行选择时,显示的产品名称
  "ohos_version": "OpenHarmony 4.1",
  "type": "mini",                       --- 构建系统的类型,mini/small/standard
  "version": "3.0",                     --- 构建系统的版本,1.0/2.0/3.0
  "device_company": "artery_board",     --- 单板厂商名,用于编译时找到/device/board/artery_board目录
  "board": "atstartf437",               --- 单板名,用于编译时找到/device/board/artery_board/atstartf437目录
  "kernel_type": "liteos_m",            --- 内核类型,因为OpenHarmony支持多内核,一块单板可能适配了多个内核,所以需要指定某个内核进行编译
  "kernel_version": "3.0.0",            --- 内核版本,一块单板可能适配了多个linux内核版本,所以需要指定某个具体的内核版本进行编译
  "subsystems": [ ]                     --- 选择所需要编译构建的子系统
}
```

2.在/device/board/artery_board/atstartf437/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"
#######################################################################################################


# Board related common compile flags.
board_cflags = [               # cflag标志
  "-mcpu=cortex-m4",         
  "-march=armv7e-m",
  "-mthumb",
  "-mfloat-abi=hard",
  "-mfpu=fpv4-sp-d16",
  "-O3",                       # gcc编译优化等级为-O3
  "-fdata-sections",
  "-ffunction-sections",
  "-g",
  "-DTRACE",
  "-DAT_START_F437_V1",        # 厂商提供sdk中说明需要添加的cflags标志
  "-DOS_USE_TRACE_SEMIHOSTING_DEBUG",
  "-DAT32F437ZMT7",            # 厂商提供sdk中说明需要添加的cflags标志
  "-DUSE_STDPERIPH_DRIVER",    # 厂商提供sdk中说明需要添加的cflags标志
  "-DEXTEND_SRAM=FLASH_EOPB0_SRAM_512K",  # 厂商提供sdk中说明需要添加的cflags标志
  "-std=c99",
]

board_cxx_flags = board_cflags

board_asmflags = []

board_ld_flags = [ # 链接选项,与厂商Makefile中的LDFLAGS变量对应。
  "-Xlinker",
  "--gc-sections",# 需要的Linker 配置
  "--specs=nano.specs", # 为了使用printf函数
  "--specs=nosys.specs", # 为了使用printf函数
    "-Wl,--wrap=_calloc_r",
    "-Wl,--wrap=_malloc_r",
    "-Wl,--wrap=_realloc_r",
    "-Wl,--wrap=_reallocf_r",
    "-Wl,--wrap=_free_r",
    "-Wl,--wrap=_memalign_r",
    "-Wl,--wrap=_malloc_usable_size_r",

]

# Board related headfiles search path.
board_include_dirs = [
    "//commonlibrary/utils_lite/include",
]

# Board adapter dir for OHOS components.
board_adapter_dir = ""

# Sysroot path.
board_configed_sysroot = ""

# Board storage type, it used for file system generation.
storage_type = ""

```

- `board_cpu`中指定芯片为cortex-m4系列mcu
- `board_toolchain`中指定编译工具链为"arm-none-eabi-gcc"
- `board_cflags`中添加编译标志,
  - 需要添加裸机工程的构建目录的[makefile](./0_使用雅特力AT32IDE开发裸机工程/437_led_printf/Debug/makefile)中的链接标识 `-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -g`,另外关于链接优化等级可以设置为 `-O3`
    - ![image.png](https://dl-harmonyos.51cto.com/i ... rocess=image/resize,w_820,h_219)
  - 还需要从裸机工程[.cproject](./0_使用雅特力AT32IDE开发裸机工程/437_led_printf/.cproject)获取板级cflags `-DAT_START_F437_V1`、`-DAT32F437ZMT7`、`-DUSE_STDPERIPH_DRIVER`、`-DEXTEND_SRAM=FLASH_EOPB0_SRAM_512K`
    - ![image.png](https://dl-harmonyos.51cto.com/i ... rocess=image/resize,w_820,h_168)
[/md]




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5