OpenHarmony常用库及其API/宏/枚举
OpenHarmony常用库及其API/宏/枚举
1. #include "ohos_init.h":
包含OpenHarmony 应用入口宏 APP_FEATURE_INIT
APP_FEATURE_INIT(func) :应用入口注册宏,系统上电自动执行 func 函数
2. #include "cmsis_os2.h":
CMSIS-RTOS2 实时操作系统接口(线程/队列/定时器)
内置类型、宏、枚举、API
1. 类型定义
osMessageQueueId_t :消息队列句柄
osTimerId_t :软件定时器句柄
osStatus_t :系统函数返回状态类型
osThreadAttr_t :线程属性结构体
成员: stack_size 栈大小、 priority 线程优先级、 name 线程名
2. 枚举/宏
osOK :函数执行成功
osTimerPeriodic :周期性定时器(循环触发)
3. 用到API
osThreadNew() :创建线程
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);
1. func:线程入口函数,格式 void func(void *arg)
2. argument:传给线程的参数,无则填 NULL
3. attr:线程属性(栈大小、优先级、名称等), NULL 用默认属性
osMessageQueueNew() :创建消息队列
osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
1. msg_count :队列最大消息数
2. msg_size :单条消息字节大小
3. attr :队列属性,传 NULL 使用默认配置
osMessageQueuePut() :向消息队列写入数据
osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);
1. mq_id :消息队列句柄
2. msg_ptr :待发送数据指针
3. msg_prio :消息优先级
4. timeout :阻塞等待超时时间
osMessageQueueGet() :从消息队列读取数据
osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);
1. mq_id :消息队列句柄
2. msg_ptr :接收数据存储指针
3. msg_prio :接收消息优先级存储指针
4. timeout :阻塞等待超时时间
osTimerNew() :创建软件定时器
osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);
1. func :定时器回调函数
2. type :定时器类型(单次/周期)
3. argument :传给回调函数的参数,无则填 NULL
4. attr :定时器属性, NULL 使用默认属性
osTimerStart() :启动定时器
osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);
1. timer_id :定时器句柄
2. ticks :定时时长(系统节拍数)
3. #include "hi_errno.h"
Hi3861 海思芯片错误码头文件
作用:定义芯片外设、系统接口的错误返回值,本代码仅引入未直接使用。
4. #include "hi_io.h"
Hi3861 引脚复用 & 上下拉配置
内置宏/枚举
1. 引脚定义
HI_IO_NAME_GPIO_0 ~ HI_IO_NAME_GPIO_14 :GPIO引脚编号
2. 引脚功能宏
HI_IO_FUNC_GPIO_11_UART2_TXD :GPIO11 复用为 UART2 发送脚
HI_IO_FUNC_GPIO_12_UART2_RXD :GPIO12 复用为 UART2 接收脚
HI_IO_FUNC_GPIO_x_GPIO :引脚复用为普通GPIO功能
HI_IO_FUNC_GPIO_6_PWM3_OUT :GPIO6 复用为 PWM3 输出
3. 上下拉配置枚举
HI_IO_PULL_UP :引脚上拉模式
4. 用到API:
hi_io_set_func(引脚, 功能) :设置引脚复用功能
hi_u32 hi_io_set_func(hi_io_name id, hi_io_func func);
1. id :目标引脚编号
2. func :引脚复用功能
hi_io_set_pull(引脚, 上下拉模式) :设置引脚上下拉
hi_u32 hi_io_set_pull(hi_io_name id, hi_io_pull pull);
1. id :目标引脚编号
2. pull :引脚上下拉配置模式
5. #include "hi_gpio.h"
Hi3861 GPIO 驱动(输入/输出/中断)
内置宏/枚举(代码用到)
1. GPIO编号
HI_GPIO_IDX_0 ~ HI_GPIO_IDX_14 :GPIO索引
2. 方向枚举
HI_GPIO_DIR_OUT :GPIO 输出模式
HI_GPIO_DIR_IN :GPIO 输入模式
3. 电平枚举
HI_GPIO_VALUE0 :低电平
HI_GPIO_VALUE1 :高电平
4. 中断相关宏
HI_INT_TYPE_EDGE :边沿触发中断
HI_GPIO_EDGE_FALL_LEVEL_LOW :下降沿触发中断
用到API:
hi_gpio_init() :初始化GPIO模块
hi_u32 hi_gpio_init(hi_gpio_idx id);
1. id :待初始化的GPIO索引
hi_gpio_set_dir() :设置GPIO输入/输出方向
hi_u32 hi_gpio_set_dir(hi_gpio_idx id, hi_gpio_dir dir);
1. id :GPIO索引
2. dir :GPIO输入/输出方向
hi_gpio_set_ouput_val() :设置GPIO输出电平
hi_u32 hi_gpio_set_ouput_val(hi_gpio_idx id, hi_gpio_value val);
1. id :GPIO索引
2. val :待设置的输出电平
hi_gpio_register_isr_function() :注册GPIO外部中断回调函数
hi_u32 hi_gpio_register_isr_function(hi_gpio_idx id, hi_gpio_isr_func func);
1. id :GPIO索引
2. func :中断回调函数
6. #include "hi_uart.h"
Hi3861 串口 UART 驱动
内置结构体 & 宏
1. 配置结构体
hi_uart_attribute :串口参数结构体
baud_rate :波特率
data_bits :数据位
stop_bits :停止位
parity :校验位
2. 串口号
HI_UART_IDX_2 :UART2 外设
3. 空指针宏
HI_NULL :等价于空指针 NULL
4. 用到API
hi_uart_init() :初始化串口hi_u32 hi_uart_init(hi_uart_idx id, const hi_uart_attribute *attr);
1. id :串口号
2. attr :串口配置参数结构体
hi_uart_write() :串口发送数据
hi_s32 hi_uart_write(hi_uart_idx id, const hi_u8 *data, hi_u32 len);
1. id :串口号
2. data :待发送数据缓冲区指针
3. len :发送数据长度
hi_uart_read() :串口接收数据
hi_s32 hi_uart_read(hi_uart_idx id, hi_u8 *data, hi_u32 len);
1. id :串口号
2. data :接收数据存储缓冲区指针
3. len :期望读取数据长度
7. #include "hi_pwm.h"
Hi3861 PWM 脉冲宽度调制驱动
内置宏
HI_PWM_PORT_PWM3 :PWM3 外设端口
用到API
hi_pwm_init() :初始化PWM模块
hi_u32 hi_pwm_init(hi_pwm_port port, const hi_pwm_attr *attr);
1. port :PWM端口号
2. attr :PWM配置参数结构体
hi_pwm_start() :启动PWM输出
hi_u32 hi_pwm_start(hi_pwm_port port);
1. port :待启动的PWM端口号
更多推荐



所有评论(0)