README.md
1# 润和星闪派物联网开发套件--软件定时器(Timer)
2
3
4
5[润和星闪派物联网开发套件](https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch) 基于海思WS63E解决方案的一套软硬件组合的综合性开发套件。
6
7
8
9## 一、Timer API
10
11| API名称 | 说明 |
12| ---------------- | ------------------------ |
13| osTimerNew | 创建和初始化定时器 |
14| osTimerGetName | 获取指定的定时器名字 |
15| osTimerStart | 启动或者重启指定的定时器 |
16| osTimerStop | 停止指定的定时器 |
17| osTimerIsRunning | 检查一个定时器是否在运行 |
18| osTimerDelete | 删除定时器 |
19
20### osTimerNew()
21
22```c
23osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
24```
25
26**参数:**
27
28| 名字 | 描述 |
29| :------- | :----------------------------------------------------------- |
30| func | 定时器回调函数. |
31| type | 定时器类型,osTimerOnce表示单次定时器,ostimer周期表示周期性定时器. |
32| argument | 定时器回调函数的参数 |
33| attr | 定时器属性 |
34
35## 二、代码分析
36
37定时器的回调函数
38
39```c
40void cb_timeout_periodic(void *arg) {
41 (void)arg;
42 times++;
43}
44```
45
46使用osTimerNew创建一个100个时钟周期调用一次回调函数cb_timeout_periodic定时器,每隔100个时钟周期检查一下全局变量times是否小于3,若不小于3则停止时钟周期
47
48```c
49void timer_periodic(void) {
50 osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
51 if (periodic_tid == NULL) {
52 printf("[Timer Test] osTimerNew(periodic timer) failed.\r\n");
53 return;
54 } else {
55 printf("[Timer Test] osTimerNew(periodic timer) success, tid: %p.\r\n",periodic_tid);
56 }
57 osStatus_t status = osTimerStart(periodic_tid, 100);
58 if (status != osOK) {
59 printf("[Timer Test] osTimerStart(periodic timer) failed.\r\n");
60 return;
61 } else {
62 printf("[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.\r\n");
63 }
64
65 while(times < 3) {
66 printf("[Timer Test] times:%d.\r\n",times);
67 osDelay(100);
68 }
69
70 status = osTimerStop(periodic_tid);
71 printf("[Timer Test] stop periodic timer, status :%d.\r\n", status);
72 status = osTimerDelete(periodic_tid);
73 printf("[Timer Test] kill periodic timer, status :%d.\r\n", status);
74}
75
76```
77
78
79
80## 三、如何编译
81
821. 将01_timer目录复制到openharmony源码的`applications\sample\wifi-iot\app`目录下,
832. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
84
85```
86 features = [
87 ...
88 "01_timer:timer_demo",
89 ...
90 ]
91```
923. 在`device\soc\hisilicon\ws63v100\sdk\build\config\target_config\ws63\config.py`文件中,找到`'ws63-liteos-app'`部分,在其`'ram_component'`中,添加以下代码:
93```
94"timer_demo"
95```
96
974. 在`device\soc\hisilicon\ws63v100\sdk\libs_url\ws63\cmake\ohos.cmake`文件中,找到`"ws63-liteos-app"`部分,在其`set(COMPONENT_LIST`部分,添加以下代码:
98```
99"timer_demo"
100```
1015. 在openharmony sdk根目录目录执行:`rm -rf out && hb set -p nearlink_dk_3863 && hb build -f`
102
103## 四、运行结果
104
105设置串口工具波特率为115200,复位开发板,查看打印结果
106
107```
108[Timer Test] osTimerNew(periodic timer) success, tid: 0xe9b4c.
109[Timer Test] osTimerStart(periodic timer) success, wait a while and stop.
110[Timer Test] times:0.
111[Timer Test] times:1.
112[Timer Test] times:2.
113[Timer Test] stop periodic timer, status :0.
114[Timer Test] kill periodic timer, status :0.
115```
116
117### 【套件支持】
118
119##### 1. 套件购买 https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch
120
121##### 2. 技术资料
122
123- Gitee码云网站(使用说明书、规格说明书、OpenHarmony开发案例等) **https://gitee.com/hihopeorg_group/near-link**
124- fbb_ws63代码仓(SDK包、技术文档下载)**https://gitee.com/HiSpark/fbb_ws63**
125
126##### 3. 互动交流
127- 海思社区星闪专区-论坛 **https://developer.hisilicon.com/forum/0133146886267870001**
128