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

stm32l1xx_hal_dac.c 45KB

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