stm32f4xx_hal_dac.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dac.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief DAC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Digital to Analog Converter (DAC) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State and Errors functions
  14. *
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### DAC Peripheral features #####
  19. ==============================================================================
  20. [..]
  21. *** DAC Channels ***
  22. ====================
  23. [..]
  24. The device integrates two 12-bit Digital Analog Converters that can
  25. be used independently or simultaneously (dual mode):
  26. (#) DAC channel1 with DAC_OUT1 (PA4) as output
  27. (#) DAC channel2 with DAC_OUT2 (PA5) as output
  28. *** DAC Triggers ***
  29. ====================
  30. [..]
  31. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  32. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  33. [..]
  34. Digital to Analog conversion can be triggered by:
  35. (#) External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_TRIGGER_EXT_IT9.
  36. The used pin (GPIOx_Pin9) must be configured in input mode.
  37. (#) Timers TRGO: TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8
  38. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
  39. (#) Software using DAC_TRIGGER_SOFTWARE
  40. *** DAC Buffer mode feature ***
  41. ===============================
  42. [..]
  43. Each DAC channel integrates an output buffer that can be used to
  44. reduce the output impedance, and to drive external loads directly
  45. without having to add an external operational amplifier.
  46. To enable, the output buffer use
  47. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  48. [..]
  49. (@) Refer to the device datasheet for more details about output
  50. impedance value with and without output buffer.
  51. *** DAC wave generation feature ***
  52. ===================================
  53. [..]
  54. Both DAC channels can be used to generate
  55. (#) Noise wave
  56. (#) Triangle wave
  57. *** DAC data format ***
  58. =======================
  59. [..]
  60. The DAC data format can be:
  61. (#) 8-bit right alignment using DAC_ALIGN_8B_R
  62. (#) 12-bit left alignment using DAC_ALIGN_12B_L
  63. (#) 12-bit right alignment using DAC_ALIGN_12B_R
  64. *** DAC data value to voltage correspondence ***
  65. ================================================
  66. [..]
  67. The analog output voltage on each DAC channel pin is determined
  68. by the following equation:
  69. DAC_OUTx = VREF+ * DOR / 4095
  70. with DOR is the Data Output Register
  71. VEF+ is the input voltage reference (refer to the device datasheet)
  72. e.g. To set DAC_OUT1 to 0.7V, use
  73. Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  74. *** DMA requests ***
  75. =====================
  76. [..]
  77. A DMA1 request can be generated when an external trigger (but not
  78. a software trigger) occurs if DMA1 requests are enabled using
  79. HAL_DAC_Start_DMA()
  80. [..]
  81. DMA1 requests are mapped as following:
  82. (#) DAC channel1 : mapped on DMA1 Stream5 channel7 which must be
  83. already configured
  84. (#) DAC channel2 : mapped on DMA1 Stream6 channel7 which must be
  85. already configured
  86. -@- For Dual mode and specific signal (Triangle and noise) generation please
  87. refer to Extension Features Driver description
  88. ##### How to use this driver #####
  89. ==============================================================================
  90. [..]
  91. (+) DAC APB clock must be enabled to get write access to DAC
  92. registers using HAL_DAC_Init()
  93. (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
  94. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  95. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
  96. *** Polling mode IO operation ***
  97. =================================
  98. [..]
  99. (+) Start the DAC peripheral using HAL_DAC_Start()
  100. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  101. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  102. *** DMA mode IO operation ***
  103. ==============================
  104. [..]
  105. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  106. of data to be transferred at each end of conversion
  107. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
  108. function is executed and user can add his own code by customization of function pointer
  109. HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
  110. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
  111. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  112. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  113. *** DAC HAL driver macros list ***
  114. =============================================
  115. [..]
  116. Below the list of most used macros in DAC HAL driver.
  117. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
  118. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
  119. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
  120. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
  121. [..]
  122. (@) You can refer to the DAC HAL driver header file for more useful macros
  123. @endverbatim
  124. ******************************************************************************
  125. * @attention
  126. *
  127. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  128. *
  129. * Redistribution and use in source and binary forms, with or without modification,
  130. * are permitted provided that the following conditions are met:
  131. * 1. Redistributions of source code must retain the above copyright notice,
  132. * this list of conditions and the following disclaimer.
  133. * 2. Redistributions in binary form must reproduce the above copyright notice,
  134. * this list of conditions and the following disclaimer in the documentation
  135. * and/or other materials provided with the distribution.
  136. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  137. * may be used to endorse or promote products derived from this software
  138. * without specific prior written permission.
  139. *
  140. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  141. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  142. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  143. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  144. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  145. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  146. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  147. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  148. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  149. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  150. *
  151. ******************************************************************************
  152. */
  153. /* Includes ------------------------------------------------------------------*/
  154. #include "stm32f4xx_hal.h"
  155. /** @addtogroup STM32F4xx_HAL_Driver
  156. * @{
  157. */
  158. /** @defgroup DAC DAC
  159. * @brief DAC driver modules
  160. * @{
  161. */
  162. #ifdef HAL_DAC_MODULE_ENABLED
  163. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  164. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  165. defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) ||\
  166. defined(STM32F469xx) || defined(STM32F479xx)
  167. /* Private typedef -----------------------------------------------------------*/
  168. /* Private define ------------------------------------------------------------*/
  169. /* Private macro -------------------------------------------------------------*/
  170. /* Private variables ---------------------------------------------------------*/
  171. /** @addtogroup DAC_Private_Functions
  172. * @{
  173. */
  174. /* Private function prototypes -----------------------------------------------*/
  175. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
  176. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
  177. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
  178. /**
  179. * @}
  180. */
  181. /* Exported functions --------------------------------------------------------*/
  182. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  183. * @{
  184. */
  185. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  186. * @brief Initialization and Configuration functions
  187. *
  188. @verbatim
  189. ==============================================================================
  190. ##### Initialization and de-initialization functions #####
  191. ==============================================================================
  192. [..] This section provides functions allowing to:
  193. (+) Initialize and configure the DAC.
  194. (+) De-initialize the DAC.
  195. @endverbatim
  196. * @{
  197. */
  198. /**
  199. * @brief Initializes the DAC peripheral according to the specified parameters
  200. * in the DAC_InitStruct.
  201. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  202. * the configuration information for the specified DAC.
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
  206. {
  207. /* Check DAC handle */
  208. if(hdac == NULL)
  209. {
  210. return HAL_ERROR;
  211. }
  212. /* Check the parameters */
  213. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  214. if(hdac->State == HAL_DAC_STATE_RESET)
  215. {
  216. /* Allocate lock resource and initialize it */
  217. hdac->Lock = HAL_UNLOCKED;
  218. /* Init the low level hardware */
  219. HAL_DAC_MspInit(hdac);
  220. }
  221. /* Initialize the DAC state*/
  222. hdac->State = HAL_DAC_STATE_BUSY;
  223. /* Set DAC error code to none */
  224. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  225. /* Initialize the DAC state*/
  226. hdac->State = HAL_DAC_STATE_READY;
  227. /* Return function status */
  228. return HAL_OK;
  229. }
  230. /**
  231. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  232. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  233. * the configuration information for the specified DAC.
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  237. {
  238. /* Check DAC handle */
  239. if(hdac == NULL)
  240. {
  241. return HAL_ERROR;
  242. }
  243. /* Check the parameters */
  244. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  245. /* Change DAC state */
  246. hdac->State = HAL_DAC_STATE_BUSY;
  247. /* DeInit the low level hardware */
  248. HAL_DAC_MspDeInit(hdac);
  249. /* Set DAC error code to none */
  250. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  251. /* Change DAC state */
  252. hdac->State = HAL_DAC_STATE_RESET;
  253. /* Release Lock */
  254. __HAL_UNLOCK(hdac);
  255. /* Return function status */
  256. return HAL_OK;
  257. }
  258. /**
  259. * @brief Initializes the DAC MSP.
  260. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  261. * the configuration information for the specified DAC.
  262. * @retval None
  263. */
  264. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
  265. {
  266. /* Prevent unused argument(s) compilation warning */
  267. UNUSED(hdac);
  268. /* NOTE : This function Should not be modified, when the callback is needed,
  269. the HAL_DAC_MspInit could be implemented in the user file
  270. */
  271. }
  272. /**
  273. * @brief DeInitializes the DAC MSP.
  274. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  275. * the configuration information for the specified DAC.
  276. * @retval None
  277. */
  278. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
  279. {
  280. /* Prevent unused argument(s) compilation warning */
  281. UNUSED(hdac);
  282. /* NOTE : This function Should not be modified, when the callback is needed,
  283. the HAL_DAC_MspDeInit could be implemented in the user file
  284. */
  285. }
  286. /**
  287. * @}
  288. */
  289. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  290. * @brief IO operation functions
  291. *
  292. @verbatim
  293. ==============================================================================
  294. ##### IO operation functions #####
  295. ==============================================================================
  296. [..] This section provides functions allowing to:
  297. (+) Start conversion.
  298. (+) Stop conversion.
  299. (+) Start conversion and enable DMA transfer.
  300. (+) Stop conversion and disable DMA transfer.
  301. (+) Get result of conversion.
  302. @endverbatim
  303. * @{
  304. */
  305. /**
  306. * @brief Enables DAC and starts conversion of channel.
  307. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  308. * the configuration information for the specified DAC.
  309. * @param Channel: The selected DAC channel.
  310. * This parameter can be one of the following values:
  311. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  312. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
  316. {
  317. uint32_t tmp1 = 0, tmp2 = 0;
  318. /* Check the parameters */
  319. assert_param(IS_DAC_CHANNEL(Channel));
  320. /* Process locked */
  321. __HAL_LOCK(hdac);
  322. /* Change DAC state */
  323. hdac->State = HAL_DAC_STATE_BUSY;
  324. /* Enable the Peripheral */
  325. __HAL_DAC_ENABLE(hdac, Channel);
  326. if(Channel == DAC_CHANNEL_1)
  327. {
  328. tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
  329. tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
  330. /* Check if software trigger enabled */
  331. if((tmp1 == DAC_CR_TEN1) && (tmp2 == DAC_CR_TSEL1))
  332. {
  333. /* Enable the selected DAC software conversion */
  334. hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
  335. }
  336. }
  337. else
  338. {
  339. tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
  340. tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;
  341. /* Check if software trigger enabled */
  342. if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
  343. {
  344. /* Enable the selected DAC software conversion*/
  345. hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
  346. }
  347. }
  348. /* Change DAC state */
  349. hdac->State = HAL_DAC_STATE_READY;
  350. /* Process unlocked */
  351. __HAL_UNLOCK(hdac);
  352. /* Return function status */
  353. return HAL_OK;
  354. }
  355. /**
  356. * @brief Disables DAC and stop conversion of channel.
  357. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  358. * the configuration information for the specified DAC.
  359. * @param Channel: The selected DAC channel.
  360. * This parameter can be one of the following values:
  361. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  362. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  363. * @retval HAL status
  364. */
  365. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  366. {
  367. /* Check the parameters */
  368. assert_param(IS_DAC_CHANNEL(Channel));
  369. /* Disable the Peripheral */
  370. __HAL_DAC_DISABLE(hdac, Channel);
  371. /* Change DAC state */
  372. hdac->State = HAL_DAC_STATE_READY;
  373. /* Return function status */
  374. return HAL_OK;
  375. }
  376. /**
  377. * @brief Enables DAC and starts conversion of channel.
  378. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  379. * the configuration information for the specified DAC.
  380. * @param Channel: The selected DAC channel.
  381. * This parameter can be one of the following values:
  382. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  383. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  384. * @param pData: The destination peripheral Buffer address.
  385. * @param Length: The length of data to be transferred from memory to DAC peripheral
  386. * @param Alignment: Specifies the data alignment for DAC channel.
  387. * This parameter can be one of the following values:
  388. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  389. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  390. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  391. * @retval HAL status
  392. */
  393. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  394. {
  395. uint32_t tmpreg = 0;
  396. /* Check the parameters */
  397. assert_param(IS_DAC_CHANNEL(Channel));
  398. assert_param(IS_DAC_ALIGN(Alignment));
  399. /* Process locked */
  400. __HAL_LOCK(hdac);
  401. /* Change DAC state */
  402. hdac->State = HAL_DAC_STATE_BUSY;
  403. if(Channel == DAC_CHANNEL_1)
  404. {
  405. /* Set the DMA transfer complete callback for channel1 */
  406. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  407. /* Set the DMA half transfer complete callback for channel1 */
  408. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  409. /* Set the DMA error callback for channel1 */
  410. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  411. /* Enable the selected DAC channel1 DMA request */
  412. hdac->Instance->CR |= DAC_CR_DMAEN1;
  413. /* Case of use of channel 1 */
  414. switch(Alignment)
  415. {
  416. case DAC_ALIGN_12B_R:
  417. /* Get DHR12R1 address */
  418. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  419. break;
  420. case DAC_ALIGN_12B_L:
  421. /* Get DHR12L1 address */
  422. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  423. break;
  424. case DAC_ALIGN_8B_R:
  425. /* Get DHR8R1 address */
  426. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  427. break;
  428. default:
  429. break;
  430. }
  431. }
  432. else
  433. {
  434. /* Set the DMA transfer complete callback for channel2 */
  435. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  436. /* Set the DMA half transfer complete callback for channel2 */
  437. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  438. /* Set the DMA error callback for channel2 */
  439. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  440. /* Enable the selected DAC channel2 DMA request */
  441. hdac->Instance->CR |= DAC_CR_DMAEN2;
  442. /* Case of use of channel 2 */
  443. switch(Alignment)
  444. {
  445. case DAC_ALIGN_12B_R:
  446. /* Get DHR12R2 address */
  447. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  448. break;
  449. case DAC_ALIGN_12B_L:
  450. /* Get DHR12L2 address */
  451. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  452. break;
  453. case DAC_ALIGN_8B_R:
  454. /* Get DHR8R2 address */
  455. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  456. break;
  457. default:
  458. break;
  459. }
  460. }
  461. /* Enable the DMA Stream */
  462. if(Channel == DAC_CHANNEL_1)
  463. {
  464. /* Enable the DAC DMA underrun interrupt */
  465. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  466. /* Enable the DMA Stream */
  467. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  468. }
  469. else
  470. {
  471. /* Enable the DAC DMA underrun interrupt */
  472. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  473. /* Enable the DMA Stream */
  474. HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  475. }
  476. /* Enable the Peripheral */
  477. __HAL_DAC_ENABLE(hdac, Channel);
  478. /* Process Unlocked */
  479. __HAL_UNLOCK(hdac);
  480. /* Return function status */
  481. return HAL_OK;
  482. }
  483. /**
  484. * @brief Disables DAC and stop conversion of channel.
  485. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  486. * the configuration information for the specified DAC.
  487. * @param Channel: The selected DAC channel.
  488. * This parameter can be one of the following values:
  489. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  490. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  491. * @retval HAL status
  492. */
  493. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  494. {
  495. HAL_StatusTypeDef status = HAL_OK;
  496. /* Check the parameters */
  497. assert_param(IS_DAC_CHANNEL(Channel));
  498. /* Disable the selected DAC channel DMA request */
  499. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
  500. /* Disable the Peripheral */
  501. __HAL_DAC_DISABLE(hdac, Channel);
  502. /* Disable the DMA Channel */
  503. /* Channel1 is used */
  504. if(Channel == DAC_CHANNEL_1)
  505. {
  506. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  507. }
  508. else /* Channel2 is used for */
  509. {
  510. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  511. }
  512. /* Check if DMA Channel effectively disabled */
  513. if(status != HAL_OK)
  514. {
  515. /* Update DAC state machine to error */
  516. hdac->State = HAL_DAC_STATE_ERROR;
  517. }
  518. else
  519. {
  520. /* Change DAC state */
  521. hdac->State = HAL_DAC_STATE_READY;
  522. }
  523. /* Return function status */
  524. return status;
  525. }
  526. /**
  527. * @brief Returns the last data output value of the selected DAC channel.
  528. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  529. * the configuration information for the specified DAC.
  530. * @param Channel: The selected DAC channel.
  531. * This parameter can be one of the following values:
  532. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  533. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  534. * @retval The selected DAC channel data output value.
  535. */
  536. uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  537. {
  538. /* Check the parameters */
  539. assert_param(IS_DAC_CHANNEL(Channel));
  540. /* Returns the DAC channel data output register value */
  541. if(Channel == DAC_CHANNEL_1)
  542. {
  543. return hdac->Instance->DOR1;
  544. }
  545. else
  546. {
  547. return hdac->Instance->DOR2;
  548. }
  549. }
  550. /**
  551. * @brief Handles DAC interrupt request
  552. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  553. * the configuration information for the specified DAC.
  554. * @retval None
  555. */
  556. void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
  557. {
  558. /* Check underrun channel 1 flag */
  559. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
  560. {
  561. /* Change DAC state to error state */
  562. hdac->State = HAL_DAC_STATE_ERROR;
  563. /* Set DAC error code to channel1 DMA underrun error */
  564. hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
  565. /* Clear the underrun flag */
  566. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
  567. /* Disable the selected DAC channel1 DMA request */
  568. hdac->Instance->CR &= ~DAC_CR_DMAEN1;
  569. /* Error callback */
  570. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  571. }
  572. /* Check underrun channel 2 flag */
  573. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
  574. {
  575. /* Change DAC state to error state */
  576. hdac->State = HAL_DAC_STATE_ERROR;
  577. /* Set DAC error code to channel2 DMA underrun error */
  578. hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
  579. /* Clear the underrun flag */
  580. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
  581. /* Disable the selected DAC channel1 DMA request */
  582. hdac->Instance->CR &= ~DAC_CR_DMAEN2;
  583. /* Error callback */
  584. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  585. }
  586. }
  587. /**
  588. * @brief Conversion complete callback in non blocking mode for Channel1
  589. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  590. * the configuration information for the specified DAC.
  591. * @retval None
  592. */
  593. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  594. {
  595. /* Prevent unused argument(s) compilation warning */
  596. UNUSED(hdac);
  597. /* NOTE : This function Should not be modified, when the callback is needed,
  598. the HAL_DAC_ConvCpltCallback could be implemented in the user file
  599. */
  600. }
  601. /**
  602. * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
  603. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  604. * the configuration information for the specified DAC.
  605. * @retval None
  606. */
  607. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  608. {
  609. /* Prevent unused argument(s) compilation warning */
  610. UNUSED(hdac);
  611. /* NOTE : This function Should not be modified, when the callback is needed,
  612. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  613. */
  614. }
  615. /**
  616. * @brief Error DAC callback for Channel1.
  617. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  618. * the configuration information for the specified DAC.
  619. * @retval None
  620. */
  621. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  622. {
  623. /* Prevent unused argument(s) compilation warning */
  624. UNUSED(hdac);
  625. /* NOTE : This function Should not be modified, when the callback is needed,
  626. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  627. */
  628. }
  629. /**
  630. * @brief DMA underrun DAC callback for channel1.
  631. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  632. * the configuration information for the specified DAC.
  633. * @retval None
  634. */
  635. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  636. {
  637. /* Prevent unused argument(s) compilation warning */
  638. UNUSED(hdac);
  639. /* NOTE : This function Should not be modified, when the callback is needed,
  640. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  641. */
  642. }
  643. /**
  644. * @}
  645. */
  646. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  647. * @brief Peripheral Control functions
  648. *
  649. @verbatim
  650. ==============================================================================
  651. ##### Peripheral Control functions #####
  652. ==============================================================================
  653. [..] This section provides functions allowing to:
  654. (+) Configure channels.
  655. (+) Set the specified data holding register value for DAC channel.
  656. @endverbatim
  657. * @{
  658. */
  659. /**
  660. * @brief Configures the selected DAC channel.
  661. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  662. * the configuration information for the specified DAC.
  663. * @param sConfig: DAC configuration structure.
  664. * @param Channel: The selected DAC channel.
  665. * This parameter can be one of the following values:
  666. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  667. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  668. * @retval HAL status
  669. */
  670. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  671. {
  672. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  673. /* Check the DAC parameters */
  674. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  675. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  676. assert_param(IS_DAC_CHANNEL(Channel));
  677. /* Process locked */
  678. __HAL_LOCK(hdac);
  679. /* Change DAC state */
  680. hdac->State = HAL_DAC_STATE_BUSY;
  681. /* Get the DAC CR value */
  682. tmpreg1 = hdac->Instance->CR;
  683. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  684. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
  685. /* Configure for the selected DAC channel: buffer output, trigger */
  686. /* Set TSELx and TENx bits according to DAC_Trigger value */
  687. /* Set BOFFx bit according to DAC_OutputBuffer value */
  688. tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
  689. /* Calculate CR register value depending on DAC_Channel */
  690. tmpreg1 |= tmpreg2 << Channel;
  691. /* Write to DAC CR */
  692. hdac->Instance->CR = tmpreg1;
  693. /* Disable wave generation */
  694. hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
  695. /* Change DAC state */
  696. hdac->State = HAL_DAC_STATE_READY;
  697. /* Process unlocked */
  698. __HAL_UNLOCK(hdac);
  699. /* Return function status */
  700. return HAL_OK;
  701. }
  702. /**
  703. * @brief Set the specified data holding register value for DAC channel.
  704. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  705. * the configuration information for the specified DAC.
  706. * @param Channel: The selected DAC channel.
  707. * This parameter can be one of the following values:
  708. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  709. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  710. * @param Alignment: Specifies the data alignment.
  711. * This parameter can be one of the following values:
  712. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  713. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  714. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  715. * @param Data: Data to be loaded in the selected data holding register.
  716. * @retval HAL status
  717. */
  718. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  719. {
  720. __IO uint32_t tmp = 0;
  721. /* Check the parameters */
  722. assert_param(IS_DAC_CHANNEL(Channel));
  723. assert_param(IS_DAC_ALIGN(Alignment));
  724. assert_param(IS_DAC_DATA(Data));
  725. tmp = (uint32_t)hdac->Instance;
  726. if(Channel == DAC_CHANNEL_1)
  727. {
  728. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  729. }
  730. else
  731. {
  732. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  733. }
  734. /* Set the DAC channel1 selected data holding register */
  735. *(__IO uint32_t *) tmp = Data;
  736. /* Return function status */
  737. return HAL_OK;
  738. }
  739. /**
  740. * @}
  741. */
  742. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  743. * @brief Peripheral State and Errors functions
  744. *
  745. @verbatim
  746. ==============================================================================
  747. ##### Peripheral State and Errors functions #####
  748. ==============================================================================
  749. [..]
  750. This subsection provides functions allowing to
  751. (+) Check the DAC state.
  752. (+) Check the DAC Errors.
  753. @endverbatim
  754. * @{
  755. */
  756. /**
  757. * @brief return the DAC state
  758. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  759. * the configuration information for the specified DAC.
  760. * @retval HAL state
  761. */
  762. HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
  763. {
  764. /* Return DAC state */
  765. return hdac->State;
  766. }
  767. /**
  768. * @brief Return the DAC error code
  769. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  770. * the configuration information for the specified DAC.
  771. * @retval DAC Error Code
  772. */
  773. uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
  774. {
  775. return hdac->ErrorCode;
  776. }
  777. /**
  778. * @}
  779. */
  780. /**
  781. * @brief DMA conversion complete callback.
  782. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  783. * the configuration information for the specified DMA module.
  784. * @retval None
  785. */
  786. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  787. {
  788. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  789. HAL_DAC_ConvCpltCallbackCh1(hdac);
  790. hdac->State= HAL_DAC_STATE_READY;
  791. }
  792. /**
  793. * @brief DMA half transfer complete callback.
  794. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  795. * the configuration information for the specified DMA module.
  796. * @retval None
  797. */
  798. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  799. {
  800. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  801. /* Conversion complete callback */
  802. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  803. }
  804. /**
  805. * @brief DMA error callback
  806. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  807. * the configuration information for the specified DMA module.
  808. * @retval None
  809. */
  810. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  811. {
  812. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  813. /* Set DAC error code to DMA error */
  814. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  815. HAL_DAC_ErrorCallbackCh1(hdac);
  816. hdac->State= HAL_DAC_STATE_READY;
  817. }
  818. /**
  819. * @}
  820. */
  821. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  822. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  823. STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx */
  824. #endif /* HAL_DAC_MODULE_ENABLED */
  825. /**
  826. * @}
  827. */
  828. /**
  829. * @}
  830. */
  831. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/