stm32g4xx_hal_dac_ex.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended DAC HAL module driver.
  6. * This file provides firmware functions to manage the extended
  7. * functionalities of the DAC peripheral.
  8. *
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2019 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### How to use this driver #####
  24. ==============================================================================
  25. [..]
  26. *** Dual mode IO operation ***
  27. ==============================
  28. [..]
  29. (+) Use HAL_DACEx_DualStart() to enable both channel and start conversion
  30. for dual mode operation.
  31. If software trigger is selected, using HAL_DACEx_DualStart() will start
  32. the conversion of the value previously set by HAL_DACEx_DualSetValue().
  33. (+) Use HAL_DACEx_DualStop() to disable both channel and stop conversion
  34. for dual mode operation.
  35. (+) Use HAL_DACEx_DualStart_DMA() to enable both channel and start conversion
  36. for dual mode operation using DMA to feed DAC converters.
  37. First issued trigger will start the conversion of the value previously
  38. set by HAL_DACEx_DualSetValue().
  39. The same callbacks that are used in single mode are called in dual mode to notify
  40. transfer completion (half complete or complete), errors or underrun.
  41. (+) Use HAL_DACEx_DualStop_DMA() to disable both channel and stop conversion
  42. for dual mode operation using DMA to feed DAC converters.
  43. (+) When Dual mode is enabled (i.e. DAC Channel1 and Channel2 are used simultaneously) :
  44. Use HAL_DACEx_DualGetValue() to get digital data to be converted and use
  45. HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in
  46. Channel 1 and Channel 2.
  47. *** Signal generation operation ***
  48. ===================================
  49. [..]
  50. (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
  51. (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
  52. (+) Use HAL_DACEx_SawtoothWaveGenerate() to generate sawtooth signal.
  53. (+) Use HAL_DACEx_SawtoothWaveDataReset() to reset sawtooth wave.
  54. (+) Use HAL_DACEx_SawtoothWaveDataStep() to step sawtooth wave.
  55. (+) HAL_DACEx_SelfCalibrate to calibrate one DAC channel.
  56. (+) HAL_DACEx_SetUserTrimming to set user trimming value.
  57. (+) HAL_DACEx_GetTrimOffset to retrieve trimming value (factory setting
  58. after reset, user setting if HAL_DACEx_SetUserTrimming have been used
  59. at least one time after reset).
  60. @endverbatim
  61. ******************************************************************************
  62. */
  63. /* Includes ------------------------------------------------------------------*/
  64. #include "stm32g4xx_hal.h"
  65. /** @addtogroup STM32G4xx_HAL_Driver
  66. * @{
  67. */
  68. #ifdef HAL_DAC_MODULE_ENABLED
  69. #if defined(DAC1) || defined(DAC2) || defined(DAC3) ||defined (DAC4)
  70. /** @defgroup DACEx DACEx
  71. * @brief DAC Extended HAL module driver
  72. * @{
  73. */
  74. /* Private typedef -----------------------------------------------------------*/
  75. /* Private define ------------------------------------------------------------*/
  76. /* Delay for DAC minimum trimming time. */
  77. /* Note: minimum time needed between two calibration steps */
  78. /* The delay below is specified under conditions: */
  79. /* - DAC channel output buffer enabled */
  80. /* Literal set to maximum value (refer to device datasheet, */
  81. /* electrical characteristics, parameter "tTRIM"). */
  82. /* Unit: us */
  83. #define DAC_DELAY_TRIM_US (50UL) /*!< Delay for DAC minimum trimming time */
  84. /* Private macro -------------------------------------------------------------*/
  85. /* Private variables ---------------------------------------------------------*/
  86. /* Private function prototypes -----------------------------------------------*/
  87. /* Exported functions --------------------------------------------------------*/
  88. /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
  89. * @{
  90. */
  91. /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions
  92. * @brief Extended IO operation functions
  93. *
  94. @verbatim
  95. ==============================================================================
  96. ##### Extended features functions #####
  97. ==============================================================================
  98. [..] This section provides functions allowing to:
  99. (+) Start conversion.
  100. (+) Stop conversion.
  101. (+) Start conversion and enable DMA transfer.
  102. (+) Stop conversion and disable DMA transfer.
  103. (+) Get result of conversion.
  104. (+) Get result of dual mode conversion.
  105. @endverbatim
  106. * @{
  107. */
  108. /**
  109. * @brief Enables DAC and starts conversion of both channels.
  110. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  111. * the configuration information for the specified DAC.
  112. * @retval HAL status
  113. */
  114. HAL_StatusTypeDef HAL_DACEx_DualStart(DAC_HandleTypeDef *hdac)
  115. {
  116. uint32_t tmp_swtrig = 0UL;
  117. __IO uint32_t wait_loop_index;
  118. /* Check the DAC peripheral handle */
  119. if (hdac == NULL)
  120. {
  121. return HAL_ERROR;
  122. }
  123. /* Check the parameters */
  124. assert_param(IS_DAC_CHANNEL(hdac->Instance, DAC_CHANNEL_2));
  125. /* Process locked */
  126. __HAL_LOCK(hdac);
  127. /* Change DAC state */
  128. hdac->State = HAL_DAC_STATE_BUSY;
  129. /* Enable the Peripheral */
  130. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_1);
  131. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_2);
  132. /* Ensure minimum wait before using peripheral after enabling it */
  133. /* Wait loop initialization and execution */
  134. /* Note: Variable divided by 2 to compensate partially */
  135. /* CPU processing cycles, scaling in us split to not */
  136. /* exceed 32 bits register capacity and handle low frequency. */
  137. wait_loop_index = ((DAC_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  138. while (wait_loop_index != 0UL)
  139. {
  140. wait_loop_index--;
  141. }
  142. /* Check if software trigger enabled */
  143. if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
  144. {
  145. tmp_swtrig |= DAC_SWTRIGR_SWTRIG1;
  146. }
  147. if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (DAC_CHANNEL_2 & 0x10UL)))
  148. {
  149. tmp_swtrig |= DAC_SWTRIGR_SWTRIG2;
  150. }
  151. /* Enable the selected DAC software conversion*/
  152. SET_BIT(hdac->Instance->SWTRIGR, tmp_swtrig);
  153. /* Change DAC state */
  154. hdac->State = HAL_DAC_STATE_READY;
  155. /* Process unlocked */
  156. __HAL_UNLOCK(hdac);
  157. /* Return function status */
  158. return HAL_OK;
  159. }
  160. /**
  161. * @brief Disables DAC and stop conversion of both channels.
  162. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  163. * the configuration information for the specified DAC.
  164. * @retval HAL status
  165. */
  166. HAL_StatusTypeDef HAL_DACEx_DualStop(DAC_HandleTypeDef *hdac)
  167. {
  168. /* Check the DAC peripheral handle */
  169. if (hdac == NULL)
  170. {
  171. return HAL_ERROR;
  172. }
  173. /* Check the parameters */
  174. assert_param(IS_DAC_CHANNEL(hdac->Instance, DAC_CHANNEL_2));
  175. /* Disable the Peripheral */
  176. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_1);
  177. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_2);
  178. /* Change DAC state */
  179. hdac->State = HAL_DAC_STATE_READY;
  180. /* Return function status */
  181. return HAL_OK;
  182. }
  183. /**
  184. * @brief Enables DAC and starts conversion of both channel 1 and 2 of the same DAC.
  185. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  186. * the configuration information for the specified DAC.
  187. * @param Channel The DAC channel that will request data from DMA.
  188. * This parameter can be one of the following values:
  189. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  190. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  191. * @param pData The destination peripheral Buffer address.
  192. * @param Length The length of data to be transferred from memory to DAC peripheral
  193. * @param Alignment Specifies the data alignment for DAC channel.
  194. * This parameter can be one of the following values:
  195. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  196. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  197. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  198. * @retval HAL status
  199. */
  200. HAL_StatusTypeDef HAL_DACEx_DualStart_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel,
  201. const uint32_t *pData, uint32_t Length, uint32_t Alignment)
  202. {
  203. HAL_StatusTypeDef status;
  204. uint32_t tmpreg = 0UL;
  205. __IO uint32_t wait_loop_index;
  206. /* Check the DAC peripheral handle */
  207. if (hdac == NULL)
  208. {
  209. return HAL_ERROR;
  210. }
  211. /* Check the parameters */
  212. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  213. /* Ensure Channel 2 exists for this particular DAC instance */
  214. assert_param(IS_DAC_CHANNEL(hdac->Instance, DAC_CHANNEL_2));
  215. assert_param(IS_DAC_ALIGN(Alignment));
  216. /* Process locked */
  217. __HAL_LOCK(hdac);
  218. /* Change DAC state */
  219. hdac->State = HAL_DAC_STATE_BUSY;
  220. if (Channel == DAC_CHANNEL_1)
  221. {
  222. /* Set the DMA transfer complete callback for channel1 */
  223. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  224. /* Set the DMA half transfer complete callback for channel1 */
  225. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  226. /* Set the DMA error callback for channel1 */
  227. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  228. /* Enable the selected DAC channel1 DMA request */
  229. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  230. }
  231. else
  232. {
  233. /* Set the DMA transfer complete callback for channel2 */
  234. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  235. /* Set the DMA half transfer complete callback for channel2 */
  236. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  237. /* Set the DMA error callback for channel2 */
  238. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  239. /* Enable the selected DAC channel2 DMA request */
  240. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  241. }
  242. switch (Alignment)
  243. {
  244. case DAC_ALIGN_12B_R:
  245. /* Get DHR12R1 address */
  246. tmpreg = (uint32_t)&hdac->Instance->DHR12RD;
  247. break;
  248. case DAC_ALIGN_12B_L:
  249. /* Get DHR12L1 address */
  250. tmpreg = (uint32_t)&hdac->Instance->DHR12LD;
  251. break;
  252. case DAC_ALIGN_8B_R:
  253. /* Get DHR8R1 address */
  254. tmpreg = (uint32_t)&hdac->Instance->DHR8RD;
  255. break;
  256. default:
  257. break;
  258. }
  259. /* Enable the DMA channel */
  260. if (Channel == DAC_CHANNEL_1)
  261. {
  262. /* Enable the DAC DMA underrun interrupt */
  263. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  264. /* Enable the DMA channel */
  265. status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  266. }
  267. else
  268. {
  269. /* Enable the DAC DMA underrun interrupt */
  270. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  271. /* Enable the DMA channel */
  272. status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  273. }
  274. /* Process Unlocked */
  275. __HAL_UNLOCK(hdac);
  276. if (status == HAL_OK)
  277. {
  278. /* Enable the Peripheral */
  279. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_1);
  280. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_2);
  281. /* Ensure minimum wait before using peripheral after enabling it */
  282. /* Wait loop initialization and execution */
  283. /* Note: Variable divided by 2 to compensate partially */
  284. /* CPU processing cycles, scaling in us split to not */
  285. /* exceed 32 bits register capacity and handle low frequency. */
  286. wait_loop_index = ((DAC_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  287. while (wait_loop_index != 0UL)
  288. {
  289. wait_loop_index--;
  290. }
  291. }
  292. else
  293. {
  294. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  295. }
  296. /* Return function status */
  297. return status;
  298. }
  299. /**
  300. * @brief Disables DAC and stop conversion both channel.
  301. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  302. * the configuration information for the specified DAC.
  303. * @param Channel The DAC channel that requests data from DMA.
  304. * This parameter can be one of the following values:
  305. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  306. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  307. * @retval HAL status
  308. */
  309. HAL_StatusTypeDef HAL_DACEx_DualStop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
  310. {
  311. HAL_StatusTypeDef status;
  312. /* Check the DAC peripheral handle */
  313. if (hdac == NULL)
  314. {
  315. return HAL_ERROR;
  316. }
  317. /* Ensure Channel 2 exists for this particular DAC instance */
  318. assert_param(IS_DAC_CHANNEL(hdac->Instance, DAC_CHANNEL_2));
  319. /* Disable the selected DAC channel DMA request */
  320. CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN2 | DAC_CR_DMAEN1);
  321. /* Disable the Peripheral */
  322. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_1);
  323. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_2);
  324. /* Disable the DMA channel */
  325. /* Channel1 is used */
  326. if (Channel == DAC_CHANNEL_1)
  327. {
  328. /* Disable the DMA channel */
  329. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  330. /* Disable the DAC DMA underrun interrupt */
  331. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
  332. }
  333. else
  334. {
  335. /* Disable the DMA channel */
  336. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  337. /* Disable the DAC DMA underrun interrupt */
  338. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
  339. }
  340. /* Check if DMA Channel effectively disabled */
  341. if (status != HAL_OK)
  342. {
  343. /* Update DAC state machine to error */
  344. hdac->State = HAL_DAC_STATE_ERROR;
  345. }
  346. else
  347. {
  348. /* Change DAC state */
  349. hdac->State = HAL_DAC_STATE_READY;
  350. }
  351. /* Return function status */
  352. return status;
  353. }
  354. /**
  355. * @brief Enable or disable the selected DAC channel wave generation.
  356. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  357. * the configuration information for the specified DAC.
  358. * @param Channel The selected DAC channel.
  359. * This parameter can be one of the following values:
  360. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  361. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  362. *
  363. * (1) On this STM32 series, parameter not available on all instances.
  364. * Refer to device datasheet for channels availability.
  365. * @param Amplitude Select max triangle amplitude.
  366. * This parameter can be one of the following values:
  367. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  368. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  369. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  370. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  371. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  372. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  373. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  374. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  375. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  376. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  377. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  378. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  379. * @retval HAL status
  380. */
  381. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  382. {
  383. /* Check the DAC peripheral handle */
  384. if (hdac == NULL)
  385. {
  386. return HAL_ERROR;
  387. }
  388. /* Check the parameters */
  389. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  390. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  391. /* Process locked */
  392. __HAL_LOCK(hdac);
  393. /* Change DAC state */
  394. hdac->State = HAL_DAC_STATE_BUSY;
  395. /* Enable the triangle wave generation for the selected DAC channel */
  396. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
  397. (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
  398. /* Change DAC state */
  399. hdac->State = HAL_DAC_STATE_READY;
  400. /* Process unlocked */
  401. __HAL_UNLOCK(hdac);
  402. /* Return function status */
  403. return HAL_OK;
  404. }
  405. /**
  406. * @brief Enable or disable the selected DAC channel wave generation.
  407. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  408. * the configuration information for the specified DAC.
  409. * @param Channel The selected DAC channel.
  410. * This parameter can be one of the following values:
  411. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  412. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  413. *
  414. * (1) On this STM32 series, parameter not available on all instances.
  415. * Refer to device datasheet for channels availability.
  416. * @param Amplitude Unmask DAC channel LFSR for noise wave generation.
  417. * This parameter can be one of the following values:
  418. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  419. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  420. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  421. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  422. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  423. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  424. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  425. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  426. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  427. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  428. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  429. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  430. * @retval HAL status
  431. */
  432. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  433. {
  434. /* Check the DAC peripheral handle */
  435. if (hdac == NULL)
  436. {
  437. return HAL_ERROR;
  438. }
  439. /* Check the parameters */
  440. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  441. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  442. /* Process locked */
  443. __HAL_LOCK(hdac);
  444. /* Change DAC state */
  445. hdac->State = HAL_DAC_STATE_BUSY;
  446. /* Enable the noise wave generation for the selected DAC channel */
  447. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
  448. (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
  449. /* Change DAC state */
  450. hdac->State = HAL_DAC_STATE_READY;
  451. /* Process unlocked */
  452. __HAL_UNLOCK(hdac);
  453. /* Return function status */
  454. return HAL_OK;
  455. }
  456. /**
  457. * @brief Enable or disable the selected DAC channel sawtooth wave generation.
  458. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  459. * the configuration information for the specified DAC.
  460. * @param Channel The selected DAC channel.
  461. * This parameter can be one of the following values:
  462. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  463. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  464. *
  465. * (1) On this STM32 series, parameter not available on all instances.
  466. * Refer to device datasheet for channels availability.
  467. * @param Polarity polarity to be used for wave generation.
  468. * This parameter can be one of the following values:
  469. * @arg DAC_SAWTOOTH_POLARITY_DECREMENT
  470. * @arg DAC_SAWTOOTH_POLARITY_INCREMENT
  471. * @param ResetData Sawtooth wave reset value.
  472. * Range is from 0 to DAC full range 4095 (0xFFF)
  473. * @param StepData Sawtooth wave step value.
  474. * 12.4 bit format, unsigned: 12 bits exponent / 4 bits mantissa
  475. * Step value step is 1/16 = 0.0625
  476. * Step value range is 0.0000 to 4095.9375 (0xFFF.F)
  477. * @note Sawtooth reset and step triggers are configured by calling @ref HAL_DAC_ConfigChannel
  478. * @retval HAL status
  479. */
  480. HAL_StatusTypeDef HAL_DACEx_SawtoothWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Polarity,
  481. uint32_t ResetData, uint32_t StepData)
  482. {
  483. /* Check the DAC peripheral handle */
  484. if (hdac == NULL)
  485. {
  486. return HAL_ERROR;
  487. }
  488. /* Check the parameters */
  489. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  490. assert_param(IS_DAC_SAWTOOTH_POLARITY(Polarity));
  491. assert_param(IS_DAC_RESET_DATA(ResetData));
  492. assert_param(IS_DAC_STEP_DATA(StepData));
  493. /* Process locked */
  494. __HAL_LOCK(hdac);
  495. /* Change DAC state */
  496. hdac->State = HAL_DAC_STATE_BUSY;
  497. if (Channel == DAC_CHANNEL_1)
  498. {
  499. /* Configure the sawtooth wave generation data parameters */
  500. MODIFY_REG(hdac->Instance->STR1,
  501. DAC_STR1_STINCDATA1 | DAC_STR1_STDIR1 | DAC_STR1_STRSTDATA1,
  502. (StepData << DAC_STR1_STINCDATA1_Pos)
  503. | Polarity
  504. | (ResetData << DAC_STR1_STRSTDATA1_Pos));
  505. }
  506. else
  507. {
  508. /* Configure the sawtooth wave generation data parameters */
  509. MODIFY_REG(hdac->Instance->STR2,
  510. DAC_STR2_STINCDATA2 | DAC_STR2_STDIR2 | DAC_STR2_STRSTDATA2,
  511. (StepData << DAC_STR2_STINCDATA2_Pos)
  512. | Polarity
  513. | (ResetData << DAC_STR2_STRSTDATA2_Pos));
  514. }
  515. /* Enable the sawtooth wave generation for the selected DAC channel */
  516. MODIFY_REG(hdac->Instance->CR,
  517. (DAC_CR_WAVE1) << (Channel & 0x10UL),
  518. (uint32_t)(DAC_CR_WAVE1_1 | DAC_CR_WAVE1_0) << (Channel & 0x10UL));
  519. /* Change DAC state */
  520. hdac->State = HAL_DAC_STATE_READY;
  521. /* Process unlocked */
  522. __HAL_UNLOCK(hdac);
  523. /* Return function status */
  524. return HAL_OK;
  525. }
  526. /**
  527. * @brief Trig sawtooth wave reset
  528. * @note This function allows to reset sawtooth wave in case of SW trigger
  529. * has been configured for this usage.
  530. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  531. * the configuration information for the specified DAC.
  532. * @param Channel The selected DAC channel.
  533. * This parameter can be one of the following values:
  534. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  535. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  536. *
  537. * (1) On this STM32 series, parameter not available on all instances.
  538. * Refer to device datasheet for channels availability.
  539. * @retval HAL status
  540. */
  541. HAL_StatusTypeDef HAL_DACEx_SawtoothWaveDataReset(DAC_HandleTypeDef *hdac, uint32_t Channel)
  542. {
  543. HAL_StatusTypeDef status = HAL_OK;
  544. /* Check the DAC peripheral handle */
  545. if (hdac == NULL)
  546. {
  547. return HAL_ERROR;
  548. }
  549. /* Check the parameters */
  550. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  551. /* Process locked */
  552. __HAL_LOCK(hdac);
  553. if (((hdac->Instance->STMODR >> (Channel & 0x10UL)) & DAC_STMODR_STRSTTRIGSEL1) == 0UL /* SW TRIGGER */)
  554. {
  555. /* Change DAC state */
  556. hdac->State = HAL_DAC_STATE_BUSY;
  557. if (Channel == DAC_CHANNEL_1)
  558. {
  559. /* Enable the selected DAC software conversion */
  560. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
  561. }
  562. else
  563. {
  564. /* Enable the selected DAC software conversion */
  565. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
  566. }
  567. /* Change DAC state */
  568. hdac->State = HAL_DAC_STATE_READY;
  569. }
  570. else
  571. {
  572. status = HAL_ERROR;
  573. }
  574. /* Process unlocked */
  575. __HAL_UNLOCK(hdac);
  576. /* Return function status */
  577. return status;
  578. }
  579. /**
  580. * @brief Trig sawtooth wave step
  581. * @note This function allows to generate step in sawtooth wave in case of
  582. * SW trigger has been configured for this usage.
  583. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  584. * the configuration information for the specified DAC.
  585. * @param Channel The selected DAC channel.
  586. * This parameter can be one of the following values:
  587. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  588. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  589. *
  590. * (1) On this STM32 series, parameter not available on all instances.
  591. * Refer to device datasheet for channels availability.
  592. * @retval HAL status
  593. */
  594. HAL_StatusTypeDef HAL_DACEx_SawtoothWaveDataStep(DAC_HandleTypeDef *hdac, uint32_t Channel)
  595. {
  596. HAL_StatusTypeDef status = HAL_OK;
  597. /* Check the DAC peripheral handle */
  598. if (hdac == NULL)
  599. {
  600. return HAL_ERROR;
  601. }
  602. /* Check the parameters */
  603. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  604. /* Process locked */
  605. __HAL_LOCK(hdac);
  606. if (((hdac->Instance->STMODR >> (Channel & 0x10UL)) & DAC_STMODR_STINCTRIGSEL1) == 0UL /* SW TRIGGER */)
  607. {
  608. /* Change DAC state */
  609. hdac->State = HAL_DAC_STATE_BUSY;
  610. if (Channel == DAC_CHANNEL_1)
  611. {
  612. /* Enable the selected DAC software conversion */
  613. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIGB1);
  614. }
  615. else
  616. {
  617. /* Enable the selected DAC software conversion */
  618. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIGB2);
  619. }
  620. /* Change DAC state */
  621. hdac->State = HAL_DAC_STATE_READY;
  622. }
  623. else
  624. {
  625. status = HAL_ERROR;
  626. }
  627. /* Process unlocked */
  628. __HAL_UNLOCK(hdac);
  629. /* Return function status */
  630. return status;
  631. }
  632. /**
  633. * @brief Set the specified data holding register value for dual DAC channel.
  634. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  635. * the configuration information for the specified DAC.
  636. * @param Alignment Specifies the data alignment for dual channel DAC.
  637. * This parameter can be one of the following values:
  638. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  639. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  640. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  641. * @param Data1 Data for DAC Channel1 to be loaded in the selected data holding register.
  642. * @param Data2 Data for DAC Channel2 to be loaded in the selected data holding register.
  643. * @note In dual mode, a unique register access is required to write in both
  644. * DAC channels at the same time.
  645. * @retval HAL status
  646. */
  647. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  648. {
  649. uint32_t data;
  650. uint32_t tmp;
  651. /* Check the DAC peripheral handle */
  652. if (hdac == NULL)
  653. {
  654. return HAL_ERROR;
  655. }
  656. /* Check the parameters */
  657. assert_param(IS_DAC_ALIGN(Alignment));
  658. assert_param(IS_DAC_DATA(Data1));
  659. assert_param(IS_DAC_DATA(Data2));
  660. /* Calculate and set dual DAC data holding register value */
  661. if (Alignment == DAC_ALIGN_8B_R)
  662. {
  663. data = ((uint32_t)Data2 << 8U) | Data1;
  664. }
  665. else
  666. {
  667. data = ((uint32_t)Data2 << 16U) | Data1;
  668. }
  669. tmp = (uint32_t)hdac->Instance;
  670. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  671. /* Set the dual DAC selected data holding register */
  672. *(__IO uint32_t *)tmp = data;
  673. /* Return function status */
  674. return HAL_OK;
  675. }
  676. /**
  677. * @brief Conversion complete callback in non-blocking mode for Channel2.
  678. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  679. * the configuration information for the specified DAC.
  680. * @retval None
  681. */
  682. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  683. {
  684. /* Prevent unused argument(s) compilation warning */
  685. UNUSED(hdac);
  686. /* NOTE : This function should not be modified, when the callback is needed,
  687. the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
  688. */
  689. }
  690. /**
  691. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel2.
  692. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  693. * the configuration information for the specified DAC.
  694. * @retval None
  695. */
  696. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  697. {
  698. /* Prevent unused argument(s) compilation warning */
  699. UNUSED(hdac);
  700. /* NOTE : This function should not be modified, when the callback is needed,
  701. the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
  702. */
  703. }
  704. /**
  705. * @brief Error DAC callback for Channel2.
  706. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  707. * the configuration information for the specified DAC.
  708. * @retval None
  709. */
  710. __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
  711. {
  712. /* Prevent unused argument(s) compilation warning */
  713. UNUSED(hdac);
  714. /* NOTE : This function should not be modified, when the callback is needed,
  715. the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
  716. */
  717. }
  718. /**
  719. * @brief DMA underrun DAC callback for Channel2.
  720. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  721. * the configuration information for the specified DAC.
  722. * @retval None
  723. */
  724. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  725. {
  726. /* Prevent unused argument(s) compilation warning */
  727. UNUSED(hdac);
  728. /* NOTE : This function should not be modified, when the callback is needed,
  729. the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
  730. */
  731. }
  732. /**
  733. * @brief Run the self calibration of one DAC channel.
  734. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  735. * the configuration information for the specified DAC.
  736. * @param sConfig DAC channel configuration structure.
  737. * @param Channel The selected DAC channel.
  738. * This parameter can be one of the following values:
  739. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  740. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  741. *
  742. * (1) On this STM32 series, parameter not available on all instances.
  743. * Refer to device datasheet for channels availability.
  744. * @retval Updates DAC_TrimmingValue. , DAC_UserTrimming set to DAC_UserTrimming
  745. * @retval HAL status
  746. * @note Calibration runs about 7 ms.
  747. */
  748. HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
  749. {
  750. HAL_StatusTypeDef status = HAL_OK;
  751. uint32_t trimmingvalue;
  752. uint32_t delta;
  753. __IO uint32_t wait_loop_index;
  754. /* store/restore channel configuration structure purpose */
  755. uint32_t oldmodeconfiguration;
  756. /* Check the parameters */
  757. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  758. /* Check the DAC handle allocation */
  759. /* Check if DAC running */
  760. if ((hdac == NULL) || (sConfig == NULL))
  761. {
  762. status = HAL_ERROR;
  763. }
  764. else if (hdac->State == HAL_DAC_STATE_BUSY)
  765. {
  766. status = HAL_ERROR;
  767. }
  768. else
  769. {
  770. /* Process locked */
  771. __HAL_LOCK(hdac);
  772. /* Store configuration */
  773. oldmodeconfiguration = (hdac->Instance->MCR & (DAC_MCR_MODE1 << (Channel & 0x10UL)));
  774. /* Disable the selected DAC channel */
  775. CLEAR_BIT((hdac->Instance->CR), (DAC_CR_EN1 << (Channel & 0x10UL)));
  776. /* Wait for ready bit to be de-asserted */
  777. HAL_Delay(1);
  778. /* Set mode in MCR for calibration */
  779. MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), 0U);
  780. /* Enable the selected DAC channel calibration */
  781. /* i.e. set DAC_CR_CENx bit */
  782. SET_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
  783. /* Init trimming counter */
  784. /* Medium value */
  785. trimmingvalue = 0x10UL;
  786. delta = 0x08UL;
  787. while (delta != 0UL)
  788. {
  789. /* Set candidate trimming */
  790. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  791. /* Wait minimum time needed between two calibration steps (OTRIM) */
  792. /* Wait loop initialization and execution */
  793. /* Note: Variable divided by 2 to compensate partially CPU processing cycles, scaling in us split to not exceed */
  794. /* 32 bits register capacity and handle low frequency. */
  795. wait_loop_index = ((DAC_DELAY_TRIM_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  796. while (wait_loop_index != 0UL)
  797. {
  798. wait_loop_index--;
  799. }
  800. if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL)))
  801. {
  802. /* DAC_SR_CAL_FLAGx is HIGH try higher trimming */
  803. trimmingvalue -= delta;
  804. }
  805. else
  806. {
  807. /* DAC_SR_CAL_FLAGx is LOW try lower trimming */
  808. trimmingvalue += delta;
  809. }
  810. delta >>= 1UL;
  811. }
  812. /* Still need to check if right calibration is current value or one step below */
  813. /* Indeed the first value that causes the DAC_SR_CAL_FLAGx bit to change from 0 to 1 */
  814. /* Set candidate trimming */
  815. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  816. /* Wait minimum time needed between two calibration steps (OTRIM) */
  817. /* Wait loop initialization and execution */
  818. /* Note: Variable divided by 2 to compensate partially CPU processing cycles, scaling in us split to not exceed */
  819. /* 32 bits register capacity and handle low frequency. */
  820. wait_loop_index = ((DAC_DELAY_TRIM_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  821. while (wait_loop_index != 0UL)
  822. {
  823. wait_loop_index--;
  824. }
  825. if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == 0UL)
  826. {
  827. /* Check trimming value below maximum */
  828. if (trimmingvalue < 0x1FU)
  829. {
  830. /* Trimming is actually one value more */
  831. trimmingvalue++;
  832. }
  833. /* Set right trimming */
  834. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  835. }
  836. /* Disable the selected DAC channel calibration */
  837. /* i.e. clear DAC_CR_CENx bit */
  838. CLEAR_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
  839. sConfig->DAC_TrimmingValue = trimmingvalue;
  840. sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
  841. /* Restore configuration */
  842. MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), oldmodeconfiguration);
  843. /* Process unlocked */
  844. __HAL_UNLOCK(hdac);
  845. }
  846. return status;
  847. }
  848. /**
  849. * @brief Set the trimming mode and trimming value (user trimming mode applied).
  850. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  851. * the configuration information for the specified DAC.
  852. * @param sConfig DAC configuration structure updated with new DAC trimming value.
  853. * @param Channel The selected DAC channel.
  854. * This parameter can be one of the following values:
  855. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  856. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  857. *
  858. * (1) On this STM32 series, parameter not available on all instances.
  859. * Refer to device datasheet for channels availability.
  860. * @param NewTrimmingValue DAC new trimming value
  861. * @retval HAL status
  862. */
  863. HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel,
  864. uint32_t NewTrimmingValue)
  865. {
  866. HAL_StatusTypeDef status = HAL_OK;
  867. /* Check the parameters */
  868. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  869. assert_param(IS_DAC_NEWTRIMMINGVALUE(NewTrimmingValue));
  870. /* Check the DAC handle and channel configuration struct allocation */
  871. if ((hdac == NULL) || (sConfig == NULL))
  872. {
  873. status = HAL_ERROR;
  874. }
  875. else
  876. {
  877. /* Process locked */
  878. __HAL_LOCK(hdac);
  879. /* Set new trimming */
  880. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (NewTrimmingValue << (Channel & 0x10UL)));
  881. /* Update trimming mode */
  882. sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
  883. sConfig->DAC_TrimmingValue = NewTrimmingValue;
  884. /* Process unlocked */
  885. __HAL_UNLOCK(hdac);
  886. }
  887. return status;
  888. }
  889. /**
  890. * @brief Return the DAC trimming value.
  891. * @param hdac DAC handle
  892. * @param Channel The selected DAC channel.
  893. * This parameter can be one of the following values:
  894. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  895. * @arg DAC_CHANNEL_2: DAC Channel2 selected (1)
  896. *
  897. * (1) On this STM32 series, parameter not available on all instances.
  898. * Refer to device datasheet for channels availability.
  899. * @retval TrimmingValue Value between Min_Data=0x00 and Max_Data=0x1F
  900. */
  901. uint32_t HAL_DACEx_GetTrimOffset(const DAC_HandleTypeDef *hdac, uint32_t Channel)
  902. {
  903. /* Check the parameter */
  904. assert_param(IS_DAC_CHANNEL(hdac->Instance, Channel));
  905. /* Retrieve trimming */
  906. return ((hdac->Instance->CCR & (DAC_CCR_OTRIM1 << (Channel & 0x10UL))) >> (Channel & 0x10UL));
  907. }
  908. /**
  909. * @}
  910. */
  911. /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions
  912. * @brief Extended Peripheral Control functions
  913. *
  914. @verbatim
  915. ==============================================================================
  916. ##### Peripheral Control functions #####
  917. ==============================================================================
  918. [..] This section provides functions allowing to:
  919. (+) Set the specified data holding register value for DAC channel.
  920. @endverbatim
  921. * @{
  922. */
  923. /**
  924. * @brief Return the last data output value of the selected DAC channel.
  925. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  926. * the configuration information for the specified DAC.
  927. * @retval The selected DAC channel data output value.
  928. */
  929. uint32_t HAL_DACEx_DualGetValue(const DAC_HandleTypeDef *hdac)
  930. {
  931. uint32_t tmp = 0UL;
  932. tmp |= hdac->Instance->DOR1;
  933. tmp |= hdac->Instance->DOR2 << 16UL;
  934. /* Returns the DAC channel data output register value */
  935. return tmp;
  936. }
  937. /**
  938. * @}
  939. */
  940. /**
  941. * @}
  942. */
  943. /* Private functions ---------------------------------------------------------*/
  944. /** @defgroup DACEx_Private_Functions DACEx private functions
  945. * @brief Extended private functions
  946. * @{
  947. */
  948. /**
  949. * @brief DMA conversion complete callback.
  950. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  951. * the configuration information for the specified DMA module.
  952. * @retval None
  953. */
  954. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  955. {
  956. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  957. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  958. hdac->ConvCpltCallbackCh2(hdac);
  959. #else
  960. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  961. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  962. hdac->State = HAL_DAC_STATE_READY;
  963. }
  964. /**
  965. * @brief DMA half transfer complete callback.
  966. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  967. * the configuration information for the specified DMA module.
  968. * @retval None
  969. */
  970. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  971. {
  972. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  973. /* Conversion complete callback */
  974. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  975. hdac->ConvHalfCpltCallbackCh2(hdac);
  976. #else
  977. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  978. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  979. }
  980. /**
  981. * @brief DMA error callback.
  982. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  983. * the configuration information for the specified DMA module.
  984. * @retval None
  985. */
  986. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  987. {
  988. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  989. /* Set DAC error code to DMA error */
  990. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  991. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  992. hdac->ErrorCallbackCh2(hdac);
  993. #else
  994. HAL_DACEx_ErrorCallbackCh2(hdac);
  995. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  996. hdac->State = HAL_DAC_STATE_READY;
  997. }
  998. /**
  999. * @}
  1000. */
  1001. /**
  1002. * @}
  1003. */
  1004. #endif /* DAC1 || DAC2 || DAC3 || DAC4 */
  1005. #endif /* HAL_DAC_MODULE_ENABLED */
  1006. /**
  1007. * @}
  1008. */