stm32g0xx_hal_dac.c 54 KB

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