stm32f4xx_hal_dac.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dac.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-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(STM32F446xx)
  166. /* Private typedef -----------------------------------------------------------*/
  167. /* Private define ------------------------------------------------------------*/
  168. /* Private macro -------------------------------------------------------------*/
  169. /* Private variables ---------------------------------------------------------*/
  170. /** @addtogroup DAC_Private_Functions
  171. * @{
  172. */
  173. /* Private function prototypes -----------------------------------------------*/
  174. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
  175. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
  176. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
  177. /**
  178. * @}
  179. */
  180. /* Exported functions --------------------------------------------------------*/
  181. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  182. * @{
  183. */
  184. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  185. * @brief Initialization and Configuration functions
  186. *
  187. @verbatim
  188. ==============================================================================
  189. ##### Initialization and de-initialization functions #####
  190. ==============================================================================
  191. [..] This section provides functions allowing to:
  192. (+) Initialize and configure the DAC.
  193. (+) De-initialize the DAC.
  194. @endverbatim
  195. * @{
  196. */
  197. /**
  198. * @brief Initializes the DAC peripheral according to the specified parameters
  199. * in the DAC_InitStruct.
  200. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  201. * the configuration information for the specified DAC.
  202. * @retval HAL status
  203. */
  204. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
  205. {
  206. /* Check DAC handle */
  207. if(hdac == NULL)
  208. {
  209. return HAL_ERROR;
  210. }
  211. /* Check the parameters */
  212. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  213. if(hdac->State == HAL_DAC_STATE_RESET)
  214. {
  215. /* Allocate lock resource and initialize it */
  216. hdac->Lock = HAL_UNLOCKED;
  217. /* Init the low level hardware */
  218. HAL_DAC_MspInit(hdac);
  219. }
  220. /* Initialize the DAC state*/
  221. hdac->State = HAL_DAC_STATE_BUSY;
  222. /* Set DAC error code to none */
  223. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  224. /* Initialize the DAC state*/
  225. hdac->State = HAL_DAC_STATE_READY;
  226. /* Return function status */
  227. return HAL_OK;
  228. }
  229. /**
  230. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  231. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  232. * the configuration information for the specified DAC.
  233. * @retval HAL status
  234. */
  235. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  236. {
  237. /* Check DAC handle */
  238. if(hdac == NULL)
  239. {
  240. return HAL_ERROR;
  241. }
  242. /* Check the parameters */
  243. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  244. /* Change DAC state */
  245. hdac->State = HAL_DAC_STATE_BUSY;
  246. /* DeInit the low level hardware */
  247. HAL_DAC_MspDeInit(hdac);
  248. /* Set DAC error code to none */
  249. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  250. /* Change DAC state */
  251. hdac->State = HAL_DAC_STATE_RESET;
  252. /* Release Lock */
  253. __HAL_UNLOCK(hdac);
  254. /* Return function status */
  255. return HAL_OK;
  256. }
  257. /**
  258. * @brief Initializes the DAC MSP.
  259. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  260. * the configuration information for the specified DAC.
  261. * @retval None
  262. */
  263. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
  264. {
  265. /* NOTE : This function Should not be modified, when the callback is needed,
  266. the HAL_DAC_MspInit could be implemented in the user file
  267. */
  268. }
  269. /**
  270. * @brief DeInitializes the DAC MSP.
  271. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  272. * the configuration information for the specified DAC.
  273. * @retval None
  274. */
  275. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
  276. {
  277. /* NOTE : This function Should not be modified, when the callback is needed,
  278. the HAL_DAC_MspDeInit could be implemented in the user file
  279. */
  280. }
  281. /**
  282. * @}
  283. */
  284. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  285. * @brief IO operation functions
  286. *
  287. @verbatim
  288. ==============================================================================
  289. ##### IO operation functions #####
  290. ==============================================================================
  291. [..] This section provides functions allowing to:
  292. (+) Start conversion.
  293. (+) Stop conversion.
  294. (+) Start conversion and enable DMA transfer.
  295. (+) Stop conversion and disable DMA transfer.
  296. (+) Get result of conversion.
  297. @endverbatim
  298. * @{
  299. */
  300. /**
  301. * @brief Enables DAC and starts conversion of channel.
  302. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  303. * the configuration information for the specified DAC.
  304. * @param Channel: The selected DAC channel.
  305. * This parameter can be one of the following values:
  306. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  307. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  308. * @retval HAL status
  309. */
  310. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
  311. {
  312. /* Check the parameters */
  313. assert_param(IS_DAC_CHANNEL(Channel));
  314. /* Process locked */
  315. __HAL_LOCK(hdac);
  316. /* Change DAC state */
  317. hdac->State = HAL_DAC_STATE_BUSY;
  318. /* Enable the Peripheral */
  319. __HAL_DAC_ENABLE(hdac, Channel);
  320. /* Change DAC state */
  321. hdac->State = HAL_DAC_STATE_READY;
  322. /* Process unlocked */
  323. __HAL_UNLOCK(hdac);
  324. /* Return function status */
  325. return HAL_OK;
  326. }
  327. /**
  328. * @brief Disables DAC and stop conversion of channel.
  329. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  330. * the configuration information for the specified DAC.
  331. * @param Channel: The selected DAC channel.
  332. * This parameter can be one of the following values:
  333. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  334. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  335. * @retval HAL status
  336. */
  337. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  338. {
  339. /* Check the parameters */
  340. assert_param(IS_DAC_CHANNEL(Channel));
  341. /* Disable the Peripheral */
  342. __HAL_DAC_DISABLE(hdac, Channel);
  343. /* Change DAC state */
  344. hdac->State = HAL_DAC_STATE_READY;
  345. /* Return function status */
  346. return HAL_OK;
  347. }
  348. /**
  349. * @brief Enables DAC and starts conversion of channel.
  350. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  351. * the configuration information for the specified DAC.
  352. * @param Channel: The selected DAC channel.
  353. * This parameter can be one of the following values:
  354. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  355. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  356. * @param pData: The destination peripheral Buffer address.
  357. * @param Length: The length of data to be transferred from memory to DAC peripheral
  358. * @param Alignment: Specifies the data alignment for DAC channel.
  359. * This parameter can be one of the following values:
  360. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  361. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  362. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  363. * @retval HAL status
  364. */
  365. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  366. {
  367. uint32_t tmpreg = 0;
  368. /* Check the parameters */
  369. assert_param(IS_DAC_CHANNEL(Channel));
  370. assert_param(IS_DAC_ALIGN(Alignment));
  371. /* Process locked */
  372. __HAL_LOCK(hdac);
  373. /* Change DAC state */
  374. hdac->State = HAL_DAC_STATE_BUSY;
  375. if(Channel == DAC_CHANNEL_1)
  376. {
  377. /* Set the DMA transfer complete callback for channel1 */
  378. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  379. /* Set the DMA half transfer complete callback for channel1 */
  380. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  381. /* Set the DMA error callback for channel1 */
  382. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  383. /* Enable the selected DAC channel1 DMA request */
  384. hdac->Instance->CR |= DAC_CR_DMAEN1;
  385. /* Case of use of channel 1 */
  386. switch(Alignment)
  387. {
  388. case DAC_ALIGN_12B_R:
  389. /* Get DHR12R1 address */
  390. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  391. break;
  392. case DAC_ALIGN_12B_L:
  393. /* Get DHR12L1 address */
  394. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  395. break;
  396. case DAC_ALIGN_8B_R:
  397. /* Get DHR8R1 address */
  398. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  399. break;
  400. default:
  401. break;
  402. }
  403. }
  404. else
  405. {
  406. /* Set the DMA transfer complete callback for channel2 */
  407. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  408. /* Set the DMA half transfer complete callback for channel2 */
  409. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  410. /* Set the DMA error callback for channel2 */
  411. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  412. /* Enable the selected DAC channel2 DMA request */
  413. hdac->Instance->CR |= DAC_CR_DMAEN2;
  414. /* Case of use of channel 2 */
  415. switch(Alignment)
  416. {
  417. case DAC_ALIGN_12B_R:
  418. /* Get DHR12R2 address */
  419. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  420. break;
  421. case DAC_ALIGN_12B_L:
  422. /* Get DHR12L2 address */
  423. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  424. break;
  425. case DAC_ALIGN_8B_R:
  426. /* Get DHR8R2 address */
  427. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  428. break;
  429. default:
  430. break;
  431. }
  432. }
  433. /* Enable the DMA Stream */
  434. if(Channel == DAC_CHANNEL_1)
  435. {
  436. /* Enable the DAC DMA underrun interrupt */
  437. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  438. /* Enable the DMA Stream */
  439. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  440. }
  441. else
  442. {
  443. /* Enable the DAC DMA underrun interrupt */
  444. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  445. /* Enable the DMA Stream */
  446. HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  447. }
  448. /* Enable the Peripheral */
  449. __HAL_DAC_ENABLE(hdac, Channel);
  450. /* Process Unlocked */
  451. __HAL_UNLOCK(hdac);
  452. /* Return function status */
  453. return HAL_OK;
  454. }
  455. /**
  456. * @brief Disables DAC and stop conversion of channel.
  457. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  458. * the configuration information for the specified DAC.
  459. * @param Channel: The selected DAC channel.
  460. * This parameter can be one of the following values:
  461. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  462. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  463. * @retval HAL status
  464. */
  465. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  466. {
  467. HAL_StatusTypeDef status = HAL_OK;
  468. /* Check the parameters */
  469. assert_param(IS_DAC_CHANNEL(Channel));
  470. /* Disable the selected DAC channel DMA request */
  471. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
  472. /* Disable the Peripheral */
  473. __HAL_DAC_DISABLE(hdac, Channel);
  474. /* Disable the DMA Channel */
  475. /* Channel1 is used */
  476. if(Channel == DAC_CHANNEL_1)
  477. {
  478. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  479. }
  480. else /* Channel2 is used for */
  481. {
  482. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  483. }
  484. /* Check if DMA Channel effectively disabled */
  485. if(status != HAL_OK)
  486. {
  487. /* Update DAC state machine to error */
  488. hdac->State = HAL_DAC_STATE_ERROR;
  489. }
  490. else
  491. {
  492. /* Change DAC state */
  493. hdac->State = HAL_DAC_STATE_READY;
  494. }
  495. /* Return function status */
  496. return status;
  497. }
  498. /**
  499. * @brief Returns the last data output value of the selected DAC channel.
  500. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  501. * the configuration information for the specified DAC.
  502. * @param Channel: The selected DAC channel.
  503. * This parameter can be one of the following values:
  504. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  505. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  506. * @retval The selected DAC channel data output value.
  507. */
  508. uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  509. {
  510. /* Check the parameters */
  511. assert_param(IS_DAC_CHANNEL(Channel));
  512. /* Returns the DAC channel data output register value */
  513. if(Channel == DAC_CHANNEL_1)
  514. {
  515. return hdac->Instance->DOR1;
  516. }
  517. else
  518. {
  519. return hdac->Instance->DOR2;
  520. }
  521. }
  522. /**
  523. * @brief Handles DAC interrupt request
  524. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  525. * the configuration information for the specified DAC.
  526. * @retval None
  527. */
  528. void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
  529. {
  530. /* Check underrun channel 1 flag */
  531. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
  532. {
  533. /* Change DAC state to error state */
  534. hdac->State = HAL_DAC_STATE_ERROR;
  535. /* Set DAC error code to channel1 DMA underrun error */
  536. hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
  537. /* Clear the underrun flag */
  538. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
  539. /* Disable the selected DAC channel1 DMA request */
  540. hdac->Instance->CR &= ~DAC_CR_DMAEN1;
  541. /* Error callback */
  542. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  543. }
  544. /* Check underrun channel 2 flag */
  545. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
  546. {
  547. /* Change DAC state to error state */
  548. hdac->State = HAL_DAC_STATE_ERROR;
  549. /* Set DAC error code to channel2 DMA underrun error */
  550. hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
  551. /* Clear the underrun flag */
  552. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
  553. /* Disable the selected DAC channel1 DMA request */
  554. hdac->Instance->CR &= ~DAC_CR_DMAEN2;
  555. /* Error callback */
  556. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  557. }
  558. }
  559. /**
  560. * @brief Conversion complete callback in non blocking mode for Channel1
  561. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  562. * the configuration information for the specified DAC.
  563. * @retval None
  564. */
  565. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  566. {
  567. /* NOTE : This function Should not be modified, when the callback is needed,
  568. the HAL_DAC_ConvCpltCallback could be implemented in the user file
  569. */
  570. }
  571. /**
  572. * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
  573. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  574. * the configuration information for the specified DAC.
  575. * @retval None
  576. */
  577. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  578. {
  579. /* NOTE : This function Should not be modified, when the callback is needed,
  580. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  581. */
  582. }
  583. /**
  584. * @brief Error DAC callback for Channel1.
  585. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  586. * the configuration information for the specified DAC.
  587. * @retval None
  588. */
  589. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  590. {
  591. /* NOTE : This function Should not be modified, when the callback is needed,
  592. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  593. */
  594. }
  595. /**
  596. * @brief DMA underrun DAC callback for channel1.
  597. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  598. * the configuration information for the specified DAC.
  599. * @retval None
  600. */
  601. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  602. {
  603. /* NOTE : This function Should not be modified, when the callback is needed,
  604. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  605. */
  606. }
  607. /**
  608. * @}
  609. */
  610. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  611. * @brief Peripheral Control functions
  612. *
  613. @verbatim
  614. ==============================================================================
  615. ##### Peripheral Control functions #####
  616. ==============================================================================
  617. [..] This section provides functions allowing to:
  618. (+) Configure channels.
  619. (+) Set the specified data holding register value for DAC channel.
  620. @endverbatim
  621. * @{
  622. */
  623. /**
  624. * @brief Configures the selected DAC channel.
  625. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  626. * the configuration information for the specified DAC.
  627. * @param sConfig: DAC configuration structure.
  628. * @param Channel: The selected DAC channel.
  629. * This parameter can be one of the following values:
  630. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  631. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  632. * @retval HAL status
  633. */
  634. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  635. {
  636. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  637. /* Check the DAC parameters */
  638. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  639. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  640. assert_param(IS_DAC_CHANNEL(Channel));
  641. /* Process locked */
  642. __HAL_LOCK(hdac);
  643. /* Change DAC state */
  644. hdac->State = HAL_DAC_STATE_BUSY;
  645. /* Get the DAC CR value */
  646. tmpreg1 = hdac->Instance->CR;
  647. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  648. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
  649. /* Configure for the selected DAC channel: buffer output, trigger */
  650. /* Set TSELx and TENx bits according to DAC_Trigger value */
  651. /* Set BOFFx bit according to DAC_OutputBuffer value */
  652. tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
  653. /* Calculate CR register value depending on DAC_Channel */
  654. tmpreg1 |= tmpreg2 << Channel;
  655. /* Write to DAC CR */
  656. hdac->Instance->CR = tmpreg1;
  657. /* Disable wave generation */
  658. hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
  659. /* Change DAC state */
  660. hdac->State = HAL_DAC_STATE_READY;
  661. /* Process unlocked */
  662. __HAL_UNLOCK(hdac);
  663. /* Return function status */
  664. return HAL_OK;
  665. }
  666. /**
  667. * @brief Set the specified data holding register value for DAC channel.
  668. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  669. * the configuration information for the specified DAC.
  670. * @param Channel: The selected DAC channel.
  671. * This parameter can be one of the following values:
  672. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  673. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  674. * @param Alignment: Specifies the data alignment.
  675. * This parameter can be one of the following values:
  676. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  677. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  678. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  679. * @param Data: Data to be loaded in the selected data holding register.
  680. * @retval HAL status
  681. */
  682. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  683. {
  684. __IO uint32_t tmp = 0;
  685. /* Check the parameters */
  686. assert_param(IS_DAC_CHANNEL(Channel));
  687. assert_param(IS_DAC_ALIGN(Alignment));
  688. assert_param(IS_DAC_DATA(Data));
  689. tmp = (uint32_t)hdac->Instance;
  690. if(Channel == DAC_CHANNEL_1)
  691. {
  692. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  693. }
  694. else
  695. {
  696. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  697. }
  698. /* Set the DAC channel1 selected data holding register */
  699. *(__IO uint32_t *) tmp = Data;
  700. /* Return function status */
  701. return HAL_OK;
  702. }
  703. /**
  704. * @}
  705. */
  706. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  707. * @brief Peripheral State and Errors functions
  708. *
  709. @verbatim
  710. ==============================================================================
  711. ##### Peripheral State and Errors functions #####
  712. ==============================================================================
  713. [..]
  714. This subsection provides functions allowing to
  715. (+) Check the DAC state.
  716. (+) Check the DAC Errors.
  717. @endverbatim
  718. * @{
  719. */
  720. /**
  721. * @brief return the DAC state
  722. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  723. * the configuration information for the specified DAC.
  724. * @retval HAL state
  725. */
  726. HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
  727. {
  728. /* Return DAC state */
  729. return hdac->State;
  730. }
  731. /**
  732. * @brief Return the DAC error code
  733. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  734. * the configuration information for the specified DAC.
  735. * @retval DAC Error Code
  736. */
  737. uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
  738. {
  739. return hdac->ErrorCode;
  740. }
  741. /**
  742. * @}
  743. */
  744. /**
  745. * @brief DMA conversion complete callback.
  746. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  747. * the configuration information for the specified DMA module.
  748. * @retval None
  749. */
  750. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  751. {
  752. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  753. HAL_DAC_ConvCpltCallbackCh1(hdac);
  754. hdac->State= HAL_DAC_STATE_READY;
  755. }
  756. /**
  757. * @brief DMA half transfer complete callback.
  758. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  759. * the configuration information for the specified DMA module.
  760. * @retval None
  761. */
  762. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  763. {
  764. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  765. /* Conversion complete callback */
  766. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  767. }
  768. /**
  769. * @brief DMA error callback
  770. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  771. * the configuration information for the specified DMA module.
  772. * @retval None
  773. */
  774. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  775. {
  776. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  777. /* Set DAC error code to DMA error */
  778. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  779. HAL_DAC_ErrorCallbackCh1(hdac);
  780. hdac->State= HAL_DAC_STATE_READY;
  781. }
  782. /**
  783. * @}
  784. */
  785. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  786. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  787. STM32F446xx
  788. */
  789. #endif /* HAL_DAC_MODULE_ENABLED */
  790. /**
  791. * @}
  792. */
  793. /**
  794. * @}
  795. */
  796. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/