3_4_AliOS Things 基础 AOS API 及 HAL API 介绍|学习笔记

简介: 快速学习3_4_AliOS Things 基础 AOS API 及 HAL API 介绍。

开发者学堂课程【HaaS 物联网应用开发课程3_4_AliOS Things 基础 AOS API 及 HAL API 介绍】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/801/detail/13829


3_4_AliOS Things 基础 AOS API 及 HAL API 介绍

 

内容介绍:

一、AOS & HAL API 介绍

二、系统定时器

三、演示环节

 

先对 AOS & HAL API 进行简单介绍,然后以定时器模块为例介绍 AOStimerAPI 的使用方法,之后再以系统中断和GPIO 操作为例介绍HAL API 用法。

 

一、AOS&HAL API 介绍

AOS API

在前面系统架构图中提到 AOS API 是 AliOS 操作系统,对系统提供的基础功能的集合。如果要使用 AOS API,需要包含声明它们的头文件。

AOS API 头文件位置
include/aos/kernel.h

AOS API 类型

按照功能对 AOS API 进行划分,分为:

>任务管理(task、workqueue)   如果使用任务管理的 API,可以去 kernel.h 中搜索 task,可以找到所有任务管理的API 及其说明。也可以通过阿里云官方帮助文档来查看 API 的使用方法以及案例。

>锁与信号量(mutex、semaphore等)

>消息队列(msg queue)

>定时器(timer)

>内存管理(malloc、free等)

>时间管理(日历,系统时间等)

>系统管理(重启等)

HAL API 介绍

HAL 是 Hardware Abstract Layer 缩写,中文是硬件抽象层

HAL 的概念最早是由微软在 Windows 系统上面提出来的。之后的操作系统不管是 Linux、Android 都有自己的 HAL层定义。HAL 层的概念也比较适合于嵌入式操作系统中,但并非所有的主流嵌入式系统都对 HAL 层进行了定义。

HAL 位于操作系统内核与硬件电路之间的接口层,其目的在于将硬件功能进行抽象并形成统一的 API 供操作系统内核或应用程序调用。通过 HAL 层对特定平台的硬件接口特性及细节进行隐藏。所以基于 HAL API 实现的功能(组件、内核、应用等)跟特定硬件平台解耦合,方便于功能组件在不同平台之间的移植。

image.png

图上代表在硬件平台 A 上可以使用的内核/组件、应用可以直接放置在硬件平台 B 上直接执行,不需要进行修改。

AliOS Things HAL API 设计思想

1.层次化思想

通过 HAL API 层的定义,将操作系统分为“硬件无关层”和“硬件相关层”两大层,上层在进行硬件操作时就无需关注当前硬件平台的具体细节,从而降低系统复杂度,并简化组件及应用的开发,出现问题之后也可以以 HAL API 为界限进行问题的界定,方便排查问题。

2.模块化思想

每个硬件模块均有一组 HAL API 对其能力进行定义。模块和模块之间相互隔离,没有耦合性。应用程序根据自己的功能需求设定对 HAL 实现层模块的依赖关系这样就可以做到 HAL 及其应用可伸缩、可裁剪的灵活性。

3.对象化思想

模块化思想也是对象化思想的一部分。除此之外,针对有多个相同功能模块的硬件平台的情况,数据结构中有用id/config 等结构对相同类型的硬件模块进行编号,并保存其独有的参数,不管类型是否相同,每个模块都有自己独有的数据结构对其私有参数进行描述,以提高软件的扩展性及重用性。

AliOS Things 对这十二种进行了抽象,包括 GPIO、UART、SPI、I2C、FLASH、RTC、PWM、TIMER、ADC、DAC、WTD、RNG

HAL API头文件:include/aos/hal/

下图是比较形象的表达,上方应用层在呼叫,

hal_gpio_output_high(&LED)代码可以在很多平台上,无需修改通过很少量的配置可以直接运行。

image.png


二、系统定时器

硬件定时器:硬件定时器会产生固定的时钟节拍,作为操作系统的时钟基准(Tick ),其数量是受硬件数量限制的。

