1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
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 the copyright holder 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_pdb.h"
32 
33 /*******************************************************************************
34  * Prototypes
35  ******************************************************************************/
36 /*!
37  * @brief Get instance number for PDB module.
38  *
39  * @param base PDB peripheral base address
40  */
41 static uint32_t PDB_GetInstance(PDB_Type *base);
42 
43 /*******************************************************************************
44  * Variables
45  ******************************************************************************/
46 /*! @brief Pointers to PDB bases for each instance. */
47 static PDB_Type *const s_pdbBases[] = PDB_BASE_PTRS;
48 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
49 /*! @brief Pointers to PDB clocks for each instance. */
50 static const clock_ip_name_t s_pdbClocks[] = PDB_CLOCKS;
51 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
52 
53 /*******************************************************************************
54  * Codes
55  ******************************************************************************/
PDB_GetInstance(PDB_Type * base)56 static uint32_t PDB_GetInstance(PDB_Type *base)
57 {
58     uint32_t instance;
59 
60     /* Find the instance index from base address mappings. */
61     for (instance = 0; instance < ARRAY_SIZE(s_pdbBases); instance++)
62     {
63         if (s_pdbBases[instance] == base)
64         {
65             break;
66         }
67     }
68 
69     assert(instance < ARRAY_SIZE(s_pdbBases));
70 
71     return instance;
72 }
73 
PDB_Init(PDB_Type * base,const pdb_config_t * config)74 void PDB_Init(PDB_Type *base, const pdb_config_t *config)
75 {
76     assert(NULL != config);
77 
78     uint32_t tmp32;
79 
80 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
81     /* Enable the clock. */
82     CLOCK_EnableClock(s_pdbClocks[PDB_GetInstance(base)]);
83 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
84 
85     /* Configure. */
86     /* PDBx_SC. */
87     tmp32 = base->SC &
88             ~(PDB_SC_LDMOD_MASK | PDB_SC_PRESCALER_MASK | PDB_SC_TRGSEL_MASK | PDB_SC_MULT_MASK | PDB_SC_CONT_MASK);
89 
90     tmp32 |= PDB_SC_LDMOD(config->loadValueMode) | PDB_SC_PRESCALER(config->prescalerDivider) |
91              PDB_SC_TRGSEL(config->triggerInputSource) | PDB_SC_MULT(config->dividerMultiplicationFactor);
92     if (config->enableContinuousMode)
93     {
94         tmp32 |= PDB_SC_CONT_MASK;
95     }
96     base->SC = tmp32;
97 
98     PDB_Enable(base, true); /* Enable the PDB module. */
99 }
100 
PDB_Deinit(PDB_Type * base)101 void PDB_Deinit(PDB_Type *base)
102 {
103     PDB_Enable(base, false); /* Disable the PDB module. */
104 
105 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
106     /* Disable the clock. */
107     CLOCK_DisableClock(s_pdbClocks[PDB_GetInstance(base)]);
108 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
109 }
110 
PDB_GetDefaultConfig(pdb_config_t * config)111 void PDB_GetDefaultConfig(pdb_config_t *config)
112 {
113     assert(NULL != config);
114 
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 }
121 
122 #if defined(FSL_FEATURE_PDB_HAS_DAC) && FSL_FEATURE_PDB_HAS_DAC
PDB_SetDACTriggerConfig(PDB_Type * base,uint32_t channel,pdb_dac_trigger_config_t * config)123 void PDB_SetDACTriggerConfig(PDB_Type *base, uint32_t channel, pdb_dac_trigger_config_t *config)
124 {
125     assert(channel < PDB_INTC_COUNT);
126     assert(NULL != config);
127 
128     uint32_t tmp32 = 0U;
129 
130     /* PDBx_DACINTC. */
131     if (config->enableExternalTriggerInput)
132     {
133         tmp32 |= PDB_INTC_EXT_MASK;
134     }
135     if (config->enableIntervalTrigger)
136     {
137         tmp32 |= PDB_INTC_TOE_MASK;
138     }
139     base->DAC[channel].INTC = tmp32;
140 }
141 #endif /* FSL_FEATURE_PDB_HAS_DAC */
142