当前位置: 首页 > article >正文

【5】AT32F437 OpenHarmony轻量系统移植教程(2)

开源地址:https://gitee.com/AT32437_OpenHarmony

学习本文档的意义

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

2.学习OpenHarmony轻量系统开发

3.适配vprintf, vfprintf, printf, snprintf 和sprintf

1.首先获取AT32F437的底层驱动源代码(Firmware Library),我们需要从官方托管的gitee仓库AT32F435_437_Firmware_Library获取

2.找到at32f435_437_board.c文件,注释掉__io_putchar函数,使用fputc函数

// #if defined (__GNUC__) && defined (__clang__)
//   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
// #else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
// #endif

再注释掉setvbuf函数,这个函数影响适配适配printf

/**
  * @brief  initialize uart
  * @param  baudrate: uart baudrate
  * @retval none
  */
void uart_print_init(uint32_t baudrate)
{
  gpio_init_type gpio_init_struct;

// #if defined (__GNUC__) && !defined (__clang__)
//   setvbuf(stdout, NULL, _IONBF, 0);               //printf函数有关
// #endif

  /* enable the uart and gpio clock */
  crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE);
  crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE);

3.在/device/board/artery_board/atstartf437/liteos_m/config.gni新增_malloc_r, _realloc_r, _reallocf_r, _free_r, _memalign_r, 和 _malloc_usable_size_r函数的wrap链接选项。

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",
]

4.然后重写printf和sprintf函数,具体实现查看dprintf.c

#include <stdarg.h>
#include <stdio.h>
#include "securec.h"
#include "los_interrupt.h"

unsigned int intSave = 0;

//print函数的buffer大小
#define BUFSIZE  512 

static void dputs(char const *s, int (*pFputc)(int n, FILE *cookie), void *cookie)
{
    intSave = LOS_IntLock();

    while (*s) {
        pFputc(*s++, cookie);
    }

    LOS_IntRestore(intSave);
}

int printf(const char *__restrict __format, ...)
{
    char buf[BUFSIZE] = { 0 };
    int len;
    va_list ap;
    va_start(ap, __format);
    len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, __format, ap);
    va_end(ap);
    if(len > 0) {
        dputs(buf, fputc, 0);
    } else {
        dputs("printf error!\n", fputc, 0);
    }
    return len;
}

5.在main.c的main.c里面初始化串口打印函数uart_print_init

3.适配vprintf, vfprintf, printf, snprintf 和sprintf

1.首先获取AT32F437的底层驱动源代码(Firmware Library),我们需要从官方托管的gitee仓库AT32F435_437_Firmware_Library获取

2.找到at32f435_437_board.c文件,注释掉__io_putchar函数,使用fputc函数

// #if defined (__GNUC__) && defined (__clang__)
//   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
// #else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
// #endif

再注释掉setvbuf函数,这个函数影响适配适配printf

/**
  * @brief  initialize uart
  * @param  baudrate: uart baudrate
  * @retval none
  */
void uart_print_init(uint32_t baudrate)
{
  gpio_init_type gpio_init_struct;

// #if defined (__GNUC__) && !defined (__clang__)
//   setvbuf(stdout, NULL, _IONBF, 0);               //printf函数有关
// #endif

  /* enable the uart and gpio clock */
  crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE);
  crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE);

3.在/device/board/artery_board/atstartf437/liteos_m/config.gni新增_malloc_r, _realloc_r, _reallocf_r, _free_r, _memalign_r, 和 _malloc_usable_size_r函数的wrap链接选项。

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",
]

4.然后重写printf和sprintf函数,具体实现查看dprintf.c

#include <stdarg.h>
#include <stdio.h>
#include "securec.h"
#include "los_interrupt.h"

unsigned int intSave = 0;

//print函数的buffer大小
#define BUFSIZE  512 

static void dputs(char const *s, int (*pFputc)(int n, FILE *cookie), void *cookie)
{
    intSave = LOS_IntLock();

    while (*s) {
        pFputc(*s++, cookie);
    }

    LOS_IntRestore(intSave);
}

int printf(const char *__restrict __format, ...)
{
    char buf[BUFSIZE] = { 0 };
    int len;
    va_list ap;
    va_start(ap, __format);
    len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, __format, ap);
    va_end(ap);
    if(len > 0) {
        dputs(buf, fputc, 0);
    } else {
        dputs("printf error!\n", fputc, 0);
    }
    return len;
}

5.在main.c的main.c里面初始化串口打印函数uart_print_init

alt text

4.添加at32f437驱动库

前面提到首先获取AT32F437的底层驱动源代码(Firmware Library),我们需要从官方托管的gitee仓库AT32F435_437_Firmware_Library获取。

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

1.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437.h中typedef int32_t INT32;typedef uint32_t UINT32;注释掉


//typedef int32_t  INT32;
typedef int16_t  INT16;
typedef int8_t   INT8;
//typedef uint32_t UINT32;

2.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437.h中,FALSE修改为AT_FALSE,TRUE修改为AT_TRUE,避开宏的重定义

typedef enum {AT_FALSE = 0, AT_TRUE = !AT_FALSE} confirm_state; 

3.device/soc/artery/at32f4xx/libraries/drivers/inc/at32f435_437_def.h中注释掉


//#define UNUSED(x)                        (void)x /* to avoid gcc/g++ warnings */

4.device/soc/artery/at32f4xx/libraries/drivers/src/at32f435_437_crm.c中注释DUMMY_NOP();这个函数

/**
  * @brief  select system clock source
  * @param  value
  *         this parameter can be one of the following values:
  *         - CRM_SCLK_HICK
  *         - CRM_SCLK_HEXT
  *         - CRM_SCLK_PLL
  * @retval none
  */
