1 /*****************************************************************************
2  * Copyright (c) 2022, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file n32l43x_dac.h
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #ifndef __N32L43X_DAC_H__
36 #define __N32L43X_DAC_H__
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include "n32l43x.h"
43 
44 /** @addtogroup n32l43x_StdPeriph_Driver
45  * @{
46  */
47 
48 /** @addtogroup DAC
49  * @{
50  */
51 
52 /** @addtogroup DAC_Exported_Types
53  * @{
54  */
55 
56 /**
57  * @brief  DAC Init structure definition
58  */
59 
60 typedef struct
61 {
62     uint32_t Trigger; /*!< Specifies the external trigger for the selected DAC channel.
63                                This parameter can be a value of @ref DAC_trigger_selection */
64 
65     uint32_t WaveGen; /*!< Specifies whether DAC channel noise waves or triangle waves
66                                       are generated, or whether no wave is generated.
67                                       This parameter can be a value of @ref DAC_wave_generation */
68 
69     uint32_t
70         LfsrUnMaskTriAmp; /*!< Specifies the LFSR mask for noise wave generation or
71                                                the maximum amplitude triangle generation for the DAC channel.
72                                                This parameter can be a value of @ref DAC_lfsrunmask_triangleamplitude */
73 
74     uint32_t BufferOutput; /*!< Specifies whether the DAC channel output buffer is enabled or disabled.
75                                     This parameter can be a value of @ref DAC_output_buffer */
76 } DAC_InitType;
77 
78 /**
79  * @}
80  */
81 
82 /** @addtogroup DAC_Exported_Constants
83  * @{
84  */
85 
86 /** @addtogroup DAC_trigger_selection
87  * @{
88  */
89 
90 #define DAC_TRG_NONE                                                                                                   \
91     ((uint32_t)0x00000000) /*!< Conversion is automatic once the DAC1_DHRxxxx register                                 \
92                                     has been loaded, and not by external trigger */
93 #define DAC_TRG_T6_TRGO                                                                                                \
94     ((uint32_t)0x00000004) /*!< TIM6 TRGO selected as external conversion trigger for DAC channel                      \
95                             */
96 #define DAC_TRG_T8_TRGO                                                                                                \
97     ((uint32_t)0x0000000C) /*!< TIM8 TRGO selected as external conversion trigger for DAC channel                      \
98                                     only in High-density devices*/
99 #define DAC_TRG_T7_TRGO                                                                                                \
100     ((uint32_t)0x00000014) /*!< TIM7 TRGO selected as external conversion trigger for DAC channel                      \
101                             */
102 #define DAC_TRG_T5_TRGO                                                                                                \
103     ((uint32_t)0x0000001C) /*!< TIM5 TRGO selected as external conversion trigger for DAC channel                      \
104                             */
105 #define DAC_TRG_T2_TRGO                                                                                                \
106     ((uint32_t)0x00000024) /*!< TIM2 TRGO selected as external conversion trigger for DAC channel                      \
107                             */
108 #define DAC_TRG_T4_TRGO                                                                                                \
109     ((uint32_t)0x0000002C) /*!< TIM4 TRGO selected as external conversion trigger for DAC channel                      \
110                             */
111 #define DAC_TRG_EXT_IT9                                                                                                \
112     ((uint32_t)0x00000034) /*!< EXTI Line9 event selected as external conversion trigger for DAC channel */
113 #define DAC_TRG_SOFTWARE ((uint32_t)0x0000003C) /*!< Conversion started by software trigger for DAC channel */
114 
115 #define IS_DAC_TRIGGER(TRIGGER)                                                                                        \
116     (((TRIGGER) == DAC_TRG_NONE) || ((TRIGGER) == DAC_TRG_T6_TRGO) || ((TRIGGER) == DAC_TRG_T8_TRGO)                   \
117      || ((TRIGGER) == DAC_TRG_T7_TRGO) || ((TRIGGER) == DAC_TRG_T5_TRGO) || ((TRIGGER) == DAC_TRG_T2_TRGO)             \
118      || ((TRIGGER) == DAC_TRG_T4_TRGO) || ((TRIGGER) == DAC_TRG_EXT_IT9) || ((TRIGGER) == DAC_TRG_SOFTWARE))
119 
120 /**
121  * @}
122  */
123 
124 /** @addtogroup DAC_wave_generation
125  * @{
126  */
127 
128 #define DAC_WAVEGEN_NONE     ((uint32_t)0x00000000)
129 #define DAC_WAVEGEN_NOISE    ((uint32_t)0x00000040)
130 #define DAC_WAVEGEN_TRIANGLE ((uint32_t)0x00000080)
131 #define IS_DAC_GENERATE_WAVE(WAVE)                                                                                     \
132     (((WAVE) == DAC_WAVEGEN_NONE) || ((WAVE) == DAC_WAVEGEN_NOISE) || ((WAVE) == DAC_WAVEGEN_TRIANGLE))
133 /**
134  * @}
135  */
136 
137 /** @addtogroup DAC_lfsrunmask_triangleamplitude
138  * @{
139  */
140 
141 #define DAC_UNMASK_LFSRBIT0 ((uint32_t)0x00000000) /*!< Unmask DAC channel LFSR bit0 for noise wave generation */
142 #define DAC_UNMASK_LFSRBITS1_0                                                                                         \
143     ((uint32_t)0x00000100) /*!< Unmask DAC channel LFSR bit[1:0] for noise wave generation                             \
144                             */
145 #define DAC_UNMASK_LFSRBITS2_0                                                                                         \
146     ((uint32_t)0x00000200) /*!< Unmask DAC channel LFSR bit[2:0] for noise wave generation                             \
147                             */
148 #define DAC_UNMASK_LFSRBITS3_0                                                                                         \
149     ((uint32_t)0x00000300) /*!< Unmask DAC channel LFSR bit[3:0] for noise wave generation                             \
150                             */
151 #define DAC_UNMASK_LFSRBITS4_0                                                                                         \
152     ((uint32_t)0x00000400) /*!< Unmask DAC channel LFSR bit[4:0] for noise wave generation                             \
153                             */
154 #define DAC_UNMASK_LFSRBITS5_0                                                                                         \
155     ((uint32_t)0x00000500) /*!< Unmask DAC channel LFSR bit[5:0] for noise wave generation                             \
156                             */
157 #define DAC_UNMASK_LFSRBITS6_0                                                                                         \
158     ((uint32_t)0x00000600) /*!< Unmask DAC channel LFSR bit[6:0] for noise wave generation                             \
159                             */
160 #define DAC_UNMASK_LFSRBITS7_0                                                                                         \
161     ((uint32_t)0x00000700) /*!< Unmask DAC channel LFSR bit[7:0] for noise wave generation                             \
162                             */
163 #define DAC_UNMASK_LFSRBITS8_0                                                                                         \
164     ((uint32_t)0x00000800) /*!< Unmask DAC channel LFSR bit[8:0] for noise wave generation                             \
165                             */
166 #define DAC_UNMASK_LFSRBITS9_0                                                                                         \
167     ((uint32_t)0x00000900) /*!< Unmask DAC channel LFSR bit[9:0] for noise wave generation                             \
168                             */
169 #define DAC_UNMASK_LFSRBITS10_0                                                                                        \
170     ((uint32_t)0x00000A00) /*!< Unmask DAC channel LFSR bit[10:0] for noise wave generation */
171 #define DAC_UNMASK_LFSRBITS11_0                                                                                        \
172     ((uint32_t)0x00000B00)                     /*!< Unmask DAC channel LFSR bit[11:0] for noise wave generation */
173 #define DAC_TRIAMP_1    ((uint32_t)0x00000000) /*!< Select max triangle amplitude of 1 */
174 #define DAC_TRIAMP_3    ((uint32_t)0x00000100) /*!< Select max triangle amplitude of 3 */
175 #define DAC_TRIAMP_7    ((uint32_t)0x00000200) /*!< Select max triangle amplitude of 7 */
176 #define DAC_TRIAMP_15   ((uint32_t)0x00000300) /*!< Select max triangle amplitude of 15 */
177 #define DAC_TRIAMP_31   ((uint32_t)0x00000400) /*!< Select max triangle amplitude of 31 */
178 #define DAC_TRIAMP_63   ((uint32_t)0x00000500) /*!< Select max triangle amplitude of 63 */
179 #define DAC_TRIAMP_127  ((uint32_t)0x00000600) /*!< Select max triangle amplitude of 127 */
180 #define DAC_TRIAMP_255  ((uint32_t)0x00000700) /*!< Select max triangle amplitude of 255 */
181 #define DAC_TRIAMP_511  ((uint32_t)0x00000800) /*!< Select max triangle amplitude of 511 */
182 #define DAC_TRIAMP_1023 ((uint32_t)0x00000900) /*!< Select max triangle amplitude of 1023 */
183 #define DAC_TRIAMP_2047 ((uint32_t)0x00000A00) /*!< Select max triangle amplitude of 2047 */
184 #define DAC_TRIAMP_4095 ((uint32_t)0x00000B00) /*!< Select max triangle amplitude of 4095 */
185 
186 #define IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(VALUE)                                                                   \
187     (((VALUE) == DAC_UNMASK_LFSRBIT0) || ((VALUE) == DAC_UNMASK_LFSRBITS1_0) || ((VALUE) == DAC_UNMASK_LFSRBITS2_0)    \
188      || ((VALUE) == DAC_UNMASK_LFSRBITS3_0) || ((VALUE) == DAC_UNMASK_LFSRBITS4_0)                                     \
189      || ((VALUE) == DAC_UNMASK_LFSRBITS5_0) || ((VALUE) == DAC_UNMASK_LFSRBITS6_0)                                     \
190      || ((VALUE) == DAC_UNMASK_LFSRBITS7_0) || ((VALUE) == DAC_UNMASK_LFSRBITS8_0)                                     \
191      || ((VALUE) == DAC_UNMASK_LFSRBITS9_0) || ((VALUE) == DAC_UNMASK_LFSRBITS10_0)                                    \
192      || ((VALUE) == DAC_UNMASK_LFSRBITS11_0) || ((VALUE) == DAC_TRIAMP_1) || ((VALUE) == DAC_TRIAMP_3)                 \
193      || ((VALUE) == DAC_TRIAMP_7) || ((VALUE) == DAC_TRIAMP_15) || ((VALUE) == DAC_TRIAMP_31)                          \
194      || ((VALUE) == DAC_TRIAMP_63) || ((VALUE) == DAC_TRIAMP_127) || ((VALUE) == DAC_TRIAMP_255)                       \
195      || ((VALUE) == DAC_TRIAMP_511) || ((VALUE) == DAC_TRIAMP_1023) || ((VALUE) == DAC_TRIAMP_2047)                    \
196      || ((VALUE) == DAC_TRIAMP_4095))
197 /**
198  * @}
199  */
200 
201 /** @addtogroup DAC_output_buffer
202  * @{
203  */
204 
205 #define DAC_BUFFOUTPUT_ENABLE             ((uint32_t)0x00000002)
206 #define DAC_BUFFOUTPUT_DISABLE            ((uint32_t)0x00000000)
207 #define IS_DAC_OUTPUT_BUFFER_STATE(STATE) (((STATE) == DAC_BUFFOUTPUT_ENABLE) || ((STATE) == DAC_BUFFOUTPUT_DISABLE))
208 /**
209  * @}
210  */
211 
212 
213 /**
214  * @}
215  */
216 
217 /** @addtogroup DAC_data_alignment
218  * @{
219  */
220 
221 #define DAC_ALIGN_R_12BIT ((uint32_t)0x00000000)
222 #define DAC_ALIGN_L_12BIT ((uint32_t)0x00000004)
223 #define DAC_ALIGN_R_8BIT  ((uint32_t)0x00000008)
224 #define IS_DAC_ALIGN(ALIGN)                                                                                            \
225     (((ALIGN) == DAC_ALIGN_R_12BIT) || ((ALIGN) == DAC_ALIGN_L_12BIT) || ((ALIGN) == DAC_ALIGN_R_8BIT))
226 /**
227  * @}
228  */
229 
230 /** @addtogroup DAC_wave_generation
231  * @{
232  */
233 
234 #define DAC_WAVE_NOISE    ((uint32_t)0x00000040)
235 #define DAC_WAVE_TRIANGLE ((uint32_t)0x00000080)
236 #define IS_DAC_WAVE(WAVE) (((WAVE) == DAC_WAVE_NOISE) || ((WAVE) == DAC_WAVE_TRIANGLE))
237 /**
238  * @}
239  */
240 
241 /** @addtogroup DAC_data
242  * @{
243  */
244 
245 #define IS_DAC_DATA(DATA) ((DATA) <= 0xFFF0)
246 /**
247  * @}
248  */
249 
250 /**
251  * @}
252  */
253 
254 /** @addtogroup DAC_Exported_Macros
255  * @{
256  */
257 
258 /**
259  * @}
260  */
261 
262 /** @addtogroup DAC_Exported_Functions
263  * @{
264  */
265 
266 void DAC_DeInit(void);
267 void DAC_Init(DAC_InitType* DAC_InitStruct);
268 void DAC_ClearStruct(DAC_InitType* DAC_InitStruct);
269 void DAC_Enable(FunctionalState Cmd);
270 
271 void DAC_DmaEnable(FunctionalState Cmd);
272 void DAC_SoftTrgEnable(FunctionalState Cmd);
273 void DAC_SoftwareTrgEnable(FunctionalState Cmd);
274 void DAC_WaveGenerationEnable(uint32_t DAC_Wave, FunctionalState Cmd);
275 void DAC_SetChData(uint32_t DAC_Align, uint16_t Data);
276 uint16_t DAC_GetOutputDataVal(void);
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif /*__N32L43X_DAC_H__ */
283        /**
284         * @}
285         */
286 
287 /**
288  * @}
289  */
290 
291 /**
292  * @}
293  */
294