README_zh.md
1# Niobe407开发板OpenHarmony内核编程开发——定时器
2本示例将演示如何在Niobe407开发板上使用cmsis 2.0 接口进行定时器开发
3
4## Timer API分析
5
6## osTimerNew()
7
8```c
9 /// Create and Initialize a timer.
10 /// \param[in] func function pointer to callback function.
11 /// \param[in] type \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
12 /// \param[in] argument argument to the timer callback function.
13 /// \param[in] attr timer attributes; NULL: default values.
14 /// \return timer ID for reference by other functions or NULL in case of error.
15 osTimerId_t osTimerNew (osTimerFunc_t func,osTimerType_t type,void *argument,const osTimerAttr_t *attr)
16```
17**描述:**
18
19函数osTimerNew创建一个一次性或周期性计时器,并将其与一个带参数的回调函数相关联。计时器在osTimerStart启动之前一直处于停止状态。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。
20> **注意** :不能在中断服务调用该函数
21**参数:**
22|名字|描述|
23|:--|:------|
24| func | 函数指针指向回调函数. |
25| type | 定时器类型,osTimerOnce表示单次定时器,ostimer周期表示周期性定时器. |
26| argument |定时器回调函数的参数|
27| attr |计时器属性|
28
29## osTimerStart()
30
31```c
32 /// Start or restart a timer.
33 /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
34 /// \param[in] ticks \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
35 /// \return status code that indicates the execution status of the function.
36 osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);
37
38```
39**描述:**
40
41函数osTimerStart启动或重新启动指定参数timer_id的计时器。参数ticks指定计时器的计数值。
42
43> **注意** :不能在中断服务调用该函数
44
45
46**参数:**
47
48|名字|描述|
49|:--|:------|
50| timer_id | 由osTimerNew获得的计时器ID. |
51| ticks | 时间滴答计时器的值. |
52
53## 软件设计
54
55## 软件设计
56
57**主要代码分析**
58
59在OS_Timer_example函数中,通过osTimerNew()函数创建了回调函数为OS_Timer1_Callback的定时器1,并通过osTimerStart()函数将该定时器设置为100个tick,因为hi3861默认10ms为一个tick,所以100个tick正好为1S钟,1S计时到后会触发OS_Timer1_Callback()函数并打印日志。定时器2也同理为3S触发OS_Timer2_Callback()函数并打印日志.
60
61```c
62 static void OS_Timer_example(void)
63{
64 osTimerId_t timerId1, timerId2;
65 uint32_t delay;
66 osStatus_t status;
67
68 timer1Exec = 1U;
69 /// Create and Initialize a timer.
70 /// \param[in] func function pointer to callback function.
71 /// \param[in] type \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
72 /// \param[in] argument argument to the timer callback function.
73 /// \param[in] attr timer attributes; NULL: default values.
74 /// \return timer ID for reference by other functions or NULL in case of error.
75 timerId1 = osTimerNew((osTimerFunc_t)OS_Timer1_Callback, osTimerPeriodic, &timer1Exec, NULL);
76 if (timerId1 != NULL)
77 {
78 delay = 100U;
79 /// Start or restart a timer.
80 /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
81 /// \param[in] ticks \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
82 /// \return status code that indicates the execution status of the function.
83 //osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);
84 status = osTimerStart(timerId1, delay);
85 if (status != osOK)
86 {
87 printf("Failed to start timer1!\n");
88 }
89 }
90
91 timer2Exec = 100U;
92 timerId2 = osTimerNew((osTimerFunc_t)OS_Timer2_Callback, osTimerPeriodic, &timer2Exec, NULL);
93 if (timerId2 != NULL)
94 {
95 delay = 300U;
96 status = osTimerStart(timerId2, delay);
97 if (status != osOK)
98 {
99 printf("Failed to start timer2!\n");
100 }
101 }
102}
103
104```
105
106## 编译调试
107- 进入//kernel/liteos_m目录, 在menuconfig配置中进入如下选项:
108
109 `(Top) → Platform → Board Selection → select board niobe407 → use talkweb niobe407 application → niobe407 application choose`
110
111- 选择 `003_system_timer`
112
113- 回到sdk根目录,执行`hb build -f`脚本进行编译。
114
115### 运行结果
116
117示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,OS_Timer1_Callback会1S打印一次数据,OS_Timer2_Callback会3S打印一次数据。
118
119```
120This is Niobe407 Timer1_Callback:1!
121This is Niobe407 Timer1_Callback:1!
122This is Niobe407 Timer1_Callback:1!
123This is Niobe407 Timer2_Callback:100!
124This is Niobe407 Timer1_Callback:1!
125This is Niobe407 Timer1_Callback:1!
126This is Niobe407 Timer1_Callback:1!
127This is Niobe407 Timer2_Callback:100!
128This is Niobe407 Timer1_Callback:1!
129This is Niobe407 Timer1_Callback:1!
130```
131