1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_DAC_H__
9 #define __HAL_DAC_H__
10 
11 #include "hal_common.h"
12 
13 /*!
14  * @addtogroup DAC
15  * @{
16  */
17 
18 /*!
19  * @brief DAC driver version number.
20  */
21 #define DAC_DRIVER_VERSION 0u /*!< dac_0. */
22 
23 /*!
24  * @addtogroup DAC_CHANNEL_SELECTION
25  * @{
26  */
27 #define DAC_CHN_NUM    (2u) /*!< The total number of DAC channel. */
28 #define DAC_CHN_1      (0u) /*!< The selection of DAC Channel 1. */
29 #define DAC_CHN_2      (1u) /*!< The selection of DAC Channel 2. */
30 /*!
31  *@}
32  */
33 
34 /*!
35  * @brief DAC align type.
36  */
37 typedef enum
38 {
39     DAC_Align_8b_Dual        = 0u,  /*!< The selection of dual channel data alignment of 8b right. */
40     DAC_Align_8b_Right       = 1u,  /*!< The selection of single channel data alignment of 8b right. */
41     DAC_Align_12b_Left       = 2u,  /*!< The selection of single channel data alignment of 12b left. */
42     DAC_Align_12b_Right      = 3u,  /*!< The selection of single channel data alignment of 12b right. */
43     DAC_Align_12b_Dual_Left  = 4u,  /*!< The selection of dual channel data alignment of 12b left. */
44     DAC_Align_12b_Dual_Right = 5u,  /*!< The selection of dual channel data alignment of 12b right. */
45 } DAC_Align_Type;
46 
47 /*!
48  * @brief DAC trigger source selection.
49  */
50 typedef enum
51 {
52     DAC_TrgSource_Alt0        = 0u, /*!< The selection of the documented trigger of value 0. */
53     DAC_TrgSource_Alt1        = 1u, /*!< The selection of the documented trigger of value 1. */
54     DAC_TrgSource_Alt2        = 2u, /*!< The selection of the documented trigger of value 2. */
55     DAC_TrgSource_Alt3        = 3u, /*!< The selection of the documented trigger of value 3. */
56     DAC_TrgSource_Alt4        = 4u, /*!< The selection of the documented trigger of value 4. */
57     DAC_TrgSource_Alt5        = 5u, /*!< The selection of the documented trigger of value 5. */
58     DAC_TrgSource_Alt6        = 6u, /*!< The selection of the documented trigger of value 6. */
59     DAC_TrgSource_Alt7        = 7u, /*!< The selection of the documented software trigger. */
60     DAC_TrgSource_None        = 16u, /*!< No trigger to be used. */
61 } DAC_TrgSource_Type;
62 
63 /*!
64  * @brief DAC LFSR unmask bit selector type for adding noise wave.
65  */
66 typedef enum
67 {
68     DAC_AddNoise_LFSRUnMask_0  = 0u,  /*!< Unmask DAC channel LFSR bit0. */
69     DAC_AddNoise_LFSRUnMask_1  = 1u,  /*!< Unmask DAC channel LFSR bit[1:0]. */
70     DAC_AddNoise_LFSRUnMask_2  = 2u,  /*!< Unmask DAC channel LFSR bit[2:0]. */
71     DAC_AddNoise_LFSRUnMask_3  = 3u,  /*!< Unmask DAC channel LFSR bit[3:0]. */
72     DAC_AddNoise_LFSRUnMask_4  = 4u,  /*!< Unmask DAC channel LFSR bit[4:0]. */
73     DAC_AddNoise_LFSRUnMask_5  = 5u,  /*!< Unmask DAC channel LFSR bit[5:0]. */
74     DAC_AddNoise_LFSRUnMask_6  = 6u,  /*!< Unmask DAC channel LFSR bit[6:0]. */
75     DAC_AddNoise_LFSRUnMask_7  = 7u,  /*!< Unmask DAC channel LFSR bit[7:0]. */
76     DAC_AddNoise_LFSRUnMask_8  = 8u,  /*!< Unmask DAC channel LFSR bit[8:0]. */
77     DAC_AddNoise_LFSRUnMask_9  = 9u,  /*!< Unmask DAC channel LFSR bit[9:0]. */
78     DAC_AddNoise_LFSRUnMask_10 = 10u, /*!< Unmask DAC channel LFSR bit[10:0]. */
79     DAC_AddNoise_LFSRUnMask_11 = 11u, /*!< Unmask DAC channel LFSR bit[11:0]. */
80 } DAC_AddNoise_Type;
81 
82 /*!
83  * @brief DAC triangle amplitude selector type for adding triangle wave.
84  */
85 typedef enum
86 {
87     DAC_AddTriangle_LFSRAmplitude_1    = 0u,  /*!< Max triangle amplitude of 1. */
88     DAC_AddTriangle_LFSRAmplitude_3    = 1u,  /*!< Max triangle amplitude of 3. */
89     DAC_AddTriangle_LFSRAmplitude_7    = 2u,  /*!< Max triangle amplitude of 7. */
90     DAC_AddTriangle_LFSRAmplitude_15   = 3u,  /*!< Max triangle amplitude of 15. */
91     DAC_AddTriangle_LFSRAmplitude_31   = 4u,  /*!< Max triangle amplitude of 31. */
92     DAC_AddTriangle_LFSRAmplitude_63   = 5u,  /*!< Max triangle amplitude of 63. */
93     DAC_AddTriangle_LFSRAmplitude_127  = 6u,  /*!< Max triangle amplitude of 127. */
94     DAC_AddTriangle_LFSRAmplitude_255  = 7u,  /*!< Max triangle amplitude of 255. */
95     DAC_AddTriangle_LFSRAmplitude_511  = 8u,  /*!< Max triangle amplitude of 511. */
96     DAC_AddTriangle_LFSRAmplitude_1023 = 9u,  /*!< Max triangle amplitude of 1023. */
97     DAC_AddTriangle_LFSRAmplitude_2047 = 10u, /*!< Max triangle amplitude of 2047. */
98     DAC_AddTriangle_LFSRAmplitude_4095 = 11u, /*!< Max triangle amplitude of 4095. */
99 } DAC_AddTriangle_Type;
100 
101 /*!
102  * @brief This type of structure instance is used to keep the settings when calling the @ref DAC_EnableAddNoise() to initialize the DAC noise wave generation.
103  */
104 typedef struct
105 {
106     DAC_AddNoise_Type AddNoise; /*!< Specify the noise wave pattern. */
107 } DAC_AddNoise_Init_Type;
108 
109 /*!
110  * @brief This type of structure instance is used to keep the settings when calling the @ref DAC_EnableAddTriangle() to initialize the DAC triangle wave generation.
111  */
112 typedef struct
113 {
114     DAC_AddTriangle_Type AddTriangle; /*!< Specify the triangle wave pattern. */
115 } DAC_AddTriangle_Init_Type;
116 
117 /*!
118  * @brief This type of structure instance is used to keep the settings when calling the @ref DAC_Init() to initialize the DAC module.
119  */
120 typedef struct
121 {
122     DAC_TrgSource_Type TrgSource; /*!< Specify whether to use the trigger and the trigger type. */
123     bool EnableOutBuf; /*!< Specify whether output buffer is enabled. */
124 } DAC_Init_Type;
125 
126 /*!
127  * @brief Initialize the DAC module.
128  *
129  * @param DACx DAC instance.
130  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
131  * @param init Pointer to the initialization structure. See to @ref DAC_Init_Type.
132  * @return None.
133  */
134 void DAC_Init(DAC_Type * DACx, uint32_t channel, DAC_Init_Type * init);
135 
136 /*!
137  * @brief Enable the DAC module.
138  *
139  * The DAC module should be enabled before outputting voltage.
140  *
141  * @param DACx DAC instance.
142  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
143  * @param enable 'true' to enable the module, 'false' to disable the module.
144  * @return None.
145  */
146 void DAC_Enable(DAC_Type * DACx, uint32_t channel, bool enable);
147 
148 /*!
149  * @brief Get the echo data from the output of the indicated DAC channel.
150  *
151  * The value in DAC_DORx register takes effect on the actual output of DAC.
152  *
153  * @param DACx DAC instance.
154  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
155  * @return The indicated DAC channel data output value.
156  */
157 uint32_t DAC_GetData(DAC_Type * DACx, uint32_t channel);
158 
159 /*!
160  * @brief Put the value into the indicated channel of the DAC module with the alignment.
161  *
162  * The value in DAC_DORx register takes effect on the actual output of DAC.
163  *
164  * @param DACx DAC instance.
165  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
166  * @param value Data value to be put in indicated DAC channel for conversion.
167  * @param align Alignment the value expected to be put into the conversion. See to @ref DAC_Align_Type.
168  * @return The current converted value of the indicated DAC channel.
169  */
170 uint32_t DAC_PutData(DAC_Type * DACx, uint32_t channel, uint32_t value, DAC_Align_Type align);
171 
172 /*!
173  * @brief Enable the feature of generating noise wave from the DAC module.
174  *
175  * If enable additional noise wave, the output value will depend on the the unmask LFSR bit and input of the indicated channel.
176  *
177  * @param DACx DAC instance.
178  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
179  * @param init Pointer to the initialization structure. See to @ref DAC_AddNoise_Init_Type. If null, disable the wave generation.
180  * @return None.
181  */
182 void DAC_EnableAddNoise(DAC_Type * DACx, uint32_t channel, DAC_AddNoise_Init_Type * init);
183 
184 /*!
185  * @brief Enable the feature of generating triangle wave from the DAC module.
186  *
187  * Defaultedly, DAC does not generate any wave.
188  * If enable additional triangle wave, the output value will depend on the the indicated amplitude and input of the indicated channel.
189  *
190  * @param DACx DAC instance.
191  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
192  * @param init Pointer to the initialization structure. See to @ref DAC_AddTriangle_Init_Type. If null, disable the wave generation.
193  * @return None.
194  */
195 void DAC_EnableAddTriangle(DAC_Type * DACx, uint32_t channel, DAC_AddTriangle_Init_Type * init);
196 
197 /*!
198  * @brief Enable the DMA trigger from the DAC module.
199  *
200  * The DMA trigger events are the same as the intertupts.
201  *
202  * @param DACx DAC instance.
203  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
204  * @param enable 'true' to enable the DMA trigger, 'false' to disable the DMA trigger.
205  * @return None.
206  */
207 void DAC_EnableDMA(DAC_Type * DACx, uint32_t channel, bool enable);
208 
209 /*!
210  * @brief Do trigger the indicated DAC channel with software trigger.
211  *
212  * @param DACx DAC instance.
213  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
214  * @return None.
215  */
216 void DAC_DoSwTrigger(DAC_Type * DACx, uint32_t channel);
217 
218 /*!
219  * @brief Do trigger the dual DAC channels with software trigger simultaneously.
220  *
221  * @param DACx DAC instance.
222  * @return None.
223  */
224 void DAC_DoDualChannelSwTrigger(DAC_Type * DACx);
225 
226 /*!
227  * @brief Get the indicated register address of the DAC module.
228  *
229  * @param DACx DAC instance.
230  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
231  * @param align Alignment that the data expects to br transferred. See to @ref DAC_Align_Type.
232  * @return The value of the address for indicated align type of the DAC channel.
233  */
234 uint32_t DAC_GetDataRegAddr(DAC_Type * DACx, uint32_t channel, DAC_Align_Type align);
235 
236 /*!
237  * @brief Put 8-bit value into the indicated channel of the DAC module.
238  *
239  * The low 8-bit of the setting value here will be output with 4-bit left shift.
240  * If the value is 0x1f, the output will be 0x1f0.
241  *
242  * @param DACx DAC instance.
243  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
244  * @param value Data value to be put in indicated DAC channel for conversion.
245  * @return None.
246  */
247 void DAC_PutData8bRightAlign(DAC_Type * DACx, uint32_t channel, uint32_t value);
248 
249 /*!
250  * @brief Put the 12-bit value into indicated channel of the DAC module.
251  *
252  * The low 12-bit of the setting value here will be output after 4-bit right shift.
253  * If the value is 0x1ff, the output will be 0x1f.
254  *
255  * @param DACx DAC instance.
256  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
257  * @param value Data value to be put in indicated DAC channel for conversion.
258  * @return None.
259  */
260 void DAC_PutData12bLeftAlign(DAC_Type * DACx, uint32_t channel, uint32_t value);
261 
262 /*!
263  * @brief Put the current data into indicated channel of the DAC module.
264  *
265  * The low 12-bit of the setting value here will output with the original value.
266  *
267  * @param DACx DAC instance.
268  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
269  * @param value Data value to be put in indicated DAC channel for conversion.
270  * @return None.
271  */
272 void DAC_PutData12bRightAlign(DAC_Type * DACx, uint32_t channel, uint32_t value);
273 
274 /*!
275  * @brief Respectively put the value into the dual channels of the DAC module 8b right aligned.
276  *
277  * Simultaneously output the low 8-bit of the low 16-bit and high 16-bit of the 32-bit value seperately with a 4-bit left shift.
278  * If value is 0x011f01ff, the output of each channel will be 0xff0 ( channel 1 ) and 0x1f0 ( channel 2 ).
279  *
280  * @param DACx DAC instance.
281  * @param value Data value to be put in DAC channel 1 and DAC channel 2 for conversion.
282  * @return None.
283  */
284 void DAC_PutDualChannelData8bRightAlign(DAC_Type * DACx, uint32_t value);
285 
286 /*!
287  * @brief Put the data into the dual channel of the DAC module 12b left aligned.
288  *
289  * Simultaneously output the high 12-bit of the low 16-bit and high 16-bit of the 32-bit value seperately after 4-bit right shift.
290  * If value is 0x011f01ff, the output of each channel will be 0x01f ( channel 1 ) and 0x011 ( channel 2 ).
291  *
292  * @param DACx DAC instance.
293  * @param value Data value to be put in DAC channel 1 and DAC channel 2 for conversion.
294  * @return None.
295  */
296 void DAC_PutDualChannelData12bLeftAlign(DAC_Type * DACx, uint32_t value);
297 
298 /*!
299  * @brief Put the data into the dual channel of the DAC module 12b right aligned.
300  *
301  * Simultaneously output the low 12-bit of the low 16-bit and high 16-bit of the 32-bit value seperately.
302  *
303  * @param DACx DAC instance.
304  * @param value Data value to be put in DAC channel 1 and DAC channel 2 for conversion.
305  * @return None.
306  */
307 void DAC_PutDualChannelData12bRightAlign(DAC_Type * DACx, uint32_t value);
308 
309 /*!
310  * @brief Get the 8-bit data register address of the DAC module.
311  *
312  * @param DACx DAC instance.
313  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
314  * @return The value of the address for DAC module's 8-bit data register .
315  */
316 uint32_t DAC_GetData8bRegAddr(DAC_Type * DACx, uint32_t channel);
317 
318 /*!
319  * @brief Get the 12-bit left-aligned data register address of the DAC module.
320  *
321  * @param DACx DAC instance.
322  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
323  * @return The value of the address for DAC module's 12-bit left-aligned data register .
324  */
325 uint32_t DAC_GetData12bLeftRegAddr(DAC_Type * DACx, uint32_t channel);
326 
327 /*!
328  * @brief Get the 12-bit right-aligned data register address of the DAC module.
329  *
330  * @param DACx DAC instance.
331  * @param channel Indicated DAC channel. See to @ref DAC_CHANNEL_SELECTION.
332  * @return The value of the address for DAC module's 12-bit right-aligned data register .
333  */
334 uint32_t DAC_GetData12bRightRegAddr(DAC_Type * DACx, uint32_t channel);
335 
336 /*!
337  * @brief Get the dual channel 8-bit data register address of the DAC module.
338  *
339  * @param DACx DAC instance.
340  * @return The value of the address for DAC module's dual channel 8-bit data register .
341  */
342 uint32_t DAC_GetDualChannelData8bRegAddr(DAC_Type * DACx);
343 
344 /*!
345  * @brief Get the dual channel 12-bit left-aligned data register address of the DAC module.
346  *
347  * @param DACx DAC instance.
348  * @return The value of the address for DAC module's 12-bit dual channel left-aligned data register .
349  */
350 uint32_t DAC_GetDualChannelData12bLeftRegAddr(DAC_Type * DACx);
351 
352 /*!
353  * @brief Get the dual channel 12-bit right-aligned data register address of the DAC module.
354  *
355  * @param DACx DAC instance.
356  * @return The value of the address for DAC module's 12-bit dual channel right-aligned data register .
357  */
358 uint32_t DAC_GetDualChannelData12bRightRegAddr(DAC_Type * DACx);
359 
360 /*!
361  *@}
362  */
363 
364 #endif /* __HAL_DAC_H__ */
365 
366