stm32l4xx_hal_dac_ex.c 25 KB

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