S32 SDK
pdb_hal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016 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_hal.h"
20 
31 /*FUNCTION*********************************************************************
32  *
33  * Function Name : PDB_HAL_Init
34  * Description : Reset PDB's registers to a known state. This state is
35  * defined in Reference Manual, which is power on reset value.
36  *
37  * Implements : PDB_HAL_Init_Activity
38  *END*************************************************************************/
39 void PDB_HAL_Init(PDB_Type * const base)
40 {
41  DEV_ASSERT(base != NULL);
42  uint32_t chn, preChn;
43 
44  base->SC = 0U;
45  PDB_HAL_Enable(base);
46  base->MOD = 0xFFFFU;
47  base->IDLY = 0xFFFFU;
48  /* For ADC trigger. */
49  for (chn = 0U; chn < FEATURE_PDB_ADC_CHANNEL_COUNT; chn++)
50  {
51  base->CH[chn].C1 = 0U;
52  base->CH[chn].S = 0U;
53  for (preChn = 0U; preChn < FEATURE_PDB_ADC_PRE_CHANNEL_COUNT; preChn++)
54  {
55  PDB_HAL_SetAdcPreTriggerDelayValue(base, chn, preChn, 0U);
56  }
57  }
58  /* For Pulse out trigger. */
59  base->POEN = 0U;
60  for (chn = 0U; chn < FEATURE_PDB_PODLY_COUNT; chn++)
61  {
62  base->POnDLY[chn].PODLY = 0U;
63  }
64  /* Load the setting value. */
66  PDB_HAL_Disable(base);
67 }
68 
69 /*FUNCTION*********************************************************************
70  *
71  * Function Name : PDB_HAL_ConfigTimer
72  * Description : Configure the PDB timer.
73  *
74  * Implements : PDB_HAL_ConfigTimer_Activity
75  *END*************************************************************************/
76 void PDB_HAL_ConfigTimer(PDB_Type * const base, const pdb_timer_config_t *configPtr)
77 {
78  DEV_ASSERT(base != NULL);
79  DEV_ASSERT(configPtr != NULL);
80  uint32_t sc;
81 
82  sc = base->SC;
83  sc &= ~( (uint32_t)PDB_SC_LDMOD_MASK
84  | (uint32_t)PDB_SC_PDBEIE_MASK
85  | (uint32_t)PDB_SC_PRESCALER_MASK
86  | (uint32_t)PDB_SC_TRGSEL_MASK
87  | (uint32_t)PDB_SC_MULT_MASK
88  | (uint32_t)PDB_SC_CONT_MASK
89  | (uint32_t)PDB_SC_DMAEN_MASK
90  | (uint32_t)PDB_SC_PDBIE_MASK
91  );
92 
93  sc |= PDB_SC_LDMOD((uint32_t)(configPtr->loadValueMode));
94  if (configPtr->seqErrIntEnable)
95  {
96  sc |= PDB_SC_PDBEIE_MASK;
97  }
98  sc |= PDB_SC_PRESCALER((uint32_t)(configPtr->clkPreDiv));
99  sc |= PDB_SC_TRGSEL((uint32_t)(configPtr->triggerInput));
100  sc |= PDB_SC_MULT((uint32_t)(configPtr->clkPreMultFactor));
101  if (configPtr->continuousModeEnable)
102  {
103  sc |= PDB_SC_CONT_MASK;
104  }
105  if (configPtr->dmaEnable)
106  {
107  sc |= PDB_SC_DMAEN_MASK;
108  }
109  if (configPtr->intEnable)
110  {
111  sc |= PDB_SC_PDBIE_MASK;
112  }
113  base->SC = sc;
114 }
115 
116 /*FUNCTION*********************************************************************
117  *
118  * Function Name : PDB_HAL_SetAdcPreTriggerBackToBackEnable
119  * Description : Switch to enable pre-trigger's back to back mode.
120  *
121  * Implements : PDB_HAL_SetAdcPreTriggerBackToBackEnable_Activity
122  *END*************************************************************************/
123 void PDB_HAL_SetAdcPreTriggerBackToBackEnable(PDB_Type * const base, uint32_t chn, uint32_t preChnMask, bool enable)
124 {
125  DEV_ASSERT(base != NULL);
127 
128  uint32_t c1 = base->CH[chn].C1;
129  if (enable)
130  {
131  c1 |= PDB_C1_BB(preChnMask);
132  }
133  else
134  {
135  c1 &= ~PDB_C1_BB(preChnMask);
136  }
137  base->CH[chn].C1 = c1;
138 }
139 
140 /*FUNCTION*********************************************************************
141  *
142  * Function Name : PDB_HAL_SetAdcPreTriggerOutputEnable
143  * Description : Switch to enable pre-trigger's output.
144  *
145  * Implements : PDB_HAL_SetAdcPreTriggerOutputEnable_Activity
146  *END*************************************************************************/
147 void PDB_HAL_SetAdcPreTriggerOutputEnable(PDB_Type * const base, uint32_t chn, uint32_t preChnMask, bool enable)
148 {
149  DEV_ASSERT(base != NULL);
151 
152  uint32_t c1 = base->CH[chn].C1;
153  if (enable)
154  {
155  c1 |= PDB_C1_TOS(preChnMask);
156  }
157  else
158  {
159  c1 &= ~PDB_C1_TOS(preChnMask);
160  }
161  base->CH[chn].C1 = c1;
162 }
163 
164 /*FUNCTION*********************************************************************
165  *
166  * Function Name : PDB_HAL_SetAdcPreTriggerEnable
167  * Description : Switch to enable pre-trigger's.
168  *
169  * Implements : PDB_HAL_SetAdcPreTriggerEnable_Activity
170  *END*************************************************************************/
171 void PDB_HAL_SetAdcPreTriggerEnable(PDB_Type * const base, uint32_t chn, uint32_t preChnMask, bool enable)
172 {
173  DEV_ASSERT(base != NULL);
175 
176  uint32_t c1 = base->CH[chn].C1;
177  if (enable)
178  {
179  c1 |= PDB_C1_EN(preChnMask);
180  }
181  else
182  {
183  c1 &= ~PDB_C1_EN(preChnMask);
184  }
185  base->CH[chn].C1 = c1;
186 }
187 
188 /*FUNCTION*********************************************************************
189  *
190  * Function Name : PDB_HAL_ClearAdcPreTriggerFlags
191  * Description : Clear the flag that the PDB counter reaches to the
192  * pre-trigger's delay value.
193  *
194  * Implements : PDB_HAL_ClearAdcPreTriggerFlags_Activity
195  *END*************************************************************************/
196 void PDB_HAL_ClearAdcPreTriggerFlags(PDB_Type * const base, uint32_t chn, uint32_t preChnMask)
197 {
198  DEV_ASSERT(base != NULL);
200 
201  /* Write 0 to clear. */
202  uint32_t s = base->CH[chn].S;
203  s &= ~PDB_S_CF( preChnMask ); /* Update the change. */
204 
205  base->CH[chn].S = s;
206 }
207 
208 /*FUNCTION*********************************************************************
209  *
210  * Function Name : PDB_HAL_ClearAdcPreTriggerSeqErrFlags
211  * Description : Clear the flag that sequence error is detected.
212  *
213  * Implements : PDB_HAL_ClearAdcPreTriggerSeqErrFlags_Activity
214  *END*************************************************************************/
215 void PDB_HAL_ClearAdcPreTriggerSeqErrFlags(PDB_Type * const base, uint32_t chn, uint32_t preChnMask)
216 {
217  DEV_ASSERT(base != NULL);
219  volatile uint32_t dummy_read;
220 
221  /* Write 0 to clear. */
222  uint32_t s = base->CH[chn].S;
223  s &= ~PDB_S_ERR( preChnMask );
224 
225  base->CH[chn].S = s;
226 
227  /* This read-after-write guarantees that the write to clear operation is completed,
228  * for the case when memory write buffering is enabled. */
229  dummy_read = base->CH[chn].S;
230  (void) dummy_read;
231 }
232 
233 /*FUNCTION*********************************************************************
234  *
235  * Function Name : PDB_HAL_SetAdcPreTriggerDelayValue
236  * Description : Set the delay value for pre-trigger.
237  *
238  * Implements : PDB_HAL_SetAdcPreTriggerDelayValue_Activity
239  *END*************************************************************************/
240 void PDB_HAL_SetAdcPreTriggerDelayValue(PDB_Type * const base, uint32_t chn, uint32_t preChn, uint32_t value)
241 {
242  DEV_ASSERT(base != NULL);
245  base->CH[chn].DLY[preChn] = value;
246 }
247 
248 /*FUNCTION*********************************************************************
249  *
250  * Function Name : PDB_HAL_SetCmpPulseOutEnable
251  * Description : Switch to enable the pulse-out trigger.
252  *
253  * Implements : PDB_HAL_SetCmpPulseOutEnable_Activity
254  *END*************************************************************************/
255 void PDB_HAL_SetCmpPulseOutEnable(PDB_Type * const base, uint32_t pulseChnMask, bool enable)
256 {
257  DEV_ASSERT(base != NULL);
258  uint32_t poen = base->POEN;
259  if (enable)
260  {
261  poen |= PDB_POEN_POEN(pulseChnMask);
262  }
263  else
264  {
265  poen &= ~PDB_POEN_POEN(pulseChnMask);
266  }
267  base->POEN = poen;
268 }
269 
270 /******************************************************************************
271  * EOF
272  *****************************************************************************/
#define PDB_SC_TRGSEL_MASK
Definition: S32K144.h:8317
#define PDB_SC_PRESCALER_MASK
Definition: S32K144.h:8321
#define PDB_SC_DMAEN_MASK
Definition: S32K144.h:8325
void PDB_HAL_ClearAdcPreTriggerFlags(PDB_Type *const base, uint32_t chn, uint32_t preChnMask)
Clears the flag which indicates that the PDB counter has reached the pre-trigger delay value...
Definition: pdb_hal.c:196
__IO uint32_t MOD
Definition: S32K144.h:8240
#define PDB_POEN_POEN(x)
Definition: S32K144.h:8387
static void PDB_HAL_Disable(PDB_Type *const base)
Switches to disable the PDB module.
Definition: pdb_hal.h:220
#define PDB_C1_EN(x)
Definition: S32K144.h:8360
bool seqErrIntEnable
Definition: pdb_hal.h:148
void PDB_HAL_ClearAdcPreTriggerSeqErrFlags(PDB_Type *const base, uint32_t chn, uint32_t preChnMask)
Clears the flag which indicates that a sequence error has been detected.
Definition: pdb_hal.c:215
union PDB_Type::@17 POnDLY[PDB_POnDLY_COUNT]
#define PDB_S_CF(x)
Definition: S32K144.h:8377
#define PDB_S_ERR(x)
Definition: S32K144.h:8373
__IO uint32_t C1
Definition: S32K144.h:8244
__IO uint32_t POEN
Definition: S32K144.h:8249
#define FEATURE_PDB_ADC_PRE_CHANNEL_COUNT
Defines the type of structure for basic timer in PDB.
Definition: pdb_hal.h:145
#define DEV_ASSERT(x)
Definition: devassert.h:78
#define FEATURE_PDB_ADC_CHANNEL_COUNT
__IO uint32_t PODLY
Definition: S32K144.h:8251
struct PDB_Type::@16 CH[PDB_CH_COUNT]
void PDB_HAL_Init(PDB_Type *const base)
Resets the PDB registers to a known state.
Definition: pdb_hal.c:39
__IO uint32_t DLY[PDB_DLY_COUNT]
Definition: S32K144.h:8246
#define PDB_SC_LDMOD_MASK
Definition: S32K144.h:8337
pdb_clk_prescaler_mult_factor_t clkPreMultFactor
Definition: pdb_hal.h:150
pdb_load_value_mode_t loadValueMode
Definition: pdb_hal.h:147
#define FEATURE_PDB_PODLY_COUNT
#define PDB_SC_PDBEIE_MASK
Definition: S32K144.h:8333
void PDB_HAL_SetAdcPreTriggerDelayValue(PDB_Type *const base, uint32_t chn, uint32_t preChn, uint32_t value)
Sets the pre-trigger delay value.
Definition: pdb_hal.c:240
void PDB_HAL_SetAdcPreTriggerOutputEnable(PDB_Type *const base, uint32_t chn, uint32_t preChnMask, bool enable)
Switches to enable the pre-trigger output.
Definition: pdb_hal.c:147
void PDB_HAL_ConfigTimer(PDB_Type *const base, const pdb_timer_config_t *configPtr)
Configure the PDB timer.
Definition: pdb_hal.c:76
pdb_clk_prescaler_div_t clkPreDiv
Definition: pdb_hal.h:149
void PDB_HAL_SetCmpPulseOutEnable(PDB_Type *const base, uint32_t pulseChnMask, bool enable)
Switches to enable the pulse-out trigger.
Definition: pdb_hal.c:255
pdb_trigger_src_t triggerInput
Definition: pdb_hal.h:151
#define PDB_SC_LDMOD(x)
Definition: S32K144.h:8340
static void PDB_HAL_SetLoadValuesCmd(PDB_Type *const base)
Loads the delay registers value for the PDB module.
Definition: pdb_hal.h:279
#define PDB_C1_TOS(x)
Definition: S32K144.h:8364
#define PDB_SC_PRESCALER(x)
Definition: S32K144.h:8324
#define PDB_SC_PDBIE_MASK
Definition: S32K144.h:8305
__IO uint32_t SC
Definition: S32K144.h:8239
void PDB_HAL_SetAdcPreTriggerBackToBackEnable(PDB_Type *const base, uint32_t chn, uint32_t preChnMask, bool enable)
Switches to enable the pre-trigger back-to-back mode.
Definition: pdb_hal.c:123
#define PDB_SC_TRGSEL(x)
Definition: S32K144.h:8320
#define PDB_SC_CONT_MASK
Definition: S32K144.h:8297
void PDB_HAL_SetAdcPreTriggerEnable(PDB_Type *const base, uint32_t chn, uint32_t preChnMask, bool enable)
Switches to enable the pre-trigger.
Definition: pdb_hal.c:171
__IO uint32_t S
Definition: S32K144.h:8245
#define PDB_SC_MULT_MASK
Definition: S32K144.h:8301
#define PDB_C1_BB(x)
Definition: S32K144.h:8368
static void PDB_HAL_Enable(PDB_Type *const base)
Switches on to enable the PDB module.
Definition: pdb_hal.h:207
__IO uint32_t IDLY
Definition: S32K144.h:8242
bool continuousModeEnable
Definition: pdb_hal.h:152
#define PDB_SC_MULT(x)
Definition: S32K144.h:8304