stm32l4xx_ll_dac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_dac.c
  4. * @author MCD Application Team
  5. * @brief DAC LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l4xx_ll_dac.h"
  38. #include "stm32l4xx_ll_bus.h"
  39. #ifdef USE_FULL_ASSERT
  40. #include "stm32_assert.h"
  41. #else
  42. #define assert_param(expr) ((void)0U)
  43. #endif
  44. /** @addtogroup STM32L4xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (DAC1)
  48. /** @addtogroup DAC_LL DAC
  49. * @{
  50. */
  51. /* Private types -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private constants ---------------------------------------------------------*/
  54. /* Private macros ------------------------------------------------------------*/
  55. /** @addtogroup DAC_LL_Private_Macros
  56. * @{
  57. */
  58. #if defined(DAC_CHANNEL2_SUPPORT)
  59. #define IS_LL_DAC_CHANNEL(__DACX__, __DAC_CHANNEL__) \
  60. ( \
  61. ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1) \
  62. || ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_2) \
  63. )
  64. #else
  65. #define IS_LL_DAC_CHANNEL(__DACX__, __DAC_CHANNEL__) \
  66. ( \
  67. ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1) \
  68. )
  69. #endif /* DAC_CHANNEL2_SUPPORT */
  70. #if defined (DAC_CR_TSEL1_3)
  71. #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__) \
  72. ( ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM1_TRGO) \
  73. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO) \
  74. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO) \
  75. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO) \
  76. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO) \
  77. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO) \
  78. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO) \
  79. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO) \
  80. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM1_OUT_TRGO) \
  81. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM2_OUT_TRGO) \
  82. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9) \
  83. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE) \
  84. )
  85. #else
  86. #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__) \
  87. ( ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE) \
  88. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO) \
  89. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO) \
  90. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO) \
  91. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO) \
  92. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO) \
  93. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO) \
  94. || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9) \
  95. )
  96. #endif /* DAC_CR_TSEL1_3 */
  97. #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__) \
  98. ( ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE) \
  99. || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE) \
  100. || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
  101. )
  102. #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_CONFIG__) \
  103. ( ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0) \
  104. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0) \
  105. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0) \
  106. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0) \
  107. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0) \
  108. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0) \
  109. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0) \
  110. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0) \
  111. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0) \
  112. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0) \
  113. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0) \
  114. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0) \
  115. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1) \
  116. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3) \
  117. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7) \
  118. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15) \
  119. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31) \
  120. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63) \
  121. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127) \
  122. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255) \
  123. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511) \
  124. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023) \
  125. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047) \
  126. || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095) \
  127. )
  128. #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__) \
  129. ( ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE) \
  130. || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE) \
  131. )
  132. #define IS_LL_DAC_OUTPUT_CONNECTION(__OUTPUT_CONNECTION__) \
  133. ( ((__OUTPUT_CONNECTION__) == LL_DAC_OUTPUT_CONNECT_GPIO) \
  134. || ((__OUTPUT_CONNECTION__) == LL_DAC_OUTPUT_CONNECT_INTERNAL) \
  135. )
  136. #define IS_LL_DAC_OUTPUT_MODE(__OUTPUT_MODE__) \
  137. ( ((__OUTPUT_MODE__) == LL_DAC_OUTPUT_MODE_NORMAL) \
  138. || ((__OUTPUT_MODE__) == LL_DAC_OUTPUT_MODE_SAMPLE_AND_HOLD) \
  139. )
  140. /**
  141. * @}
  142. */
  143. /* Private function prototypes -----------------------------------------------*/
  144. /* Exported functions --------------------------------------------------------*/
  145. /** @addtogroup DAC_LL_Exported_Functions
  146. * @{
  147. */
  148. /** @addtogroup DAC_LL_EF_Init
  149. * @{
  150. */
  151. /**
  152. * @brief De-initialize registers of the selected DAC instance
  153. * to their default reset values.
  154. * @param DACx DAC instance
  155. * @retval An ErrorStatus enumeration value:
  156. * - SUCCESS: DAC registers are de-initialized
  157. * - ERROR: not applicable
  158. */
  159. ErrorStatus LL_DAC_DeInit(DAC_TypeDef *DACx)
  160. {
  161. /* Check the parameters */
  162. assert_param(IS_DAC_ALL_INSTANCE(DACx));
  163. /* Force reset of DAC clock */
  164. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC1);
  165. /* Release reset of DAC clock */
  166. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
  167. return SUCCESS;
  168. }
  169. /**
  170. * @brief Initialize some features of DAC instance.
  171. * @note The setting of these parameters by function @ref LL_DAC_Init()
  172. * is conditioned to DAC state:
  173. * DAC instance must be disabled.
  174. * @param DACx DAC instance
  175. * @param DAC_Channel This parameter can be one of the following values:
  176. * @arg @ref LL_DAC_CHANNEL_1
  177. * @arg @ref LL_DAC_CHANNEL_2 (1)
  178. *
  179. * (1) On this STM32 serie, parameter not available on all devices.
  180. * Refer to device datasheet for channels availability.
  181. * @param DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
  182. * @retval An ErrorStatus enumeration value:
  183. * - SUCCESS: DAC registers are initialized
  184. * - ERROR: DAC registers are not initialized
  185. */
  186. ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
  187. {
  188. ErrorStatus status = SUCCESS;
  189. /* Check the parameters */
  190. assert_param(IS_DAC_ALL_INSTANCE(DACx));
  191. assert_param(IS_LL_DAC_CHANNEL(DACx, DAC_Channel));
  192. assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
  193. assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
  194. assert_param(IS_LL_DAC_OUTPUT_CONNECTION(DAC_InitStruct->OutputConnection));
  195. assert_param(IS_LL_DAC_OUTPUT_MODE(DAC_InitStruct->OutputMode));
  196. assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
  197. if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
  198. {
  199. assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGenerationConfig));
  200. }
  201. /* Note: Hardware constraint (refer to description of this function) */
  202. /* DAC instance must be disabled. */
  203. if(LL_DAC_IsEnabled(DACx, DAC_Channel) == 0U)
  204. {
  205. /* Configuration of DAC channel: */
  206. /* - TriggerSource */
  207. /* - WaveAutoGeneration */
  208. /* - OutputBuffer */
  209. /* - OutputConnection */
  210. /* - OutputMode */
  211. if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
  212. {
  213. MODIFY_REG(DACx->CR,
  214. ( DAC_CR_TSEL1
  215. | DAC_CR_WAVE1
  216. | DAC_CR_MAMP1
  217. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  218. ,
  219. ( DAC_InitStruct->TriggerSource
  220. | DAC_InitStruct->WaveAutoGeneration
  221. | DAC_InitStruct->WaveAutoGenerationConfig
  222. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  223. );
  224. }
  225. else
  226. {
  227. MODIFY_REG(DACx->CR,
  228. ( DAC_CR_TSEL1
  229. | DAC_CR_WAVE1
  230. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  231. ,
  232. ( DAC_InitStruct->TriggerSource
  233. | LL_DAC_WAVE_AUTO_GENERATION_NONE
  234. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  235. );
  236. }
  237. MODIFY_REG(DACx->MCR,
  238. ( DAC_MCR_MODE1_1
  239. | DAC_MCR_MODE1_0
  240. | DAC_MCR_MODE1_2
  241. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  242. ,
  243. ( DAC_InitStruct->OutputBuffer
  244. | DAC_InitStruct->OutputConnection
  245. | DAC_InitStruct->OutputMode
  246. ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
  247. );
  248. }
  249. else
  250. {
  251. /* Initialization error: DAC instance is not disabled. */
  252. status = ERROR;
  253. }
  254. return status;
  255. }
  256. /**
  257. * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
  258. * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
  259. * whose fields will be set to default values.
  260. * @retval None
  261. */
  262. void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
  263. {
  264. /* Set DAC_InitStruct fields to default values */
  265. DAC_InitStruct->TriggerSource = LL_DAC_TRIG_SOFTWARE;
  266. DAC_InitStruct->WaveAutoGeneration = LL_DAC_WAVE_AUTO_GENERATION_NONE;
  267. /* Note: Parameter discarded if wave auto generation is disabled, */
  268. /* set anyway to its default value. */
  269. DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
  270. DAC_InitStruct->OutputBuffer = LL_DAC_OUTPUT_BUFFER_ENABLE;
  271. DAC_InitStruct->OutputConnection = LL_DAC_OUTPUT_CONNECT_GPIO;
  272. DAC_InitStruct->OutputMode = LL_DAC_OUTPUT_MODE_NORMAL;
  273. }
  274. /**
  275. * @}
  276. */
  277. /**
  278. * @}
  279. */
  280. /**
  281. * @}
  282. */
  283. #endif /* DAC1 */
  284. /**
  285. * @}
  286. */
  287. #endif /* USE_FULL_LL_DRIVER */
  288. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/