選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

517 行
17 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended DAC HAL module driver.
  6. * This file provides firmware functions to manage the extended
  7. * functionalities of the DAC peripheral.
  8. *
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2016 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### How to use this driver #####
  24. ==============================================================================
  25. [..]
  26. *** Signal generation operation ***
  27. ===================================
  28. [..]
  29. (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
  30. (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
  31. @endverbatim
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f4xx_hal.h"
  36. /** @addtogroup STM32F4xx_HAL_Driver
  37. * @{
  38. */
  39. #ifdef HAL_DAC_MODULE_ENABLED
  40. #if defined(DAC)
  41. /** @defgroup DACEx DACEx
  42. * @brief DAC Extended HAL module driver
  43. * @{
  44. */
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* Private define ------------------------------------------------------------*/
  47. /* Private macro -------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* Exported functions --------------------------------------------------------*/
  51. /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
  52. * @{
  53. */
  54. /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions
  55. * @brief Extended IO operation functions
  56. *
  57. @verbatim
  58. ==============================================================================
  59. ##### Extended features functions #####
  60. ==============================================================================
  61. [..] This section provides functions allowing to:
  62. (+) Start conversion.
  63. (+) Stop conversion.
  64. (+) Start conversion and enable DMA transfer.
  65. (+) Stop conversion and disable DMA transfer.
  66. (+) Get result of conversion.
  67. (+) Get result of dual mode conversion.
  68. @endverbatim
  69. * @{
  70. */
  71. #if defined(DAC_CHANNEL2_SUPPORT)
  72. /**
  73. * @brief Enables DAC and starts conversion of both channels.
  74. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  75. * the configuration information for the specified DAC.
  76. * @retval HAL status
  77. */
  78. HAL_StatusTypeDef HAL_DACEx_DualStart(DAC_HandleTypeDef *hdac)
  79. {
  80. uint32_t tmp_swtrig = 0UL;
  81. /* Check the DAC peripheral handle */
  82. if (hdac == NULL)
  83. {
  84. return HAL_ERROR;
  85. }
  86. /* Process locked */
  87. __HAL_LOCK(hdac);
  88. /* Change DAC state */
  89. hdac->State = HAL_DAC_STATE_BUSY;
  90. /* Enable the Peripheral */
  91. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_1);
  92. __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_2);
  93. /* Check if software trigger enabled */
  94. if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
  95. {
  96. tmp_swtrig |= DAC_SWTRIGR_SWTRIG1;
  97. }
  98. if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (DAC_CHANNEL_2 & 0x10UL)))
  99. {
  100. tmp_swtrig |= DAC_SWTRIGR_SWTRIG2;
  101. }
  102. /* Enable the selected DAC software conversion*/
  103. SET_BIT(hdac->Instance->SWTRIGR, tmp_swtrig);
  104. /* Change DAC state */
  105. hdac->State = HAL_DAC_STATE_READY;
  106. /* Process unlocked */
  107. __HAL_UNLOCK(hdac);
  108. /* Return function status */
  109. return HAL_OK;
  110. }
  111. /**
  112. * @brief Disables DAC and stop conversion of both channels.
  113. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  114. * the configuration information for the specified DAC.
  115. * @retval HAL status
  116. */
  117. HAL_StatusTypeDef HAL_DACEx_DualStop(DAC_HandleTypeDef *hdac)
  118. {
  119. /* Check the DAC peripheral handle */
  120. if (hdac == NULL)
  121. {
  122. return HAL_ERROR;
  123. }
  124. /* Disable the Peripheral */
  125. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_1);
  126. __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_2);
  127. /* Change DAC state */
  128. hdac->State = HAL_DAC_STATE_READY;
  129. /* Return function status */
  130. return HAL_OK;
  131. }
  132. #endif /* DAC_CHANNEL2_SUPPORT */
  133. /**
  134. * @brief Enable or disable the selected DAC channel wave generation.
  135. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  136. * the configuration information for the specified DAC.
  137. * @param Channel The selected DAC channel.
  138. * This parameter can be one of the following values:
  139. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  140. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  141. * @param Amplitude Select max triangle amplitude.
  142. * This parameter can be one of the following values:
  143. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  144. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  145. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  146. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  147. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  148. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  149. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  150. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  151. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  152. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  153. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  154. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  158. {
  159. /* Check the DAC peripheral handle */
  160. if (hdac == NULL)
  161. {
  162. return HAL_ERROR;
  163. }
  164. /* Check the parameters */
  165. assert_param(IS_DAC_CHANNEL(Channel));
  166. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  167. /* Process locked */
  168. __HAL_LOCK(hdac);
  169. /* Change DAC state */
  170. hdac->State = HAL_DAC_STATE_BUSY;
  171. /* Enable the triangle wave generation for the selected DAC channel */
  172. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
  173. (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
  174. /* Change DAC state */
  175. hdac->State = HAL_DAC_STATE_READY;
  176. /* Process unlocked */
  177. __HAL_UNLOCK(hdac);
  178. /* Return function status */
  179. return HAL_OK;
  180. }
  181. /**
  182. * @brief Enable or disable the selected DAC channel wave generation.
  183. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  184. * the configuration information for the specified DAC.
  185. * @param Channel The selected DAC channel.
  186. * This parameter can be one of the following values:
  187. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  188. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  189. * @param Amplitude Unmask DAC channel LFSR for noise wave generation.
  190. * This parameter can be one of the following values:
  191. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  192. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  193. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  194. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  195. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  196. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  197. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  198. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  199. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  200. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  201. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  202. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  206. {
  207. /* Check the DAC peripheral handle */
  208. if (hdac == NULL)
  209. {
  210. return HAL_ERROR;
  211. }
  212. /* Check the parameters */
  213. assert_param(IS_DAC_CHANNEL(Channel));
  214. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  215. /* Process locked */
  216. __HAL_LOCK(hdac);
  217. /* Change DAC state */
  218. hdac->State = HAL_DAC_STATE_BUSY;
  219. /* Enable the noise wave generation for the selected DAC channel */
  220. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
  221. (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
  222. /* Change DAC state */
  223. hdac->State = HAL_DAC_STATE_READY;
  224. /* Process unlocked */
  225. __HAL_UNLOCK(hdac);
  226. /* Return function status */
  227. return HAL_OK;
  228. }
  229. #if defined(DAC_CHANNEL2_SUPPORT)
  230. /**
  231. * @brief Set the specified data holding register value for dual DAC channel.
  232. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  233. * the configuration information for the specified DAC.
  234. * @param Alignment Specifies the data alignment for dual channel DAC.
  235. * This parameter can be one of the following values:
  236. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  237. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  238. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  239. * @param Data1 Data for DAC Channel1 to be loaded in the selected data holding register.
  240. * @param Data2 Data for DAC Channel2 to be loaded in the selected data holding register.
  241. * @note In dual mode, a unique register access is required to write in both
  242. * DAC channels at the same time.
  243. * @retval HAL status
  244. */
  245. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  246. {
  247. uint32_t data;
  248. uint32_t tmp;
  249. /* Check the DAC peripheral handle */
  250. if (hdac == NULL)
  251. {
  252. return HAL_ERROR;
  253. }
  254. /* Check the parameters */
  255. assert_param(IS_DAC_ALIGN(Alignment));
  256. assert_param(IS_DAC_DATA(Data1));
  257. assert_param(IS_DAC_DATA(Data2));
  258. /* Calculate and set dual DAC data holding register value */
  259. if (Alignment == DAC_ALIGN_8B_R)
  260. {
  261. data = ((uint32_t)Data2 << 8U) | Data1;
  262. }
  263. else
  264. {
  265. data = ((uint32_t)Data2 << 16U) | Data1;
  266. }
  267. tmp = (uint32_t)hdac->Instance;
  268. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  269. /* Set the dual DAC selected data holding register */
  270. *(__IO uint32_t *)tmp = data;
  271. /* Return function status */
  272. return HAL_OK;
  273. }
  274. /**
  275. * @brief Conversion complete callback in non-blocking mode for Channel2.
  276. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  277. * the configuration information for the specified DAC.
  278. * @retval None
  279. */
  280. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  281. {
  282. /* Prevent unused argument(s) compilation warning */
  283. UNUSED(hdac);
  284. /* NOTE : This function should not be modified, when the callback is needed,
  285. the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
  286. */
  287. }
  288. /**
  289. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel2.
  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_DACEx_ConvHalfCpltCallbackCh2(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_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
  300. */
  301. }
  302. /**
  303. * @brief Error DAC callback for Channel2.
  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_DACEx_ErrorCallbackCh2(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_DACEx_ErrorCallbackCh2 could be implemented in the user file
  314. */
  315. }
  316. /**
  317. * @brief DMA underrun DAC callback for Channel2.
  318. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  319. * the configuration information for the specified DAC.
  320. * @retval None
  321. */
  322. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  323. {
  324. /* Prevent unused argument(s) compilation warning */
  325. UNUSED(hdac);
  326. /* NOTE : This function should not be modified, when the callback is needed,
  327. the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
  328. */
  329. }
  330. #endif /* DAC_CHANNEL2_SUPPORT */
  331. /**
  332. * @}
  333. */
  334. /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions
  335. * @brief Extended Peripheral Control functions
  336. *
  337. @verbatim
  338. ==============================================================================
  339. ##### Peripheral Control functions #####
  340. ==============================================================================
  341. [..] This section provides functions allowing to:
  342. (+) Set the specified data holding register value for DAC channel.
  343. @endverbatim
  344. * @{
  345. */
  346. #if defined(DAC_CHANNEL2_SUPPORT)
  347. /**
  348. * @brief Return the last data output value of the selected DAC channel.
  349. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  350. * the configuration information for the specified DAC.
  351. * @retval The selected DAC channel data output value.
  352. */
  353. uint32_t HAL_DACEx_DualGetValue(const DAC_HandleTypeDef *hdac)
  354. {
  355. uint32_t tmp = 0UL;
  356. tmp |= hdac->Instance->DOR1;
  357. tmp |= hdac->Instance->DOR2 << 16UL;
  358. /* Returns the DAC channel data output register value */
  359. return tmp;
  360. }
  361. #endif /* DAC_CHANNEL2_SUPPORT */
  362. /**
  363. * @}
  364. */
  365. /**
  366. * @}
  367. */
  368. /* Private functions ---------------------------------------------------------*/
  369. /** @defgroup DACEx_Private_Functions DACEx private functions
  370. * @brief Extended private functions
  371. * @{
  372. */
  373. #if defined(DAC_CHANNEL2_SUPPORT)
  374. /**
  375. * @brief DMA conversion complete callback.
  376. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  377. * the configuration information for the specified DMA module.
  378. * @retval None
  379. */
  380. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  381. {
  382. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  383. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  384. hdac->ConvCpltCallbackCh2(hdac);
  385. #else
  386. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  387. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  388. hdac->State = HAL_DAC_STATE_READY;
  389. }
  390. /**
  391. * @brief DMA half transfer complete callback.
  392. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  393. * the configuration information for the specified DMA module.
  394. * @retval None
  395. */
  396. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  397. {
  398. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  399. /* Conversion complete callback */
  400. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  401. hdac->ConvHalfCpltCallbackCh2(hdac);
  402. #else
  403. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  404. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  405. }
  406. /**
  407. * @brief DMA error callback.
  408. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  409. * the configuration information for the specified DMA module.
  410. * @retval None
  411. */
  412. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  413. {
  414. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  415. /* Set DAC error code to DMA error */
  416. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  417. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  418. hdac->ErrorCallbackCh2(hdac);
  419. #else
  420. HAL_DACEx_ErrorCallbackCh2(hdac);
  421. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  422. hdac->State = HAL_DAC_STATE_READY;
  423. }
  424. #endif /* DAC_CHANNEL2_SUPPORT */
  425. /**
  426. * @}
  427. */
  428. /**
  429. * @}
  430. */
  431. #endif /* DAC */
  432. #endif /* HAL_DAC_MODULE_ENABLED */
  433. /**
  434. * @}
  435. */