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
19 ******************************************************************************
\r
21 Version Control Information (Perforce)
\r
22 ******************************************************************************
\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
32 * This file implements the Basic Timer API functions
\r
33 ******************************************************************************/
\r
35 /** @defgroup Basic_Timer
\r
39 #include "common_lib.h"
\r
41 #include "..\pcr\pcr.h"
\r
43 /** Basic Timer Sleep Registers & Bit Positions */
\r
44 static const uint32_t btmr_pcr_id[BTIMER_MAX_INSTANCE] = {
\r
53 #ifdef PLIB_BTIMER_CHECK_ID
\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
59 static uint8_t btmr_valid(uint8_t btimer_id)
\r
61 if ( btimer_id < (PID_BTIMER_MAX ) ) {
\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
75 static uint8_t btmr_valid(uint8_t btimer_id) { return 1; }
\r
80 /* ---------------------------------------------------------------------- */
\r
81 /* Basic Timer Intitialization function */
\r
82 /* ---------------------------------------------------------------------- */
\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
91 void btimer_init(uint8_t btimer_id,
\r
94 uint32_t initial_count,
\r
95 uint32_t preload_count)
\r
99 if (btmr_valid(btimer_id)) {
\r
101 btimer_reset(btimer_id);
\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
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
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
115 if (tmr_cntl & BTIMER_AUTO_RESTART) {
\r
116 p_btimer_ctrl_auto_restart_set(btimer_id);
\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
125 /* ---------------------------------------------------------------------- */
\r
126 /* Functions to program and read the Basic Timer Counter */
\r
127 /* ---------------------------------------------------------------------- */
\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
136 void btimer_count_set(uint8_t btimer_id, uint32_t count)
\r
138 if ( btmr_valid(btimer_id) ) {
\r
140 p_btimer_count_set(btimer_id, count);
\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
150 uint32_t btimer_count_get(uint8_t btimer_id)
\r
155 if ( btmr_valid(btimer_id) ) {
\r
157 cnt = p_btimer_count_get(btimer_id);
\r
163 /* ---------------------------------------------------------------------- */
\r
164 /* Function to reload counter from Preload Register */
\r
165 /* ---------------------------------------------------------------------- */
\r
167 /** Force timer to reload counter from preload
\r
169 * @param btimer_id Basic Timer ID.
\r
170 * @note Hardware will only reload counter if timer is running.
\r
172 void btimer_reload(uint8_t btimer_id)
\r
174 if ( btmr_valid(btimer_id) ) {
\r
176 if (p_btimer_ctrl_start_get(btimer_id)) //Check if timer is running
\r
178 p_btimer_ctrl_reload_set(btimer_id);
\r
183 /* ---------------------------------------------------------------------- */
\r
184 /* Functions for stopping and starting the basic Timer */
\r
185 /* ---------------------------------------------------------------------- */
\r
187 /** Start timer counting.
\r
188 * @param btimer_id Basic Timer ID.
\r
190 void btimer_start(uint8_t btimer_id)
\r
192 if ( btmr_valid(btimer_id) ) {
\r
194 p_btimer_ctrl_start_set(btimer_id);
\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
203 void btimer_stop(uint8_t btimer_id)
\r
205 if ( btmr_valid(btimer_id) ) {
\r
207 p_btimer_ctrl_start_clr(btimer_id);
\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
216 uint8_t btimer_is_started(uint8_t btimer_id)
\r
221 if ( btmr_valid(btimer_id) ) {
\r
223 if (p_btimer_ctrl_start_get(btimer_id))
\r
231 /* ---------------------------------------------------------------------- */
\r
232 /* Function to perform basic timer soft reset */
\r
233 /* ---------------------------------------------------------------------- */
\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
240 void btimer_reset(uint8_t btimer_id)
\r
243 uint8_t soft_reset_sts;
\r
245 if (btmr_valid(btimer_id)) {
\r
247 p_btimer_ctrl_soft_reset_set(btimer_id);
\r
251 soft_reset_sts = p_btimer_ctrl_soft_reset_sts_get(btimer_id);
\r
253 if (0 == soft_reset_sts){
\r
257 while ( wait_cnt-- );
\r
261 /* ---------------------------------------------------------------------- */
\r
262 /* Functions to halt/unhalt the timer counting */
\r
263 /* ---------------------------------------------------------------------- */
\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
271 void btimer_halt(uint8_t btimer_id)
\r
273 if ( btmr_valid(btimer_id) ) {
\r
275 p_btimer_ctrl_halt_set(btimer_id);
\r
279 /** Unhalt timer counting.
\r
280 * @param btimer_id Basic Timer ID.
\r
282 void btimer_unhalt(uint8_t btimer_id)
\r
284 if ( btmr_valid(btimer_id) ) {
\r
286 p_btimer_ctrl_halt_clr(btimer_id);
\r
290 /* ---------------------------------------------------------------------- */
\r
291 /* Functions for Basic Timer interrupt */
\r
292 /* ---------------------------------------------------------------------- */
\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
299 void btimer_interrupt_enable(uint8_t btimer_id, uint8_t ien)
\r
301 if (btmr_valid(btimer_id)) {
\r
303 p_btimer_int_enable_set(btimer_id);
\r
306 p_btimer_int_enable_set(btimer_id);
\r
308 p_btimer_int_enable_clr(btimer_id);
\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
319 uint8_t btimer_interrupt_status_get_clr(uint8_t btimer_id)
\r
324 if (btmr_valid(btimer_id)) {
\r
326 sts = p_btimer_int_status_get(btimer_id);
\r
328 p_btimer_int_status_clr(btimer_id);
\r
334 #if 0 //Temporary disable until interrupt module
\r
336 /* ---------------------------------------------------------------------- */
\r
337 /* Functions for Basic Timer GIRQ */
\r
338 /* ---------------------------------------------------------------------- */
\r
340 /** Enables GIRQ enable bit for the timer
\r
341 * @param btimer_id Basic Timer ID.
\r
343 void btimer_girq_enable_set(uint8_t btimer_id)
\r
345 if (btmr_valid(btimer_id))
\r
347 //Note: Bit Position is same as Timer ID
\r
348 p_ecia_girq_enable_set(BTIMER_GIRQ, btimer_id);
\r
352 /** Clears GIRQ enable bit for the timer
\r
353 * @param btimer_id Basic Timer ID.
\r
355 void btimer_girq_enable_clr(uint8_t btimer_id)
\r
357 if (btmr_valid(btimer_id))
\r
359 //Note: Bit Position is same as Timer ID
\r
360 p_ecia_girq_enable_clr(BTIMER_GIRQ, btimer_id);
\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
369 uint8_t btimer_girq_src_get(uint8_t btimer_id)
\r
374 if (btmr_valid(btimer_id))
\r
376 //Note: Bit Position is same as Timer ID
\r
377 retVal = p_ecia_girq_source_get(BTIMER_GIRQ, btimer_id);
\r
383 /** Clears GIRQ source bit for the timer
\r
384 * @param btimer_id Basic Timer ID.
\r
386 void btimer_girq_src_clr(uint8_t btimer_id)
\r
388 if (btmr_valid(btimer_id))
\r
390 //Note: Bit Position is same as Timer ID
\r
391 p_ecia_girq_source_clr(BTIMER_GIRQ, btimer_id);
\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
399 uint8_t btimer_girq_result_get(uint8_t btimer_id)
\r
404 if (btmr_valid(btimer_id))
\r
406 //Note: Bit Position is same as Timer ID
\r
407 retVal = p_ecia_girq_result_get(BTIMER_GIRQ, btimer_id);
\r
414 /* ---------------------------------------------------------------------- */
\r
415 /* Functions for Basic Timer Sleep */
\r
416 /* ---------------------------------------------------------------------- */
\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
422 void btimer_sleep(uint8_t btimer_id, uint8_t sleep_en)
\r
424 uint32_t pcr_blk_id;
\r
426 if ( btmr_valid(btimer_id) )
\r
428 pcr_blk_id = btmr_pcr_id[btimer_id];
\r
430 pcr_sleep_enable(pcr_blk_id, sleep_en);
\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
438 uint32_t btimer_clk_reqd_sts_get(uint8_t btimer_id)
\r
441 uint32_t pcr_blk_id;
\r
444 if ( btmr_valid(btimer_id) )
\r
446 pcr_blk_id = btmr_pcr_id[btimer_id];
\r
448 retVal = pcr_clock_reqd_status_get(pcr_blk_id);
\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
458 void btimer_reset_on_sleep(uint8_t btimer_id, uint8_t reset_en)
\r
460 uint32_t pcr_blk_id;
\r
462 if ( btmr_valid(btimer_id) )
\r
464 pcr_blk_id = btmr_pcr_id[btimer_id];
\r
466 pcr_reset_enable(pcr_blk_id, reset_en);
\r
470 /* end btimer_api.c */
\r
472 /** @} //Peripheral Basic_Timer
\r