stm32l4xx_hal_dac_ex.c 26 KB

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