Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32f1xx_hal_dac_ex.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief DAC HAL module driver. |
||
6 | * This file provides firmware functions to manage the extended |
||
7 | * functionalities of the DAC peripheral. |
||
8 | * |
||
9 | * |
||
10 | @verbatim |
||
11 | ============================================================================== |
||
12 | ##### How to use this driver ##### |
||
13 | ============================================================================== |
||
14 | [..] |
||
15 | *** Dual mode IO operation *** |
||
16 | ============================== |
||
17 | (+) When Dual mode is enabled (i.e. DAC Channel1 and Channel2 are used simultaneously) : |
||
18 | Use HAL_DACEx_DualGetValue() to get digital data to be converted and use |
||
19 | HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in |
||
20 | Channel 1 and Channel 2. |
||
21 | |||
22 | *** Signal generation operation *** |
||
23 | =================================== |
||
24 | (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal. |
||
25 | (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal. |
||
26 | |||
27 | @endverbatim |
||
28 | ****************************************************************************** |
||
29 | * @attention |
||
30 | * |
||
31 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
32 | * All rights reserved.</center></h2> |
||
33 | * |
||
34 | * This software component is licensed by ST under BSD 3-Clause license, |
||
35 | * the "License"; You may not use this file except in compliance with the |
||
36 | * License. You may obtain a copy of the License at: |
||
37 | * opensource.org/licenses/BSD-3-Clause |
||
38 | * |
||
39 | ****************************************************************************** |
||
40 | */ |
||
41 | |||
42 | |||
43 | /* Includes ------------------------------------------------------------------*/ |
||
44 | #include "stm32f1xx_hal.h" |
||
45 | |||
46 | /** @addtogroup STM32F1xx_HAL_Driver |
||
47 | * @{ |
||
48 | */ |
||
49 | |||
50 | #ifdef HAL_DAC_MODULE_ENABLED |
||
51 | |||
52 | #if defined(DAC) |
||
53 | |||
54 | /** @defgroup DACEx DACEx |
||
55 | * @brief DAC Extended HAL module driver |
||
56 | * @{ |
||
57 | */ |
||
58 | |||
59 | /* Private typedef -----------------------------------------------------------*/ |
||
60 | /* Private define ------------------------------------------------------------*/ |
||
61 | /* Private macro -------------------------------------------------------------*/ |
||
62 | /* Private variables ---------------------------------------------------------*/ |
||
63 | /* Private function prototypes -----------------------------------------------*/ |
||
64 | /* Exported functions --------------------------------------------------------*/ |
||
65 | |||
66 | /** @defgroup DACEx_Exported_Functions DACEx Exported Functions |
||
67 | * @{ |
||
68 | */ |
||
69 | |||
70 | /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions |
||
71 | * @brief Extended IO operation functions |
||
72 | * |
||
73 | @verbatim |
||
74 | ============================================================================== |
||
75 | ##### Extended features functions ##### |
||
76 | ============================================================================== |
||
77 | [..] This section provides functions allowing to: |
||
78 | (+) Start conversion. |
||
79 | (+) Stop conversion. |
||
80 | (+) Start conversion and enable DMA transfer. |
||
81 | (+) Stop conversion and disable DMA transfer. |
||
82 | (+) Get result of conversion. |
||
83 | (+) Get result of dual mode conversion. |
||
84 | |||
85 | @endverbatim |
||
86 | * @{ |
||
87 | */ |
||
88 | |||
89 | /** |
||
90 | * @brief Enable or disable the selected DAC channel wave generation. |
||
91 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
92 | * the configuration information for the specified DAC. |
||
93 | * @param Channel The selected DAC channel. |
||
94 | * This parameter can be one of the following values: |
||
95 | * @arg DAC_CHANNEL_1: DAC Channel1 selected |
||
96 | * @arg DAC_CHANNEL_2: DAC Channel2 selected |
||
97 | * @param Amplitude Select max triangle amplitude. |
||
98 | * This parameter can be one of the following values: |
||
99 | * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1 |
||
100 | * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3 |
||
101 | * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7 |
||
102 | * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15 |
||
103 | * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31 |
||
104 | * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63 |
||
105 | * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127 |
||
106 | * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255 |
||
107 | * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511 |
||
108 | * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023 |
||
109 | * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047 |
||
110 | * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095 |
||
111 | * @retval HAL status |
||
112 | */ |
||
113 | HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude) |
||
114 | { |
||
115 | /* Check the parameters */ |
||
116 | assert_param(IS_DAC_CHANNEL(Channel)); |
||
117 | assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)); |
||
118 | |||
119 | /* Process locked */ |
||
120 | __HAL_LOCK(hdac); |
||
121 | |||
122 | /* Change DAC state */ |
||
123 | hdac->State = HAL_DAC_STATE_BUSY; |
||
124 | |||
125 | /* Enable the triangle wave generation for the selected DAC channel */ |
||
126 | MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL)); |
||
127 | |||
128 | /* Change DAC state */ |
||
129 | hdac->State = HAL_DAC_STATE_READY; |
||
130 | |||
131 | /* Process unlocked */ |
||
132 | __HAL_UNLOCK(hdac); |
||
133 | |||
134 | /* Return function status */ |
||
135 | return HAL_OK; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @brief Enable or disable the selected DAC channel wave generation. |
||
140 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
141 | * the configuration information for the specified DAC. |
||
142 | * @param Channel The selected DAC channel. |
||
143 | * This parameter can be one of the following values: |
||
144 | * @arg DAC_CHANNEL_1: DAC Channel1 selected |
||
145 | * @arg DAC_CHANNEL_2: DAC Channel2 selected |
||
146 | * @param Amplitude Unmask DAC channel LFSR for noise wave generation. |
||
147 | * This parameter can be one of the following values: |
||
148 | * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation |
||
149 | * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation |
||
150 | * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation |
||
151 | * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation |
||
152 | * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation |
||
153 | * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation |
||
154 | * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation |
||
155 | * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation |
||
156 | * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation |
||
157 | * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation |
||
158 | * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation |
||
159 | * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation |
||
160 | * @retval HAL status |
||
161 | */ |
||
162 | HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude) |
||
163 | { |
||
164 | /* Check the parameters */ |
||
165 | assert_param(IS_DAC_CHANNEL(Channel)); |
||
166 | assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)); |
||
167 | |||
168 | /* Process locked */ |
||
169 | __HAL_LOCK(hdac); |
||
170 | |||
171 | /* Change DAC state */ |
||
172 | hdac->State = HAL_DAC_STATE_BUSY; |
||
173 | |||
174 | /* Enable the noise wave generation for the selected DAC channel */ |
||
175 | MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL)); |
||
176 | |||
177 | /* Change DAC state */ |
||
178 | hdac->State = HAL_DAC_STATE_READY; |
||
179 | |||
180 | /* Process unlocked */ |
||
181 | __HAL_UNLOCK(hdac); |
||
182 | |||
183 | /* Return function status */ |
||
184 | return HAL_OK; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @brief Set the specified data holding register value for dual DAC channel. |
||
189 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
190 | * the configuration information for the specified DAC. |
||
191 | * @param Alignment Specifies the data alignment for dual channel DAC. |
||
192 | * This parameter can be one of the following values: |
||
193 | * DAC_ALIGN_8B_R: 8bit right data alignment selected |
||
194 | * DAC_ALIGN_12B_L: 12bit left data alignment selected |
||
195 | * DAC_ALIGN_12B_R: 12bit right data alignment selected |
||
196 | * @param Data1 Data for DAC Channel1 to be loaded in the selected data holding register. |
||
197 | * @param Data2 Data for DAC Channel2 to be loaded in the selected data holding register. |
||
198 | * @note In dual mode, a unique register access is required to write in both |
||
199 | * DAC channels at the same time. |
||
200 | * @retval HAL status |
||
201 | */ |
||
202 | HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2) |
||
203 | { |
||
204 | uint32_t data; |
||
205 | uint32_t tmp; |
||
206 | |||
207 | /* Check the parameters */ |
||
208 | assert_param(IS_DAC_ALIGN(Alignment)); |
||
209 | assert_param(IS_DAC_DATA(Data1)); |
||
210 | assert_param(IS_DAC_DATA(Data2)); |
||
211 | |||
212 | /* Calculate and set dual DAC data holding register value */ |
||
213 | if (Alignment == DAC_ALIGN_8B_R) |
||
214 | { |
||
215 | data = ((uint32_t)Data2 << 8U) | Data1; |
||
216 | } |
||
217 | else |
||
218 | { |
||
219 | data = ((uint32_t)Data2 << 16U) | Data1; |
||
220 | } |
||
221 | |||
222 | tmp = (uint32_t)hdac->Instance; |
||
223 | tmp += DAC_DHR12RD_ALIGNMENT(Alignment); |
||
224 | |||
225 | /* Set the dual DAC selected data holding register */ |
||
226 | *(__IO uint32_t *)tmp = data; |
||
227 | |||
228 | /* Return function status */ |
||
229 | return HAL_OK; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @brief Conversion complete callback in non-blocking mode for Channel2. |
||
234 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
235 | * the configuration information for the specified DAC. |
||
236 | * @retval None |
||
237 | */ |
||
238 | __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac) |
||
239 | { |
||
240 | /* Prevent unused argument(s) compilation warning */ |
||
241 | UNUSED(hdac); |
||
242 | |||
243 | /* NOTE : This function should not be modified, when the callback is needed, |
||
244 | the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file |
||
245 | */ |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @brief Conversion half DMA transfer callback in non-blocking mode for Channel2. |
||
250 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
251 | * the configuration information for the specified DAC. |
||
252 | * @retval None |
||
253 | */ |
||
254 | __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac) |
||
255 | { |
||
256 | /* Prevent unused argument(s) compilation warning */ |
||
257 | UNUSED(hdac); |
||
258 | |||
259 | /* NOTE : This function should not be modified, when the callback is needed, |
||
260 | the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file |
||
261 | */ |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @brief Error DAC callback for Channel2. |
||
266 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
267 | * the configuration information for the specified DAC. |
||
268 | * @retval None |
||
269 | */ |
||
270 | __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac) |
||
271 | { |
||
272 | /* Prevent unused argument(s) compilation warning */ |
||
273 | UNUSED(hdac); |
||
274 | |||
275 | /* NOTE : This function should not be modified, when the callback is needed, |
||
276 | the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file |
||
277 | */ |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @brief DMA underrun DAC callback for Channel2. |
||
282 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
283 | * the configuration information for the specified DAC. |
||
284 | * @retval None |
||
285 | */ |
||
286 | __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac) |
||
287 | { |
||
288 | /* Prevent unused argument(s) compilation warning */ |
||
289 | UNUSED(hdac); |
||
290 | |||
291 | /* NOTE : This function should not be modified, when the callback is needed, |
||
292 | the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file |
||
293 | */ |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @} |
||
298 | */ |
||
299 | |||
300 | /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions |
||
301 | * @brief Extended Peripheral Control functions |
||
302 | * |
||
303 | @verbatim |
||
304 | ============================================================================== |
||
305 | ##### Peripheral Control functions ##### |
||
306 | ============================================================================== |
||
307 | [..] This section provides functions allowing to: |
||
308 | (+) Set the specified data holding register value for DAC channel. |
||
309 | |||
310 | @endverbatim |
||
311 | * @{ |
||
312 | */ |
||
313 | |||
314 | /** |
||
315 | * @brief Return the last data output value of the selected DAC channel. |
||
316 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
317 | * the configuration information for the specified DAC. |
||
318 | * @retval The selected DAC channel data output value. |
||
319 | */ |
||
320 | uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac) |
||
321 | { |
||
322 | uint32_t tmp = 0U; |
||
323 | |||
324 | tmp |= hdac->Instance->DOR1; |
||
325 | |||
326 | tmp |= hdac->Instance->DOR2 << 16U; |
||
327 | |||
328 | /* Returns the DAC channel data output register value */ |
||
329 | return tmp; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @} |
||
334 | */ |
||
335 | |||
336 | /** |
||
337 | * @} |
||
338 | */ |
||
339 | |||
340 | /* Private functions ---------------------------------------------------------*/ |
||
341 | /** @defgroup DACEx_Private_Functions DACEx private functions |
||
342 | * @brief Extended private functions |
||
343 | * @{ |
||
344 | */ |
||
345 | |||
346 | /** |
||
347 | * @brief DMA conversion complete callback. |
||
348 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
||
349 | * the configuration information for the specified DMA module. |
||
350 | * @retval None |
||
351 | */ |
||
352 | void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma) |
||
353 | { |
||
354 | DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
355 | |||
356 | #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) |
||
357 | hdac->ConvCpltCallbackCh2(hdac); |
||
358 | #else |
||
359 | HAL_DACEx_ConvCpltCallbackCh2(hdac); |
||
360 | #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ |
||
361 | |||
362 | hdac->State = HAL_DAC_STATE_READY; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @brief DMA half transfer complete callback. |
||
367 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
||
368 | * the configuration information for the specified DMA module. |
||
369 | * @retval None |
||
370 | */ |
||
371 | void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma) |
||
372 | { |
||
373 | DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
374 | /* Conversion complete callback */ |
||
375 | #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) |
||
376 | hdac->ConvHalfCpltCallbackCh2(hdac); |
||
377 | #else |
||
378 | HAL_DACEx_ConvHalfCpltCallbackCh2(hdac); |
||
379 | #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @brief DMA error callback. |
||
384 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
||
385 | * the configuration information for the specified DMA module. |
||
386 | * @retval None |
||
387 | */ |
||
388 | void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma) |
||
389 | { |
||
390 | DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
391 | |||
392 | /* Set DAC error code to DMA error */ |
||
393 | hdac->ErrorCode |= HAL_DAC_ERROR_DMA; |
||
394 | |||
395 | #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) |
||
396 | hdac->ErrorCallbackCh2(hdac); |
||
397 | #else |
||
398 | HAL_DACEx_ErrorCallbackCh2(hdac); |
||
399 | #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ |
||
400 | |||
401 | hdac->State = HAL_DAC_STATE_READY; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @} |
||
406 | */ |
||
407 | |||
408 | /** |
||
409 | * @} |
||
410 | */ |
||
411 | |||
412 | #endif /* DAC */ |
||
413 | |||
414 | #endif /* HAL_DAC_MODULE_ENABLED */ |
||
415 | |||
416 | /** |
||
417 | * @} |
||
418 | */ |
||
419 | |||
420 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |