1
0

stm32l4xx_hal_dac.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_dac.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief DAC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Digital to Analog Converter (DAC) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State and Errors functions
  14. *
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### DAC Peripheral features #####
  19. ==============================================================================
  20. [..]
  21. *** DAC Channels ***
  22. ====================
  23. [..]
  24. STM32L4 devices integrate one or two 12-bit Digital Analog Converters
  25. (i.e. one or 2 channel(s))
  26. 1 channel : STM32L451xx STM32L452xx STM32L462xx
  27. 2 channels: STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx
  28. STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx
  29. When 2 channels are available, the 2 converters (i.e. channel1 & channel2)
  30. can be used independently or simultaneously (dual mode):
  31. (#) DAC channel1 with DAC_OUT1 (PA4) as output or connected to on-chip
  32. peripherals (ex. OPAMPs, comparators).
  33. (#) Whenever present, DAC channel2 with DAC_OUT2 (PA5) as output
  34. or connected to on-chip peripherals (ex. OPAMPs, comparators).
  35. *** DAC Triggers ***
  36. ====================
  37. [..]
  38. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  39. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  40. [..]
  41. Digital to Analog conversion can be triggered by:
  42. (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
  43. The used pin (GPIOx_PIN_9) must be configured in input mode.
  44. (#) Timers TRGO: TIM2, TIM3, TIM4, TIM5, TIM6 and TIM7
  45. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T3_TRGO...)
  46. (#) Software using DAC_TRIGGER_SOFTWARE
  47. *** DAC Buffer mode feature ***
  48. ===============================
  49. [..]
  50. Each DAC channel integrates an output buffer that can be used to
  51. reduce the output impedance, and to drive external loads directly
  52. without having to add an external operational amplifier.
  53. To enable, the output buffer use
  54. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  55. [..]
  56. (@) Refer to the device datasheet for more details about output
  57. impedance value with and without output buffer.
  58. *** DAC connect feature ***
  59. ===============================
  60. [..]
  61. Each DAC channel can be connected internally.
  62. To connect, use
  63. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
  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. VEF+ 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 DMA1 request can be generated when an external trigger (but not a software trigger)
  144. occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
  145. DMA requests are mapped as following:
  146. (#) DAC channel1: mapped either on
  147. (++) DMA1 request 6 channel3
  148. (++) or DMA2 request channel4 which must be already configured
  149. (#) DAC channel2 (whenever present): mapped either on
  150. (++) DMA1 request 5 channel4
  151. (++) or DMA2 request 3 channel5 which must be already configured
  152. [..]
  153. (@) For Dual mode and specific signal (Triangle and noise) generation please
  154. refer to Extended Features Driver description
  155. ##### How to use this driver #####
  156. ==============================================================================
  157. [..]
  158. (+) DAC APB clock must be enabled to get write access to DAC
  159. registers using HAL_DAC_Init()
  160. (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
  161. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  162. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA() functions.
  163. *** Calibration mode IO operation ***
  164. ======================================
  165. [..]
  166. (+) Retrieve the factory trimming (calibration settings) using HAL_DACEx_GetTrimOffset()
  167. (+) Run the calibration using HAL_DACEx_SelfCalibrate()
  168. (+) Update the trimming while DAC running using HAL_DACEx_SetUserTrimming()
  169. *** Polling mode IO operation ***
  170. =================================
  171. [..]
  172. (+) Start the DAC peripheral using HAL_DAC_Start()
  173. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  174. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  175. *** DMA mode IO operation ***
  176. ==============================
  177. [..]
  178. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  179. of data to be transferred at each end of conversion
  180. (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  181. function is executed and user can add his own code by customization of function pointer
  182. HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  183. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  184. function is executed and user can add his own code by customization of function pointer
  185. HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
  186. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
  187. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  188. (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
  189. HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2()
  190. function is executed and user can add his own code by customization of function pointer
  191. HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2() and
  192. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1()
  193. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  194. *** DAC HAL driver macros list ***
  195. =============================================
  196. [..]
  197. Below the list of most used macros in DAC HAL driver.
  198. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
  199. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
  200. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
  201. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
  202. [..]
  203. (@) You can refer to the DAC HAL driver header file for more useful macros
  204. @endverbatim
  205. ******************************************************************************
  206. * @attention
  207. *
  208. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  209. *
  210. * Redistribution and use in source and binary forms, with or without modification,
  211. * are permitted provided that the following conditions are met:
  212. * 1. Redistributions of source code must retain the above copyright notice,
  213. * this list of conditions and the following disclaimer.
  214. * 2. Redistributions in binary form must reproduce the above copyright notice,
  215. * this list of conditions and the following disclaimer in the documentation
  216. * and/or other materials provided with the distribution.
  217. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  218. * may be used to endorse or promote products derived from this software
  219. * without specific prior written permission.
  220. *
  221. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  222. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  223. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  224. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  225. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  226. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  227. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  228. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  229. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  230. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  231. *
  232. ******************************************************************************
  233. */
  234. /* Includes ------------------------------------------------------------------*/
  235. #include "stm32l4xx_hal.h"
  236. /** @addtogroup STM32L4xx_HAL_Driver
  237. * @{
  238. */
  239. /** @defgroup DAC DAC
  240. * @brief DAC driver modules
  241. * @{
  242. */
  243. #ifdef HAL_DAC_MODULE_ENABLED
  244. /* Private typedef -----------------------------------------------------------*/
  245. /* Private define ------------------------------------------------------------*/
  246. /* Private constants ---------------------------------------------------------*/
  247. /** @addtogroup DAC_Private_Constants DAC Private Constants
  248. * @{
  249. */
  250. #define TIMEOUT_DAC_CALIBCONFIG ((uint32_t)1) /* 1 ms */
  251. #define HFSEL_ENABLE_THRESHOLD_80MHZ ((uint32_t)80000000) /* 80 mHz */
  252. /**
  253. * @}
  254. */
  255. /* Private macro -------------------------------------------------------------*/
  256. /* Private macro -------------------------------------------------------------*/
  257. /* Private variables ---------------------------------------------------------*/
  258. /* Private function prototypes -----------------------------------------------*/
  259. /** @defgroup DAC_Private_Functions DAC Private Functions
  260. * @{
  261. */
  262. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
  263. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
  264. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
  265. /**
  266. * @}
  267. */
  268. /* Exported functions -------------------------------------------------------*/
  269. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  270. * @{
  271. */
  272. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  273. * @brief Initialization and Configuration functions
  274. *
  275. @verbatim
  276. ==============================================================================
  277. ##### Initialization and de-initialization functions #####
  278. ==============================================================================
  279. [..] This section provides functions allowing to:
  280. (+) Initialize and configure the DAC.
  281. (+) De-initialize the DAC.
  282. @endverbatim
  283. * @{
  284. */
  285. /**
  286. * @brief Initialize the DAC peripheral according to the specified parameters
  287. * in the DAC_InitStruct and initialize the associated handle.
  288. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  289. * the configuration information for the specified DAC.
  290. * @retval HAL status
  291. */
  292. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
  293. {
  294. /* Check DAC handle */
  295. if(hdac == NULL)
  296. {
  297. return HAL_ERROR;
  298. }
  299. /* Check the parameters */
  300. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  301. if(hdac->State == HAL_DAC_STATE_RESET)
  302. {
  303. /* Allocate lock resource and initialize it */
  304. hdac->Lock = HAL_UNLOCKED;
  305. /* Init the low level hardware */
  306. HAL_DAC_MspInit(hdac);
  307. }
  308. /* Initialize the DAC state*/
  309. hdac->State = HAL_DAC_STATE_BUSY;
  310. /* Set DAC error code to none */
  311. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  312. /* Initialize the DAC state*/
  313. hdac->State = HAL_DAC_STATE_READY;
  314. /* Return function status */
  315. return HAL_OK;
  316. }
  317. /**
  318. * @brief Deinitialize the DAC peripheral registers to their default reset values.
  319. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  320. * the configuration information for the specified DAC.
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  324. {
  325. /* Check DAC handle */
  326. if(hdac == NULL)
  327. {
  328. return HAL_ERROR;
  329. }
  330. /* Check the parameters */
  331. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  332. /* Change DAC state */
  333. hdac->State = HAL_DAC_STATE_BUSY;
  334. /* DeInit the low level hardware */
  335. HAL_DAC_MspDeInit(hdac);
  336. /* Set DAC error code to none */
  337. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  338. /* Change DAC state */
  339. hdac->State = HAL_DAC_STATE_RESET;
  340. /* Release Lock */
  341. __HAL_UNLOCK(hdac);
  342. /* Return function status */
  343. return HAL_OK;
  344. }
  345. /**
  346. * @brief Initialize the DAC MSP.
  347. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  348. * the configuration information for the specified DAC.
  349. * @retval None
  350. */
  351. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
  352. {
  353. /* Prevent unused argument(s) compilation warning */
  354. UNUSED(hdac);
  355. /* NOTE : This function should not be modified, when the callback is needed,
  356. the HAL_DAC_MspInit could be implemented in the user file
  357. */
  358. }
  359. /**
  360. * @brief DeInitialize the DAC MSP.
  361. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  362. * the configuration information for the specified DAC.
  363. * @retval None
  364. */
  365. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
  366. {
  367. /* Prevent unused argument(s) compilation warning */
  368. UNUSED(hdac);
  369. /* NOTE : This function should not be modified, when the callback is needed,
  370. the HAL_DAC_MspDeInit could be implemented in the user file
  371. */
  372. }
  373. /**
  374. * @}
  375. */
  376. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  377. * @brief IO operation functions
  378. *
  379. @verbatim
  380. ==============================================================================
  381. ##### IO operation functions #####
  382. ==============================================================================
  383. [..] This section provides functions allowing to:
  384. (+) Start conversion.
  385. (+) Stop conversion.
  386. (+) Start conversion and enable DMA transfer.
  387. (+) Stop conversion and disable DMA transfer.
  388. (+) Get result of conversion.
  389. @endverbatim
  390. * @{
  391. */
  392. /**
  393. * @brief Enables DAC and starts 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 (when supported)
  400. * @retval HAL status
  401. */
  402. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
  403. {
  404. /* Check the parameters */
  405. assert_param(IS_DAC_CHANNEL(Channel));
  406. /* Process locked */
  407. __HAL_LOCK(hdac);
  408. /* Change DAC state */
  409. hdac->State = HAL_DAC_STATE_BUSY;
  410. /* Enable the Peripheral */
  411. __HAL_DAC_ENABLE(hdac, Channel);
  412. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  413. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  414. if(Channel == DAC_CHANNEL_1)
  415. {
  416. /* Check if software trigger enabled */
  417. if((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == (DAC_CR_TEN1 | DAC_CR_TSEL1))
  418. {
  419. /* Enable the selected DAC software conversion */
  420. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
  421. }
  422. }
  423. else
  424. {
  425. /* Check if software trigger enabled */
  426. if((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_CR_TEN2 | DAC_CR_TSEL2))
  427. {
  428. /* Enable the selected DAC software conversion*/
  429. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
  430. }
  431. }
  432. #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
  433. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
  434. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  435. /* Check if software trigger enabled */
  436. if((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == (DAC_CR_TEN1 | DAC_CR_TSEL1))
  437. {
  438. /* Enable the selected DAC software conversion */
  439. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
  440. }
  441. #endif /* STM32L451xx STM32L452xx STM32L462xx */
  442. /* Change DAC state */
  443. hdac->State = HAL_DAC_STATE_READY;
  444. /* Process unlocked */
  445. __HAL_UNLOCK(hdac);
  446. /* Return function status */
  447. return HAL_OK;
  448. }
  449. /**
  450. * @brief Disables DAC and stop conversion of channel.
  451. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  452. * the configuration information for the specified DAC.
  453. * @param Channel: The selected DAC channel.
  454. * This parameter can be one of the following values:
  455. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  456. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  457. * @retval HAL status
  458. */
  459. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  460. {
  461. /* Check the parameters */
  462. assert_param(IS_DAC_CHANNEL(Channel));
  463. /* Disable the Peripheral */
  464. __HAL_DAC_DISABLE(hdac, Channel);
  465. /* Change DAC state */
  466. hdac->State = HAL_DAC_STATE_READY;
  467. /* Return function status */
  468. return HAL_OK;
  469. }
  470. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  471. /**
  472. * @brief Enables DAC and starts conversion of channel.
  473. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  474. * the configuration information for the specified DAC.
  475. * @param Channel: The selected DAC channel.
  476. * This parameter can be one of the following values:
  477. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  478. * @param pData: The destination peripheral Buffer address.
  479. * @param Length: The length of data to be transferred from memory to DAC peripheral
  480. * @param Alignment: Specifies the data alignment for DAC channel.
  481. * This parameter can be one of the following values:
  482. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  483. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  484. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  485. * @retval HAL status
  486. */
  487. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  488. {
  489. uint32_t tmpreg = 0;
  490. /* Check the parameters */
  491. assert_param(IS_DAC_CHANNEL(Channel));
  492. assert_param(IS_DAC_ALIGN(Alignment));
  493. /* Process locked */
  494. __HAL_LOCK(hdac);
  495. /* Change DAC state */
  496. hdac->State = HAL_DAC_STATE_BUSY;
  497. /* Set the DMA transfer complete callback for channel1 */
  498. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  499. /* Set the DMA half transfer complete callback for channel1 */
  500. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  501. /* Set the DMA error callback for channel1 */
  502. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  503. /* Enable the selected DAC channel1 DMA request */
  504. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  505. /* Case of use of channel 1 */
  506. switch(Alignment)
  507. {
  508. case DAC_ALIGN_12B_R:
  509. /* Get DHR12R1 address */
  510. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  511. break;
  512. case DAC_ALIGN_12B_L:
  513. /* Get DHR12L1 address */
  514. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  515. break;
  516. case DAC_ALIGN_8B_R:
  517. /* Get DHR8R1 address */
  518. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  519. break;
  520. default:
  521. break;
  522. }
  523. /* Enable the DMA channel */
  524. /* Enable the DAC DMA underrun interrupt */
  525. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  526. /* Enable the DMA channel */
  527. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  528. /* Process Unlocked */
  529. __HAL_UNLOCK(hdac);
  530. /* Enable the Peripheral */
  531. __HAL_DAC_ENABLE(hdac, Channel);
  532. /* Return function status */
  533. return HAL_OK;
  534. }
  535. #endif /* STM32L451xx STM32L452xx STM32L462xx */
  536. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  537. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  538. /**
  539. * @brief Enables DAC and starts conversion of channel.
  540. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  541. * the configuration information for the specified DAC.
  542. * @param Channel: The selected DAC channel.
  543. * This parameter can be one of the following values:
  544. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  545. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  546. * @param pData: The destination peripheral Buffer address.
  547. * @param Length: The length of data to be transferred from memory to DAC peripheral
  548. * @param Alignment: Specifies the data alignment for DAC channel.
  549. * This parameter can be one of the following values:
  550. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  551. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  552. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  553. * @retval HAL status
  554. */
  555. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  556. {
  557. uint32_t tmpreg = 0;
  558. /* Check the parameters */
  559. assert_param(IS_DAC_CHANNEL(Channel));
  560. assert_param(IS_DAC_ALIGN(Alignment));
  561. /* Process locked */
  562. __HAL_LOCK(hdac);
  563. /* Change DAC state */
  564. hdac->State = HAL_DAC_STATE_BUSY;
  565. if(Channel == DAC_CHANNEL_1)
  566. {
  567. /* Set the DMA transfer complete callback for channel1 */
  568. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  569. /* Set the DMA half transfer complete callback for channel1 */
  570. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  571. /* Set the DMA error callback for channel1 */
  572. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  573. /* Enable the selected DAC channel1 DMA request */
  574. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  575. /* Case of use of channel 1 */
  576. switch(Alignment)
  577. {
  578. case DAC_ALIGN_12B_R:
  579. /* Get DHR12R1 address */
  580. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  581. break;
  582. case DAC_ALIGN_12B_L:
  583. /* Get DHR12L1 address */
  584. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  585. break;
  586. case DAC_ALIGN_8B_R:
  587. /* Get DHR8R1 address */
  588. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  589. break;
  590. default:
  591. break;
  592. }
  593. }
  594. else
  595. {
  596. /* Set the DMA transfer complete callback for channel2 */
  597. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  598. /* Set the DMA half transfer complete callback for channel2 */
  599. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  600. /* Set the DMA error callback for channel2 */
  601. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  602. /* Enable the selected DAC channel2 DMA request */
  603. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  604. /* Case of use of channel 2 */
  605. switch(Alignment)
  606. {
  607. case DAC_ALIGN_12B_R:
  608. /* Get DHR12R2 address */
  609. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  610. break;
  611. case DAC_ALIGN_12B_L:
  612. /* Get DHR12L2 address */
  613. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  614. break;
  615. case DAC_ALIGN_8B_R:
  616. /* Get DHR8R2 address */
  617. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  618. break;
  619. default:
  620. break;
  621. }
  622. }
  623. /* Enable the DMA channel */
  624. if(Channel == DAC_CHANNEL_1)
  625. {
  626. /* Enable the DAC DMA underrun interrupt */
  627. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  628. /* Enable the DMA channel */
  629. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  630. }
  631. else
  632. {
  633. /* Enable the DAC DMA underrun interrupt */
  634. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  635. /* Enable the DMA channel */
  636. HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  637. }
  638. /* Process Unlocked */
  639. __HAL_UNLOCK(hdac);
  640. /* Enable the Peripheral */
  641. __HAL_DAC_ENABLE(hdac, Channel);
  642. /* Return function status */
  643. return HAL_OK;
  644. }
  645. #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
  646. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
  647. /**
  648. * @brief Disables DAC and stop conversion of channel.
  649. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  650. * the configuration information for the specified DAC.
  651. * @param Channel: The selected DAC channel.
  652. * This parameter can be one of the following values:
  653. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  654. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  655. * @retval HAL status
  656. */
  657. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  658. {
  659. HAL_StatusTypeDef status = HAL_OK;
  660. /* Check the parameters */
  661. assert_param(IS_DAC_CHANNEL(Channel));
  662. /* Disable the selected DAC channel DMA request */
  663. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
  664. /* Disable the Peripheral */
  665. __HAL_DAC_DISABLE(hdac, Channel);
  666. /* Disable the DMA channel */
  667. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  668. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  669. /* Channel1 is used */
  670. if (Channel == DAC_CHANNEL_1)
  671. {
  672. /* Disable the DMA channel */
  673. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  674. /* Disable the DAC DMA underrun interrupt */
  675. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
  676. }
  677. else /* Channel2 is used for */
  678. {
  679. /* Disable the DMA channel */
  680. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  681. /* Disable the DAC DMA underrun interrupt */
  682. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
  683. }
  684. #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
  685. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
  686. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  687. /* Disable the DMA channel */
  688. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  689. /* Disable the DAC DMA underrun interrupt */
  690. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
  691. #endif /* STM32L451xx STM32L452xx STM32L462xx */
  692. /* Check if DMA Channel effectively disabled */
  693. if (status != HAL_OK)
  694. {
  695. /* Update DAC state machine to error */
  696. hdac->State = HAL_DAC_STATE_ERROR;
  697. }
  698. else
  699. {
  700. /* Change DAC state */
  701. hdac->State = HAL_DAC_STATE_READY;
  702. }
  703. /* Return function status */
  704. return status;
  705. }
  706. /* DAC channel 2 is available on top of DAC channel 1 in */
  707. /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
  708. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
  709. /**
  710. * @brief Handles DAC interrupt request
  711. * This function uses the interruption of DMA
  712. * underrun.
  713. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  714. * the configuration information for the specified DAC.
  715. * @retval None
  716. */
  717. void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
  718. {
  719. if(__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR1))
  720. {
  721. /* Check underrun flag of DAC channel 1 */
  722. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
  723. {
  724. /* Change DAC state to error state */
  725. hdac->State = HAL_DAC_STATE_ERROR;
  726. /* Set DAC error code to chanel1 DMA underrun error */
  727. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
  728. /* Clear the underrun flag */
  729. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
  730. /* Disable the selected DAC channel1 DMA request */
  731. CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  732. /* Error callback */
  733. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  734. }
  735. }
  736. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  737. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  738. if(__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR2))
  739. {
  740. /* Check underrun flag of DAC channel 1 */
  741. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
  742. {
  743. /* Change DAC state to error state */
  744. hdac->State = HAL_DAC_STATE_ERROR;
  745. /* Set DAC error code to channel2 DMA underrun error */
  746. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
  747. /* Clear the underrun flag */
  748. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
  749. /* Disable the selected DAC channel1 DMA request */
  750. CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  751. /* Error callback */
  752. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  753. }
  754. }
  755. #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
  756. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
  757. }
  758. /**
  759. * @brief Set the specified data holding register value for DAC channel.
  760. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  761. * the configuration information for the specified DAC.
  762. * @param Channel: The selected DAC channel.
  763. * This parameter can be one of the following values:
  764. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  765. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  766. * @param Alignment: Specifies the data alignment.
  767. * This parameter can be one of the following values:
  768. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  769. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  770. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  771. * @param Data: Data to be loaded in the selected data holding register.
  772. * @retval HAL status
  773. */
  774. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  775. {
  776. __IO uint32_t tmp = 0;
  777. /* Check the parameters */
  778. assert_param(IS_DAC_CHANNEL(Channel));
  779. assert_param(IS_DAC_ALIGN(Alignment));
  780. assert_param(IS_DAC_DATA(Data));
  781. tmp = (uint32_t)hdac->Instance;
  782. if(Channel == DAC_CHANNEL_1)
  783. {
  784. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  785. }
  786. else
  787. {
  788. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  789. }
  790. /* Set the DAC channel selected data holding register */
  791. *(__IO uint32_t *) tmp = Data;
  792. /* Return function status */
  793. return HAL_OK;
  794. }
  795. /**
  796. * @brief Conversion complete callback in non-blocking mode for Channel1
  797. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  798. * the configuration information for the specified DAC.
  799. * @retval None
  800. */
  801. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  802. {
  803. /* Prevent unused argument(s) compilation warning */
  804. UNUSED(hdac);
  805. /* NOTE : This function should not be modified, when the callback is needed,
  806. the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
  807. */
  808. }
  809. /**
  810. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel1
  811. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  812. * the configuration information for the specified DAC.
  813. * @retval None
  814. */
  815. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  816. {
  817. /* Prevent unused argument(s) compilation warning */
  818. UNUSED(hdac);
  819. /* NOTE : This function should not be modified, when the callback is needed,
  820. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  821. */
  822. }
  823. /**
  824. * @brief Error DAC callback for Channel1.
  825. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  826. * the configuration information for the specified DAC.
  827. * @retval None
  828. */
  829. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  830. {
  831. /* Prevent unused argument(s) compilation warning */
  832. UNUSED(hdac);
  833. /* NOTE : This function should not be modified, when the callback is needed,
  834. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  835. */
  836. }
  837. /**
  838. * @brief DMA underrun DAC callback for channel1.
  839. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  840. * the configuration information for the specified DAC.
  841. * @retval None
  842. */
  843. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  844. {
  845. /* Prevent unused argument(s) compilation warning */
  846. UNUSED(hdac);
  847. /* NOTE : This function should not be modified, when the callback is needed,
  848. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  849. */
  850. }
  851. /**
  852. * @}
  853. */
  854. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  855. * @brief Peripheral Control functions
  856. *
  857. @verbatim
  858. ==============================================================================
  859. ##### Peripheral Control functions #####
  860. ==============================================================================
  861. [..] This section provides functions allowing to:
  862. (+) Configure channels.
  863. (+) Set the specified data holding register value for DAC channel.
  864. @endverbatim
  865. * @{
  866. */
  867. /**
  868. * @brief Returns the last data output value of the selected DAC channel.
  869. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  870. * the configuration information for the specified DAC.
  871. * @param Channel: The selected DAC channel.
  872. * This parameter can be one of the following values:
  873. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  874. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  875. * @retval The selected DAC channel data output value.
  876. */
  877. uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  878. {
  879. /* Check the parameters */
  880. assert_param(IS_DAC_CHANNEL(Channel));
  881. /* Returns the DAC channel data output register value */
  882. #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  883. return hdac->Instance->DOR1;
  884. #endif /* STM32L451xx STM32L452xx STM32L462xx */
  885. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  886. defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  887. if(Channel == DAC_CHANNEL_1)
  888. {
  889. return hdac->Instance->DOR1;
  890. }
  891. else
  892. {
  893. return hdac->Instance->DOR2;
  894. }
  895. #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
  896. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
  897. }
  898. /**
  899. * @brief Configures the selected DAC channel.
  900. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  901. * the configuration information for the specified DAC.
  902. * @param sConfig: DAC configuration structure.
  903. * @param Channel: The selected DAC channel.
  904. * This parameter can be one of the following values:
  905. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  906. * @arg DAC_CHANNEL_2: DAC Channel2 selected (Whenever present)
  907. * @retval HAL status
  908. */
  909. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  910. {
  911. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  912. uint32_t tickstart = 0;
  913. /* Check the DAC parameters */
  914. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  915. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  916. assert_param(IS_DAC_CHIP_CONNECTION(sConfig->DAC_ConnectOnChipPeripheral));
  917. assert_param(IS_DAC_TRIMMING(sConfig->DAC_UserTrimming));
  918. if ((sConfig->DAC_UserTrimming) == DAC_TRIMMING_USER)
  919. {
  920. assert_param(IS_DAC_TRIMMINGVALUE(sConfig->DAC_TrimmingValue));
  921. }
  922. assert_param(IS_DAC_SAMPLEANDHOLD(sConfig->DAC_SampleAndHold));
  923. if ((sConfig->DAC_SampleAndHold) == DAC_SAMPLEANDHOLD_ENABLE)
  924. {
  925. assert_param(IS_DAC_SAMPLETIME(sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime));
  926. assert_param(IS_DAC_HOLDTIME(sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime));
  927. assert_param(IS_DAC_REFRESHTIME(sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime));
  928. }
  929. assert_param(IS_DAC_CHANNEL(Channel));
  930. /* Process locked */
  931. __HAL_LOCK(hdac);
  932. /* Change DAC state */
  933. hdac->State = HAL_DAC_STATE_BUSY;
  934. if(sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)
  935. /* Sample on old configuration */
  936. {
  937. /* SampleTime */
  938. if (Channel == DAC_CHANNEL_1)
  939. {
  940. /* Get timeout */
  941. tickstart = HAL_GetTick();
  942. /* SHSR1 can be written when BWST1 equals RESET */
  943. while (((hdac->Instance->SR) & DAC_SR_BWST1)!= RESET)
  944. {
  945. /* Check for the Timeout */
  946. if((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
  947. {
  948. /* Update error code */
  949. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
  950. /* Change the DMA state */
  951. hdac->State = HAL_DAC_STATE_TIMEOUT;
  952. return HAL_TIMEOUT;
  953. }
  954. }
  955. HAL_Delay(1);
  956. hdac->Instance->SHSR1 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
  957. }
  958. #if !defined (STM32L451xx) & !defined (STM32L452xx) & !defined (STM32L462xx)
  959. else /* Channel 2 */
  960. {
  961. /* SHSR2 can be written when BWST2 equals RESET */
  962. while (((hdac->Instance->SR) & DAC_SR_BWST2)!= RESET)
  963. {
  964. /* Check for the Timeout */
  965. if((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
  966. {
  967. /* Update error code */
  968. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
  969. /* Change the DMA state */
  970. hdac->State = HAL_DAC_STATE_TIMEOUT;
  971. return HAL_TIMEOUT;
  972. }
  973. }
  974. HAL_Delay(1);
  975. hdac->Instance->SHSR2 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
  976. }
  977. #endif /* STM32L451xx STM32L452xx STM32L462xx */
  978. /* HoldTime */
  979. MODIFY_REG (hdac->Instance->SHHR, DAC_SHHR_THOLD1<<Channel, (sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime)<<Channel);
  980. /* RefreshTime */
  981. MODIFY_REG (hdac->Instance->SHRR, DAC_SHRR_TREFRESH1<<Channel, (sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime)<<Channel);
  982. }
  983. if(sConfig->DAC_UserTrimming == DAC_TRIMMING_USER)
  984. /* USER TRIMMING */
  985. {
  986. /* Get the DAC CCR value */
  987. tmpreg1 = hdac->Instance->CCR;
  988. /* Clear trimming value */
  989. tmpreg1 &= ~(((uint32_t)(DAC_CCR_OTRIM1)) << Channel);
  990. /* Configure for the selected trimming offset */
  991. tmpreg2 = sConfig->DAC_TrimmingValue;
  992. /* Calculate CCR register value depending on DAC_Channel */
  993. tmpreg1 |= tmpreg2 << Channel;
  994. /* Write to DAC CCR */
  995. hdac->Instance->CCR = tmpreg1;
  996. }
  997. /* else factory trimming is used (factory setting are available at reset)*/
  998. /* SW Nothing has nothing to do */
  999. /* Get the DAC MCR value */
  1000. tmpreg1 = hdac->Instance->MCR;
  1001. /* Clear DAC_MCR_MODE2_0, DAC_MCR_MODE2_1 and DAC_MCR_MODE2_2 bits */
  1002. tmpreg1 &= ~(((uint32_t)(DAC_MCR_MODE1)) << Channel);
  1003. /* Configure for the selected DAC channel: mode, buffer output & on chip peripheral connect */
  1004. tmpreg2 = (sConfig->DAC_SampleAndHold | sConfig->DAC_OutputBuffer | sConfig->DAC_ConnectOnChipPeripheral);
  1005. /* Calculate MCR register value depending on DAC_Channel */
  1006. tmpreg1 |= tmpreg2 << Channel;
  1007. /* Write to DAC MCR */
  1008. hdac->Instance->MCR = tmpreg1;
  1009. /* DAC in normal operating mode hence clear DAC_CR_CENx bit */
  1010. CLEAR_BIT (hdac->Instance->CR, DAC_CR_CEN1 << Channel);
  1011. /* Get the DAC CR value */
  1012. tmpreg1 = hdac->Instance->CR;
  1013. /* Clear TENx, TSELx, WAVEx and MAMPx bits */
  1014. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1)) << Channel);
  1015. /* Configure for the selected DAC channel: trigger */
  1016. /* Set TSELx and TENx bits according to DAC_Trigger value */
  1017. tmpreg2 = (sConfig->DAC_Trigger);
  1018. /* Calculate CR register value depending on DAC_Channel */
  1019. tmpreg1 |= tmpreg2 << Channel;
  1020. /* Write to DAC CR */
  1021. hdac->Instance->CR = tmpreg1;
  1022. /* Disable wave generation */
  1023. hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
  1024. /* Change DAC state */
  1025. hdac->State = HAL_DAC_STATE_READY;
  1026. /* Process unlocked */
  1027. __HAL_UNLOCK(hdac);
  1028. /* Return function status */
  1029. return HAL_OK;
  1030. }
  1031. /**
  1032. * @}
  1033. */
  1034. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  1035. * @brief Peripheral State and Errors functions
  1036. *
  1037. @verbatim
  1038. ==============================================================================
  1039. ##### Peripheral State and Errors functions #####
  1040. ==============================================================================
  1041. [..]
  1042. This subsection provides functions allowing to
  1043. (+) Check the DAC state.
  1044. (+) Check the DAC Errors.
  1045. @endverbatim
  1046. * @{
  1047. */
  1048. /**
  1049. * @brief return the DAC handle state
  1050. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  1051. * the configuration information for the specified DAC.
  1052. * @retval HAL state
  1053. */
  1054. HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
  1055. {
  1056. /* Return DAC handle state */
  1057. return hdac->State;
  1058. }
  1059. /**
  1060. * @brief Return the DAC error code
  1061. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  1062. * the configuration information for the specified DAC.
  1063. * @retval DAC Error Code
  1064. */
  1065. uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
  1066. {
  1067. return hdac->ErrorCode;
  1068. }
  1069. /**
  1070. * @}
  1071. */
  1072. /**
  1073. * @}
  1074. */
  1075. /** @addtogroup DAC_Private_Functions
  1076. * @{
  1077. */
  1078. /**
  1079. * @brief DMA conversion complete callback.
  1080. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1081. * the configuration information for the specified DMA module.
  1082. * @retval None
  1083. */
  1084. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  1085. {
  1086. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1087. HAL_DAC_ConvCpltCallbackCh1(hdac);
  1088. hdac->State= HAL_DAC_STATE_READY;
  1089. }
  1090. /**
  1091. * @brief DMA half transfer complete callback.
  1092. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1093. * the configuration information for the specified DMA module.
  1094. * @retval None
  1095. */
  1096. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  1097. {
  1098. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1099. /* Conversion complete callback */
  1100. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  1101. }
  1102. /**
  1103. * @brief DMA error callback
  1104. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1105. * the configuration information for the specified DMA module.
  1106. * @retval None
  1107. */
  1108. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  1109. {
  1110. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1111. /* Set DAC error code to DMA error */
  1112. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  1113. HAL_DAC_ErrorCallbackCh1(hdac);
  1114. hdac->State= HAL_DAC_STATE_READY;
  1115. }
  1116. /**
  1117. * @}
  1118. */
  1119. #endif /* HAL_DAC_MODULE_ENABLED */
  1120. /**
  1121. * @}
  1122. */
  1123. /**
  1124. * @}
  1125. */
  1126. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/