软件定时器:通过软件程序模拟出来的定时器,软件定时器会消耗一定的 CPU 和内存资源。在精确度上不如硬件计时器,优点在于数量可以很多。本节主要讲述软件计时器的概念,每个软件计时器都保存着自己的到期时间以及时间超时。软件计时器管理模块会扫描所有的软件定时器设定,每个软件计时器到期之后会呼叫其预定的超时时间处理函数。

定时器安装是否重复可以分为一次性定时器和周期性定时器两种

AliOS Things 操作系统对用户提供定时器接口的是 Timer 模块。同样可以设定 Timer 是一次性的还是周期性的。是需要 Timer 到期自动执行还是需要手动启动 Timer。

1. 一次性 Timer

2. 周期性 Timer

3. 自动/手动运行 Timer

API名称

功能

aos_timer_new

创建一个软件定时器,创建后自动运行

aos_timer_new_ext

创建一个软件定时器,可通过入参决定创建后是否自动运行。

aos_timer_start

启动软件定时器

aos_timer_stop

停止软件定时器

aos_timer_change

修改软件定时器的定时参数

aos_timer_free

删除软件定时器

创建单次定时任务的代码:

#include "aos/ kernel.h"

aos_timer_t  g_single_timer;

static void g_single_timer_handler(void *arg1,voids arg2)  //超时处理函数

{

printf("enter %s\r\n",__func__);

return;

}

/**

* set up a timer with timeout value of period and set timer 's handler to g_single_timer_handler

* @param period - timer timeout value,in unit of ms

*

* @return 0 for success; negative no. for failure

*/

int single_timer_example(int period){

int ret = -1;

if (period <= 0) {

printf("invalid period:%d\r\n", period);

return -1;

}

/*setup a timer with timeout value set to period ms and disable autoload function */

ret = aos_timer_new_ext(&g_single_timer,g_single_timer_handler,NULL,period,0,0);

//第一个0-单次定时   第二个0-不自动执行

if (ret != 0) {

printf("timer create failed\r\n");

return 2;

}

/* start the timer */

aos_timer_start(&g_single_timer);   //启动定时器

/* sleep for (period * 2) ms */

usleep (period * 2 * 1000);

/* stop the timer again */

aos_timer_stop(&g_single_timer);  //停止定时器

/*

* setup timer’s timeout value to 1000 ms

* should change timer's settings when timer is stopped

*/

aos_timer_change(&g_single_timer,1000);  //修改定时器超时时间

/* satart the timer */

aos_timer_start(&g_single_timer);  //重新启动定时器

/* sleep for (period * 2) ms */

usleep (period * 2 * 1000) ;   //等待2秒

/* stop the timer */

aos_timer_stop(&g_single_timer);   //停止定时器

/* free the timer's resource */

aos_timer_free(&g_single_timer);   //删除定时器

return 0;

}

创建自动加载周期性定时器

aos_timer_t  g_repeat_autoload_timer;

static void repeat_autoload_repeat_non_autoload_timer_handler(void *arg1,void* arg2)

{

printf("enter %s\r\n".,__func_);

return;

}

/**

* set up an auto reload timer with timeout value of period and set timer's handler to repeat autoload_repeat_non autoload_timer handler

* param period - timer timeout value, in unit of ms

*

* return 0 for success; negative no. for failure

*/

int g_repeat_autoload_timer_example(int period){

int ret = -1;

if (period <= 0){

printf("invalid period:%d\r\n", period);

return -1;

}

/*setup a timer with timeout value set to period ms and enable autoload function */

ret = aos_timer_new(&g_repeat_autoload_timer,repeat_autoload_repeat_non_autoload_timer_handler,NULL,period,1);   //此处1为周期性加载

if (ret != 0) {

printf("timer create failed\r\n");

return 2;

}

/* start the timer */

aos_timer_start(&g_repeat_autoload_timer) ;

/* sleep for(period * 2)ms */

usleep (period * 2 * 1000);

/* stop the timer again */

aos_timer_stop(&g_repeat_autoload_timer);

/* free the timer's resource*/

aos_timer_free(&g_repeat_autoload_timer);

return 0;

}

添加定时器测试CLI命令

#include <aos/cli.h>

