You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1384 lines
46 KiB

12 months ago
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### DAC Peripheral features #####
  28. ==============================================================================
  29. [..]
  30. *** DAC Channels ***
  31. ====================
  32. [..]
  33. STM32F4 devices integrate two 12-bit Digital Analog Converters
  34. The 2 converters (i.e. channel1 & channel2)
  35. can be used independently or simultaneously (dual mode):
  36. (#) DAC channel1 with DAC_OUT1 (PA4) as output
  37. (#) DAC channel2 with DAC_OUT2 (PA5) as output
  38. *** DAC Triggers ***
  39. ====================
  40. [..]
  41. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  42. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  43. [..]
  44. Digital to Analog conversion can be triggered by:
  45. (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
  46. The used pin (GPIOx_PIN_9) must be configured in input mode.
  47. (#) Timers TRGO: TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8
  48. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
  49. (#) Software using DAC_TRIGGER_SOFTWARE
  50. *** DAC Buffer mode feature ***
  51. ===============================
  52. [..]
  53. Each DAC channel integrates an output buffer that can be used to
  54. reduce the output impedance, and to drive external loads directly
  55. without having to add an external operational amplifier.
  56. To enable, the output buffer use
  57. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  58. [..]
  59. (@) Refer to the device datasheet for more details about output
  60. impedance value with and without output buffer.
  61. *** DAC wave generation feature ***
  62. ===================================
  63. [..]
  64. Both DAC channels can be used to generate
  65. (#) Noise wave
  66. (#) Triangle wave
  67. *** DAC data format ***
  68. =======================
  69. [..]
  70. The DAC data format can be:
  71. (#) 8-bit right alignment using DAC_ALIGN_8B_R
  72. (#) 12-bit left alignment using DAC_ALIGN_12B_L
  73. (#) 12-bit right alignment using DAC_ALIGN_12B_R
  74. *** DAC data value to voltage correspondence ***
  75. ================================================
  76. [..]
  77. The analog output voltage on each DAC channel pin is determined
  78. by the following equation:
  79. [..]
  80. DAC_OUTx = VREF+ * DOR / 4095
  81. (+) with DOR is the Data Output Register
  82. [..]
  83. VREF+ is the input voltage reference (refer to the device datasheet)
  84. [..]
  85. e.g. To set DAC_OUT1 to 0.7V, use
  86. (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  87. *** DMA requests ***
  88. =====================
  89. [..]
  90. A DMA request can be generated when an external trigger (but not a software trigger)
  91. occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
  92. DMA1 requests are mapped as following:
  93. (#) DAC channel1 mapped on DMA1 Stream5 channel7 which must be
  94. already configured
  95. (#) DAC channel2 mapped on DMA1 Stream6 channel7 which must be
  96. already configured
  97. [..]
  98. (@) For Dual mode and specific signal (Triangle and noise) generation please
  99. refer to Extended Features Driver description
  100. ##### How to use this driver #####
  101. ==============================================================================
  102. [..]
  103. (+) DAC APB clock must be enabled to get write access to DAC
  104. registers using HAL_DAC_Init()
  105. (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
  106. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  107. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA() functions.
  108. *** Polling mode IO operation ***
  109. =================================
  110. [..]
  111. (+) Start the DAC peripheral using HAL_DAC_Start()
  112. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  113. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  114. *** DMA mode IO operation ***
  115. ==============================
  116. [..]
  117. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  118. of data to be transferred at each end of conversion
  119. First issued trigger will start the conversion of the value previously set by HAL_DAC_SetValue().
  120. (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  121. function is executed and user can add his own code by customization of function pointer
  122. HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  123. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  124. function is executed and user can add his own code by customization of function pointer
  125. HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  126. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
  127. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  128. (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
  129. HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2()
  130. function is executed and user can add his own code by customization of function pointer
  131. HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2() and
  132. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1()
  133. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  134. *** Callback registration ***
  135. =============================================
  136. [..]
  137. The compilation define USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
  138. allows the user to configure dynamically the driver callbacks.
  139. Use Functions HAL_DAC_RegisterCallback() to register a user callback,
  140. it allows to register following callbacks:
  141. (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
  142. (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
  143. (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
  144. (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
  145. (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
  146. (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
  147. (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
  148. (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
  149. (+) MspInitCallback : DAC MspInit.
  150. (+) MspDeInitCallback : DAC MspdeInit.
  151. This function takes as parameters the HAL peripheral handle, the Callback ID
  152. and a pointer to the user callback function.
  153. Use function HAL_DAC_UnRegisterCallback() to reset a callback to the default
  154. weak (overridden) function. It allows to reset following callbacks:
  155. (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
  156. (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
  157. (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
  158. (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
  159. (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
  160. (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
  161. (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
  162. (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
  163. (+) MspInitCallback : DAC MspInit.
  164. (+) MspDeInitCallback : DAC MspdeInit.
  165. (+) All Callbacks
  166. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  167. By default, after the HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
  168. all callbacks are reset to the corresponding legacy weak (overridden) functions.
  169. Exception done for MspInit and MspDeInit callbacks that are respectively
  170. reset to the legacy weak (overridden) functions in the HAL_DAC_Init
  171. and HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
  172. If not, MspInit or MspDeInit are not null, the HAL_DAC_Init and HAL_DAC_DeInit
  173. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  174. Callbacks can be registered/unregistered in READY state only.
  175. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  176. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  177. during the Init/DeInit.
  178. In that case first register the MspInit/MspDeInit user callbacks
  179. using HAL_DAC_RegisterCallback before calling HAL_DAC_DeInit
  180. or HAL_DAC_Init function.
  181. When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
  182. not defined, the callback registering feature is not available
  183. and weak (overridden) callbacks are used.
  184. *** DAC HAL driver macros list ***
  185. =============================================
  186. [..]
  187. Below the list of most used macros in DAC HAL driver.
  188. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
  189. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
  190. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
  191. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
  192. [..]
  193. (@) You can refer to the DAC HAL driver header file for more useful macros
  194. @endverbatim
  195. ******************************************************************************
  196. */
  197. /* Includes ------------------------------------------------------------------*/
  198. #include "stm32f4xx_hal.h"
  199. /** @addtogroup STM32F4xx_HAL_Driver
  200. * @{
  201. */
  202. #ifdef HAL_DAC_MODULE_ENABLED
  203. #if defined(DAC)
  204. /** @defgroup DAC DAC
  205. * @brief DAC driver modules
  206. * @{
  207. */
  208. /* Private typedef -----------------------------------------------------------*/
  209. /* Private define ------------------------------------------------------------*/
  210. /* Private constants ---------------------------------------------------------*/
  211. /* Private macro -------------------------------------------------------------*/
  212. /* Private variables ---------------------------------------------------------*/
  213. /* Private function prototypes -----------------------------------------------*/
  214. /* Exported functions -------------------------------------------------------*/
  215. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  216. * @{
  217. */
  218. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  219. * @brief Initialization and Configuration functions
  220. *
  221. @verbatim
  222. ==============================================================================
  223. ##### Initialization and de-initialization functions #####
  224. ==============================================================================
  225. [..] This section provides functions allowing to:
  226. (+) Initialize and configure the DAC.
  227. (+) De-initialize the DAC.
  228. @endverbatim
  229. * @{
  230. */
  231. /**
  232. * @brief Initialize the DAC peripheral according to the specified parameters
  233. * in the DAC_InitStruct and initialize the associated handle.
  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_Init(DAC_HandleTypeDef *hdac)
  239. {
  240. /* Check the DAC peripheral 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. if (hdac->State == HAL_DAC_STATE_RESET)
  248. {
  249. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  250. /* Init the DAC Callback settings */
  251. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  252. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  253. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  254. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  255. #if defined(DAC_CHANNEL2_SUPPORT)
  256. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  257. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  258. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  259. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  260. #endif /* DAC_CHANNEL2_SUPPORT */
  261. if (hdac->MspInitCallback == NULL)
  262. {
  263. hdac->MspInitCallback = HAL_DAC_MspInit;
  264. }
  265. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  266. /* Allocate lock resource and initialize it */
  267. hdac->Lock = HAL_UNLOCKED;
  268. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  269. /* Init the low level hardware */
  270. hdac->MspInitCallback(hdac);
  271. #else
  272. /* Init the low level hardware */
  273. HAL_DAC_MspInit(hdac);
  274. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  275. }
  276. /* Initialize the DAC state*/
  277. hdac->State = HAL_DAC_STATE_BUSY;
  278. /* Set DAC error code to none */
  279. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  280. /* Initialize the DAC state*/
  281. hdac->State = HAL_DAC_STATE_READY;
  282. /* Return function status */
  283. return HAL_OK;
  284. }
  285. /**
  286. * @brief Deinitialize the DAC peripheral registers to their default reset values.
  287. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  288. * the configuration information for the specified DAC.
  289. * @retval HAL status
  290. */
  291. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef *hdac)
  292. {
  293. /* Check the DAC peripheral handle */
  294. if (hdac == NULL)
  295. {
  296. return HAL_ERROR;
  297. }
  298. /* Check the parameters */
  299. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  300. /* Change DAC state */
  301. hdac->State = HAL_DAC_STATE_BUSY;
  302. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  303. if (hdac->MspDeInitCallback == NULL)
  304. {
  305. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  306. }
  307. /* DeInit the low level hardware */
  308. hdac->MspDeInitCallback(hdac);
  309. #else
  310. /* DeInit the low level hardware */
  311. HAL_DAC_MspDeInit(hdac);
  312. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  313. /* Set DAC error code to none */
  314. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  315. /* Change DAC state */
  316. hdac->State = HAL_DAC_STATE_RESET;
  317. /* Release Lock */
  318. __HAL_UNLOCK(hdac);
  319. /* Return function status */
  320. return HAL_OK;
  321. }
  322. /**
  323. * @brief Initialize the DAC MSP.
  324. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  325. * the configuration information for the specified DAC.
  326. * @retval None
  327. */
  328. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
  329. {
  330. /* Prevent unused argument(s) compilation warning */
  331. UNUSED(hdac);
  332. /* NOTE : This function should not be modified, when the callback is needed,
  333. the HAL_DAC_MspInit could be implemented in the user file
  334. */
  335. }
  336. /**
  337. * @brief DeInitialize the DAC MSP.
  338. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  339. * the configuration information for the specified DAC.
  340. * @retval None
  341. */
  342. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
  343. {
  344. /* Prevent unused argument(s) compilation warning */
  345. UNUSED(hdac);
  346. /* NOTE : This function should not be modified, when the callback is needed,
  347. the HAL_DAC_MspDeInit could be implemented in the user file
  348. */
  349. }
  350. /**
  351. * @}
  352. */
  353. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  354. * @brief IO operation functions
  355. *
  356. @verbatim
  357. ==============================================================================
  358. ##### IO operation functions #####
  359. ==============================================================================
  360. [..] This section provides functions allowing to:
  361. (+) Start conversion.
  362. (+) Stop conversion.
  363. (+) Start conversion and enable DMA transfer.
  364. (+) Stop conversion and disable DMA transfer.
  365. (+) Get result of conversion.
  366. @endverbatim
  367. * @{
  368. */
  369. /**
  370. * @brief Enables DAC and starts conversion of channel.
  371. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  372. * the configuration information for the specified DAC.
  373. * @param Channel The selected DAC channel.
  374. * This parameter can be one of the following values:
  375. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  376. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  377. * @retval HAL status
  378. */
  379. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
  380. {
  381. /* Check the DAC peripheral handle */
  382. if (hdac == NULL)
  383. {
  384. return HAL_ERROR;
  385. }
  386. /* Check the parameters */
  387. assert_param(IS_DAC_CHANNEL(Channel));
  388. /* Process locked */
  389. __HAL_LOCK(hdac);
  390. /* Change DAC state */
  391. hdac->State = HAL_DAC_STATE_BUSY;
  392. /* Enable the Peripheral */
  393. __HAL_DAC_ENABLE(hdac, Channel);
  394. if (Channel == DAC_CHANNEL_1)
  395. {
  396. /* Check if software trigger enabled */
  397. if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
  398. {
  399. /* Enable the selected DAC software conversion */
  400. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
  401. }
  402. }
  403. #if defined(DAC_CHANNEL2_SUPPORT)
  404. else
  405. {
  406. /* Check if software trigger enabled */
  407. if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (Channel & 0x10UL)))
  408. {
  409. /* Enable the selected DAC software conversion*/
  410. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
  411. }
  412. }
  413. #endif /* DAC_CHANNEL2_SUPPORT */
  414. /* Change DAC state */
  415. hdac->State = HAL_DAC_STATE_READY;
  416. /* Process unlocked */
  417. __HAL_UNLOCK(hdac);
  418. /* Return function status */
  419. return HAL_OK;
  420. }
  421. /**
  422. * @brief Disables DAC and stop conversion of channel.
  423. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  424. * the configuration information for the specified DAC.
  425. * @param Channel The selected DAC channel.
  426. * This parameter can be one of the following values:
  427. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  428. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  429. * @retval HAL status
  430. */
  431. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
  432. {
  433. /* Check the DAC peripheral handle */
  434. if (hdac == NULL)
  435. {
  436. return HAL_ERROR;
  437. }
  438. /* Check the parameters */
  439. assert_param(IS_DAC_CHANNEL(Channel));
  440. /* Disable the Peripheral */
  441. __HAL_DAC_DISABLE(hdac, Channel);
  442. /* Change DAC state */
  443. hdac->State = HAL_DAC_STATE_READY;
  444. /* Return function status */
  445. return HAL_OK;
  446. }
  447. /**
  448. * @brief Enables DAC and starts conversion of channel.
  449. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  450. * the configuration information for the specified DAC.
  451. * @param Channel The selected DAC channel.
  452. * This parameter can be one of the following values:
  453. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  454. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  455. * @param pData The source Buffer address.
  456. * @param Length The length of data to be transferred from memory to DAC peripheral
  457. * @param Alignment Specifies the data alignment for DAC channel.
  458. * This parameter can be one of the following values:
  459. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  460. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  461. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  462. * @retval HAL status
  463. */
  464. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, const uint32_t *pData, uint32_t Length,
  465. uint32_t Alignment)
  466. {
  467. HAL_StatusTypeDef status = HAL_ERROR;
  468. uint32_t tmpreg;
  469. /* Check the DAC peripheral handle */
  470. if (hdac == NULL)
  471. {
  472. return HAL_ERROR;
  473. }
  474. /* Check the parameters */
  475. assert_param(IS_DAC_CHANNEL(Channel));
  476. assert_param(IS_DAC_ALIGN(Alignment));
  477. /* Process locked */
  478. __HAL_LOCK(hdac);
  479. /* Change DAC state */
  480. hdac->State = HAL_DAC_STATE_BUSY;
  481. if (Channel == DAC_CHANNEL_1)
  482. {
  483. /* Set the DMA transfer complete callback for channel1 */
  484. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  485. /* Set the DMA half transfer complete callback for channel1 */
  486. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  487. /* Set the DMA error callback for channel1 */
  488. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  489. /* Enable the selected DAC channel1 DMA request */
  490. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  491. /* Case of use of channel 1 */
  492. switch (Alignment)
  493. {
  494. case DAC_ALIGN_12B_R:
  495. /* Get DHR12R1 address */
  496. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  497. break;
  498. case DAC_ALIGN_12B_L:
  499. /* Get DHR12L1 address */
  500. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  501. break;
  502. default: /* case DAC_ALIGN_8B_R */
  503. /* Get DHR8R1 address */
  504. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  505. break;
  506. }
  507. }
  508. #if defined(DAC_CHANNEL2_SUPPORT)
  509. else
  510. {
  511. /* Set the DMA transfer complete callback for channel2 */
  512. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  513. /* Set the DMA half transfer complete callback for channel2 */
  514. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  515. /* Set the DMA error callback for channel2 */
  516. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  517. /* Enable the selected DAC channel2 DMA request */
  518. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  519. /* Case of use of channel 2 */
  520. switch (Alignment)
  521. {
  522. case DAC_ALIGN_12B_R:
  523. /* Get DHR12R2 address */
  524. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  525. break;
  526. case DAC_ALIGN_12B_L:
  527. /* Get DHR12L2 address */
  528. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  529. break;
  530. default: /* case DAC_ALIGN_8B_R */
  531. /* Get DHR8R2 address */
  532. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  533. break;
  534. }
  535. }
  536. #endif /* DAC_CHANNEL2_SUPPORT */
  537. if (Channel == DAC_CHANNEL_1)
  538. {
  539. /* Enable the DAC DMA underrun interrupt */
  540. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  541. /* Enable the DMA Stream */
  542. status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  543. }
  544. #if defined(DAC_CHANNEL2_SUPPORT)
  545. else
  546. {
  547. /* Enable the DAC DMA underrun interrupt */
  548. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  549. /* Enable the DMA Stream */
  550. status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  551. }
  552. #endif /* DAC_CHANNEL2_SUPPORT */
  553. /* Process Unlocked */
  554. __HAL_UNLOCK(hdac);
  555. if (status == HAL_OK)
  556. {
  557. /* Enable the Peripheral */
  558. __HAL_DAC_ENABLE(hdac, Channel);
  559. }
  560. else
  561. {
  562. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  563. }
  564. /* Return function status */
  565. return status;
  566. }
  567. /**
  568. * @brief Disables DAC and stop conversion of channel.
  569. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  570. * the configuration information for the specified DAC.
  571. * @param Channel The selected DAC channel.
  572. * This parameter can be one of the following values:
  573. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  574. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  575. * @retval HAL status
  576. */
  577. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
  578. {
  579. /* Check the DAC peripheral handle */
  580. if (hdac == NULL)
  581. {
  582. return HAL_ERROR;
  583. }
  584. /* Check the parameters */
  585. assert_param(IS_DAC_CHANNEL(Channel));
  586. /* Disable the selected DAC channel DMA request */
  587. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << (Channel & 0x10UL));
  588. /* Disable the Peripheral */
  589. __HAL_DAC_DISABLE(hdac, Channel);
  590. /* Disable the DMA Stream */
  591. /* Channel1 is used */
  592. if (Channel == DAC_CHANNEL_1)
  593. {
  594. /* Disable the DMA Stream */
  595. (void)HAL_DMA_Abort(hdac->DMA_Handle1);
  596. /* Disable the DAC DMA underrun interrupt */
  597. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
  598. }
  599. #if defined(DAC_CHANNEL2_SUPPORT)
  600. else /* Channel2 is used for */
  601. {
  602. /* Disable the DMA Stream */
  603. (void)HAL_DMA_Abort(hdac->DMA_Handle2);
  604. /* Disable the DAC DMA underrun interrupt */
  605. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
  606. }
  607. #endif /* DAC_CHANNEL2_SUPPORT */
  608. /* Change DAC state */
  609. hdac->State = HAL_DAC_STATE_READY;
  610. /* Return function status */
  611. return HAL_OK;
  612. }
  613. /**
  614. * @brief Handles DAC interrupt request
  615. * This function uses the interruption of DMA
  616. * underrun.
  617. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  618. * the configuration information for the specified DAC.
  619. * @retval None
  620. */
  621. void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
  622. {
  623. uint32_t itsource = hdac->Instance->CR;
  624. uint32_t itflag = hdac->Instance->SR;
  625. if ((itsource & DAC_IT_DMAUDR1) == DAC_IT_DMAUDR1)
  626. {
  627. /* Check underrun flag of DAC channel 1 */
  628. if ((itflag & DAC_FLAG_DMAUDR1) == DAC_FLAG_DMAUDR1)
  629. {
  630. /* Change DAC state to error state */
  631. hdac->State = HAL_DAC_STATE_ERROR;
  632. /* Set DAC error code to channel1 DMA underrun error */
  633. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
  634. /* Clear the underrun flag */
  635. __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR1);
  636. /* Disable the selected DAC channel1 DMA request */
  637. __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN1);
  638. /* Error callback */
  639. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  640. hdac->DMAUnderrunCallbackCh1(hdac);
  641. #else
  642. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  643. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  644. }
  645. }
  646. #if defined(DAC_CHANNEL2_SUPPORT)
  647. if ((itsource & DAC_IT_DMAUDR2) == DAC_IT_DMAUDR2)
  648. {
  649. /* Check underrun flag of DAC channel 2 */
  650. if ((itflag & DAC_FLAG_DMAUDR2) == DAC_FLAG_DMAUDR2)
  651. {
  652. /* Change DAC state to error state */
  653. hdac->State = HAL_DAC_STATE_ERROR;
  654. /* Set DAC error code to channel2 DMA underrun error */
  655. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
  656. /* Clear the underrun flag */
  657. __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR2);
  658. /* Disable the selected DAC channel2 DMA request */
  659. __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN2);
  660. /* Error callback */
  661. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  662. hdac->DMAUnderrunCallbackCh2(hdac);
  663. #else
  664. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  665. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  666. }
  667. }
  668. #endif /* DAC_CHANNEL2_SUPPORT */
  669. }
  670. /**
  671. * @brief Set the specified data holding register value for DAC channel.
  672. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  673. * the configuration information for the specified DAC.
  674. * @param Channel The selected DAC channel.
  675. * This parameter can be one of the following values:
  676. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  677. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  678. * @param Alignment Specifies the data alignment.
  679. * This parameter can be one of the following values:
  680. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  681. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  682. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  683. * @param Data Data to be loaded in the selected data holding register.
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  687. {
  688. __IO uint32_t tmp = 0UL;
  689. /* Check the DAC peripheral handle */
  690. if (hdac == NULL)
  691. {
  692. return HAL_ERROR;
  693. }
  694. /* Check the parameters */
  695. assert_param(IS_DAC_CHANNEL(Channel));
  696. assert_param(IS_DAC_ALIGN(Alignment));
  697. assert_param(IS_DAC_DATA(Data));
  698. tmp = (uint32_t)hdac->Instance;
  699. if (Channel == DAC_CHANNEL_1)
  700. {
  701. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  702. }
  703. #if defined(DAC_CHANNEL2_SUPPORT)
  704. else
  705. {
  706. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  707. }
  708. #endif /* DAC_CHANNEL2_SUPPORT */
  709. /* Set the DAC channel selected data holding register */
  710. *(__IO uint32_t *) tmp = Data;
  711. /* Return function status */
  712. return HAL_OK;
  713. }
  714. /**
  715. * @brief Conversion complete callback in non-blocking mode for Channel1
  716. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  717. * the configuration information for the specified DAC.
  718. * @retval None
  719. */
  720. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
  721. {
  722. /* Prevent unused argument(s) compilation warning */
  723. UNUSED(hdac);
  724. /* NOTE : This function should not be modified, when the callback is needed,
  725. the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
  726. */
  727. }
  728. /**
  729. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel1
  730. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  731. * the configuration information for the specified DAC.
  732. * @retval None
  733. */
  734. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
  735. {
  736. /* Prevent unused argument(s) compilation warning */
  737. UNUSED(hdac);
  738. /* NOTE : This function should not be modified, when the callback is needed,
  739. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  740. */
  741. }
  742. /**
  743. * @brief Error DAC callback for Channel1.
  744. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  745. * the configuration information for the specified DAC.
  746. * @retval None
  747. */
  748. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  749. {
  750. /* Prevent unused argument(s) compilation warning */
  751. UNUSED(hdac);
  752. /* NOTE : This function should not be modified, when the callback is needed,
  753. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  754. */
  755. }
  756. /**
  757. * @brief DMA underrun DAC callback for channel1.
  758. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  759. * the configuration information for the specified DAC.
  760. * @retval None
  761. */
  762. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  763. {
  764. /* Prevent unused argument(s) compilation warning */
  765. UNUSED(hdac);
  766. /* NOTE : This function should not be modified, when the callback is needed,
  767. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  768. */
  769. }
  770. /**
  771. * @}
  772. */
  773. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  774. * @brief Peripheral Control functions
  775. *
  776. @verbatim
  777. ==============================================================================
  778. ##### Peripheral Control functions #####
  779. ==============================================================================
  780. [..] This section provides functions allowing to:
  781. (+) Configure channels.
  782. (+) Set the specified data holding register value for DAC channel.
  783. @endverbatim
  784. * @{
  785. */
  786. /**
  787. * @brief Returns the last data output value of the selected DAC channel.
  788. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  789. * the configuration information for the specified DAC.
  790. * @param Channel The selected DAC channel.
  791. * This parameter can be one of the following values:
  792. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  793. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  794. * @retval The selected DAC channel data output value.
  795. */
  796. uint32_t HAL_DAC_GetValue(const DAC_HandleTypeDef *hdac, uint32_t Channel)
  797. {
  798. uint32_t result = 0;
  799. /* Check the DAC peripheral handle */
  800. assert_param(hdac != NULL);
  801. /* Check the parameters */
  802. assert_param(IS_DAC_CHANNEL(Channel));
  803. if (Channel == DAC_CHANNEL_1)
  804. {
  805. result = hdac->Instance->DOR1;
  806. }
  807. #if defined(DAC_CHANNEL2_SUPPORT)
  808. else
  809. {
  810. result = hdac->Instance->DOR2;
  811. }
  812. #endif /* DAC_CHANNEL2_SUPPORT */
  813. /* Returns the DAC channel data output register value */
  814. return result;
  815. }
  816. /**
  817. * @brief Configures the selected DAC channel.
  818. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  819. * the configuration information for the specified DAC.
  820. * @param sConfig DAC configuration structure.
  821. * @param Channel The selected DAC channel.
  822. * This parameter can be one of the following values:
  823. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  824. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  825. * @retval HAL status
  826. */
  827. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac,
  828. const DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
  829. {
  830. HAL_StatusTypeDef status = HAL_OK;
  831. uint32_t tmpreg1;
  832. uint32_t tmpreg2;
  833. /* Check the DAC peripheral handle and channel configuration struct */
  834. if ((hdac == NULL) || (sConfig == NULL))
  835. {
  836. return HAL_ERROR;
  837. }
  838. /* Check the DAC parameters */
  839. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  840. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  841. assert_param(IS_DAC_CHANNEL(Channel));
  842. /* Process locked */
  843. __HAL_LOCK(hdac);
  844. /* Change DAC state */
  845. hdac->State = HAL_DAC_STATE_BUSY;
  846. /* Get the DAC CR value */
  847. tmpreg1 = hdac->Instance->CR;
  848. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  849. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1))
  850. << (Channel & 0x10UL));
  851. /* Configure for the selected DAC channel: buffer output, trigger */
  852. /* Set TSELx and TENx bits according to DAC_Trigger value */
  853. /* Set BOFFx bit according to DAC_OutputBuffer value */
  854. tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
  855. /* Calculate CR register value depending on DAC_Channel */
  856. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  857. /* Write to DAC CR */
  858. hdac->Instance->CR = tmpreg1;
  859. /* Disable wave generation */
  860. CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << (Channel & 0x10UL)));
  861. /* Change DAC state */
  862. hdac->State = HAL_DAC_STATE_READY;
  863. /* Process unlocked */
  864. __HAL_UNLOCK(hdac);
  865. /* Return function status */
  866. return status;
  867. }
  868. /**
  869. * @}
  870. */
  871. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  872. * @brief Peripheral State and Errors functions
  873. *
  874. @verbatim
  875. ==============================================================================
  876. ##### Peripheral State and Errors functions #####
  877. ==============================================================================
  878. [..]
  879. This subsection provides functions allowing to
  880. (+) Check the DAC state.
  881. (+) Check the DAC Errors.
  882. @endverbatim
  883. * @{
  884. */
  885. /**
  886. * @brief return the DAC handle state
  887. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  888. * the configuration information for the specified DAC.
  889. * @retval HAL state
  890. */
  891. HAL_DAC_StateTypeDef HAL_DAC_GetState(const DAC_HandleTypeDef *hdac)
  892. {
  893. /* Return DAC handle state */
  894. return hdac->State;
  895. }
  896. /**
  897. * @brief Return the DAC error code
  898. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  899. * the configuration information for the specified DAC.
  900. * @retval DAC Error Code
  901. */
  902. uint32_t HAL_DAC_GetError(const DAC_HandleTypeDef *hdac)
  903. {
  904. return hdac->ErrorCode;
  905. }
  906. /**
  907. * @}
  908. */
  909. /**
  910. * @}
  911. */
  912. /** @addtogroup DAC_Exported_Functions
  913. * @{
  914. */
  915. /** @addtogroup DAC_Exported_Functions_Group1
  916. * @{
  917. */
  918. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  919. /**
  920. * @brief Register a User DAC Callback
  921. * To be used instead of the weak (overridden) predefined callback
  922. * @note The HAL_DAC_RegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to register
  923. * callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
  924. * @param hdac DAC handle
  925. * @param CallbackID ID of the callback to be registered
  926. * This parameter can be one of the following values:
  927. * @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK DAC Error Callback ID
  928. * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 Complete Callback ID
  929. * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
  930. * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
  931. * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
  932. * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
  933. * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
  934. * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
  935. * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
  936. * @arg @ref HAL_DAC_MSPINIT_CB_ID DAC MSP Init Callback ID
  937. * @arg @ref HAL_DAC_MSPDEINIT_CB_ID DAC MSP DeInit Callback ID
  938. *
  939. * @param pCallback pointer to the Callback function
  940. * @retval status
  941. */
  942. HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
  943. pDAC_CallbackTypeDef pCallback)
  944. {
  945. HAL_StatusTypeDef status = HAL_OK;
  946. /* Check the DAC peripheral handle */
  947. if (hdac == NULL)
  948. {
  949. return HAL_ERROR;
  950. }
  951. if (pCallback == NULL)
  952. {
  953. /* Update the error code */
  954. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  955. return HAL_ERROR;
  956. }
  957. if (hdac->State == HAL_DAC_STATE_READY)
  958. {
  959. switch (CallbackID)
  960. {
  961. case HAL_DAC_CH1_COMPLETE_CB_ID :
  962. hdac->ConvCpltCallbackCh1 = pCallback;
  963. break;
  964. case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
  965. hdac->ConvHalfCpltCallbackCh1 = pCallback;
  966. break;
  967. case HAL_DAC_CH1_ERROR_ID :
  968. hdac->ErrorCallbackCh1 = pCallback;
  969. break;
  970. case HAL_DAC_CH1_UNDERRUN_CB_ID :
  971. hdac->DMAUnderrunCallbackCh1 = pCallback;
  972. break;
  973. #if defined(DAC_CHANNEL2_SUPPORT)
  974. case HAL_DAC_CH2_COMPLETE_CB_ID :
  975. hdac->ConvCpltCallbackCh2 = pCallback;
  976. break;
  977. case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
  978. hdac->ConvHalfCpltCallbackCh2 = pCallback;
  979. break;
  980. case HAL_DAC_CH2_ERROR_ID :
  981. hdac->ErrorCallbackCh2 = pCallback;
  982. break;
  983. case HAL_DAC_CH2_UNDERRUN_CB_ID :
  984. hdac->DMAUnderrunCallbackCh2 = pCallback;
  985. break;
  986. #endif /* DAC_CHANNEL2_SUPPORT */
  987. case HAL_DAC_MSPINIT_CB_ID :
  988. hdac->MspInitCallback = pCallback;
  989. break;
  990. case HAL_DAC_MSPDEINIT_CB_ID :
  991. hdac->MspDeInitCallback = pCallback;
  992. break;
  993. default :
  994. /* Update the error code */
  995. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  996. /* update return status */
  997. status = HAL_ERROR;
  998. break;
  999. }
  1000. }
  1001. else if (hdac->State == HAL_DAC_STATE_RESET)
  1002. {
  1003. switch (CallbackID)
  1004. {
  1005. case HAL_DAC_MSPINIT_CB_ID :
  1006. hdac->MspInitCallback = pCallback;
  1007. break;
  1008. case HAL_DAC_MSPDEINIT_CB_ID :
  1009. hdac->MspDeInitCallback = pCallback;
  1010. break;
  1011. default :
  1012. /* Update the error code */
  1013. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1014. /* update return status */
  1015. status = HAL_ERROR;
  1016. break;
  1017. }
  1018. }
  1019. else
  1020. {
  1021. /* Update the error code */
  1022. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1023. /* update return status */
  1024. status = HAL_ERROR;
  1025. }
  1026. return status;
  1027. }
  1028. /**
  1029. * @brief Unregister a User DAC Callback
  1030. * DAC Callback is redirected to the weak (overridden) predefined callback
  1031. * @note The HAL_DAC_UnRegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to un-register
  1032. * callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
  1033. * @param hdac DAC handle
  1034. * @param CallbackID ID of the callback to be unregistered
  1035. * This parameter can be one of the following values:
  1036. * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 transfer Complete Callback ID
  1037. * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
  1038. * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
  1039. * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
  1040. * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
  1041. * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
  1042. * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
  1043. * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
  1044. * @arg @ref HAL_DAC_MSPINIT_CB_ID DAC MSP Init Callback ID
  1045. * @arg @ref HAL_DAC_MSPDEINIT_CB_ID DAC MSP DeInit Callback ID
  1046. * @arg @ref HAL_DAC_ALL_CB_ID DAC All callbacks
  1047. * @retval status
  1048. */
  1049. HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
  1050. {
  1051. HAL_StatusTypeDef status = HAL_OK;
  1052. /* Check the DAC peripheral handle */
  1053. if (hdac == NULL)
  1054. {
  1055. return HAL_ERROR;
  1056. }
  1057. if (hdac->State == HAL_DAC_STATE_READY)
  1058. {
  1059. switch (CallbackID)
  1060. {
  1061. case HAL_DAC_CH1_COMPLETE_CB_ID :
  1062. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  1063. break;
  1064. case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
  1065. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  1066. break;
  1067. case HAL_DAC_CH1_ERROR_ID :
  1068. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  1069. break;
  1070. case HAL_DAC_CH1_UNDERRUN_CB_ID :
  1071. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  1072. break;
  1073. #if defined(DAC_CHANNEL2_SUPPORT)
  1074. case HAL_DAC_CH2_COMPLETE_CB_ID :
  1075. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  1076. break;
  1077. case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
  1078. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  1079. break;
  1080. case HAL_DAC_CH2_ERROR_ID :
  1081. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  1082. break;
  1083. case HAL_DAC_CH2_UNDERRUN_CB_ID :
  1084. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  1085. break;
  1086. #endif /* DAC_CHANNEL2_SUPPORT */
  1087. case HAL_DAC_MSPINIT_CB_ID :
  1088. hdac->MspInitCallback = HAL_DAC_MspInit;
  1089. break;
  1090. case HAL_DAC_MSPDEINIT_CB_ID :
  1091. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  1092. break;
  1093. case HAL_DAC_ALL_CB_ID :
  1094. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  1095. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  1096. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  1097. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  1098. #if defined(DAC_CHANNEL2_SUPPORT)
  1099. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  1100. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  1101. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  1102. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  1103. #endif /* DAC_CHANNEL2_SUPPORT */
  1104. hdac->MspInitCallback = HAL_DAC_MspInit;
  1105. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  1106. break;
  1107. default :
  1108. /* Update the error code */
  1109. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1110. /* update return status */
  1111. status = HAL_ERROR;
  1112. break;
  1113. }
  1114. }
  1115. else if (hdac->State == HAL_DAC_STATE_RESET)
  1116. {
  1117. switch (CallbackID)
  1118. {
  1119. case HAL_DAC_MSPINIT_CB_ID :
  1120. hdac->MspInitCallback = HAL_DAC_MspInit;
  1121. break;
  1122. case HAL_DAC_MSPDEINIT_CB_ID :
  1123. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  1124. break;
  1125. default :
  1126. /* Update the error code */
  1127. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1128. /* update return status */
  1129. status = HAL_ERROR;
  1130. break;
  1131. }
  1132. }
  1133. else
  1134. {
  1135. /* Update the error code */
  1136. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1137. /* update return status */
  1138. status = HAL_ERROR;
  1139. }
  1140. return status;
  1141. }
  1142. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1143. /**
  1144. * @}
  1145. */
  1146. /**
  1147. * @}
  1148. */
  1149. /** @addtogroup DAC_Private_Functions
  1150. * @{
  1151. */
  1152. /**
  1153. * @brief DMA conversion complete callback.
  1154. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1155. * the configuration information for the specified DMA module.
  1156. * @retval None
  1157. */
  1158. void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  1159. {
  1160. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1161. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1162. hdac->ConvCpltCallbackCh1(hdac);
  1163. #else
  1164. HAL_DAC_ConvCpltCallbackCh1(hdac);
  1165. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1166. hdac->State = HAL_DAC_STATE_READY;
  1167. }
  1168. /**
  1169. * @brief DMA half transfer complete callback.
  1170. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1171. * the configuration information for the specified DMA module.
  1172. * @retval None
  1173. */
  1174. void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  1175. {
  1176. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1177. /* Conversion complete callback */
  1178. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1179. hdac->ConvHalfCpltCallbackCh1(hdac);
  1180. #else
  1181. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  1182. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1183. }
  1184. /**
  1185. * @brief DMA error callback
  1186. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1187. * the configuration information for the specified DMA module.
  1188. * @retval None
  1189. */
  1190. void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  1191. {
  1192. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1193. /* Set DAC error code to DMA error */
  1194. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  1195. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1196. hdac->ErrorCallbackCh1(hdac);
  1197. #else
  1198. HAL_DAC_ErrorCallbackCh1(hdac);
  1199. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1200. hdac->State = HAL_DAC_STATE_READY;
  1201. }
  1202. /**
  1203. * @}
  1204. */
  1205. /**
  1206. * @}
  1207. */
  1208. #endif /* DAC */
  1209. #endif /* HAL_DAC_MODULE_ENABLED */
  1210. /**
  1211. * @}
  1212. */