38
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file stm32f4xx_hal_dac.c
|
|
4 * @author MCD Application Team
|
|
5 * @version V1.2.0
|
|
6 * @date 26-December-2014
|
|
7 * @brief DAC HAL module driver.
|
|
8 * This file provides firmware functions to manage the following
|
|
9 * functionalities of the Digital to Analog Converter (DAC) peripheral:
|
|
10 * + Initialization and de-initialization functions
|
|
11 * + IO operation functions
|
|
12 * + Peripheral Control functions
|
|
13 * + Peripheral State and Errors functions
|
|
14 *
|
|
15 *
|
|
16 @verbatim
|
|
17 ==============================================================================
|
|
18 ##### DAC Peripheral features #####
|
|
19 ==============================================================================
|
|
20 [..]
|
|
21 *** DAC Channels ***
|
|
22 ====================
|
|
23 [..]
|
|
24 The device integrates two 12-bit Digital Analog Converters that can
|
|
25 be used independently or simultaneously (dual mode):
|
|
26 (#) DAC channel1 with DAC_OUT1 (PA4) as output
|
|
27 (#) DAC channel2 with DAC_OUT2 (PA5) as output
|
|
28
|
|
29 *** DAC Triggers ***
|
|
30 ====================
|
|
31 [..]
|
|
32 Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
|
|
33 and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
|
|
34 [..]
|
|
35 Digital to Analog conversion can be triggered by:
|
|
36 (#) External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_TRIGGER_EXT_IT9.
|
|
37 The used pin (GPIOx_Pin9) must be configured in input mode.
|
|
38
|
|
39 (#) Timers TRGO: TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8
|
|
40 (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
|
|
41
|
|
42 (#) Software using DAC_TRIGGER_SOFTWARE
|
|
43
|
|
44 *** DAC Buffer mode feature ***
|
|
45 ===============================
|
|
46 [..]
|
|
47 Each DAC channel integrates an output buffer that can be used to
|
|
48 reduce the output impedance, and to drive external loads directly
|
|
49 without having to add an external operational amplifier.
|
|
50 To enable, the output buffer use
|
|
51 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
|
|
52 [..]
|
|
53 (@) Refer to the device datasheet for more details about output
|
|
54 impedance value with and without output buffer.
|
|
55
|
|
56 *** DAC wave generation feature ***
|
|
57 ===================================
|
|
58 [..]
|
|
59 Both DAC channels can be used to generate
|
|
60 (#) Noise wave
|
|
61 (#) Triangle wave
|
|
62
|
|
63 *** DAC data format ***
|
|
64 =======================
|
|
65 [..]
|
|
66 The DAC data format can be:
|
|
67 (#) 8-bit right alignment using DAC_ALIGN_8B_R
|
|
68 (#) 12-bit left alignment using DAC_ALIGN_12B_L
|
|
69 (#) 12-bit right alignment using DAC_ALIGN_12B_R
|
|
70
|
|
71 *** DAC data value to voltage correspondence ***
|
|
72 ================================================
|
|
73 [..]
|
|
74 The analog output voltage on each DAC channel pin is determined
|
|
75 by the following equation:
|
|
76 DAC_OUTx = VREF+ * DOR / 4095
|
|
77 with DOR is the Data Output Register
|
|
78 VEF+ is the input voltage reference (refer to the device datasheet)
|
|
79 e.g. To set DAC_OUT1 to 0.7V, use
|
|
80 Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
|
|
81
|
|
82 *** DMA requests ***
|
|
83 =====================
|
|
84 [..]
|
|
85 A DMA1 request can be generated when an external trigger (but not
|
|
86 a software trigger) occurs if DMA1 requests are enabled using
|
|
87 HAL_DAC_Start_DMA()
|
|
88 [..]
|
|
89 DMA1 requests are mapped as following:
|
|
90 (#) DAC channel1 : mapped on DMA1 Stream5 channel7 which must be
|
|
91 already configured
|
|
92 (#) DAC channel2 : mapped on DMA1 Stream6 channel7 which must be
|
|
93 already configured
|
|
94
|
|
95 -@- For Dual mode and specific signal (Triangle and noise) generation please
|
|
96 refer to Extension Features Driver description
|
|
97
|
|
98
|
|
99 ##### How to use this driver #####
|
|
100 ==============================================================================
|
|
101 [..]
|
|
102 (+) DAC APB clock must be enabled to get write access to DAC
|
|
103 registers using HAL_DAC_Init()
|
|
104 (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
|
|
105 (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
|
|
106 (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
|
|
107
|
|
108 *** Polling mode IO operation ***
|
|
109 =================================
|
|
110 [..]
|
|
111 (+) Start the DAC peripheral using HAL_DAC_Start()
|
|
112 (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
|
|
113 (+) Stop the DAC peripheral using HAL_DAC_Stop()
|
|
114
|
|
115 *** DMA mode IO operation ***
|
|
116 ==============================
|
|
117 [..]
|
|
118 (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
|
|
119 of data to be transferred at each end of conversion
|
|
120 (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
|
|
121 function is executed and user can add his own code by customization of function pointer
|
|
122 HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
|
|
123 (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
|
|
124 add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
|
|
125 (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
|
|
126
|
|
127 *** DAC HAL driver macros list ***
|
|
128 =============================================
|
|
129 [..]
|
|
130 Below the list of most used macros in DAC HAL driver.
|
|
131
|
|
132 (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
|
|
133 (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
|
|
134 (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
|
|
135 (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
|
|
136
|
|
137 [..]
|
|
138 (@) You can refer to the DAC HAL driver header file for more useful macros
|
|
139
|
|
140 @endverbatim
|
|
141 ******************************************************************************
|
|
142 * @attention
|
|
143 *
|
|
144 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
|
|
145 *
|
|
146 * Redistribution and use in source and binary forms, with or without modification,
|
|
147 * are permitted provided that the following conditions are met:
|
|
148 * 1. Redistributions of source code must retain the above copyright notice,
|
|
149 * this list of conditions and the following disclaimer.
|
|
150 * 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
151 * this list of conditions and the following disclaimer in the documentation
|
|
152 * and/or other materials provided with the distribution.
|
|
153 * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
154 * may be used to endorse or promote products derived from this software
|
|
155 * without specific prior written permission.
|
|
156 *
|
|
157 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
158 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
159 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
160 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
161 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
162 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
163 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
164 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
165 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
166 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
167 *
|
|
168 ******************************************************************************
|
|
169 */
|
|
170
|
|
171
|
|
172 /* Includes ------------------------------------------------------------------*/
|
|
173 #include "stm32f4xx_hal.h"
|
|
174
|
|
175 /** @addtogroup STM32F4xx_HAL_Driver
|
|
176 * @{
|
|
177 */
|
|
178
|
|
179 /** @defgroup DAC DAC
|
|
180 * @brief DAC driver modules
|
|
181 * @{
|
|
182 */
|
|
183
|
|
184 #ifdef HAL_DAC_MODULE_ENABLED
|
|
185
|
|
186 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
|
|
187 /* Private typedef -----------------------------------------------------------*/
|
|
188 /* Private define ------------------------------------------------------------*/
|
|
189 /* Private macro -------------------------------------------------------------*/
|
|
190 /* Private variables ---------------------------------------------------------*/
|
|
191 /** @addtogroup DAC_Private_Functions
|
|
192 * @{
|
|
193 */
|
|
194 /* Private function prototypes -----------------------------------------------*/
|
|
195 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
|
|
196 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
|
|
197 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
|
|
198 /**
|
|
199 * @}
|
|
200 */
|
|
201
|
|
202 /* Exported functions --------------------------------------------------------*/
|
|
203 /** @defgroup DAC_Exported_Functions DAC Exported Functions
|
|
204 * @{
|
|
205 */
|
|
206
|
|
207 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
|
|
208 * @brief Initialization and Configuration functions
|
|
209 *
|
|
210 @verbatim
|
|
211 ==============================================================================
|
|
212 ##### Initialization and de-initialization functions #####
|
|
213 ==============================================================================
|
|
214 [..] This section provides functions allowing to:
|
|
215 (+) Initialize and configure the DAC.
|
|
216 (+) De-initialize the DAC.
|
|
217
|
|
218 @endverbatim
|
|
219 * @{
|
|
220 */
|
|
221
|
|
222 /**
|
|
223 * @brief Initializes the DAC peripheral according to the specified parameters
|
|
224 * in the DAC_InitStruct.
|
|
225 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
226 * the configuration information for the specified DAC.
|
|
227 * @retval HAL status
|
|
228 */
|
|
229 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
|
|
230 {
|
|
231 /* Check DAC handle */
|
|
232 if(hdac == NULL)
|
|
233 {
|
|
234 return HAL_ERROR;
|
|
235 }
|
|
236 /* Check the parameters */
|
|
237 assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
|
|
238
|
|
239 if(hdac->State == HAL_DAC_STATE_RESET)
|
|
240 {
|
|
241 /* Init the low level hardware */
|
|
242 HAL_DAC_MspInit(hdac);
|
|
243 }
|
|
244
|
|
245 /* Initialize the DAC state*/
|
|
246 hdac->State = HAL_DAC_STATE_BUSY;
|
|
247
|
|
248 /* Set DAC error code to none */
|
|
249 hdac->ErrorCode = HAL_DAC_ERROR_NONE;
|
|
250
|
|
251 /* Initialize the DAC state*/
|
|
252 hdac->State = HAL_DAC_STATE_READY;
|
|
253
|
|
254 /* Return function status */
|
|
255 return HAL_OK;
|
|
256 }
|
|
257
|
|
258 /**
|
|
259 * @brief Deinitializes the DAC peripheral registers to their default reset values.
|
|
260 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
261 * the configuration information for the specified DAC.
|
|
262 * @retval HAL status
|
|
263 */
|
|
264 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
|
|
265 {
|
|
266 /* Check DAC handle */
|
|
267 if(hdac == NULL)
|
|
268 {
|
|
269 return HAL_ERROR;
|
|
270 }
|
|
271
|
|
272 /* Check the parameters */
|
|
273 assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
|
|
274
|
|
275 /* Change DAC state */
|
|
276 hdac->State = HAL_DAC_STATE_BUSY;
|
|
277
|
|
278 /* DeInit the low level hardware */
|
|
279 HAL_DAC_MspDeInit(hdac);
|
|
280
|
|
281 /* Set DAC error code to none */
|
|
282 hdac->ErrorCode = HAL_DAC_ERROR_NONE;
|
|
283
|
|
284 /* Change DAC state */
|
|
285 hdac->State = HAL_DAC_STATE_RESET;
|
|
286
|
|
287 /* Release Lock */
|
|
288 __HAL_UNLOCK(hdac);
|
|
289
|
|
290 /* Return function status */
|
|
291 return HAL_OK;
|
|
292 }
|
|
293
|
|
294 /**
|
|
295 * @brief Initializes the DAC MSP.
|
|
296 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
297 * the configuration information for the specified DAC.
|
|
298 * @retval None
|
|
299 */
|
|
300 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
|
|
301 {
|
|
302 /* NOTE : This function Should not be modified, when the callback is needed,
|
|
303 the HAL_DAC_MspInit could be implemented in the user file
|
|
304 */
|
|
305 }
|
|
306
|
|
307 /**
|
|
308 * @brief DeInitializes the DAC MSP.
|
|
309 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
310 * the configuration information for the specified DAC.
|
|
311 * @retval None
|
|
312 */
|
|
313 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
|
|
314 {
|
|
315 /* NOTE : This function Should not be modified, when the callback is needed,
|
|
316 the HAL_DAC_MspDeInit could be implemented in the user file
|
|
317 */
|
|
318 }
|
|
319
|
|
320 /**
|
|
321 * @}
|
|
322 */
|
|
323
|
|
324 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
|
|
325 * @brief IO operation functions
|
|
326 *
|
|
327 @verbatim
|
|
328 ==============================================================================
|
|
329 ##### IO operation functions #####
|
|
330 ==============================================================================
|
|
331 [..] This section provides functions allowing to:
|
|
332 (+) Start conversion.
|
|
333 (+) Stop conversion.
|
|
334 (+) Start conversion and enable DMA transfer.
|
|
335 (+) Stop conversion and disable DMA transfer.
|
|
336 (+) Get result of conversion.
|
|
337
|
|
338 @endverbatim
|
|
339 * @{
|
|
340 */
|
|
341
|
|
342 /**
|
|
343 * @brief Enables DAC and starts conversion of channel.
|
|
344 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
345 * the configuration information for the specified DAC.
|
|
346 * @param Channel: The selected DAC channel.
|
|
347 * This parameter can be one of the following values:
|
|
348 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
349 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
350 * @retval HAL status
|
|
351 */
|
|
352 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
|
|
353 {
|
|
354 uint32_t tmp1 = 0, tmp2 = 0;
|
|
355
|
|
356 /* Check the parameters */
|
|
357 assert_param(IS_DAC_CHANNEL(Channel));
|
|
358
|
|
359 /* Process locked */
|
|
360 __HAL_LOCK(hdac);
|
|
361
|
|
362 /* Change DAC state */
|
|
363 hdac->State = HAL_DAC_STATE_BUSY;
|
|
364
|
|
365 /* Enable the Peripheral */
|
|
366 __HAL_DAC_ENABLE(hdac, Channel);
|
|
367
|
|
368 if(Channel == DAC_CHANNEL_1)
|
|
369 {
|
|
370 tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
|
|
371 tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
|
|
372 /* Check if software trigger enabled */
|
|
373 if((tmp1 == DAC_CR_TEN1) && (tmp2 == DAC_CR_TSEL1))
|
|
374 {
|
|
375 /* Enable the selected DAC software conversion */
|
|
376 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
|
|
377 }
|
|
378 }
|
|
379 else
|
|
380 {
|
|
381 tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
|
|
382 tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;
|
|
383 /* Check if software trigger enabled */
|
|
384 if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
|
|
385 {
|
|
386 /* Enable the selected DAC software conversion*/
|
|
387 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
|
|
388 }
|
|
389 }
|
|
390
|
|
391 /* Change DAC state */
|
|
392 hdac->State = HAL_DAC_STATE_READY;
|
|
393
|
|
394 /* Process unlocked */
|
|
395 __HAL_UNLOCK(hdac);
|
|
396
|
|
397 /* Return function status */
|
|
398 return HAL_OK;
|
|
399 }
|
|
400
|
|
401 /**
|
|
402 * @brief Disables DAC and stop conversion of channel.
|
|
403 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
404 * the configuration information for the specified DAC.
|
|
405 * @param Channel: The selected DAC channel.
|
|
406 * This parameter can be one of the following values:
|
|
407 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
408 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
409 * @retval HAL status
|
|
410 */
|
|
411 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
|
|
412 {
|
|
413 /* Check the parameters */
|
|
414 assert_param(IS_DAC_CHANNEL(Channel));
|
|
415
|
|
416 /* Disable the Peripheral */
|
|
417 __HAL_DAC_DISABLE(hdac, Channel);
|
|
418
|
|
419 /* Change DAC state */
|
|
420 hdac->State = HAL_DAC_STATE_READY;
|
|
421
|
|
422 /* Return function status */
|
|
423 return HAL_OK;
|
|
424 }
|
|
425
|
|
426 /**
|
|
427 * @brief Enables DAC and starts conversion of channel.
|
|
428 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
429 * the configuration information for the specified DAC.
|
|
430 * @param Channel: The selected DAC channel.
|
|
431 * This parameter can be one of the following values:
|
|
432 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
433 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
434 * @param pData: The destination peripheral Buffer address.
|
|
435 * @param Length: The length of data to be transferred from memory to DAC peripheral
|
|
436 * @param Alignment: Specifies the data alignment for DAC channel.
|
|
437 * This parameter can be one of the following values:
|
|
438 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
|
|
439 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
|
|
440 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
|
|
441 * @retval HAL status
|
|
442 */
|
|
443 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
|
|
444 {
|
|
445 uint32_t tmpreg = 0;
|
|
446
|
|
447 /* Check the parameters */
|
|
448 assert_param(IS_DAC_CHANNEL(Channel));
|
|
449 assert_param(IS_DAC_ALIGN(Alignment));
|
|
450
|
|
451 /* Process locked */
|
|
452 __HAL_LOCK(hdac);
|
|
453
|
|
454 /* Change DAC state */
|
|
455 hdac->State = HAL_DAC_STATE_BUSY;
|
|
456
|
|
457 if(Channel == DAC_CHANNEL_1)
|
|
458 {
|
|
459 /* Set the DMA transfer complete callback for channel1 */
|
|
460 hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
|
|
461
|
|
462 /* Set the DMA half transfer complete callback for channel1 */
|
|
463 hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
|
|
464
|
|
465 /* Set the DMA error callback for channel1 */
|
|
466 hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
|
|
467
|
|
468 /* Enable the selected DAC channel1 DMA request */
|
|
469 hdac->Instance->CR |= DAC_CR_DMAEN1;
|
|
470
|
|
471 /* Case of use of channel 1 */
|
|
472 switch(Alignment)
|
|
473 {
|
|
474 case DAC_ALIGN_12B_R:
|
|
475 /* Get DHR12R1 address */
|
|
476 tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
|
|
477 break;
|
|
478 case DAC_ALIGN_12B_L:
|
|
479 /* Get DHR12L1 address */
|
|
480 tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
|
|
481 break;
|
|
482 case DAC_ALIGN_8B_R:
|
|
483 /* Get DHR8R1 address */
|
|
484 tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
|
|
485 break;
|
|
486 default:
|
|
487 break;
|
|
488 }
|
|
489 }
|
|
490 else
|
|
491 {
|
|
492 /* Set the DMA transfer complete callback for channel2 */
|
|
493 hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
|
|
494
|
|
495 /* Set the DMA half transfer complete callback for channel2 */
|
|
496 hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
|
|
497
|
|
498 /* Set the DMA error callback for channel2 */
|
|
499 hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
|
|
500
|
|
501 /* Enable the selected DAC channel2 DMA request */
|
|
502 hdac->Instance->CR |= DAC_CR_DMAEN2;
|
|
503
|
|
504 /* Case of use of channel 2 */
|
|
505 switch(Alignment)
|
|
506 {
|
|
507 case DAC_ALIGN_12B_R:
|
|
508 /* Get DHR12R2 address */
|
|
509 tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
|
|
510 break;
|
|
511 case DAC_ALIGN_12B_L:
|
|
512 /* Get DHR12L2 address */
|
|
513 tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
|
|
514 break;
|
|
515 case DAC_ALIGN_8B_R:
|
|
516 /* Get DHR8R2 address */
|
|
517 tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
|
|
518 break;
|
|
519 default:
|
|
520 break;
|
|
521 }
|
|
522 }
|
|
523
|
|
524 /* Enable the DMA Stream */
|
|
525 if(Channel == DAC_CHANNEL_1)
|
|
526 {
|
|
527 /* Enable the DAC DMA underrun interrupt */
|
|
528 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
|
|
529
|
|
530 /* Enable the DMA Stream */
|
|
531 HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
|
|
532 }
|
|
533 else
|
|
534 {
|
|
535 /* Enable the DAC DMA underrun interrupt */
|
|
536 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
|
|
537
|
|
538 /* Enable the DMA Stream */
|
|
539 HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
|
|
540 }
|
|
541
|
|
542 /* Enable the Peripheral */
|
|
543 __HAL_DAC_ENABLE(hdac, Channel);
|
|
544
|
|
545 /* Process Unlocked */
|
|
546 __HAL_UNLOCK(hdac);
|
|
547
|
|
548 /* Return function status */
|
|
549 return HAL_OK;
|
|
550 }
|
|
551
|
|
552 /**
|
|
553 * @brief Disables DAC and stop conversion of channel.
|
|
554 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
555 * the configuration information for the specified DAC.
|
|
556 * @param Channel: The selected DAC channel.
|
|
557 * This parameter can be one of the following values:
|
|
558 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
559 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
560 * @retval HAL status
|
|
561 */
|
|
562 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
|
|
563 {
|
|
564 HAL_StatusTypeDef status = HAL_OK;
|
|
565
|
|
566 /* Check the parameters */
|
|
567 assert_param(IS_DAC_CHANNEL(Channel));
|
|
568
|
|
569 /* Disable the selected DAC channel DMA request */
|
|
570 hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
|
|
571
|
|
572 /* Disable the Peripheral */
|
|
573 __HAL_DAC_DISABLE(hdac, Channel);
|
|
574
|
|
575 /* Disable the DMA Channel */
|
|
576 /* Channel1 is used */
|
|
577 if(Channel == DAC_CHANNEL_1)
|
|
578 {
|
|
579 status = HAL_DMA_Abort(hdac->DMA_Handle1);
|
|
580 }
|
|
581 else /* Channel2 is used for */
|
|
582 {
|
|
583 status = HAL_DMA_Abort(hdac->DMA_Handle2);
|
|
584 }
|
|
585
|
|
586 /* Check if DMA Channel effectively disabled */
|
|
587 if(status != HAL_OK)
|
|
588 {
|
|
589 /* Update DAC state machine to error */
|
|
590 hdac->State = HAL_DAC_STATE_ERROR;
|
|
591 }
|
|
592 else
|
|
593 {
|
|
594 /* Change DAC state */
|
|
595 hdac->State = HAL_DAC_STATE_READY;
|
|
596 }
|
|
597
|
|
598 /* Return function status */
|
|
599 return status;
|
|
600 }
|
|
601
|
|
602 /**
|
|
603 * @brief Returns the last data output value of the selected DAC channel.
|
|
604 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
605 * the configuration information for the specified DAC.
|
|
606 * @param Channel: The selected DAC channel.
|
|
607 * This parameter can be one of the following values:
|
|
608 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
609 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
610 * @retval The selected DAC channel data output value.
|
|
611 */
|
|
612 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
|
|
613 {
|
|
614 /* Check the parameters */
|
|
615 assert_param(IS_DAC_CHANNEL(Channel));
|
|
616
|
|
617 /* Returns the DAC channel data output register value */
|
|
618 if(Channel == DAC_CHANNEL_1)
|
|
619 {
|
|
620 return hdac->Instance->DOR1;
|
|
621 }
|
|
622 else
|
|
623 {
|
|
624 return hdac->Instance->DOR2;
|
|
625 }
|
|
626 }
|
|
627
|
|
628 /**
|
|
629 * @brief Handles DAC interrupt request
|
|
630 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
631 * the configuration information for the specified DAC.
|
|
632 * @retval None
|
|
633 */
|
|
634 void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
|
|
635 {
|
|
636 /* Check underrun channel 1 flag */
|
|
637 if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
|
|
638 {
|
|
639 /* Change DAC state to error state */
|
|
640 hdac->State = HAL_DAC_STATE_ERROR;
|
|
641
|
|
642 /* Set DAC error code to channel1 DMA underrun error */
|
|
643 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
|
|
644
|
|
645 /* Clear the underrun flag */
|
|
646 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
|
|
647
|
|
648 /* Disable the selected DAC channel1 DMA request */
|
|
649 hdac->Instance->CR &= ~DAC_CR_DMAEN1;
|
|
650
|
|
651 /* Error callback */
|
|
652 HAL_DAC_DMAUnderrunCallbackCh1(hdac);
|
|
653 }
|
|
654 /* Check underrun channel 2 flag */
|
|
655 if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
|
|
656 {
|
|
657 /* Change DAC state to error state */
|
|
658 hdac->State = HAL_DAC_STATE_ERROR;
|
|
659
|
|
660 /* Set DAC error code to channel2 DMA underrun error */
|
|
661 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
|
|
662
|
|
663 /* Clear the underrun flag */
|
|
664 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
|
|
665
|
|
666 /* Disable the selected DAC channel1 DMA request */
|
|
667 hdac->Instance->CR &= ~DAC_CR_DMAEN2;
|
|
668
|
|
669 /* Error callback */
|
|
670 HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
|
|
671 }
|
|
672 }
|
|
673
|
|
674 /**
|
|
675 * @brief Conversion complete callback in non blocking mode for Channel1
|
|
676 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
677 * the configuration information for the specified DAC.
|
|
678 * @retval None
|
|
679 */
|
|
680 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
|
|
681 {
|
|
682 /* NOTE : This function Should not be modified, when the callback is needed,
|
|
683 the HAL_DAC_ConvCpltCallback could be implemented in the user file
|
|
684 */
|
|
685 }
|
|
686
|
|
687 /**
|
|
688 * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
|
|
689 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
690 * the configuration information for the specified DAC.
|
|
691 * @retval None
|
|
692 */
|
|
693 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
|
|
694 {
|
|
695 /* NOTE : This function Should not be modified, when the callback is needed,
|
|
696 the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
|
|
697 */
|
|
698 }
|
|
699
|
|
700 /**
|
|
701 * @brief Error DAC callback for Channel1.
|
|
702 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
703 * the configuration information for the specified DAC.
|
|
704 * @retval None
|
|
705 */
|
|
706 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
|
|
707 {
|
|
708 /* NOTE : This function Should not be modified, when the callback is needed,
|
|
709 the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
|
|
710 */
|
|
711 }
|
|
712
|
|
713 /**
|
|
714 * @brief DMA underrun DAC callback for channel1.
|
|
715 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
716 * the configuration information for the specified DAC.
|
|
717 * @retval None
|
|
718 */
|
|
719 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
|
|
720 {
|
|
721 /* NOTE : This function Should not be modified, when the callback is needed,
|
|
722 the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
|
|
723 */
|
|
724 }
|
|
725
|
|
726 /**
|
|
727 * @}
|
|
728 */
|
|
729
|
|
730 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
|
|
731 * @brief Peripheral Control functions
|
|
732 *
|
|
733 @verbatim
|
|
734 ==============================================================================
|
|
735 ##### Peripheral Control functions #####
|
|
736 ==============================================================================
|
|
737 [..] This section provides functions allowing to:
|
|
738 (+) Configure channels.
|
|
739 (+) Set the specified data holding register value for DAC channel.
|
|
740
|
|
741 @endverbatim
|
|
742 * @{
|
|
743 */
|
|
744
|
|
745 /**
|
|
746 * @brief Configures the selected DAC channel.
|
|
747 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
748 * the configuration information for the specified DAC.
|
|
749 * @param sConfig: DAC configuration structure.
|
|
750 * @param Channel: The selected DAC channel.
|
|
751 * This parameter can be one of the following values:
|
|
752 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
753 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
754 * @retval HAL status
|
|
755 */
|
|
756 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
|
|
757 {
|
|
758 uint32_t tmpreg1 = 0, tmpreg2 = 0;
|
|
759
|
|
760 /* Check the DAC parameters */
|
|
761 assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
|
|
762 assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
|
|
763 assert_param(IS_DAC_CHANNEL(Channel));
|
|
764
|
|
765 /* Process locked */
|
|
766 __HAL_LOCK(hdac);
|
|
767
|
|
768 /* Change DAC state */
|
|
769 hdac->State = HAL_DAC_STATE_BUSY;
|
|
770
|
|
771 /* Get the DAC CR value */
|
|
772 tmpreg1 = hdac->Instance->CR;
|
|
773 /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
|
|
774 tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
|
|
775 /* Configure for the selected DAC channel: buffer output, trigger */
|
|
776 /* Set TSELx and TENx bits according to DAC_Trigger value */
|
|
777 /* Set BOFFx bit according to DAC_OutputBuffer value */
|
|
778 tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
|
|
779 /* Calculate CR register value depending on DAC_Channel */
|
|
780 tmpreg1 |= tmpreg2 << Channel;
|
|
781 /* Write to DAC CR */
|
|
782 hdac->Instance->CR = tmpreg1;
|
|
783 /* Disable wave generation */
|
|
784 hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
|
|
785
|
|
786 /* Change DAC state */
|
|
787 hdac->State = HAL_DAC_STATE_READY;
|
|
788
|
|
789 /* Process unlocked */
|
|
790 __HAL_UNLOCK(hdac);
|
|
791
|
|
792 /* Return function status */
|
|
793 return HAL_OK;
|
|
794 }
|
|
795
|
|
796 /**
|
|
797 * @brief Set the specified data holding register value for DAC channel.
|
|
798 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
799 * the configuration information for the specified DAC.
|
|
800 * @param Channel: The selected DAC channel.
|
|
801 * This parameter can be one of the following values:
|
|
802 * @arg DAC_CHANNEL_1: DAC Channel1 selected
|
|
803 * @arg DAC_CHANNEL_2: DAC Channel2 selected
|
|
804 * @param Alignment: Specifies the data alignment.
|
|
805 * This parameter can be one of the following values:
|
|
806 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
|
|
807 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
|
|
808 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
|
|
809 * @param Data: Data to be loaded in the selected data holding register.
|
|
810 * @retval HAL status
|
|
811 */
|
|
812 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
|
|
813 {
|
|
814 __IO uint32_t tmp = 0;
|
|
815
|
|
816 /* Check the parameters */
|
|
817 assert_param(IS_DAC_CHANNEL(Channel));
|
|
818 assert_param(IS_DAC_ALIGN(Alignment));
|
|
819 assert_param(IS_DAC_DATA(Data));
|
|
820
|
|
821 tmp = (uint32_t)hdac->Instance;
|
|
822 if(Channel == DAC_CHANNEL_1)
|
|
823 {
|
|
824 tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
|
|
825 }
|
|
826 else
|
|
827 {
|
|
828 tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
|
|
829 }
|
|
830
|
|
831 /* Set the DAC channel1 selected data holding register */
|
|
832 *(__IO uint32_t *) tmp = Data;
|
|
833
|
|
834 /* Return function status */
|
|
835 return HAL_OK;
|
|
836 }
|
|
837
|
|
838 /**
|
|
839 * @}
|
|
840 */
|
|
841
|
|
842 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
|
|
843 * @brief Peripheral State and Errors functions
|
|
844 *
|
|
845 @verbatim
|
|
846 ==============================================================================
|
|
847 ##### Peripheral State and Errors functions #####
|
|
848 ==============================================================================
|
|
849 [..]
|
|
850 This subsection provides functions allowing to
|
|
851 (+) Check the DAC state.
|
|
852 (+) Check the DAC Errors.
|
|
853
|
|
854 @endverbatim
|
|
855 * @{
|
|
856 */
|
|
857
|
|
858 /**
|
|
859 * @brief return the DAC state
|
|
860 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
861 * the configuration information for the specified DAC.
|
|
862 * @retval HAL state
|
|
863 */
|
|
864 HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
|
|
865 {
|
|
866 /* Return DAC state */
|
|
867 return hdac->State;
|
|
868 }
|
|
869
|
|
870
|
|
871 /**
|
|
872 * @brief Return the DAC error code
|
|
873 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
|
|
874 * the configuration information for the specified DAC.
|
|
875 * @retval DAC Error Code
|
|
876 */
|
|
877 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
|
|
878 {
|
|
879 return hdac->ErrorCode;
|
|
880 }
|
|
881
|
|
882 /**
|
|
883 * @}
|
|
884 */
|
|
885
|
|
886 /**
|
|
887 * @brief DMA conversion complete callback.
|
|
888 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
889 * the configuration information for the specified DMA module.
|
|
890 * @retval None
|
|
891 */
|
|
892 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
|
|
893 {
|
|
894 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
895
|
|
896 HAL_DAC_ConvCpltCallbackCh1(hdac);
|
|
897
|
|
898 hdac->State= HAL_DAC_STATE_READY;
|
|
899 }
|
|
900
|
|
901 /**
|
|
902 * @brief DMA half transfer complete callback.
|
|
903 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
904 * the configuration information for the specified DMA module.
|
|
905 * @retval None
|
|
906 */
|
|
907 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
|
|
908 {
|
|
909 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
910 /* Conversion complete callback */
|
|
911 HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
|
|
912 }
|
|
913
|
|
914 /**
|
|
915 * @brief DMA error callback
|
|
916 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
917 * the configuration information for the specified DMA module.
|
|
918 * @retval None
|
|
919 */
|
|
920 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
|
|
921 {
|
|
922 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
923
|
|
924 /* Set DAC error code to DMA error */
|
|
925 hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
|
|
926
|
|
927 HAL_DAC_ErrorCallbackCh1(hdac);
|
|
928
|
|
929 hdac->State= HAL_DAC_STATE_READY;
|
|
930 }
|
|
931
|
|
932 /**
|
|
933 * @}
|
|
934 */
|
|
935 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
|
|
936 #endif /* HAL_DAC_MODULE_ENABLED */
|
|
937
|
|
938 /**
|
|
939 * @}
|
|
940 */
|
|
941
|
|
942 /**
|
|
943 * @}
|
|
944 */
|
|
945
|
|
946 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|