stm32l1xx_hal_dac.c 33 KB

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