]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/pit/src/fsl_pit_driver.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / drivers / pit / src / fsl_pit_driver.c
1 /*
2  * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  
31 #include "fsl_pit_common.h"
32 #include "fsl_pit_driver.h"
33 #include "fsl_clock_manager.h"
34 #include "fsl_interrupt_manager.h"
35
36 /*******************************************************************************
37  * Variables
38  ******************************************************************************/
39
40 /* pit source clock variable which will be updated in PIT_DRV_Init. */
41 uint64_t pitSourceClock;
42
43 /*******************************************************************************
44  * Code
45  ******************************************************************************/
46
47 /*FUNCTION**********************************************************************
48  *
49  * Function Name : PIT_DRV_Init
50  * Description   : Initialize PIT module.
51  * This function must be called before calling all the other PIT driver functions.
52  * This function un-gates the PIT clock and enables the PIT module. The isRunInDebug
53  * passed into function will affect all timer channels. 
54  *
55  *END**************************************************************************/
56 void PIT_DRV_Init(uint32_t instance, bool isRunInDebug)
57 {
58     assert(instance < HW_PIT_INSTANCE_COUNT);
59
60     uint32_t baseAddr = g_pitBaseAddr[instance];
61
62     /* Un-gate pit clock*/
63     CLOCK_SYS_EnablePitClock( 0U);
64
65     /* Enable PIT module clock*/
66     PIT_HAL_Enable(baseAddr);
67
68     /* Set timer run or stop in debug mode*/
69     PIT_HAL_SetTimerRunInDebugCmd(baseAddr, isRunInDebug);
70    
71     /* Finally, update pit source clock frequency.*/
72     pitSourceClock  = CLOCK_SYS_GetPitFreq(0);    
73 }
74
75 /*FUNCTION**********************************************************************
76  *
77  * Function Name : PIT_DRV_InitChannel
78  * Description   : Initialize PIT channel.
79  * This function initialize PIT timers by channel. Pass in timer number and its
80  * config structure. Timers do not start counting by default after calling this
81  * function. Function PIT_DRV_StartTimer must be called to start timer counting. 
82  * Call PIT_DRV_SetTimerPeriodByUs to re-set the period.
83  *
84  *END**************************************************************************/
85 void PIT_DRV_InitChannel(uint32_t instance, uint32_t channel, const pit_user_config_t * config)
86 {
87     assert(instance < HW_PIT_INSTANCE_COUNT);
88
89     uint32_t baseAddr = g_pitBaseAddr[instance];
90     /* Set timer period.*/
91     PIT_DRV_SetTimerPeriodByUs(instance, channel, config->periodUs);
92
93     #if FSL_FEATURE_PIT_HAS_CHAIN_MODE
94     /* Configure timer chained or not.*/
95     PIT_HAL_SetTimerChainCmd(baseAddr, channel, config->isTimerChained);
96     #endif
97
98     /* Enable or disable interrupt.*/
99     PIT_HAL_SetIntCmd(baseAddr, channel, config->isInterruptEnabled);
100
101     /* Configure NVIC*/
102     if (config->isInterruptEnabled)
103     {
104         /* Enable PIT interrupt.*/
105         INT_SYS_EnableIRQ(g_pitIrqId[channel]);
106     }
107 }
108
109 /*FUNCTION**********************************************************************
110  *
111  * Function Name : PIT_DRV_Deinit 
112  * Description   : Disable PIT module and gate control
113  * This function will disable all PIT interrupts and PIT clock. Then gate the
114  * PIT clock control. pit_init must be called in order to use PIT again.
115  * 
116  *END**************************************************************************/
117 void PIT_DRV_Deinit(uint32_t instance)
118 {
119     assert(instance < HW_PIT_INSTANCE_COUNT);
120
121     uint32_t baseAddr = g_pitBaseAddr[instance];
122     uint32_t i;
123
124     /* Disable all PIT interrupts. */
125     for (i=0; i < FSL_FEATURE_PIT_TIMER_COUNT; i++)
126     {
127         PIT_HAL_SetIntCmd(baseAddr, i, false);
128         INT_SYS_DisableIRQ(g_pitIrqId[i]);
129     }
130
131     /* Disable PIT module clock*/
132     PIT_HAL_Disable(baseAddr);
133
134     /* Gate PIT clock control*/
135     CLOCK_SYS_DisablePitClock( 0U);
136 }
137
138 /*FUNCTION**********************************************************************
139  *
140  * Function Name : PIT_DRV_StartTimer
141  * Description   : Start timer counting.
142  * After calling this function, timers load period value, count down to 0 and
143  * then load the respective start value again. Each time a timer reaches 0,
144  * it will generate a trigger pulse and set the timeout interrupt flag.
145  * 
146  *END**************************************************************************/
147 void PIT_DRV_StartTimer(uint32_t instance, uint32_t channel)
148 {
149     assert(instance < HW_PIT_INSTANCE_COUNT);
150
151     uint32_t baseAddr = g_pitBaseAddr[instance];
152     PIT_HAL_StartTimer(baseAddr, channel);
153 }
154
155 /*FUNCTION**********************************************************************
156  *
157  * Function Name : PIT_DRV_StopTimer
158  * Description   : Stop timer counting.
159  * This function will stop every timer counting. Timers will reload their periods
160  * respectively after calling PIT_DRV_StartTimer next time.
161  *
162  *END**************************************************************************/
163 void PIT_DRV_StopTimer(uint32_t instance, uint32_t channel)
164 {
165     assert(instance < HW_PIT_INSTANCE_COUNT);
166
167     uint32_t baseAddr = g_pitBaseAddr[instance];
168     PIT_HAL_StopTimer(baseAddr, channel);
169 }
170
171 /*FUNCTION**********************************************************************
172  *
173  * Function Name : PIT_DRV_SetTimerPeriodByUs
174  * Description   : Set timer period in microseconds unit.
175  * The period range depends on the frequency of PIT source clock. If required
176  * period is out the range, try to use lifetime timer if applicable. 
177  *
178  *END**************************************************************************/
179 void PIT_DRV_SetTimerPeriodByUs(uint32_t instance, uint32_t channel, uint32_t us)
180 {
181     assert(instance < HW_PIT_INSTANCE_COUNT);
182
183     uint32_t baseAddr = g_pitBaseAddr[instance];
184     /* Calculate the count value, assign it to timer counter register.*/
185     uint32_t count = (uint32_t)(us * pitSourceClock / 1000000U - 1U);
186     PIT_HAL_SetTimerPeriodByCount(baseAddr, channel, count);
187 }
188
189 /*FUNCTION**********************************************************************
190  *
191  * Function Name : PIT_DRV_ReadTimerUs
192  * Description   : Read current timer value in microseconds unit.
193  * This function will return an absolute time stamp in the unit of microseconds.
194  * One common use of this function is to measure the running time of part of
195  * code. Just call this function at both the beginning and end of code, the time
196  * difference between these two time stamp will be the running time (Need to 
197  * make sure the running time will not exceed the timer period). Also, the time
198  * stamp returned is up-counting.
199  *
200  *END**************************************************************************/
201 uint32_t PIT_DRV_ReadTimerUs(uint32_t instance, uint32_t channel)
202 {
203     assert(instance < HW_PIT_INSTANCE_COUNT);
204
205     uint32_t baseAddr = g_pitBaseAddr[instance];
206     /* Get current timer count, and reverse it to up-counting.*/
207     uint64_t currentTime = (~PIT_HAL_ReadTimerCount(baseAddr, channel));
208
209     /* Convert count numbers to microseconds unit.*/
210     currentTime = (currentTime * 1000000U) / pitSourceClock;
211     return (uint32_t)currentTime;
212 }
213
214 #if FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
215 /*FUNCTION**********************************************************************
216  *
217  * Function Name : PIT_DRV_SetLifetimeTimerPeriodByUs
218  * Description   : Set lifetime timer period (Timers must be chained).
219  * Timer 1 must be chained with timer 0 before using lifetime timer. The period
220  * range is restricted by "period * pitSourceClock < max of an uint64_t integer",
221  * or it may cause a overflow and is not able to set correct period.
222  *
223  *END**************************************************************************/
224 void PIT_DRV_SetLifetimeTimerPeriodByUs(uint32_t instance, uint64_t us)
225 {
226     assert(instance < HW_PIT_INSTANCE_COUNT);
227
228     uint32_t baseAddr = g_pitBaseAddr[instance];
229     uint64_t lifeTimeCount;
230     
231     /* Calculate the counter value.*/
232     lifeTimeCount = us * pitSourceClock / 1000000U - 1U;
233
234     /* Assign to timers.*/
235     PIT_HAL_SetTimerPeriodByCount(baseAddr, 0U, (uint32_t)lifeTimeCount);
236     PIT_HAL_SetTimerPeriodByCount(baseAddr, 1U, (uint32_t)(lifeTimeCount >> 32U));
237 }
238
239 /*FUNCTION**********************************************************************
240  *
241  * Function Name : PIT_DRV_ReadLifetimeTimerUs
242  * Description   : Read current lifetime value in microseconds unit.
243  * Return an absolute time stamp in the unit of microseconds. The time stamp 
244  * value will not exceed the timer period. Also, the timer is up-counting.
245  *
246  *END**************************************************************************/
247 uint64_t PIT_DRV_ReadLifetimeTimerUs(uint32_t instance)
248 {
249     assert(instance < HW_PIT_INSTANCE_COUNT);
250
251     uint32_t baseAddr = g_pitBaseAddr[instance];
252     /* Get current lifetime timer count, and reverse it to up-counting.*/
253     uint64_t currentTime = (~PIT_HAL_ReadLifetimeTimerCount(baseAddr));
254
255     /* Convert count numbers to microseconds unit.*/
256     /* Note: using currentTime * 1000 rather than 1000000 to avoid short time overflow. */
257     return currentTime = (currentTime * 1000U) / (pitSourceClock / 1000U);
258 }
259 #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER*/
260
261 /*******************************************************************************
262  * EOF
263  ******************************************************************************/
264