stm32f1xx_ll_dac.c 12 KB

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