stm32l1xx_hal_dac_ex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @brief DAC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of DAC extension peripheral:
  8. * + Extended features functions
  9. *
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### How to use this driver #####
  14. ==============================================================================
  15. [..]
  16. (+) When Dual mode is enabled (i.e DAC Channel1 and Channel2 are used simultaneously) :
  17. Use HAL_DACEx_DualGetValue() to get digital data to be converted and use
  18. HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in Channel 1 and Channel 2.
  19. (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
  20. (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
  21. @endverbatim
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. * 1. Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright notice,
  32. * this list of conditions and the following disclaimer in the documentation
  33. * and/or other materials provided with the distribution.
  34. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  47. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. ******************************************************************************
  50. */
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "stm32l1xx_hal.h"
  53. /** @addtogroup STM32L1xx_HAL_Driver
  54. * @{
  55. */
  56. /** @defgroup DACEx DACEx
  57. * @brief DAC driver modules
  58. * @{
  59. */
  60. #ifdef HAL_DAC_MODULE_ENABLED
  61. /* Private typedef -----------------------------------------------------------*/
  62. /* Private define ------------------------------------------------------------*/
  63. /* Private macro -------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /* Private function prototypes -----------------------------------------------*/
  66. /* Private functions ---------------------------------------------------------*/
  67. /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
  68. * @{
  69. */
  70. /** @defgroup DACEx_Exported_Functions_Group1 Extended features functions
  71. * @brief Extended features functions
  72. *
  73. @verbatim
  74. ==============================================================================
  75. ##### Extended features functions #####
  76. ==============================================================================
  77. [..] This section provides functions allowing to:
  78. (+) Start conversion.
  79. (+) Stop conversion.
  80. (+) Start conversion and enable DMA transfer.
  81. (+) Stop conversion and disable DMA transfer.
  82. (+) Get result of conversion.
  83. (+) Get result of dual mode conversion.
  84. @endverbatim
  85. * @{
  86. */
  87. /**
  88. * @brief Returns the last data output value of the selected DAC channel.
  89. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  90. * the configuration information for the specified DAC.
  91. * @retval The selected DAC channel data output value.
  92. */
  93. uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac)
  94. {
  95. uint32_t tmp = 0;
  96. tmp |= hdac->Instance->DOR1;
  97. tmp |= hdac->Instance->DOR2 << 16;
  98. /* Returns the DAC channel data output register value */
  99. return tmp;
  100. }
  101. /**
  102. * @brief Enables or disables the selected DAC channel wave generation.
  103. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  104. * the configuration information for the specified DAC.
  105. * @param Channel: The selected DAC channel.
  106. * This parameter can be one of the following values:
  107. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  108. * @param Amplitude: Select max triangle amplitude.
  109. * This parameter can be one of the following values:
  110. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  111. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  112. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  113. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  114. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  115. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  116. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  117. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  118. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  119. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  120. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  121. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  122. * @retval HAL status
  123. */
  124. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  125. {
  126. /* Check the parameters */
  127. assert_param(IS_DAC_CHANNEL(Channel));
  128. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  129. /* Process locked */
  130. __HAL_LOCK(hdac);
  131. /* Change DAC state */
  132. hdac->State = HAL_DAC_STATE_BUSY;
  133. /* Enable the selected wave generation for the selected DAC channel */
  134. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1)|(DAC_CR_MAMP1))<<Channel, (DAC_CR_WAVE1_1 | Amplitude) << Channel);
  135. /* Change DAC state */
  136. hdac->State = HAL_DAC_STATE_READY;
  137. /* Process unlocked */
  138. __HAL_UNLOCK(hdac);
  139. /* Return function status */
  140. return HAL_OK;
  141. }
  142. /**
  143. * @brief Enables or disables the selected DAC channel wave generation.
  144. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  145. * the configuration information for the specified DAC.
  146. * @param Channel: The selected DAC channel.
  147. * This parameter can be one of the following values:
  148. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  149. * @param Amplitude: Unmask DAC channel LFSR for noise wave generation.
  150. * This parameter can be one of the following values:
  151. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  152. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  153. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  154. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  155. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  156. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  157. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  158. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  159. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  160. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  161. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  162. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  163. * @retval HAL status
  164. */
  165. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  166. {
  167. /* Check the parameters */
  168. assert_param(IS_DAC_CHANNEL(Channel));
  169. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  170. /* Process locked */
  171. __HAL_LOCK(hdac);
  172. /* Change DAC state */
  173. hdac->State = HAL_DAC_STATE_BUSY;
  174. /* Enable the selected wave generation for the selected DAC channel */
  175. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1)|(DAC_CR_MAMP1))<<Channel, (DAC_CR_WAVE1_0 | Amplitude) << Channel);
  176. /* Change DAC state */
  177. hdac->State = HAL_DAC_STATE_READY;
  178. /* Process unlocked */
  179. __HAL_UNLOCK(hdac);
  180. /* Return function status */
  181. return HAL_OK;
  182. }
  183. /**
  184. * @brief Set the specified data holding register value for dual DAC channel.
  185. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  186. * the configuration information for the specified DAC.
  187. * @param Alignment: Specifies the data alignment for dual channel DAC.
  188. * This parameter can be one of the following values:
  189. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  190. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  191. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  192. * @param Data1: Data for DAC Channel2 to be loaded in the selected data holding register.
  193. * @param Data2: Data for DAC Channel1 to be loaded in the selected data holding register.
  194. * @note In dual mode, a unique register access is required to write in both
  195. * DAC channels at the same time.
  196. * @retval HAL status
  197. */
  198. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef* hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  199. {
  200. uint32_t data = 0, tmp = 0;
  201. /* Check the parameters */
  202. assert_param(IS_DAC_ALIGN(Alignment));
  203. assert_param(IS_DAC_DATA(Data1));
  204. assert_param(IS_DAC_DATA(Data2));
  205. /* Calculate and set dual DAC data holding register value */
  206. if (Alignment == DAC_ALIGN_8B_R)
  207. {
  208. data = ((uint32_t)Data2 << 8) | Data1;
  209. }
  210. else
  211. {
  212. data = ((uint32_t)Data2 << 16) | Data1;
  213. }
  214. tmp = (uint32_t)hdac->Instance;
  215. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  216. /* Set the dual DAC selected data holding register */
  217. *(__IO uint32_t *)tmp = data;
  218. /* Return function status */
  219. return HAL_OK;
  220. }
  221. /**
  222. * @brief Conversion complete callback in non blocking mode for Channel2
  223. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  224. * the configuration information for the specified DAC.
  225. * @retval None
  226. */
  227. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  228. {
  229. /* Prevent unused argument(s) compilation warning */
  230. UNUSED(hdac);
  231. /* NOTE : This function Should not be modified, when the callback is needed,
  232. the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
  233. */
  234. }
  235. /**
  236. * @brief Conversion half DMA transfer callback in non blocking mode for Channel2
  237. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  238. * the configuration information for the specified DAC.
  239. * @retval None
  240. */
  241. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  242. {
  243. /* Prevent unused argument(s) compilation warning */
  244. UNUSED(hdac);
  245. /* NOTE : This function Should not be modified, when the callback is needed,
  246. the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
  247. */
  248. }
  249. /**
  250. * @brief Error DAC callback for Channel2.
  251. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  252. * the configuration information for the specified DAC.
  253. * @retval None
  254. */
  255. __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
  256. {
  257. /* Prevent unused argument(s) compilation warning */
  258. UNUSED(hdac);
  259. /* NOTE : This function Should not be modified, when the callback is needed,
  260. the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
  261. */
  262. }
  263. /**
  264. * @brief DMA underrun DAC callback for channel2.
  265. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  266. * the configuration information for the specified DAC.
  267. * @retval None
  268. */
  269. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  270. {
  271. /* Prevent unused argument(s) compilation warning */
  272. UNUSED(hdac);
  273. /* NOTE : This function Should not be modified, when the callback is needed,
  274. the HAL_DAC_DMAUnderrunCallbackCh2 could be implemented in the user file
  275. */
  276. }
  277. /**
  278. * @}
  279. */
  280. /**
  281. * @}
  282. */
  283. /** @defgroup DACEx_Private_Functions DACEx Private Functions
  284. * @{
  285. */
  286. /**
  287. * @brief DMA conversion complete callback.
  288. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  289. * the configuration information for the specified DMA module.
  290. * @retval None
  291. */
  292. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  293. {
  294. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  295. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  296. hdac->State= HAL_DAC_STATE_READY;
  297. }
  298. /**
  299. * @brief DMA half transfer complete callback.
  300. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  301. * the configuration information for the specified DMA module.
  302. * @retval None
  303. */
  304. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  305. {
  306. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  307. /* Conversion complete callback */
  308. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  309. }
  310. /**
  311. * @brief DMA error callback
  312. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  313. * the configuration information for the specified DMA module.
  314. * @retval None
  315. */
  316. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  317. {
  318. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  319. /* Set DAC error code to DMA error */
  320. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  321. HAL_DACEx_ErrorCallbackCh2(hdac);
  322. hdac->State= HAL_DAC_STATE_READY;
  323. }
  324. /**
  325. * @}
  326. */
  327. #endif /* HAL_DAC_MODULE_ENABLED */
  328. /**
  329. * @}
  330. */
  331. /**
  332. * @}
  333. */
  334. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/