stm32g0xx_hal_dac_ex.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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) 2018 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. *** Signal generation operation ***
  27. ===================================
  28. [..]
  29. (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
  30. (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
  31. (+) HAL_DACEx_SelfCalibrate to calibrate one DAC channel.
  32. (+) HAL_DACEx_SetUserTrimming to set user trimming value.
  33. (+) HAL_DACEx_GetTrimOffset to retrieve trimming value (factory setting
  34. after reset, user setting if HAL_DACEx_SetUserTrimming have been used
  35. at least one time after reset).
  36. @endverbatim
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32g0xx_hal.h"
  41. /** @addtogroup STM32G0xx_HAL_Driver
  42. * @{
  43. */
  44. #ifdef HAL_DAC_MODULE_ENABLED
  45. #if defined(DAC1)
  46. /** @defgroup DACEx DACEx
  47. * @brief DAC Extended HAL module driver
  48. * @{
  49. */
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /* Delay for DAC minimum trimming time. */
  53. /* Note: minimum time needed between two calibration steps */
  54. /* The delay below is specified under conditions: */
  55. /* - DAC channel output buffer enabled */
  56. /* Literal set to maximum value (refer to device datasheet, */
  57. /* electrical characteristics, parameter "tTRIM"). */
  58. /* Unit: us */
  59. #define DAC_DELAY_TRIM_US (50UL) /*!< Delay for DAC minimum trimming time */
  60. /* Private macro -------------------------------------------------------------*/
  61. /* Private variables ---------------------------------------------------------*/
  62. /* Private function prototypes -----------------------------------------------*/
  63. /* Exported functions --------------------------------------------------------*/
  64. /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
  65. * @{
  66. */
  67. /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions
  68. * @brief Extended IO operation functions
  69. *
  70. @verbatim
  71. ==============================================================================
  72. ##### Extended features functions #####
  73. ==============================================================================
  74. [..] This section provides functions allowing to:
  75. (+) Start conversion.
  76. (+) Stop conversion.
  77. (+) Start conversion and enable DMA transfer.
  78. (+) Stop conversion and disable DMA transfer.
  79. (+) Get result of conversion.
  80. (+) Get result of dual mode conversion.
  81. @endverbatim
  82. * @{
  83. */
  84. /**
  85. * @brief Enables DAC and starts conversion of both channels.
  86. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  87. * the configuration information for the specified DAC.
  88. * @retval HAL status
  89. */
  90. HAL_StatusTypeDef HAL_DACEx_DualStart(DAC_HandleTypeDef *hdac)
  91. {
  92. uint32_t tmp_swtrig = 0UL;
  93. /* Check the DAC peripheral handle */
  94. if (hdac == NULL)
  95. {
  96. return HAL_ERROR;
  97. }
  98. /* Process locked */
  99. __HAL_LOCK(hdac);
  100. /* Change DAC state */
  101. hdac->State = HAL_DAC_STATE_BUSY;
  102. /* Enable the Peripheral */
  103. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_1);
  104. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_2);
  105. /* Check if software trigger enabled */
  106. if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
  107. {
  108. tmp_swtrig |= DAC_SWTRIGR_SWTRIG1;
  109. }
  110. if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (DAC_CHANNEL_2 & 0x10UL)))
  111. {
  112. tmp_swtrig |= DAC_SWTRIGR_SWTRIG2;
  113. }
  114. /* Enable the selected DAC software conversion*/
  115. SET_BIT(hdac->Instance->SWTRIGR, tmp_swtrig);
  116. /* Change DAC state */
  117. hdac->State = HAL_DAC_STATE_READY;
  118. /* Process unlocked */
  119. __HAL_UNLOCK(hdac);
  120. /* Return function status */
  121. return HAL_OK;
  122. }
  123. /**
  124. * @brief Disables DAC and stop conversion of both channels.
  125. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  126. * the configuration information for the specified DAC.
  127. * @retval HAL status
  128. */
  129. HAL_StatusTypeDef HAL_DACEx_DualStop(DAC_HandleTypeDef *hdac)
  130. {
  131. /* Check the DAC peripheral handle */
  132. if (hdac == NULL)
  133. {
  134. return HAL_ERROR;
  135. }
  136. /* Disable the Peripheral */
  137. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_1);
  138. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_2);
  139. /* Change DAC state */
  140. hdac->State = HAL_DAC_STATE_READY;
  141. /* Return function status */
  142. return HAL_OK;
  143. }
  144. /**
  145. * @brief Enable or disable the selected DAC channel wave generation.
  146. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  147. * the configuration information for the specified DAC.
  148. * @param Channel The selected DAC channel.
  149. * This parameter can be one of the following values:
  150. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  151. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  152. * @param Amplitude Select max triangle amplitude.
  153. * This parameter can be one of the following values:
  154. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  155. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  156. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  157. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  158. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  159. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  160. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  161. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  162. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  163. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  164. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  165. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  166. * @retval HAL status
  167. */
  168. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  169. {
  170. /* Check the DAC peripheral handle */
  171. if (hdac == NULL)
  172. {
  173. return HAL_ERROR;
  174. }
  175. /* Check the parameters */
  176. assert_param(IS_DAC_CHANNEL(Channel));
  177. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  178. /* Process locked */
  179. __HAL_LOCK(hdac);
  180. /* Change DAC state */
  181. hdac->State = HAL_DAC_STATE_BUSY;
  182. /* Enable the triangle wave generation for the selected DAC channel */
  183. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
  184. (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
  185. /* Change DAC state */
  186. hdac->State = HAL_DAC_STATE_READY;
  187. /* Process unlocked */
  188. __HAL_UNLOCK(hdac);
  189. /* Return function status */
  190. return HAL_OK;
  191. }
  192. /**
  193. * @brief Enable or disable the selected DAC channel wave generation.
  194. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  195. * the configuration information for the specified DAC.
  196. * @param Channel The selected DAC channel.
  197. * This parameter can be one of the following values:
  198. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  199. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  200. * @param Amplitude Unmask DAC channel LFSR for noise wave generation.
  201. * This parameter can be one of the following values:
  202. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  203. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  204. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  205. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  206. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  207. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  208. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  209. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  210. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  211. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  212. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  213. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  217. {
  218. /* Check the DAC peripheral handle */
  219. if (hdac == NULL)
  220. {
  221. return HAL_ERROR;
  222. }
  223. /* Check the parameters */
  224. assert_param(IS_DAC_CHANNEL(Channel));
  225. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  226. /* Process locked */
  227. __HAL_LOCK(hdac);
  228. /* Change DAC state */
  229. hdac->State = HAL_DAC_STATE_BUSY;
  230. /* Enable the noise wave generation for the selected DAC channel */
  231. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
  232. (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
  233. /* Change DAC state */
  234. hdac->State = HAL_DAC_STATE_READY;
  235. /* Process unlocked */
  236. __HAL_UNLOCK(hdac);
  237. /* Return function status */
  238. return HAL_OK;
  239. }
  240. /**
  241. * @brief Set the specified data holding register value for dual DAC channel.
  242. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  243. * the configuration information for the specified DAC.
  244. * @param Alignment Specifies the data alignment for dual channel DAC.
  245. * This parameter can be one of the following values:
  246. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  247. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  248. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  249. * @param Data1 Data for DAC Channel1 to be loaded in the selected data holding register.
  250. * @param Data2 Data for DAC Channel2 to be loaded in the selected data holding register.
  251. * @note In dual mode, a unique register access is required to write in both
  252. * DAC channels at the same time.
  253. * @retval HAL status
  254. */
  255. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  256. {
  257. uint32_t data;
  258. uint32_t tmp;
  259. /* Check the DAC peripheral handle */
  260. if (hdac == NULL)
  261. {
  262. return HAL_ERROR;
  263. }
  264. /* Check the parameters */
  265. assert_param(IS_DAC_ALIGN(Alignment));
  266. assert_param(IS_DAC_DATA(Data1));
  267. assert_param(IS_DAC_DATA(Data2));
  268. /* Calculate and set dual DAC data holding register value */
  269. if (Alignment == DAC_ALIGN_8B_R)
  270. {
  271. data = ((uint32_t)Data2 << 8U) | Data1;
  272. }
  273. else
  274. {
  275. data = ((uint32_t)Data2 << 16U) | Data1;
  276. }
  277. tmp = (uint32_t)hdac->Instance;
  278. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  279. /* Set the dual DAC selected data holding register */
  280. *(__IO uint32_t *)tmp = data;
  281. /* Return function status */
  282. return HAL_OK;
  283. }
  284. /**
  285. * @brief Conversion complete callback in non-blocking mode for Channel2.
  286. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  287. * the configuration information for the specified DAC.
  288. * @retval None
  289. */
  290. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  291. {
  292. /* Prevent unused argument(s) compilation warning */
  293. UNUSED(hdac);
  294. /* NOTE : This function should not be modified, when the callback is needed,
  295. the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
  296. */
  297. }
  298. /**
  299. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel2.
  300. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  301. * the configuration information for the specified DAC.
  302. * @retval None
  303. */
  304. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  305. {
  306. /* Prevent unused argument(s) compilation warning */
  307. UNUSED(hdac);
  308. /* NOTE : This function should not be modified, when the callback is needed,
  309. the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
  310. */
  311. }
  312. /**
  313. * @brief Error DAC callback for Channel2.
  314. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  315. * the configuration information for the specified DAC.
  316. * @retval None
  317. */
  318. __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
  319. {
  320. /* Prevent unused argument(s) compilation warning */
  321. UNUSED(hdac);
  322. /* NOTE : This function should not be modified, when the callback is needed,
  323. the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
  324. */
  325. }
  326. /**
  327. * @brief DMA underrun DAC callback for Channel2.
  328. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  329. * the configuration information for the specified DAC.
  330. * @retval None
  331. */
  332. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  333. {
  334. /* Prevent unused argument(s) compilation warning */
  335. UNUSED(hdac);
  336. /* NOTE : This function should not be modified, when the callback is needed,
  337. the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
  338. */
  339. }
  340. /**
  341. * @brief Run the self calibration of one DAC channel.
  342. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  343. * the configuration information for the specified DAC.
  344. * @param sConfig DAC channel configuration structure.
  345. * @param Channel The selected DAC channel.
  346. * This parameter can be one of the following values:
  347. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  348. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  349. * @retval Updates DAC_TrimmingValue. , DAC_UserTrimming set to DAC_UserTrimming
  350. * @retval HAL status
  351. * @note Calibration runs about 7 ms.
  352. */
  353. HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
  354. {
  355. HAL_StatusTypeDef status = HAL_OK;
  356. uint32_t trimmingvalue;
  357. uint32_t delta;
  358. __IO uint32_t wait_loop_index;
  359. /* store/restore channel configuration structure purpose */
  360. uint32_t oldmodeconfiguration;
  361. /* Check the parameters */
  362. assert_param(IS_DAC_CHANNEL(Channel));
  363. /* Check the DAC handle allocation */
  364. /* Check if DAC running */
  365. if ((hdac == NULL) || (sConfig == NULL))
  366. {
  367. status = HAL_ERROR;
  368. }
  369. else if (hdac->State == HAL_DAC_STATE_BUSY)
  370. {
  371. status = HAL_ERROR;
  372. }
  373. else
  374. {
  375. /* Process locked */
  376. __HAL_LOCK(hdac);
  377. /* Store configuration */
  378. oldmodeconfiguration = (hdac->Instance->MCR & (DAC_MCR_MODE1 << (Channel & 0x10UL)));
  379. /* Disable the selected DAC channel */
  380. CLEAR_BIT((hdac->Instance->CR), (DAC_CR_EN1 << (Channel & 0x10UL)));
  381. /* Set mode in MCR for calibration */
  382. MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), 0U);
  383. /* Enable the selected DAC channel calibration */
  384. /* i.e. set DAC_CR_CENx bit */
  385. SET_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
  386. /* Init trimming counter */
  387. /* Medium value */
  388. trimmingvalue = 16UL;
  389. delta = 8UL;
  390. while (delta != 0UL)
  391. {
  392. /* Set candidate trimming */
  393. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  394. /* Wait minimum time needed between two calibration steps (OTRIM) */
  395. /* Wait loop initialization and execution */
  396. /* Note: Variable divided by 2 to compensate partially CPU processing cycles, scaling in us split to not exceed */
  397. /* 32 bits register capacity and handle low frequency. */
  398. wait_loop_index = ((DAC_DELAY_TRIM_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  399. while (wait_loop_index != 0UL)
  400. {
  401. wait_loop_index--;
  402. }
  403. if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL)))
  404. {
  405. /* DAC_SR_CAL_FLAGx is HIGH try higher trimming */
  406. trimmingvalue -= delta;
  407. }
  408. else
  409. {
  410. /* DAC_SR_CAL_FLAGx is LOW try lower trimming */
  411. trimmingvalue += delta;
  412. }
  413. delta >>= 1UL;
  414. }
  415. /* Still need to check if right calibration is current value or one step below */
  416. /* Indeed the first value that causes the DAC_SR_CAL_FLAGx bit to change from 0 to 1 */
  417. /* Set candidate trimming */
  418. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  419. /* Wait minimum time needed between two calibration steps (OTRIM) */
  420. /* Wait loop initialization and execution */
  421. /* Note: Variable divided by 2 to compensate partially CPU processing cycles, scaling in us split to not exceed */
  422. /* 32 bits register capacity and handle low frequency. */
  423. wait_loop_index = ((DAC_DELAY_TRIM_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  424. while (wait_loop_index != 0UL)
  425. {
  426. wait_loop_index--;
  427. }
  428. if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == 0UL)
  429. {
  430. /* Trimming is actually one value more */
  431. trimmingvalue++;
  432. /* Set right trimming */
  433. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  434. }
  435. /* Disable the selected DAC channel calibration */
  436. /* i.e. clear DAC_CR_CENx bit */
  437. CLEAR_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
  438. sConfig->DAC_TrimmingValue = trimmingvalue;
  439. sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
  440. /* Restore configuration */
  441. MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), oldmodeconfiguration);
  442. /* Process unlocked */
  443. __HAL_UNLOCK(hdac);
  444. }
  445. return status;
  446. }
  447. /**
  448. * @brief Set the trimming mode and trimming value (user trimming mode applied).
  449. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  450. * the configuration information for the specified DAC.
  451. * @param sConfig DAC configuration structure updated with new DAC trimming value.
  452. * @param Channel The selected DAC channel.
  453. * This parameter can be one of the following values:
  454. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  455. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  456. * @param NewTrimmingValue DAC new trimming value
  457. * @retval HAL status
  458. */
  459. HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel,
  460. uint32_t NewTrimmingValue)
  461. {
  462. HAL_StatusTypeDef status = HAL_OK;
  463. /* Check the parameters */
  464. assert_param(IS_DAC_CHANNEL(Channel));
  465. assert_param(IS_DAC_NEWTRIMMINGVALUE(NewTrimmingValue));
  466. /* Check the DAC handle and channel configuration struct allocation */
  467. if ((hdac == NULL) || (sConfig == NULL))
  468. {
  469. status = HAL_ERROR;
  470. }
  471. else
  472. {
  473. /* Process locked */
  474. __HAL_LOCK(hdac);
  475. /* Set new trimming */
  476. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (NewTrimmingValue << (Channel & 0x10UL)));
  477. /* Update trimming mode */
  478. sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
  479. sConfig->DAC_TrimmingValue = NewTrimmingValue;
  480. /* Process unlocked */
  481. __HAL_UNLOCK(hdac);
  482. }
  483. return status;
  484. }
  485. /**
  486. * @brief Return the DAC trimming value.
  487. * @param hdac DAC handle
  488. * @param Channel The selected DAC channel.
  489. * This parameter can be one of the following values:
  490. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  491. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  492. * @retval Trimming value : range: 0->31
  493. *
  494. */
  495. uint32_t HAL_DACEx_GetTrimOffset(const DAC_HandleTypeDef *hdac, uint32_t Channel)
  496. {
  497. /* Check the parameter */
  498. assert_param(IS_DAC_CHANNEL(Channel));
  499. /* Retrieve trimming */
  500. return ((hdac->Instance->CCR & (DAC_CCR_OTRIM1 << (Channel & 0x10UL))) >> (Channel & 0x10UL));
  501. }
  502. /**
  503. * @}
  504. */
  505. /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions
  506. * @brief Extended Peripheral Control functions
  507. *
  508. @verbatim
  509. ==============================================================================
  510. ##### Peripheral Control functions #####
  511. ==============================================================================
  512. [..] This section provides functions allowing to:
  513. (+) Set the specified data holding register value for DAC channel.
  514. @endverbatim
  515. * @{
  516. */
  517. /**
  518. * @brief Return the last data output value of the selected DAC channel.
  519. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  520. * the configuration information for the specified DAC.
  521. * @retval The selected DAC channel data output value.
  522. */
  523. uint32_t HAL_DACEx_DualGetValue(const DAC_HandleTypeDef *hdac)
  524. {
  525. uint32_t tmp = 0UL;
  526. tmp |= hdac->Instance->DOR1;
  527. tmp |= hdac->Instance->DOR2 << 16UL;
  528. /* Returns the DAC channel data output register value */
  529. return tmp;
  530. }
  531. /**
  532. * @}
  533. */
  534. /**
  535. * @}
  536. */
  537. /* Private functions ---------------------------------------------------------*/
  538. /** @defgroup DACEx_Private_Functions DACEx private functions
  539. * @brief Extended private functions
  540. * @{
  541. */
  542. /**
  543. * @brief DMA conversion complete callback.
  544. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  545. * the configuration information for the specified DMA module.
  546. * @retval None
  547. */
  548. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  549. {
  550. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  551. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  552. hdac->ConvCpltCallbackCh2(hdac);
  553. #else
  554. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  555. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  556. hdac->State = HAL_DAC_STATE_READY;
  557. }
  558. /**
  559. * @brief DMA half transfer complete callback.
  560. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  561. * the configuration information for the specified DMA module.
  562. * @retval None
  563. */
  564. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  565. {
  566. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  567. /* Conversion complete callback */
  568. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  569. hdac->ConvHalfCpltCallbackCh2(hdac);
  570. #else
  571. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  572. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  573. }
  574. /**
  575. * @brief DMA error callback.
  576. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  577. * the configuration information for the specified DMA module.
  578. * @retval None
  579. */
  580. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  581. {
  582. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  583. /* Set DAC error code to DMA error */
  584. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  585. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  586. hdac->ErrorCallbackCh2(hdac);
  587. #else
  588. HAL_DACEx_ErrorCallbackCh2(hdac);
  589. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  590. hdac->State = HAL_DAC_STATE_READY;
  591. }
  592. /**
  593. * @}
  594. */
  595. /**
  596. * @}
  597. */
  598. #endif /* DAC1 */
  599. #endif /* HAL_DAC_MODULE_ENABLED */
  600. /**
  601. * @}
  602. */