void crm_sysclk_switch(crm_sclk_type value)
{
  CRM->cfg_bit.sclksel = value;
  //DUMMY_NOP();
}

5.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437_board.h中 添加 #define AT_START_F437_V1

6.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437.h中相关宏为修改AT32F437ZMT7,相关代码如下

/**
  * tip: to avoid modifying this file each time you need to switch between these
  *      devices, you can define the device in your toolchain compiler preprocessor.
  */

#define AT32F437xx

/**
  * define with package
  */


#define AT32F437Zx

/**
  * define with memory density
  */

#define AT32F437xM

7.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437_conf.h中添加

alt text

#if !defined  TRUE
#define TRUE  AT_TRUE
#endif
#if !defined  FALSE
#define FALSE AT_FALSE
#endif

8.所有的at32437驱动文件都要包含头文件#include "los_compiler.h",为了解决UNUSED没有定义的问题

alt text

5.内核子系统适配

在vendor\artery\AT-START-F437\config.json添加内核子系统及相关配置,如下所示:

    "subsystems": [ 
        {
            "subsystem": "kernel",
            "components": [
                {
                    "component": "liteos_m"
                }
            ]
        }
    ],

4.添加at32f437驱动库

前面提到首先获取AT32F437的底层驱动源代码(Firmware Library),我们需要从官方托管的gitee仓库AT32F435_437_Firmware_Library获取。

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

1.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437.h中typedef int32_t INT32;typedef uint32_t UINT32;注释掉


//typedef int32_t  INT32;
typedef int16_t  INT16;
typedef int8_t   INT8;
//typedef uint32_t UINT32;

2.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437.h中,FALSE修改为AT_FALSE,TRUE修改为AT_TRUE,避开宏的重定义

typedef enum {AT_FALSE = 0, AT_TRUE = !AT_FALSE} confirm_state; 

3.device/soc/artery/at32f4xx/libraries/drivers/inc/at32f435_437_def.h中注释掉


//#define UNUSED(x)                        (void)x /* to avoid gcc/g++ warnings */

4.device/soc/artery/at32f4xx/libraries/drivers/src/at32f435_437_crm.c中注释DUMMY_NOP();这个函数

/**
  * @brief  select system clock source
  * @param  value
  *         this parameter can be one of the following values:
  *         - CRM_SCLK_HICK
  *         - CRM_SCLK_HEXT
  *         - CRM_SCLK_PLL
  * @retval none
  */
void crm_sysclk_switch(crm_sclk_type value)
{
  CRM->cfg_bit.sclksel = value;
  //DUMMY_NOP();
}

5.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437_board.h中 添加 #define AT_START_F437_V1

6.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437.h中相关宏为修改AT32F437ZMT7,相关代码如下

/**
  * tip: to avoid modifying this file each time you need to switch between these
  *      devices, you can define the device in your toolchain compiler preprocessor.
  */

#define AT32F437xx

/**
  * define with package
  */


#define AT32F437Zx

/**
  * define with memory density
  */

#define AT32F437xM

7.device/soc/artery/at32f4xx/liteos_m/include/at32f435_437_conf.h中添加

image.png

#if !defined  TRUE
#define TRUE  AT_TRUE
#endif
#if !defined  FALSE
#define FALSE AT_FALSE
#endif

8.所有的at32437驱动文件都要包含头文件#include "los_compiler.h",为了解决UNUSED没有定义的问题

image.png

5.内核子系统适配

在vendor\artery\AT-START-F437\config.json添加内核子系统及相关配置,如下所示:

    "subsystems": [ 
        {
            "subsystem": "kernel",
            "components": [
                {
                    "component": "liteos_m"
                }
            ]
        }
    ],

http://www.kler.cn/news/311484.html

相关文章:

  • SSH 服务器,防止链接断开,训练终止操作————screen
  • Elasticsearch 分片迁移与移除集群节点操作
  • POI生成Excel文件增加数据验证(下拉序列)
  • 获取多媒体相册的内容
  • WEB攻防-JavaWweb项目JWT身份攻击组件安全访问控制
  • Qt 菜单栏、工具栏、状态栏、标签、铆接部件(浮动窗口) 设置窗口核心部件(文本编辑控件)的基本使用
  • 代理模式-动态代理
  • Vue3:defineProps接收父组件传递的数据
  • java八股文之Redis
  • Git 分支规范
  • 【Redis】个人笔记
  • java项目之在线考试与学习交流网页平台源码(springboot)
  • Ruby-SAML CVE-2024-45409 漏洞解决方案
  • 【快速笔记】freeRTOS
  • Loki 分布式日志中心服务
  • Hive基本原理与数据开发
  • 唯徳知识产权管理系统 UploadFileWordTemplate 任意文件读取
  • 开源项目 GAN 漫画风格化 UGATIT
  • 如何借助项目管理系统实现审批流程的自动化与标准化?
  • 无人机 PX4 飞控 | EKF 使用传感器汇总与添加传感器方法
  • Photoshop使用方法大全
  • Zookeeper工作机制和特点
  • 软件验收测试报告模版分享?专业软件验收测试公司推荐
  • 如何安装和注册 GitLab Runner
  • 【STM32系统】基于STM32设计的SD卡数据读取与上位机显示系统(SDIO接口驱动、雷龙SD卡)——文末资料下载
  • 单片机,传感器等低功耗管理
  • 模块化编程:构建灵活与高效的系统
  • python简介
  • L67 【哈工大_操作系统】操作系统历史 学习任务
  • spring MVC基础注解汇总