训练营PLSR题目
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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