You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f1xx_ll_dac.c 12KB

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