stm32l4xx_hal_dac.c 66 KB

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