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