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: 2016/09/22 08:03:49 $
\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
42 //#include "..\interrupt\ecia.h"
\r
44 /** Basic Timer Sleep Registers & Bit Positions */
\r
45 static const uint32_t btmr_pcr_id[BTIMER_MAX_INSTANCE] = {
\r
54 #ifdef PLIB_BTIMER_CHECK_ID
\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
60 static uint8_t btmr_valid(uint8_t btimer_id)
\r
62 if ( btimer_id < (PID_BTIMER_MAX ) ) {
\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
76 static uint8_t btmr_valid(uint8_t btimer_id) { return 1; }
\r
81 /* ---------------------------------------------------------------------- */
\r
82 /* Basic Timer Intitialization function */
\r
83 /* ---------------------------------------------------------------------- */
\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
92 void btimer_init(uint8_t btimer_id,
\r
95 uint32_t initial_count,
\r
96 uint32_t preload_count)
\r
100 if (btmr_valid(btimer_id)) {
\r
102 btimer_reset(btimer_id);
\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
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
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
116 if (tmr_cntl & BTIMER_AUTO_RESTART) {
\r
117 p_btimer_ctrl_auto_restart_set(btimer_id);
\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
126 /* ---------------------------------------------------------------------- */
\r
127 /* Functions to program and read the Basic Timer Counter */
\r
128 /* ---------------------------------------------------------------------- */
\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
137 void btimer_count_set(uint8_t btimer_id, uint32_t count)
\r
139 if ( btmr_valid(btimer_id) ) {
\r
141 p_btimer_count_set(btimer_id, count);
\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
151 uint32_t btimer_count_get(uint8_t btimer_id)
\r
156 if ( btmr_valid(btimer_id) ) {
\r
158 cnt = p_btimer_count_get(btimer_id);
\r
164 /* ---------------------------------------------------------------------- */
\r
165 /* Function to reload counter from Preload Register */
\r
166 /* ---------------------------------------------------------------------- */
\r
168 /** Force timer to reload counter from preload
\r
170 * @param btimer_id Basic Timer ID.
\r
171 * @note Hardware will only reload counter if timer is running.
\r
173 void btimer_reload(uint8_t btimer_id)
\r
175 if ( btmr_valid(btimer_id) ) {
\r
177 if (p_btimer_ctrl_start_get(btimer_id)) //Check if timer is running
\r
179 p_btimer_ctrl_reload_set(btimer_id);
\r
184 /* ---------------------------------------------------------------------- */
\r
185 /* Functions for stopping and starting the basic Timer */
\r
186 /* ---------------------------------------------------------------------- */
\r
188 /** Start timer counting.
\r
189 * @param btimer_id Basic Timer ID.
\r
191 void btimer_start(uint8_t btimer_id)
\r
193 if ( btmr_valid(btimer_id) ) {
\r
195 p_btimer_ctrl_start_set(btimer_id);
\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
204 void btimer_stop(uint8_t btimer_id)
\r
206 if ( btmr_valid(btimer_id) ) {
\r
208 p_btimer_ctrl_start_clr(btimer_id);
\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
217 uint8_t btimer_is_started(uint8_t btimer_id)
\r
222 if ( btmr_valid(btimer_id) ) {
\r
224 if (p_btimer_ctrl_start_get(btimer_id))
\r
232 /* ---------------------------------------------------------------------- */
\r
233 /* Function to perform basic timer soft reset */
\r
234 /* ---------------------------------------------------------------------- */
\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
241 void btimer_reset(uint8_t btimer_id)
\r
244 uint8_t soft_reset_sts;
\r
246 if (btmr_valid(btimer_id)) {
\r
248 p_btimer_ctrl_soft_reset_set(btimer_id);
\r
252 soft_reset_sts = p_btimer_ctrl_soft_reset_sts_get(btimer_id);
\r
254 if (0 == soft_reset_sts){
\r
258 while ( wait_cnt-- );
\r
262 /* ---------------------------------------------------------------------- */
\r
263 /* Functions to halt/unhalt the timer counting */
\r
264 /* ---------------------------------------------------------------------- */
\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
272 void btimer_halt(uint8_t btimer_id)
\r
274 if ( btmr_valid(btimer_id) ) {
\r
276 p_btimer_ctrl_halt_set(btimer_id);
\r
280 /** Unhalt timer counting.
\r
281 * @param btimer_id Basic Timer ID.
\r
283 void btimer_unhalt(uint8_t btimer_id)
\r
285 if ( btmr_valid(btimer_id) ) {
\r
287 p_btimer_ctrl_halt_clr(btimer_id);
\r
291 /* ---------------------------------------------------------------------- */
\r
292 /* Functions for Basic Timer interrupt */
\r
293 /* ---------------------------------------------------------------------- */
\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
300 void btimer_interrupt_enable(uint8_t btimer_id, uint8_t ien)
\r
302 if (btmr_valid(btimer_id)) {
\r
304 p_btimer_int_enable_set(btimer_id);
\r
307 p_btimer_int_enable_set(btimer_id);
\r
309 p_btimer_int_enable_clr(btimer_id);
\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
320 uint8_t btimer_interrupt_status_get_clr(uint8_t btimer_id)
\r
325 if (btmr_valid(btimer_id)) {
\r
327 sts = p_btimer_int_status_get(btimer_id);
\r
329 p_btimer_int_status_clr(btimer_id);
\r
335 #if 0 //Temporary disable until interrupt module
\r
337 /* ---------------------------------------------------------------------- */
\r
338 /* Functions for Basic Timer GIRQ */
\r
339 /* ---------------------------------------------------------------------- */
\r
341 /** Enables GIRQ enable bit for the timer
\r
342 * @param btimer_id Basic Timer ID.
\r
344 void btimer_girq_enable_set(uint8_t btimer_id)
\r
346 if (btmr_valid(btimer_id))
\r
348 //Note: Bit Position is same as Timer ID
\r
349 p_ecia_girq_enable_set(BTIMER_GIRQ, btimer_id);
\r
353 /** Clears GIRQ enable bit for the timer
\r
354 * @param btimer_id Basic Timer ID.
\r
356 void btimer_girq_enable_clr(uint8_t btimer_id)
\r
358 if (btmr_valid(btimer_id))
\r
360 //Note: Bit Position is same as Timer ID
\r
361 p_ecia_girq_enable_clr(BTIMER_GIRQ, btimer_id);
\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
370 uint8_t btimer_girq_src_get(uint8_t btimer_id)
\r
375 if (btmr_valid(btimer_id))
\r
377 //Note: Bit Position is same as Timer ID
\r
378 retVal = p_ecia_girq_source_get(BTIMER_GIRQ, btimer_id);
\r
384 /** Clears GIRQ source bit for the timer
\r
385 * @param btimer_id Basic Timer ID.
\r
387 void btimer_girq_src_clr(uint8_t btimer_id)
\r
389 if (btmr_valid(btimer_id))
\r
391 //Note: Bit Position is same as Timer ID
\r
392 p_ecia_girq_source_clr(BTIMER_GIRQ, btimer_id);
\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
400 uint8_t btimer_girq_result_get(uint8_t btimer_id)
\r
405 if (btmr_valid(btimer_id))
\r
407 //Note: Bit Position is same as Timer ID
\r
408 retVal = p_ecia_girq_result_get(BTIMER_GIRQ, btimer_id);
\r
415 /* ---------------------------------------------------------------------- */
\r
416 /* Functions for Basic Timer Sleep */
\r
417 /* ---------------------------------------------------------------------- */
\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
423 void btimer_sleep(uint8_t btimer_id, uint8_t sleep_en)
\r
425 uint32_t pcr_blk_id;
\r
427 if ( btmr_valid(btimer_id) )
\r
429 pcr_blk_id = btmr_pcr_id[btimer_id];
\r
431 pcr_sleep_enable(pcr_blk_id, sleep_en);
\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
439 uint32_t btimer_clk_reqd_sts_get(uint8_t btimer_id)
\r
442 uint32_t pcr_blk_id;
\r
445 if ( btmr_valid(btimer_id) )
\r
447 pcr_blk_id = btmr_pcr_id[btimer_id];
\r
449 retVal = pcr_clock_reqd_status_get(pcr_blk_id);
\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
459 void btimer_reset_on_sleep(uint8_t btimer_id, uint8_t reset_en)
\r
461 uint32_t pcr_blk_id;
\r
463 if ( btmr_valid(btimer_id) )
\r
465 pcr_blk_id = btmr_pcr_id[btimer_id];
\r
467 pcr_reset_enable(pcr_blk_id, reset_en);
\r
471 /* end btimer_api.c */
\r
473 /** @} //Peripheral Basic_Timer
\r