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 with unit testing feedbacks
\r
27 ******************************************************************************/
\r
29 * \brief Basic Timer Peripheral Header file
\r
32 * This file is the header file for Basic Timer Peripheral
\r
33 ******************************************************************************/
\r
35 /** @defgroup Basic_Timer
\r
42 /******************************************************************************/
\r
43 /** Logical Timer ID for APIs.
\r
44 * This is the timer IDs passed to Basic Timer API function calls
\r
45 *******************************************************************************/
\r
57 /* ---------------------------------------------------------------------- */
\r
58 /* Logical flags for Timer Control */
\r
59 /* ---------------------------------------------------------------------- */
\r
60 //This is for tmr_cntl parameter in btimer_init function
\r
61 #define BTIMER_AUTO_RESTART (0x08u)
\r
62 #define BTIMER_ONE_SHOT (0u)
\r
63 #define BTIMER_COUNT_UP (0x04u)
\r
64 #define BTIMER_COUNT_DOWN (0u)
\r
65 #define BTIMER_INT_EN (0x01u)
\r
66 #define BTIMER_NO_INT (0u)
\r
67 /* ---------------------------------------------------------------------- */
\r
70 //Timer Block Hardware Bits and Masks
\r
71 #define BTIMER_CNTL_HALT (0x80UL)
\r
72 #define BTIMER_CNTL_RELOAD (0x40UL)
\r
73 #define BTIMER_CNTL_START (0x20UL)
\r
74 #define BTIMER_CNTL_SOFT_RESET (0x10UL)
\r
75 #define BTIMER_CNTL_AUTO_RESTART (0x08UL)
\r
76 #define BTIMER_CNTL_COUNT_UP (0x04UL)
\r
77 #define BTIMER_CNTL_ENABLE (0x01UL)
\r
79 #define BTIMER_CNTL_HALT_BIT (7U)
\r
80 #define BTIMER_CNTL_RELOAD_BIT (6U)
\r
81 #define BTIMER_CNTL_START_BIT (5U)
\r
82 #define BTIMER_CNTRL_SOFT_RESET_BIT (4U)
\r
83 #define BTIMER_CNTL_AUTO_RESTART_BIT (3U)
\r
84 #define BTIMER_CNTL_COUNT_DIR_BIT (2U)
\r
85 #define BTIMER_CNTL_ENABLE_BIT (0U)
\r
87 #define BTIMER_GIRQ MEC_GIRQ23_ID
\r
88 #define BTIMER_MAX_INSTANCE PID_BTIMER_MAX
\r
91 /* ---------------------------------------------------------------------- */
\r
92 /* API - Basic Timer Intitialization function */
\r
93 /* ---------------------------------------------------------------------- */
\r
95 /** Initialize specified timer
\r
96 * @param btimer_id Basic Timer ID
\r
97 * @param tmr_cntl Logical flags for Timer Control
\r
98 * @param initial_count Initial Count
\r
99 * @param preload_count Preload Count
\r
100 * @note Performs a soft reset of the timer before configuration
\r
102 void btimer_init(uint8_t btimer_id,
\r
104 uint16_t prescaler,
\r
105 uint32_t initial_count,
\r
106 uint32_t preload_count);
\r
108 /* ---------------------------------------------------------------------- */
\r
109 /* API - Functions to program and read the Basic Timer Counter */
\r
110 /* ---------------------------------------------------------------------- */
\r
111 /** Program timer's counter register.
\r
112 * @param btimer_id Basic Timer ID
\r
113 * @param count new counter value
\r
114 * @note Timer hardware may implement a 16-bit or 32-bit
\r
115 * hardware counter. If the timer is 16-bit only the lower
\r
116 * 16-bits of the count paramter are used.
\r
118 void btimer_count_set(uint8_t btimer_id, uint32_t count);
\r
120 /** Return current value of timer's count register.
\r
121 * @param btimer_id Basic Timer ID.
\r
122 * @return uint32_t timer count may be 32 or 16 bits depending
\r
123 * upon the hardware. Timers 0-3 are 16-bit
\r
124 * and Timers 4-5 are 32-bit.
\r
126 uint32_t btimer_count_get(uint8_t btimer_id);
\r
128 /* ---------------------------------------------------------------------- */
\r
129 /* API - Function to reload counter from Preload Register */
\r
130 /* ---------------------------------------------------------------------- */
\r
131 /** Force timer to reload counter from preload
\r
133 * @param btimer_id Basic Timer ID.
\r
134 * @note Hardware will only reload counter if timer is running.
\r
136 void btimer_reload(uint8_t btimer_id);
\r
138 /* ---------------------------------------------------------------------- */
\r
139 /* API - Functions for stopping and starting the basic Timer */
\r
140 /* ---------------------------------------------------------------------- */
\r
141 /** Start timer counting.
\r
142 * @param btimer_id Basic Timer ID.
\r
144 void btimer_start(uint8_t btimer_id);
\r
147 * @param btimer_id Basic Timer ID.
\r
148 * @note When a stopped timer is started again it will reload
\r
149 * the count register from preload value.
\r
151 void btimer_stop(uint8_t btimer_id);
\r
153 /** Return state of timer's START bit.
\r
154 * @param btimer_id Basic Timer ID.
\r
155 * @return uint8_t 0(timer not started), 1 (timer started)
\r
157 uint8_t btimer_is_started(uint8_t btimer_id);
\r
159 /* ---------------------------------------------------------------------- */
\r
160 /* API - Function to perform basic timer soft reset */
\r
161 /* ---------------------------------------------------------------------- */
\r
162 /** Peform soft reset of specified timer.
\r
163 * @param btimer_id Basic Timer ID
\r
164 * @note Soft reset set all registers to POR values.
\r
165 * Spins 256 times waiting on hardware to clear reset bit.
\r
167 void btimer_reset(uint8_t btimer_id);
\r
169 /* ---------------------------------------------------------------------- */
\r
170 /* API - Functions to halt/unhalt the timer counting */
\r
171 /* ---------------------------------------------------------------------- */
\r
172 /** Halt timer counting with no reload on unhalt.
\r
173 * @param btimer_id Basic Timer ID.
\r
174 * @note A halted timer will not reload the count register when
\r
175 * unhalted, it will continue counting from the current
\r
178 void btimer_halt(uint8_t btimer_id);
\r
180 /** Unhalt timer counting.
\r
181 * @param btimer_id Basic Timer ID.
\r
183 void btimer_unhalt(uint8_t btimer_id);
\r
185 /* ---------------------------------------------------------------------- */
\r
186 /* API - Functions for Basic Timer interrupt */
\r
187 /* ---------------------------------------------------------------------- */
\r
188 /** Enable specified timer's interrupt from the block.
\r
189 * @param btimer_id Basic Timer ID.
\r
190 * @param ien Non-zero enable interrupt in timer block, 0
\r
193 void btimer_interrupt_enable(uint8_t btimer_id, uint8_t ien);
\r
195 /** Read Timer interrupt status and clear if set
\r
196 * @param btimer_id Basic Timer ID.
\r
197 * @return uint8_t 1 (Timer interrupt status set) else 0.
\r
198 * @note If timer interrupt status is set then clear it before
\r
201 uint8_t btimer_interrupt_status_get_clr(uint8_t btimer_id);
\r
203 /* ---------------------------------------------------------------------- */
\r
204 /* API - Functions for Basic Timer GIRQ */
\r
205 /* ---------------------------------------------------------------------- */
\r
206 /** Enables GIRQ enable bit for the timer
\r
207 * @param btimer_id Basic Timer ID.
\r
209 void btimer_girq_enable_set(uint8_t btimer_id);
\r
211 /** Clears GIRQ enable bit for the timer
\r
212 * @param btimer_id Basic Timer ID.
\r
214 void btimer_girq_enable_clr(uint8_t btimer_id);
\r
216 /** Returns GIRQ source bit for the timer
\r
217 * @param btimer_id Basic Timer ID.
\r
218 * @return uint8_t 0(src bit not set), Non-zero (src bit set)
\r
220 uint8_t btimer_girq_src_get(uint8_t btimer_id);
\r
222 /** Clears GIRQ source bit for the timer
\r
223 * @param btimer_id Basic Timer ID.
\r
225 void btimer_girq_src_clr(uint8_t btimer_id);
\r
227 /** Returns GIRQ result bit for the timer
\r
228 * @param btimer_id Basic Timer ID.
\r
229 * @return uint8_t 0(result bit not set), Non-zero (result bit set)
\r
231 uint8_t btimer_girq_result_get(uint8_t btimer_id);
\r
233 /* ---------------------------------------------------------------------- */
\r
234 /* API - Functions for Basic Timer Sleep */
\r
235 /* ---------------------------------------------------------------------- */
\r
236 /** Enable/Disable clock gating on idle of a timer
\r
237 * @param btimer_id Basic Timer ID.
\r
238 * @param sleep_en 1 = Sleep enable, 0 = Sleep disable
\r
240 void btimer_sleep(uint8_t btimer_id, uint8_t sleep_en);
\r
242 /** Returns clk required status for the timer block
\r
243 * @param btimer_id Basic Timer ID.
\r
244 * @return Non-zero if clk required, else 0
\r
246 uint32_t btimer_clk_reqd_sts_get(uint8_t btimer_id);
\r
248 /** Enable/Disable reset on sleep for the timer block
\r
249 * @param btimer_id Basic Timer ID.
\r
250 * @param reset_en 1 to enable, 0 to disable
\r
252 void btimer_reset_on_sleep(uint8_t btimer_id, uint8_t reset_en);
\r
254 /* ---------------------------------------------------------------------- */
\r
255 /* Peripheral Function - Functions to set and read Timer Counter Register */
\r
256 /* ---------------------------------------------------------------------- */
\r
257 /** Sets timer counter
\r
258 * @param btimer_id Basic Timer ID
\r
259 * @param count - 32-bit counter
\r
261 void p_btimer_count_set(uint8_t btimer_id, uint32_t count);
\r
263 /** Read the timer counter
\r
264 * @param btimer_id Basic Timer ID
\r
265 * @return count - 32-bit counter
\r
267 uint32_t p_btimer_count_get(uint8_t btimer_id);
\r
270 /* ---------------------------------------------------------------------- */
\r
271 /* Peripheral Function - Function to program the Preload */
\r
272 /* ---------------------------------------------------------------------- */
\r
273 /** Sets preload for the counter
\r
274 * @param btimer_id Basic Timer ID
\r
275 * @param preload_count - 32-bit pre-load value
\r
277 void p_btimer_preload_set(uint8_t btimer_id, uint32_t preload_count);
\r
279 /* ---------------------------------------------------------------------- */
\r
280 /* Peripheral Functions - Functions for basic timer interrupts */
\r
281 /* ---------------------------------------------------------------------- */
\r
282 /** Reads the interrupt status bit in the timer block
\r
283 * @param btimer_id Basic Timer ID
\r
284 * @return status - 1 if interrupt status set, else 0
\r
286 uint8_t p_btimer_int_status_get(uint8_t btimer_id);
\r
288 /** Clears interrupt status bit in the timer block
\r
289 * @param btimer_id Basic Timer ID
\r
291 void p_btimer_int_status_clr(uint8_t btimer_id);
\r
293 /** Sets interrupt enable bit in the timer block
\r
294 * @param btimer_id Basic Timer ID
\r
296 void p_btimer_int_enable_set(uint8_t btimer_id);
\r
298 /** Clears interrupt enable bit for the timer block
\r
299 * @param btimer_id Basic Timer ID
\r
301 void p_btimer_int_enable_clr(uint8_t btimer_id);
\r
303 /* ---------------------------------------------------------------------- */
\r
304 /* Peripheral Functions - Functions for Control Register */
\r
305 /* ---------------------------------------------------------------------- */
\r
306 /** Writes the control register 32-bits
\r
307 * @param btimer_id Basic Timer ID
\r
308 * @param value - 32-bit value to program
\r
310 void p_btimer_ctrl_write(uint8_t btimer_id, uint32_t value);
\r
312 /** Reads the control register
\r
313 * @param btimer_id Basic Timer ID
\r
314 * @return uint32_t - 32-bit value
\r
316 uint32_t p_btimer_ctrl_read(uint8_t btimer_id);
\r
318 /** Clears enable bit in the control register
\r
319 * @param btimer_id Basic Timer ID
\r
321 void p_btimer_ctrl_enable_set(uint8_t btimer_id);
\r
323 /** Clears enable bit in the control register
\r
324 * @param btimer_id Basic Timer ID
\r
326 void p_btimer_ctrl_enable_clr(uint8_t btimer_id);
\r
328 /** Sets counter direction bit in the control register
\r
329 * @param btimer_id Basic Timer ID
\r
331 void p_btimer_ctrl_counter_dir_set(uint8_t btimer_id);
\r
333 /** Clears counter direction bit in the control register
\r
334 * @param btimer_id Basic Timer ID
\r
336 void p_btimer_ctrl_counter_dir_clr(uint8_t btimer_id);
\r
338 /** Sets auto restart bit in the control register
\r
339 * @param btimer_id Basic Timer ID
\r
341 void p_btimer_ctrl_auto_restart_set(uint8_t btimer_id);
\r
343 /** Clears auto resetart bit in the control register
\r
344 * @param btimer_id Basic Timer ID
\r
346 void p_btimer_ctrl_auto_restart_clr(uint8_t btimer_id);
\r
348 /** Sets soft reset bit in the control register
\r
349 * @param btimer_id Basic Timer ID
\r
351 void p_btimer_ctrl_soft_reset_set(uint8_t btimer_id);
\r
353 /** Read Soft Reset bit
\r
354 * @param btimer_id Basic Timer ID
\r
355 * @return 0 if soft reset status bit cleared; else non-zero value
\r
357 uint8_t p_btimer_ctrl_soft_reset_sts_get(uint8_t btimer_id);
\r
359 /** Sets start bit in the control register
\r
360 * @param btimer_id Basic Timer ID
\r
362 void p_btimer_ctrl_start_set(uint8_t btimer_id);
\r
364 /** Read start bit in the control register
\r
365 * @param btimer_id Basic Timer ID
\r
366 * @return 0 if start bit not set; else non-zero value
\r
368 uint8_t p_btimer_ctrl_start_get(uint8_t btimer_id);
\r
370 /** Clears start bit in the control register
\r
371 * @param btimer_id Basic Timer ID
\r
373 void p_btimer_ctrl_start_clr(uint8_t btimer_id);
\r
375 /** Sets reload bit in the control register
\r
376 * @param btimer_id Basic Timer ID
\r
378 void p_btimer_ctrl_reload_set(uint8_t btimer_id);
\r
380 /** Clears reload bit in the control register
\r
381 * @param btimer_id Basic Timer ID
\r
383 void p_btimer_ctrl_reload_clr(uint8_t btimer_id);
\r
385 /** Sets halt bit in the control register
\r
386 * @param btimer_id Basic Timer ID
\r
388 void p_btimer_ctrl_halt_set(uint8_t btimer_id);
\r
390 /** Clears halt bit in the control register
\r
391 * @param btimer_id Basic Timer ID
\r
394 void p_btimer_ctrl_halt_clr(uint8_t btimer_id);
\r
396 /** Sets prescale value
\r
397 * @param btimer_id Basic Timer ID
\r
398 * @param prescaler - 16-bit pre-scale value
\r
400 void p_btimer_ctrl_prescale_set(uint8_t btimer_id, uint16_t prescaler);
\r
403 #endif // #ifndef _BTIMER_H
\r
405 /* end btimer_perphl.c */
\r
407 /** @} //Peripherals Basic_Timer
\r