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.
 
 
 

1022 lines
38 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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. STM32L0 devices integrate one or two 12-bit Digital Analog Converters
  23. (i.e. one or 2 channel(s))
  24. 1 channel : STM32L05x STM32L06x devices
  25. 2 channels: STM32L07x STM32L08x devices
  26. When 2 channels are available, the 2 converters (i.e. channel1 & channel2)
  27. can be used independently or simultaneously (dual mode):
  28. (#) DAC channel1 with DAC_OUT1 (PA4) as output
  29. (#) DAC channel2 with DAC_OUT2 (PA5) as output (STM32L07x/STM32L08x only)
  30. (#) Channel1 & channel2 can be used independently or simultaneously in dual mode (STM32L07x/STM32L08x only)
  31. *** DAC Triggers ***
  32. ====================
  33. [..]
  34. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  35. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  36. [..]
  37. Digital to Analog conversion can be triggered by:
  38. (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
  39. The used pin (GPIOx_PIN_9) must be configured in input mode.
  40. (#) Timers TRGO:
  41. STM32L05x/STM32L06x : TIM2, TIM6 and TIM21
  42. STM32L07x/STM32L08x : TIM2, TIM3, TIM6, TIM7 and TIM21
  43. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T6_TRGO...)
  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 wave generation feature ***
  57. ===================================
  58. [..]
  59. Both DAC channels can be used to generate
  60. (#) Noise wave using HAL_DACEx_NoiseWaveGenerate()
  61. (#) Triangle wave using HAL_DACEx_TriangleWaveGenerate()
  62. *** DAC data format ***
  63. =======================
  64. [..]
  65. The DAC data format can be:
  66. (#) 8-bit right alignment using DAC_ALIGN_8B_R
  67. (#) 12-bit left alignment using DAC_ALIGN_12B_L
  68. (#) 12-bit right alignment using DAC_ALIGN_12B_R
  69. *** DAC data value to voltage correspondence ***
  70. ================================================
  71. [..]
  72. The analog output voltage on each DAC channel pin is determined
  73. by the following equation:
  74. [..]
  75. DAC_OUTx = VREF+ * DOR / 4095
  76. (+) with DOR is the Data Output Register
  77. [..]
  78. VEF+ is the input voltage reference (refer to the device datasheet)
  79. [..]
  80. e.g. To set DAC_OUT1 to 0.7V, use
  81. (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  82. *** DMA requests ***
  83. =====================
  84. [..]
  85. A DMA1 request can be generated when an external trigger (but not a software trigger)
  86. occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
  87. DMA1 requests are mapped as following:
  88. (#) DAC channel1 : mapped on DMA1 Request9 channel2 which must be
  89. already configured
  90. (#) DAC channel2 : mapped on DMA1 Request15 channel4 which must be
  91. already configured (STM32L07x/STM32L08x only)
  92. -@- For Dual mode (STM32L07x/STM32L08x only) and specific signal (Triangle and noise) generation please
  93. refer to Extension Features Driver description
  94. ##### How to use this driver #####
  95. ==============================================================================
  96. [..]
  97. (+) DAC APB clock must be enabled to get write access to DAC
  98. registers using HAL_DAC_Init()
  99. (+) Configure DAC_OUT1: PA4 in analog mode.
  100. (+) Configure DAC_OUT2: PA5 in analog mode (STM32L07x/STM32L08x only).
  101. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  102. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
  103. *** Polling mode IO operation ***
  104. =================================
  105. [..]
  106. (+) Start the DAC peripheral using HAL_DAC_Start()
  107. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  108. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  109. *** DMA mode IO operation ***
  110. ==============================
  111. [..]
  112. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  113. of data to be transferred at each end of conversion
  114. (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1()or HAL_DAC_ConvHalfCpltCallbackCh2()
  115. function is executed and user can add his own code by customization of function pointer
  116. HAL_DAC_ConvHalfCpltCallbackCh1 or HAL_DAC_ConvHalfCpltCallbackCh2
  117. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
  118. function is executed and user can add his own code by customization of function pointer
  119. HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
  120. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
  121. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  122. (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
  123. HAL_DAC_DMAUnderrunCallbackCh1()or HAL_DAC_DMAUnderrunCallbackCh2()
  124. function is executed and user can add his own code by customization of function pointer
  125. HAL_DAC_DMAUnderrunCallbackCh1 or HAL_DAC_DMAUnderrunCallbackCh2
  126. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  127. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  128. *** Callback registration ***
  129. =============================================
  130. [..]
  131. The compilation define USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
  132. allows the user to configure dynamically the driver callbacks.
  133. Use Functions @ref HAL_DAC_RegisterCallback() to register a user callback,
  134. it allows to register following callbacks:
  135. (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
  136. (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
  137. (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
  138. (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
  139. (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
  140. (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
  141. (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
  142. (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
  143. (+) MspInitCallback : DAC MspInit.
  144. (+) MspDeInitCallback : DAC MspdeInit.
  145. This function takes as parameters the HAL peripheral handle, the Callback ID
  146. and a pointer to the user callback function.
  147. Use function @ref HAL_DAC_UnRegisterCallback() to reset a callback to the default
  148. weak (surcharged) function. It allows to reset following callbacks:
  149. (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
  150. (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
  151. (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
  152. (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
  153. (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
  154. (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
  155. (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
  156. (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
  157. (+) MspInitCallback : DAC MspInit.
  158. (+) MspDeInitCallback : DAC MspdeInit.
  159. (+) All Callbacks
  160. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  161. By default, after the @ref HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
  162. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  163. Exception done for MspInit and MspDeInit callbacks that are respectively
  164. reset to the legacy weak (surcharged) functions in the @ref HAL_DAC_Init
  165. and @ref HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
  166. If not, MspInit or MspDeInit are not null, the @ref HAL_DAC_Init and @ref HAL_DAC_DeInit
  167. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  168. Callbacks can be registered/unregistered in READY state only.
  169. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  170. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  171. during the Init/DeInit.
  172. In that case first register the MspInit/MspDeInit user callbacks
  173. using @ref HAL_DAC_RegisterCallback before calling @ref HAL_DAC_DeInit
  174. or @ref HAL_DAC_Init function.
  175. When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
  176. not defined, the callback registering feature is not available
  177. and weak (surcharged) callbacks are used.
  178. *** DAC HAL driver macros list ***
  179. =============================================
  180. [..]
  181. Below the list of most used macros in DAC HAL driver.
  182. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
  183. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
  184. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
  185. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
  186. [..]
  187. (@) You can refer to the DAC HAL driver header file for more useful macros
  188. @endverbatim
  189. ******************************************************************************
  190. * @attention
  191. *
  192. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  193. * All rights reserved.</center></h2>
  194. *
  195. * This software component is licensed by ST under BSD 3-Clause license,
  196. * the "License"; You may not use this file except in compliance with the
  197. * License. You may obtain a copy of the License at:
  198. * opensource.org/licenses/BSD-3-Clause
  199. *
  200. ******************************************************************************
  201. */
  202. #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4) && !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined (STM32L051xx) && !defined (STM32L061xx) && !defined (STM32L071xx) && !defined (STM32L081xx)
  203. /* Includes ------------------------------------------------------------------*/
  204. #include "stm32l0xx_hal.h"
  205. #ifdef HAL_DAC_MODULE_ENABLED
  206. /** @addtogroup STM32L0xx_HAL_Driver
  207. * @{
  208. */
  209. /** @addtogroup DAC
  210. * @brief DAC driver modules
  211. * @{
  212. */
  213. /* Private typedef -----------------------------------------------------------*/
  214. /* Private define ------------------------------------------------------------*/
  215. /* Private macro -------------------------------------------------------------*/
  216. /* Private variables ---------------------------------------------------------*/
  217. /* Private function prototypes -----------------------------------------------*/
  218. /* Private functions ---------------------------------------------------------*/
  219. /** @addtogroup DAC_Exported_Functions
  220. * @{
  221. */
  222. /** @addtogroup DAC_Exported_Functions_Group1
  223. * @brief Initialization and Configuration functions
  224. *
  225. @verbatim
  226. ==============================================================================
  227. ##### Initialization and de-initialization functions #####
  228. ==============================================================================
  229. [..] This section provides functions allowing to:
  230. (+) Initialize and configure the DAC.
  231. (+) De-initialize the DAC.
  232. @endverbatim
  233. * @{
  234. */
  235. /**
  236. * @brief Initialize the DAC peripheral according to the specified parameters
  237. * in the DAC_InitStruct and initialize the associated handle.
  238. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  239. * the configuration information for the specified DAC.
  240. * @retval HAL status
  241. */
  242. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
  243. {
  244. /* Check DAC handle */
  245. if(hdac == NULL)
  246. {
  247. return HAL_ERROR;
  248. }
  249. /* Check the parameters */
  250. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  251. if(hdac->State == HAL_DAC_STATE_RESET)
  252. {
  253. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  254. /* Init the DAC Callback settings */
  255. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  256. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  257. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  258. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  259. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  260. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  261. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  262. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  263. if(hdac->MspInitCallback == NULL)
  264. {
  265. hdac->MspInitCallback = HAL_DAC_MspInit;
  266. }
  267. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  268. /* Allocate lock resource and initialize it */
  269. hdac->Lock = HAL_UNLOCKED;
  270. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  271. /* Init the low level hardware */
  272. hdac->MspInitCallback(hdac);
  273. #else
  274. /* Init the low level hardware */
  275. HAL_DAC_MspInit(hdac);
  276. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  277. }
  278. /* Initialize the DAC state*/
  279. hdac->State = HAL_DAC_STATE_BUSY;
  280. /* Set DAC error code to none */
  281. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  282. /* Initialize the DAC state*/
  283. hdac->State = HAL_DAC_STATE_READY;
  284. /* Return function status */
  285. return HAL_OK;
  286. }
  287. /**
  288. * @brief Deinitialize the DAC peripheral registers to their default reset values.
  289. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  290. * the configuration information for the specified DAC.
  291. * @retval HAL status
  292. */
  293. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  294. {
  295. /* Check DAC handle */
  296. if(hdac == NULL)
  297. {
  298. return HAL_ERROR;
  299. }
  300. /* Check the parameters */
  301. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  302. /* Change DAC state */
  303. hdac->State = HAL_DAC_STATE_BUSY;
  304. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  305. if(hdac->MspDeInitCallback == NULL)
  306. {
  307. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  308. }
  309. /* DeInit the low level hardware */
  310. hdac->MspDeInitCallback(hdac);
  311. #else
  312. /* DeInit the low level hardware */
  313. HAL_DAC_MspDeInit(hdac);
  314. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  315. /* Set DAC error code to none */
  316. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  317. /* Change DAC state */
  318. hdac->State = HAL_DAC_STATE_RESET;
  319. /* Release Lock */
  320. __HAL_UNLOCK(hdac);
  321. /* Return function status */
  322. return HAL_OK;
  323. }
  324. /**
  325. * @brief Initialize the DAC MSP.
  326. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  327. * the configuration information for the specified DAC.
  328. * @retval None
  329. */
  330. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
  331. {
  332. /* Prevent unused argument(s) compilation warning */
  333. UNUSED(hdac);
  334. /* NOTE : This function should not be modified, when the callback is needed,
  335. the HAL_DAC_MspInit could be implemented in the user file
  336. */
  337. }
  338. /**
  339. * @brief DeInitialize the DAC MSP.
  340. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  341. * the configuration information for the specified DAC.
  342. * @retval None
  343. */
  344. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
  345. {
  346. /* Prevent unused argument(s) compilation warning */
  347. UNUSED(hdac);
  348. /* NOTE : This function should not be modified, when the callback is needed,
  349. the HAL_DAC_MspDeInit could be implemented in the user file
  350. */
  351. }
  352. /**
  353. * @}
  354. */
  355. /** @addtogroup DAC_Exported_Functions_Group2
  356. * @brief IO operation functions
  357. *
  358. @verbatim
  359. ==============================================================================
  360. ##### IO operation functions #####
  361. ==============================================================================
  362. [..] This section provides functions allowing to:
  363. (+) Start conversion.
  364. (+) Stop conversion.
  365. (+) Start conversion and enable DMA transfer.
  366. (+) Stop conversion and disable DMA transfer.
  367. (+) Get result of conversion.
  368. (+) Get result of dual mode conversion (STM32L07xx/STM32L08xx only)
  369. @endverbatim
  370. * @{
  371. */
  372. /**
  373. * @brief Enables DAC and starts conversion of channel.
  374. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  375. * the configuration information for the specified DAC.
  376. * @param Channel The selected DAC channel.
  377. * This parameter can be one of the following values:
  378. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  379. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  380. * @retval HAL status
  381. */
  382. __weak HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
  383. {
  384. /* Prevent unused argument(s) compilation warning */
  385. UNUSED(hdac);
  386. UNUSED(Channel);
  387. /* Note : This function is defined into this file for library reference. */
  388. /* Function content is located into file stm32l0xx_hal_dac_ex.c */
  389. /* Return function status */
  390. return HAL_OK;
  391. }
  392. /**
  393. * @brief Disables DAC and stop conversion of channel.
  394. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  395. * the configuration information for the specified DAC.
  396. * @param Channel The selected DAC channel.
  397. * This parameter can be one of the following values:
  398. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  399. * @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
  400. * @retval HAL status
  401. */
  402. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  403. {
  404. /* Check the parameters */
  405. assert_param(IS_DAC_CHANNEL(Channel));
  406. /* Disable the Peripheral */
  407. __HAL_DAC_DISABLE(hdac, Channel);
  408. /* Change DAC state */
  409. hdac->State = HAL_DAC_STATE_READY;
  410. /* Return function status */
  411. return HAL_OK;
  412. }
  413. /**
  414. * @brief Enables DAC and starts conversion of channel using DMA.
  415. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  416. * the configuration information for the specified DAC.
  417. * @param Channel The selected DAC channel.
  418. * This parameter can be one of the following values:
  419. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  420. * @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
  421. * @param pData The destination peripheral Buffer address.
  422. * @param Length The length of data to be transferred from memory to DAC peripheral
  423. * @param Alignment Specifies the data alignment for DAC channel.
  424. * This parameter can be one of the following values:
  425. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  426. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  427. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  428. * @retval HAL status
  429. */
  430. __weak HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  431. {
  432. /* Prevent unused argument(s) compilation warning */
  433. UNUSED(hdac);
  434. UNUSED(Channel);
  435. UNUSED(pData);
  436. UNUSED(Length);
  437. UNUSED(Alignment);
  438. /* Note : This function is defined into this file for library reference. */
  439. /* Function content is located into file stm32l0xx_hal_dac_ex.c */
  440. /* Return function status */
  441. return HAL_OK;
  442. }
  443. /**
  444. * @brief Disables DAC and stop conversion of channel.
  445. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  446. * the configuration information for the specified DAC.
  447. * @param Channel The selected DAC channel.
  448. * This parameter can be one of the following values:
  449. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  450. * @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
  451. * @retval HAL status
  452. */
  453. __weak HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  454. {
  455. /* Prevent unused argument(s) compilation warning */
  456. UNUSED(hdac);
  457. UNUSED(Channel);
  458. /* Note : This function is defined into this file for library reference. */
  459. /* Function content is located into file stm32l0xx_hal_dac_ex.c */
  460. /* Return function status */
  461. return HAL_OK;
  462. }
  463. /**
  464. * @brief Returns the last data output value of the selected DAC channel.
  465. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  466. * the configuration information for the specified DAC.
  467. * @param Channel The selected DAC channel.
  468. * This parameter can be one of the following values:
  469. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  470. * @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
  471. * @retval The selected DAC channel data output value.
  472. */
  473. __weak uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  474. {
  475. /* Prevent unused argument(s) compilation warning */
  476. UNUSED(hdac);
  477. UNUSED(Channel);
  478. /* Note : This function is defined into this file for library reference. */
  479. /* Function content is located into file stm32l0xx_hal_dac_ex.c */
  480. /* Return function status */
  481. return 0U;
  482. }
  483. /**
  484. * @brief Handles DAC interrupt request
  485. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  486. * the configuration information for the specified DAC.
  487. * @retval None
  488. */
  489. __weak void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
  490. {
  491. /* Prevent unused argument(s) compilation warning */
  492. UNUSED(hdac);
  493. /* Note : This function is defined into this file for library reference. */
  494. /* Function content is located into file stm32l0xx_hal_dac_ex.c */
  495. }
  496. /**
  497. * @brief Conversion complete callback in non-blocking mode for Channel1
  498. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  499. * the configuration information for the specified DAC.
  500. * @retval None
  501. */
  502. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  503. {
  504. /* Prevent unused argument(s) compilation warning */
  505. UNUSED(hdac);
  506. /* NOTE : This function should not be modified, when the callback is needed,
  507. the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
  508. */
  509. }
  510. /**
  511. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel1
  512. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  513. * the configuration information for the specified DAC.
  514. * @retval None
  515. */
  516. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  517. {
  518. /* Prevent unused argument(s) compilation warning */
  519. UNUSED(hdac);
  520. /* NOTE : This function should not be modified, when the callback is needed,
  521. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  522. */
  523. }
  524. /**
  525. * @brief Error DAC callback for Channel1.
  526. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  527. * the configuration information for the specified DAC.
  528. * @retval None
  529. */
  530. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  531. {
  532. /* Prevent unused argument(s) compilation warning */
  533. UNUSED(hdac);
  534. /* NOTE : This function should not be modified, when the callback is needed,
  535. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  536. */
  537. }
  538. /**
  539. * @brief DMA underrun DAC callback for channel1.
  540. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  541. * the configuration information for the specified DAC.
  542. * @retval None
  543. */
  544. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  545. {
  546. /* Prevent unused argument(s) compilation warning */
  547. UNUSED(hdac);
  548. /* NOTE : This function should not be modified, when the callback is needed,
  549. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  550. */
  551. }
  552. /**
  553. * @}
  554. */
  555. /** @addtogroup DAC_Exported_Functions_Group3
  556. * @brief Peripheral Control functions
  557. *
  558. @verbatim
  559. ==============================================================================
  560. ##### Peripheral Control functions #####
  561. ==============================================================================
  562. [..] This section provides functions allowing to:
  563. (+) Configure channels.
  564. (+) Set the specified data holding register value for DAC channel.
  565. @endverbatim
  566. * @{
  567. */
  568. /**
  569. * @brief Configures the selected DAC channel.
  570. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  571. * the configuration information for the specified DAC.
  572. * @param sConfig DAC configuration structure.
  573. * @param Channel The selected DAC channel.
  574. * This parameter can be one of the following values:
  575. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  576. * @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
  577. * @retval HAL status
  578. */
  579. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  580. {
  581. uint32_t tmpreg1 = 0U, tmpreg2 = 0U;
  582. /* Check the DAC parameters */
  583. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  584. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  585. assert_param(IS_DAC_CHANNEL(Channel));
  586. /* Process locked */
  587. __HAL_LOCK(hdac);
  588. /* Change DAC state */
  589. hdac->State = HAL_DAC_STATE_BUSY;
  590. /* Get the DAC CR value */
  591. tmpreg1 = hdac->Instance->CR;
  592. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  593. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
  594. /* Configure for the selected DAC channel: buffer output, trigger */
  595. /* Set TSELx and TENx bits according to DAC_Trigger value */
  596. /* Set BOFFx bit according to DAC_OutputBuffer value */
  597. tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
  598. /* Calculate CR register value depending on DAC_Channel */
  599. tmpreg1 |= tmpreg2 << Channel;
  600. /* Write to DAC CR */
  601. hdac->Instance->CR = tmpreg1;
  602. /* Disable wave generation */
  603. CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << Channel));
  604. /* Change DAC state */
  605. hdac->State = HAL_DAC_STATE_READY;
  606. /* Process unlocked */
  607. __HAL_UNLOCK(hdac);
  608. /* Return function status */
  609. return HAL_OK;
  610. }
  611. /**
  612. * @}
  613. */
  614. /** @addtogroup DAC_Exported_Functions_Group4
  615. * @brief Peripheral State and Errors functions
  616. *
  617. @verbatim
  618. ==============================================================================
  619. ##### Peripheral State and Errors functions #####
  620. ==============================================================================
  621. [..]
  622. This subsection provides functions allowing to
  623. (+) Check the DAC state.
  624. (+) Check the DAC Errors.
  625. @endverbatim
  626. * @{
  627. */
  628. /**
  629. * @brief return the DAC handle state
  630. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  631. * the configuration information for the specified DAC.
  632. * @retval HAL state
  633. */
  634. HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
  635. {
  636. /* Return DAC handle state */
  637. return hdac->State;
  638. }
  639. /**
  640. * @brief Return the DAC error code
  641. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  642. * the configuration information for the specified DAC.
  643. * @retval DAC Error Code
  644. */
  645. uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
  646. {
  647. return hdac->ErrorCode;
  648. }
  649. /**
  650. * @brief Set the specified data holding register value for DAC channel.
  651. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  652. * the configuration information for the specified DAC.
  653. * @param Channel The selected DAC channel.
  654. * This parameter can be one of the following values:
  655. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  656. * @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
  657. * @param Alignment Specifies the data alignment.
  658. * This parameter can be one of the following values:
  659. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  660. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  661. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  662. * @param Data Data to be loaded in the selected data holding register.
  663. * @retval HAL status
  664. */
  665. __weak HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  666. {
  667. /* Prevent unused argument(s) compilation warning */
  668. UNUSED(hdac);
  669. UNUSED(Channel);
  670. UNUSED(Alignment);
  671. UNUSED(Data);
  672. /* Note : This function is defined into this file for library reference. */
  673. /* Function content is located into file stm32l0xx_hal_dac_ex.c */
  674. /* Return function status */
  675. return HAL_OK;
  676. }
  677. /**
  678. * @}
  679. */
  680. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  681. /**
  682. * @brief Register a User DAC Callback
  683. * To be used instead of the weak (surcharged) predefined callback
  684. * @param hdac DAC handle
  685. * @param CallbackID ID of the callback to be registered
  686. * This parameter can be one of the following values:
  687. * @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK DAC Error Callback ID
  688. * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 Complete Callback ID
  689. * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
  690. * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
  691. * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
  692. * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
  693. * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
  694. * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
  695. * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
  696. * @arg @ref HAL_DAC_MSP_INIT_CB_ID DAC MSP Init Callback ID
  697. * @arg @ref HAL_DAC_MSP_DEINIT_CB_ID DAC MSP DeInit Callback ID
  698. *
  699. * @param pCallback pointer to the Callback function
  700. * @retval status
  701. */
  702. HAL_StatusTypeDef HAL_DAC_RegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackId, pDAC_CallbackTypeDef pCallback)
  703. {
  704. HAL_StatusTypeDef status = HAL_OK;
  705. if(pCallback == NULL)
  706. {
  707. /* Update the error code */
  708. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  709. return HAL_ERROR;
  710. }
  711. /* Process locked */
  712. __HAL_LOCK(hdac);
  713. if(hdac->State == HAL_DAC_STATE_READY)
  714. {
  715. switch (CallbackId)
  716. {
  717. case HAL_DAC_CH1_COMPLETE_CB_ID :
  718. hdac->ConvCpltCallbackCh1 = pCallback;
  719. break;
  720. case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
  721. hdac->ConvHalfCpltCallbackCh1 = pCallback;
  722. break;
  723. case HAL_DAC_CH1_ERROR_ID :
  724. hdac->ErrorCallbackCh1 = pCallback;
  725. break;
  726. case HAL_DAC_CH1_UNDERRUN_CB_ID :
  727. hdac->DMAUnderrunCallbackCh1 = pCallback;
  728. break;
  729. case HAL_DAC_CH2_COMPLETE_CB_ID :
  730. hdac->ConvCpltCallbackCh2 = pCallback;
  731. break;
  732. case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
  733. hdac->ConvHalfCpltCallbackCh2 = pCallback;
  734. break;
  735. case HAL_DAC_CH2_ERROR_ID :
  736. hdac->ErrorCallbackCh2 = pCallback;
  737. break;
  738. case HAL_DAC_CH2_UNDERRUN_CB_ID :
  739. hdac->DMAUnderrunCallbackCh2 = pCallback;
  740. break;
  741. case HAL_DAC_MSP_INIT_CB_ID :
  742. hdac->MspInitCallback = pCallback;
  743. break;
  744. case HAL_DAC_MSP_DEINIT_CB_ID :
  745. hdac->MspDeInitCallback = pCallback;
  746. break;
  747. default :
  748. /* Update the error code */
  749. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  750. /* update return status */
  751. status = HAL_ERROR;
  752. break;
  753. }
  754. }
  755. else if (hdac->State == HAL_DAC_STATE_RESET)
  756. {
  757. switch (CallbackId)
  758. {
  759. case HAL_DAC_MSP_INIT_CB_ID :
  760. hdac->MspInitCallback = pCallback;
  761. break;
  762. case HAL_DAC_MSP_DEINIT_CB_ID :
  763. hdac->MspDeInitCallback = pCallback;
  764. break;
  765. default :
  766. /* Update the error code */
  767. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  768. /* update return status */
  769. status = HAL_ERROR;
  770. break;
  771. }
  772. }
  773. else
  774. {
  775. /* Update the error code */
  776. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  777. /* update return status */
  778. status = HAL_ERROR;
  779. }
  780. /* Release Lock */
  781. __HAL_UNLOCK(hdac);
  782. return status;
  783. }
  784. /**
  785. * @brief Unregister a User DAC Callback
  786. * DAC Callback is redirected to the weak (surcharged) predefined callback
  787. * @param hdac DAC handle
  788. * @param CallbackID ID of the callback to be unregistered
  789. * This parameter can be one of the following values:
  790. * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 tranfer Complete Callback ID
  791. * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
  792. * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
  793. * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
  794. * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
  795. * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
  796. * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
  797. * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
  798. * @arg @ref HAL_DAC_MSP_INIT_CB_ID DAC MSP Init Callback ID
  799. * @arg @ref HAL_DAC_MSP_DEINIT_CB_ID DAC MSP DeInit Callback ID
  800. * @arg @ref HAL_DAC_ALL_CB_ID DAC All callbacks
  801. * @retval status
  802. */
  803. HAL_StatusTypeDef HAL_DAC_UnRegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
  804. {
  805. HAL_StatusTypeDef status = HAL_OK;
  806. /* Process locked */
  807. __HAL_LOCK(hdac);
  808. if(hdac->State == HAL_DAC_STATE_READY)
  809. {
  810. switch (CallbackID)
  811. {
  812. case HAL_DAC_CH1_COMPLETE_CB_ID :
  813. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  814. break;
  815. case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
  816. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  817. break;
  818. case HAL_DAC_CH1_ERROR_ID :
  819. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  820. break;
  821. case HAL_DAC_CH1_UNDERRUN_CB_ID :
  822. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  823. break;
  824. case HAL_DAC_CH2_COMPLETE_CB_ID :
  825. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  826. break;
  827. case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
  828. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  829. break;
  830. case HAL_DAC_CH2_ERROR_ID :
  831. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  832. break;
  833. case HAL_DAC_CH2_UNDERRUN_CB_ID :
  834. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  835. break;
  836. case HAL_DAC_MSP_INIT_CB_ID :
  837. hdac->MspInitCallback = HAL_DAC_MspInit;
  838. break;
  839. case HAL_DAC_MSP_DEINIT_CB_ID :
  840. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  841. break;
  842. case HAL_DAC_ALL_CB_ID :
  843. hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
  844. hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
  845. hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
  846. hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
  847. hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
  848. hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
  849. hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
  850. hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
  851. hdac->MspInitCallback = HAL_DAC_MspInit;
  852. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  853. break;
  854. default :
  855. /* Update the error code */
  856. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  857. /* update return status */
  858. status = HAL_ERROR;
  859. break;
  860. }
  861. }
  862. else if (hdac->State == HAL_DAC_STATE_RESET)
  863. {
  864. switch (CallbackID)
  865. {
  866. case HAL_DAC_MSP_INIT_CB_ID :
  867. hdac->MspInitCallback = HAL_DAC_MspInit;
  868. break;
  869. case HAL_DAC_MSP_DEINIT_CB_ID :
  870. hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
  871. break;
  872. default :
  873. /* Update the error code */
  874. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  875. /* update return status */
  876. status = HAL_ERROR;
  877. break;
  878. }
  879. }
  880. else
  881. {
  882. /* Update the error code */
  883. hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
  884. /* update return status */
  885. status = HAL_ERROR;
  886. }
  887. /* Release Lock */
  888. __HAL_UNLOCK(hdac);
  889. return status;
  890. }
  891. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  892. /**
  893. * @}
  894. */
  895. /**
  896. * @}
  897. */
  898. /**
  899. * @}
  900. */
  901. #endif /* HAL_DAC_MODULE_ENABLED */
  902. #endif /* !STM32L010xB && !STM32L010x8 && !STM32L010x6 && !STM32L010x4 && !STM32L011xx && !STM32L021xx && !STM32L031xx && !STM32L041xx && !STM32L051xx && !STM32L061xx&& !STM32L071xx&& !STM32L081xx */
  903. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/