1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
89
90
91
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
141
142
143
144
145
146
147
148
149
150
151
152
153
154
157
158
159
160
161
166
167
168
169
170
171
172
173
177
178
179
180
181
182
183
184
185
186
187
188
196
197
198
199
200
201
202
203
204
205
206
207
208
209
213
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
350
351
352
353
354
355
360
361
362
363
364
365
370
371
372
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
426
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
465
466
467
468
469
470
471
472
473
474
475
476
480
481
485
486
488
489
490
491
492
493
497
498
499
500
501
502
503
508
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
596
597
598
599
600
601
602
603
604
605
606
607
608
609
620
621
626
638
639
640
659
660
663
664
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define VDDA_APPLI ((uint32_t)3300)
#define VAR_CONVERTED_DATA_INIT_VALUE (__LL_ADC_DIGITAL_SCALE(LL_ADC_RESOLUTION_12B) + 1)
#define DIGITAL_SCALE_12BITS (__LL_ADC_DIGITAL_SCALE(LL_ADC_RESOLUTION_12B))
Private define
ADC_HandleTypeDef AdcHandle;
#if defined(WAVEFORM_GENERATION)
DAC_HandleTypeDef DacHandle; /* ... */
#endif
__IO uint16_t uhADCxConvertedData = VAR_CONVERTED_DATA_INIT_VALUE;
__IO uint16_t uhADCxConvertedData_Voltage_mVolt = 0;
__IO uint8_t ubAdcGrpRegularUnitaryConvStatus = 2;
__IO uint8_t ubUserButtonClickEvent = RESET;
Private variables
static void SystemClock_Config(void);
static void Error_Handler(void);
static void Configure_ADC(void);
#if defined(WAVEFORM_GENERATION)
static void Generate_waveform_SW_update_Config(void);
static void Generate_waveform_SW_update(void);/* ... */
#endif
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED2);
BSP_LED_Init(LED1);
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
Configure_ADC();
#if defined(WAVEFORM_GENERATION)
Generate_waveform_SW_update_Config();/* ... */
#endif
while (1)
{
while ((ubUserButtonClickEvent) == RESET)
{
}while ((ubUserButtonClickEvent) == RESET) { ... }
ubUserButtonClickEvent = RESET;
#if defined(WAVEFORM_GENERATION)
Generate_waveform_SW_update();/* ... */
#endif
BSP_LED_Off(LED1);
if (ubAdcGrpRegularUnitaryConvStatus != 0)
{
ubAdcGrpRegularUnitaryConvStatus = 0;
}if (ubAdcGrpRegularUnitaryConvStatus != 0) { ... }
uhADCxConvertedData = VAR_CONVERTED_DATA_INIT_VALUE;
if (LL_ADC_IsEnabled(ADCx) == 0)
{
if (HAL_ADC_Start_IT(&AdcHandle) != HAL_OK)
{
Error_Handler();
}if (HAL_ADC_Start_IT(&AdcHandle) != HAL_OK) { ... }
}if (LL_ADC_IsEnabled(ADCx) == 0) { ... }
else
{
if (LL_ADC_IsEnabled(ADCx) == 1)
{
LL_ADC_REG_StartConversionSWStart(ADCx);
}if (LL_ADC_IsEnabled(ADCx) == 1) { ... }
else
{
Error_Handler();
}else { ... }
}else { ... }
}while (1) { ... }
}{ ... }
/* ... */
void Configure_ADC(void)
{
ADC_ChannelConfTypeDef sConfig;
AdcHandle.Instance = ADCx;
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 1;
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
{
Error_Handler();
}if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { ... }
sConfig.Channel = ADCx_CHANNELa;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
{
Error_Handler();
}if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { ... }
}{ ... }
#if defined(WAVEFORM_GENERATION)
/* ... */
static void Generate_waveform_SW_update_Config(void)
{
static DAC_ChannelConfTypeDef sConfig;
DacHandle.Instance = DACx;
if (HAL_DAC_Init(&DacHandle) != HAL_OK)
{
Error_Handler();
}if (HAL_DAC_Init(&DacHandle) != HAL_OK) { ... }
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK)
{
Error_Handler();
}if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK) { ... }
if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa, DAC_ALIGN_12B_R, DIGITAL_SCALE_12BITS/2) != HAL_OK)
{
Error_Handler();
}if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa, DAC_ALIGN_12B_R, DIGITAL_SCALE_12BITS/2) != HAL_OK) { ... }
if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK)
{
Error_Handler();
}if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK) { ... }
}{ ... }
/* ... */
static void Generate_waveform_SW_update(void)
{
static uint8_t ub_dac_steps_count = 0;
if (HAL_DAC_SetValue(&DacHandle,
DACx_CHANNEL_TO_ADCx_CHANNELa,
DAC_ALIGN_12B_R,
((DIGITAL_SCALE_12BITS * ub_dac_steps_count) / 4)
) != HAL_OK)
{
Error_Handler();
}if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa, DAC_ALIGN_12B_R, ((DIGITAL_SCALE_12BITS * ub_dac_steps_count) / 4) ) != HAL_OK) { ... }
HAL_Delay(1);
if (ub_dac_steps_count < 4)
{
ub_dac_steps_count++;
}if (ub_dac_steps_count < 4) { ... }
else
{
ub_dac_steps_count = 0;
}else { ... }
}{ ... }
#endif/* ... */
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
while(1) { ; }
}if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... }
if(HAL_PWREx_EnableOverDrive() != HAL_OK)
{
while(1) { ; }
}if (HAL_PWREx_EnableOverDrive() != HAL_OK) { ... }
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
while(1) { ; }
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { ... }
}{ ... }
/* ... */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == USER_BUTTON_PIN)
{
ubUserButtonClickEvent = SET;
}if (GPIO_Pin == USER_BUTTON_PIN) { ... }
}{ ... }
/* ... */
void AdcGrpRegularUnitaryConvComplete_Callback()
{
uhADCxConvertedData = LL_ADC_REG_ReadConversionData12(ADCx);
uhADCxConvertedData_Voltage_mVolt = __LL_ADC_CALC_DATA_TO_VOLTAGE(VDDA_APPLI, uhADCxConvertedData, LL_ADC_RESOLUTION_12B);
ubAdcGrpRegularUnitaryConvStatus = 1;
BSP_LED_On(LED1);
}{ ... }
/* ... */
void AdcGrpRegularOverrunError_Callback(void)
{
LL_ADC_DisableIT_OVR(ADCx);
Error_Handler();
}{ ... }
#if defined(WAVEFORM_GENERATION)
/* ... */
void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
{
Error_Handler();
}{ ... }
#endif/* ... */
/* ... */
static void Error_Handler(void)
{
while(1)
{
BSP_LED_Toggle(LED2);
HAL_Delay(500);
}while (1) { ... }
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */