STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_dac.c
Go to the documentation of this file.
1 
174 /* Includes ------------------------------------------------------------------*/
175 #include "stm32f7xx_hal.h"
176 
186 #ifdef HAL_DAC_MODULE_ENABLED
187 
188 /* Private typedef -----------------------------------------------------------*/
189 /* Private define ------------------------------------------------------------*/
190 /* Private macro -------------------------------------------------------------*/
191 /* Private variables ---------------------------------------------------------*/
195 /* Private function prototypes -----------------------------------------------*/
196 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
197 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
198 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
203 /* Exported functions --------------------------------------------------------*/
231 {
232  /* Check DAC handle */
233  if(hdac == NULL)
234  {
235  return HAL_ERROR;
236  }
237  /* Check the parameters */
239 
240  if(hdac->State == HAL_DAC_STATE_RESET)
241  {
242  /* Allocate lock resource and initialize it */
243  hdac->Lock = HAL_UNLOCKED;
244  /* Init the low level hardware */
245  HAL_DAC_MspInit(hdac);
246  }
247 
248  /* Initialize the DAC state*/
249  hdac->State = HAL_DAC_STATE_BUSY;
250 
251  /* Set DAC error code to none */
253 
254  /* Initialize the DAC state*/
255  hdac->State = HAL_DAC_STATE_READY;
256 
257  /* Return function status */
258  return HAL_OK;
259 }
260 
268 {
269  /* Check DAC handle */
270  if(hdac == NULL)
271  {
272  return HAL_ERROR;
273  }
274 
275  /* Check the parameters */
277 
278  /* Change DAC state */
279  hdac->State = HAL_DAC_STATE_BUSY;
280 
281  /* DeInit the low level hardware */
282  HAL_DAC_MspDeInit(hdac);
283 
284  /* Set DAC error code to none */
286 
287  /* Change DAC state */
288  hdac->State = HAL_DAC_STATE_RESET;
289 
290  /* Release Lock */
291  __HAL_UNLOCK(hdac);
292 
293  /* Return function status */
294  return HAL_OK;
295 }
296 
303 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
304 {
305  /* Prevent unused argument(s) compilation warning */
306  UNUSED(hdac);
307 
308  /* NOTE : This function Should not be modified, when the callback is needed,
309  the HAL_DAC_MspInit could be implemented in the user file
310  */
311 }
312 
319 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
320 {
321  /* Prevent unused argument(s) compilation warning */
322  UNUSED(hdac);
323 
324  /* NOTE : This function Should not be modified, when the callback is needed,
325  the HAL_DAC_MspDeInit could be implemented in the user file
326  */
327 }
328 
361 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
362 {
363  uint32_t tmp1 = 0, tmp2 = 0;
364 
365  /* Check the parameters */
366  assert_param(IS_DAC_CHANNEL(Channel));
367 
368  /* Process locked */
369  __HAL_LOCK(hdac);
370 
371  /* Change DAC state */
372  hdac->State = HAL_DAC_STATE_BUSY;
373 
374  /* Enable the Peripheral */
375  __HAL_DAC_ENABLE(hdac, Channel);
376 
377  if(Channel == DAC_CHANNEL_1)
378  {
379  tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
380  tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
381  /* Check if software trigger enabled */
382  if((tmp1 == DAC_CR_TEN1) && (tmp2 == DAC_CR_TSEL1))
383  {
384  /* Enable the selected DAC software conversion */
385  hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
386  }
387  }
388  else
389  {
390  tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
391  tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;
392  /* Check if software trigger enabled */
393  if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
394  {
395  /* Enable the selected DAC software conversion*/
396  hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
397  }
398  }
399 
400  /* Change DAC state */
401  hdac->State = HAL_DAC_STATE_READY;
402 
403  /* Process unlocked */
404  __HAL_UNLOCK(hdac);
405 
406  /* Return function status */
407  return HAL_OK;
408 }
409 
420 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
421 {
422  /* Check the parameters */
423  assert_param(IS_DAC_CHANNEL(Channel));
424 
425  /* Disable the Peripheral */
426  __HAL_DAC_DISABLE(hdac, Channel);
427 
428  /* Change DAC state */
429  hdac->State = HAL_DAC_STATE_READY;
430 
431  /* Return function status */
432  return HAL_OK;
433 }
434 
452 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
453 {
454  uint32_t tmpreg = 0;
455 
456  /* Check the parameters */
457  assert_param(IS_DAC_CHANNEL(Channel));
458  assert_param(IS_DAC_ALIGN(Alignment));
459 
460  /* Process locked */
461  __HAL_LOCK(hdac);
462 
463  /* Change DAC state */
464  hdac->State = HAL_DAC_STATE_BUSY;
465 
466  if(Channel == DAC_CHANNEL_1)
467  {
468  /* Set the DMA transfer complete callback for channel1 */
469  hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
470 
471  /* Set the DMA half transfer complete callback for channel1 */
472  hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
473 
474  /* Set the DMA error callback for channel1 */
475  hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
476 
477  /* Enable the selected DAC channel1 DMA request */
478  hdac->Instance->CR |= DAC_CR_DMAEN1;
479 
480  /* Case of use of channel 1 */
481  switch(Alignment)
482  {
483  case DAC_ALIGN_12B_R:
484  /* Get DHR12R1 address */
485  tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
486  break;
487  case DAC_ALIGN_12B_L:
488  /* Get DHR12L1 address */
489  tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
490  break;
491  case DAC_ALIGN_8B_R:
492  /* Get DHR8R1 address */
493  tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
494  break;
495  default:
496  break;
497  }
498  }
499  else
500  {
501  /* Set the DMA transfer complete callback for channel2 */
503 
504  /* Set the DMA half transfer complete callback for channel2 */
506 
507  /* Set the DMA error callback for channel2 */
509 
510  /* Enable the selected DAC channel2 DMA request */
511  hdac->Instance->CR |= DAC_CR_DMAEN2;
512 
513  /* Case of use of channel 2 */
514  switch(Alignment)
515  {
516  case DAC_ALIGN_12B_R:
517  /* Get DHR12R2 address */
518  tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
519  break;
520  case DAC_ALIGN_12B_L:
521  /* Get DHR12L2 address */
522  tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
523  break;
524  case DAC_ALIGN_8B_R:
525  /* Get DHR8R2 address */
526  tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
527  break;
528  default:
529  break;
530  }
531  }
532 
533  /* Enable the DMA Stream */
534  if(Channel == DAC_CHANNEL_1)
535  {
536  /* Enable the DAC DMA underrun interrupt */
538 
539  /* Enable the DMA Stream */
540  HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
541  }
542  else
543  {
544  /* Enable the DAC DMA underrun interrupt */
546 
547  /* Enable the DMA Stream */
548  HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
549  }
550 
551  /* Enable the Peripheral */
552  __HAL_DAC_ENABLE(hdac, Channel);
553 
554  /* Process Unlocked */
555  __HAL_UNLOCK(hdac);
556 
557  /* Return function status */
558  return HAL_OK;
559 }
560 
572 {
573  HAL_StatusTypeDef status = HAL_OK;
574 
575  /* Check the parameters */
576  assert_param(IS_DAC_CHANNEL(Channel));
577 
578  /* Disable the selected DAC channel DMA request */
579  hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
580 
581  /* Disable the Peripheral */
582  __HAL_DAC_DISABLE(hdac, Channel);
583 
584  /* Disable the DMA Channel */
585  /* Channel1 is used */
586  if(Channel == DAC_CHANNEL_1)
587  {
588  status = HAL_DMA_Abort(hdac->DMA_Handle1);
589  }
590  else /* Channel2 is used for */
591  {
592  status = HAL_DMA_Abort(hdac->DMA_Handle2);
593  }
594 
595  /* Check if DMA Channel effectively disabled */
596  if(status != HAL_OK)
597  {
598  /* Update DAC state machine to error */
599  hdac->State = HAL_DAC_STATE_ERROR;
600  }
601  else
602  {
603  /* Change DAC state */
604  hdac->State = HAL_DAC_STATE_READY;
605  }
606 
607  /* Return function status */
608  return status;
609 }
610 
621 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
622 {
623  /* Check the parameters */
624  assert_param(IS_DAC_CHANNEL(Channel));
625 
626  /* Returns the DAC channel data output register value */
627  if(Channel == DAC_CHANNEL_1)
628  {
629  return hdac->Instance->DOR1;
630  }
631  else
632  {
633  return hdac->Instance->DOR2;
634  }
635 }
636 
644 {
645  /* Check underrun channel 1 flag */
647  {
648  /* Change DAC state to error state */
649  hdac->State = HAL_DAC_STATE_ERROR;
650 
651  /* Set DAC error code to channel1 DMA underrun error */
653 
654  /* Clear the underrun flag */
656 
657  /* Disable the selected DAC channel1 DMA request */
658  hdac->Instance->CR &= ~DAC_CR_DMAEN1;
659 
660  /* Error callback */
662  }
663  /* Check underrun channel 2 flag */
665  {
666  /* Change DAC state to error state */
667  hdac->State = HAL_DAC_STATE_ERROR;
668 
669  /* Set DAC error code to channel2 DMA underrun error */
671 
672  /* Clear the underrun flag */
674 
675  /* Disable the selected DAC channel1 DMA request */
676  hdac->Instance->CR &= ~DAC_CR_DMAEN2;
677 
678  /* Error callback */
680  }
681 }
682 
690 {
691  /* Prevent unused argument(s) compilation warning */
692  UNUSED(hdac);
693 
694  /* NOTE : This function Should not be modified, when the callback is needed,
695  the HAL_DAC_ConvCpltCallback could be implemented in the user file
696  */
697 }
698 
706 {
707  /* Prevent unused argument(s) compilation warning */
708  UNUSED(hdac);
709 
710  /* NOTE : This function Should not be modified, when the callback is needed,
711  the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
712  */
713 }
714 
722 {
723  /* Prevent unused argument(s) compilation warning */
724  UNUSED(hdac);
725 
726  /* NOTE : This function Should not be modified, when the callback is needed,
727  the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
728  */
729 }
730 
738 {
739  /* Prevent unused argument(s) compilation warning */
740  UNUSED(hdac);
741 
742  /* NOTE : This function Should not be modified, when the callback is needed,
743  the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
744  */
745 }
746 
778 {
779  uint32_t tmpreg1 = 0, tmpreg2 = 0;
780 
781  /* Check the DAC parameters */
784  assert_param(IS_DAC_CHANNEL(Channel));
785 
786  /* Process locked */
787  __HAL_LOCK(hdac);
788 
789  /* Change DAC state */
790  hdac->State = HAL_DAC_STATE_BUSY;
791 
792  /* Get the DAC CR value */
793  tmpreg1 = hdac->Instance->CR;
794  /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
795  tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
796  /* Configure for the selected DAC channel: buffer output, trigger */
797  /* Set TSELx and TENx bits according to DAC_Trigger value */
798  /* Set BOFFx bit according to DAC_OutputBuffer value */
799  tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
800  /* Calculate CR register value depending on DAC_Channel */
801  tmpreg1 |= tmpreg2 << Channel;
802  /* Write to DAC CR */
803  hdac->Instance->CR = tmpreg1;
804  /* Disable wave generation */
805  hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
806 
807  /* Change DAC state */
808  hdac->State = HAL_DAC_STATE_READY;
809 
810  /* Process unlocked */
811  __HAL_UNLOCK(hdac);
812 
813  /* Return function status */
814  return HAL_OK;
815 }
816 
833 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
834 {
835  __IO uint32_t tmp = 0;
836 
837  /* Check the parameters */
838  assert_param(IS_DAC_CHANNEL(Channel));
839  assert_param(IS_DAC_ALIGN(Alignment));
840  assert_param(IS_DAC_DATA(Data));
841 
842  tmp = (uint32_t)hdac->Instance;
843  if(Channel == DAC_CHANNEL_1)
844  {
845  tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
846  }
847  else
848  {
849  tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
850  }
851 
852  /* Set the DAC channel1 selected data holding register */
853  *(__IO uint32_t *) tmp = Data;
854 
855  /* Return function status */
856  return HAL_OK;
857 }
858 
886 {
887  /* Return DAC state */
888  return hdac->State;
889 }
890 
891 
898 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
899 {
900  return hdac->ErrorCode;
901 }
902 
913 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
914 {
915  DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
916 
918 
919  hdac->State= HAL_DAC_STATE_READY;
920 }
921 
928 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
929 {
930  DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
931  /* Conversion complete callback */
933 }
934 
941 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
942 {
943  DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
944 
945  /* Set DAC error code to DMA error */
946  hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
947 
949 
950  hdac->State= HAL_DAC_STATE_READY;
951 }
952 
957 #endif /* HAL_DAC_MODULE_ENABLED */
958 
967 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
__IO uint32_t DHR8R2
Definition: stm32f745xx.h:345
uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef *hdac, uint32_t Channel)
void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
__IO uint32_t DHR12L2
Definition: stm32f745xx.h:344
#define HAL_DAC_ERROR_NONE
#define DAC_CR_TEN2
Definition: stm32f745xx.h:3110
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
#define DAC_CR_TEN1
Definition: stm32f745xx.h:3093
DMA_HandleTypeDef * DMA_Handle2
#define HAL_DAC_ERROR_DMA
#define assert_param(expr)
Include module&#39;s header file.
HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t *pData, uint32_t Length, uint32_t Alignment)
#define DAC_CR_MAMP1
Definition: stm32f745xx.h:3101
#define DAC_ALIGN_12B_R
HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
HAL_DAC_StateTypeDef
HAL State structures definition.
__IO uint32_t CR
Definition: stm32f745xx.h:338
#define __HAL_UNLOCK(__HANDLE__)
void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
#define IS_DAC_DATA(DATA)
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
#define DAC_CR_DMAEN2
Definition: stm32f745xx.h:3123
#define IS_DAC_OUTPUT_BUFFER_STATE(STATE)
__IO uint32_t DHR12R2
Definition: stm32f745xx.h:343
__IO uint32_t DHR12R1
Definition: stm32f745xx.h:340
HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef *hdac)
DAC_TypeDef * Instance
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
#define __HAL_LOCK(__HANDLE__)
#define DAC_ALIGN_8B_R
#define NULL
Definition: usbd_def.h:53
#define DAC_CR_TSEL2
Definition: stm32f745xx.h:3111
#define DAC_DHR12R2_ALIGNMENT(__ALIGNMENT__)
Set DHR12R2 alignment.
#define DAC_SWTRIGR_SWTRIG2
Definition: stm32f745xx.h:3128
#define __IO
Definition: core_cm0.h:213
#define DAC_CR_DMAEN1
Definition: stm32f745xx.h:3106
__IO uint32_t DOR2
Definition: stm32f745xx.h:350
#define __HAL_DAC_GET_FLAG(__HANDLE__, __FLAG__)
Get the selected DAC&#39;s flag status.
#define DAC_CR_BOFF1
Definition: stm32f745xx.h:3092
This file contains all the functions prototypes for the HAL module driver.
void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
__IO uint32_t DHR8R1
Definition: stm32f745xx.h:342
__IO uint32_t ErrorCode
void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
HAL_LockTypeDef Lock
#define DAC_SWTRIGR_SWTRIG1
Definition: stm32f745xx.h:3127
#define DAC_CR_WAVE1
Definition: stm32f745xx.h:3098
void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
#define DAC_ALIGN_12B_L
__IO HAL_DAC_StateTypeDef State
DAC Configuration regular Channel structure definition.
#define UNUSED(x)
#define __HAL_DAC_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the DAC&#39;s flag.
HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef *hdac)
#define DAC_CHANNEL_1
uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
__IO uint32_t DHR12L1
Definition: stm32f745xx.h:341
#define __HAL_DAC_DISABLE(__HANDLE__, __DAC_CHANNEL__)
Disable the DAC channel.
#define DAC_FLAG_DMAUDR2
#define DAC_IT_DMAUDR1
DMA handle Structure definition.
DMA_HandleTypeDef * DMA_Handle1
#define DAC_DHR12R1_ALIGNMENT(__ALIGNMENT__)
Set DHR12R1 alignment.
#define DAC_IT_DMAUDR2
#define __HAL_DAC_ENABLE(__HANDLE__, __DAC_CHANNEL__)
Enable the DAC channel.
__IO uint32_t SWTRIGR
Definition: stm32f745xx.h:339
#define HAL_DAC_ERROR_DMAUNDERRUNCH2
__IO uint32_t DOR1
Definition: stm32f745xx.h:349
#define IS_DAC_CHANNEL(CHANNEL)
HAL_StatusTypeDef
HAL Status structures definition.
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
#define IS_DAC_ALL_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:8793
DAC handle Structure definition.
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
#define IS_DAC_ALIGN(ALIGN)
HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef *hdac)
#define DAC_CR_TSEL1
Definition: stm32f745xx.h:3094
#define __HAL_DAC_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the DAC interrupt.
#define HAL_DAC_ERROR_DMAUNDERRUNCH1
#define IS_DAC_TRIGGER(TRIGGER)
void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
#define DAC_FLAG_DMAUDR1