| ISF 2.1 Intelligent Sensing Framework for Kinetis with Processor Expert

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
fsl_pit_driver.c
Go to the documentation of this file.
1 /*!
2 ********************************************************************************
3 * File: fsl_pit_driver.c
4 *
5 * Copyright (c) 2014, Freescale Semiconductor, Inc.
6 *
7 *******************************************************************************/
8 /*!
9 * @file fsl_pit_driver.c
10 * @brief \b fsl_pit_driver.c implements the PIT driver.
11 */
12
13 #include "fsl_pit_driver.h"
14 #include "BM_pit_instance.h"
15 #include "isf_fsl_TU_PEx.h"
16
17 /*******************************************************************************
18 * Definitions
19 ******************************************************************************/
20
21 /*******************************************************************************
22 * Variables
23 ******************************************************************************/
24
25 /* pit source clock variable which will be updated in pit_init_module. */
26 uint64_t pitSourceClock;
27
28 /* Table to save PIT IRQ enum numbers defined in CMSIS files. This table is
29 * defined in fsl_pit_irq.c */
30 //extern IRQn_Type pit_irq_ids[FSL_FEATURE_PIT_TIMER_COUNT];
31
32
33 /*******************************************************************************
34 * Code
35 ******************************************************************************/
36
37 /*FUNCTION**********************************************************************
38 *
39 * Function Name : pit_init_module
40 * Description : Initialize PIT module.
41 * This function must be called before calling all the other PIT driver functions.
42 * This function un-gates the PIT clock and enables the PIT module. The isRunInDebug
43 * passed into function will affect all timer channels.
44 *
45 *END**************************************************************************/
46 void pit_init_module(bool isRunInDebug)
47 {
48 /* Enable PIT module clock*/
49 pit_hal_enable();
50
51 /* Set timer run or stop in debug mode*/
52 pit_hal_configure_timer_run_in_debug(isRunInDebug);
53
54 /* Finally, update pit source clock frequency.*/
55 // clock_manager_get_frequency(kBusClock, &busClock);
56 pitSourceClock = (uint64_t)BM_PIT_SOURCE_CLK;
57 }
58
59 /*FUNCTION**********************************************************************
60 *
61 * Function Name : pit_init_channel
62 * Description : Initialize PIT channel.
63 * This function initialize PIT timers by channel. Pass in timer number and its
64 * config structure. Timers do not start counting by default after calling this
65 * function. Function pit_timer_start must be called to start timer counting.
66 * Call pit_set_timer_period_us to re-set the period.
67 *
68 *END**************************************************************************/
69 void pit_init_channel(uint32_t timer, const pit_user_config_t * config)
70 {
71 TU_instance_tbl[timer].fnTULDDInit(NULL);
72
73 /* Set timer period.*/
74 pit_set_timer_period_us(timer, config->periodUs);
75
76
77 /* Enable or disable interrupt.*/
78 pit_hal_configure_interrupt(timer, config->isInterruptEnabled);
79
80 }
81
82 /*FUNCTION**********************************************************************
83 *
84 * Function Name : pit_shutdown
85 * Description : Disable PIT module and gate control
86 * This function will disable all PIT interrupts and PIT clock. Then gate the
87 * PIT clock control. pit_init must be called in order to use PIT again.
88 *
89 *END**************************************************************************/
90 void pit_shutdown(void)
91 {
92 /* Disable PIT module clock*/
93 pit_hal_disable();
94 }
95
96 /*FUNCTION**********************************************************************
97 *
98 * Function Name : pit_timer_start
99 * Description : Start timer counting.
100 * After calling this function, timers load period value, count down to 0 and
101 * then load the respective start value again. Each time a timer reaches 0,
102 * it will generate a trigger pulse and set the timeout interrupt flag.
103 *
104 *END**************************************************************************/
105 void pit_timer_start(uint32_t timer)
106 {
107 pit_hal_timer_start(timer);
108 }
109
110 /*FUNCTION**********************************************************************
111 *
112 * Function Name : pit_timer_stop
113 * Description : Stop timer counting.
114 * This function will stop every timer counting. Timers will reload their periods
115 * respectively after calling pit_timer_start next time.
116 *
117 *END**************************************************************************/
118 void pit_timer_stop(uint32_t timer)
119 {
120 pit_hal_timer_stop(timer);
121 }
122
123 /*FUNCTION**********************************************************************
124 *
125 * Function Name : pit_set_timer_period_us
126 * Description : Set timer period in microseconds unit.
127 * The period range depends on the frequency of PIT source clock. If required
128 * period is out the range, try to use lifetime timer if applicable.
129 *
130 *END**************************************************************************/
131 void pit_set_timer_period_us(uint32_t timer, uint32_t us)
132 {
133 /* Calculate the count value, assign it to timer counter register.*/
134 uint32_t count = (uint32_t)(us * BM_PIT_SCALER - 1U);
135 pit_hal_set_timer_period_count(timer, count);
136 }
137
138 /*FUNCTION**********************************************************************
139 *
140 * Function Name : pit_read_timer_us
141 * Description : Read current timer value in microseconds unit.
142 * This function will return an absolute time stamp in the unit of microseconds.
143 * One common use of this function is to measure the running time of part of
144 * code. Just call this function at both the beginning and end of code, the time
145 * difference between these two time stamp will be the running time (Need to
146 * make sure the running time will not exceed the timer period). Also, the time
147 * stamp returned is up-counting.
148 *
149 *END**************************************************************************/
150 uint32_t pit_read_timer_us(uint32_t timer)
151 {
152 /* Get current timer count, and reverse it to up-counting.*/
153 uint64_t currentTime = pit_hal_read_timer_count(timer);
154
155 /* Convert count numbers to microseconds unit.*/
156 currentTime = (currentTime * 1000000U) / pitSourceClock;
157 return (uint32_t)currentTime;
158 }
159
160 #if FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
161 /*FUNCTION**********************************************************************
162 *
163 * Function Name : pit_set_lifetime_timer_period_us
164 * Description : Set lifetime timer period (Timers must be chained).
165 * Timer 1 must be chained with timer 0 before using lifetime timer. The period
166 * range is restricted by "period * pitSourceClock < max of an uint64_t integer",
167 * or it may cause a overflow and is not able to set correct period.
168 *
169 *END**************************************************************************/
170 void pit_set_lifetime_timer_period_us(uint64_t us)
171 {
172 uint64_t lifeTimeCount;
173
174 /* Calculate the counter value.*/
175 lifeTimeCount = us * pitSourceClock / 1000000U - 1U;
176
177 /* Assign to timers.*/
178 pit_hal_set_timer_period_count(0U, (uint32_t)lifeTimeCount);
179 pit_hal_set_timer_period_count(1U, (uint32_t)(lifeTimeCount >> 32U));
180 }
181
182 /*FUNCTION**********************************************************************
183 *
184 * Function Name : pit_read_lifetime_timer_us
185 * Description : Read current lifetime value in microseconds unit.
186 * Return an absolute time stamp in the unit of microseconds. The time stamp
187 * value will not exceed the timer period. Also, the timer is up-counting.
188 *
189 *END**************************************************************************/
190 uint64_t pit_read_lifetime_timer_us(void)
191 {
192 /* Get current lifetime timer count, and reverse it to up-counting.*/
193 uint64_t currentTime = (~pit_hal_read_lifetime_timer_count());
194
195 /* Convert count numbers to microseconds unit.*/
196 /* Note: using currentTime * 1000 rather than 1000000 to avoid short time overflow. */
197 return currentTime = (currentTime * 1000U) / (pitSourceClock / 1000U);
198 }
199 #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER*/
200
201 void pit_init(bool bEnableirq, uint32 usec)
202 {
203 pit_user_config_t pit_config = {
204 .isInterruptEnabled = bEnableirq,
205 .isTimerChained = FALSE,
206 .periodUs = usec
207 };
208 pit_init_module(FALSE);
209 pit_init_channel(ISF_BM_PIT_INSTANCE,&pit_config);
210
211 // This call will start the timer.
212 pit_set_timer_period_us(ISF_BM_PIT_INSTANCE,usec);
213
214 // Just clear the interrupt flag
215 pit_hal_clear_interrupt_flag(ISF_BM_PIT_INSTANCE);
216
217 //bm_timer_set_time(usec);
218
219 // Need to stop timer.
220 pit_timer_stop(ISF_BM_PIT_INSTANCE);
221 }
222
223 /*******************************************************************************
224 * EOF
225 ******************************************************************************/
226
227
228
pit_shutdown
void pit_shutdown(void)
Disable PIT module and gate control.
Definition: fsl_pit_driver.c:90
pit_hal_configure_interrupt
void pit_hal_configure_interrupt(uint32_t timer, bool enable)
Enable or disable timer interrupt.
Definition: fsl_pit_hal.c:92
ISF_BM_PIT_INSTANCE
#define ISF_BM_PIT_INSTANCE
Definition: BM_pit_instance.h:2
FALSE
#define FALSE
Definition: isf_types.h:56
pit_set_timer_period_us
void pit_set_timer_period_us(uint32_t timer, uint32_t us)
Set timer period in microsecond units.
Definition: fsl_pit_driver.c:131
pit_init_module
void pit_init_module(bool isRunInDebug)
Initialize PIT module.
Definition: fsl_pit_driver.c:46
uint32
unsigned long uint32
This defines uint32 as unsigned long.
Definition: isf_types.h:36
pit_init
void pit_init(bool bEnableirq, uint32 usec)
Complete initialization of the PIT timer.
Definition: fsl_pit_driver.c:201
PitUserConfig::periodUs
uint32_t periodUs
Definition: fsl_pit_driver.h:41
BM_pit_instance.h
pit_init_channel
void pit_init_channel(uint32_t timer, const pit_user_config_t *config)
Initialize PIT channel.
Definition: fsl_pit_driver.c:69
pit_hal_clear_interrupt_flag
void pit_hal_clear_interrupt_flag(uint32_t timer)
Clear timer interrupt flag.
Definition: fsl_pit_hal.c:105
isf_fsl_TU_PEx.h
pit_hal_timer_start
void pit_hal_timer_start(uint32_t timer)
Start timer counting.
Definition: fsl_pit_hal.c:56
pit_timer_stop
void pit_timer_stop(uint32_t timer)
Stop timer counting.
Definition: fsl_pit_driver.c:118
pit_hal_set_timer_period_count
void pit_hal_set_timer_period_count(uint32_t timer, uint32_t count)
Set timer period in units of count.
Definition: fsl_pit_hal.c:71
PitUserConfig::isInterruptEnabled
bool isInterruptEnabled
Definition: fsl_pit_driver.h:39
pit_hal_read_timer_count
uint32_t pit_hal_read_timer_count(uint32_t timer)
Read current timer counting value.
Definition: fsl_pit_hal.c:83
pit_hal_enable
void pit_hal_enable(void)
Enable PIT module.
Definition: fsl_pit_hal.c:41
BM_PIT_SCALER
#define BM_PIT_SCALER
Definition: BM_pit_instance.h:3
pit_hal_configure_timer_run_in_debug
void pit_hal_configure_timer_run_in_debug(bool timerRun)
Configure timers to continue to run or stop in debug mode.
Definition: fsl_pit_hal.c:51
pit_timer_start
void pit_timer_start(uint32_t timer)
Start timer counting.
Definition: fsl_pit_driver.c:105
pit_hal_timer_stop
void pit_hal_timer_stop(uint32_t timer)
Stop timer counting.
Definition: fsl_pit_hal.c:61
pit_read_timer_us
uint32_t pit_read_timer_us(uint32_t timer)
Read current timer value in microsecond units.
Definition: fsl_pit_driver.c:150
BM_PIT_SOURCE_CLK
#define BM_PIT_SOURCE_CLK
Definition: BM_pit_instance.h:4
fsl_pit_driver.h
fsl_pit_driver.h defines structures and types for the PIT driver.
TU_instance_tbl
const TU_instance_PEx_t TU_instance_tbl[]
Definition: BM_pit_instance_tbl.h:2
PitUserConfig
PIT timer configuration structure.
Definition: fsl_pit_driver.h:38
pitSourceClock
uint64_t pitSourceClock
Definition: fsl_pit_driver.c:26
pit_hal_disable
void pit_hal_disable(void)
Disable PIT module.
Definition: fsl_pit_hal.c:46
TU_instance_PEx_t::fnTULDDInit
fnTUInit_t fnTULDDInit
Definition: isf_fsl_TU_PEx.h:56
- © Freescale Semiconductor, Inc. 2015. All Rights Reserved.