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.

stm32l1xx_ll_dac.c 12KB

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