]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/dac/fsl_dac_hal.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / hal / dac / fsl_dac_hal.h
1 /*
2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3 * All rights reserved.
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 Freescale Semiconductor, Inc. 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 #ifndef __FSL_DAC_HAL_H__
32 #define __FSL_DAC_HAL_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "fsl_device_registers.h"
38 #include "fsl_dac_features.h"
39
40 /*!
41 * @addtogroup dac_hal
42 * @{
43 */
44
45 /******************************************************************************
46 * Definitions
47 *****************************************************************************/
48
49 /*!
50 * @brief DAC status return codes.
51 */
52 typedef enum _dac_status
53 {
54 kStatus_DAC_Success = 0U, /*!< Success. */
55 kStatus_DAC_InvalidArgument = 1U, /*!< Invalid argument existed. */
56 kStatus_DAC_Failed = 2U /*!< Execution failed. */
57 } dac_status_t;
58
59 /*!
60 * @brief Defines the type of selection for DAC module's reference voltage source.
61 *
62 * See the appropriate SoC Reference Manual for actual connections.
63 */
64 typedef enum _dac_ref_volt_src_mode
65 {
66 kDacRefVoltSrcOfVref1 = 0U, /*!< Select DACREF_1 as the reference voltage. */
67 kDacRefVoltSrcOfVref2 = 1U, /*!< Select DACREF_2 as the reference voltage. */
68 } dac_ref_volt_src_mode_t;
69
70 /*!
71 * @brief Defines the type of selection for DAC module trigger mode.
72 */
73 typedef enum _dac_trigger_mode
74 {
75 kDacTriggerByHardware = 0U, /*!< Select hardware trigger. */
76 kDacTriggerBySoftware = 1U /*!< Select software trigger. */
77 } dac_trigger_mode_t;
78
79 /*!
80 * @brief Defines the type of selection for buffer watermark mode.
81 *
82 * If the buffer feature for DAC module is enabled, a watermark event will
83 * occur when the buffer index hits the watermark.
84 */
85 typedef enum _dac_buff_watermark_mode
86 {
87 kDacBuffWatermarkFromUpperAs1Word = 0U, /*!< Select 1 word away from the upper of buffer. */
88 kDacBuffWatermarkFromUpperAs2Word = 1U, /*!< Select 2 word away from the upper of buffer. */
89 kDacBuffWatermarkFromUpperAs3Word = 2U, /*!< Select 3 word away from the upper of buffer. */
90 kDacBuffWatermarkFromUpperAs4Word = 3U, /*!< Select 4 word away from the upper of buffer. */
91 } dac_buff_watermark_mode_t;
92
93 /*!
94 * @brief Defines the type of selection for buffer work mode.
95 *
96 * There are three kinds of work modes when the DAC buffer is enabled.
97 * Normal mode - When the buffer index hits the upper level, it
98 * starts (0) on the next trigger.
99 * Swing mode - When the buffer index hits the upper level, it goes backward to
100 * the start and is reduced one-by-one on the next trigger. When the buffer index
101 * hits the start, it goes backward to the upper level and increases one-by-one
102 * on the next trigger.
103 * One-Time-Scan mode - The buffer index can only be increased on the next trigger.
104 * When the buffer index hits the upper level, it is not updated by the trigger.
105 * FIFO mode
106 */
107 typedef enum _dac_buff_work_mode
108 {
109 kDacBuffWorkAsNormalMode = 0U, /*!< Buffer works as Normal. */
110 kDacBuffWorkAsSwingMode = 1U, /*!< Buffer works as swing. */
111 kDacBuffWorkAsOneTimeScanMode = 2U, /*!< Buffer works as one time scan.*/
112 kDacBuffWorkAsFIFOMode = 3U /*!< Buffer works as FIFO.*/
113 } dac_buff_work_mode_t;
114
115 #if defined(__cplusplus)
116 extern "C" {
117 #endif
118
119 /*******************************************************************************
120 * API
121 ******************************************************************************/
122
123 /*!
124 * @brief Resets all configurable registers to be in the reset state for DAC.
125 *
126 * This function resets all configurable registers to be in the reset state for DAC.
127 * It should be called before configuring the DAC module.
128 *
129 * @param baseAddr The DAC peripheral base address.
130 */
131 void DAC_HAL_Init(uint32_t baseAddr);
132
133 /*!
134 * @brief Sets the 12-bit value for the DAC items in the buffer.
135 *
136 * This function sets the value assembled by the low 8 bits and high 4
137 * bits of 12-bit DAC item in the buffer.
138 *
139 * @param baseAddr The DAC peripheral base address.
140 * @param index Buffer index.
141 * @param value Setting value.
142 */
143 void DAC_HAL_SetBuffValue(uint32_t baseAddr, uint8_t index, uint16_t value);
144
145 /*!
146 * @brief Gets the 12-bit value from the DAC item in the buffer.
147 *
148 * This function gets the value assembled by the low 8 bits and high 4
149 * bits of 12-bit DAC item in the buffer.
150 *
151 * @param baseAddr The DAC peripheral base address.
152 * @param index Buffer index.
153 * @return Current setting value.
154 */
155 uint16_t DAC_HAL_GetBuffValue(uint32_t baseAddr, uint8_t index);
156
157 /*!
158 * @brief Clears the flag of the DAC buffer read pointer.
159 *
160 * This function clears the flag of the DAC buffer read pointer when it hits the
161 * bottom position.
162 *
163 * @param baseAddr The DAC peripheral base address.
164 */
165 static inline void DAC_HAL_ClearBuffIndexUpperFlag(uint32_t baseAddr)
166 {
167 BW_DAC_SR_DACBFRPBF(baseAddr, 0U);
168 }
169
170 /*!
171 * @brief Gets the flag of DAC buffer read pointer when it hits the bottom position.
172 *
173 * This function gets the flag of DAC buffer read pointer when it hits the
174 * bottom position.
175 *
176 * @param baseAddr The DAC peripheral base address.
177 * @return Assertion of indicated event.
178 */
179 static inline bool DAC_HAL_GetBuffIndexUpperFlag(uint32_t baseAddr)
180 {
181 return ( 1U == BR_DAC_SR_DACBFRPBF(baseAddr) );
182 }
183
184 /*!
185 * @brief Clears the flag of the DAC buffer read pointer when it hits the top position.
186 *
187 * This function clears the flag of the DAC buffer read pointer when it hits the
188 * top position.
189 *
190 * @param baseAddr The DAC peripheral base address.
191 */
192 static inline void DAC_HAL_ClearBuffIndexStartFlag(uint32_t baseAddr)
193 {
194 BW_DAC_SR_DACBFRPTF(baseAddr, 0U);
195 }
196
197 /*!
198 * @brief Gets the flag of the DAC buffer read pointer when it hits the top position.
199 *
200 * This function gets the flag of the DAC buffer read pointer when it hits the
201 * top position.
202 *
203 * @param baseAddr The DAC peripheral base address.
204 * @return Assertion of indicated event.
205 */
206 static inline bool DAC_HAL_GetBuffIndexStartFlag(uint32_t baseAddr)
207 {
208 return ( 1U == BR_DAC_SR_DACBFRPTF(baseAddr) );
209 }
210
211 #if FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
212
213 /*!
214 * @brief Gets the flag of the DAC buffer read pointer when it hits the watermark position.
215 *
216 * This function gets the flag of the DAC buffer read pointer when it hits the
217 * watermark position.
218 *
219 * @param baseAddr The DAC peripheral base address.
220 * @return Assertion of indicated event.
221 */
222 static inline bool DAC_HAL_GetBuffIndexWatermarkFlag(uint32_t baseAddr)
223 {
224 return ( 1U == BR_DAC_SR_DACBFWMF(baseAddr) );
225 }
226
227 /*!
228 * @brief Clears the flag of the DAC buffer read pointer when it hits the watermark position.
229 *
230 * This function clears the flag of the DAC buffer read pointer when it hits the
231 * watermark position.
232 *
233 * @param baseAddr The DAC peripheral base address.
234 * @return Assertion of indicated event.
235 */
236 static inline void DAC_HAL_ClearBuffIndexWatermarkFlag(uint32_t baseAddr)
237 {
238 BW_DAC_SR_DACBFWMF(baseAddr, 0U);
239 }
240 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
241
242 /*!
243 * @brief Enables the Programmable Reference Generator.
244 *
245 * This function enables the Programmable Reference Generator. Then the
246 * DAC system is enabled.
247 *
248 * @param baseAddr The DAC peripheral base address.
249 */
250 static inline void DAC_HAL_Enable(uint32_t baseAddr)
251 {
252 BW_DAC_C0_DACEN(baseAddr, 1U);
253 }
254
255 /*!
256 * @brief Disables the Programmable Reference Generator.
257 *
258 * This function disables the Programmable Reference Generator. Then the
259 * DAC system is disabled.
260 *
261 * @param baseAddr The DAC peripheral base address.
262 */
263 static inline void DAC_HAL_Disable(uint32_t baseAddr)
264 {
265 BW_DAC_C0_DACEN(baseAddr, 0U);
266 }
267
268 /*!
269 * @brief Sets the reference voltage source mode for the DAC module.
270 *
271 * This function sets the reference voltage source mode for the DAC module.
272 *
273 * @param baseAddr The DAC peripheral base address.
274 * @param mode Selection of enumeration mode. See to "dac_ref_volt_src_mode_t".
275 */
276 static inline void DAC_HAL_SetRefVoltSrcMode(uint32_t baseAddr, dac_ref_volt_src_mode_t mode)
277 {
278 BW_DAC_C0_DACRFS(baseAddr, ((kDacRefVoltSrcOfVref1==mode)?0U:1U) );
279 }
280
281 /*!
282 * @brief Sets the trigger mode for the DAC module.
283 *
284 * This function sets the trigger mode for the DAC module.
285 *
286 * @param baseAddr The DAC peripheral base address.
287 * @param mode Selection of enumeration mode. See to "dac_trigger_mode_t".
288 */
289 static inline void DAC_HAL_SetTriggerMode(uint32_t baseAddr, dac_trigger_mode_t mode)
290 {
291 BW_DAC_C0_DACTRGSEL(baseAddr, ((kDacTriggerByHardware==mode)?0U:1U) );
292 }
293
294 /*!
295 * @brief Triggers the converter with software.
296 *
297 * This function triggers the converter with software. If the DAC software
298 * trigger is selected and buffer enabled, calling this API advances the
299 * buffer read pointer once.
300 *
301 * @param baseAddr The DAC peripheral base address.
302 */
303 static inline void DAC_HAL_SetSoftTriggerCmd(uint32_t baseAddr)
304 {
305 BW_DAC_C0_DACSWTRG(baseAddr, 1U);
306 }
307
308 /*!
309 * @brief Switches to enable working in low power mode for the DAC module.
310 *
311 * This function switches to enable working in low power mode for the DAC module.
312 *
313 * @param baseAddr The DAC peripheral base address.
314 * @param enable Switcher to assert the feature.
315 */
316 static inline void DAC_HAL_SetLowPowerCmd(uint32_t baseAddr, bool enable)
317 {
318 BW_DAC_C0_LPEN(baseAddr, (enable?1U:0U) );
319 }
320
321 #if FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
322 /*!
323 * @brief Switches to enable the interrupt when buffer read pointer hits the watermark position.
324 *
325 * This function switches to enable the interrupt when the buffer read pointer hits
326 * the watermark position.
327 *
328 * @param baseAddr The DAC peripheral base address.
329 * @param enable Switcher to assert the feature.
330 */
331 static inline void DAC_HAL_SetBuffIndexWatermarkIntCmd(uint32_t baseAddr, bool enable)
332 {
333 BW_DAC_C0_DACBWIEN(baseAddr, (enable?1U:0U) );
334 }
335 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
336
337 /*!
338 * @brief Switches to enable the interrupt when the buffer read pointer hits the top position.
339 *
340 * This function switches to enable the interrupt when the buffer read pointer hits
341 * the top position.
342 *
343 * @param baseAddr The DAC peripheral base address.
344 * @param enable Switcher to assert the feature.
345 */
346 static inline void DAC_HAL_SetBuffIndexStartIntCmd(uint32_t baseAddr, bool enable)
347 {
348 BW_DAC_C0_DACBTIEN(baseAddr, (enable?1U:0U) );
349 }
350
351 /*!
352 * @brief Switches to enable the interrupt when the buffer read pointer hits the bottom position.
353 *
354 * This function switches to enable the interrupt when the buffer read pointer hits
355 * the bottom position.
356 *
357 * @param baseAddr The DAC peripheral base address.
358 * @param enable Switcher to assert the feature.
359 */
360 static inline void DAC_HAL_SetBuffIndexUpperIntCmd(uint32_t baseAddr, bool enable)
361 {
362 BW_DAC_C0_DACBBIEN(baseAddr, (enable?1U:0U) );
363 }
364
365 /*!
366 * @brief Switches to enable the DMA for DAC.
367 *
368 * This function switches to enable the DMA for the DAC module. When the DMA is enabled,
369 * DMA request is generated by the original interrupts, which are
370 * not presented on this module at the same time.
371 *
372 * @param baseAddr The DAC peripheral base address.
373 * @param enable Switcher to assert the feature.
374 */
375 static inline void DAC_HAL_SetDmaCmd(uint32_t baseAddr, bool enable)
376 {
377 BW_DAC_C1_DMAEN(baseAddr, (enable?1U:0U) );
378 }
379
380 #if FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
381 /*!
382 * @brief Sets the watermark mode of the buffer for the DAC module.
383 *
384 * This function sets the watermark mode of the buffer for the DAC module.
385 *
386 * @param baseAddr The DAC peripheral base address.
387 * @param mode Selection of enumeration mode. See to "dac_buff_watermark_mode_t".
388 */
389 static inline void DAC_HAL_SetBuffWatermarkMode(uint32_t baseAddr, dac_buff_watermark_mode_t mode)
390 {
391 BW_DAC_C1_DACBFWM(baseAddr, (uint8_t)mode);
392 }
393 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
394
395 /*!
396 * @brief Sets the work mode of the buffer for the DAC module.
397 *
398 * This function sets the work mode of the buffer for the DAC module.
399 *
400 * @param baseAddr The DAC peripheral base address.
401 * @param mode Selection of enumeration mode. See to "dac_buff_work_mode_t".
402 */
403 static inline void DAC_HAL_SetBuffWorkMode(uint32_t baseAddr, dac_buff_work_mode_t mode)
404 {
405 BW_DAC_C1_DACBFMD(baseAddr, (uint8_t)mode );
406 }
407
408 /*!
409 * @brief Switches to enable the buffer for the DAC module.
410 *
411 * This function switches to enable the buffer for the DAC module.
412 *
413 * @param baseAddr The DAC peripheral base address.
414 * @param enable Switcher to assert the feature.
415 */
416 static inline void DAC_HAL_SetBuffCmd(uint32_t baseAddr, bool enable)
417 {
418 BW_DAC_C1_DACBFEN(baseAddr, (enable?1U:0U) );
419 }
420
421 /*!
422 * @brief Gets the buffer index upper limitation for the DAC module.
423 *
424 * This function gets the upper buffer index upper limitation for the DAC module.
425 *
426 * @param baseAddr The DAC peripheral base address.
427 * @return Value of buffer index upper limitation.
428 */
429 static inline uint8_t DAC_HAL_GetBuffUpperIndex(uint32_t baseAddr)
430 {
431 return BR_DAC_C2_DACBFUP(baseAddr);
432 }
433
434 /*!
435 * @brief Sets the buffer index upper limitation for the DAC module.
436 *
437 * This function sets the upper buffer index upper limitation for the DAC module.
438 *
439 * @param baseAddr The DAC peripheral base address.
440 * @param index Setting value of upper limitation for buffer index.
441 */
442 static inline void DAC_HAL_SetBuffUpperIndex(uint32_t baseAddr, uint8_t index)
443 {
444 assert(index < HW_DAC_DATnL_COUNT);
445 BW_DAC_C2_DACBFUP(baseAddr , index);
446 }
447
448 /*!
449 * @brief Gets the current buffer index upper limitation for the DAC module.
450 *
451 * This function gets the current buffer index for the DAC module.
452 *
453 * @param baseAddr The DAC peripheral base address.
454 * @return Value of current buffer index.
455 */
456 static inline uint8_t DAC_HAL_GetBuffCurrentIndex(uint32_t baseAddr)
457 {
458 return BR_DAC_C2_DACBFRP(baseAddr);
459 }
460
461 /*!
462 * @brief Sets the buffer index for the DAC module.
463 *
464 * This function sets the upper buffer index for the DAC module.
465 *
466 * @param baseAddr the DAC peripheral base address.
467 * @param index Setting value for buffer index.
468 */
469 static inline void DAC_HAL_SetBuffCurrentIndex(uint32_t baseAddr, uint8_t index)
470 {
471 assert(index < HW_DAC_DATnL_COUNT);
472 BW_DAC_C2_DACBFRP(baseAddr, index);
473 }
474
475 #if defined(__cplusplus)
476 }
477 #endif
478
479 /*!
480 * @}
481 */
482
483 #endif /* __FSL_DAC_HAL_H__ */
484
485 /******************************************************************************
486 * EOF
487 *****************************************************************************/
488
Imprint / Impressum