-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32h573i_discovery_usbpd_pwr.c
1821 lines (1634 loc) · 55.2 KB
/
stm32h573i_discovery_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
/**
******************************************************************************
* @file stm32h573i_discovery_usbpd_pwr.c
* @author MCD Application Team
* @brief This file provides a set of firmware functions to manage USB-PD Power
* available on STM32H573I-DK board(MB1677) from STMicroelectronics :
* - VBUS control
* - VBUS voltage/current measurement
* - VCONN control
* - VBUS presence detection
******************************************************************************
* @attention
*
* Copyright (c) 2023 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.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h573i_discovery_usbpd_pwr.h"
#include "stm32h573i_discovery_bus.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H573I_DK
* @{
*/
/** @defgroup STM32H573I_DK_USBPD_PWR USBPD_PWR
* @{
*/
/** @defgroup STM32H573I_DK_USBPD_PWR_Exported_Variables USBPD_PWR Exported Variables
* @{
*/
/**
* @}
*/
/** @defgroup STM32H573I_DK_USBPD_PWR_Private_Typedef USBPD_PWR Private Typedef
* @{
*/
/**
* @brief Port HW configuration description
*/
typedef struct
{
uint8_t Type; /*!< HW component associated to Port */
uint8_t Address; /*!< I2C address (if any) associated to Port */
} USBPD_PWR_PortConfig_t;
/**
* @brief Port BSP Power status
*/
typedef struct
{
uint8_t IsInitialized; /*!< Port Context Initialization status */
uint8_t IsItEnabled; /*!< Port IT enabled status */
USBPD_PWR_PowerRoleTypeDef PwrRole; /*!< Port current power role (SRC or SNK) */
USBPD_PWR_PowerModeTypeDef PwrSavingMode; /*!< Port current power saving mode */
uint32_t LastFaultTick; /*!< Current Tick on last detected fault */
USBPD_PWR_VBUSDetectCallbackFunc *VBUSDetectCallback;/*!< Port callback for VBUS detection event */
} USBPD_PWR_PortStatus_t;
/**
* @}
*/
/** @defgroup STM32H573I_DK_USBPD_PWR_Private_Constants USBPD_PWR Private Constants
* @{
*/
/* Maximum digital value of the ADC output (12 Bits resolution)
To converting ADC measurement to an absolute voltage value:
VCHANNELx = ADCx_DATA x (VDD/ADC_FULL_SCALE)
*/
#define ADC_FULL_SCALE (0x0FFFU)
#define VDDA_APPLI (3300U)
/* Minimum time between 2 detected faults allowing to consider that
fault could be due to a false detection (could be recovered).
=> if 2 faults occurred in less than that duration, no recovery will be executed */
#define USBPD_PWR_FAULT_MIN_TIME_RECOVERY (500U) /* 500 ms */
/**
* @}
*/
/** @defgroup STM32H573I_DK_USBPD_PWR_Private_Macros USBPD_PWR Private Macros
* @{
*/
/**
* @}
*/
/** @defgroup STM32H573I_DK_USBPD_PWR_Private_Functions USBPD_PWR Private Functions
* @{
*/
static void MX_ADC_Init(void);
static void PWR_TCPP0203_GPIOConfigInit(uint32_t PortNum);
static void PWR_TCPP0203_ITConfigInit(uint32_t PortNum);
static int32_t PWR_TCPP0203_BUSConfigInit(uint32_t PortNum, uint16_t Address);
static int32_t PWR_TCPP0203_ConfigDeInit(uint32_t PortNum);
static void PWR_TCPP0203_EventCallback(uint32_t PortNum);
static uint32_t PWR_TCPP0203_ConvertADCDataToVoltage(uint32_t ADCData, uint32_t Ra, uint32_t Rb);
static int32_t PWR_TCPP0203_ConvertADCDataToCurrent(uint32_t ADCData);
/**
* @}
*/
/** @defgroup STM32H573I_DK_USBPD_PWR_Private_Variables USBPD_PWR Private Variables
* @{
*/
static USBPD_PWR_PortConfig_t USBPD_PWR_Port_Configs[USBPD_PWR_INSTANCES_NBR] =
{
{
USBPD_PWR_HW_CONFIG_TYPE_TCPP03, /* Port 0 : TCPP03 Type */
TCPP0203_I2C_ADDRESS_X68, /* TCPP03 on shield (Address 0x34) */
},
};
static USBPD_PWR_PortStatus_t USBPD_PWR_Port_Status[USBPD_PWR_INSTANCES_NBR] =
{
{0U, 0U, POWER_ROLE_SINK, USBPD_PWR_MODE_OFF, 0U, NULL},
};
static TCPP0203_Object_t USBPD_PWR_PortCompObj[USBPD_PWR_INSTANCES_NBR] = { 0 };
/**
* @}
*/
/** @addtogroup STM32H573I_DK_USBPD_PWR_Exported_Functions
* @{
*/
/**
* @brief Global initialization of PWR resource used by USB-PD
* @param PortNum Type-C port identifier
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_Init(uint32_t PortNum)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_Init --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (USBPD_PWR_Port_Status[PortNum].IsInitialized == 0U)
{
/* Initialisation according to HW configuration type of port */
switch (USBPD_PWR_Port_Configs[PortNum].Type)
{
case USBPD_PWR_HW_CONFIG_TYPE_TCPP02:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP03:
/* Set default Role to SNK */
USBPD_PWR_Port_Status[PortNum].PwrRole = POWER_ROLE_SINK;
/* Set default Power Mode to Hibernate */
USBPD_PWR_Port_Status[PortNum].PwrSavingMode = USBPD_PWR_MODE_HIBERNATE;
/* Reset port callback for VBUS detection event */
USBPD_PWR_Port_Status[PortNum].VBUSDetectCallback = NULL;
/* Reset last detected fault Tick */
USBPD_PWR_Port_Status[PortNum].LastFaultTick = 0;
/* Initialize required HW for GPIO, BUS communication, Interrupts */
/* Initialize required GPIOs */
PWR_TCPP0203_GPIOConfigInit(PortNum);
/* Initialize EXTI for FLGn signal */
PWR_TCPP0203_ITConfigInit(PortNum);
/* Enable component */
TCPP0203_PORT0_ENABLE_GPIO_SET();
/* Initialize required BUS for communication */
ret = PWR_TCPP0203_BUSConfigInit(PortNum, USBPD_PWR_Port_Configs[PortNum].Address);
break;
case USBPD_PWR_HW_CONFIG_TYPE_DEFAULT:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP01:
default:
ret = BSP_ERROR_WRONG_PARAM;
break;
}
if (ret == BSP_ERROR_NONE)
{
/* Set Initialisation flag */
USBPD_PWR_Port_Status[PortNum].IsInitialized = 1U;
}
}
}
return ret;
}
/**
* @brief Global de-initialization of PWR resource used by USB-PD
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum)
{
int32_t ret;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_DeInit --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Deinitialize Power config for port */
switch (USBPD_PWR_Port_Configs[PortNum].Type)
{
case USBPD_PWR_HW_CONFIG_TYPE_TCPP02:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP03:
/* Perform any treatments required for de-initialization for GPIO, BUS communication, Interrupts */
ret = PWR_TCPP0203_ConfigDeInit(PortNum);
break;
case USBPD_PWR_HW_CONFIG_TYPE_DEFAULT:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP01:
default:
ret = BSP_ERROR_WRONG_PARAM;
break;
}
/* Reset Initialisation flag */
USBPD_PWR_Port_Status[PortNum].IsInitialized = 0U;
}
return ret;
}
/**
* @brief Assign Power role for current Port (Source or Sink)
* @param PortNum Type-C port identifier
* This parameter can take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param Role Type-C port role
* This parameter can take one of the following values:
* @arg @ref POWER_ROLE_SOURCE
* @arg @ref POWER_ROLE_SINK
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_SetRole(uint32_t PortNum, USBPD_PWR_PowerRoleTypeDef Role)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
switch (Role)
{
case POWER_ROLE_SOURCE:
/* Port Role is now SRC */
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_SetRole : SRC --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Set port power Role */
USBPD_PWR_Port_Status[PortNum].PwrRole = Role;
/* Ensure that TCPP0203 mode is not set to Hibernate */
if (USBPD_PWR_Port_Status[PortNum].PwrSavingMode == USBPD_PWR_MODE_HIBERNATE)
{
if (TCPP0203_SetPowerMode(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_POWER_MODE_LOWPOWER) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
USBPD_PWR_Port_Status[PortNum].PwrSavingMode = USBPD_PWR_MODE_LOWPOWER;
}
}
break;
case POWER_ROLE_SINK:
/* Port Role is now SNK */
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_SetRole : SNK --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Set port power Role */
USBPD_PWR_Port_Status[PortNum].PwrRole = Role;
break;
default:
ret = BSP_ERROR_WRONG_PARAM;
break;
}
}
return ret;
}
/**
* @brief Set operating mode of Port regarding power saving constraints
* @param PortNum Type-C port identifier
* This parameter can take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param PwrMode Type-C port power saving mode
* This parameter can take one of the following values:
* @arg @ref USBPD_PWR_MODE_OFF
* @arg @ref USBPD_PWR_MODE_HIBERNATE
* @arg @ref USBPD_PWR_MODE_LOWPOWER
* @arg @ref USBPD_PWR_MODE_NORMAL
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_SetPowerMode(uint32_t PortNum, USBPD_PWR_PowerModeTypeDef PwrMode)
{
uint8_t flg_reg;
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_SetPowerMode --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Evaluate requested Power config for port */
switch (PwrMode)
{
case USBPD_PWR_MODE_OFF:
/* Port should enter OFF mode : Reset TCPP0203 Reg0 */
if (TCPP0203_Reset(&USBPD_PWR_PortCompObj[PortNum]) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
break;
case USBPD_PWR_MODE_HIBERNATE:
/* Port Mode is Hibernate : (Default state at startup) suitable for SNK role
- Dead Batteries Enabled,
- Gate Driver Consumer controlled by I2C, Gate Driver Provider OFF
- waiting for VBUS_OK in FLGn (interrupt)
*/
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- Hibernate --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (TCPP0203_SetPowerMode(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_POWER_MODE_HIBERNATE) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
/* Check if VBUS is present (in case of SNK VBUS powered device) */
/* Read Flags register (FLGn) */
if (TCPP0203_ReadFlagRegister(&USBPD_PWR_PortCompObj[PortNum], &flg_reg) == TCPP0203_OK)
{
/* If FLGn has been set to 0 in LOW POWER or HIBERNATE mode,
it is expected that a VBUS_OK condition is detected.
In this case, if any, vBus Detection callback could be called */
if ((flg_reg & TCPP0203_FLAG_VBUS_OK_SET) == TCPP0203_FLAG_VBUS_OK_SET)
{
if (USBPD_PWR_Port_Status[PortNum].VBUSDetectCallback != NULL)
{
/* In case of TCPP03 in Hibernate Mode (SNK config),
Mode is set to Low power to enable TCPP03 behavior on CC lines */
/* Switch to Low Power mode */
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- Low Power --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (TCPP0203_SetPowerMode(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_POWER_MODE_LOWPOWER) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
USBPD_PWR_Port_Status[PortNum].VBUSDetectCallback(PortNum, VBUS_CONNECTED);
}
}
else
{
/* VBUS indication not present in FLGn flags : nothing more to be done */
}
}
break;
case USBPD_PWR_MODE_LOWPOWER:
/* Port Mode is Low Power : suitable while toggling SNK/SRC is case of DRP
- Dead Batteries Disabled,
- Gate Driver Consumer or Provider controlled by I2C
- waiting for VBUS_OK in FLGn (interrupt)
*/
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- Low Power --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (TCPP0203_SetPowerMode(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_POWER_MODE_LOWPOWER) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
break;
case USBPD_PWR_MODE_NORMAL:
/* Port Mode is Normal : No power saving measure (Explicit contract in place) */
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- Normal --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (TCPP0203_SetPowerMode(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_POWER_MODE_NORMAL) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
break;
default:
ret = BSP_ERROR_WRONG_PARAM;
break;
}
/* Set port low power operating mode */
USBPD_PWR_Port_Status[PortNum].PwrSavingMode = PwrMode;
}
return ret;
}
/**
* @brief Get operating mode of Port regarding power saving constraints
* @param PortNum Type-C port identifier
* This parameter can take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @param PwrMode Pointer on current Type-C port power saving mode value
* Following values are available :
* @arg @ref USBPD_PWR_MODE_OFF
* @arg @ref USBPD_PWR_MODE_HIBERNATE
* @arg @ref USBPD_PWR_MODE_LOWPOWER
* @arg @ref USBPD_PWR_MODE_NORMAL
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_GetPowerMode(uint32_t PortNum, USBPD_PWR_PowerModeTypeDef *PwrMode)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_GetPowerMode --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Get port low power operating mode */
*PwrMode = USBPD_PWR_Port_Status[PortNum].PwrSavingMode;
}
return ret;
}
/**
* @brief Initialize the hardware resources used by the Type-C power delivery (PD)
* controller.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum)
{
int32_t ret;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_VBUSInit --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Initialize required HW for VBUS management */
switch (USBPD_PWR_Port_Configs[PortNum].Type)
{
case USBPD_PWR_HW_CONFIG_TYPE_TCPP02:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP03:
/* Switch to Normal mode */
ret = BSP_USBPD_PWR_SetPowerMode(PortNum, USBPD_PWR_MODE_NORMAL);
MX_ADC_Init();
/* Start Conversion */
LL_ADC_REG_StartConversion(USBPD_PWR_VSENSE_ADC_INSTANCE);
break;
case USBPD_PWR_HW_CONFIG_TYPE_DEFAULT:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP01:
default:
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
break;
}
}
return ret;
}
/**
* @brief Release the hardware resources used by the Type-C power delivery (PD)
* controller.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum)
{
int32_t ret;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_VBUSDeInit --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* De-Initialize required HW for VBUS management */
switch (USBPD_PWR_Port_Configs[PortNum].Type)
{
case USBPD_PWR_HW_CONFIG_TYPE_TCPP02:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP03:
/* Restore default gates configuration for Low power mode */
/* => Close Gate Driver Consumer */
if (TCPP0203_SetGateDriverConsumer(&USBPD_PWR_PortCompObj[PortNum],
TCPP0203_GD_CONSUMER_SWITCH_CLOSED) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- GDP/GDC setting : default --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (USBPD_PWR_Port_Status[PortNum].PwrRole == POWER_ROLE_SOURCE)
{
/* Switch to Low Power mode */
ret = BSP_USBPD_PWR_SetPowerMode(PortNum, USBPD_PWR_MODE_LOWPOWER);
}
else
{
/* Switch to Hibernate mode */
ret = BSP_USBPD_PWR_SetPowerMode(PortNum, USBPD_PWR_MODE_HIBERNATE);
}
}
break;
case USBPD_PWR_HW_CONFIG_TYPE_DEFAULT:
case USBPD_PWR_HW_CONFIG_TYPE_TCPP01:
default:
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
break;
}
}
return ret;
}
/**
* @brief Enable power supply over VBUS.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_VBUSOn --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
/* Port Role is now SRC : Close Gate Driver Provider */
if (USBPD_PWR_Port_Status[PortNum].PwrRole == POWER_ROLE_SOURCE)
{
if (TCPP0203_SetGateDriverProvider(&USBPD_PWR_PortCompObj[PortNum],
TCPP0203_GD_PROVIDER_SWITCH_CLOSED) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- GDP/GDC setting : SRC --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
}
else
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
return ret;
}
/**
* @brief Disable power supply over VBUS.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum)
{
int32_t ret = BSP_ERROR_NONE;
uint32_t vbus;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Open Gate driver provider */
#if (USE_BSP_USBPD_PWR_TRACE == 1)
BSP_USBPD_PWR_TRACE(PortNum, "-- BSP_USBPD_PWR_VBUSOff --");
#endif /* (USE_BSP_USBPD_PWR_TRACE == 1) */
if (TCPP0203_SetGateDriverProvider(&USBPD_PWR_PortCompObj[PortNum],
TCPP0203_GD_PROVIDER_SWITCH_OPEN) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Set Discharge On */
if (TCPP0203_SetVBusDischarge(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_VBUS_DISCHARGE_ON) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Wait VBUS level becomes lower than USBPD_PWR_LOW_VBUS_THRESHOLD */
uint8_t counter = 0;
(void)BSP_USBPD_PWR_VBUSGetVoltage(PortNum, &vbus);
while ((vbus >= (USBPD_PWR_LOW_VBUS_THRESHOLD - 100U)) && (counter < 20U))
{
counter++;
HAL_Delay(20);
(void)BSP_USBPD_PWR_VBUSGetVoltage(PortNum, &vbus);
}
HAL_Delay(10);
/* Set Discharge Off */
if (TCPP0203_SetVBusDischarge(&USBPD_PWR_PortCompObj[PortNum], TCPP0203_VBUS_DISCHARGE_OFF) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
}
}
return ret;
}
/**
* @brief Get actual VBUS status.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum, uint8_t *pState)
{
int32_t ret = BSP_ERROR_NONE;
uint8_t val = 0;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (TCPP0203_GetGateDriverProviderAck(&USBPD_PWR_PortCompObj[PortNum], &val) != TCPP0203_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
if (val == TCPP0203_GD_PROVIDER_SWITCH_ACK_OPEN)
{
*pState = 0;
}
else
{
*pState = 1;
}
}
}
return ret;
}
/**
* @brief Set a fixed PDO and manage the power control.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum,
uint32_t VbusTargetInmv,
uint32_t OperatingCurrent,
uint32_t MaxOperatingCurrent)
{
int32_t ret = BSP_ERROR_NONE;
UNUSED(MaxOperatingCurrent);
UNUSED(OperatingCurrent);
UNUSED(VbusTargetInmv);
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
return ret;
}
/**
* @brief Set a variable PDO and manage the power control.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum,
uint32_t VbusTargetMinInmv,
uint32_t VbusTargetMaxInmv,
uint32_t OperatingCurrent,
uint32_t MaxOperatingCurrent)
{
int32_t ret;
UNUSED(MaxOperatingCurrent);
UNUSED(OperatingCurrent);
UNUSED(VbusTargetMaxInmv);
UNUSED(VbusTargetMinInmv);
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
}
/**
* @brief Set a Battery PDO and manage the power control.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum,
uint32_t VbusTargetMin,
uint32_t VbusTargetMax,
uint32_t OperatingPower,
uint32_t MaxOperatingPower)
{
int32_t ret;
UNUSED(OperatingPower);
UNUSED(VbusTargetMax);
UNUSED(VbusTargetMin);
UNUSED(MaxOperatingPower);
/* Check if instance is valid */
if (PortNum >= 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;
}
/**
* @brief Set a APDO and manage the power control.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum,
uint32_t VbusTargetInmv,
uint32_t OperatingCurrent,
int32_t Delta)
{
int32_t ret;
UNUSED(Delta);
UNUSED(OperatingCurrent);
UNUSED(VbusTargetInmv);
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
}
/**
* @brief Set the VBUS disconnection voltage threshold.
* @note Callback function invoked when VBUS falls below programmed threshold.
* @note By default VBUS disconnection threshold is set to 3.3V
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum,
uint32_t VoltageThreshold)
{
UNUSED(VoltageThreshold);
int32_t ret;
/* Check if instance is valid */
if (PortNum >= USBPD_PWR_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
return ret;
}
/**
* @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 PortNum Type-C port identifier
* This parameter can 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 PortNum,
USBPD_PWR_VBUSDetectCallbackFunc *pfnVBUSDetectCallback)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if ((PortNum >= USBPD_PWR_INSTANCES_NBR) || (NULL == pfnVBUSDetectCallback))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Set port callback for VBUS detection event */
USBPD_PWR_Port_Status[PortNum].VBUSDetectCallback = pfnVBUSDetectCallback;
}
return ret;
}
/**
* @brief Get actual voltage level measured on the VBUS line.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum, uint32_t *pVoltage)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if ((PortNum >= USBPD_PWR_INSTANCES_NBR) || (NULL == pVoltage))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
uint32_t voltage;
static __IO uint16_t adc_value;
adc_value = (uint16_t) LL_ADC_REG_ReadConversionData12(USBPD_PWR_VSENSE_ADC_INSTANCE);
voltage = PWR_TCPP0203_ConvertADCDataToVoltage(adc_value, USBPD_PWR_VSENSE_RA, USBPD_PWR_VSENSE_RB);
*pVoltage = voltage;
}
return ret;
}
/**
* @brief Get actual current level measured on the VBUS line.
* @param PortNum Type-C port identifier
* This parameter can 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 PortNum, int32_t *pCurrent)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if instance is valid */
if ((PortNum >= USBPD_PWR_INSTANCES_NBR) || (NULL == pCurrent))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
int32_t current;
uint16_t adc_value;
adc_value = 0U; /* Not yet implemented */
current = PWR_TCPP0203_ConvertADCDataToCurrent(adc_value);
*pCurrent = current;
}
return ret;
}
/**
* @brief Activate discharge on VBUS.
* @param PortNum Type-C port identifier
* This parameter can take one of the following values:
* @arg @ref USBPD_PWR_TYPE_C_PORT_1
* @retval BSP status
*/
int32_t BSP_USBPD_PWR_VBUSDischargeOn(uint32_t PortNum)
{
int32_t ret = BSP_ERROR_NONE;