stm32f4xx_hal_dac.c 45 KB

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