stm32f1xx_hal_dac.c 46 KB

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