OpenHarmony开发者论坛

标题: 兆易创新Cortex-M7 GD32H459适配OpenHarmony轻量系统适配教程 [打印本页]

作者: 离北况归    时间: 2024-10-10 02:30
标题: 兆易创新Cortex-M7 GD32H459适配OpenHarmony轻量系统适配教程
[md]笔者利用国庆假期的时间适配了一款Cortex-M7 的国产厂商兆易创新GD32H459,开源地址:https://gitee.com/GD32H759_OpenHarmony

# OpenHarmony 4.1r 轻量系统移植到GD32H759文档

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

1.学习如何移植OpenHarmony轻量系统4.1r到GD32 m7内核GD32H737/GD32H757/GD32H759系列MCU上。

2.本文档移植的具体型号为GD32H759IMT6,采用的开发板是[慧勤智远GD32H759IMT6开发板](https://item.taobao.com/item.htm ... 2%7D&xxc=ad_ztc)

## 2.学习OpenHarmony轻量系统开发

1.移植之前必须要先熟悉慧勤智远GD32H759IMT6开发板的使用,了解板子的裸机编程、板子裸机工程的默认调试串口。

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

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

1.兆易创新提供了官方的开发工具GD32 Embedded Builder,下载地址:https://www.gd32mcu.com/cn/download/0?kw=GD32H7

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

2.解压直接打开(免安装)

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

3.新建C工程

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

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

4.选择arm工程
![image.png](https://dl-harmonyos.51cto.com/i ... rocess=image/resize,w_820,h_763)

5.选择GD32H759系列芯片GD32H759IMT6

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

6.此时就可以看到GD32H759IMT6的驱动库、ld链接文件和.s文件

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

7.简单点个灯

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

8.调试串口,PA9和PA10两个GPIO口

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

[GD32H759IMT6点灯和printf打印样例](https://gitee.com/GD32H759_OpenH ... %BE%8B/GD32H759IMT6)

### 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.轻量系统GD32H759IMT6芯片移植案例

### 3.1 目录规划

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

芯片适配目录规划为:

```
device
├── board                  --- 单板厂商目录
│   └── gigadevice_board   --- 单板厂商名字:兆易创新
└── soc                    --- SoC厂商目录
    └── gigadevice         --- SoC厂商名称
          └── gd32h7xx     --- SoC Series名
```

产品样例目录规划为:

```
vendor
└── gigadevice              --- 开发产品样例厂商目录
    └── GD32H759IMT6_BOARD  --- 产品名字:GD32H759IMT6_BOARD
```

### 3.2 预编译适配

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

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

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

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

2.在device\board\gigadevice_board\gd32h759\liteos_m目录下新增一个config.gni文件,用于描述该产品的编译配置信息,内核配置为m7

```
# 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".
# GD32H759IMT6
board_cpu = "cortex-m7"
```

### 3.3 添加GD32H759IMT6驱动库

把原厂的驱动文件放置在device\soc\gigadevice\gd32h7xx\libraries和device\soc\gigadevice\gd32h7xx\liteos_m目录下面,为了消除编译报错需要进行一些修改

1.device/soc/artery/at32f4xx/libraries/drivers/inc/gd32h7xx_timer.h中INVALID修改为GD32H759IMT6_INVALID,规避重定义问题

```
/* constants definitions */
/* UPIFBU bit state */
/*GD32H759IMT6*/
typedef enum {VALID_RESET = 0, VALID_SET = 1, GD32H759IMT6_INVALID = 2} UPIFBUStatus;
```

2.device/soc/artery/at32f4xx/libraries/drivers/src/gd32h7xx_timer.c中INVALID修改为GD32H759IMT6_INVALID,规避重定义问题

```
/*!
    \brief      get the UPIFBU bit in the TIMERx_CNT register
    \param[in]  timer_periph: TIMERx(x=0~7,14~16,22,23,30,31,40~44,50,51)
    \param[out] none
    \retval     UPIFBUStatus: VALID_SET or VALID_RESET or GD32H759IMT6_INVALID
      \ret        VALID_SET: the UPIFBU is valid and value is 1
      \ret        VALID_RESET: the UPIFBU is valid and value is 0
      \ret        GD32H759IMT6_INVALID: the UPIFBU is invalid
*/
UPIFBUStatus timer_upifbu_bit_get(uint32_t timer_periph)
{
    if((TIMER_CTL0(timer_periph) & TIMER_CTL0_UPIFBUEN)) {
        if((TIMER50 == timer_periph) || (TIMER51 == timer_periph)) {
            if(TIMER_CNTH(timer_periph) & TIMER_CNT_UPIFBU) {
                return VALID_SET;
            } else {
                return VALID_RESET;
            }
        } else {
            if(TIMER_CNT(timer_periph) & TIMER_CNT_UPIFBU) {
                return VALID_SET;
            } else {
                return VALID_RESET;
            }
        }
    } else {
        return GD32H759IMT6_INVALID;
    }
}
```

### 3.4 修改.ld文件

GD32H759IMT6是链接脚本gd32h7xx_flash.ld,适配OpenHarmony轻量系统需要做以下修改,具体修改对比请查看[GD32H759IMT6适配OpenHarmony轻量系统前后gd32h7xx_flash](https://gitee.com/GD32H759_OpenH ... %BF%AE%E6%94%B9.htm)

1.注释 `GROUP(libgcc.a libc.a libm.a libnosys.a)`,取消libgcc.a libc.a libm.a libnosys.a的引用

### 3.5 在config.gni添加编译时需要的cflag和LDFLAGS

1.前面提到了兆易创新提供了开发工具GD32 Embedded Builder,需要从中提取编译时需要添加的编译标志,如下图所示

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

```
arm-none-eabi-gcc -mcpu=cortex-m7 -mthumb -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections  -g3 -Wl,-Map,"GD32H759IMT6.map" -T gd32h7xx_flash.ld -Xlinker --gc-sections -L"../ldscripts" -Xlinker --cref --specs=nano.specs -u_printf_float --specs=nosys.specs -o "GD32H759IMT6.elf"  ./src/gd32h759i_eval.o ./src/gd32h7xx_it.o ./src/main.o ./src/systick.o  ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_adc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_can.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_cau.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_cau_aes.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_cau_des.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_cau_tdes.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_cmp.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_cpdm.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_crc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_ctc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_dac.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_dbg.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_dci.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_dma.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_edout.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_efuse.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_enet.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_exmc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_exti.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_fac.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_fmc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_fwdgt.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_gpio.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_hau.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_hau_sha_md5.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_hpdf.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_hwsem.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_i2c.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_ipa.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_lpdts.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_mdio.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_mdma.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_misc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_ospi.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_ospim.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_pmu.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_rameccmu.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_rcu.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_rspdif.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_rtc.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_rtdec.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_sai.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_sdio.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_spi.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_syscfg.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_timer.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_tli.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_tmu.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_trigsel.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_trng.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_usart.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_vref.o ./firmware/gd32h7xx_standard_peripheral/src/gd32h7xx_wwdgt.o  ./firmware/cmsis/src/syscalls.o ./firmware/cmsis/src/system_gd32h7xx.o  ./firmware/cmsis/gcc_startup/startup_gd32h7xx.o   
```

2.还需要添加MCU宏 `GD32H7XX`,`USE_STDPERIPH_DRIVER`

### 3.6 Cortex-M7的MCU和Cortex-M4适配Liteos-m的不同——fpu适配

适配Cortex-M7的GD32H759IMT6时,需要在config.gni的board_cflags中添加 `-D__FPU_PRESENT=1U``-D__FPU_USED=1U`,并且需要在gd32h7xx.h中注释掉__FPU_PRESENT。

OpenHarmony轻量系统源码kernel/liteos_m/arch/arm/cortex-m7/gcc/los_dispatch.S中需要同时定义__FPU_PRESENT=1U和__FPU_USED=1U,fpu才能添加进编译

相关issues:https://gitee.com/openharmony/ke ... ?from=project-issue

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

### 3.7 修改.s文件,并且LiteOS-M接管外部GD32H759IMT6中断

[startup_gd32h7xx.S](https://gitee.com/GD32H759_OpenH ... /startup_gd32h7xx.S)中继续使用GD32H759IMT6原生的中断处理函数

```
                    .word     SysTick_Handler                     /*使用GD32H759IMT6原生的中断处理函数*/
```

然后在[target_config.h](https://gitee.com/GD32H759_OpenH ... ude/target_config.h)的 `LOSCFG_PLATFORM_HWI`和 `LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT`定义为1。表明LiteOS-M接管了GD32H759IMT6的中断
[/md]




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