aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/drivers/src/pdb/fsl_pdb_driver.c
blob: 750ea3dc4080b6e6170d24c36d03b58fdfd8ee29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>
#include "fsl_pdb_driver.h"
#include "fsl_clock_manager.h"
#include "fsl_interrupt_manager.h"
#if FSL_FEATURE_SOC_PDB_COUNT

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_Init
 * Description   : Initialize the PDB counter and trigger input for PDB module.
 * It resets PDB registers and enables the clock for PDB. So it should be 
 * called before any operation to PDB module. After initialized, the PDB can
 * ack as a triggered timer, which lays the foundation for other features in
 * PDB module.
 *
 *END*************************************************************************/
pdb_status_t PDB_DRV_Init(uint32_t instance, const pdb_timer_config_t *userConfigPtr)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    if (!userConfigPtr)
    {
        return kStatus_PDB_InvalidArgument;
    }
    /* Enable the clock gate from clock manager. */
    CLOCK_SYS_EnablePdbClock(instance);

    /* Reset the registers for PDB module to reset state. */
    PDB_HAL_Init(base);
    PDB_HAL_Enable(base);
    PDB_HAL_ConfigTimer(base, userConfigPtr);

    /* Configure NVIC. */
    if (userConfigPtr->intEnable)
    {
        INT_SYS_EnableIRQ(g_pdbIrqId[instance] );/* Enable PDB interrupt in NVIC level.*/
    }
    else
    {
        INT_SYS_DisableIRQ(g_pdbIrqId[instance] );/* Disable PDB interrupt in NVIC level.*/
    }

    return kStatus_PDB_Success;
}


/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_Deinit
 * Description   : De-initialize the PDB module.
 * When the PDB module is not used. Calling this function would shutdown the 
 * PDB module and reduce the power consumption.
 *
 *END*************************************************************************/
pdb_status_t PDB_DRV_Deinit(uint32_t instance)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    INT_SYS_DisableIRQ( g_pdbIrqId[instance] );
    PDB_HAL_Disable(base);
    CLOCK_SYS_DisablePdbClock(instance);

    return kStatus_PDB_Success;
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SoftTriggerCmd
 * Description   : Trigger PDB by software trigger. 
 * When the PDB is set to use software trigger as input, Calling this function
 * would trigger the PDB.
 *
 *END*************************************************************************/
void PDB_DRV_SoftTriggerCmd(uint32_t instance)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    PDB_HAL_SetSoftTriggerCmd(base);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_GetTimerValue
 * Description   : Get the current counter value in PDB module.
 *
 *END*************************************************************************/
uint32_t PDB_DRV_GetTimerValue(uint32_t instance)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    return PDB_HAL_GetTimerValue(base);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_GetTimerIntFlag
 * Description   : Get the interrupt flag for PDB module. It will be
 * asserted if the PDB interrupt occurs.
 *
 *END*************************************************************************/
bool PDB_DRV_GetTimerIntFlag(uint32_t instance)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    return PDB_HAL_GetTimerIntFlag(base);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_ClearTimerIntFlag
 * Description   : Clear the interrupt flag for PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_ClearTimerIntFlag(uint32_t instance)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    PDB_HAL_ClearTimerIntFlag(base);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_LoadValuesCmd
 * Description   : Execute the command of loading values.
 *
 *END*************************************************************************/
void PDB_DRV_LoadValuesCmd(uint32_t instance)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    PDB_HAL_SetLoadValuesCmd(base);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetTimerModulusValue
 * Description   : Set the value of timer modulus.
 *
 *END*************************************************************************/
void PDB_DRV_SetTimerModulusValue(uint32_t instance, uint32_t value)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetTimerModulusValue(base, value);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetValueForTimerInterrupt
 * Description   : Set the value for the timer interrupt.
 *
 *END*************************************************************************/
void PDB_DRV_SetValueForTimerInterrupt(uint32_t instance, uint32_t value)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetValueForTimerInterrupt(base, value);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_ConfigAdcPreTrigger
 * Description   : Configure the ADC pre_trigger in the PDB module.
 *
 *END*************************************************************************/
