stm32l4xx_hal_dac.c 52 KB

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