1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_dac32.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.dac32"
14 #endif
15 
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 /*!
20  * @brief Get instance number for DAC32 module.
21  *
22  * @param base DAC32 peripheral base address
23  */
24 static uint32_t DAC32_GetInstance(DAC_Type *base);
25 
26 /*******************************************************************************
27  * Variables
28  ******************************************************************************/
29 /*! @brief Pointers to DAC32 bases for each instance. */
30 static DAC_Type *const s_dac32Bases[] = DAC_BASE_PTRS;
31 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
32 /*! @brief Pointers to DAC32 clocks for each instance. */
33 static const clock_ip_name_t s_dac32Clocks[] = DAC_CLOCKS;
34 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
35 
36 /*******************************************************************************
37  * Codes
38  ******************************************************************************/
DAC32_GetInstance(DAC_Type * base)39 static uint32_t DAC32_GetInstance(DAC_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_dac32Bases); instance++)
45     {
46         if (s_dac32Bases[instance] == base)
47         {
48             break;
49         }
50     }
51 
52     assert(instance < ARRAY_SIZE(s_dac32Bases));
53 
54     return instance;
55 }
56 
57 /*!
58  * brief Initializes the DAC32 module.
59  *
60  * This function initializes the DAC32 module, including:
61  *  - Enabling the clock for DAC32 module.
62  *  - Configuring the DAC32 converter with a user configuration.
63  *  - Enabling the DAC32 module.
64  *
65  * param base DAC32 peripheral base address.
66  * param config Pointer to the configuration structure. See "dac32_config_t".
67  */
DAC32_Init(DAC_Type * base,const dac32_config_t * config)68 void DAC32_Init(DAC_Type *base, const dac32_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 before any operation to DAC32 registers.*/
76     CLOCK_EnableClock(s_dac32Clocks[DAC32_GetInstance(base)]);
77 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
78 
79     /* Configure. */
80     tmp32 = base->STATCTRL & ~(DAC_STATCTRL_DACRFS_MASK | DAC_STATCTRL_LPEN_MASK);
81     if (kDAC32_ReferenceVoltageSourceVref2 == config->referenceVoltageSource)
82     {
83         tmp32 |= DAC_STATCTRL_DACRFS_MASK;
84     }
85     if (config->enableLowPowerMode)
86     {
87         tmp32 |= DAC_STATCTRL_LPEN_MASK;
88     }
89     base->STATCTRL = tmp32;
90 
91     /* DAC32_Enable(base, true); */
92     /* Move this function to application, so that users can enable it when they will. */
93 }
94 
95 /*!
96  * brief De-initializes the DAC32 module.
97  *
98  * This function de-initializes the DAC32 module, including:
99  *  - Disabling the DAC32 module.
100  *  - Disabling the clock for the DAC32 module.
101  *
102  * param base DAC32 peripheral base address.
103  */
DAC32_Deinit(DAC_Type * base)104 void DAC32_Deinit(DAC_Type *base)
105 {
106     DAC32_Enable(base, false);
107 
108 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
109     /* Disable the clock. */
110     CLOCK_DisableClock(s_dac32Clocks[DAC32_GetInstance(base)]);
111 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
112 }
113 
114 /*!
115  * brief Initializes the DAC32 user configuration structure.
116  *
117  * This function initializes the user configuration structure to a default value. The default values are:
118  * code
119  *   config->referenceVoltageSource = kDAC32_ReferenceVoltageSourceVref2;
120  *   config->enableLowPowerMode = false;
121  * endcode
122  * param config Pointer to the configuration structure. See "dac32_config_t".
123  */
DAC32_GetDefaultConfig(dac32_config_t * config)124 void DAC32_GetDefaultConfig(dac32_config_t *config)
125 {
126     assert(NULL != config);
127 
128     /* Initializes the configure structure to zero. */
129     (void)memset(config, 0, sizeof(*config));
130 
131     config->referenceVoltageSource = kDAC32_ReferenceVoltageSourceVref2;
132     config->enableLowPowerMode     = false;
133 }
134 
135 /*!
136  * brief Configures the DAC32 buffer.
137  *
138  * param base   DAC32 peripheral base address.
139  * param config Pointer to the configuration structure. See "dac32_buffer_config_t".
140  */
DAC32_SetBufferConfig(DAC_Type * base,const dac32_buffer_config_t * config)141 void DAC32_SetBufferConfig(DAC_Type *base, const dac32_buffer_config_t *config)
142 {
143     assert(NULL != config);
144 
145     uint32_t tmp32;
146 
147     tmp32 = base->STATCTRL & ~(DAC_STATCTRL_DACTRGSEL_MASK | DAC_STATCTRL_DACBFMD_MASK | DAC_STATCTRL_DACBFUP_MASK |
148                                DAC_STATCTRL_DACBFWM_MASK);
149     if (kDAC32_BufferTriggerBySoftwareMode == config->triggerMode)
150     {
151         tmp32 |= DAC_STATCTRL_DACTRGSEL_MASK;
152     }
153     tmp32 |= (DAC_STATCTRL_DACBFWM(config->watermark) | DAC_STATCTRL_DACBFMD(config->workMode) |
154               DAC_STATCTRL_DACBFUP(config->upperLimit));
155     base->STATCTRL = tmp32;
156 }
157 
158 /*!
159  * brief Initializes the DAC32 buffer configuration structure.
160  *
161  * This function initializes the DAC32 buffer configuration structure to a default value. The default values are:
162  * code
163  *   config->triggerMode = kDAC32_BufferTriggerBySoftwareMode;
164  *   config->watermark   = kDAC32_BufferWatermark1Word;
165  *   config->workMode    = kDAC32_BufferWorkAsNormalMode;
166  *   config->upperLimit  = DAC_DAT_COUNT * 2U - 1U;
167  * endcode
168  * param config Pointer to the configuration structure. See "dac32_buffer_config_t".
169  */
DAC32_GetDefaultBufferConfig(dac32_buffer_config_t * config)170 void DAC32_GetDefaultBufferConfig(dac32_buffer_config_t *config)
171 {
172     assert(NULL != config);
173 
174     /* Initializes the configure structure to zero. */
175     (void)memset(config, 0, sizeof(*config));
176 
177     config->triggerMode = kDAC32_BufferTriggerBySoftwareMode;
178     config->watermark   = kDAC32_BufferWatermark1Word;
179     config->workMode    = kDAC32_BufferWorkAsNormalMode;
180     config->upperLimit  = DAC_DAT_COUNT * 2U - 1U;
181 }
182 
183 /*!
184  * brief Sets the value for  items in the buffer.
185  *
186  * param base  DAC32 peripheral base address.
187  * param index Setting index for items in the buffer. The available index should not exceed the size of the DAC32
188  * buffer.
189  * param value Setting value for items in the buffer. 12-bits are available.
190  */
DAC32_SetBufferValue(DAC_Type * base,uint32_t index,uint32_t value)191 void DAC32_SetBufferValue(DAC_Type *base, uint32_t index, uint32_t value)
192 {
193     assert(index < (DAC_DAT_COUNT * 2U));
194 
195     if (0U == (index % 2U))
196     {
197         index            = index / 2U; /* Register index. */
198         base->DAT[index] = (base->DAT[index] & ~(DAC_DAT_DATA0_MASK)) | DAC_DAT_DATA0(value);
199     }
200     else
201     {
202         index            = index / 2U; /* Register index. */
203         base->DAT[index] = (base->DAT[index] & ~(DAC_DAT_DATA1_MASK)) | DAC_DAT_DATA1(value);
204     }
205 }
206