You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1535 lines
52 KiB

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