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