stm32f7xx_hal_dac.c 31 KB

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