static void single_timer(char *buf, int len,int argc,char **argv)

{

single_timer_example(1000);   //single_timer的处理函数里呼叫single_timer_example,传入1000ms

return;

}

static void repeat_timer(char *buf, int len, int argc,char **argv)

{

repeat_timer_example(2000);  //repeat_timer呼叫repeat_timer_example,传入2000ms

return;

}

static void autoload_repeat_timer(char *buf,int len,int argc,char **argv)

{

g_repeat_autoload_timer_example(3000) ;

return;

}

struct cli_command timer_cli_cmds [] = {   //这组测试包括三个命令

{"stimer" ,"single timer test" , single_timer},

{"rtimer" ,"non-autoload repeat timer test" , repeat_timer},  //不自动加载的周期性Timer

{"artimer","autoload repeat timer test", autoload_repeat_timer},  //自动加载的周期性Timer

 

};

static int timer_test_cmd_init(void){

return aos_cli_register_commands(&timer_cli_cmds [0],   //呼叫aos_cli_register_commands把timer_cli_cmds这组定时器的测试指令添加到command中

sizeof(timer_cli_cmds)/sizeof(timer_cli_cmds[0]));

}

 

三、演示环节

以之前创建的 new_comp 组件为例,将 Timer 的实现都放在 new component 组件中,输入

vim components/peripherals/new_comp/src/

将所有的 timer_test 函数都放在 timer_test.c 中,输入 vim components/peripherals/new_comp/src/timer_test.c

入口函数就是 int new_comp_timer_test_cmd_init(void){},它里面就是将 stimer、rtimer、artimer 这一组测试指令添加到 cli 组件中

再来查看 single_timer,

single_timer 中呼叫 single_timer_example

repeat_timer 中呼叫 repeat_timer_example

再将该文件添加到 makefile 中,

输入 vim components/peripherals/new_comp/aos.mk

进入后将./src/timer_test.c 添加到 new_comp 的 makefile 中

退出后输入

vim components/peripherals/new_comp/src/new_comp.c

进入后在 new_comp 组件初始化的地方呼叫

new_comp_timer_test_cmd_init()

退出后输入

vim application/example/helloworld_demo/appdemo.c

呼叫函数 new_comp_init()

退出后进行编译,先来进行清理输入 aos make disclean

再输入 aos make helloworld_demo@haaseduk1 -c config

再执行 aos make

编译完成后执行 aos upload

烧录完成后打开一个新的串口,打开 picocom 查看串口输出日志

依次执行 stimer、rtimer、artimer 三条指令,输入#stimer,结果显示1秒后超时处理了三次,执行#rtimer,结果显示2秒后进入超时处理,输入#artimer,结果显示3秒超时后进入了 autoload_repeat。

相关文章
|
27天前
|
人工智能 监控 安全
F5社区学习笔记:API和AI如何改变应用安全?
F5社区学习笔记:API和AI如何改变应用安全?
27 1
|
22天前
|
jenkins API 持续交付
jenkins学习笔记之十五:SonarSQube API使用
jenkins学习笔记之十五:SonarSQube API使用
|
4月前
|
XML API 数据格式
【Qt 学习笔记】QWidget的enable属性 | API的介绍
【Qt 学习笔记】QWidget的enable属性 | API的介绍
119 0
|
前端开发 API
前端学习笔记202305学习笔记第二十二天-学生列表api封装1
前端学习笔记202305学习笔记第二十二天-学生列表api封装1
40 0
|
API
java202304java学习笔记第六十六天-ssm-mybatis-相应api之1
java202304java学习笔记第六十六天-ssm-mybatis-相应api之1
54 0
|
前端开发 API
前端学习笔记202305学习笔记第二十二天-学生列表api封装2
前端学习笔记202305学习笔记第二十二天-学生列表api封装2
49 1
|
前端开发 API
前端学习笔记202305学习笔记第二十二天-新增修改api的封装2
前端学习笔记202305学习笔记第二十二天-新增修改api的封装2
57 0
前端学习笔记202305学习笔记第二十二天-新增修改api的封装2
|
前端开发 API
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
67 0
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
48 0
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
43 0