1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_pdb.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.pdb"
14 #endif
15 
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 /*!
20  * @brief Get instance number for PDB module.
21  *
22  * @param base PDB peripheral base address
23  */
24 static uint32_t PDB_GetInstance(PDB_Type *base);
25 
26 /*******************************************************************************
27  * Variables
28  ******************************************************************************/
29 /*! @brief Pointers to PDB bases for each instance. */
30 static PDB_Type *const s_pdbBases[] = PDB_BASE_PTRS;
31 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
32 /*! @brief Pointers to PDB clocks for each instance. */
33 static const clock_ip_name_t s_pdbClocks[] = PDB_CLOCKS;
34 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
35 
36 /*******************************************************************************
37  * Codes
38  ******************************************************************************/
PDB_GetInstance(PDB_Type * base)39 static uint32_t PDB_GetInstance(PDB_Type *base)
40 {
41     uint32_t instance;
42 
43     /* Find the instance index from base address mappings. */
44     for (instance = 0; instance < ARRAY_SIZE(s_pdbBases); instance++)
45     {
46         if (s_pdbBases[instance] == base)
47         {
48             break;
49         }
50     }
51 
52     assert(instance < ARRAY_SIZE(s_pdbBases));
53 
54     return instance;
55 }
56 
57 /*!
58  * brief Initializes the PDB module.
59  *
60  * This function initializes the PDB module. The operations included are as follows.
61  *  - Enable the clock for PDB instance.
62  *  - Configure the PDB module.
63  *  - Enable the PDB module.
64  *
65  * param base PDB peripheral base address.
66  * param config Pointer to the configuration structure. See "pdb_config_t".
67  */
PDB_Init(PDB_Type * base,const pdb_config_t * config)68 void PDB_Init(PDB_Type *base, const pdb_config_t *config)
69 {
70     assert(NULL != config);
71 
72     uint32_t tmp32;
73 
74 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
75     /* Enable the clock. */
76     CLOCK_EnableClock(s_pdbClocks[PDB_GetInstance(base)]);
77 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
78 
79     /* Configure. */
80     /* PDBx_SC. */
81     tmp32 = base->SC &
82             ~(PDB_SC_LDMOD_MASK | PDB_SC_PRESCALER_MASK | PDB_SC_TRGSEL_MASK | PDB_SC_MULT_MASK | PDB_SC_CONT_MASK);
83 
84     tmp32 |= PDB_SC_LDMOD(config->loadValueMode) | PDB_SC_PRESCALER(config->prescalerDivider) |
85              PDB_SC_TRGSEL(config->triggerInputSource) | PDB_SC_MULT(config->dividerMultiplicationFactor);
86     if (config->enableContinuousMode)
87     {
88         tmp32 |= PDB_SC_CONT_MASK;
89     }
90     base->SC = tmp32;
91 
92     PDB_Enable(base, true); /* Enable the PDB module. */
93 }
94 
95 /*!
96  * brief De-initializes the PDB module.
97  *
98  * param base PDB peripheral base address.
99  */
PDB_Deinit(PDB_Type * base)100 void PDB_Deinit(PDB_Type *base)
101 {
102     PDB_Enable(base, false); /* Disable the PDB module. */
103 
104 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
105     /* Disable the clock. */
106     CLOCK_DisableClock(s_pdbClocks[PDB_GetInstance(base)]);
107 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
108 }
109 
110 /*!
111  * brief Initializes the PDB user configuration structure.
112  *
113  * This function initializes the user configuration structure to a default value. The default values are as follows.
114  * code
115  *   config->loadValueMode = kPDB_LoadValueImmediately;
116  *   config->prescalerDivider = kPDB_PrescalerDivider1;
117  *   config->dividerMultiplicationFactor = kPDB_DividerMultiplicationFactor1;
118  *   config->triggerInputSource = kPDB_TriggerSoftware;
119  *   config->enableContinuousMode = false;
120  * endcode
121  * param config Pointer to configuration structure. See "pdb_config_t".
122  */
PDB_GetDefaultConfig(pdb_config_t * config)123 void PDB_GetDefaultConfig(pdb_config_t *config)
124 {
125     assert(NULL != config);
126 
127     /* Initializes the configure structure to zero. */
128     (void)memset(config, 0, sizeof(*config));
129 
130     config->loadValueMode               = kPDB_LoadValueImmediately;
131     config->prescalerDivider            = kPDB_PrescalerDivider1;
132     config->dividerMultiplicationFactor = kPDB_DividerMultiplicationFactor1;
133     config->triggerInputSource          = kPDB_TriggerSoftware;
134     config->enableContinuousMode        = false;
135 }
136 
137 #if defined(FSL_FEATURE_PDB_HAS_DAC) && FSL_FEATURE_PDB_HAS_DAC
138 /*!
139  * brief Configures the DAC trigger in the PDB module.
140  *
141  * param base    PDB peripheral base address.
142  * param channel Channel index for DAC instance.
143  * param config  Pointer to the configuration structure. See "pdb_dac_trigger_config_t".
144  */
PDB_SetDACTriggerConfig(PDB_Type * base,pdb_dac_trigger_channel_t channel,pdb_dac_trigger_config_t * config)145 void PDB_SetDACTriggerConfig(PDB_Type *base, pdb_dac_trigger_channel_t channel, pdb_dac_trigger_config_t *config)
146 {
147     assert((uint8_t)channel < (uint8_t)FSL_FEATURE_PDB_DAC_INTERVAL_TRIGGER_COUNT);
148     assert(NULL != config);
149 
150     uint32_t tmp32 = 0U;
151 
152     /* PDBx_DACINTC. */
153     if (config->enableExternalTriggerInput)
154     {
155         tmp32 |= PDB_INTC_EXT_MASK;
156     }
157     if (config->enableIntervalTrigger)
158     {
159         tmp32 |= PDB_INTC_TOE_MASK;
160     }
161     base->DAC[channel].INTC = tmp32;
162 }
163 #endif /* FSL_FEATURE_PDB_HAS_DAC */
164