stm32f1xx_hal_dac.c 47 KB

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