1  /** mbed Microcontroller Library
2   ******************************************************************************
3   * @file    timer_api.c
4   * @author
5   * @version V1.0.0
6   * @date    2016-08-01
7   * @brief   This file provides mbed API for gtimer.
8   ******************************************************************************
9   * @attention
10   *
11   * This module is a confidential and proprietary property of RealTek and
12   * possession or use of this module requires written permission of RealTek.
13   *
14   * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
15   ******************************************************************************
16   */
17 
18 #include "objects.h"
19 #include "timer_api.h"
20 
21 extern void aos_interrupt_enter();
22 extern void aos_interrupt_leave();
23 
24 /** @defgroup AmebaD_Mbed_API
25   * @{
26   */
27 
28 /** @defgroup MBED_TIMER
29  *  @brief      MBED_TIMER driver modules.
30  *  @{
31  */
32 
33 /** @defgroup MBED_TIMER_Exported_Functions MBED_TIMER Exported Functions
34   * @{
35   */
36 
37 /**
38   * @brief  gtimer interrupt handler function.
39   * @param  data: timer IRQ callback parameter
40   * @retval   none
41   */
gtimer_timeout_handler(uint32_t data)42 static void gtimer_timeout_handler (uint32_t data)
43 {
44 	gtimer_t *obj = (gtimer_t *)data;
45 	uint32_t tid = obj->timer_id;
46 	gtimer_irq_handler handler;
47 
48   aos_interrupt_enter();
49 
50 	RTIM_INTClear(TIMx[tid]);
51 
52 	if (obj->handler != NULL) {
53 		handler = (gtimer_irq_handler)obj->handler;
54 		handler(obj->hid);
55 	}
56 
57 	if (!obj->is_periodcal) {
58 		gtimer_stop(obj);
59 	}
60 
61   aos_interrupt_leave();
62 
63 }
64 
65 /**
66   * @brief  Initializes the timer device, include timer registers and interrupt.
67   * @param  obj: timer object define in application software.
68   * @param  tid: general timer ID, which can be one of the following parameters:
69   *              @arg TIMER0
70   *              @arg TIMER1
71   *              @arg TIMER2
72   *              @arg TIMER3
73   * @note KM4 TIMER0/1/2/3 are recommended.
74   * @retval none
75   */
gtimer_init(gtimer_t * obj,uint32_t tid)76 void gtimer_init (gtimer_t *obj, uint32_t tid)
77 {
78 	RTIM_TimeBaseInitTypeDef TIM_InitStruct;
79 
80 	assert_param(tid < GTIMER_MAX);
81 
82 	obj->timer_id = tid;
83 
84 	RTIM_TimeBaseStructInit(&TIM_InitStruct);
85 	TIM_InitStruct.TIM_Idx = (u8)tid;
86 
87 	TIM_InitStruct.TIM_UpdateEvent = ENABLE; /* UEV enable */
88 	TIM_InitStruct.TIM_UpdateSource = TIM_UpdateSource_Overflow;
89 	TIM_InitStruct.TIM_ARRProtection = ENABLE;
90 
91 	RTIM_TimeBaseInit(TIMx[tid], &TIM_InitStruct, TIMx_irq[tid], (IRQ_FUN) gtimer_timeout_handler, (u32)obj);
92 }
93 
94 /**
95   * @brief  Deinitializes the timer, including clock/function/timer registers.
96   * @param  obj: gtimer object defined in application software.
97   * @retval none
98   */
gtimer_deinit(gtimer_t * obj)99 void gtimer_deinit (gtimer_t *obj)
100 {
101 	uint32_t tid = obj->timer_id;
102 
103 	assert_param(tid < GTIMER_MAX);
104 
105 	RTIM_DeInit(TIMx[tid]);
106 }
107 
108 /**
109   * @brief  Get counter value of the specified timer.
110   * @param  obj: timer object define in application software.
111   * @retval value: counter value
112   */
gtimer_read_tick(gtimer_t * obj)113 uint32_t gtimer_read_tick (gtimer_t *obj)
114 {
115 	uint32_t tid = obj->timer_id;
116 
117 	assert_param(tid < GTIMER_MAX);
118 
119 	return (RTIM_GetCount(TIMx[tid]));
120 }
121 
122 /**
123   * @brief  Read current timer tick in microsecond.
124   * @param  obj: gtimer object defined in application software.
125   * @retval 64 bit tick time
126   */
gtimer_read_us(gtimer_t * obj)127 uint64_t gtimer_read_us (gtimer_t *obj)  //need to be test in IAR(64bit computing)
128 {
129 	assert_param(obj->timer_id < GTIMER_MAX);
130 
131 	uint64_t time_us;
132 	time_us = gtimer_read_tick(obj) *1000000 /32768;
133 
134 	return (time_us);
135 }
136 
137 /**
138   * @brief  Change period of the specified timer.
139   * @param  obj: timer object define in application software.
140   * @param  duration_us: the new period to be set in microseconds.
141   * @retval none
142   */
gtimer_reload(gtimer_t * obj,uint32_t duration_us)143 void gtimer_reload (gtimer_t *obj, uint32_t duration_us)
144 {
145 	uint32_t tid = obj->timer_id;
146 	uint32_t temp = (uint32_t)((float)duration_us  / 1000000 *32768);
147 
148 	assert_param(tid < GTIMER_MAX);
149 
150     	RTIM_ChangePeriodImmediate(TIMx[tid], temp);
151 }
152 
153 /**
154   * @brief  Start the specified timer and enable update interrupt.
155   * @param  obj: timer object define in application software.
156   * @retval none
157   */
gtimer_start(gtimer_t * obj)158 void gtimer_start (gtimer_t *obj)
159 {
160 	uint32_t tid = obj->timer_id;
161 
162 	assert_param(tid < GTIMER_MAX);
163 
164 	RTIM_INTConfig(TIMx[tid], TIM_IT_Update, ENABLE);
165 	RTIM_Cmd(TIMx[tid], ENABLE);
166 }
167 
168 /**
169   * @brief Start the specified timer in one-shot mode with specified period and interrupt handler.
170   * @param  obj: timer object define in application software.
171   * @param  duration_us: the new period to be set in microseconds.
172   * @param  handler: user defined IRQ callback function
173   * @param  hid: user defined IRQ callback parameter
174   * @retval none
175   * @note This function set the timer into one-shot mode which stops after the first time the counter overflows.
176   */
gtimer_start_one_shout(gtimer_t * obj,uint32_t duration_us,void * handler,uint32_t hid)177 void gtimer_start_one_shout (gtimer_t *obj, uint32_t duration_us, void* handler, uint32_t hid)
178 {
179 	assert_param(obj->timer_id < GTIMER_MAX);
180 
181 	obj->is_periodcal = _FALSE;
182 	obj->handler = handler;
183 	obj->hid = hid;
184 
185 	gtimer_reload(obj, duration_us);
186 	gtimer_start(obj);
187 }
188 
189 /**
190   * @brief Start the specified timer in periodical mode with specified period and interrupt handler.
191   * @param  obj: timer object define in application software.
192   * @param  duration_us: the new period to be set in microseconds.
193   * @param  handler: user defined IRQ callback function
194   * @param  hid: user defined IRQ callback parameter
195   * @retval none
196   * @note This functon set the timer into periodical mode which will restart to count from 0 each time the counter overflows.
197   */
gtimer_start_periodical(gtimer_t * obj,uint32_t duration_us,void * handler,uint32_t hid)198 void gtimer_start_periodical (gtimer_t *obj, uint32_t duration_us, void* handler, uint32_t hid)
199 {
200 	assert_param(obj->timer_id < GTIMER_MAX);
201 
202 	obj->is_periodcal = _TRUE;
203 	obj->handler = handler;
204 	obj->hid = hid;
205 
206 	gtimer_reload(obj, duration_us);
207 	gtimer_start(obj);
208 }
209 
210 /**
211   * @brief Disable the specified timer peripheral.
212   * @param  obj: timer object define in application software.
213   * @retval none
214   */
gtimer_stop(gtimer_t * obj)215 void gtimer_stop (gtimer_t *obj)
216 {
217 	uint32_t tid = obj->timer_id;
218 
219 	assert_param(tid < GTIMER_MAX);
220 
221 	RTIM_Cmd(TIMx[tid], DISABLE);
222 }
223 /**
224   * @}
225   */
226 
227 /**
228   * @}
229   */
230 
231 /**
232   * @}
233   */
234