hal_dac.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_dac.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE DAC FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_DAC_C_
  19. // Files includes
  20. #include "hal_dac.h"
  21. #include "hal_rcc.h"
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /// @addtogroup MM32_Hardware_Abstract_Layer
  24. /// @{
  25. ////////////////////////////////////////////////////////////////////////////////
  26. /// @addtogroup DAC_HAL
  27. /// @{
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// @addtogroup DAC_Exported_Functions
  30. /// @{
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// @brief Deinitializes the DAC peripheral registers to their default reset values.
  33. /// @param None.
  34. /// @retval None.
  35. ////////////////////////////////////////////////////////////////////////////////
  36. void DAC_DeInit(void)
  37. {
  38. exRCC_APB1PeriphReset(RCC_APB1ENR_DAC);
  39. }
  40. ////////////////////////////////////////////////////////////////////////////////
  41. /// @brief Initializes the DAC peripheral according to the specified parameters in the DAC_InitStruct.
  42. /// @param channel: the selected DAC channel.
  43. /// @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure that contains the configuration information for the specified
  44. /// DAC channel.
  45. /// @retval None.
  46. ////////////////////////////////////////////////////////////////////////////////
  47. void DAC_Init(emDACCH_TypeDef channel, DAC_InitTypeDef* init_struct)
  48. {
  49. DAC->CR &= ~((DAC_CR_BOFF1 | DAC_CR_TEN1 | DAC_CR_TSEL1 | DAC_CR_WAVE1 | DAC_CR_MAMP1) << channel);
  50. DAC->CR |= (((u32)(init_struct->DAC_Trigger) | (u32)(init_struct->DAC_WaveGeneration) |
  51. (u32)(init_struct->DAC_LFSRUnmask_TriangleAmplitude) | (u32)(init_struct->DAC_OutputBuffer))
  52. << channel);
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////
  55. /// @brief Fills each DAC_InitStruct member with its default value.
  56. /// @param DAC_InitStruct : pointer to a DAC_InitTypeDef structure which will be initialized.
  57. /// @retval None.
  58. ////////////////////////////////////////////////////////////////////////////////
  59. void DAC_StructInit(DAC_InitTypeDef* init_struct)
  60. {
  61. init_struct->DAC_Trigger = DAC_Trigger_None;
  62. init_struct->DAC_WaveGeneration = DAC_WaveGeneration_None;
  63. init_struct->DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1;
  64. init_struct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////
  67. /// @brief Enables or disables the specified DAC channel.
  68. /// @param channel: the selected DAC channel.
  69. /// @param state: new state of the DAC channel.
  70. /// @retval None.
  71. ////////////////////////////////////////////////////////////////////////////////
  72. void DAC_Cmd(emDACCH_TypeDef channel, FunctionalState state)
  73. {
  74. (state) ? (DAC->CR |= DAC_CR_EN1 << channel) : (DAC->CR &= ~(DAC_CR_EN1 << channel));
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. /// @brief Enables or disables the specified DAC channel DMA request.
  78. /// @param channel: the selected DAC channel.
  79. /// @param state: new state of the selected DAC channel DMA request.
  80. /// @retval None.
  81. ////////////////////////////////////////////////////////////////////////////////
  82. void DAC_DMACmd(emDACCH_TypeDef channel, FunctionalState state)
  83. {
  84. (state) ? (DAC->CR |= DAC_CR_DMAEN1 << channel) : (DAC->CR &= ~(DAC_CR_DMAEN1 << channel));
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////
  87. /// @brief Enables or disables the selected DAC channel software trigger.
  88. /// @param channel: the selected DAC channel.
  89. /// @param state: new state of the selected DAC channel software trigger.
  90. /// @retval None.
  91. ////////////////////////////////////////////////////////////////////////////////
  92. void DAC_SoftwareTriggerCmd(emDACCH_TypeDef channel, FunctionalState state)
  93. {
  94. (state) ? (DAC->SWTRIGR |= (DAC_SWTRIGR_SWTRIG1 << (channel >> 4)))
  95. : (DAC->SWTRIGR &= ~(DAC_SWTRIGR_SWTRIG1 << (channel >> 4)));
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////
  98. /// @brief Enables or disables simultaneously the two DAC channels software triggers.
  99. /// @param state: new state of the DAC channels software triggers.
  100. /// @retval None.
  101. ////////////////////////////////////////////////////////////////////////////////
  102. void DAC_DualSoftwareTriggerCmd(FunctionalState state)
  103. {
  104. (state) ? (DAC->SWTRIGR |= (DAC_SWTRIGR_SWTRIG1 | DAC_SWTRIGR_SWTRIG2))
  105. : (DAC->SWTRIGR &= ~(DAC_SWTRIGR_SWTRIG1 | DAC_SWTRIGR_SWTRIG2));
  106. }
  107. ////////////////////////////////////////////////////////////////////////////////
  108. /// @brief Enables or disables the selected DAC channel wave generation.
  109. /// @param channel: the selected DAC channel.
  110. /// @param wave: Specifies the wave type to enable or disable.
  111. /// @param state: new state of the selected DAC channel wave generation.
  112. /// @retval None.
  113. ////////////////////////////////////////////////////////////////////////////////
  114. void DAC_WaveGenerationCmd(emDACCH_TypeDef channel, emDACWAVE_TypeDef wave, FunctionalState state)
  115. {
  116. (state) ? (DAC->CR |= wave << channel) : (DAC->CR &= ~(wave << channel));
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////
  119. /// @brief Set the specified data holding register value for DAC channel1.
  120. /// @param alignement: Specifies the data alignement for DAC channel1.
  121. /// @param data : data to be loaded in the selected data holding register.
  122. /// @retval None.
  123. ////////////////////////////////////////////////////////////////////////////////
  124. void DAC_SetChannel1Data(emDACALIGN_TypeDef alignement, u16 data)
  125. {
  126. *((u32*)(DAC_BASE + DHR12R1_Offset + alignement)) = data;
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. /// @brief Set the specified data holding register value for DAC channel2.
  130. /// @param alignement: Specifies the data alignement for DAC channel2.
  131. /// @param data : data to be loaded in the selected data holding
  132. /// register.
  133. /// @retval None.
  134. ////////////////////////////////////////////////////////////////////////////////
  135. void DAC_SetChannel2Data(emDACALIGN_TypeDef alignement, u16 data)
  136. {
  137. *((u32*)(DAC_BASE + DHR12R2_Offset + alignement)) = data;
  138. }
  139. ////////////////////////////////////////////////////////////////////////////////
  140. /// @brief Set the specified data holding register value for dual channel DAC.
  141. /// @param alignement: Specifies the data alignement for dual channel DAC.
  142. /// @param data2: data for DAC Channel2 to be loaded in the selected data holding register.
  143. /// @param data1: data for DAC Channel1 to be loaded in the selected data holding register.
  144. /// @retval None.
  145. ////////////////////////////////////////////////////////////////////////////////
  146. void DAC_SetDualChannelData(emDACALIGN_TypeDef alignement, u16 data2, u16 data1)
  147. {
  148. u32 data = ((alignement == DAC_Align_8b_R) ? ((data2 << 8) | data1) : ((data2 << 16) | data1));
  149. *((u32*)(DAC_BASE + DHR12RD_Offset + alignement)) = data;
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////
  152. /// @brief Returns the last data output value of the selected DAC cahnnel.
  153. /// @param channel: the selected DAC channel.
  154. /// @retval The selected DAC channel data output value.
  155. ////////////////////////////////////////////////////////////////////////////////
  156. u16 DAC_GetDataOutputValue(emDACCH_TypeDef channel)
  157. {
  158. return (*(vu32*)(DAC_BASE + DOR_Offset + (channel >> 2)));
  159. }
  160. /// @}
  161. /// @}
  162. /// @}