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.

stm32f1xx_hal_dac.c 46KB

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