stm32f4xx_hal_dac.c 44 KB

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