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. |
||
9 | mjames | 6 | * This file provides firmware functions to manage the extended |
7 | * functionalities of the DAC peripheral. |
||
2 | mjames | 8 | * |
9 | mjames | 9 | * |
10 | @verbatim |
||
2 | mjames | 11 | ============================================================================== |
12 | ##### How to use this driver ##### |
||
13 | ============================================================================== |
||
9 | mjames | 14 | [..] |
15 | *** Dual mode IO operation *** |
||
16 | ============================== |
||
17 | (+) When Dual mode is enabled (i.e. DAC Channel1 and Channel2 are used simultaneously) : |
||
2 | mjames | 18 | Use HAL_DACEx_DualGetValue() to get digital data to be converted and use |
9 | mjames | 19 | HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in |
20 | Channel 1 and Channel 2. |
||
21 | |||
22 | *** Signal generation operation *** |
||
23 | =================================== |
||
2 | mjames | 24 | (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal. |
25 | (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal. |
||
9 | mjames | 26 | |
27 | @endverbatim |
||
2 | mjames | 28 | ****************************************************************************** |
29 | * @attention |
||
30 | * |
||
9 | mjames | 31 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
32 | * All rights reserved.</center></h2> |
||
2 | mjames | 33 | * |
9 | mjames | 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 |
||
2 | mjames | 38 | * |
39 | ****************************************************************************** |
||
9 | mjames | 40 | */ |
2 | mjames | 41 | |
42 | |||
43 | /* Includes ------------------------------------------------------------------*/ |
||
44 | #include "stm32f1xx_hal.h" |
||
45 | |||
46 | /** @addtogroup STM32F1xx_HAL_Driver |
||
47 | * @{ |
||
48 | */ |
||
49 | |||
9 | mjames | 50 | #ifdef HAL_DAC_MODULE_ENABLED |
51 | |||
52 | #if defined(DAC) |
||
53 | |||
2 | mjames | 54 | /** @defgroup DACEx DACEx |
9 | mjames | 55 | * @brief DAC Extended HAL module driver |
2 | mjames | 56 | * @{ |
9 | mjames | 57 | */ |
2 | mjames | 58 | |
59 | /* Private typedef -----------------------------------------------------------*/ |
||
60 | /* Private define ------------------------------------------------------------*/ |
||
61 | /* Private macro -------------------------------------------------------------*/ |
||
62 | /* Private variables ---------------------------------------------------------*/ |
||
63 | /* Private function prototypes -----------------------------------------------*/ |
||
64 | /* Exported functions --------------------------------------------------------*/ |
||
9 | mjames | 65 | |
2 | mjames | 66 | /** @defgroup DACEx_Exported_Functions DACEx Exported Functions |
67 | * @{ |
||
68 | */ |
||
69 | |||
9 | mjames | 70 | /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions |
71 | * @brief Extended IO operation functions |
||
72 | * |
||
73 | @verbatim |
||
2 | mjames | 74 | ============================================================================== |
75 | ##### Extended features functions ##### |
||
9 | mjames | 76 | ============================================================================== |
2 | mjames | 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. |
||
9 | mjames | 84 | |
2 | mjames | 85 | @endverbatim |
86 | * @{ |
||
87 | */ |
||
88 | |||
89 | /** |
||
9 | mjames | 90 | * @brief Enable or disable the selected DAC channel wave generation. |
91 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
2 | mjames | 92 | * the configuration information for the specified DAC. |
9 | mjames | 93 | * @param Channel The selected DAC channel. |
2 | mjames | 94 | * This parameter can be one of the following values: |
9 | mjames | 95 | * @arg DAC_CHANNEL_1: DAC Channel1 selected |
96 | * @arg DAC_CHANNEL_2: DAC Channel2 selected |
||
97 | * @param Amplitude Select max triangle amplitude. |
||
2 | mjames | 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 |
||
9 | mjames | 110 | * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095 |
2 | mjames | 111 | * @retval HAL status |
112 | */ |
||
9 | mjames | 113 | HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude) |
114 | { |
||
2 | mjames | 115 | /* Check the parameters */ |
116 | assert_param(IS_DAC_CHANNEL(Channel)); |
||
117 | assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)); |
||
9 | mjames | 118 | |
2 | mjames | 119 | /* Process locked */ |
120 | __HAL_LOCK(hdac); |
||
9 | mjames | 121 | |
2 | mjames | 122 | /* Change DAC state */ |
123 | hdac->State = HAL_DAC_STATE_BUSY; |
||
9 | mjames | 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 | |||
2 | mjames | 128 | /* Change DAC state */ |
129 | hdac->State = HAL_DAC_STATE_READY; |
||
9 | mjames | 130 | |
2 | mjames | 131 | /* Process unlocked */ |
132 | __HAL_UNLOCK(hdac); |
||
9 | mjames | 133 | |
2 | mjames | 134 | /* Return function status */ |
135 | return HAL_OK; |
||
136 | } |
||
137 | |||
138 | /** |
||
9 | mjames | 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. |
||
2 | mjames | 143 | * This parameter can be one of the following values: |
9 | mjames | 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: |
||
2 | mjames | 148 | * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation |
9 | mjames | 149 | * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation |
2 | mjames | 150 | * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation |
9 | mjames | 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 |
||
2 | mjames | 160 | * @retval HAL status |
161 | */ |
||
9 | mjames | 162 | HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude) |
163 | { |
||
2 | mjames | 164 | /* Check the parameters */ |
165 | assert_param(IS_DAC_CHANNEL(Channel)); |
||
166 | assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)); |
||
9 | mjames | 167 | |
2 | mjames | 168 | /* Process locked */ |
169 | __HAL_LOCK(hdac); |
||
9 | mjames | 170 | |
2 | mjames | 171 | /* Change DAC state */ |
172 | hdac->State = HAL_DAC_STATE_BUSY; |
||
9 | mjames | 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 | |||
2 | mjames | 177 | /* Change DAC state */ |
178 | hdac->State = HAL_DAC_STATE_READY; |
||
9 | mjames | 179 | |
2 | mjames | 180 | /* Process unlocked */ |
181 | __HAL_UNLOCK(hdac); |
||
9 | mjames | 182 | |
2 | mjames | 183 | /* Return function status */ |
184 | return HAL_OK; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @brief Set the specified data holding register value for dual DAC channel. |
||
9 | mjames | 189 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
2 | mjames | 190 | * the configuration information for the specified DAC. |
9 | mjames | 191 | * @param Alignment Specifies the data alignment for dual channel DAC. |
2 | mjames | 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 |
||
9 | mjames | 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. |
||
2 | mjames | 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 | */ |
||
9 | mjames | 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 | |||
2 | mjames | 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)); |
||
9 | mjames | 211 | |
2 | mjames | 212 | /* Calculate and set dual DAC data holding register value */ |
213 | if (Alignment == DAC_ALIGN_8B_R) |
||
214 | { |
||
9 | mjames | 215 | data = ((uint32_t)Data2 << 8U) | Data1; |
2 | mjames | 216 | } |
217 | else |
||
218 | { |
||
219 | data = ((uint32_t)Data2 << 16U) | Data1; |
||
220 | } |
||
9 | mjames | 221 | |
2 | mjames | 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; |
||
9 | mjames | 227 | |
2 | mjames | 228 | /* Return function status */ |
229 | return HAL_OK; |
||
230 | } |
||
231 | |||
232 | /** |
||
9 | mjames | 233 | * @brief Conversion complete callback in non-blocking mode for Channel2. |
234 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
2 | mjames | 235 | * the configuration information for the specified DAC. |
236 | * @retval None |
||
237 | */ |
||
9 | mjames | 238 | __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac) |
2 | mjames | 239 | { |
240 | /* Prevent unused argument(s) compilation warning */ |
||
241 | UNUSED(hdac); |
||
9 | mjames | 242 | |
243 | /* NOTE : This function should not be modified, when the callback is needed, |
||
2 | mjames | 244 | the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file |
245 | */ |
||
246 | } |
||
247 | |||
248 | /** |
||
9 | mjames | 249 | * @brief Conversion half DMA transfer callback in non-blocking mode for Channel2. |
250 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
2 | mjames | 251 | * the configuration information for the specified DAC. |
252 | * @retval None |
||
253 | */ |
||
9 | mjames | 254 | __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac) |
2 | mjames | 255 | { |
256 | /* Prevent unused argument(s) compilation warning */ |
||
257 | UNUSED(hdac); |
||
9 | mjames | 258 | |
259 | /* NOTE : This function should not be modified, when the callback is needed, |
||
2 | mjames | 260 | the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file |
261 | */ |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @brief Error DAC callback for Channel2. |
||
9 | mjames | 266 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
2 | mjames | 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); |
||
9 | mjames | 274 | |
275 | /* NOTE : This function should not be modified, when the callback is needed, |
||
2 | mjames | 276 | the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file |
277 | */ |
||
278 | } |
||
279 | |||
280 | /** |
||
9 | mjames | 281 | * @brief DMA underrun DAC callback for Channel2. |
282 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
2 | mjames | 283 | * the configuration information for the specified DAC. |
284 | * @retval None |
||
285 | */ |
||
9 | mjames | 286 | __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac) |
2 | mjames | 287 | { |
288 | /* Prevent unused argument(s) compilation warning */ |
||
289 | UNUSED(hdac); |
||
290 | |||
9 | mjames | 291 | /* NOTE : This function should not be modified, when the callback is needed, |
2 | mjames | 292 | the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file |
293 | */ |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @} |
||
298 | */ |
||
299 | |||
9 | mjames | 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 | * @{ |
||
2 | mjames | 312 | */ |
313 | |||
314 | /** |
||
9 | mjames | 315 | * @brief Return the last data output value of the selected DAC channel. |
316 | * @param hdac pointer to a DAC_HandleTypeDef structure that contains |
||
2 | mjames | 317 | * the configuration information for the specified DAC. |
9 | mjames | 318 | * @retval The selected DAC channel data output value. |
2 | mjames | 319 | */ |
9 | mjames | 320 | uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac) |
2 | mjames | 321 | { |
9 | mjames | 322 | uint32_t tmp = 0U; |
2 | mjames | 323 | |
9 | mjames | 324 | tmp |= hdac->Instance->DOR1; |
2 | mjames | 325 | |
9 | mjames | 326 | tmp |= hdac->Instance->DOR2 << 16U; |
2 | mjames | 327 | |
9 | mjames | 328 | /* Returns the DAC channel data output register value */ |
329 | return tmp; |
||
2 | mjames | 330 | } |
331 | |||
332 | /** |
||
9 | mjames | 333 | * @} |
2 | mjames | 334 | */ |
335 | |||
336 | /** |
||
337 | * @} |
||
338 | */ |
||
339 | |||
9 | mjames | 340 | /* Private functions ---------------------------------------------------------*/ |
341 | /** @defgroup DACEx_Private_Functions DACEx private functions |
||
342 | * @brief Extended private functions |
||
343 | * @{ |
||
2 | mjames | 344 | */ |
345 | |||
346 | /** |
||
9 | mjames | 347 | * @brief DMA conversion complete callback. |
348 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
||
2 | mjames | 349 | * the configuration information for the specified DMA module. |
350 | * @retval None |
||
351 | */ |
||
9 | mjames | 352 | void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma) |
2 | mjames | 353 | { |
9 | mjames | 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; |
||
2 | mjames | 363 | } |
364 | |||
365 | /** |
||
9 | mjames | 366 | * @brief DMA half transfer complete callback. |
367 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
||
2 | mjames | 368 | * the configuration information for the specified DMA module. |
369 | * @retval None |
||
370 | */ |
||
9 | mjames | 371 | void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma) |
2 | mjames | 372 | { |
9 | mjames | 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 */ |
||
2 | mjames | 380 | } |
381 | |||
382 | /** |
||
9 | mjames | 383 | * @brief DMA error callback. |
384 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
||
2 | mjames | 385 | * the configuration information for the specified DMA module. |
386 | * @retval None |
||
387 | */ |
||
9 | mjames | 388 | void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma) |
2 | mjames | 389 | { |
9 | mjames | 390 | DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
391 | |||
2 | mjames | 392 | /* Set DAC error code to DMA error */ |
393 | hdac->ErrorCode |= HAL_DAC_ERROR_DMA; |
||
9 | mjames | 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; |
||
2 | mjames | 402 | } |
403 | |||
404 | /** |
||
405 | * @} |
||
406 | */ |
||
407 | |||
408 | /** |
||
409 | * @} |
||
410 | */ |
||
411 | |||
9 | mjames | 412 | #endif /* DAC */ |
413 | |||
414 | #endif /* HAL_DAC_MODULE_ENABLED */ |
||
415 | |||
2 | mjames | 416 | /** |
417 | * @} |
||
418 | */ |
||
419 | |||
420 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |