stm32g4xx_hal_dac.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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) 2019 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. STM32G4 devices integrate up to seven 12-bit Digital Analog Converters,
  34. up to six of them grouped by pair forming a DAC instance.
  35. The 2 converters of an single instance (i.e. channel1 & channel2)
  36. can be used independently or simultaneously (dual mode):
  37. (#) DAC channel1 with DAC_OUT1 as output (not for all) or connected to on-chip
  38. peripherals (ex. comparators, operational amplifier).
  39. (#) DAC channel2 with DAC_OUT2 as output (not for all) or connected to on-chip
  40. peripherals (ex. comparators, operational amplifier).
  41. Note: when an instance only includes one converter, only independent mode is
  42. supported by this converter.
  43. STM32G4 instances & converters availability and output PIO mapping (DAC_OUTx):
  44. ----------------------------------------------------------------------------
  45. | DAC1 | DAC2 | DAC3 | DAC4 |
  46. ----------------------------------------------------------------------------
  47. Channel 1 | | YES | YES | YES | YES
  48. | DAC_OUT1 | PA4 | PA6 | - | -
  49. ----------------------------------------------------------------------------
  50. Channel 2 | | YES | NO | YES | YES
  51. | DAC_OUT2 | PA5 | - | - | -
  52. ----------------------------------------------------------------------------
  53. Note: On this STM32 series, all devices do not include each DAC instances listed
  54. above. Refer to device datasheet for DACx instance availability.
  55. *** DAC Triggers ***
  56. ====================
  57. [..]
  58. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  59. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  60. [..]
  61. Digital to Analog conversion can be triggered by:
  62. (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
  63. The used pin (GPIOx_PIN_9) must be configured in input mode.
  64. (#) Timers TRGO: TIM1, TIM2, TIM3, TIM4, TIM6, TIM7, TIM8 and TIM15
  65. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T3_TRGO...)
  66. (#) Software using DAC_TRIGGER_SOFTWARE
  67. (#) HRTimer TRGO: HRTIM1 (1)
  68. (DAC_TRIGGER_HRTIM_TRG01, DAC_TRIGGER_HRTIM_TRG02...)
  69. [..]
  70. Specific triggers for sawtooth generation:
  71. (#) External event: EXTI Line 10 (any GPIOx_PIN_10) using DAC_TRIGGER_EXT_IT10.
  72. The used pin (GPIOx_PIN_10) must be configured in input mode.
  73. (#) HRTimer Step & Reset: HRTIM1 (1)
  74. (DAC_TRIGGER_HRTIM_RST_TRG1, DAC_TRIGGER_HRTIM_STEP_TRG1...)
  75. Note: On this STM32 series, parameter only available if HRTIM feature is
  76. supported (refer to device datasheet for supported features list)
  77. *** DAC Buffer mode feature ***
  78. ===============================
  79. [..]
  80. Each DAC channel integrates an output buffer that can be used to
  81. reduce the output impedance, and to drive external loads directly
  82. without having to add an external operational amplifier.
  83. To enable, the output buffer use
  84. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  85. [..]
  86. (@) Refer to the device datasheet for more details about output
  87. impedance value with and without output buffer.
  88. *** DAC connect feature ***
  89. ===============================
  90. [..]
  91. Each DAC channel can be connected internally.
  92. To connect, use
  93. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_INTERNAL;
  94. or
  95. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_BOTH; (caution: dependence to other parameters,
  96. refer to literal description).
  97. *** GPIO configurations guidelines ***
  98. =====================
  99. [..]
  100. When a DAC channel is used (ex channel1 on PA4) and the other is not
  101. (ex channel2 on PA5 is configured in Analog and disabled).
  102. Channel1 may disturb channel2 as coupling effect.
  103. Note that there is no coupling on channel2 as soon as channel2 is turned on.
  104. Coupling on adjacent channel could be avoided as follows:
  105. when unused PA5 is configured as INPUT PULL-UP or DOWN.
  106. PA5 is configured in ANALOG just before it is turned on.
  107. *** DAC Sample and Hold feature ***
  108. ========================
  109. [..]
  110. For each converter, 2 modes are supported: normal mode and
  111. "sample and hold" mode (i.e. low power mode).
  112. In the sample and hold mode, the DAC core converts data, then holds the
  113. converted voltage on a capacitor. When not converting, the DAC cores and
  114. buffer are completely turned off between samples and the DAC output is
  115. tri-stated, therefore reducing the overall power consumption. A new
  116. stabilization period is needed before each new conversion.
  117. The sample and hold allow setting internal or external voltage @
  118. low power consumption cost (output value can be at any given rate either
  119. by CPU or DMA).
  120. The Sample and hold block and registers uses either LSI & run in
  121. several power modes: run mode, sleep mode, low power run, low power sleep
  122. mode & stop1 mode.
  123. Low power stop1 mode allows only static conversion.
  124. To enable Sample and Hold mode
  125. Enable LSI using HAL_RCC_OscConfig with RCC_OSCILLATORTYPE_LSI &
  126. RCC_LSI_ON parameters.
  127. Use DAC_InitStructure.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_ENABLE;
  128. & DAC_ChannelConfTypeDef.DAC_SampleAndHoldConfig.DAC_SampleTime,
  129. DAC_HoldTime & DAC_RefreshTime;
  130. *** DAC calibration feature ***
  131. ===================================
  132. [..]
  133. (#) The 2 converters (channel1 & channel2) provide calibration capabilities.
  134. (++) Calibration aims at correcting some offset of output buffer.
  135. (++) The DAC uses either factory calibration settings OR user defined
  136. calibration (trimming) settings (i.e. trimming mode).
  137. (++) The user defined settings can be figured out using self calibration
  138. handled by HAL_DACEx_SelfCalibrate.
  139. (++) HAL_DACEx_SelfCalibrate:
  140. (+++) Runs automatically the calibration.
  141. (+++) Enables the user trimming mode
  142. (+++) Updates a structure with trimming values with fresh calibration
  143. results.
  144. The user may store the calibration results for larger
  145. (ex monitoring the trimming as a function of temperature
  146. for instance)
  147. *** DAC wave generation feature ***
  148. ===================================
  149. [..]
  150. Both DAC channels can be used to generate
  151. (#) Noise wave
  152. (#) Triangle wave
  153. (#) Sawtooth wave
  154. *** DAC data format ***
  155. =======================
  156. [..]
  157. The DAC data format can be:
  158. (#) 8-bit right alignment using DAC_ALIGN_8B_R
  159. (#) 12-bit left alignment using DAC_ALIGN_12B_L
  160. (#) 12-bit right alignment using DAC_ALIGN_12B_R
  161. *** DAC data value to voltage correspondence ***
  162. ================================================
  163. [..]
  164. The analog output voltage on each DAC channel pin is determined
  165. by the following equation:
  166. [..]
  167. DAC_OUTx = VREF+ * DOR / 4095
  168. (+) with DOR is the Data Output Register
  169. [..]
  170. VREF+ is the input voltage reference (refer to the device datasheet)
  171. [..]
  172. e.g. To set DAC_OUT1 to 0.7V, use
  173. (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  174. *** DMA requests ***
  175. =====================
  176. [..]
  177. A DMAMUX request can be generated when an external trigger (but not a software trigger)
  178. occurs if DMAMUX requests are enabled using HAL_DAC_Start_DMA().
  179. DMAMUX requests are mapped as following:
  180. ----------------------------------------------------------------------------
  181. | DAC1 | DAC2 | DAC3 | DAC4 |
  182. ----------------------------------------------------------------------------
  183. Channel 1 | | 6 | 41 | 102 | 104
  184. ----------------------------------------------------------------------------
  185. Channel 2 | | 7 | - | 103 | 105
  186. ----------------------------------------------------------------------------
  187. Note: On this STM32 series, all devices do not include each DAC instances listed
  188. above. Refer to device datasheet for DACx instance availability.
  189. *** High frequency interface mode ***
  190. =====================================
  191. [..]
  192. The high frequency interface informs DAC instance about the bus frequency in use.
  193. It is mandatory information for DAC (as internal timing of DAC is bus frequency dependent)
  194. provided thanks to parameter DAC_HighFrequency handled in HAL_DAC_ConfigChannel () function.
  195. Use of DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC value of DAC_HighFrequency is recommended
  196. function figured out the correct setting.
  197. The high frequency mode is same for all converters of a same DAC instance. Either same
  198. parameter DAC_HighFrequency is used for all DAC converters or again self
  199. DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC detection parameter.
  200. [..]
  201. (@) For Dual mode and specific signal (Sawtooth, triangle and noise) generation
  202. please refer to Extended Features Driver description
  203. ##### How to use this driver #####
  204. ==============================================================================
  205. [..]
  206. (+) DAC APB clock must be enabled to get write access to DAC
  207. registers using HAL_DAC_Init()
  208. (+) If available & needed, configure DAC_OUTx (DAC_OUT1, DAC_OUT2) in analog mode.
  209. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  210. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA() functions.
  211. *** Calibration mode IO operation ***
  212. ======================================
  213. [..]
  214. (+) Retrieve the factory trimming (calibration settings) using HAL_DACEx_GetTrimOffset()
  215. (+) Run the calibration using HAL_DACEx_SelfCalibrate()
  216. (+) Update the trimming while DAC running using HAL_DACEx_SetUserTrimming()
  217. *** Polling mode IO operation ***
  218. =================================
  219. [..]
  220. (+) Start the DAC peripheral using HAL_DAC_Start()
  221. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  222. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  223. *** DMA mode IO operation ***
  224. ==============================
  225. [..]
  226. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  227. of data to be transferred at each end of conversion
  228. First issued trigger will start the conversion of the value previously set by HAL_DAC_SetValue().
  229. (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  230. function is executed and user can add his own code by customization of function pointer
  231. HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  232. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  233. function is executed and user can add his own code by customization of function pointer
  234. HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  235. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
  236. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  237. (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
  238. HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2()
  239. function is executed and user can add his own code by customization of function pointer
  240. HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2() and
  241. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1()
  242. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  243. *** Callback registration ***
  244. =============================================
  245. [..]
  246. The compilation define USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
  247. allows the user to configure dynamically the driver callbacks.
  248. Use Functions HAL_DAC_RegisterCallback() to register a user callback,
  249. it allows to register following callbacks:
  250. (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
  251. (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
  252. (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
  253. (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
  254. (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
  255. (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
  256. (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
  257. (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
  258. (+) MspInitCallback : DAC MspInit.
  259. (+) MspDeInitCallback : DAC MspdeInit.
  260. This function takes as parameters the HAL peripheral handle, the Callback ID
  261. and a pointer to the user callback function.
  262. Use function HAL_DAC_UnRegisterCallback() to reset a callback to the default
  263. weak (overridden) function. It allows to reset following callbacks:
  264. (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
  265. (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
  266. (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
  267. (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
  268. (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
  269. (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
  270. (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
  271. (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
  272. (+) MspInitCallback : DAC MspInit.
  273. (+) MspDeInitCallback : DAC MspdeInit.
  274. (+) All Callbacks
  275. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  276. By default, after the HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
  277. all callbacks are reset to the corresponding legacy weak (overridden) functions.
  278. Exception done for MspInit and MspDeInit callbacks that are respectively
  279. reset to the legacy weak (overridden) functions in the HAL_DAC_Init
  280. and HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
  281. If not, MspInit or MspDeInit are not null, the HAL_DAC_Init and HAL_DAC_DeInit
  282. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  283. Callbacks can be registered/unregistered in READY state only.
  284. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  285. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  286. during the Init/DeInit.
  287. In that case first register the MspInit/MspDeInit user callbacks
  288. using HAL_DAC_RegisterCallback before calling HAL_DAC_DeInit
  289. or HAL_DAC_Init function.
  290. When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
  291. not defined, the callback registering feature is not available
  292. and weak (overridden) callbacks are used.
  293. *** DAC HAL driver macros list ***
  294. =============================================
  295. [..]
  296. Below the list of most used macros in DAC HAL driver.
  297. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
  298. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
  299. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
  300. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
  301. [..]
  302. (@) You can refer to the DAC HAL driver header file for more useful macros
  303. @endverbatim
  304. ******************************************************************************
  305. */
  306. /* Includes ------------------------------------------------------------------*/
  307. #include "stm32g4xx_hal.h"
  308. /** @addtogroup STM32G4xx_HAL_Driver
  309. * @{
  310. */
  311. #ifdef HAL_DAC_MODULE_ENABLED
  312. #if defined(DAC1) || defined(DAC2) || defined(DAC3) ||defined (DAC4)
  313. /** @defgroup DAC DAC
  314. * @brief DAC driver modules
  315. * @{
  316. */
  317. /* Private typedef -----------------------------------------------------------*/
  318. /* Private define ------------------------------------------------------------*/
  319. /* Private constants ---------------------------------------------------------*/
  320. /** @addtogroup DAC_Private_Constants DAC Private Constants
  321. * @{
  322. */
  323. #define TIMEOUT_DAC_CALIBCONFIG 1U /* 1 ms */
  324. #define HFSEL_ENABLE_THRESHOLD_80MHZ 80000000U /* 80 MHz */
  325. #define HFSEL_ENABLE_THRESHOLD_160MHZ 160000000U /* 160 MHz */
  326. /**
  327. * @}
  328. */
  329. /* Private macro -------------------------------------------------------------*/
  330. /* Private variables ---------------------------------------------------------*/
  331. /* Private function prototypes -----------------------------------------------*/
  332. /* Exported functions -------------------------------------------------------*/
  333. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  334. * @{
  335. */
  336. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  337. * @brief Initialization and Configuration functions
  338. *
  339. @verbatim
  340. ==============================================================================
  341. ##### Initialization and de-initialization functions #####
  342. ==============================================================================
  343. [..] This section provides functions allowing to:
  344. (+) Initialize and configure the DAC.
  345. (+) De-initialize the DAC.
  346. @endverbatim
  347. * @{
  348. */
  349. /**
  350. * @brief Initialize the DAC peripheral according to the specified parameters
  351. * in the DAC_InitStruct and initialize the associated handle.
  352. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  353. * the configuration information for the specified DAC.
  354. * @retval HAL status
  355. */
  356. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef *hdac)
  357. {
  358. /* Check the DAC peripheral handle */
  359. if (hdac == NULL)
  360. {
  361. return HAL_ERROR;
  362. }
  363. /* Check the parameters */
  364. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  365. if (hdac->State == HAL_DAC_STATE_RESET)
  366. {
  367. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  368. /* Init the DAC Callback settings */
  369. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  370. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  371. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  372. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  373. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  374. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  375. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  376. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  377. if (hdac->MspInitCallback == NULL)
  378. {
  379. hdac->MspInitCallback = HAL_DAC_MspInit;
  380. }
  381. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  382. /* Allocate lock resource and initialize it */
  383. hdac->Lock = HAL_UNLOCKED;
  384. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  385. /* Init the low level hardware */
  386. hdac->MspInitCallback(hdac);
  387. #else
  388. /* Init the low level hardware */
  389. HAL_DAC_MspInit(hdac);
  390. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  391. }
  392. /* Initialize the DAC state*/
  393. hdac->State = HAL_DAC_STATE_BUSY;
  394. /* Set DAC error code to none */
  395. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  396. /* Initialize the DAC state*/
  397. hdac->State = HAL_DAC_STATE_READY;
  398. /* Return function status */
  399. return HAL_OK;
  400. }
  401. /**
  402. * @brief Deinitialize the DAC peripheral registers to their default reset values.
  403. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  404. * the configuration information for the specified DAC.
  405. * @retval HAL status
  406. */
  407. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef *hdac)
  408. {
  409. /* Check the DAC peripheral handle */
  410. if (hdac == NULL)
  411. {
  412. return HAL_ERROR;
  413. }
  414. /* Check the parameters */
  415. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  416. /* Change DAC state */
  417. hdac->State = HAL_DAC_STATE_BUSY;
  418. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  419. if (hdac->MspDeInitCallback == NULL)
  420. {
  421. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  422. }
  423. /* DeInit the low level hardware */
  424. hdac->MspDeInitCallback(hdac);
  425. #else
  426. /* DeInit the low level hardware */
  427. HAL_DAC_MspDeInit(hdac);
  428. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  429. /* Set DAC error code to none */
  430. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  431. /* Change DAC state */
  432. hdac->State = HAL_DAC_STATE_RESET;
  433. /* Release Lock */
  434. __HAL_UNLOCK(hdac);
  435. /* Return function status */
  436. return HAL_OK;
  437. }
  438. /**
  439. * @brief Initialize the DAC MSP.
  440. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  441. * the configuration information for the specified DAC.
  442. * @retval None
  443. */
  444. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
  445. {
  446. /* Prevent unused argument(s) compilation warning */
  447. UNUSED(hdac);
  448. /* NOTE : This function should not be modified, when the callback is needed,
  449. the HAL_DAC_MspInit could be implemented in the user file
  450. */
  451. }
  452. /**
  453. * @brief DeInitialize the DAC MSP.
  454. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  455. * the configuration information for the specified DAC.
  456. * @retval None
  457. */
  458. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
  459. {
  460. /* Prevent unused argument(s) compilation warning */
  461. UNUSED(hdac);
  462. /* NOTE : This function should not be modified, when the callback is needed,
  463. the HAL_DAC_MspDeInit could be implemented in the user file
  464. */
  465. }
  466. /**
  467. * @}
  468. */
  469. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  470. * @brief IO operation functions
  471. *
  472. @verbatim
  473. ==============================================================================
  474. ##### IO operation functions #####
  475. ==============================================================================
  476. [..] This section provides functions allowing to:
  477. (+) Start conversion.
  478. (+) Stop conversion.
  479. (+) Start conversion and enable DMA transfer.
  480. (+) Stop conversion and disable DMA transfer.
  481. (+) Get result of conversion.
  482. @endverbatim
  483. * @{
  484. */
  485. /**
  486. * @brief Enables DAC and starts conversion of channel.
  487. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  488. * the configuration information for the specified DAC.
  489. * @param Channel The selected DAC channel.
  490. * This parameter can be one of the following values:
  491. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  492. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  493. *
  494. * (1) On this STM32 series, parameter not available on all instances.
  495. * Refer to device datasheet for channels availability.
  496. * @retval HAL status
  497. */
  498. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
  499. {
  500. __IO uint32_t wait_loop_index;
  501. /* Check the DAC peripheral handle */
  502. if (hdac == NULL)
  503. {
  504. return HAL_ERROR;
  505. }
  506. /* Check the parameters */
  507. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  508. /* Process locked */
  509. __HAL_LOCK(hdac);
  510. /* Change DAC state */
  511. hdac->State = HAL_DAC_STATE_BUSY;
  512. /* Enable the Peripheral */
  513. __HAL_DAC_ENABLE(hdac, Channel);
  514. /* Ensure minimum wait before using peripheral after enabling it */
  515. /* Wait loop initialization and execution */
  516. /* Note: Variable divided by 2 to compensate partially CPU processing cycles, scaling in us split to not exceed 32 */
  517. /* bits register capacity and handle low frequency. */
  518. wait_loop_index = ((DAC_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  519. while (wait_loop_index != 0UL)
  520. {
  521. wait_loop_index--;
  522. }
  523. if (Channel == DAC_CHANNEL_1)
  524. {
  525. /* Check if software trigger enabled */
  526. if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
  527. {
  528. /* Enable the selected DAC software conversion */
  529. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
  530. }
  531. }
  532. else
  533. {
  534. /* Check if software trigger enabled */
  535. if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (Channel & 0x10UL)))
  536. {
  537. /* Enable the selected DAC software conversion*/
  538. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
  539. }
  540. }
  541. /* Change DAC state */
  542. hdac->State = HAL_DAC_STATE_READY;
  543. /* Process unlocked */
  544. __HAL_UNLOCK(hdac);
  545. /* Return function status */
  546. return HAL_OK;
  547. }
  548. /**
  549. * @brief Disables DAC and stop conversion of channel.
  550. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  551. * the configuration information for the specified DAC.
  552. * @param Channel The selected DAC channel.
  553. * This parameter can be one of the following values:
  554. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  555. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  556. *
  557. * (1) On this STM32 series, parameter not available on all instances.
  558. * Refer to device datasheet for channels availability.
  559. * @retval HAL status
  560. */
  561. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
  562. {
  563. /* Check the DAC peripheral handle */
  564. if (hdac == NULL)
  565. {
  566. return HAL_ERROR;
  567. }
  568. /* Check the parameters */
  569. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  570. /* Disable the Peripheral */
  571. __HAL_DAC_DISABLE(hdac, Channel);
  572. /* Change DAC state */
  573. hdac->State = HAL_DAC_STATE_READY;
  574. /* Return function status */
  575. return HAL_OK;
  576. }
  577. /**
  578. * @brief Enables DAC and starts conversion of channel.
  579. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  580. * the configuration information for the specified DAC.
  581. * @param Channel The selected DAC channel.
  582. * This parameter can be one of the following values:
  583. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  584. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  585. *
  586. * (1) On this STM32 series, parameter not available on all instances.
  587. * Refer to device datasheet for channels availability.
  588. * @param pData The source Buffer address.
  589. * @param Length The length of data to be transferred from memory to DAC peripheral
  590. * @param Alignment Specifies the data alignment for DAC channel.
  591. * This parameter can be one of the following values:
  592. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  593. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  594. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  595. * @retval HAL status
  596. */
  597. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, const uint32_t *pData, uint32_t Length,
  598. uint32_t Alignment)
  599. {
  600. HAL_StatusTypeDef status;
  601. uint32_t tmpreg;
  602. __IO uint32_t wait_loop_index;
  603. /* Check the DAC peripheral handle */
  604. if (hdac == NULL)
  605. {
  606. return HAL_ERROR;
  607. }
  608. /* Check the parameters */
  609. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  610. assert_param(IS_DAC_ALIGN(Alignment));
  611. /* Process locked */
  612. __HAL_LOCK(hdac);
  613. /* Change DAC state */
  614. hdac->State = HAL_DAC_STATE_BUSY;
  615. if (Channel == DAC_CHANNEL_1)
  616. {
  617. /* Set the DMA transfer complete callback for channel1 */
  618. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  619. /* Set the DMA half transfer complete callback for channel1 */
  620. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  621. /* Set the DMA error callback for channel1 */
  622. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  623. /* Enable the selected DAC channel1 DMA request */
  624. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  625. /* Case of use of channel 1 */
  626. switch (Alignment)
  627. {
  628. case DAC_ALIGN_12B_R:
  629. /* Get DHR12R1 address */
  630. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  631. break;
  632. case DAC_ALIGN_12B_L:
  633. /* Get DHR12L1 address */
  634. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  635. break;
  636. default: /* case DAC_ALIGN_8B_R */
  637. /* Get DHR8R1 address */
  638. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  639. break;
  640. }
  641. }
  642. else
  643. {
  644. /* Set the DMA transfer complete callback for channel2 */
  645. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  646. /* Set the DMA half transfer complete callback for channel2 */
  647. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  648. /* Set the DMA error callback for channel2 */
  649. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  650. /* Enable the selected DAC channel2 DMA request */
  651. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  652. /* Case of use of channel 2 */
  653. switch (Alignment)
  654. {
  655. case DAC_ALIGN_12B_R:
  656. /* Get DHR12R2 address */
  657. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  658. break;
  659. case DAC_ALIGN_12B_L:
  660. /* Get DHR12L2 address */
  661. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  662. break;
  663. default: /* case DAC_ALIGN_8B_R */
  664. /* Get DHR8R2 address */
  665. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  666. break;
  667. }
  668. }
  669. if (Channel == DAC_CHANNEL_1)
  670. {
  671. /* Enable the DAC DMA underrun interrupt */
  672. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  673. /* Enable the DMA channel */
  674. status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  675. }
  676. else
  677. {
  678. /* Enable the DAC DMA underrun interrupt */
  679. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  680. /* Enable the DMA channel */
  681. status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  682. }
  683. /* Process Unlocked */
  684. __HAL_UNLOCK(hdac);
  685. if (status == HAL_OK)
  686. {
  687. /* Enable the Peripheral */
  688. __HAL_DAC_ENABLE(hdac, Channel);
  689. /* Ensure minimum wait before using peripheral after enabling it */
  690. /* Wait loop initialization and execution */
  691. /* Note: Variable divided by 2 to compensate partially */
  692. /* CPU processing cycles, scaling in us split to not */
  693. /* exceed 32 bits register capacity and handle low frequency. */
  694. wait_loop_index = ((DAC_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  695. while (wait_loop_index != 0UL)
  696. {
  697. wait_loop_index--;
  698. }
  699. }
  700. else
  701. {
  702. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  703. }
  704. /* Return function status */
  705. return status;
  706. }
  707. /**
  708. * @brief Disables DAC and stop conversion of channel.
  709. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  710. * the configuration information for the specified DAC.
  711. * @param Channel The selected DAC channel.
  712. * This parameter can be one of the following values:
  713. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  714. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  715. *
  716. * (1) On this STM32 series, parameter not available on all instances.
  717. * Refer to device datasheet for channels availability.
  718. * @retval HAL status
  719. */
  720. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
  721. {
  722. /* Check the DAC peripheral handle */
  723. if (hdac == NULL)
  724. {
  725. return HAL_ERROR;
  726. }
  727. /* Check the parameters */
  728. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  729. /* Disable the selected DAC channel DMA request */
  730. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << (Channel & 0x10UL));
  731. /* Disable the Peripheral */
  732. __HAL_DAC_DISABLE(hdac, Channel);
  733. /* Disable the DMA channel */
  734. /* Channel1 is used */
  735. if (Channel == DAC_CHANNEL_1)
  736. {
  737. /* Disable the DMA channel */
  738. (void)HAL_DMA_Abort(hdac->DMA_Handle1);
  739. /* Disable the DAC DMA underrun interrupt */
  740. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
  741. }
  742. else /* Channel2 is used for */
  743. {
  744. /* Disable the DMA channel */
  745. (void)HAL_DMA_Abort(hdac->DMA_Handle2);
  746. /* Disable the DAC DMA underrun interrupt */
  747. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
  748. }
  749. /* Change DAC state */
  750. hdac->State = HAL_DAC_STATE_READY;
  751. /* Return function status */
  752. return HAL_OK;
  753. }
  754. /**
  755. * @brief Handles DAC interrupt request
  756. * This function uses the interruption of DMA
  757. * underrun.
  758. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  759. * the configuration information for the specified DAC.
  760. * @retval None
  761. */
  762. void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
  763. {
  764. uint32_t itsource = hdac->Instance->CR;
  765. uint32_t itflag = hdac->Instance->SR;
  766. if ((itsource & DAC_IT_DMAUDR1) == DAC_IT_DMAUDR1)
  767. {
  768. /* Check underrun flag of DAC channel 1 */
  769. if ((itflag & DAC_FLAG_DMAUDR1) == DAC_FLAG_DMAUDR1)
  770. {
  771. /* Change DAC state to error state */
  772. hdac->State = HAL_DAC_STATE_ERROR;
  773. /* Set DAC error code to channel1 DMA underrun error */
  774. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
  775. /* Clear the underrun flag */
  776. __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR1);
  777. /* Disable the selected DAC channel1 DMA request */
  778. __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN1);
  779. /* Error callback */
  780. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  781. hdac->DMAUnderrunCallbackCh1(hdac);
  782. #else
  783. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  784. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  785. }
  786. }
  787. if ((itsource & DAC_IT_DMAUDR2) == DAC_IT_DMAUDR2)
  788. {
  789. /* Check underrun flag of DAC channel 2 */
  790. if ((itflag & DAC_FLAG_DMAUDR2) == DAC_FLAG_DMAUDR2)
  791. {
  792. /* Change DAC state to error state */
  793. hdac->State = HAL_DAC_STATE_ERROR;
  794. /* Set DAC error code to channel2 DMA underrun error */
  795. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
  796. /* Clear the underrun flag */
  797. __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR2);
  798. /* Disable the selected DAC channel2 DMA request */
  799. __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN2);
  800. /* Error callback */
  801. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  802. hdac->DMAUnderrunCallbackCh2(hdac);
  803. #else
  804. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  805. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  806. }
  807. }
  808. }
  809. /**
  810. * @brief Set the specified data holding register value for DAC channel.
  811. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  812. * the configuration information for the specified DAC.
  813. * @param Channel The selected DAC channel.
  814. * This parameter can be one of the following values:
  815. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  816. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  817. *
  818. * (1) On this STM32 series, parameter not available on all instances.
  819. * Refer to device datasheet for channels availability.
  820. * @param Alignment Specifies the data alignment.
  821. * This parameter can be one of the following values:
  822. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  823. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  824. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  825. * @param Data Data to be loaded in the selected data holding register.
  826. * @retval HAL status
  827. */
  828. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  829. {
  830. __IO uint32_t tmp = 0UL;
  831. /* Check the DAC peripheral handle */
  832. if (hdac == NULL)
  833. {
  834. return HAL_ERROR;
  835. }
  836. /* Check the parameters */
  837. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  838. assert_param(IS_DAC_ALIGN(Alignment));
  839. /* In case DMA Double data mode is activated, DATA range is almost full uin32_t one: no check */
  840. if ((hdac->Instance->MCR & (DAC_MCR_DMADOUBLE1 << (Channel & 0x10UL))) == 0UL)
  841. {
  842. assert_param(IS_DAC_DATA(Data));
  843. }
  844. tmp = (uint32_t)hdac->Instance;
  845. if (Channel == DAC_CHANNEL_1)
  846. {
  847. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  848. }
  849. else
  850. {
  851. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  852. }
  853. /* Set the DAC channel selected data holding register */
  854. *(__IO uint32_t *) tmp = Data;
  855. /* Return function status */
  856. return HAL_OK;
  857. }
  858. /**
  859. * @brief Conversion complete callback in non-blocking mode for Channel1
  860. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  861. * the configuration information for the specified DAC.
  862. * @retval None
  863. */
  864. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
  865. {
  866. /* Prevent unused argument(s) compilation warning */
  867. UNUSED(hdac);
  868. /* NOTE : This function should not be modified, when the callback is needed,
  869. the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
  870. */
  871. }
  872. /**
  873. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel1
  874. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  875. * the configuration information for the specified DAC.
  876. * @retval None
  877. */
  878. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
  879. {
  880. /* Prevent unused argument(s) compilation warning */
  881. UNUSED(hdac);
  882. /* NOTE : This function should not be modified, when the callback is needed,
  883. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  884. */
  885. }
  886. /**
  887. * @brief Error DAC callback for Channel1.
  888. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  889. * the configuration information for the specified DAC.
  890. * @retval None
  891. */
  892. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  893. {
  894. /* Prevent unused argument(s) compilation warning */
  895. UNUSED(hdac);
  896. /* NOTE : This function should not be modified, when the callback is needed,
  897. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  898. */
  899. }
  900. /**
  901. * @brief DMA underrun DAC callback for channel1.
  902. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  903. * the configuration information for the specified DAC.
  904. * @retval None
  905. */
  906. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  907. {
  908. /* Prevent unused argument(s) compilation warning */
  909. UNUSED(hdac);
  910. /* NOTE : This function should not be modified, when the callback is needed,
  911. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  912. */
  913. }
  914. /**
  915. * @}
  916. */
  917. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  918. * @brief Peripheral Control functions
  919. *
  920. @verbatim
  921. ==============================================================================
  922. ##### Peripheral Control functions #####
  923. ==============================================================================
  924. [..] This section provides functions allowing to:
  925. (+) Configure channels.
  926. (+) Set the specified data holding register value for DAC channel.
  927. @endverbatim
  928. * @{
  929. */
  930. /**
  931. * @brief Returns the last data output value of the selected DAC channel.
  932. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  933. * the configuration information for the specified DAC.
  934. * @param Channel The selected DAC channel.
  935. * This parameter can be one of the following values:
  936. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  937. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  938. *
  939. * (1) On this STM32 series, parameter not available on all instances.
  940. * Refer to device datasheet for channels availability.
  941. * @retval The selected DAC channel data output value.
  942. */
  943. uint32_t HAL_DAC_GetValue(const DAC_HandleTypeDef *hdac, uint32_t Channel)
  944. {
  945. uint32_t result;
  946. /* Check the DAC peripheral handle */
  947. assert_param(hdac != NULL);
  948. /* Check the parameters */
  949. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  950. if (Channel == DAC_CHANNEL_1)
  951. {
  952. result = hdac->Instance->DOR1;
  953. }
  954. else
  955. {
  956. result = hdac->Instance->DOR2;
  957. }
  958. /* Returns the DAC channel data output register value */
  959. return result;
  960. }
  961. /**
  962. * @brief Configures the selected DAC channel.
  963. * @note By calling this function, the high frequency interface mode (HFSEL bits)
  964. * will be set. This parameter scope is the DAC instance. As the function
  965. * is called for each channel, the @ref DAC_HighFrequency of @arg sConfig
  966. * must be the same at each call.
  967. * (or DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC self detect).
  968. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  969. * the configuration information for the specified DAC.
  970. * @param sConfig DAC configuration structure.
  971. * @param Channel The selected DAC channel.
  972. * This parameter can be one of the following values:
  973. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  974. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  975. *
  976. * (1) On this STM32 series, parameter not available on all instances.
  977. * Refer to device datasheet for channels availability.
  978. * @retval HAL status
  979. */
  980. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac,
  981. const DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
  982. {
  983. HAL_StatusTypeDef status = HAL_OK;
  984. uint32_t tmpreg1;
  985. uint32_t tmpreg2;
  986. uint32_t tickstart;
  987. uint32_t hclkfreq;
  988. uint32_t connectOnChip;
  989. /* Check the DAC peripheral handle and channel configuration struct */
  990. if ((hdac == NULL) || (sConfig == NULL))
  991. {
  992. return HAL_ERROR;
  993. }
  994. /* Check the DAC parameters */
  995. assert_param(IS_DAC_HIGH_FREQUENCY_MODE(sConfig->DAC_HighFrequency));
  996. assert_param(IS_DAC_TRIGGER(hdac->Instance, sConfig->DAC_Trigger));
  997. assert_param(IS_DAC_TRIGGER2(hdac->Instance, sConfig->DAC_Trigger2));
  998. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  999. assert_param(IS_DAC_CHIP_CONNECTION(sConfig->DAC_ConnectOnChipPeripheral));
  1000. assert_param(IS_DAC_TRIMMING(sConfig->DAC_UserTrimming));
  1001. if ((sConfig->DAC_UserTrimming) == DAC_TRIMMING_USER)
  1002. {
  1003. assert_param(IS_DAC_TRIMMINGVALUE(sConfig->DAC_TrimmingValue));
  1004. }
  1005. assert_param(IS_DAC_SAMPLEANDHOLD(sConfig->DAC_SampleAndHold));
  1006. if ((sConfig->DAC_SampleAndHold) == DAC_SAMPLEANDHOLD_ENABLE)
  1007. {
  1008. assert_param(IS_DAC_SAMPLETIME(sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime));
  1009. assert_param(IS_DAC_HOLDTIME(sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime));
  1010. assert_param(IS_DAC_REFRESHTIME(sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime));
  1011. }
  1012. else
  1013. {
  1014. /* In case of mode normal and buffer disabled, connection to both on chip periph and external pin is not possible */
  1015. if (sConfig->DAC_OutputBuffer == DAC_OUTPUTBUFFER_DISABLE)
  1016. {
  1017. assert_param(sConfig->DAC_ConnectOnChipPeripheral != DAC_CHIPCONNECT_BOTH);
  1018. }
  1019. }
  1020. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  1021. assert_param(IS_FUNCTIONAL_STATE(sConfig->DAC_DMADoubleDataMode));
  1022. assert_param(IS_FUNCTIONAL_STATE(sConfig->DAC_SignedFormat));
  1023. /* Process locked */
  1024. __HAL_LOCK(hdac);
  1025. /* Change DAC state */
  1026. hdac->State = HAL_DAC_STATE_BUSY;
  1027. /* Sample and hold configuration */
  1028. if (sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)
  1029. {
  1030. /* Get timeout */
  1031. tickstart = HAL_GetTick();
  1032. if (Channel == DAC_CHANNEL_1)
  1033. {
  1034. /* SHSR1 can be written when BWST1 is cleared */
  1035. while (((hdac->Instance->SR) & DAC_SR_BWST1) != 0UL)
  1036. {
  1037. /* Check for the Timeout */
  1038. if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
  1039. {
  1040. /* New check to avoid false timeout detection in case of preemption */
  1041. if (((hdac->Instance->SR) & DAC_SR_BWST1) != 0UL)
  1042. {
  1043. /* Update error code */
  1044. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
  1045. /* Change the DMA state */
  1046. hdac->State = HAL_DAC_STATE_TIMEOUT;
  1047. return HAL_TIMEOUT;
  1048. }
  1049. }
  1050. }
  1051. hdac->Instance->SHSR1 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
  1052. }
  1053. else /* Channel 2 */
  1054. {
  1055. /* SHSR2 can be written when BWST2 is cleared */
  1056. while (((hdac->Instance->SR) & DAC_SR_BWST2) != 0UL)
  1057. {
  1058. /* Check for the Timeout */
  1059. if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
  1060. {
  1061. /* New check to avoid false timeout detection in case of preemption */
  1062. if (((hdac->Instance->SR) & DAC_SR_BWST2) != 0UL)
  1063. {
  1064. /* Update error code */
  1065. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
  1066. /* Change the DMA state */
  1067. hdac->State = HAL_DAC_STATE_TIMEOUT;
  1068. return HAL_TIMEOUT;
  1069. }
  1070. }
  1071. }
  1072. hdac->Instance->SHSR2 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
  1073. }
  1074. /* HoldTime */
  1075. MODIFY_REG(hdac->Instance->SHHR, DAC_SHHR_THOLD1 << (Channel & 0x10UL),
  1076. (sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime) << (Channel & 0x10UL));
  1077. /* RefreshTime */
  1078. MODIFY_REG(hdac->Instance->SHRR, DAC_SHRR_TREFRESH1 << (Channel & 0x10UL),
  1079. (sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime) << (Channel & 0x10UL));
  1080. }
  1081. if (sConfig->DAC_UserTrimming == DAC_TRIMMING_USER)
  1082. /* USER TRIMMING */
  1083. {
  1084. /* Get the DAC CCR value */
  1085. tmpreg1 = hdac->Instance->CCR;
  1086. /* Clear trimming value */
  1087. tmpreg1 &= ~(((uint32_t)(DAC_CCR_OTRIM1)) << (Channel & 0x10UL));
  1088. /* Configure for the selected trimming offset */
  1089. tmpreg2 = sConfig->DAC_TrimmingValue;
  1090. /* Calculate CCR register value depending on DAC_Channel */
  1091. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  1092. /* Write to DAC CCR */
  1093. hdac->Instance->CCR = tmpreg1;
  1094. }
  1095. /* else factory trimming is used (factory setting are available at reset)*/
  1096. /* SW Nothing has nothing to do */
  1097. /* Get the DAC MCR value */
  1098. tmpreg1 = hdac->Instance->MCR;
  1099. /* Clear DAC_MCR_MODEx bits */
  1100. tmpreg1 &= ~(((uint32_t)(DAC_MCR_MODE1)) << (Channel & 0x10UL));
  1101. /* Configure for the selected DAC channel: mode, buffer output & on chip peripheral connect */
  1102. if (sConfig->DAC_ConnectOnChipPeripheral == DAC_CHIPCONNECT_EXTERNAL)
  1103. {
  1104. connectOnChip = 0x00000000UL;
  1105. }
  1106. else if (sConfig->DAC_ConnectOnChipPeripheral == DAC_CHIPCONNECT_INTERNAL)
  1107. {
  1108. connectOnChip = DAC_MCR_MODE1_0;
  1109. }
  1110. else /* (sConfig->DAC_ConnectOnChipPeripheral == DAC_CHIPCONNECT_BOTH) */
  1111. {
  1112. if (sConfig->DAC_OutputBuffer == DAC_OUTPUTBUFFER_ENABLE)
  1113. {
  1114. connectOnChip = DAC_MCR_MODE1_0;
  1115. }
  1116. else
  1117. {
  1118. connectOnChip = 0x00000000UL;
  1119. }
  1120. }
  1121. tmpreg2 = (sConfig->DAC_SampleAndHold | sConfig->DAC_OutputBuffer | connectOnChip);
  1122. /* Clear DAC_MCR_DMADOUBLEx */
  1123. tmpreg1 &= ~(((uint32_t)(DAC_MCR_DMADOUBLE1)) << (Channel & 0x10UL));
  1124. /* Configure for the selected DAC channel: DMA double data mode */
  1125. tmpreg2 |= (sConfig->DAC_DMADoubleDataMode == ENABLE) ? DAC_MCR_DMADOUBLE1 : 0UL;
  1126. /* Clear DAC_MCR_SINFORMATx */
  1127. tmpreg1 &= ~(((uint32_t)(DAC_MCR_SINFORMAT1)) << (Channel & 0x10UL));
  1128. /* Configure for the selected DAC channel: Signed format */
  1129. tmpreg2 |= (sConfig->DAC_SignedFormat == ENABLE) ? DAC_MCR_SINFORMAT1 : 0UL;
  1130. /* Clear DAC_MCR_HFSEL bits */
  1131. tmpreg1 &= ~(DAC_MCR_HFSEL);
  1132. /* Configure for both DAC channels: high frequency mode */
  1133. if (DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC == sConfig->DAC_HighFrequency)
  1134. {
  1135. hclkfreq = HAL_RCC_GetHCLKFreq();
  1136. if (hclkfreq > HFSEL_ENABLE_THRESHOLD_160MHZ)
  1137. {
  1138. tmpreg1 |= DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_160MHZ;
  1139. }
  1140. else if (hclkfreq > HFSEL_ENABLE_THRESHOLD_80MHZ)
  1141. {
  1142. tmpreg1 |= DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MHZ;
  1143. }
  1144. else
  1145. {
  1146. tmpreg1 |= DAC_HIGH_FREQUENCY_INTERFACE_MODE_DISABLE;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. tmpreg1 |= sConfig->DAC_HighFrequency;
  1152. }
  1153. /* Calculate MCR register value depending on DAC_Channel */
  1154. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  1155. /* Write to DAC MCR */
  1156. hdac->Instance->MCR = tmpreg1;
  1157. /* DAC in normal operating mode hence clear DAC_CR_CENx bit */
  1158. CLEAR_BIT(hdac->Instance->CR, DAC_CR_CEN1 << (Channel & 0x10UL));
  1159. /* Get the DAC CR value */
  1160. tmpreg1 = hdac->Instance->CR;
  1161. /* Clear TENx, TSELx, WAVEx and MAMPx bits */
  1162. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1)) << (Channel & 0x10UL));
  1163. /* Configure for the selected DAC channel: trigger */
  1164. /* Set TSELx and TENx bits according to DAC_Trigger value */
  1165. tmpreg2 = sConfig->DAC_Trigger;
  1166. /* Calculate CR register value depending on DAC_Channel */
  1167. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  1168. /* Write to DAC CR */
  1169. hdac->Instance->CR = tmpreg1;
  1170. /* Disable wave generation */
  1171. CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << (Channel & 0x10UL)));
  1172. /* Set STRSTTRIGSELx and STINCTRIGSELx bits according to DAC_Trigger & DAC_Trigger2 values */
  1173. tmpreg2 = ((sConfig->DAC_Trigger & DAC_CR_TSEL1) >> DAC_CR_TSEL1_Pos) << DAC_STMODR_STRSTTRIGSEL1_Pos;
  1174. tmpreg2 |= ((sConfig->DAC_Trigger2 & DAC_CR_TSEL1) >> DAC_CR_TSEL1_Pos) << DAC_STMODR_STINCTRIGSEL1_Pos;
  1175. /* Modify STMODR register value depending on DAC_Channel */
  1176. MODIFY_REG(hdac->Instance->STMODR, (DAC_STMODR_STINCTRIGSEL1 | DAC_STMODR_STRSTTRIGSEL1)
  1177. << (Channel & 0x10UL), tmpreg2 << (Channel & 0x10UL));
  1178. /* Change DAC state */
  1179. hdac->State = HAL_DAC_STATE_READY;
  1180. /* Process unlocked */
  1181. __HAL_UNLOCK(hdac);
  1182. /* Return function status */
  1183. return status;
  1184. }
  1185. /**
  1186. * @}
  1187. */
  1188. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  1189. * @brief Peripheral State and Errors functions
  1190. *
  1191. @verbatim
  1192. ==============================================================================
  1193. ##### Peripheral State and Errors functions #####
  1194. ==============================================================================
  1195. [..]
  1196. This subsection provides functions allowing to
  1197. (+) Check the DAC state.
  1198. (+) Check the DAC Errors.
  1199. @endverbatim
  1200. * @{
  1201. */
  1202. /**
  1203. * @brief return the DAC handle state
  1204. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  1205. * the configuration information for the specified DAC.
  1206. * @retval HAL state
  1207. */
  1208. HAL_DAC_StateTypeDef HAL_DAC_GetState(const DAC_HandleTypeDef *hdac)
  1209. {
  1210. /* Return DAC handle state */
  1211. return hdac->State;
  1212. }
  1213. /**
  1214. * @brief Return the DAC error code
  1215. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  1216. * the configuration information for the specified DAC.
  1217. * @retval DAC Error Code
  1218. */
  1219. uint32_t HAL_DAC_GetError(const DAC_HandleTypeDef *hdac)
  1220. {
  1221. return hdac->ErrorCode;
  1222. }
  1223. /**
  1224. * @}
  1225. */
  1226. /**
  1227. * @}
  1228. */
  1229. /** @addtogroup DAC_Exported_Functions
  1230. * @{
  1231. */
  1232. /** @addtogroup DAC_Exported_Functions_Group1
  1233. * @{
  1234. */
  1235. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1236. /**
  1237. * @brief Register a User DAC Callback
  1238. * To be used instead of the weak (overridden) predefined callback
  1239. * @note The HAL_DAC_RegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to register
  1240. * callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
  1241. * @param hdac DAC handle
  1242. * @param CallbackID ID of the callback to be registered
  1243. * This parameter can be one of the following values:
  1244. * @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK DAC Error Callback ID
  1245. * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 Complete Callback ID
  1246. * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
  1247. * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
  1248. * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
  1249. * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
  1250. * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
  1251. * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
  1252. * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
  1253. * @arg @ref HAL_DAC_MSPINIT_CB_ID DAC MSP Init Callback ID
  1254. * @arg @ref HAL_DAC_MSPDEINIT_CB_ID DAC MSP DeInit Callback ID
  1255. *
  1256. * @param pCallback pointer to the Callback function
  1257. * @retval status
  1258. */
  1259. HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
  1260. pDAC_CallbackTypeDef pCallback)
  1261. {
  1262. HAL_StatusTypeDef status = HAL_OK;
  1263. /* Check the DAC peripheral handle */
  1264. if (hdac == NULL)
  1265. {
  1266. return HAL_ERROR;
  1267. }
  1268. if (pCallback == NULL)
  1269. {
  1270. /* Update the error code */
  1271. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1272. return HAL_ERROR;
  1273. }
  1274. if (hdac->State == HAL_DAC_STATE_READY)
  1275. {
  1276. switch (CallbackID)
  1277. {
  1278. case HAL_DAC_CH1_COMPLETE_CB_ID :
  1279. hdac->ConvCpltCallbackCh1 = pCallback;
  1280. break;
  1281. case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
  1282. hdac->ConvHalfCpltCallbackCh1 = pCallback;
  1283. break;
  1284. case HAL_DAC_CH1_ERROR_ID :
  1285. hdac->ErrorCallbackCh1 = pCallback;
  1286. break;
  1287. case HAL_DAC_CH1_UNDERRUN_CB_ID :
  1288. hdac->DMAUnderrunCallbackCh1 = pCallback;
  1289. break;
  1290. case HAL_DAC_CH2_COMPLETE_CB_ID :
  1291. hdac->ConvCpltCallbackCh2 = pCallback;
  1292. break;
  1293. case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
  1294. hdac->ConvHalfCpltCallbackCh2 = pCallback;
  1295. break;
  1296. case HAL_DAC_CH2_ERROR_ID :
  1297. hdac->ErrorCallbackCh2 = pCallback;
  1298. break;
  1299. case HAL_DAC_CH2_UNDERRUN_CB_ID :
  1300. hdac->DMAUnderrunCallbackCh2 = pCallback;
  1301. break;
  1302. case HAL_DAC_MSPINIT_CB_ID :
  1303. hdac->MspInitCallback = pCallback;
  1304. break;
  1305. case HAL_DAC_MSPDEINIT_CB_ID :
  1306. hdac->MspDeInitCallback = pCallback;
  1307. break;
  1308. default :
  1309. /* Update the error code */
  1310. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1311. /* update return status */
  1312. status = HAL_ERROR;
  1313. break;
  1314. }
  1315. }
  1316. else if (hdac->State == HAL_DAC_STATE_RESET)
  1317. {
  1318. switch (CallbackID)
  1319. {
  1320. case HAL_DAC_MSPINIT_CB_ID :
  1321. hdac->MspInitCallback = pCallback;
  1322. break;
  1323. case HAL_DAC_MSPDEINIT_CB_ID :
  1324. hdac->MspDeInitCallback = pCallback;
  1325. break;
  1326. default :
  1327. /* Update the error code */
  1328. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1329. /* update return status */
  1330. status = HAL_ERROR;
  1331. break;
  1332. }
  1333. }
  1334. else
  1335. {
  1336. /* Update the error code */
  1337. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1338. /* update return status */
  1339. status = HAL_ERROR;
  1340. }
  1341. return status;
  1342. }
  1343. /**
  1344. * @brief Unregister a User DAC Callback
  1345. * DAC Callback is redirected to the weak (overridden) predefined callback
  1346. * @note The HAL_DAC_UnRegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to un-register
  1347. * callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
  1348. * @param hdac DAC handle
  1349. * @param CallbackID ID of the callback to be unregistered
  1350. * This parameter can be one of the following values:
  1351. * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 transfer Complete Callback ID
  1352. * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
  1353. * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
  1354. * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
  1355. * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
  1356. * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
  1357. * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
  1358. * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
  1359. * @arg @ref HAL_DAC_MSPINIT_CB_ID DAC MSP Init Callback ID
  1360. * @arg @ref HAL_DAC_MSPDEINIT_CB_ID DAC MSP DeInit Callback ID
  1361. * @arg @ref HAL_DAC_ALL_CB_ID DAC All callbacks
  1362. * @retval status
  1363. */
  1364. HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
  1365. {
  1366. HAL_StatusTypeDef status = HAL_OK;
  1367. /* Check the DAC peripheral handle */
  1368. if (hdac == NULL)
  1369. {
  1370. return HAL_ERROR;
  1371. }
  1372. if (hdac->State == HAL_DAC_STATE_READY)
  1373. {
  1374. switch (CallbackID)
  1375. {
  1376. case HAL_DAC_CH1_COMPLETE_CB_ID :
  1377. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  1378. break;
  1379. case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
  1380. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  1381. break;
  1382. case HAL_DAC_CH1_ERROR_ID :
  1383. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  1384. break;
  1385. case HAL_DAC_CH1_UNDERRUN_CB_ID :
  1386. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  1387. break;
  1388. case HAL_DAC_CH2_COMPLETE_CB_ID :
  1389. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  1390. break;
  1391. case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
  1392. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  1393. break;
  1394. case HAL_DAC_CH2_ERROR_ID :
  1395. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  1396. break;
  1397. case HAL_DAC_CH2_UNDERRUN_CB_ID :
  1398. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  1399. break;
  1400. case HAL_DAC_MSPINIT_CB_ID :
  1401. hdac->MspInitCallback = HAL_DAC_MspInit;
  1402. break;
  1403. case HAL_DAC_MSPDEINIT_CB_ID :
  1404. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  1405. break;
  1406. case HAL_DAC_ALL_CB_ID :
  1407. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  1408. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  1409. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  1410. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  1411. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  1412. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  1413. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  1414. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  1415. hdac->MspInitCallback = HAL_DAC_MspInit;
  1416. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  1417. break;
  1418. default :
  1419. /* Update the error code */
  1420. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1421. /* update return status */
  1422. status = HAL_ERROR;
  1423. break;
  1424. }
  1425. }
  1426. else if (hdac->State == HAL_DAC_STATE_RESET)
  1427. {
  1428. switch (CallbackID)
  1429. {
  1430. case HAL_DAC_MSPINIT_CB_ID :
  1431. hdac->MspInitCallback = HAL_DAC_MspInit;
  1432. break;
  1433. case HAL_DAC_MSPDEINIT_CB_ID :
  1434. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  1435. break;
  1436. default :
  1437. /* Update the error code */
  1438. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1439. /* update return status */
  1440. status = HAL_ERROR;
  1441. break;
  1442. }
  1443. }
  1444. else
  1445. {
  1446. /* Update the error code */
  1447. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  1448. /* update return status */
  1449. status = HAL_ERROR;
  1450. }
  1451. return status;
  1452. }
  1453. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1454. /**
  1455. * @}
  1456. */
  1457. /**
  1458. * @}
  1459. */
  1460. /** @addtogroup DAC_Private_Functions
  1461. * @{
  1462. */
  1463. /**
  1464. * @brief DMA conversion complete callback.
  1465. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1466. * the configuration information for the specified DMA module.
  1467. * @retval None
  1468. */
  1469. void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  1470. {
  1471. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1472. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1473. hdac->ConvCpltCallbackCh1(hdac);
  1474. #else
  1475. HAL_DAC_ConvCpltCallbackCh1(hdac);
  1476. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1477. hdac->State = HAL_DAC_STATE_READY;
  1478. }
  1479. /**
  1480. * @brief DMA half transfer complete callback.
  1481. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1482. * the configuration information for the specified DMA module.
  1483. * @retval None
  1484. */
  1485. void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  1486. {
  1487. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1488. /* Conversion complete callback */
  1489. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1490. hdac->ConvHalfCpltCallbackCh1(hdac);
  1491. #else
  1492. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  1493. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1494. }
  1495. /**
  1496. * @brief DMA error callback
  1497. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1498. * the configuration information for the specified DMA module.
  1499. * @retval None
  1500. */
  1501. void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  1502. {
  1503. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1504. /* Set DAC error code to DMA error */
  1505. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  1506. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  1507. hdac->ErrorCallbackCh1(hdac);
  1508. #else
  1509. HAL_DAC_ErrorCallbackCh1(hdac);
  1510. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  1511. hdac->State = HAL_DAC_STATE_READY;
  1512. }
  1513. /**
  1514. * @}
  1515. */
  1516. /**
  1517. * @}
  1518. */
  1519. #endif /* DAC1 || DAC2 || DAC3 || DAC4 */
  1520. #endif /* HAL_DAC_MODULE_ENABLED */
  1521. /**
  1522. * @}
  1523. */