1 /*
2 * @brief LPC15xx D/A conversion driver
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2014
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #ifndef __DAC_15XX_H_
33 #define __DAC_15XX_H_
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /** @defgroup DAC_15XX CHIP: LPC15xx D/A conversion driver
40 * @ingroup CHIP_15XX_Drivers
41 * @{
42 */
43
44 /**
45 * @brief DAC register block structure
46 */
47 typedef struct { /*!< DAC Structure */
48 __IO uint32_t VAL; /*!< DAC register. Holds the conversion data */
49 __IO uint32_t CTRL; /*!< DAC control register */
50 __IO uint32_t CNTVAL; /*!< DAC counter value register */
51 } LPC_DAC_T;
52
53 /** After this field is written with a
54 new VALUE, the voltage on the AOUT pin (with respect to VSSA)
55 is VALUE*((VREFP_DAC - VREFN)/4095) + VREFN */
56 #define DAC_VALUE(n) ((uint32_t) ((n & 0x0FFF) << 4))
57
58 /* Bit Definitions for DAC Control register */
59 #define DAC_INT_DMA_FLAG (1 << 0)
60 #define DAC_TRIG_SRC_MASK (0x7 << 1)
61 #define DAC_TRIG_SRC_BIT (1 << 1)
62 #define DAC_POLARITY (1 << 4)
63 #define DAC_SYNC_BYPASS (1 << 5)
64 #define DAC_TIM_ENA_BIT (1 << 6)
65 #define DAC_DBLBUF_ENA (1 << 7)
66 #define DAC_SHUTOFF_ENA (1 << 8)
67 #define DAC_SHUTOFF_FLAG (1 << 9)
68 #define DAC_DACCTRL_MASK ((uint32_t) 0xFF << 1)
69 #define DAC_CTRL_UNUSED ((uint32_t) 0x7FFFF << 13)
70
71 /** Value to reload interrupt/DMA timer */
72 #define DAC_CNT_VALUE(n) ((uint32_t) ((n) & 0xffff))
73
74 /**
75 * @brief Initial DAC configuration - Value to AOUT is 0
76 * @param pDAC : pointer to LPC_DAC_T
77 * @return Nothing
78 */
79 void Chip_DAC_Init(LPC_DAC_T *pDAC);
80
81 /**
82 * @brief Shutdown DAC
83 * @param pDAC : pointer to LPC_DAC_T
84 * @return Nothing
85 */
86 void Chip_DAC_DeInit(LPC_DAC_T *pDAC);
87
88 /**
89 * @brief Update value to DAC buffer
90 * @param pDAC : pointer to LPC_DAC_T
91 * @param dac_value : 12 bit input value for conversion
92 * @return Nothing
93 */
Chip_DAC_UpdateValue(LPC_DAC_T * pDAC,uint32_t dac_value)94 STATIC INLINE void Chip_DAC_UpdateValue(LPC_DAC_T *pDAC, uint32_t dac_value)
95 {
96 pDAC->VAL = DAC_VALUE(dac_value);
97 }
98
99 /**
100 * @brief Get status for interrupt/DMA time out
101 * @param pDAC : pointer to LPC_DAC_T
102 * @return TRUE if interrupt/DMA flag is set else returns FALSE
103 */
Chip_DAC_GetIntStatus(LPC_DAC_T * pDAC)104 STATIC INLINE bool Chip_DAC_GetIntStatus(LPC_DAC_T *pDAC)
105 {
106 return (pDAC->CTRL & DAC_INT_DMA_FLAG) != 0;
107 }
108
109 /**
110 * @brief Set Interrupt/DMA trigger source as Internal timer, enable timer before this call
111 * @param pDAC : pointer to LPC_DAC_T
112 * @return Nothing
113 */
Chip_DAC_SetTrgSrcInternal(LPC_DAC_T * pDAC)114 STATIC INLINE void Chip_DAC_SetTrgSrcInternal(LPC_DAC_T *pDAC)
115 {
116 pDAC->CTRL &= ~(DAC_CTRL_UNUSED | DAC_TRIG_SRC_BIT);
117 }
118
119 /**
120 * @brief Set Interrupt/DMA trigger source as External Pin
121 * @param pDAC : pointer to LPC_DAC_T
122 * @return Nothing
123 */
Chip_DAC_SetTrgSrcExternal(LPC_DAC_T * pDAC)124 STATIC INLINE void Chip_DAC_SetTrgSrcExternal(LPC_DAC_T *pDAC)
125 {
126 pDAC->CTRL = (pDAC->CTRL & ~DAC_CTRL_UNUSED) | DAC_TRIG_SRC_BIT;
127 }
128
129 /**
130 * @brief Set Polarity for external trigger pin
131 * @param pDAC : pointer to LPC_DAC_T
132 * @param falling_edge : If TRUE indicates that the trigger polarity is Falling edge
133 * else it is a Rising edge
134 * @return Nothing
135 */
Chip_DAC_SetExtTriggerPolarity(LPC_DAC_T * pDAC,bool falling_edge)136 STATIC INLINE void Chip_DAC_SetExtTriggerPolarity(LPC_DAC_T *pDAC, bool falling_edge)
137 {
138 if (falling_edge) {
139 pDAC->CTRL = (pDAC->CTRL & ~DAC_CTRL_UNUSED) | DAC_POLARITY;
140 }
141 else {
142 pDAC->CTRL &= ~(DAC_CTRL_UNUSED | DAC_POLARITY );
143 }
144 }
145
146 /**
147 * @brief Enable Sync Bypass, only if external trigger is in sync with SysClk
148 * @param pDAC : pointer to LPC_DAC_T
149 * @return Nothing
150 */
Chip_DAC_EnableSyncBypass(LPC_DAC_T * pDAC)151 STATIC INLINE void Chip_DAC_EnableSyncBypass(LPC_DAC_T *pDAC)
152 {
153 pDAC->CTRL = (pDAC->CTRL & ~DAC_CTRL_UNUSED) | DAC_SYNC_BYPASS;
154 }
155
156 /**
157 * @brief Disable Sync Bypass
158 * @param pDAC : pointer to LPC_DAC_T
159 * @return Nothing
160 */
Chip_DAC_DisableSyncBypass(LPC_DAC_T * pDAC)161 STATIC INLINE void Chip_DAC_DisableSyncBypass(LPC_DAC_T *pDAC)
162 {
163 pDAC->CTRL &= ~(DAC_CTRL_UNUSED | DAC_SYNC_BYPASS);
164 }
165
166 /**
167 * @brief Enable Internal Timer, CNTVAL should be loaded before enabling timer
168 * @param pDAC : pointer to LPC_DAC_T
169 * @return Nothing
170 */
Chip_DAC_EnableIntTimer(LPC_DAC_T * pDAC)171 STATIC INLINE void Chip_DAC_EnableIntTimer(LPC_DAC_T *pDAC)
172 {
173 pDAC->CTRL = (pDAC->CTRL & ~DAC_CTRL_UNUSED) | DAC_TIM_ENA_BIT;
174 }
175
176 /**
177 * @brief Disable Internal Timer
178 * @param pDAC : pointer to LPC_DAC_T
179 * @return Nothing
180 */
Chip_DAC_DisableIntTimer(LPC_DAC_T * pDAC)181 STATIC INLINE void Chip_DAC_DisableIntTimer(LPC_DAC_T *pDAC)
182 {
183 pDAC->CTRL &= ~(DAC_CTRL_UNUSED | DAC_TIM_ENA_BIT);
184 }
185
186 /**
187 * @brief Enable DAC Double Buffer
188 * @param pDAC : pointer to LPC_DAC_T
189 * @return Nothing
190 */
Chip_DAC_EnableDoubleBuffer(LPC_DAC_T * pDAC)191 STATIC INLINE void Chip_DAC_EnableDoubleBuffer(LPC_DAC_T *pDAC)
192 {
193 pDAC->CTRL = (pDAC->CTRL & ~DAC_CTRL_UNUSED) | DAC_DBLBUF_ENA;
194 }
195
196 /**
197 * @brief Disable DAC Double Buffer
198 * @param pDAC : pointer to LPC_DAC_T
199 * @return Nothing
200 */
Chip_DAC_DisableDoubleBuffer(LPC_DAC_T * pDAC)201 STATIC INLINE void Chip_DAC_DisableDoubleBuffer(LPC_DAC_T *pDAC)
202 {
203 pDAC->CTRL &= ~(DAC_CTRL_UNUSED | DAC_DBLBUF_ENA);
204 }
205
206 /**
207 * @brief Enable DAC Shut Off
208 * @param pDAC : pointer to LPC_DAC_T
209 * @return Nothing
210 */
Chip_DAC_EnableShutOff(LPC_DAC_T * pDAC)211 STATIC INLINE void Chip_DAC_EnableShutOff(LPC_DAC_T *pDAC)
212 {
213 pDAC->CTRL = (pDAC->CTRL & ~DAC_CTRL_UNUSED) | DAC_SHUTOFF_ENA;
214 }
215
216 /**
217 * @brief Disable DAC Shut Off
218 * @param pDAC : pointer to LPC_DAC_T
219 * @return Nothing
220 */
Chip_DAC_DisableShutOff(LPC_DAC_T * pDAC)221 STATIC INLINE void Chip_DAC_DisableShutOff(LPC_DAC_T *pDAC)
222 {
223 pDAC->CTRL &= ~(DAC_CTRL_UNUSED | DAC_SHUTOFF_ENA);
224 }
225
226 /**
227 * @brief Get status of DAC Shut Off
228 * @param pDAC : pointer to LPC_DAC_T
229 * @return TRUE if DAC is shut off else returns FALSE
230 */
Chip_DAC_GetShutOffStatus(LPC_DAC_T * pDAC)231 STATIC INLINE bool Chip_DAC_GetShutOffStatus(LPC_DAC_T *pDAC)
232 {
233 return (pDAC->CTRL & DAC_SHUTOFF_FLAG) != 0;
234 }
235
236 /**
237 * @brief Enables the DMA operation and controls DMA timer
238 * @param pDAC : pointer to LPC_DAC_T
239 * @param dacFlags : An Or'ed value of the following DAC values:
240 * - DAC_TRIG_SRC_BIT :set trigger source for Interrupt/DMA
241 * 0 - Internal timer trigger, 1 - External trigger
242 * - DAC_POLARITY :polarity of the trigger if it is external
243 * - DAC_SYNC_BYPASS :Synchronize selection is trigger is external
244 * - DAC_TIM_ENA_BIT :enable/disable internal timer
245 * - DAC_DBLBUF_ENA :enable/disable DAC double buffering feature
246 * - DAC_SHUTOFF_ENA :enable/disable DAC Shutoff Pin
247 * @return Nothing
248 * @note Pass an Or'ed value of the DAC flags to enable those options.
249 */
Chip_DAC_ConfigDMAConverterControl(LPC_DAC_T * pDAC,uint32_t dacFlags)250 STATIC INLINE void Chip_DAC_ConfigDMAConverterControl(LPC_DAC_T *pDAC, uint32_t dacFlags)
251 {
252 uint32_t temp;
253
254 temp = pDAC->CTRL & ~(DAC_CTRL_UNUSED | DAC_DACCTRL_MASK);
255 pDAC->CTRL = temp | dacFlags;
256 }
257
258 /**
259 * @brief Set reload value for interrupt/DMA timer
260 * @param pDAC : pointer to LPC_DAC_T
261 * @param time_out : time out to reload for interrupt/DMA timer.
262 * time out rate will be SysClk/(time_out + 1)
263 * @return Nothing
264 */
Chip_DAC_SetDMATimeOut(LPC_DAC_T * pDAC,uint32_t time_out)265 STATIC INLINE void Chip_DAC_SetDMATimeOut(LPC_DAC_T *pDAC, uint32_t time_out)
266 {
267 pDAC->CNTVAL = DAC_CNT_VALUE(time_out);
268 }
269
270 /**
271 * @brief Set reload value for interrupt/DMA timer to trigger periodic interrupts
272 * @param pDAC : pointer to LPC_DAC_T
273 * @param periodHz : Frequency of Timer interrupts in Hz
274 * @return Nothing
275 */
Chip_DAC_SetReqInterval(LPC_DAC_T * pDAC,uint32_t periodHz)276 STATIC INLINE void Chip_DAC_SetReqInterval(LPC_DAC_T *pDAC, uint32_t periodHz)
277 {
278 uint32_t time_out = Chip_Clock_GetSystemClockRate() / periodHz - 1;
279 pDAC->CNTVAL = DAC_CNT_VALUE(time_out);
280 }
281
282 /**
283 * @}
284 */
285
286 #ifdef __cplusplus
287 }
288 #endif
289
290 #endif /* __DAC_15XX_H_ */
291