pdb_status_t PDB_DRV_ConfigAdcPreTrigger(uint32_t instance, uint32_t chn, const pdb_adc_pretrigger_config_t *configPtr)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    if (!configPtr)
    {
        return kStatus_PDB_InvalidArgument; 
    }
    
    PDB_HAL_SetAdcPreTriggerEnable(base, chn, 1U << (configPtr->adcPreTriggerIdx), configPtr->preTriggerEnable);
    PDB_HAL_SetAdcPreTriggerOutputEnable(base, chn, 1U << (configPtr->adcPreTriggerIdx), configPtr->preTriggerOutputEnable);
    PDB_HAL_SetAdcPreTriggerBackToBackEnable(base, chn, 1U << (configPtr->adcPreTriggerIdx), configPtr->preTriggerBackToBackEnable);
    
    return kStatus_PDB_Success;
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_GetAdcPreTriggerFlags
 * Description   : Get the ADC pre_trigger flag in the PDB module.
 *
 *END*************************************************************************/
uint32_t PDB_DRV_GetAdcPreTriggerFlags(uint32_t instance, uint32_t chn, uint32_t preChnMask)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    return PDB_HAL_GetAdcPreTriggerFlags(base, chn, preChnMask);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_ClearAdcPreTriggerFlags
 * Description   : Clear the ADC pre_trigger flag in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_ClearAdcPreTriggerFlags(uint32_t instance, uint32_t chn, uint32_t preChnMask)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_ClearAdcPreTriggerFlags(base, chn, preChnMask);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_GetAdcPreTriggerSeqErrFlags
 * Description   : Get the ADC pre_trigger flag in the PDB module.
 *
 *END*************************************************************************/
uint32_t PDB_DRV_GetAdcPreTriggerSeqErrFlags(uint32_t instance, uint32_t chn, uint32_t preChnMask)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    return PDB_HAL_GetAdcPreTriggerSeqErrFlags(base, chn, preChnMask);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_ClearAdcPreTriggerSeqErrFlags
 * Description   : Clear the ADC pre_trigger flag in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_ClearAdcPreTriggerSeqErrFlags(uint32_t instance, uint32_t chn, uint32_t preChnMask)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];

    PDB_HAL_ClearAdcPreTriggerSeqErrFlags(base, chn, preChnMask);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetAdcPreTriggerDelayValue
 * Description   : Set the ADC pre_trigger delay value in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_SetAdcPreTriggerDelayValue(uint32_t instance, uint32_t chn, uint32_t preChn, uint32_t value)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetAdcPreTriggerDelayValue(base, chn, preChn, value);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_ConfigDacInterval
 * Description   : Configure the DAC interval in the PDB module.
 *
 *END*************************************************************************/
pdb_status_t PDB_DRV_ConfigDacInterval(uint32_t instance, uint32_t dacChn, const pdb_dac_interval_config_t *configPtr)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    if (!configPtr)
    {
        return kStatus_PDB_InvalidArgument; 
    }
    
    PDB_HAL_SetDacIntervalTriggerEnable(base, dacChn, configPtr->intervalTriggerEnable);
    PDB_HAL_SetDacExtTriggerInputEnable(base, dacChn, configPtr->extTriggerInputEnable);
    
    return kStatus_PDB_Success;
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetDacIntervalValue
 * Description   : Set the DAC interval value in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_SetDacIntervalValue(uint32_t instance, uint32_t dacChn, uint32_t value)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetDacIntervalValue(base, dacChn, value);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetCmpPulseOutEnable
 * Description   : Switch on/off the CMP pulse out in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_SetCmpPulseOutEnable(uint32_t instance, uint32_t pulseChnMask, bool enable)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetCmpPulseOutEnable(base, pulseChnMask, enable);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetCmpPulseOutDelayForHigh
 * Description   : Set the CMP pulse out delay value for high in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_SetCmpPulseOutDelayForHigh(uint32_t instance, uint32_t pulseChn, uint32_t value)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetCmpPulseOutDelayForHigh(base, pulseChn, value);
}

/*FUNCTION*********************************************************************
 *
 * Function Name : PDB_DRV_SetCmpPulseOutDelayForLow
 * Description   : Set the CMP pulse out delay value for low in the PDB module.
 *
 *END*************************************************************************/
void PDB_DRV_SetCmpPulseOutDelayForLow(uint32_t instance, uint32_t pulseChn, uint32_t value)
{
    assert(instance < PDB_INSTANCE_COUNT);
    PDB_Type * base = g_pdbBase[instance];
    
    PDB_HAL_SetCmpPulseOutDelayForLow(base, pulseChn, value);
}
#endif

/*******************************************************************************
 * EOF
 ******************************************************************************/