stm32f1xx_hal_dac.c 47 KB

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