1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include "aos/hal/timer.h"
6 #define IGNORE_HAL_TIMER_RAW_API_CHECK
7 #include "hal_timer_raw.h"
8 #define BES_HAL_DEBUG 0
9 #include "app_hal.h"
10 #undef hal_timer_start
11 #undef hal_timer_stop
12 #include "hal_trace.h"
13
14 static timer_dev_t _tim;
15
oneshot_timer_handler(uint32_t elapsed)16 static void oneshot_timer_handler(uint32_t elapsed)
17 {
18 TRACE("%s: elapsed=%d", __FUNCTION__, elapsed);
19 _tim.config.cb(_tim.config.arg);
20 }
21
periodic_timer_handler(uint32_t elapsed)22 static void periodic_timer_handler(uint32_t elapsed)
23 {
24 TRACE("%s: elapsed=%d", __FUNCTION__, elapsed);
25 _tim.config.cb(_tim.config.arg);
26 }
27
_hal_timer_stop(void)28 static void _hal_timer_stop(void)
29 {
30 hal_timer_stop_nickname();
31 }
32
_hal_timer_para_chg(timer_dev_t * tim,timer_config_t para)33 static int32_t _hal_timer_para_chg(timer_dev_t *tim, timer_config_t para)
34 {
35 int32_t ret = 0;
36 enum HAL_TIMER_TYPE_T type = HAL_TIMER_TYPE_ONESHOT;
37 HAL_TIMER_IRQ_HANDLER_T handler = oneshot_timer_handler;
38 ENTER_FUNCTION();
39
40 memcpy(&_tim, tim, sizeof(timer_dev_t));
41 if (_tim.config.reload_mode == TIMER_RELOAD_AUTO) {
42 type = HAL_TIMER_TYPE_PERIODIC;
43 handler = periodic_timer_handler;
44 }
45 hal_timer_setup(type, handler);
46
47 LEAVE_FUNCTION();
48 return 0;
49 }
50
51
52 /**
53 * init a hardware timer
54 *
55 * @param[in] tim timer device
56 *
57 * @return 0 : on success, EIO : if an error occurred with any step
58 */
hal_timer_init(timer_dev_t * tim)59 int32_t hal_timer_init(timer_dev_t *tim)
60 {
61 int32_t ret = 0;
62 ENTER_FUNCTION();
63 ret = _hal_timer_para_chg(tim, tim->config);
64 LEAVE_FUNCTION();
65 return 0;
66 }
67
68 /**
69 * start a hardware timer
70 *
71 * @param[in] tim timer device
72 *
73 * @return 0 : on success, EIO : if an error occurred with any step
74 */
hal_timer_start(timer_dev_t * tim)75 int32_t hal_timer_start(timer_dev_t *tim)
76 {
77 int32_t ret = 0;
78 ENTER_FUNCTION();
79 hal_timer_start_nickname(US_TO_TICKS(tim->config.period));
80 LEAVE_FUNCTION();
81 return 0;
82 }
83
84 /**
85 * stop a hardware timer
86 *
87 * @param[in] tim timer device
88 *
89 * @return none
90 */
hal_timer_stop(timer_dev_t * tim)91 void hal_timer_stop(timer_dev_t *tim)
92 {
93 ENTER_FUNCTION();
94 _hal_timer_stop();
95 LEAVE_FUNCTION();
96 }
97
98 /**
99 * change the config of a hardware timer
100 *
101 * @param[in] tim timer device
102 * @param[in] para timer config
103 *
104 * @return 0 : on success, EIO : if an error occurred with any step
105 */
hal_timer_para_chg(timer_dev_t * tim,timer_config_t para)106 int32_t hal_timer_para_chg(timer_dev_t *tim, timer_config_t para)
107 {
108 int32_t ret = 0;
109 ENTER_FUNCTION();
110 ret = _hal_timer_para_chg(tim, para);
111 LEAVE_FUNCTION();
112 return 0;
113 }
114
115 /**
116 * De-initialises an TIMER interface, Turns off an TIMER hardware interface
117 *
118 * @param[in] tim timer device
119 *
120 * @return 0 : on success, EIO : if an error occurred with any step
121 */
hal_timer_finalize(timer_dev_t * tim)122 int32_t hal_timer_finalize(timer_dev_t *tim)
123 {
124 int32_t ret = 0;
125 ENTER_FUNCTION();
126 _hal_timer_stop();
127 LEAVE_FUNCTION();
128 return ret;
129 }
130
_hal_timer_test_cb(void * arg)131 static void _hal_timer_test_cb(void *arg)
132 {
133 TRACEME("%s arg=0x%X\n", arg);
134 }
135
_hal_timer_test()136 void _hal_timer_test()
137 {
138 uint32_t cur_ticks = hal_sys_timer_get();
139 uint32_t st;
140 timer_dev_t tim = {0, {1000, TIMER_RELOAD_MANU, _hal_timer_test_cb, 100}, NULL};
141
142 hal_timer_init(&tim);
143 hal_timer_start(&tim);
144 krhino_task_sleep(3000);
145 tim.config.period = 20;
146 hal_timer_start(&tim);
147 TRACEME("TIMER: oneshot start 20\n");
148 hal_timer_stop(&tim);
149 hal_timer_finalize(&tim);
150
151 tim.config.reload_mode = TIMER_RELOAD_AUTO;
152 hal_timer_init(&tim);
153 hal_timer_start(&tim);
154 krhino_task_sleep(3000);
155 tim.config.period = 20;
156 hal_timer_start(&tim);
157 krhino_task_sleep(30000);
158 TRACEME("TIMER: oneshot start 20\n");
159 hal_timer_stop(&tim);
160 hal_timer_finalize(&tim);
161 }
162
163
164