stm32f7xx_hal_dac.c 31 KB

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