HAL_DAC.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. ******************************************************************************
  3. * @file HAL_DAC.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief DAC HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (DAC).
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /*********************************************************************************
  16. * Function : HAL_DAC_IRQHandler
  17. * Description : This function uses the interruption of DMA underrun.
  18. * Input : hdac : pointer to a DAC_HandleTypeDef structure that contains
  19. * the configuration information for DAC module
  20. * Output :
  21. * Author : CWT Data : 2020定
  22. **********************************************************************************/
  23. void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
  24. {
  25. if((hdac->Instance->SR&DAC_SR_DMAUDR1)==DAC_SR_DMAUDR1||(hdac->Instance->SR &DAC_SR_DMAUDR2)==DAC_SR_DMAUDR2)
  26. {
  27. //clear the DMA underrun
  28. hdac->Instance->SR|=DAC_SR_DMAUDR1|DAC_SR_DMAUDR2;
  29. }
  30. }
  31. /*********************************************************************************
  32. * Function : HAL_DAC_MspInit
  33. * Description : Initialize the DAC MSP.
  34. * Input : hdac : pointer to a DAC_HandleTypeDef structure that contains
  35. * the configuration information for DAC module
  36. * Output :
  37. * Author : CWT Data : 2020定
  38. **********************************************************************************/
  39. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
  40. {
  41. /* NOTE : This function should not be modified, when the callback is needed,
  42. the HAL_DAC_MspInit can be implemented in the user file
  43. */
  44. /* For Example */
  45. if(hdac->Instance==DAC)
  46. {
  47. /* Enable DAC clock */
  48. System_Module_Enable(EN_DAC);
  49. GPIO_InitTypeDef GPIO_InitStructure;
  50. /* Initialization GPIO */
  51. /**DAC1 GPIO Configuration
  52. PB1 ------> DAC_OUT1
  53. PB0 ------> DAC_OUT2
  54. */
  55. GPIO_InitStructure.Pin = GPIO_PIN_1|GPIO_PIN_0;
  56. GPIO_InitStructure.Pull=GPIO_NOPULL;
  57. GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
  58. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  59. /* Enable the DAC DMA underrun interrupt */
  60. hdac->Instance->CR |= DAC_CR_DMAUDRIE1|DAC_CR_DMAUDRIE2;
  61. NVIC_ClearPendingIRQ(DAC_IRQn);
  62. NVIC_SetPriority(DAC_IRQn, 5);
  63. NVIC_EnableIRQ(DAC_IRQn);
  64. }
  65. }
  66. /*********************************************************************************
  67. * Function : HAL_DAC_MspDeInit
  68. * Description : DAC MSP De-Initialization.
  69. * Input : hdac : pointer to a DAC_HandleTypeDef structure that contains
  70. * the configuration information for DAC module
  71. * Output :
  72. * Author : CWT Data : 2020定
  73. **********************************************************************************/
  74. void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
  75. {
  76. if(hdac->Instance==DAC)
  77. {
  78. /* USER CODE BEGIN DAC1_MspDeInit 0 */
  79. /* USER CODE END DAC1_MspDeInit 0 */
  80. /* Peripheral clock disable */
  81. System_Module_Disable(EN_DAC);
  82. /**DAC1 GPIO Configuration
  83. PB1 ------> DAC_OUT1
  84. PB0 ------> DAC_OUT2
  85. */
  86. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0);
  87. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_1);
  88. /* DAC1 DMA DeInit */
  89. HAL_DMA_DeInit(hdac->DMA_Handle1);
  90. HAL_DMA_DeInit(hdac->DMA_Handle2);
  91. /* USER CODE BEGIN DAC1_MspDeInit 1 */
  92. /* USER CODE END DAC1_MspDeInit 1 */
  93. }
  94. }
  95. /*********************************************************************************
  96. * Function : HAL_DAC_Init
  97. * Description : Initializes the CAN peripheral according to the specified parameters in the DAC_HandleTypeDef..
  98. * Input : hdac : pointer to a DAC_HandleTypeDef structure that contains
  99. * the configuration information for DAC module
  100. * Output : HAL status
  101. * Author : CWT Data : 2020定
  102. **********************************************************************************/
  103. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef *hdac)
  104. {
  105. uint8_t InitStatus = HAL_ERROR;
  106. /* Check the parameters */
  107. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  108. System_Module_Reset(RST_DAC);
  109. HAL_DAC_MspInit(hdac);
  110. return HAL_OK;
  111. }
  112. /*********************************************************************************
  113. * Function : HAL_DAC_DeInit
  114. * Description : Deinitialize the DAC peripheral registers to their default reset values.
  115. * Input : hdac : pointer to a DAC_HandleTypeDef structure that contains
  116. * the configuration information for DAC module
  117. * Output : HAL status
  118. * Author : CWT Data : 2020定
  119. **********************************************************************************/
  120. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  121. {
  122. /* Check DAC handle */
  123. if (hdac == NULL)
  124. {
  125. return HAL_ERROR;
  126. }
  127. /* Check the parameters */
  128. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  129. HAL_DAC_MspDeInit(hdac);
  130. /* Return function status */
  131. return HAL_OK;
  132. }
  133. /*********************************************************************************
  134. * Function : HAL_DAC_ConfigChannel
  135. * Description : Configures the selected DAC channel.
  136. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  137. * the configuration information for the specified DAC.
  138. * sConfig:DAC configuration structure
  139. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @argDAC_CHANNEL_2
  140. * Output : HAL status
  141. * Author : CWT Data : 2020定
  142. **********************************************************************************/
  143. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  144. {
  145. uint32_t tmpreg1, tmpreg2;
  146. uint32_t tickstart = 0U;
  147. uint32_t ConnectOnChipPeripheral=0U;
  148. /* Check the DAC parameters */
  149. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  150. if(!IS_DAC_TRIGGER(sConfig->DAC_Trigger)) return HAL_ERROR;
  151. if(!IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer)) return HAL_ERROR;
  152. if(!IS_DAC_CHIP_CONNECTION(sConfig->DAC_ConnectOnChipPeripheral)) return HAL_ERROR;
  153. if(!IS_DAC_TRIMMING(sConfig->DAC_UserTrimming)) return HAL_ERROR;
  154. if ((sConfig->DAC_UserTrimming) == DAC_TRIMMING_USER)
  155. {
  156. if(!IS_DAC_TRIMMINGVALUE(sConfig->DAC_TrimmingValue)) return HAL_ERROR;
  157. }
  158. if(!IS_DAC_SAMPLEANDHOLD(sConfig->DAC_SampleAndHold)) return HAL_ERROR;
  159. if ((sConfig->DAC_SampleAndHold) == DAC_SAMPLEANDHOLD_ENABLE)
  160. {
  161. if(!IS_DAC_SAMPLETIME(sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime)) return HAL_ERROR;
  162. if(!IS_DAC_HOLDTIME(sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime)) return HAL_ERROR;
  163. if(!IS_DAC_REFRESHTIME(sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime)) return HAL_ERROR;
  164. }
  165. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  166. if (sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)
  167. /* Sample on old configuration */
  168. {
  169. /* SampleTime */
  170. if (Channel == DAC_CHANNEL_1)
  171. {
  172. hdac->Instance->SHSR1 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
  173. }
  174. else /* Channel 2 */
  175. hdac->Instance->SHSR2 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
  176. /* HoldTime */
  177. MODIFY_REG(hdac->Instance->SHHR, DAC_SHHR_THOLD1 << (Channel & 0x10UL), (sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime) << (Channel & 0x10UL));
  178. /* RefreshTime */
  179. MODIFY_REG(hdac->Instance->SHRR, DAC_SHRR_TREFRESH1 << (Channel & 0x10UL), (sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime) << (Channel & 0x10UL));
  180. }
  181. if (sConfig->DAC_UserTrimming == DAC_TRIMMING_USER)
  182. /* USER TRIMMING */
  183. {
  184. /* Get the DAC CCR value */
  185. tmpreg1 = hdac->Instance->CCR;
  186. /* Clear trimming value */
  187. tmpreg1 &= ~(((uint32_t)(DAC_CCR_OTRIM1)) << (Channel & 0x10UL));
  188. /* Configure for the selected trimming offset */
  189. tmpreg2 = sConfig->DAC_TrimmingValue;
  190. /* Calculate CCR register value depending on DAC_Channel */
  191. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  192. /* Write to DAC CCR */
  193. hdac->Instance->CCR = tmpreg1;
  194. }
  195. else
  196. {
  197. /* factory trimming in NVR,read to DAC_CCR */
  198. uint32_t OTRIM=*(uint32_t *)(0x80248);
  199. uint32_t OTRIM_high=(OTRIM&0xffff0000)>>16;
  200. uint32_t OTRIM_low=(OTRIM&0xffff);
  201. if (OTRIM_low==((~OTRIM_high)&0xffff))
  202. {
  203. tmpreg1=(OTRIM_low&0x1f)|(((OTRIM_low&0x3E0)>>5)<<16);
  204. hdac->Instance->CCR = tmpreg1;
  205. }
  206. }
  207. /* Get the DAC MCR value */
  208. tmpreg1 = hdac->Instance->MCR;
  209. /* Clear DAC_MCR_MODEx bits */
  210. tmpreg1 &= ~(((uint32_t)(DAC_MCR_MODE1)) << (Channel & 0x10UL));
  211. /* Configure for the selected DAC channel: mode, buffer output & on chip peripheral connect */
  212. ConnectOnChipPeripheral=sConfig->DAC_ConnectOnChipPeripheral;
  213. if((sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)&&(sConfig->DAC_OutputBuffer==DAC_OUTPUTBUFFER_DISABLE))
  214. {
  215. ConnectOnChipPeripheral=(!ConnectOnChipPeripheral);
  216. }
  217. tmpreg2 = (sConfig->DAC_SampleAndHold | sConfig->DAC_OutputBuffer | ConnectOnChipPeripheral);
  218. /* Calculate MCR register value depending on DAC_Channel */
  219. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  220. /* Write to DAC MCR */
  221. hdac->Instance->MCR = tmpreg1;
  222. /* DAC in normal operating mode hence clear DAC_CR_CENx bit */
  223. CLEAR_BIT(hdac->Instance->CR, DAC_CR_CEN1 << (Channel & 0x10UL));
  224. /* Get the DAC CR value */
  225. tmpreg1 = hdac->Instance->CR;
  226. /* Clear TENx, TSELx, WAVEx and MAMPx bits */
  227. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1)) << (Channel & 0x10UL));
  228. /* Configure for the selected DAC channel: trigger */
  229. /* Set TSELx and TENx bits according to DAC_Trigger value */
  230. tmpreg2 = sConfig->DAC_Trigger;
  231. /* Calculate CR register value depending on DAC_Channel */
  232. tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
  233. /* Write to DAC CR */
  234. hdac->Instance->CR = tmpreg1;
  235. /* Disable wave generation */
  236. hdac->Instance->CR &= ~(DAC_CR_WAVE1 << (Channel & 0x10UL));
  237. /* Return function status */
  238. return HAL_OK;
  239. }
  240. /*********************************************************************************
  241. * Function : HAL_DAC_Start
  242. * Description : Enables DAC and starts conversion of channel.
  243. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  244. * the configuration information for the specified DAC.
  245. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @argDAC_CHANNEL_2
  246. * Output : HAL status
  247. * Author : CWT Data : 2020定
  248. **********************************************************************************/
  249. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
  250. {
  251. /* Check the parameters */
  252. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  253. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  254. uint32_t tmp1 = 0U, tmp2 = 0U;
  255. if (Channel == DAC_CHANNEL_1)
  256. {
  257. hdac->Instance->CR|=DAC_CR_EN1;
  258. tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
  259. tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
  260. /* Check if software trigger enabled */
  261. if((tmp1 == DAC_CR_TEN1) && (tmp2 == DAC_CR_TSEL1))
  262. {
  263. /* Enable the selected DAC software conversion */
  264. hdac->Instance->SWTRIGR|=DAC_SWTRIGR_SWTRIG1;
  265. }
  266. }
  267. else
  268. {
  269. hdac->Instance->CR|=DAC_CR_EN2;
  270. tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
  271. tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;
  272. /* Check if software trigger enabled */
  273. if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
  274. {
  275. /* Enable the selected DAC software conversion */
  276. hdac->Instance->SWTRIGR|=DAC_SWTRIGR_SWTRIG2;
  277. }
  278. }
  279. /* Return function status */
  280. return HAL_OK;
  281. }
  282. /*********************************************************************************
  283. * Function : HAL_DAC_Stop
  284. * Description : Disables DAC and stop conversion of channel.
  285. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  286. * the configuration information for the specified DAC.
  287. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @argDAC_CHANNEL_2
  288. * Output : HAL status
  289. * Author : CWT Data : 2020定
  290. **********************************************************************************/
  291. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  292. {
  293. /* Check the parameters */
  294. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  295. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  296. /* Disable the Peripheral */
  297. if (Channel == DAC_CHANNEL_1)
  298. {
  299. hdac->Instance->CR&=~DAC_CR_EN1;
  300. }
  301. else
  302. {
  303. hdac->Instance->CR&=~DAC_CR_EN2;
  304. }
  305. /* Return function status */
  306. return HAL_OK;
  307. }
  308. /*********************************************************************************
  309. * Function : HAL_DAC_Start_DMA
  310. * Description : Enables DAC and starts conversion of channel.
  311. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  312. * the configuration information for the specified DAC.
  313. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @argDAC_CHANNEL_2 @arg DAC_CHANNEL_Dual
  314. * pData: The destination peripheral Buffer address.
  315. * Length: The length of data to be transferred from memory to DAC peripheral
  316. * Alignment: Specifies the data alignment for DAC channel.This parameter can be one of the following values:
  317. @arg DAC_ALIGN_8B_R @arg DAC_ALIGN_12B_L @arg DAC_ALIGN_12B_R
  318. * Output : HAL status
  319. * Author : CWT Data : 2020定
  320. **********************************************************************************/
  321. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t *pData, uint32_t Length, uint32_t Alignment)
  322. {
  323. HAL_StatusTypeDef status;
  324. uint32_t DstAddr = 0U;
  325. /* Check the parameters */
  326. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  327. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  328. if(!IS_DAC_ALIGN(Alignment)) return HAL_ERROR;
  329. if (Channel == DAC_CHANNEL_1)
  330. {
  331. /* Enable the DAC DMA underrun interrupt */
  332. /* Enable the selected DAC channel2 DMA request */
  333. hdac->Instance->CR |= DAC_CR_EN1|DAC_CR_DMAEN1|DAC_CR_DMAUDRIE1;
  334. /* Case of use of channel 1 */
  335. switch (Alignment)
  336. {
  337. case DAC_ALIGN_12B_R:
  338. /* Get DHR12R1 address */
  339. DstAddr = (uint32_t)&hdac->Instance->DHR12R1;
  340. break;
  341. case DAC_ALIGN_12B_L:
  342. /* Get DHR12L1 address */
  343. DstAddr = (uint32_t)&hdac->Instance->DHR12L1;
  344. break;
  345. case DAC_ALIGN_8B_R:
  346. /* Get DHR8R1 address */
  347. DstAddr = (uint32_t)&hdac->Instance->DHR8R1;
  348. break;
  349. default:
  350. break;
  351. }
  352. status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, DstAddr, Length);
  353. }
  354. else if(Channel == DAC_CHANNEL_2)
  355. {
  356. /* Enable the DAC DMA underrun interrupt */
  357. /* Enable the selected DAC channel2 DMA request */
  358. hdac->Instance->CR |= DAC_CR_EN2|DAC_CR_DMAEN2|DAC_CR_DMAUDRIE2;
  359. /* Case of use of channel 1 */
  360. switch (Alignment)
  361. {
  362. case DAC_ALIGN_12B_R:
  363. /* Get DHR12R1 address */
  364. DstAddr = (uint32_t)&hdac->Instance->DHR12R2;
  365. break;
  366. case DAC_ALIGN_12B_L:
  367. /* Get DHR12L1 address */
  368. DstAddr = (uint32_t)&hdac->Instance->DHR12L2;
  369. break;
  370. case DAC_ALIGN_8B_R:
  371. /* Get DHR8R1 address */
  372. DstAddr = (uint32_t)&hdac->Instance->DHR8R2;
  373. break;
  374. default:
  375. break;
  376. }
  377. status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, DstAddr, Length);
  378. }
  379. else/* DualChannel */
  380. {
  381. hdac->Instance->CR |= DAC_CR_EN1|DAC_CR_DMAEN1|DAC_CR_DMAUDRIE1|DAC_CR_EN2 ;
  382. /* Case of use of channel_1 DMA change two DAC channel */
  383. switch (Alignment)
  384. {
  385. case DAC_ALIGN_12B_R:
  386. /* Get DHR12R1 address */
  387. DstAddr = (uint32_t)&hdac->Instance->DHR12RD;
  388. break;
  389. case DAC_ALIGN_12B_L:
  390. /* Get DHR12L1 address */
  391. DstAddr = (uint32_t)&hdac->Instance->DHR12LD;
  392. break;
  393. case DAC_ALIGN_8B_R:
  394. /* Get DHR8R1 address */
  395. DstAddr = (uint32_t)&hdac->Instance->DHR8RD;
  396. break;
  397. default:
  398. break;
  399. }
  400. status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, DstAddr, Length);
  401. }
  402. /* Return function status */
  403. return status;
  404. }
  405. /*********************************************************************************
  406. * Function : HAL_DAC_Stop_DMA
  407. * Description : Disables DAC and stop conversion of channel.
  408. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  409. * the configuration information for the specified DAC.
  410. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @argDAC_CHANNEL_2 @arg DAC_CHANNEL_Dual
  411. * Output : HAL status
  412. * Author : CWT Data : 2020定
  413. **********************************************************************************/
  414. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  415. {
  416. HAL_StatusTypeDef status = HAL_OK;
  417. /* Check the parameters */
  418. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  419. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  420. /* Disable the selected DAC channel DMA request */
  421. /* Disable the DMA Channel */
  422. /* Channel1 is used */
  423. if(Channel == DAC_CHANNEL_1)
  424. {
  425. hdac->Instance->CR &= ~DAC_CR_DMAEN1;
  426. /* Disable the Peripheral */
  427. hdac->Instance->CR&=~DAC_CR_EN1;
  428. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  429. }
  430. else if(Channel == DAC_CHANNEL_2) /* Channel2 is used for */
  431. {
  432. hdac->Instance->CR &= ~DAC_CR_DMAEN2;
  433. hdac->Instance->CR&=~DAC_CR_EN2;
  434. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  435. }
  436. else
  437. {
  438. hdac->Instance->CR &= ~DAC_CR_DMAEN1;
  439. hdac->Instance->CR &= ~DAC_CR_DMAEN2;
  440. /* Disable the Peripheral */
  441. hdac->Instance->CR&=~DAC_CR_EN1;
  442. hdac->Instance->CR&=~DAC_CR_EN2;
  443. status = HAL_DMA_Abort(hdac->DMA_Handle1)|HAL_DMA_Abort(hdac->DMA_Handle2);
  444. }
  445. /* Return function status */
  446. return status;
  447. }
  448. /*********************************************************************************
  449. * Function : HAL_DAC_SetChannelValue
  450. * Description : Set the specified data holding register value for DAC channel.
  451. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  452. * the configuration information for the specified DAC.
  453. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @argDAC_CHANNEL_2
  454. * Alignment: Specifies the data alignment for DAC channel.This parameter can be one of the following values:
  455. * @arg DAC_ALIGN_8B_R @arg DAC_ALIGN_12B_L @arg DAC_ALIGN_12B_R
  456. * Data:The destination peripheral Buffer address.
  457. * Output : HAL status
  458. * Author : CWT Data : 2020定
  459. **********************************************************************************/
  460. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  461. {
  462. __IO uint32_t tmp = 0;
  463. /* Check the parameters */
  464. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  465. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  466. if(!IS_DAC_ALIGN(Alignment)) return HAL_ERROR;
  467. tmp = (uint32_t)hdac->Instance;
  468. if (Channel == DAC_CHANNEL_1)
  469. {
  470. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  471. }
  472. else
  473. {
  474. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  475. }
  476. /* Calculate and set dual DAC data holding register value */
  477. if (Alignment == DAC_ALIGN_12B_L)
  478. {
  479. Data = (uint32_t)Data << 4;
  480. }
  481. /* Set the DAC channel selected data holding register */
  482. *(__IO uint32_t *) tmp = Data;
  483. /* Return function status */
  484. return HAL_OK;
  485. }
  486. /*********************************************************************************
  487. * Function : HAL_DACEx_DualSetValue
  488. * Description : Set the specified data holding register value for dual DAC channel.
  489. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  490. * the configuration information for the specified DAC.
  491. * Alignment: Specifies the data alignment for DAC channel.This parameter can be one of the following values:
  492. * @arg DAC_ALIGN_8B_R @arg DAC_ALIGN_12B_L @arg DAC_ALIGN_12B_R
  493. * Datax:The destination peripheral Buffer address.
  494. * Output : HAL status
  495. * Author : CWT Data : 2020定
  496. **********************************************************************************/
  497. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  498. {
  499. uint32_t data, tmp;
  500. /* Check the parameters */
  501. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  502. if(!IS_DAC_ALIGN(Alignment)) return HAL_ERROR;
  503. /* Calculate and set dual DAC data holding register value */
  504. if (Alignment == DAC_ALIGN_12B_L)
  505. {
  506. data = ((uint32_t)Data2 << 20U) | (Data1<<4);
  507. }
  508. else
  509. {
  510. data = ((uint32_t)Data2 << 16U) | Data1;
  511. }
  512. tmp = (uint32_t)hdac->Instance;
  513. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  514. /* Set the dual DAC selected data holding register */
  515. *(__IO uint32_t *)tmp = data;
  516. /* Return function status */
  517. return HAL_OK;
  518. }
  519. /*********************************************************************************
  520. * Function : HAL_DAC_GetValue
  521. * Description : Returns the last data output value of the selected DAC channel.
  522. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  523. * the configuration information for the specified DAC.
  524. * Channel:This parameter can be one of the following values: @arg DAC_CHANNEL_1 @arg DAC_CHANNEL_2
  525. * Output : The selected DAC channel data output value.
  526. * Author : CWT Data : 2020定
  527. **********************************************************************************/
  528. uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  529. {
  530. /* Check the parameters */
  531. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  532. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  533. /* Returns the DAC channel data output register value */
  534. if(Channel == DAC_CHANNEL_1)
  535. {
  536. return hdac->Instance->DOR1;
  537. }
  538. else
  539. {
  540. return hdac->Instance->DOR2;
  541. }
  542. }
  543. /*********************************************************************************
  544. * Function : HAL_DACEx_DualGetValue
  545. * Description : Return the last data output value of the selected DAC channel.
  546. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  547. * the configuration information for the specified DAC.
  548. * Output : The selected DAC channel data output value.
  549. * Author : CWT Data : 2020定
  550. **********************************************************************************/
  551. uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac)
  552. {
  553. /* Check the parameters */
  554. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  555. uint32_t tmp = 0U;
  556. tmp |= hdac->Instance->DOR1;
  557. tmp |= hdac->Instance->DOR2 << 16U;
  558. /* Returns the DAC channel data output register value */
  559. return tmp;
  560. }
  561. /*********************************************************************************
  562. * Function :HAL_DACEx_TriangleWaveGenerate
  563. * Description : Enable or disable the selected DAC channel wave generation.
  564. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  565. * the configuration information for the specified DAC.
  566. * Channel:The selected DAC channel. This parameter can be one of the following values:
  567. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  568. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  569. * Amplitude: Amplitude Select max triangle amplitude.
  570. * This parameter can be one of the following values:
  571. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  572. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  573. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  574. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  575. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  576. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  577. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  578. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  579. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  580. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  581. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  582. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  583. * Author : CWT Data : 2020定
  584. **********************************************************************************/
  585. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  586. {
  587. /* Check the parameters */
  588. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  589. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  590. if(!IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)) return HAL_ERROR;
  591. /* Enable the triangle wave generation for the selected DAC channel */
  592. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
  593. /* Return function status */
  594. return HAL_OK;
  595. }
  596. /*********************************************************************************
  597. * Function : HAL_DACEx_NoiseWaveGenerate
  598. * Description : Enable or disable the selected DAC channel wave generation
  599. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  600. * the configuration information for the specified DAC.
  601. * Channel:The selected DAC channel. This parameter can be one of the following values:
  602. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  603. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  604. * Amplitude: Amplitude Unmask DAC channel LFSR for noise wave generation.
  605. * This parameter can be one of the following values:
  606. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  607. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  608. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  609. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  610. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  611. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  612. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  613. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  614. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  615. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  616. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  617. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  618. * Output : HAL status
  619. * Author : CWT Data : 2020定
  620. **********************************************************************************/
  621. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  622. {
  623. /* Check the parameters */
  624. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  625. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  626. if(!IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)) return HAL_ERROR;
  627. /* Enable the noise wave generation for the selected DAC channel */
  628. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
  629. /* Return function status */
  630. return HAL_OK;
  631. }
  632. /*********************************************************************************
  633. * Function : HAL_DACEx_SelfCalibrate
  634. * Description : SRun the self calibration of one DAC channel.
  635. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  636. * the configuration information for the specified DAC.
  637. * sConfig:sConfig DAC channel configuration structure
  638. * Channel:The selected DAC channel. This parameter can be one of the following values:
  639. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  640. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  641. * Output : HAL status
  642. * Author : CWT Data : 2020定
  643. **********************************************************************************/
  644. HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
  645. {
  646. /* Check the parameters */
  647. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  648. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  649. HAL_StatusTypeDef status = HAL_OK;
  650. __IO uint32_t tmp;
  651. uint32_t trimmingvalue;
  652. uint32_t laststatus=0;
  653. uint32_t nowstatus=0;
  654. SET_BIT((hdac->Instance->CR), (DAC_CR_EN1 << (Channel & 0x10UL)));
  655. tmp = (uint32_t)hdac->Instance;
  656. if (Channel == DAC_CHANNEL_1)
  657. {
  658. tmp += DAC_DHR12R1_ALIGNMENT(DAC_ALIGN_12B_R);
  659. }
  660. else
  661. {
  662. tmp += DAC_DHR12R2_ALIGNMENT(DAC_ALIGN_12B_R);
  663. }
  664. *(__IO uint32_t *) tmp = 0x0800U;
  665. /* Enable the selected DAC channel calibration */
  666. /* i.e. set DAC_CR_CENx bit */
  667. SET_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
  668. /* Init trimming counter */
  669. /* Medium value ,trimmingvalue:0-31(0x1f)*/
  670. for(trimmingvalue=0;trimmingvalue<32;trimmingvalue++)
  671. {
  672. /* Set candidate trimming */
  673. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
  674. System_Delay_MS(1);
  675. laststatus=nowstatus;
  676. nowstatus=(hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL)))>>(DAC_SR_CAL_FLAG1_Pos +Channel);
  677. /* tOFFTRIMmax delay x ms as per datasheet (electrical characteristics */
  678. /* i.e. minimum time needed between two calibration steps */
  679. if (nowstatus==1&&laststatus==0)
  680. {
  681. break;
  682. }
  683. }
  684. /* Disable the selected DAC channel calibration */
  685. /* i.e. clear DAC_CR_CENx bit */
  686. CLEAR_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
  687. /* Disable the selected DAC channel */
  688. CLEAR_BIT((hdac->Instance->CR), (DAC_CR_EN1 << (Channel & 0x10UL)));
  689. sConfig->DAC_TrimmingValue = trimmingvalue;
  690. sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
  691. return status;
  692. }
  693. /*********************************************************************************
  694. * Function : HAL_DACEx_SetUserTrimming
  695. * Description : Set the trimming mode and trimming value (user trimming mode applied).
  696. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  697. * the configuration information for the specified DAC.
  698. * sConfig:sConfig DAC channel configuration structure
  699. * Channel:The selected DAC channel. This parameter can be one of the following values:
  700. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  701. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  702. * NewTrimmingValue: DAC new trimming value
  703. * Output : HAL status
  704. * Author : CWT Data : 2020定
  705. **********************************************************************************/
  706. HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel, uint32_t NewTrimmingValue)
  707. {
  708. HAL_StatusTypeDef status = HAL_OK;
  709. /* Check the parameters */
  710. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  711. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  712. if(!IS_DAC_Calibration_TRIM(NewTrimmingValue)) return HAL_ERROR;
  713. /* Check the DAC handle allocation */
  714. if (hdac == NULL)
  715. {
  716. status = HAL_ERROR;
  717. }
  718. else
  719. {
  720. /* Set new trimming */
  721. MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (NewTrimmingValue << (Channel & 0x10UL)));
  722. /* Update trimming mode */
  723. sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
  724. sConfig->DAC_TrimmingValue = NewTrimmingValue;
  725. }
  726. return status;
  727. }
  728. /*********************************************************************************
  729. * Function : HAL_DACEx_GetTrimOffset
  730. * Description : Return the DAC trimming value.
  731. * Input : hdac : hdac pointer to a DAC_HandleTypeDef structure that contains
  732. * the configuration information for the specified DAC.
  733. * Channel:The selected DAC channel. This parameter can be one of the following values:
  734. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  735. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  736. * Output : Trimming value : range: 0->31
  737. * Author : CWT Data : 2020定
  738. **********************************************************************************/
  739. uint32_t HAL_DACEx_GetTrimOffset(DAC_HandleTypeDef *hdac, uint32_t Channel)
  740. {
  741. /* Check the parameters */
  742. if(!IS_DAC_ALL_PERIPH(hdac->Instance)) return HAL_ERROR;
  743. if(!IS_DAC_CHANNEL(Channel)) return HAL_ERROR;
  744. /* Retrieve trimming */
  745. return ((hdac->Instance->CCR & (DAC_CCR_OTRIM1 << (Channel & 0x10UL))) >> (Channel & 0x10UL));
  746. }