stm32f1xx_ll_dac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_dac.c
  4. * @author MCD Application Team
  5. * @brief DAC LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f1xx_ll_dac.h"
  21. #include "stm32f1xx_ll_bus.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif /* USE_FULL_ASSERT */
  27. /** @addtogroup STM32F1xx_LL_Driver
  28. * @{
  29. */
  30. #if defined(DAC)
  31. /** @addtogroup DAC_LL DAC
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup DAC_LL_Private_Macros
  39. * @{
  40. */
  41. #define IS_LL_DAC_CHANNEL(__DAC_CHANNEL__) \
  42. (((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1) \
  43. || ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_2) \
  44. )
  45. #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__) \
  46. (((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE) \
  47. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO) \
  48. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO) \
  49. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO) \
  50. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO) \
  51. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO) \
  52. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO) \
  53. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO) \
  54. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO) \
  55. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9) \
  56. )
  57. #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__) \
  58. (((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE) \
  59. || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE) \
  60. || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
  61. )
  62. #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_MODE__, __WAVE_AUTO_GENERATION_CONFIG__) \
  63. ( (((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE) \
  64. && (((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0) \
  65. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0) \
  66. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0) \
  67. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0) \
  68. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0) \
  69. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0) \
  70. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0) \
  71. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0) \
  72. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0) \
  73. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0) \
  74. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0) \
  75. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0)) \
  76. ) \
  77. ||(((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
  78. && (((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1) \
  79. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3) \
  80. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7) \
  81. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15) \
  82. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31) \
  83. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63) \
  84. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127) \
  85. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255) \
  86. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511) \
  87. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023) \
  88. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047) \
  89. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095)) \
  90. ) \
  91. )
  92. #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__) \
  93. (((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE) \
  94. || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE) \
  95. )
  96. /**
  97. * @}
  98. */
  99. /* Private function prototypes -----------------------------------------------*/
  100. /* Exported functions --------------------------------------------------------*/
  101. /** @addtogroup DAC_LL_Exported_Functions
  102. * @{
  103. */
  104. /** @addtogroup DAC_LL_EF_Init
  105. * @{
  106. */
  107. /**
  108. * @brief De-initialize registers of the selected DAC instance
  109. * to their default reset values.
  110. * @param DACx DAC instance
  111. * @retval An ErrorStatus enumeration value:
  112. * - SUCCESS: DAC registers are de-initialized
  113. * - ERROR: not applicable
  114. */
  115. ErrorStatus LL_DAC_DeInit(const DAC_TypeDef *DACx)
  116. {
  117. /* Check the parameters */
  118. assert_param(IS_DAC_ALL_INSTANCE(DACx));
  119. /* Force reset of DAC clock */
  120. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC1);
  121. /* Release reset of DAC clock */
  122. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
  123. return SUCCESS;
  124. }
  125. /**
  126. * @brief Initialize some features of DAC channel.
  127. * @note @ref LL_DAC_Init() aims to ease basic configuration of a DAC channel.
  128. * Leaving it ready to be enabled and output:
  129. * a level by calling one of
  130. * @ref LL_DAC_ConvertData12RightAligned
  131. * @ref LL_DAC_ConvertData12LeftAligned
  132. * @ref LL_DAC_ConvertData8RightAligned
  133. * or one of the supported autogenerated wave.
  134. * @note This function allows configuration of:
  135. * - Output mode
  136. * - Trigger
  137. * - Wave generation
  138. * @note The setting of these parameters by function @ref LL_DAC_Init()
  139. * is conditioned to DAC state:
  140. * DAC channel must be disabled.
  141. * @param DACx DAC instance
  142. * @param DAC_Channel This parameter can be one of the following values:
  143. * @arg @ref LL_DAC_CHANNEL_1
  144. * @arg @ref LL_DAC_CHANNEL_2
  145. * @param DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
  146. * @retval An ErrorStatus enumeration value:
  147. * - SUCCESS: DAC registers are initialized
  148. * - ERROR: DAC registers are not initialized
  149. */
  150. ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, const LL_DAC_InitTypeDef *DAC_InitStruct)
  151. {
  152. ErrorStatus status = SUCCESS;
  153. /* Check the parameters */
  154. assert_param(IS_DAC_ALL_INSTANCE(DACx));
  155. assert_param(IS_LL_DAC_CHANNEL(DAC_Channel));
  156. assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
  157. assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
  158. assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
  159. if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
  160. {
  161. assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGeneration,
  162. DAC_InitStruct->WaveAutoGenerationConfig));
  163. }
  164. /* Note: Hardware constraint (refer to description of this function) */
  165. /* DAC instance must be disabled. */
  166. if (LL_DAC_IsEnabled(DACx, DAC_Channel) == 0UL)
  167. {
  168. /* Configuration of DAC channel: */
  169. /* - TriggerSource */
  170. /* - WaveAutoGeneration */
  171. /* - OutputBuffer */
  172. /* - OutputMode */
  173. if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
  174. {
  175. MODIFY_REG(DACx->CR,
  176. (DAC_CR_TSEL1
  177. | DAC_CR_WAVE1
  178. | DAC_CR_MAMP1
  179. | DAC_CR_BOFF1
  180. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  181. ,
  182. (DAC_InitStruct->TriggerSource
  183. | DAC_InitStruct->WaveAutoGeneration
  184. | DAC_InitStruct->WaveAutoGenerationConfig
  185. | DAC_InitStruct->OutputBuffer
  186. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  187. );
  188. }
  189. else
  190. {
  191. MODIFY_REG(DACx->CR,
  192. (DAC_CR_TSEL1
  193. | DAC_CR_WAVE1
  194. | DAC_CR_BOFF1
  195. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  196. ,
  197. (DAC_InitStruct->TriggerSource
  198. | LL_DAC_WAVE_AUTO_GENERATION_NONE
  199. | DAC_InitStruct->OutputBuffer
  200. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  201. );
  202. }
  203. }
  204. else
  205. {
  206. /* Initialization error: DAC instance is not disabled. */
  207. status = ERROR;
  208. }
  209. return status;
  210. }
  211. /**
  212. * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
  213. * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
  214. * whose fields will be set to default values.
  215. * @retval None
  216. */
  217. void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
  218. {
  219. /* Set DAC_InitStruct fields to default values */
  220. DAC_InitStruct->TriggerSource = LL_DAC_TRIG_SOFTWARE;
  221. DAC_InitStruct->WaveAutoGeneration = LL_DAC_WAVE_AUTO_GENERATION_NONE;
  222. /* Note: Parameter discarded if wave auto generation is disabled, */
  223. /* set anyway to its default value. */
  224. DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
  225. DAC_InitStruct->OutputBuffer = LL_DAC_OUTPUT_BUFFER_ENABLE;
  226. }
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @}
  232. */
  233. /**
  234. * @}
  235. */
  236. #endif /* DAC */
  237. /**
  238. * @}
  239. */
  240. #endif /* USE_FULL_LL_DRIVER */