stm32f1xx_hal_dac.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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. The device integrates two 12-bit Digital Analog Converters that can
  23. be used independently or simultaneously (dual mode):
  24. (#) DAC channel1 with DAC_OUT1 (PA4) as output
  25. (#) DAC channel2 with DAC_OUT2 (PA5) as output
  26. *** DAC Triggers ***
  27. ====================
  28. [..]
  29. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  30. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  31. [..]
  32. Digital to Analog conversion can be triggered by:
  33. (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
  34. The used pin (GPIOx_PIN_9) must be configured in input mode.
  35. (#) Timers TRGO: TIM2, TIM4, TIM6, TIM7
  36. For STM32F10x connectivity line devices and STM32F100x devices: TIM3
  37. For STM32F10x high-density and XL-density devices: TIM8
  38. For STM32F100x high-density value line devices: TIM15 as
  39. replacement of TIM5.
  40. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
  41. (#) Software using DAC_TRIGGER_SOFTWARE
  42. *** DAC Buffer mode feature ***
  43. ===============================
  44. [..]
  45. Each DAC channel integrates an output buffer that can be used to
  46. reduce the output impedance, and to drive external loads directly
  47. without having to add an external operational amplifier.
  48. To enable, the output buffer use
  49. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  50. [..]
  51. (@) Refer to the device datasheet for more details about output
  52. impedance value with and without output buffer.
  53. *** DAC connect feature ***
  54. ===============================
  55. [..]
  56. Each DAC channel can be connected internally.
  57. To connect, use
  58. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
  59. *** GPIO configurations guidelines ***
  60. =====================
  61. [..]
  62. When a DAC channel is used (ex channel1 on PA4) and the other is not
  63. (ex channel1 on PA5 is configured in Analog and disabled).
  64. Channel1 may disturb channel2 as coupling effect.
  65. Note that there is no coupling on channel2 as soon as channel2 is turned on.
  66. Coupling on adjacent channel could be avoided as follows:
  67. when unused PA5 is configured as INPUT PULL-UP or DOWN.
  68. PA5 is configured in ANALOG just before it is turned on.
  69. *** DAC wave generation feature ***
  70. ===================================
  71. [..]
  72. Both DAC channels can be used to generate
  73. (#) Noise wave using HAL_DACEx_NoiseWaveGenerate()
  74. (#) Triangle wave using HAL_DACEx_TriangleWaveGenerate()
  75. *** DAC data format ***
  76. =======================
  77. [..]
  78. The DAC data format can be:
  79. (#) 8-bit right alignment using DAC_ALIGN_8B_R
  80. (#) 12-bit left alignment using DAC_ALIGN_12B_L
  81. (#) 12-bit right alignment using DAC_ALIGN_12B_R
  82. *** DAC data value to voltage correspondance ***
  83. ================================================
  84. [..]
  85. The analog output voltage on each DAC channel pin is determined
  86. by the following equation:
  87. [..]
  88. DAC_OUTx = VREF+ * DOR / 4095
  89. (+) with DOR is the Data Output Register
  90. [..]
  91. VEF+ is the input voltage reference (refer to the device datasheet)
  92. [..]
  93. e.g. To set DAC_OUT1 to 0.7V, use
  94. (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  95. *** DMA requests ***
  96. =====================
  97. [..]
  98. A DMA1 request can be generated when an external trigger (but not
  99. a software trigger) occurs if DMA1 requests are enabled using
  100. HAL_DAC_Start_DMA()
  101. [..]
  102. DMA requests are mapped as following:
  103. (#) DAC channel1 :
  104. For STM32F100x low-density, medium-density, high-density with DAC
  105. DMA remap:
  106. mapped on DMA1 channel3 which must be
  107. already configured
  108. For STM32F100x high-density without DAC DMA remap and other
  109. STM32F1 devices:
  110. mapped on DMA2 channel3 which must be
  111. already configured
  112. (#) DAC channel2 :
  113. For STM32F100x low-density, medium-density, high-density with DAC
  114. DMA remap:
  115. mapped on DMA1 channel4 which must be
  116. already configured
  117. For STM32F100x high-density without DAC DMA remap and other
  118. STM32F1 devices:
  119. mapped on DMA2 channel4 which must be
  120. already configured
  121. ##### How to use this driver #####
  122. ==============================================================================
  123. [..]
  124. (+) DAC APB clock must be enabled to get write access to DAC
  125. registers using HAL_DAC_Init()
  126. (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
  127. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  128. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
  129. *** Polling mode IO operation ***
  130. =================================
  131. [..]
  132. (+) Start the DAC peripheral using HAL_DAC_Start()
  133. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  134. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  135. *** DMA mode IO operation ***
  136. ==============================
  137. [..]
  138. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  139. of data to be transferred at each end of conversion
  140. (+) At the middle of data transfer HAL_DACEx_ConvHalfCpltCallbackCh1()or HAL_DACEx_ConvHalfCpltCallbackCh2()
  141. function is executed and user can add his own code by customization of function pointer
  142. HAL_DAC_ConvHalfCpltCallbackCh1 or HAL_DAC_ConvHalfCpltCallbackCh2
  143. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
  144. function is executed and user can add his own code by customization of function pointer
  145. HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
  146. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() or HAL_DACEx_ErrorCallbackCh2() function is executed and user can
  147. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1 or HAL_DACEx_ErrorCallbackCh2
  148. (+) For STM32F100x devices with specific feature: DMA underrun.
  149. In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
  150. HAL_DAC_DMAUnderrunCallbackCh1()or HAL_DACEx_DMAUnderrunCallbackCh2()
  151. function is executed and user can add his own code by customization of function pointer
  152. HAL_DAC_DMAUnderrunCallbackCh1 or HAL_DACEx_DMAUnderrunCallbackCh2
  153. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  154. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  155. *** DAC HAL driver macros list ***
  156. =============================================
  157. [..]
  158. Below the list of most used macros in DAC HAL driver.
  159. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral (For STM32F100x devices with specific feature: DMA underrun)
  160. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral (For STM32F100x devices with specific feature: DMA underrun)
  161. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags (For STM32F100x devices with specific feature: DMA underrun)
  162. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status (For STM32F100x devices with specific feature: DMA underrun)
  163. [..]
  164. (@) You can refer to the DAC HAL driver header file for more useful macros
  165. @endverbatim
  166. ******************************************************************************
  167. * @attention
  168. *
  169. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  170. *
  171. * Redistribution and use in source and binary forms, with or without modification,
  172. * are permitted provided that the following conditions are met:
  173. * 1. Redistributions of source code must retain the above copyright notice,
  174. * this list of conditions and the following disclaimer.
  175. * 2. Redistributions in binary form must reproduce the above copyright notice,
  176. * this list of conditions and the following disclaimer in the documentation
  177. * and/or other materials provided with the distribution.
  178. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  179. * may be used to endorse or promote products derived from this software
  180. * without specific prior written permission.
  181. *
  182. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  183. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  184. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  185. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  186. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  187. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  188. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  189. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  190. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  191. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  192. *
  193. ******************************************************************************
  194. */
  195. /* Includes ------------------------------------------------------------------*/
  196. #include "stm32f1xx_hal.h"
  197. /** @addtogroup STM32F1xx_HAL_Driver
  198. * @{
  199. */
  200. /** @defgroup DAC DAC
  201. * @brief DAC driver modules
  202. * @{
  203. */
  204. #ifdef HAL_DAC_MODULE_ENABLED
  205. #if defined (STM32F100xB) || defined (STM32F100xE) || defined (STM32F101xE) || defined (STM32F101xG) || defined (STM32F103xE) || defined (STM32F103xG) || defined (STM32F105xC) || defined (STM32F107xC)
  206. /* Private typedef -----------------------------------------------------------*/
  207. /* Private define ------------------------------------------------------------*/
  208. /* Private macro -------------------------------------------------------------*/
  209. /* Private variables ---------------------------------------------------------*/
  210. /* Private function prototypes -----------------------------------------------*/
  211. /* Exported functions -------------------------------------------------------*/
  212. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  213. * @{
  214. */
  215. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  216. * @brief Initialization and Configuration functions
  217. *
  218. @verbatim
  219. ==============================================================================
  220. ##### Initialization and de-initialization functions #####
  221. ==============================================================================
  222. [..] This section provides functions allowing to:
  223. (+) Initialize and configure the DAC.
  224. (+) De-initialize the DAC.
  225. @endverbatim
  226. * @{
  227. */
  228. /**
  229. * @brief Initializes the DAC peripheral according to the specified parameters
  230. * in the DAC_InitStruct.
  231. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  232. * the configuration information for the specified DAC.
  233. * @retval HAL status
  234. */
  235. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
  236. {
  237. /* Check DAC handle */
  238. if(hdac == NULL)
  239. {
  240. return HAL_ERROR;
  241. }
  242. /* Check the parameters */
  243. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  244. if(hdac->State == HAL_DAC_STATE_RESET)
  245. {
  246. /* Allocate lock resource and initialize it */
  247. hdac->Lock = HAL_UNLOCKED;
  248. /* Init the low level hardware */
  249. HAL_DAC_MspInit(hdac);
  250. }
  251. /* Initialize the DAC state*/
  252. hdac->State = HAL_DAC_STATE_BUSY;
  253. /* Set DAC error code to none */
  254. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  255. /* Initialize the DAC state*/
  256. hdac->State = HAL_DAC_STATE_READY;
  257. /* Return function status */
  258. return HAL_OK;
  259. }
  260. /**
  261. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  262. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  263. * the configuration information for the specified DAC.
  264. * @retval HAL status
  265. */
  266. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  267. {
  268. /* Check DAC handle */
  269. if(hdac == NULL)
  270. {
  271. return HAL_ERROR;
  272. }
  273. /* Check the parameters */
  274. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  275. /* Change DAC state */
  276. hdac->State = HAL_DAC_STATE_BUSY;
  277. /* DeInit the low level hardware */
  278. HAL_DAC_MspDeInit(hdac);
  279. /* Set DAC error code to none */
  280. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  281. /* Change DAC state */
  282. hdac->State = HAL_DAC_STATE_RESET;
  283. /* Release Lock */
  284. __HAL_UNLOCK(hdac);
  285. /* Return function status */
  286. return HAL_OK;
  287. }
  288. /**
  289. * @brief Initializes the DAC MSP.
  290. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  291. * the configuration information for the specified DAC.
  292. * @retval None
  293. */
  294. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
  295. {
  296. /* Prevent unused argument(s) compilation warning */
  297. UNUSED(hdac);
  298. /* NOTE : This function Should not be modified, when the callback is needed,
  299. the HAL_DAC_MspInit could be implemented in the user file
  300. */
  301. }
  302. /**
  303. * @brief DeInitializes the DAC MSP.
  304. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  305. * the configuration information for the specified DAC.
  306. * @retval None
  307. */
  308. __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
  309. {
  310. /* Prevent unused argument(s) compilation warning */
  311. UNUSED(hdac);
  312. /* NOTE : This function Should not be modified, when the callback is needed,
  313. the HAL_DAC_MspDeInit could be implemented in the user file
  314. */
  315. }
  316. /**
  317. * @}
  318. */
  319. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  320. * @brief IO operation functions
  321. *
  322. @verbatim
  323. ==============================================================================
  324. ##### IO operation functions #####
  325. ==============================================================================
  326. [..] This section provides functions allowing to:
  327. (+) Start conversion.
  328. (+) Stop conversion.
  329. (+) Start conversion and enable DMA transfer.
  330. (+) Stop conversion and disable DMA transfer.
  331. (+) Get result of conversion.
  332. @endverbatim
  333. * @{
  334. */
  335. /**
  336. * @brief Enables DAC and starts conversion of channel.
  337. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  338. * the configuration information for the specified DAC.
  339. * @param Channel: The selected DAC channel.
  340. * This parameter can be one of the following values:
  341. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  342. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  343. * @retval HAL status
  344. */
  345. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
  346. {
  347. /* Check the parameters */
  348. assert_param(IS_DAC_CHANNEL(Channel));
  349. /* Process locked */
  350. __HAL_LOCK(hdac);
  351. /* Change DAC state */
  352. hdac->State = HAL_DAC_STATE_BUSY;
  353. /* Enable the Peripharal */
  354. __HAL_DAC_ENABLE(hdac, Channel);
  355. if(Channel == DAC_CHANNEL_1)
  356. {
  357. /* Check if software trigger enabled */
  358. if((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == (DAC_CR_TEN1 | DAC_CR_TSEL1))
  359. {
  360. /* Enable the selected DAC software conversion */
  361. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
  362. }
  363. }
  364. else
  365. {
  366. /* Check if software trigger enabled */
  367. if((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_CR_TEN2 | DAC_CR_TSEL2))
  368. {
  369. /* Enable the selected DAC software conversion*/
  370. SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
  371. }
  372. }
  373. /* Change DAC state */
  374. hdac->State = HAL_DAC_STATE_READY;
  375. /* Process unlocked */
  376. __HAL_UNLOCK(hdac);
  377. /* Return function status */
  378. return HAL_OK;
  379. }
  380. /**
  381. * @brief Disables DAC and stop conversion of channel.
  382. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  383. * the configuration information for the specified DAC.
  384. * @param Channel: The selected DAC channel.
  385. * This parameter can be one of the following values:
  386. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  387. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  388. * @retval HAL status
  389. */
  390. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_DAC_CHANNEL(Channel));
  394. /* Disable the Peripheral */
  395. __HAL_DAC_DISABLE(hdac, Channel);
  396. /* Change DAC state */
  397. hdac->State = HAL_DAC_STATE_READY;
  398. /* Return function status */
  399. return HAL_OK;
  400. }
  401. /**
  402. * @brief Enables DAC and starts conversion of channel.
  403. * Note: For STM32F100x devices with specific feature: DMA underrun.
  404. * On these devices, this function enables the interruption of DMA
  405. * underrun.
  406. * (refer to redefinition of this function in DAC extended file)
  407. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  408. * the configuration information for the specified DAC.
  409. * @param Channel: The selected DAC channel.
  410. * This parameter can be one of the following values:
  411. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  412. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  413. * @param pData: The Source memory Buffer address.
  414. * @param Length: The length of data to be transferred from memory to DAC peripheral
  415. * @param Alignment: Specifies the data alignment for DAC channel.
  416. * This parameter can be one of the following values:
  417. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  418. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  419. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  420. * @retval HAL status
  421. */
  422. __weak HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  423. {
  424. uint32_t tmpreg = 0U;
  425. /* Check the parameters */
  426. assert_param(IS_DAC_CHANNEL(Channel));
  427. assert_param(IS_DAC_ALIGN(Alignment));
  428. /* Process locked */
  429. __HAL_LOCK(hdac);
  430. /* Change DAC state */
  431. hdac->State = HAL_DAC_STATE_BUSY;
  432. if(Channel == DAC_CHANNEL_1)
  433. {
  434. /* Set the DMA transfer complete callback for channel1 */
  435. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  436. /* Set the DMA half transfer complete callback for channel1 */
  437. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  438. /* Set the DMA error callback for channel1 */
  439. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  440. /* Enable the selected DAC channel1 DMA request */
  441. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  442. /* Case of use of channel 1 */
  443. switch(Alignment)
  444. {
  445. case DAC_ALIGN_12B_R:
  446. /* Get DHR12R1 address */
  447. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  448. break;
  449. case DAC_ALIGN_12B_L:
  450. /* Get DHR12L1 address */
  451. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  452. break;
  453. case DAC_ALIGN_8B_R:
  454. /* Get DHR8R1 address */
  455. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  456. break;
  457. default:
  458. break;
  459. }
  460. }
  461. else
  462. {
  463. /* Set the DMA transfer complete callback for channel2 */
  464. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  465. /* Set the DMA half transfer complete callback for channel2 */
  466. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  467. /* Set the DMA error callback for channel2 */
  468. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  469. /* Enable the selected DAC channel2 DMA request */
  470. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  471. /* Case of use of channel 2 */
  472. switch(Alignment)
  473. {
  474. case DAC_ALIGN_12B_R:
  475. /* Get DHR12R2 address */
  476. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  477. break;
  478. case DAC_ALIGN_12B_L:
  479. /* Get DHR12L2 address */
  480. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  481. break;
  482. case DAC_ALIGN_8B_R:
  483. /* Get DHR8R2 address */
  484. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  485. break;
  486. default:
  487. break;
  488. }
  489. }
  490. /* Enable the DMA channel */
  491. if(Channel == DAC_CHANNEL_1)
  492. {
  493. /* Enable the DMA channel */
  494. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  495. }
  496. else
  497. {
  498. /* Enable the DMA channel */
  499. HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  500. }
  501. /* Process Unlocked */
  502. __HAL_UNLOCK(hdac);
  503. /* Enable the Peripharal */
  504. __HAL_DAC_ENABLE(hdac, Channel);
  505. /* Return function status */
  506. return HAL_OK;
  507. }
  508. /**
  509. * @brief Disables DAC and stop conversion of channel.
  510. * Note: For STM32F100x devices with specific feature: DMA underrun.
  511. * On these devices, this function disables the interruption of DMA
  512. * underrun.
  513. * (refer to redefinition of this function in DAC extended file)
  514. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  515. * the configuration information for the specified DAC.
  516. * @param Channel: The selected DAC channel.
  517. * This parameter can be one of the following values:
  518. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  519. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  520. * @retval HAL status
  521. */
  522. __weak HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  523. {
  524. HAL_StatusTypeDef status = HAL_OK;
  525. /* Check the parameters */
  526. assert_param(IS_DAC_CHANNEL(Channel));
  527. /* Disable the selected DAC channel DMA request */
  528. CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN1 << Channel);
  529. /* Disable the Peripharal */
  530. __HAL_DAC_DISABLE(hdac, Channel);
  531. /* Disable the DMA Channel */
  532. /* Channel1 is used */
  533. if (Channel == DAC_CHANNEL_1)
  534. {
  535. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  536. }
  537. else /* Channel2 is used for */
  538. {
  539. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  540. }
  541. /* Check if DMA Channel effectively disabled */
  542. if (status != HAL_OK)
  543. {
  544. /* Update ADC state machine to error */
  545. hdac->State = HAL_DAC_STATE_ERROR;
  546. }
  547. else
  548. {
  549. /* Change DAC state */
  550. hdac->State = HAL_DAC_STATE_READY;
  551. }
  552. /* Return function status */
  553. return status;
  554. }
  555. /**
  556. * @brief Returns the last data output value of the selected DAC channel.
  557. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  558. * the configuration information for the specified DAC.
  559. * @param Channel: The selected DAC channel.
  560. * This parameter can be one of the following values:
  561. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  562. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  563. * @retval The selected DAC channel data output value.
  564. */
  565. uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  566. {
  567. /* Check the parameters */
  568. assert_param(IS_DAC_CHANNEL(Channel));
  569. /* Returns the DAC channel data output register value */
  570. if(Channel == DAC_CHANNEL_1)
  571. {
  572. return hdac->Instance->DOR1;
  573. }
  574. else
  575. {
  576. return hdac->Instance->DOR2;
  577. }
  578. }
  579. /**
  580. * @brief Conversion complete callback in non blocking mode for Channel1
  581. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  582. * the configuration information for the specified DAC.
  583. * @retval None
  584. */
  585. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  586. {
  587. /* Prevent unused argument(s) compilation warning */
  588. UNUSED(hdac);
  589. /* NOTE : This function Should not be modified, when the callback is needed,
  590. the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
  591. */
  592. }
  593. /**
  594. * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
  595. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  596. * the configuration information for the specified DAC.
  597. * @retval None
  598. */
  599. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  600. {
  601. /* Prevent unused argument(s) compilation warning */
  602. UNUSED(hdac);
  603. /* NOTE : This function Should not be modified, when the callback is needed,
  604. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  605. */
  606. }
  607. /**
  608. * @brief Error DAC callback for Channel1.
  609. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  610. * the configuration information for the specified DAC.
  611. * @retval None
  612. */
  613. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  614. {
  615. /* Prevent unused argument(s) compilation warning */
  616. UNUSED(hdac);
  617. /* NOTE : This function Should not be modified, when the callback is needed,
  618. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  619. */
  620. }
  621. /**
  622. * @}
  623. */
  624. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  625. * @brief Peripheral Control functions
  626. *
  627. @verbatim
  628. ==============================================================================
  629. ##### Peripheral Control functions #####
  630. ==============================================================================
  631. [..] This section provides functions allowing to:
  632. (+) Configure channels.
  633. (+) Set the specified data holding register value for DAC channel.
  634. @endverbatim
  635. * @{
  636. */
  637. /**
  638. * @brief Configures the selected DAC channel.
  639. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  640. * the configuration information for the specified DAC.
  641. * @param sConfig: DAC configuration structure.
  642. * @param Channel: The selected DAC channel.
  643. * This parameter can be one of the following values:
  644. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  645. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  646. * @retval HAL status
  647. */
  648. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  649. {
  650. uint32_t tmpreg1 = 0U;
  651. /* Check the DAC parameters */
  652. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  653. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  654. assert_param(IS_DAC_CHANNEL(Channel));
  655. /* Process locked */
  656. __HAL_LOCK(hdac);
  657. /* Change DAC state */
  658. hdac->State = HAL_DAC_STATE_BUSY;
  659. /* Configure for the selected DAC channel: buffer output, trigger */
  660. /* Set TSELx and TENx bits according to DAC_Trigger value */
  661. /* Set BOFFx bit according to DAC_OutputBuffer value */
  662. SET_BIT(tmpreg1, (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer));
  663. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  664. /* Calculate CR register value depending on DAC_Channel */
  665. MODIFY_REG(hdac->Instance->CR,
  666. ((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel,
  667. tmpreg1 << Channel);
  668. /* Disable wave generation */
  669. hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
  670. /* Change DAC state */
  671. hdac->State = HAL_DAC_STATE_READY;
  672. /* Process unlocked */
  673. __HAL_UNLOCK(hdac);
  674. /* Return function status */
  675. return HAL_OK;
  676. }
  677. /**
  678. * @brief Set the specified data holding register value for DAC channel.
  679. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  680. * the configuration information for the specified DAC.
  681. * @param Channel: The selected DAC channel.
  682. * This parameter can be one of the following values:
  683. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  684. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  685. * @param Alignment: Specifies the data alignment.
  686. * This parameter can be one of the following values:
  687. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  688. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  689. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  690. * @param Data: Data to be loaded in the selected data holding register.
  691. * @retval HAL status
  692. */
  693. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  694. {
  695. __IO uint32_t tmp = 0U;
  696. /* Check the parameters */
  697. assert_param(IS_DAC_CHANNEL(Channel));
  698. assert_param(IS_DAC_ALIGN(Alignment));
  699. assert_param(IS_DAC_DATA(Data));
  700. tmp = (uint32_t)hdac->Instance;
  701. if(Channel == DAC_CHANNEL_1)
  702. {
  703. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  704. }
  705. else
  706. {
  707. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  708. }
  709. /* Set the DAC channel selected data holding register */
  710. *(__IO uint32_t *) tmp = Data;
  711. /* Return function status */
  712. return HAL_OK;
  713. }
  714. /**
  715. * @}
  716. */
  717. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  718. * @brief Peripheral State and Errors functions
  719. *
  720. @verbatim
  721. ==============================================================================
  722. ##### Peripheral State and Errors functions #####
  723. ==============================================================================
  724. [..]
  725. This subsection provides functions allowing to
  726. (+) Check the DAC state.
  727. (+) Check the DAC Errors.
  728. @endverbatim
  729. * @{
  730. */
  731. /**
  732. * @brief return the DAC state
  733. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  734. * the configuration information for the specified DAC.
  735. * @retval HAL state
  736. */
  737. HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
  738. {
  739. /* Return DAC state */
  740. return hdac->State;
  741. }
  742. /**
  743. * @brief Return the DAC error code
  744. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  745. * the configuration information for the specified DAC.
  746. * @retval DAC Error Code
  747. */
  748. uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
  749. {
  750. return hdac->ErrorCode;
  751. }
  752. /**
  753. * @}
  754. */
  755. /**
  756. * @}
  757. */
  758. /** @addtogroup DAC_Private_Functions
  759. * @{
  760. */
  761. /**
  762. * @brief DMA conversion complete callback.
  763. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  764. * the configuration information for the specified DMA module.
  765. * @retval None
  766. */
  767. void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  768. {
  769. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  770. HAL_DAC_ConvCpltCallbackCh1(hdac);
  771. hdac->State = HAL_DAC_STATE_READY;
  772. }
  773. /**
  774. * @brief DMA half transfer complete callback.
  775. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  776. * the configuration information for the specified DMA module.
  777. * @retval None
  778. */
  779. void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  780. {
  781. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  782. /* Conversion complete callback */
  783. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  784. }
  785. /**
  786. * @brief DMA error callback
  787. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  788. * the configuration information for the specified DMA module.
  789. * @retval None
  790. */
  791. void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  792. {
  793. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  794. /* Set DAC error code to DMA error */
  795. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  796. HAL_DAC_ErrorCallbackCh1(hdac);
  797. hdac->State = HAL_DAC_STATE_READY;
  798. }
  799. /**
  800. * @}
  801. */
  802. #endif /* STM32F100xB || STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  803. #endif /* HAL_DAC_MODULE_ENABLED */
  804. /**
  805. * @}
  806. */
  807. /**
  808. * @}
  809. */
  810. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/