stm32l1xx_hal_dac.c 32 KB

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