stm32l1xx_hal_dac_ex.c 15 KB

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