S32 SDK
pdb_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
19 #include "pdb_driver.h"
20 #include "pdb_hw_access.h"
21 #include "interrupt_manager.h"
22 
42 /*******************************************************************************
43  * Variables
44  ******************************************************************************/
45 
46 /* Table of base addresses for PDB instances. */
48 
49 /* Table to save PDB IRQ enum numbers. */
51 
52 /*FUNCTION*********************************************************************
53  *
54  * Function Name : PDB_DRV_Init
55  * Description : Initialize the PDB counter and trigger input for PDB module.
56  * It resets PDB registers and enables the clock for PDB. So it should be
57  * called before any operation to PDB module. After initialized, the PDB can
58  * ack as a triggered timer, which lays the foundation for other features in
59  * PDB module.
60  *
61  * Implements : PDB_DRV_Init_Activity
62  *END*************************************************************************/
63 void PDB_DRV_Init(const uint32_t instance,
64  const pdb_timer_config_t * const userConfigPtr)
65 {
66  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
67  DEV_ASSERT(userConfigPtr != NULL);
68  /* Table of PDB clock names as defined in clock manager. */
69  static const clock_names_t s_pdbClkNames[PDB_INSTANCE_COUNT] = {PDB0_CLK, PDB1_CLK};
70 
71  status_t clkStatus;
72 
73  PDB_Type * base = s_pdbBase[instance];
74 
75  clock_names_t instanceClkName = s_pdbClkNames[instance];
76 
77  /* Get the PDB clock as configured in the clock manager */
78  clkStatus = CLOCK_SYS_GetFreq(instanceClkName, NULL);
79 
80  /* Exit if clock is not enabled for current instance. */
81  DEV_ASSERT(clkStatus == STATUS_SUCCESS);
82  (void)clkStatus;
83 
84  /* Reset the registers for PDB module to reset state. */
85  PDB_Init(base);
86  PDB_Enable(base);
87  PDB_ConfigTimer(base, userConfigPtr);
88 
89  /* Configure NVIC. */
90  if (userConfigPtr->intEnable || userConfigPtr->seqErrIntEnable)
91  {
92  INT_SYS_EnableIRQ(s_pdbIrqId[instance]); /* Enable PDB interrupt in NVIC level.*/
93  }
94  else
95  {
96  INT_SYS_DisableIRQ(s_pdbIrqId[instance]); /* Disable PDB interrupt in NVIC level.*/
97  }
98 }
99 
100 /*FUNCTION*********************************************************************
101  *
102  * Function Name : PDB_DRV_Deinit
103  * Description : De-initialize the PDB module.
104  * When the PDB module is not used. Calling this function would shutdown the
105  * PDB module and reduce the power consumption.
106  *
107  * Implements : PDB_DRV_Deinit_Activity
108  *END*************************************************************************/
109 void PDB_DRV_Deinit(const uint32_t instance)
110 {
111  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
112  PDB_Type * base = s_pdbBase[instance];
113 
114  INT_SYS_DisableIRQ(s_pdbIrqId[instance]);
115  PDB_Disable(base);
116 }
117 
118 /*FUNCTION*********************************************************************
119  *
120  * Function Name : PDB_DRV_SoftTriggerCmd
121  * Description : Trigger PDB by software trigger.
122  * When the PDB is set to use software trigger as input, Calling this function
123  * would trigger the PDB.
124  *
125  * Implements : PDB_DRV_SoftTriggerCmd_Activity
126  *END*************************************************************************/
127 void PDB_DRV_SoftTriggerCmd(const uint32_t instance)
128 {
129  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
130  PDB_Type * base = s_pdbBase[instance];
131 
132  PDB_SetSoftTriggerCmd(base);
133 }
134 
135 /*FUNCTION*********************************************************************
136  *
137  * Function Name : PDB_DRV_GetTimerValue
138  * Description : Get the current counter value in PDB module.
139  *
140  * Implements : PDB_DRV_GetTimerValue_Activity
141  *END*************************************************************************/
142 uint32_t PDB_DRV_GetTimerValue(const uint32_t instance)
143 {
144  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
145  PDB_Type const * base = s_pdbBase[instance];
146 
147  return PDB_GetTimerValue(base);
148 }
149 
150 /*FUNCTION*********************************************************************
151  *
152  * Function Name : PDB_DRV_GetTimerIntFlag
153  * Description : Get the interrupt flag for PDB module. It will be
154  * asserted if the PDB interrupt occurs.
155  *
156  * Implements : PDB_DRV_GetTimerIntFlag_Activity
157  *END*************************************************************************/
158 bool PDB_DRV_GetTimerIntFlag(const uint32_t instance)
159 {
160  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
161  PDB_Type const * base = s_pdbBase[instance];
162 
163  return PDB_GetTimerIntFlag(base);
164 }
165 
166 /*FUNCTION*********************************************************************
167  *
168  * Function Name : PDB_DRV_ClearTimerIntFlag
169  * Description : Clear the interrupt flag for PDB module.
170  *
171  * Implements : PDB_DRV_ClearTimerIntFlag_Activity
172  *END*************************************************************************/
173 void PDB_DRV_ClearTimerIntFlag(const uint32_t instance)
174 {
175  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
176  PDB_Type * base = s_pdbBase[instance];
177 
178  PDB_ClearTimerIntFlag(base);
179 }
180 
181 /*FUNCTION*********************************************************************
182  *
183  * Function Name : PDB_DRV_LoadValuesCmd
184  * Description : Execute the command of loading values.
185  *
186  * Implements : PDB_DRV_LoadValuesCmd_Activity
187  *END*************************************************************************/
188 void PDB_DRV_LoadValuesCmd(const uint32_t instance)
189 {
190  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
191  PDB_Type * base = s_pdbBase[instance];
192 
193  PDB_SetLoadValuesCmd(base);
194 }
195 
196 /*FUNCTION*********************************************************************
197  *
198  * Function Name : PDB_DRV_SetTimerModulusValue
199  * Description : Set the value of timer modulus.
200  *
201  * Implements : PDB_DRV_SetTimerModulusValue_Activity
202  *END*************************************************************************/
203 void PDB_DRV_SetTimerModulusValue(const uint32_t instance,
204  const uint32_t value)
205 {
206  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
207  PDB_Type * base = s_pdbBase[instance];
208 
209  PDB_SetTimerModulusValue(base, value);
210 }
211 
212 /*FUNCTION*********************************************************************
213  *
214  * Function Name : PDB_DRV_SetValueForTimerInterrupt
215  * Description : Set the value for the timer interrupt.
216  *
217  * Implements : PDB_DRV_SetValueForTimerInterrupt_Activity
218  *END*************************************************************************/
219 void PDB_DRV_SetValueForTimerInterrupt(const uint32_t instance,
220  const uint32_t value)
221 {
222  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
223  PDB_Type * base = s_pdbBase[instance];
224 
225  PDB_SetValueForTimerInterrupt(base, value);
226 }
227 
228 /*FUNCTION*********************************************************************
229  *
230  * Function Name : PDB_DRV_ConfigAdcPreTrigger
231  * Description : Configure the ADC pre_trigger in the PDB module.
232  *
233  * Implements : PDB_DRV_ConfigAdcPreTrigger_Activity
234  *END*************************************************************************/
235 void PDB_DRV_ConfigAdcPreTrigger(const uint32_t instance,
236  const uint32_t chn,
237  const pdb_adc_pretrigger_config_t * configPtr)
238 {
239  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
241  DEV_ASSERT(configPtr != NULL);
242  PDB_Type * base = s_pdbBase[instance];
243 
244  PDB_SetAdcPreTriggerEnable(base, chn, ((uint32_t)1U << (configPtr->adcPreTriggerIdx)), configPtr->preTriggerEnable);
245  PDB_SetAdcPreTriggerOutputEnable(base, chn, ((uint32_t)1U << (configPtr->adcPreTriggerIdx)), configPtr->preTriggerOutputEnable);
246  PDB_SetAdcPreTriggerBackToBackEnable(base, chn, ((uint32_t)1U << (configPtr->adcPreTriggerIdx)), configPtr->preTriggerBackToBackEnable);
247 }
248 
249 /*FUNCTION*********************************************************************
250  *
251  * Function Name : PDB_DRV_GetAdcPreTriggerFlags
252  * Description : Get the ADC pre_trigger flag in the PDB module.
253  *
254  * Implements : PDB_DRV_GetAdcPreTriggerFlags_Activity
255  *END*************************************************************************/
256 uint32_t PDB_DRV_GetAdcPreTriggerFlags(const uint32_t instance,
257  const uint32_t chn,
258  const uint32_t preChnMask)
259 {
260  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
262  PDB_Type const * base = s_pdbBase[instance];
263 
264  return PDB_GetAdcPreTriggerFlags(base, chn, preChnMask);
265 }
266 
267 /*FUNCTION*********************************************************************
268  *
269  * Function Name : PDB_DRV_ClearAdcPreTriggerFlags
270  * Description : Clear the ADC pre_trigger flag in the PDB module.
271  *
272  * Implements : PDB_DRV_ClearAdcPreTriggerFlags_Activity
273  *END*************************************************************************/
274 void PDB_DRV_ClearAdcPreTriggerFlags(const uint32_t instance,
275  const uint32_t chn,
276  const uint32_t preChnMask)
277 {
278  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
280  PDB_Type * base = s_pdbBase[instance];
281 
282  PDB_ClearAdcPreTriggerFlags(base, chn, preChnMask);
283 }
284 
285 /*FUNCTION*********************************************************************
286  *
287  * Function Name : PDB_DRV_GetAdcPreTriggerSeqErrFlags
288  * Description : Get the ADC pre_trigger flag in the PDB module.
289  *
290  * Implements : PDB_DRV_GetAdcPreTriggerSeqErrFlags_Activity
291  *END*************************************************************************/
292 uint32_t PDB_DRV_GetAdcPreTriggerSeqErrFlags(const uint32_t instance,
293  const uint32_t chn,
294  const uint32_t preChnMask)
295 {
296  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
298  PDB_Type const * base = s_pdbBase[instance];
299 
300  return PDB_GetAdcPreTriggerSeqErrFlags(base, chn, preChnMask);
301 }
302 
303 /*FUNCTION*********************************************************************
304  *
305  * Function Name : PDB_DRV_ClearAdcPreTriggerSeqErrFlags
306  * Description : Clear the ADC pre_trigger flag in the PDB module.
307  *
308  * Implements : PDB_DRV_ClearAdcPreTriggerSeqErrFlags_Activity
309  *END*************************************************************************/
310 void PDB_DRV_ClearAdcPreTriggerSeqErrFlags(const uint32_t instance,
311  const uint32_t chn,
312  const uint32_t preChnMask)
313 {
314  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
316  PDB_Type * base = s_pdbBase[instance];
317 
318  PDB_ClearAdcPreTriggerSeqErrFlags(base, chn, preChnMask);
319 }
320 
321 /*FUNCTION*********************************************************************
322  *
323  * Function Name : PDB_DRV_SetAdcPreTriggerDelayValue
324  * Description : Set the ADC pre_trigger delay value in the PDB module.
325  *
326  * Implements : PDB_DRV_SetAdcPreTriggerDelayValue_Activity
327  *END*************************************************************************/
328 void PDB_DRV_SetAdcPreTriggerDelayValue(const uint32_t instance,
329  const uint32_t chn,
330  const uint32_t preChn,
331  const uint32_t value)
332 {
333  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
335  PDB_Type * base = s_pdbBase[instance];
336 
337  PDB_SetAdcPreTriggerDelayValue(base, chn, preChn, value);
338 }
339 
340 /*FUNCTION*********************************************************************
341  *
342  * Function Name : PDB_DRV_SetCmpPulseOutEnable
343  * Description : Switch on/off the CMP pulse out in the PDB module.
344  *
345  * Implements : PDB_DRV_SetCmpPulseOutEnable_Activity
346  *END*************************************************************************/
347 void PDB_DRV_SetCmpPulseOutEnable(const uint32_t instance,
348  const uint32_t pulseChnMask,
349  const bool enable)
350 {
351  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
352  PDB_Type * base = s_pdbBase[instance];
353 
354  PDB_SetCmpPulseOutEnable(base, pulseChnMask, enable);
355 }
356 
357 /*FUNCTION*********************************************************************
358  *
359  * Function Name : PDB_DRV_SetCmpPulseOutDelayForHigh
360  * Description : Set the CMP pulse out delay value for high in the PDB module.
361  *
362  * Implements : PDB_DRV_SetCmpPulseOutDelayForHigh_Activity
363  *END*************************************************************************/
364 void PDB_DRV_SetCmpPulseOutDelayForHigh(const uint32_t instance,
365  const uint32_t pulseChn,
366  const uint32_t value)
367 {
368  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
370  PDB_Type * base = s_pdbBase[instance];
371 
372  PDB_SetCmpPulseOutDelayForHigh(base, pulseChn, value);
373 }
374 
375 /*FUNCTION*********************************************************************
376  *
377  * Function Name : PDB_DRV_SetCmpPulseOutDelayForLow
378  * Description : Set the CMP pulse out delay value for low in the PDB module.
379  *
380  * Implements : PDB_DRV_SetCmpPulseOutDelayForLow_Activity
381  *END*************************************************************************/
382 void PDB_DRV_SetCmpPulseOutDelayForLow(const uint32_t instance,
383  const uint32_t pulseChn,
384  const uint32_t value)
385 {
386  DEV_ASSERT(instance < PDB_INSTANCE_COUNT);
388  PDB_Type * base = s_pdbBase[instance];
389 
390  PDB_SetCmpPulseOutDelayForLow(base, pulseChn, value);
391 }
392 
393 /*******************************************************************************
394  * EOF
395  ******************************************************************************/
void PDB_DRV_ClearTimerIntFlag(const uint32_t instance)
Clears the interrupt flag.
Definition: pdb_driver.c:173
void PDB_DRV_SetCmpPulseOutEnable(const uint32_t instance, const uint32_t pulseChnMask, const bool enable)
Switches on/off the CMP pulse out in the PDB module.
Definition: pdb_driver.c:347
void PDB_DRV_SetAdcPreTriggerDelayValue(const uint32_t instance, const uint32_t chn, const uint32_t preChn, const uint32_t value)
Sets the ADC pre_trigger delay value in the PDB module.
Definition: pdb_driver.c:328
void PDB_DRV_SetTimerModulusValue(const uint32_t instance, const uint32_t value)
Sets the value of timer modulus.
Definition: pdb_driver.c:203
void PDB_DRV_ConfigAdcPreTrigger(const uint32_t instance, const uint32_t chn, const pdb_adc_pretrigger_config_t *configPtr)
Configures the ADC pre_trigger in the PDB module.
Definition: pdb_driver.c:235
void PDB_DRV_SetValueForTimerInterrupt(const uint32_t instance, const uint32_t value)
Sets the value for the timer interrupt.
Definition: pdb_driver.c:219
uint32_t PDB_DRV_GetAdcPreTriggerSeqErrFlags(const uint32_t instance, const uint32_t chn, const uint32_t preChnMask)
Gets the ADC pre_trigger flag in the PDB module.
Definition: pdb_driver.c:292
uint32_t PDB_DRV_GetAdcPreTriggerFlags(const uint32_t instance, const uint32_t chn, const uint32_t preChnMask)
Gets the ADC pre_trigger flag in the PDB module.
Definition: pdb_driver.c:256
Defines the type of structure for basic timer in PDB.
Definition: pdb_driver.h:120
bool PDB_DRV_GetTimerIntFlag(const uint32_t instance)
Gets the PDB interrupt flag.
Definition: pdb_driver.c:158
void INT_SYS_DisableIRQ(IRQn_Type irqNumber)
Disables an interrupt for a given IRQ number.
#define PDB_IRQS
Definition: S32K142.h:8206
#define DEV_ASSERT(x)
Definition: devassert.h:77
#define FEATURE_PDB_PODLY_COUNT
uint32_t PDB_DRV_GetTimerValue(const uint32_t instance)
Gets the current counter value in the PDB module.
Definition: pdb_driver.c:142
void PDB_DRV_SetCmpPulseOutDelayForLow(const uint32_t instance, const uint32_t pulseChn, const uint32_t value)
Sets the CMP pulse out delay value for low in the PDB module.
Definition: pdb_driver.c:382
clock_names_t
Clock names.
#define PDB_BASE_PTRS
Definition: S32K142.h:8200
void PDB_DRV_ClearAdcPreTriggerSeqErrFlags(const uint32_t instance, const uint32_t chn, const uint32_t preChnMask)
Clears the ADC pre_trigger flag in the PDB module.
Definition: pdb_driver.c:310
status_t CLOCK_SYS_GetFreq(clock_names_t clockName, uint32_t *frequency)
Gets the clock frequency for a specific clock name.
void PDB_DRV_ClearAdcPreTriggerFlags(const uint32_t instance, const uint32_t chn, const uint32_t preChnMask)
Clears the ADC pre_trigger flag in the PDB module.
Definition: pdb_driver.c:274
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
void PDB_DRV_SoftTriggerCmd(const uint32_t instance)
Triggers the PDB with a software trigger.
Definition: pdb_driver.c:127
void PDB_DRV_LoadValuesCmd(const uint32_t instance)
Executes the command of loading values.
Definition: pdb_driver.c:188
void PDB_DRV_Deinit(const uint32_t instance)
De-initializes the PDB module.
Definition: pdb_driver.c:109
static const IRQn_Type s_pdbIrqId[PDB_INSTANCE_COUNT]
Definition: pdb_driver.c:50
void PDB_DRV_SetCmpPulseOutDelayForHigh(const uint32_t instance, const uint32_t pulseChn, const uint32_t value)
Sets the CMP pulse out delay value for high in the PDB module.
Definition: pdb_driver.c:364
static PDB_Type *const s_pdbBase[PDB_INSTANCE_COUNT]
Definition: pdb_driver.c:47
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
void PDB_DRV_Init(const uint32_t instance, const pdb_timer_config_t *const userConfigPtr)
Initializes the PDB counter and triggers input.
Definition: pdb_driver.c:63
#define FEATURE_PDB_ADC_CHANNEL_COUNT
Defines the type of structure for configuring ADC's pre_trigger.
Definition: pdb_driver.h:137
#define PDB_INSTANCE_COUNT
Definition: S32K142.h:8185
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K142.h:192