stm32f1xx_hal_dac.c 32 KB

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