-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32l5xx_nucleo_usbpd_pwr.c
1070 lines (953 loc) · 36.9 KB
/
stm32l5xx_nucleo_usbpd_pwr.c
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
26
27
28
29
30
31
32
33
34
35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
269
270
271
272
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
301
302
303
304
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
423
424
425
426
427
428
429
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32l5xx_nucleo_usbpd_pwr.c
* @author MCD Application Team
* @brief This file provides a set of functions needed to manage the Type-C
* Power Delivery (PD):
* - VBUS control
* - VBUS voltage/current measurement
* - VCONN control
* - VBUS presence detection
******************************************************************************
* @attention
*
* Copyright (c) 2019 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "stm32l5xx_nucleo_usbpd_pwr.h"
/* USER CODE BEGIN include */
/* USER CODE END include */
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32L5XX_NUCLEO
* @{
*/
/** @defgroup STM32L5XX_NUCLEO_USBPD_PWR STM32L5XX_NUCLEO USBPD POWER
* @{
*/
/** @defgroup STM32L5XX_NUCLEO_USBPD_PWR_Private_Typedef STM32L5XX_NUCLEO USBPD POWER Private Typedef
* @{
*/
/* USER CODE BEGIN POWER_Private_Typedef */
/* USER CODE END POWER_Private_Typedef */
/**
* @}
*/
/** @defgroup STM32L5XX_NUCLEO_USBPD_PWR_Private_Constants STM32L5XX_NUCLEO USBPD POWER Private Constants
* @{
*/
/* USER CODE BEGIN POWER_Private_Constants */
/* Delay between ADC end of calibration and ADC enable. */
/* Delay estimation in CPU cycles: Case of ADC enable done */
/* immediately after ADC calibration, ADC clock setting slow */
/* (LL_ADC_CLOCK_ASYNC_DIV32). Use a higher delay if ratio */
/* (CPU clock / ADC clock) is above 32. */
#define ADC_DELAY_CALIB_ENABLE_CPU_CYCLES ((uint32_t)(LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES * 32U))
#define VDDA_APPLI 3300U
/* USER CODE END POWER_Private_Constants */
/**
* @}
*/
/** @defgroup STM32L5XX_NUCLEO_USBPD_PWR_Private_Macros STM32L5XX_NUCLEO USBPD POWER Private Macros
* @{
*/
/* USER CODE BEGIN POWER_Private_Macros */
/* USER CODE END POWER_Private_Macros */
/**
* @}
*/
/** @defgroup STM32L5XX_NUCLEO_USBPD_PWR_Private_Variables STM32L5XX_NUCLEO USBPD POWER Private Variables
* @{
*/
/* USER CODE BEGIN POWER_Private_Variables */
/* USER CODE END POWER_Private_Variables */
/**
* @}
*/
/** @defgroup STM32L5XX_NUCLEO_USBPD_PWR_Private_Functions STM32L5XX_NUCLEO USBPD POWER Private Functions
* @{
*/
/* USER CODE BEGIN POWER_Private_Prototypes */
static void PWR_Configure_ADC(void);
static void PWR_Activate_ADC(void);
/* USER CODE END POWER_Private_Prototypes */
/**
* @}
*/
/** @addtogroup STM32L5XX_NUCLEO_USBPD_PWR_Exported_Functions
* @{
*/
/**
* @brief Global initialization of PWR resource used by USB-PD
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_Init(uint32_t Instance)
{
/* USER CODE BEGIN BSP_USBPD_PWR_Init */
/* Check if instance is valid */
int32_t ret = BSP_ERROR_NONE;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Disable DB management on TCPP01 */
GPIO_TCPP01_DB_CLK_ENABLE();
LL_GPIO_SetPinMode(GPIO_TCPP01_DB_PORT, GPIO_TCPP01_DB_PIN, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinSpeed(GPIO_TCPP01_DB_PORT, GPIO_TCPP01_DB_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIO_TCPP01_DB_PORT, GPIO_TCPP01_DB_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIO_TCPP01_DB_PORT, GPIO_TCPP01_DB_PIN, LL_GPIO_PULL_UP);
LL_GPIO_SetOutputPin(GPIO_TCPP01_DB_PORT, GPIO_TCPP01_DB_PIN);
}
return ret;
/* USER CODE END BSP_USBPD_PWR_Init */
}
/**
* @brief Global de-initialization of PWR resource used by USB-PD
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_Deinit(uint32_t Instance)
{
/* USER CODE BEGIN BSP_USBPD_PWR_Deinit */
/* Check if instance is valid */
int32_t ret = BSP_ERROR_NONE;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_Deinit */
}
/**
* @brief Initialize the hardware resources used by the Type-C power delivery (PD)
* controller.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSInit(uint32_t Instance)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSInit */
/* Check if instance is valid */
int32_t ret = BSP_ERROR_NONE;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* For Sink cases */
PWR_Configure_ADC();
PWR_Activate_ADC();
/* MeasureVrefAnalog(); */
LL_ADC_REG_StartConversion(VSENSE_ADC_INSTANCE);
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSInit */
}
/**
* @brief Release the hardware resources used by the Type-C power delivery (PD)
* controller.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSDeInit(uint32_t Instance)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSDeInit */
/* Check if instance is valid */
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSDeInit */
}
/**
* @brief Enable power supply over VBUS.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSOn(uint32_t Instance)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSOn */
/* Check if instance is valid */
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSOn */
}
/**
* @brief Disable power supply over VBUS.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSOff(uint32_t Instance)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSOff */
/* Check if instance is valid */
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSOff */
}
/**
* @brief Set a fixed/variable PDO and manage the power control.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param VbusTargetInmv the vbus Target (in mV)
* @param OperatingCurrent the Operating Current (in mA)
* @param MaxOperatingCurrent the Max Operating Current (in mA)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSSetVoltage_Fixed(uint32_t Instance,
uint32_t VbusTargetInmv,
uint32_t OperatingCurrent,
uint32_t MaxOperatingCurrent)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_Fixed */
/* Check if instance is valid */
int32_t ret = BSP_ERROR_NONE;
UNUSED(MaxOperatingCurrent);
UNUSED(OperatingCurrent);
UNUSED(VbusTargetInmv);
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSSetVoltage_Fixed */
}
/**
* @brief Set a fixed/variable PDO and manage the power control.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param VbusTargetMinInmv the vbus Target min (in mV)
* @param VbusTargetMaxInmv the vbus Target max (in mV)
* @param OperatingCurrent the Operating Current (in mA)
* @param MaxOperatingCurrent the Max Operating Current (in mA)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSSetVoltage_Variable(uint32_t Instance,
uint32_t VbusTargetMinInmv,
uint32_t VbusTargetMaxInmv,
uint32_t OperatingCurrent,
uint32_t MaxOperatingCurrent)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_Variable */
/* Check if instance is valid */
int32_t ret;
UNUSED(MaxOperatingCurrent);
UNUSED(OperatingCurrent);
UNUSED(VbusTargetMaxInmv);
UNUSED(VbusTargetMinInmv);
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_Variable */
}
/**
* @brief Set a Battery PDO and manage the power control.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param VbusTargetMin the vbus Target min (in mV)
* @param VbusTargetMax the vbus Target max (in mV)
* @param OperatingPower the Operating Power (in mW)
* @param MaxOperatingPower the Max Operating Power (in mW)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSSetVoltage_Battery(uint32_t Instance,
uint32_t VbusTargetMin,
uint32_t VbusTargetMax,
uint32_t OperatingPower,
uint32_t MaxOperatingPower)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_Battery */
/* Check if instance is valid */
int32_t ret;
UNUSED(OperatingPower);
UNUSED(VbusTargetMax);
UNUSED(VbusTargetMin);
UNUSED(MaxOperatingPower);
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Set the power, the precision must be at 5% */
/* Set the current limitation */
/* not implemented */
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_Battery */
}
/**
* @brief Set a APDO and manage the power control.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param VbusTargetInmv the vbus Target (in mV)
* @param OperatingCurrent the Operating current (in mA)
* @param Delta Delta between with previous APDO (in mV), 0 means APDO start
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSSetVoltage_APDO(uint32_t Instance,
uint32_t VbusTargetInmv,
uint32_t OperatingCurrent,
int32_t Delta)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_APDO */
/* Check if instance is valid */
int32_t ret;
UNUSED(Delta);
UNUSED(OperatingCurrent);
UNUSED(VbusTargetInmv);
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSSetVoltage_APDO */
}
/**
* @brief Get actual voltage level measured on the VBUS line.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param pVoltage Pointer on measured voltage level (in mV)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSGetVoltage(uint32_t Instance, uint32_t *pVoltage)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSGetVoltage */
/* Check if instance is valid */
int32_t ret = BSP_ERROR_NONE;
if ((Instance >= USBPD_PWR_INSTANCES_NBR) || (NULL == pVoltage))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
uint32_t voltage;
voltage = (uint32_t) __LL_ADC_CALC_DATA_TO_VOLTAGE(VDDA_APPLI, (uint32_t) LL_ADC_REG_ReadConversionData12(VSENSE_ADC_INSTANCE), LL_ADC_RESOLUTION_12B); /* mV */
/* STM32L5XX_NUCLEO board is used */
/* Theorically, it should have been 7.613 (Divider R17/R13 (49.9K/330K) for VSENSE */
voltage *= 7613u;
voltage /= 1000u;
*pVoltage = voltage;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSGetVoltage */
}
/**
* @brief Get actual current level measured on the VBUS line.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param pCurrent Pointer on measured current level (in mA)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSGetCurrent(uint32_t Instance, int32_t *pCurrent)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSGetCurrent */
/* Check if instance is valid */
int32_t ret;
if ((Instance >= USBPD_PWR_INSTANCES_NBR) || (NULL == pCurrent))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSGetCurrent */
}
/**
* @brief Initialize VCONN sourcing.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_CC1
* @arg @ref USBPD_PWR_TYPE_C_CC2
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VCONNInit(uint32_t Instance,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VCONNInit */
/* Check if instance is valid */
UNUSED(CCPinId);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VCONNInit */
}
/**
* @brief Un-Initialize VCONN sourcing.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_CC1
* @arg @ref USBPD_PWR_TYPE_C_CC2
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VCONNDeInit(uint32_t Instance,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VCONNDeInit */
/* Check if instance is valid */
UNUSED(CCPinId);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VCONNDeInit */
}
/**
* @brief Enable VCONN sourcing.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_CC1
* @arg @ref USBPD_PWR_TYPE_C_CC2
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VCONNOn(uint32_t Instance,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VCONNOn */
/* Check if instance is valid */
UNUSED(CCPinId);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VCONNOn */
}
/**
* @brief Disable VCONN sourcing.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param CCPinId CC pin identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_CC1
* @arg @ref USBPD_PWR_TYPE_C_CC2
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VCONNOff(uint32_t Instance,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VCONNOff */
/* Check if instance is valid */
UNUSED(CCPinId);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VCONNOff */
}
/**
* @brief Get actual VCONN status.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_CC1
* @arg @ref USBPD_PWR_TYPE_C_CC2
* @param pState VCONN status (1: On, 0: Off)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VCONNIsOn(uint32_t Instance,
uint32_t CCPinId, uint8_t *pState)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VCONNIsOn */
/* Check if instance is valid */
UNUSED(CCPinId);
int32_t ret;
if ((Instance >= USBPD_PWR_INSTANCES_NBR) || (NULL == pState))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VCONNIsOn */
}
/**
* @brief Set the VBUS disconnection voltage threshold.
* @note Callback function registered through BSP_USBPD_PWR_RegisterVBUSDetectCallback
* function call is invoked when VBUS falls below programmed threshold.
* @note By default VBUS disconnection threshold is set to 3.3V
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param VoltageThreshold VBUS disconnection voltage threshold (in mV)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_SetVBUSDisconnectionThreshold(uint32_t Instance,
uint32_t VoltageThreshold)
{
/* USER CODE BEGIN BSP_USBPD_PWR_SetVBUSDisconnectionThreshold */
/* Check if instance is valid */
UNUSED(VoltageThreshold);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_SetVBUSDisconnectionThreshold */
}
/**
* @brief Register USB Type-C Current callback function.
* @note Callback function invoked when VBUS rises above 4V (VBUS present) or
* when VBUS falls below programmed threshold (VBUS absent).
* @note Callback function is un-registered when callback function pointer
* argument is NULL.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param pfnVBUSDetectCallback callback function pointer
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_RegisterVBUSDetectCallback(uint32_t Instance,
USBPD_PWR_VBUSDetectCallbackFunc *pfnVBUSDetectCallback)
{
/* USER CODE BEGIN BSP_USBPD_PWR_RegisterVBUSDetectCallback */
/* Check if instance is valid */
UNUSED(pfnVBUSDetectCallback);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_RegisterVBUSDetectCallback */
}
/**
* @brief Get actual VBUS status.
* @param Instance Type-C port identifier
* This parameter can be take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param pState VBUS status (1: On, 0: Off)
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSIsOn(uint32_t Instance, uint8_t *pState)
{
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSIsOn */
/* Check if instance is valid */
UNUSED(pState);
int32_t ret;
if (Instance >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
/* USER CODE END BSP_USBPD_PWR_VBUSIsOn */
}
/**
* @}
*/
/** @addtogroup STM32L5XX_NUCLEO_USBPD_PWR_Private_Functions
* @{
*/
/* USER CODE BEGIN POWER_Private_Functions */
/**
* @brief Configure ADC (ADC instance) and GPIO used by ADC channels.
* @note In case re-use of this function outside of this example:
* This function includes checks of ADC hardware constraints before
* executing some configuration functions.
* - In this example, all these checks are not necessary but are
* implemented anyway to show the best practice usages
* corresponding to reference manual procedure.
* (On some STM32 series, setting of ADC features are not
* conditioned to ADC state. However, in order to be compliant with
* other STM32 series and to show the best practice usages,
* ADC state is checked anyway with same constraints).
* Software can be optimized by removing some of these checks,
* if they are not relevant considering previous settings and actions
* in user application.
* - If ADC is not in the appropriate state to modify some parameters,
* the setting of these parameters is bypassed without error
* reporting:
* it can be the expected behavior in case of recall of this
* function to update only a few parameters (which update fulfills
* the ADC state).
* Otherwise, it is up to the user to set the appropriate error
* reporting in user application.
* @note Peripheral configuration is minimal configuration from reset values.
* Thus, some useless LL unitary functions calls below are provided as
* commented examples - setting is default configuration from reset.
* @retval None
*/
static void PWR_Configure_ADC(void)
{
/*## Configuration of GPIO used by ADC channels ############################*/
uint32_t wait_loop_index;
/* Note: On this STM32 device, ADC1 channel 0 is mapped on GPIO pin PA.00 */
/* Enable GPIO Clock */
VSENSE_GPIO_ENABLE_CLOCK();
/* Configure GPIO in analog mode to be used as ADC input */
LL_GPIO_SetPinMode(VSENSE_GPIO_PORT, VSENSE_GPIO_PIN, LL_GPIO_MODE_ANALOG);
/*## Configuration of ADC ##################################################*/
/*## Configuration of ADC hierarchical scope: common to several ADC ########*/
/* Enable ADC clock (core clock) */
VSENSE_ADC_ENABLE_CLOCK();
/* Note: Hardware constraint (refer to description of the functions */
/* below): */
/* On this STM32 series, setting of these features is conditioned to */
/* ADC state: */
/* All ADC instances of the ADC common group must be disabled. */
/* Note: In this example, all these checks are not necessary but are */
/* implemented anyway to show the best practice usages */
/* corresponding to reference manual procedure. */
/* Software can be optimized by removing some of these checks, if */
/* they are not relevant considering previous settings and actions */
/* in user application. */
uint32_t IsEnabled = __LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(VSENSE_ADC_COMMON);
if (IsEnabled == 0U)
{
/* Note: Call of the functions below are commented because they are */
/* useless in this example: */
/* setting corresponding to default configuration from reset state. */
/* Set ADC clock (conversion clock) common to several ADC instances */
/* Note: On this STM32 series, ADC common clock asynchronous prescaler */
/* is applied to each ADC instance if ADC instance clock is */
/* set to clock source asynchronous */
/* (refer to function "LL_ADC_SetClock()" below). */
LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSOURCE_SYSCLK);
LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(VSENSE_ADC_INSTANCE), LL_ADC_CLOCK_ASYNC_DIV256);
/* Set ADC measurement path to internal channels */
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(VSENSE_ADC_INSTANCE), LL_ADC_PATH_INTERNAL_VREFINT);
/*## Configuration of ADC hierarchical scope: multimode ####################*/
/* Note: Feature not available on this STM32 series */
}
/*## Configuration of ADC hierarchical scope: ADC instance #################*/
/* Note: Hardware constraint (refer to description of the functions */
/* below): */
/* On this STM32 series, setting of these features is conditioned to */
/* ADC state: */
/* ADC must be disabled. */
if (LL_ADC_IsEnabled(VSENSE_ADC_INSTANCE) == 0U)
{
/* Delay for internal voltage reference stabilization time. */
/* Compute number of CPU cycles to wait for, from delay in us. */
/* Note: Variable divided by 2 to compensate partially */
/* CPU processing cycles (depends on compilation optimization). */
/* Note: If system core clock frequency is below 200kHz, wait time */
/* is only a few CPU processing cycles. */
wait_loop_index = ((LL_ADC_DELAY_VREFINT_STAB_US * (SystemCoreClock / (100000U * 2U))) / 10U);
while (wait_loop_index != 0U)
{
wait_loop_index--;
}
}
/*## Configuration of ADC hierarchical scope: ADC group regular ############*/
/* Note: Hardware constraint (refer to description of the functions */
/* below): */
/* On this STM32 series, setting of these features is conditioned to */
/* ADC state: */
/* ADC must be disabled or enabled without conversion on going */
/* on group regular. */
uint32_t is_convreg_ongoing = LL_ADC_REG_IsConversionOngoing(VSENSE_ADC_INSTANCE);
if ((LL_ADC_IsEnabled(VSENSE_ADC_INSTANCE) == 0U) ||
(is_convreg_ongoing == 0U))
{
/* Set ADC group regular trigger source */
LL_ADC_REG_SetTriggerSource(VSENSE_ADC_INSTANCE, LL_ADC_REG_TRIG_SOFTWARE);
/* Set ADC group regular continuous mode */
LL_ADC_REG_SetContinuousMode(VSENSE_ADC_INSTANCE, LL_ADC_REG_CONV_CONTINUOUS);
/* Set ADC group regular overrun behavior */
LL_ADC_REG_SetOverrun(VSENSE_ADC_INSTANCE, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
/* Set ADC group regular sequencer */
/* Note: On this STM32 series, ADC group regular sequencer has */
/* two settings: */
/* - Sequencer configured to fully configurable: */
/* sequencer length and each rank */
/* affectation to a channel are configurable. */
/* Channels selection is limited to channels 0 to 14. */
/* - Sequencer configured to not fully configurable: */
/* sequencer length and each rank affectation to a channel */
/* are fixed by channel HW number. */
/* Channels selection is not limited (channels 0 to 18). */
/* Refer to description of function */
/* "LL_ADC_REG_SetSequencerConfigurable()". */
/* Set ADC group regular sequence: channel on rank corresponding to */
/* channel number. */
LL_ADC_REG_SetSequencerRanks(VSENSE_ADC_INSTANCE, VSENSE_ADC_RANK, VSENSE_ADC_CHANNEL);
}
/*## Configuration of ADC hierarchical scope: ADC group injected ###########*/
/* Note: Feature available on this STM32 series but not used */
/*## Configuration of ADC hierarchical scope: channels #####################*/
/* Note: Hardware constraint (refer to description of the functions */
/* below): */
/* On this STM32 series, setting of these features is conditioned to */
/* ADC state: */
/* ADC must be disabled or enabled without conversion on going */
/* on either groups regular or injected. */
is_convreg_ongoing = LL_ADC_REG_IsConversionOngoing(VSENSE_ADC_INSTANCE);
uint32_t is_convinj_ongoing = LL_ADC_INJ_IsConversionOngoing(VSENSE_ADC_INSTANCE);
if ((LL_ADC_IsEnabled(VSENSE_ADC_INSTANCE) == 0U) ||
((is_convreg_ongoing == 0U) &&
(is_convinj_ongoing == 0U)))
{
/* Set ADC channels sampling time */
LL_ADC_SetChannelSamplingTime(VSENSE_ADC_INSTANCE, VSENSE_ADC_CHANNEL, LL_ADC_SAMPLINGTIME_47CYCLES_5);
}
/*## Configuration of ADC transversal scope: analog watchdog ###############*/
/* Note: On this STM32 series, there is only 1 analog watchdog available. */
/* Set ADC analog watchdog: channels to be monitored */
LL_ADC_SetAnalogWDMonitChannels(VSENSE_ADC_INSTANCE, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
/* Set ADC analog watchdog: thresholds */
LL_ADC_ConfigAnalogWDThresholds(VSENSE_ADC_INSTANCE, LL_ADC_AWD1, 700, 600);
/*## Configuration of ADC transversal scope: oversampling ##################*/
/* Set ADC oversampling scope */
LL_ADC_SetOverSamplingScope(VSENSE_ADC_INSTANCE, LL_ADC_OVS_GRP_REGULAR_CONTINUED);
/* Set ADC oversampling parameters */
LL_ADC_ConfigOverSamplingRatioShift(VSENSE_ADC_INSTANCE, LL_ADC_OVS_RATIO_16, LL_ADC_OVS_SHIFT_RIGHT_4);
/*## Configuration of ADC interruptions ####################################*/
/* Enable ADC analog watchdog 1 interruption */
LL_ADC_EnableIT_AWD1(VSENSE_ADC_INSTANCE);
}
/**
* @brief Perform ADC activation procedure to make it ready to convert
* (ADC instance: VSENSE_ADC_INSTANCE).
* @note Operations:
* - ADC instance
* - Run ADC self calibration
* - Enable ADC
* - ADC group regular
* none: ADC conversion start-stop to be performed
* after this function
* - ADC group injected
* Feature not available (feature not available on this STM32 series)
* @retval None
*/
static void PWR_Activate_ADC(void)
{
__IO uint32_t wait_loop_index = 0;
__IO uint32_t backup_setting_adc_dma_transfer;
#if (USE_TIMEOUT == 1)
uint32_t Timeout = 0U; /* Variable used for timeout management */
#endif /* USE_TIMEOUT */
/*## Operation on ADC hierarchical scope: ADC instance #####################*/
/* Note: Hardware constraint (refer to description of the functions */
/* below): */
/* On this STM32 series, setting of these features is conditioned to */
/* ADC state: */
/* ADC must be disabled. */
/* Note: In this example, all these checks are not necessary but are */
/* implemented anyway to show the best practice usages */
/* corresponding to reference manual procedure. */
/* Software can be optimized by removing some of these checks, if */
/* they are not relevant considering previous settings and actions */
/* in user application. */
if (LL_ADC_IsEnabled(VSENSE_ADC_INSTANCE) == 0U)
{
/* Disable ADC deep power down mode */
LL_ADC_DisableDeepPowerDown(VSENSE_ADC_INSTANCE);
/* Enable ADC internal voltage regulator */
LL_ADC_EnableInternalRegulator(VSENSE_ADC_INSTANCE);
/* Delay for ADC internal voltage regulator stabilization. */
/* Compute number of CPU cycles to wait for, from delay in us. */
/* Note: Variable divided by 2 to compensate partially */
/* CPU processing cycles (depends on compilation optimization). */
/* Note: If system core clock frequency is below 200kHz, wait time */
/* is only a few CPU processing cycles. */
wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000U * 2U))) / 10U);
while (wait_loop_index != 0U)
{
wait_loop_index--;
}
/* Disable ADC DMA transfer request during calibration */
/* Note: Specificity of this STM32 series: Calibration factor is */
/* available in data register and also transferred by DMA. */
/* To not insert ADC calibration factor among ADC conversion data */
/* in DMA destination address, DMA transfer must be disabled during */
/* calibration. */
backup_setting_adc_dma_transfer = LL_ADC_REG_GetDMATransfer(VSENSE_ADC_INSTANCE);
LL_ADC_REG_SetDMATransfer(VSENSE_ADC_INSTANCE, LL_ADC_REG_DMA_TRANSFER_NONE);
/* Run ADC self calibration */
LL_ADC_StartCalibration(VSENSE_ADC_INSTANCE, LL_ADC_SINGLE_ENDED);
/* Poll for ADC effectively calibrated */
#if (USE_TIMEOUT == 1)
Timeout = ADC_CALIBRATION_TIMEOUT_MS;
#endif /* USE_TIMEOUT */
while (LL_ADC_IsCalibrationOnGoing(VSENSE_ADC_INSTANCE) != 0U)
{
#if (USE_TIMEOUT == 1)
/* Check Systick counter flag to decrement the time-out value */
if (LL_SYSTICK_IsActiveCounterFlag())
{
if (Timeout-- == 0)
{
/* Time-out occurred. Set LED to blinking mode */
LED_Blinking(LED_BLINK_ERROR);
}
}
#endif /* USE_TIMEOUT */
}
/* Restore ADC DMA transfer request after calibration */
LL_ADC_REG_SetDMATransfer(VSENSE_ADC_INSTANCE, backup_setting_adc_dma_transfer);