]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC1302_Keil/peripheral_library/basic_timer/btimer_api.c
04e4737871d1fe64bac9761aad9c2c808e760f75
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC1302_Keil / peripheral_library / basic_timer / btimer_api.c
1 /*****************************************************************************\r
2 * © 2015 Microchip Technology Inc. and its subsidiaries.\r
3 * You may use this software and any derivatives exclusively with\r
4 * Microchip products.\r
5 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS".\r
6 * NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,\r
7 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,\r
8 * AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP\r
9 * PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.\r
10 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,\r
11 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND\r
12 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS\r
13 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.\r
14 * TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL\r
15 * CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF\r
16 * FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.\r
17 * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE\r
18 * OF THESE TERMS.\r
19 ******************************************************************************\r
20 \r
21 Version Control Information (Perforce)\r
22 ******************************************************************************\r
23 $Revision: #2 $ \r
24 $DateTime: 2015/11/24 06:27:00 $ \r
25 $Author: amohandas $\r
26 Last Change:    Updated for tabs\r
27 ******************************************************************************/\r
28 /** @file btimer_api.c\r
29 * \brief Basic Timer APIs Source file\r
30 * \author jvasanth\r
31\r
32 * This file implements the Basic Timer API functions  \r
33 ******************************************************************************/\r
34 \r
35 /** @defgroup Basic_Timer\r
36  *  @{\r
37  */\r
38 \r
39 #include "common_lib.h"\r
40 #include "btimer.h"\r
41 #include "..\pcr\pcr.h"\r
42 //#include "..\interrupt\ecia.h"\r
43 \r
44 /** Basic Timer Sleep Registers & Bit Positions */\r
45 static const uint32_t btmr_pcr_id[BTIMER_MAX_INSTANCE] = {\r
46     PCR_BTIMER0,\r
47     PCR_BTIMER1,\r
48     PCR_BTIMER2,\r
49     PCR_BTIMER3,\r
50     PCR_BTIMER4,\r
51     PCR_BTIMER5\r
52 };\r
53 \r
54 #ifdef PLIB_BTIMER_CHECK_ID\r
55 \r
56 /** Local helper that checks if logical Timer ID is valid.  \r
57  * @param btimer_id Basic Timer ID \r
58  * @return uint8_t Non-zero(VALID), 0(Invalid)\r
59  */\r
60 static uint8_t btmr_valid(uint8_t btimer_id)\r
61 {\r
62     if ( btimer_id < (PID_BTIMER_MAX ) ) {\r
63         return true;\r
64     }\r
65     return false;\r
66 }\r
67 \r
68 #else\r
69 \r
70 \r
71 /** This version of tmr_valid skips checking always returning 1.  \r
72  *  Compiler may optimize it out.\r
73  * @param btimer_id Basic Timer ID \r
74  * @return uint8_t 1(VALID) \r
75  */\r
76 static uint8_t btmr_valid(uint8_t btimer_id) { return 1; }\r
77 \r
78 #endif\r
79 \r
80 \r
81 /* ---------------------------------------------------------------------- */\r
82 /*                 Basic Timer Intitialization function                   */\r
83 /* ---------------------------------------------------------------------- */\r
84 \r
85 /** Initialize specified timer\r
86  * @param btimer_id Basic Timer ID\r
87  * @param tmr_cntl Logical flags for Timer Control\r
88  * @param initial_count Initial Count\r
89  * @param preload_count Preload Count\r
90  * @note Performs a soft reset of the timer before configuration \r
91  */\r
92 void btimer_init(uint8_t btimer_id, \r
93                uint16_t tmr_cntl,\r
94                uint16_t prescaler,\r
95                uint32_t initial_count,\r
96                uint32_t preload_count)\r
97 {    \r
98          uint32_t value;    \r
99 \r
100     if (btmr_valid(btimer_id)) {\r
101                         \r
102         btimer_reset(btimer_id); \r
103         \r
104         // Ungate timer clocks and program prescale\r
105         value = ((uint32_t)prescaler << 16) + (BTIMER_CNTL_ENABLE);                       \r
106         p_btimer_ctrl_write(btimer_id, value);\r
107     \r
108         // Program Preload & initial counter value\r
109         p_btimer_preload_set(btimer_id, preload_count);\r
110         p_btimer_count_set(btimer_id, initial_count);                   \r
111         \r
112         // Program control register, interrupt enable, and clear status\r
113         if (tmr_cntl & BTIMER_COUNT_UP) {                                       \r
114             p_btimer_ctrl_counter_dir_set(btimer_id);            \r
115         }\r
116         if (tmr_cntl & BTIMER_AUTO_RESTART) {\r
117             p_btimer_ctrl_auto_restart_set(btimer_id);             \r
118         }        \r
119         if (tmr_cntl & BTIMER_INT_EN) {                                 \r
120             p_btimer_int_enable_set(btimer_id); // enable first\r
121             p_btimer_int_status_clr(btimer_id); // clear status          \r
122         }\r
123     }\r
124 }\r
125 \r
126 /* ---------------------------------------------------------------------- */\r
127 /*          Functions to program and read the Basic Timer Counter         */\r
128 /* ---------------------------------------------------------------------- */\r
129 \r
130 /** Program timer's counter register.\r
131  * @param btimer_id Basic Timer ID\r
132  * @param count new counter value \r
133  * @note Timer hardware may implement a 16-bit or 32-bit \r
134  *       hardware counter. If the timer is 16-bit only the lower\r
135  *       16-bits of the count paramter are used.\r
136  */\r
137 void btimer_count_set(uint8_t btimer_id, uint32_t count)\r
138 {\r
139     if ( btmr_valid(btimer_id) ) {       \r
140                         \r
141         p_btimer_count_set(btimer_id, count);        \r
142     }\r
143 }\r
144 \r
145 /** Return current value of timer's count register.\r
146  * @param btimer_id Basic Timer ID. \r
147  * @return uint32_t timer count may be 32 or 16 bits depending \r
148  *         upon the hardware.  Timers 0-3 are 16-bit\r
149  *         and Timers 4-5 are 32-bit.\r
150  */\r
151 uint32_t btimer_count_get(uint8_t btimer_id)\r
152 {    \r
153     uint32_t cnt;\r
154     \r
155     cnt = 0ul;\r
156     if ( btmr_valid(btimer_id) ) {        \r
157                         \r
158         cnt = p_btimer_count_get(btimer_id);        \r
159     }\r
160     \r
161     return cnt;\r
162 }\r
163 \r
164 /* ---------------------------------------------------------------------- */\r
165 /*          Function to reload counter from Preload Register              */\r
166 /* ---------------------------------------------------------------------- */\r
167 \r
168 /** Force timer to reload counter from preload \r
169  * register.  \r
170  * @param btimer_id Basic Timer ID. \r
171  * @note Hardware will only reload counter if timer is running. \r
172  */\r
173 void btimer_reload(uint8_t btimer_id)\r
174 {\r
175     if ( btmr_valid(btimer_id) ) {        \r
176                         \r
177         if (p_btimer_ctrl_start_get(btimer_id)) //Check if timer is running\r
178         {\r
179                 p_btimer_ctrl_reload_set(btimer_id);\r
180         }\r
181     }\r
182 }\r
183 \r
184 /* ---------------------------------------------------------------------- */\r
185 /*           Functions for stopping and starting the basic Timer          */\r
186 /* ---------------------------------------------------------------------- */\r
187 \r
188 /** Start timer counting.\r
189  * @param btimer_id Basic Timer ID.\r
190  */\r
191 void btimer_start(uint8_t btimer_id)\r
192 {\r
193     if ( btmr_valid(btimer_id) ) {\r
194             \r
195         p_btimer_ctrl_start_set(btimer_id);\r
196     }\r
197 }\r
198 \r
199 /** Stop timer. \r
200  * @param btimer_id Basic Timer ID. \r
201  * @note When a stopped timer is started again it will reload \r
202  *       the count register from preload value.\r
203  */\r
204 void btimer_stop(uint8_t btimer_id)\r
205 {\r
206     if ( btmr_valid(btimer_id) ) {        \r
207                         \r
208         p_btimer_ctrl_start_clr(btimer_id);                     \r
209         \r
210     }\r
211 }\r
212 \r
213 /** Return state of timer's START bit. \r
214  * @param btimer_id Basic Timer ID. \r
215  * @return uint8_t 0(timer not started), 1 (timer started)\r
216  */\r
217 uint8_t btimer_is_started(uint8_t btimer_id)\r
218\r
219     uint8_t sts;\r
220     \r
221     sts = 0;\r
222     if ( btmr_valid(btimer_id) ) {        \r
223                         \r
224         if (p_btimer_ctrl_start_get(btimer_id)) \r
225         {\r
226             sts = 1;\r
227         }                                       \r
228     }\r
229     return sts;\r
230 }\r
231 \r
232 /* ---------------------------------------------------------------------- */\r
233 /*                Function to perform basic timer soft reset              */\r
234 /* ---------------------------------------------------------------------- */\r
235 \r
236 /** Peform soft reset of specified timer. \r
237  * @param btimer_id Basic Timer ID \r
238  * @note Soft reset set all registers to POR values.\r
239  * Spins 256 times waiting on hardware to clear reset bit. \r
240  */\r
241 void btimer_reset(uint8_t btimer_id)\r
242 {   \r
243     uint32_t wait_cnt;\r
244     uint8_t soft_reset_sts;\r
245 \r
246     if (btmr_valid(btimer_id)) {      \r
247                  \r
248         p_btimer_ctrl_soft_reset_set(btimer_id);    \r
249 \r
250         wait_cnt = 256ul;\r
251         do {\r
252             soft_reset_sts = p_btimer_ctrl_soft_reset_sts_get(btimer_id);\r
253             \r
254             if (0 == soft_reset_sts){          \r
255                 break;\r
256             }\r
257         } \r
258         while ( wait_cnt-- ); \r
259     }      \r
260 }\r
261 \r
262 /* ---------------------------------------------------------------------- */\r
263 /*                Functions to halt/unhalt the timer counting             */\r
264 /* ---------------------------------------------------------------------- */\r
265 \r
266 /** Halt timer counting with no reload on unhalt.   \r
267  * @param btimer_id Basic Timer ID. \r
268  * @note A halted timer will not reload the count register when \r
269  *       unhalted, it will continue counting from the current\r
270  *       count value.\r
271  */\r
272 void btimer_halt(uint8_t btimer_id)\r
273 {\r
274     if ( btmr_valid(btimer_id) ) {\r
275                         \r
276         p_btimer_ctrl_halt_set(btimer_id);        \r
277     }\r
278 }\r
279 \r
280 /** Unhalt timer counting. \r
281  * @param btimer_id Basic Timer ID.\r
282  */\r
283 void btimer_unhalt(uint8_t btimer_id)\r
284 {\r
285     if ( btmr_valid(btimer_id) ) { \r
286                         \r
287         p_btimer_ctrl_halt_clr(btimer_id);\r
288     }\r
289 }\r
290 \r
291 /* ---------------------------------------------------------------------- */\r
292 /*                Functions for Basic Timer interrupt                     */\r
293 /* ---------------------------------------------------------------------- */\r
294 \r
295 /** Enable specified timer's interrupt from the block. \r
296  * @param btimer_id Basic Timer ID.\r
297  * @param ien Non-zero enable interrupt in timer block, 0 \r
298  *            disable.\r
299  */\r
300 void btimer_interrupt_enable(uint8_t btimer_id, uint8_t ien)\r
301 {    \r
302     if (btmr_valid(btimer_id)) {      \r
303                         \r
304         p_btimer_int_enable_set(btimer_id);\r
305 \r
306         if (ien) {\r
307              p_btimer_int_enable_set(btimer_id);\r
308         } else {\r
309              p_btimer_int_enable_clr(btimer_id);\r
310         }\r
311     }\r
312 }\r
313 \r
314 /** Read Timer interrupt status and clear if set \r
315  * @param btimer_id Basic Timer ID. \r
316  * @return uint8_t 1 (Timer interrupt status set) else 0. \r
317  * @note If timer interrupt status is set then clear it before \r
318  *       returning.\r
319  */\r
320 uint8_t btimer_interrupt_status_get_clr(uint8_t btimer_id)\r
321 {    \r
322     uint8_t sts;\r
323 \r
324     sts = 0;\r
325     if (btmr_valid(btimer_id)) {        \r
326                                 \r
327                                 sts = p_btimer_int_status_get(btimer_id);\r
328         if (sts) {\r
329                                         p_btimer_int_status_clr(btimer_id);            \r
330         }\r
331     }\r
332     return sts;\r
333 }\r
334 \r
335 #if 0 //Temporary disable until interrupt module\r
336 \r
337 /* ---------------------------------------------------------------------- */\r
338 /*                Functions for Basic Timer GIRQ                          */\r
339 /* ---------------------------------------------------------------------- */\r
340 \r
341 /** Enables GIRQ enable bit for the timer \r
342  * @param btimer_id Basic Timer ID.\r
343  */\r
344 void btimer_girq_enable_set(uint8_t btimer_id)\r
345 {\r
346     if (btmr_valid(btimer_id))\r
347     {\r
348         //Note: Bit Position is same as Timer ID                        \r
349         p_ecia_girq_enable_set(BTIMER_GIRQ, btimer_id);\r
350     }           \r
351 }\r
352 \r
353 /** Clears GIRQ enable bit for the timer \r
354  * @param btimer_id Basic Timer ID.\r
355  */\r
356 void btimer_girq_enable_clr(uint8_t btimer_id)\r
357 {       \r
358     if (btmr_valid(btimer_id))\r
359     {                   \r
360         //Note: Bit Position is same as Timer ID                        \r
361         p_ecia_girq_enable_clr(BTIMER_GIRQ, btimer_id);\r
362     }   \r
363                 \r
364 }\r
365 \r
366 /** Returns GIRQ source bit for the timer \r
367  * @param btimer_id Basic Timer ID.\r
368  * @return uint8_t 0(src bit not set), Non-zero (src bit set)\r
369  */\r
370 uint8_t btimer_girq_src_get(uint8_t btimer_id)\r
371 {\r
372     uint8_t retVal;\r
373 \r
374     retVal = 0;\r
375     if (btmr_valid(btimer_id))\r
376     {\r
377         //Note: Bit Position is same as Timer ID                        \r
378         retVal = p_ecia_girq_source_get(BTIMER_GIRQ, btimer_id);                        \r
379     }\r
380 \r
381     return retVal;\r
382 }\r
383 \r
384 /** Clears GIRQ source bit for the timer \r
385  * @param btimer_id Basic Timer ID.\r
386  */\r
387 void btimer_girq_src_clr(uint8_t btimer_id)\r
388 {\r
389     if (btmr_valid(btimer_id))\r
390     {\r
391         //Note: Bit Position is same as Timer ID                        \r
392         p_ecia_girq_source_clr(BTIMER_GIRQ, btimer_id);                         \r
393     }                   \r
394 }\r
395 \r
396 /** Returns GIRQ result bit for the timer \r
397  * @param btimer_id Basic Timer ID.\r
398  * @return uint8_t 0(result bit not set), Non-zero (result bit set)\r
399  */\r
400 uint8_t btimer_girq_result_get(uint8_t btimer_id)\r
401 {\r
402     uint8_t retVal;\r
403 \r
404     retVal = 0;\r
405     if (btmr_valid(btimer_id))\r
406     {\r
407         //Note: Bit Position is same as Timer ID                        \r
408         retVal = p_ecia_girq_result_get(BTIMER_GIRQ, btimer_id);                        \r
409     }\r
410 \r
411     return retVal;                      \r
412 }\r
413 #endif\r
414 \r
415 /* ---------------------------------------------------------------------- */\r
416 /*                Functions for Basic Timer Sleep                         */\r
417 /* ---------------------------------------------------------------------- */\r
418 \r
419 /** Enable/Disable clock gating on idle of a timer  \r
420  * @param btimer_id Basic Timer ID.\r
421  * @param sleep_en 1 = Sleep enable, 0 = Sleep disable\r
422  */\r
423 void btimer_sleep(uint8_t btimer_id, uint8_t sleep_en)\r
424 {\r
425     uint32_t pcr_blk_id;\r
426                 \r
427     if ( btmr_valid(btimer_id) ) \r
428     {                           \r
429         pcr_blk_id = btmr_pcr_id[btimer_id];                    \r
430                         \r
431         pcr_sleep_enable(pcr_blk_id, sleep_en);        \r
432     }\r
433 }\r
434 \r
435 /** Returns clk required status for the timer block\r
436  * @param btimer_id Basic Timer ID.\r
437  * @return Non-zero if clk required, else 0\r
438  */\r
439 uint32_t btimer_clk_reqd_sts_get(uint8_t btimer_id)\r
440 {\r
441     uint32_t retVal;\r
442     uint32_t pcr_blk_id;\r
443         \r
444     retVal = 0ul;   \r
445     if ( btmr_valid(btimer_id) ) \r
446     {                   \r
447         pcr_blk_id = btmr_pcr_id[btimer_id];\r
448         \r
449         retVal = pcr_clock_reqd_status_get(pcr_blk_id);                        \r
450     }\r
451                 \r
452     return retVal;\r
453 }\r
454 \r
455 /** Enable/Disable reset on sleep for the timer block \r
456  * @param btimer_id Basic Timer ID.\r
457  * @param reset_en 1 to enable, 0 to disable\r
458  */\r
459 void btimer_reset_on_sleep(uint8_t btimer_id, uint8_t reset_en)\r
460 {\r
461     uint32_t pcr_blk_id;   \r
462     \r
463     if ( btmr_valid(btimer_id) ) \r
464     {                   \r
465         pcr_blk_id = btmr_pcr_id[btimer_id];\r
466     \r
467         pcr_reset_enable(pcr_blk_id, reset_en);                 \r
468     }\r
469 }\r
470 \r
471 /* end btimer_api.c */\r
472 \r
473 /**   @} //Peripheral Basic_Timer\r
474  */\r