-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm.c
1687 lines (1501 loc) · 59.2 KB
/
ppm.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 ppm.c
* @author MCD Application Team
* @brief PPM file
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 "ppm.h"
#include "usbpd_dpm_core.h"
#include "usbpd_dpm_conf.h"
#include "usbpd_dpm_user.h"
#include "usbpd_pwr_if.h"
#if defined(_RTOS)
#include "cmsis_os.h"
#endif /* _RTOS */
#if defined(_TRACE)
#include "usbpd_trace.h"
#include "stdio.h"
#endif /* _TRACE */
#include "string.h"
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_CORE
* @{
*/
/** @addtogroup USBPD_CORE_PPM
* @{
*/
/* Private typedef -----------------------------------------------------------*/
typedef struct
{
UCSI_VERSION_t UcsiVersion;
UCSI_SET_NOTIFICATION_ENABLE_t SetNotificationEnable;
UCSI_GET_CAPABILITY_IN_t Capability;
UCSI_GET_CONNECTOR_CAPABILITY_IN_t ConnectorCapability[USBPD_PORT_COUNT];
UCSI_CC_OPERATION_MODE_t CC_Mode[USBPD_PORT_COUNT];
UCSI_USB_OPERATION_ROLE_t USB_Role[USBPD_PORT_COUNT];
UCSI_POWER_DIRECTION_MODE_t PowerMode[USBPD_PORT_COUNT];
UCSI_POWER_DIRECTION_ROLE_t PowerRole[USBPD_PORT_COUNT];
/* Used for I2C data transfer*/
UCSI_CCI_t AckCCI_Status;
UCSI_CONTROL_t Control;
UCSI_MESSAGE_IN_t MessageIn;
UCSI_MESSAGE_OUT_t MessageOut;
uint8_t Error;
} PPM_HandleTypeDef;
/* Private define ------------------------------------------------------------*/
/* Optional Features */
#define FEATURE_SET_UOM_SUPPORTED 0U
#define FEATURE_SET_POWER_LEVEL_SUPPORTED 0U
#if defined(_VDM)
#define FEATURE_ALTERNATEMODEDETAILS_SUPPORTED 0U
#define FEATURE_ALTERNATEMODEOVERRIDE_SUPPORTED 0U
#endif /* _VDM */
#define FEATURE_PDODETAILS_SUPPORTED 1U
#define FEATURE_CABLEDETAILS_SUPPORTED 0U
#define FEATURE_EXTERNALSUPPLYNOTIFICATION_SUPPORTED 0U
#define FEATURE_PDRESETNOTIFICATION_SUPPORTED 0U
#define FEATURE_GET_PD_MESSAGE_SUPPORTED 0U
#if defined(_TRACE)
#define PPM_DEBUG_NONE (0U)
#define PPM_DEBUG_LEVEL_0 (1U << 0)
#define PPM_DEBUG_LEVEL_1 (1U << 1)
#define PPM_DEBUG_LEVEL_2 (1U << 2)
#endif /* _TRACE */
#ifndef __IO
#define __IO volatile /* __IO macro definition */
#endif /* __IO */
/* Format event
[ PORT | XX | YY | EVENT ]*/
#define MASK_EVENT 0xFFU
#define SET_PORT_EVENT(__PORT__, __EVENT__) (((__PORT__) << 24) | (__EVENT__))
#define GET_PORT_EVENT(__EVENT__) (((__EVENT__) >> 24) & 0xFFU)
typedef enum
{
PPM_USER_EVENT_TIMER, /* TIMER EVENT */
PPM_USER_EVENT_COMMAND, /* COMMAND RECEIVED */
PPM_USER_EVENT_NOTIFICATION, /* NOTIFICATION RECEIVED */
PPM_USER_EVENT_NONE, /* NO EVENT */
} PPM_USER_EVENT;
#ifdef _RTOS
#define PPM_BOX_MESSAGES_MAX 2U
#define FREERTOS_PPM_PRIORITY osPriorityLow
#define FREERTOS_PPM_STACK_SIZE 256U
#endif /* _RTOS */
#define PPM_WAIT_ACK_TIMEOUT 1000U /* Timeout in ms for which to stay in state PPM_WAIT_FOR_CMD_COMPL_ACK */
typedef enum
{
PPM_IDLE_NOT_READY, /* PPM Idle (notification disabled) */
PPM_IDLE_READY, /* PPM Idle (notification enabled) */
PPM_BUSY, /* PPM Busy */
PPM_WAIT_FOR_ASYNCH_EVENT_ACK, /* Wait for asynch event ack */
PPM_PROCESSING_COMMAND, /* Processing command */
PPM_WAIT_FOR_CMD_COMPL_ACK, /* Wait for command completion ack */
} PPM_STATE;
/* Private macro -------------------------------------------------------------*/
#if defined(_TRACE)
#define PPM_DEBUG(_LEVEL_, _PORT_, __MSG__) \
do \
{ \
if((PPM_DebugLevel & (_LEVEL_)) == (_LEVEL_)) \
{ \
USBPD_TRACE_Add(USBPD_TRACE_DEBUG, (_PORT_), 0U,(uint8_t*)(__MSG__), sizeof(__MSG__)); \
} \
} while(0);
#define PPM_DEBUG_WITH_ARG(_LEVEL_, _PORT_, ...) \
do \
{ \
if ((PPM_DebugLevel & (_LEVEL_)) == (_LEVEL_)) \
{ \
char _str[70U]; \
(void)snprintf(_str, 70U, __VA_ARGS__); \
USBPD_TRACE_Add(USBPD_TRACE_DEBUG, (uint8_t)(_PORT_), 0U, (uint8_t*)_str, strlen(_str)); \
} \
} while(0);
#define PPM_TRACE(_LEVEL_, _ID_, _PTR_DATA_, _SIZE_) \
do \
{ \
if ((PPM_DebugLevel & (_LEVEL_)) == (_LEVEL_)) \
{ \
USBPD_TRACE_Add(USBPD_TRACE_UCSI, 0U, _ID_, _PTR_DATA_, _SIZE_); \
} \
} while(0);
#else
#define PPM_TRACE(_LEVEL_, _ID_, _PTR_DATA_, _SIZE_)
#define PPM_DEBUG(_LEVEL_, _PORT_, __MSG__)
#define PPM_DEBUG_WITH_ARG(_LEVEL_, _PORT_, ...)
#endif /* _TRACE */
#if defined(_TRACE)
#define SWITCH_TO_PPM_STATE(_STATE_) \
do \
{ \
PPM_State = (_STATE_); \
if (PPM_State != PPM_PreviousState) \
{ \
PPM_PreviousState = PPM_State; \
PPM_TRACE(PPM_DEBUG_LEVEL_2, UCSI_TRACE_PPM_STATE, (uint8_t*)&PPM_State, 1U); \
} \
} while(0);
#else
#define SWITCH_TO_PPM_STATE(_STATE_) PPM_State = (_STATE_);
#endif /* _TRACE */
/* Private variables ---------------------------------------------------------*/
#if defined(_TRACE)
uint8_t PPM_DebugLevel = /*PPM_DEBUG_LEVEL_2 | */PPM_DEBUG_LEVEL_1 | PPM_DEBUG_LEVEL_0 | PPM_DEBUG_NONE;
#endif /* _TRACE */
PPM_HandleTypeDef PPM_Handle;
extern USBPD_HandleTypeDef DPM_Ports[USBPD_PORT_COUNT];
#if defined(_RTOS)
osMessageQId PPMMsgBox;
static void USBPD_PPM_UserExecute(void const *argument);
#if (osCMSIS < 0x20000U)
osThreadDef(PPM, USBPD_PPM_UserExecute, FREERTOS_PPM_PRIORITY, 0U, FREERTOS_PPM_STACK_SIZE);
#else /* osCMSIS >= 0x20000U */
osThreadAttr_t PPM_Thread_Atrr =
{
.name = "PPM",
.priority = FREERTOS_PPM_PRIORITY,
.stack_size = FREERTOS_PPM_STACK_SIZE
};
#endif /* osCMSIS < 0x20000U */
#elif defined(USE_STM32_UTILITY_OS)
#else
#warning "NRTOS Version not implemented"
#endif /* _RTOS */
/* Local variable to store port status */
uint16_t CurrentPort;
uint8_t AsynchEventPending = 0;
uint8_t CommandPending = 0;
__IO PPM_STATE PPM_State = PPM_IDLE_NOT_READY;
__IO PPM_STATE PPM_PreviousState = PPM_IDLE_NOT_READY;
/* Private function prototypes -----------------------------------------------*/
/** @defgroup USBPD_CORE_PPM_Private_Functions USBPD CORE PPM Private Functions
* @{
*/
static void PPM_ManageCommandNotReady(PUCSI_CONTROL_t PtrControl);
static void PPM_ManageCommandReady(PUCSI_CONTROL_t PtrControl);
static void PPM_NotifyOPM(uint32_t Command, uint32_t Event);
static USBPD_StatusTypeDef PPM_FillDefaultConfiguration(void);
static USBPD_StatusTypeDef PPM_FillConnectorCapability(uint8_t PortNumber,
PUCSI_GET_CONNECTOR_CAPABILITY_IN_t PtrConnectorCapability);
static USBPD_StatusTypeDef PPM_Reset(void);
static USBPD_StatusTypeDef PPM_Cancel(void);
static USBPD_StatusTypeDef PPM_ConnectorReset(uint8_t PortNumber, uint8_t HardReset);
static USBPD_StatusTypeDef PPM_AckCcCi(uint8_t ConnectorChangeAck, uint8_t CommandCompletedAck);
static USBPD_StatusTypeDef PPM_SetNotificationEnable(uint16_t SetNotificationEnable);
static USBPD_StatusTypeDef PPM_GetCapabilityStatus(void);
static USBPD_StatusTypeDef PPM_GetConnectorCapabilityStatus(uint8_t PortNumber);
#if FEATURE_SET_UOM_SUPPORTED
static USBPD_StatusTypeDef PPM_SetCC_OperationMode(uint8_t PortNumber, UCSI_CC_OPERATION_MODE_t Mode);
#endif /* FEATURE_SET_UOM_SUPPORTED */
static USBPD_StatusTypeDef PPM_SetUSB_OperationRole(uint8_t PortNumber, UCSI_USB_OPERATION_ROLE_t Role);
#if FEATURE_SET_PDM_SUPPORTED
static USBPD_StatusTypeDef PPM_SetPowerDirectionMode(uint8_t PortNumber, UCSI_POWER_DIRECTION_MODE_t PowerMode);
#endif /* FEATURE_SET_PDM_SUPPORTED */
static USBPD_StatusTypeDef PPM_SetPowerDirectionRole(uint8_t PortNumber, UCSI_POWER_DIRECTION_ROLE_t PowerRole);
#if FEATURE_ALTERNATEMODEDETAILS_SUPPORTED
static USBPD_StatusTypeDef PPM_GetAlternateModes(uint8_t PortNumber,
PUCSI_GET_ALTERNATE_MODES_COMMAND PtrGetAlternatesModes);
static USBPD_StatusTypeDef PPM_GetCamSupported(uint8_t PortNumber);
static USBPD_StatusTypeDef PPM_GetCurrentCam(uint8_t PortNumber);
#if FEATURE_ALTERNATEMODEOVERRIDE_SUPPORTED
static USBPD_StatusTypeDef PPM_SetNewCam(uint8_t PortNumber, uint8_t EnterOrExit, uint8_t NewCAM, uint32_t AMSpecific);
#endif /* FEATURE_ALTERNATEMODEOVERRIDE_SUPPORTED */
#endif /* FEATURE_ALTERNATEMODEDETAILS_SUPPORTED */
#if FEATURE_PDODETAILS_SUPPORTED
static USBPD_StatusTypeDef PPM_GetPDOs(uint8_t PortNumber, PUCSI_GET_PDOS_COMMAND_t PtrGetPdos);
#endif /* FEATURE_PDODETAILS_SUPPORTED */
#if FEATURE_CABLEDETAILS_SUPPORTED
static USBPD_StatusTypeDef PPM_GetCableProperty(uint8_t PortNumber);
#endif /* FEATURE_CABLEDETAILS_SUPPORTED */
static USBPD_StatusTypeDef PPM_GetConnectorStatus(uint8_t PortNumber);
static USBPD_StatusTypeDef PPM_GetErrorStatus(void);
#if FEATURE_SET_POWER_LEVEL_SUPPORTED
static USBPD_StatusTypeDef PPM_SetPowerLevel(PUCSI_SET_POWER_LEVEL_COMMAND PtrSetPowerLevel);
#endif /* FEATURE_SET_POWER_LEVEL_SUPPORTED */
#if FEATURE_GET_PD_MESSAGE_SUPPORTED
static USBPD_StatusTypeDef PPM_GetPDMessage(PUCSI_GET_PD_MESSAGE_COMMAND PtrGetPDMessage);
#endif /* FEATURE_GET_PD_MESSAGE_SUPPORTED */
static USBPD_StatusTypeDef PPM_UnknownCommand(uint8_t PortNumber);
extern uint32_t HAL_GetTick(void);
/**
* @}
*/
/** @defgroup USBPD_CORE_PPM_Exported_Functions USBPD CORE PPM Exported Functions
* @{
*/
#define UCSI_CONNECTOR_IDLE 0x00U
#define UCSI_CONNECTOR_CHANGE 0x01U
uint8_t ConnectorChange[USBPD_PORT_COUNT];
/**
* @brief Initialize the default configuration used by the USB-PD stack
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PPM_Init(void)
{
uint32_t index = 0;
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_INIT, NULL, 0U);
/* Initialize command lists */
PPM_Handle.Error = 0U;
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
PPM_FillDefaultConfiguration();
/* Initialize each USB-PD connector capability */
for (index = 0; index < USBPD_PORT_COUNT; index++)
{
PPM_FillConnectorCapability(index, &PPM_Handle.ConnectorCapability[index]);
ConnectorChange[index] = UCSI_CONNECTOR_IDLE;
}
#if defined(_RTOS)
#if (osCMSIS < 0x20000U)
osMessageQDef(MsgBox, PPM_BOX_MESSAGES_MAX, uint32_t);
PPMMsgBox = osMessageCreate(osMessageQ(MsgBox), NULL);
if (NULL == osThreadCreate(osThread(PPM), &PPMMsgBox))
#else
PPMMsgBox = osMessageQueueNew(PPM_BOX_MESSAGES_MAX, sizeof(uint32_t), NULL);
if (NULL == osThreadNew(USBPD_PPM_UserExecute, &PPMMsgBox, &PPM_Thread_Atrr))
#endif /* osCMSIS < 0x20000U */
{
return USBPD_ERROR;
}
#elif defined(USE_STM32_UTILITY_OS)
#else
#warning " NRTOS Version not implemented"
#endif /* _RTOS */
return USBPD_OK;
}
#if defined(_RTOS)
#if (osCMSIS < 0x20000U)
static void USBPD_PPM_UserExecute(void const *argument)
#else
static void USBPD_PPM_UserExecute(void *argument)
#endif /* osCMSIS < 0x20000U */
{
uint32_t _timing = osWaitForever;
static uint32_t tickstart = 0;
osMessageQId queue = *(osMessageQId *)argument;
do
{
#if (osCMSIS < 0x20000U)
osEvent event = osMessageGet(queue, _timing);
switch (((PPM_USER_EVENT)event.value.v & MASK_EVENT))
#else
uint32_t event;
(void)osMessageQueueGet(queue, &event, NULL, _timing);
switch (((PPM_USER_EVENT)event & MASK_EVENT))
#endif /* osCMSIS < 0x20000U */
{
case PPM_USER_EVENT_COMMAND:
{
PUCSI_CONTROL_t ptr_control = (PUCSI_CONTROL_t)&PPM_Handle.Control;
switch (PPM_State)
{
case PPM_IDLE_NOT_READY: /* PPM Idle (notification disabled) */
PPM_ManageCommandNotReady(ptr_control);
break;
default:
PPM_ManageCommandReady(ptr_control);
break;
}
break;
}
case PPM_USER_EVENT_NOTIFICATION:
case PPM_USER_EVENT_NONE:
case PPM_USER_EVENT_TIMER:
default:
break;
}
/* Manage PPM State Machine */
switch (PPM_State)
{
case PPM_IDLE_READY: /* PPM Idle (notification enabled) */
/* Save time, for timeout mechanism */
tickstart = HAL_GetTick();
if (0U != AsynchEventPending)
{
SWITCH_TO_PPM_STATE(PPM_WAIT_FOR_ASYNCH_EVENT_ACK);
}
break;
case PPM_IDLE_NOT_READY: /* PPM Idle (notification disabled) */
AsynchEventPending = 0U;
break;
case PPM_WAIT_FOR_ASYNCH_EVENT_ACK: /* Wait for asynch event ack */
if (0U != AsynchEventPending)
{
/** Acknowledge Command Indicator:
* The PPM shall set this field to one when it completes the ACK_CC_CI
* (Acknowledge Command Completion and/or Change Indication) command. The PPM shall automatically reset this
* bit when it receives the next command from the OPM.
* If this field is set to one, then the only other field that can be set is the Connector Change
* Indicator field.
*/
PPM_Handle.AckCCI_Status.AsUInt32 &= 0x20000000;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
if (PPM_Handle.Error == 1U)
{
PPM_Handle.AckCCI_Status.ErrorIndicator = 1U;
PPM_Handle.Error = 0U;
}
PPM_NotifyOPM(0U, 1U);
PPM_TRACE(PPM_DEBUG_LEVEL_1, UCSI_TRACE_PPM_EVENT, (uint8_t *)&PPM_Handle.AckCCI_Status.AsUInt32, 4U);
/* Clear Async event pending */
AsynchEventPending = 0U;
}
break;
case PPM_BUSY: /* PPM Busy */
case PPM_PROCESSING_COMMAND: /* Processing command */
case PPM_WAIT_FOR_CMD_COMPL_ACK: /* Wait for command completion ack */
if (PPM_Handle.AckCCI_Status.AcknowledgeCommandIndicator)
{
/* Switch to PPM Idle (Notification enabled) */
SWITCH_TO_PPM_STATE(PPM_IDLE_READY);
}
else
{
/* Manage timeout in case OPM never ack command */
if ((HAL_GetTick() - tickstart) > PPM_WAIT_ACK_TIMEOUT) /* 1s timeout */
{
PPM_DEBUG(PPM_DEBUG_LEVEL_1, USBPD_PORT_0,
"PPM_WAIT_FOR_CMD_COMPL_ACK timeout. Switching to state PPM_IDLE_READY");
/* Abort waiting */
PPM_Handle.AckCCI_Status.AcknowledgeCommandIndicator = 1U;
SWITCH_TO_PPM_STATE(PPM_IDLE_READY);
}
}
default:
break;
}
if (0U != AsynchEventPending)
{
/* 2 Asynch has been raised. 1st one has been correctly managed. */
_timing = 0U;
}
else
{
_timing = osWaitForever;
}
} while (1);
}
#elif defined(USE_STM32_UTILITY_OS)
#else
#warning "NRTOS Version not implemented"
#endif /* _RTOS */
/**
* @brief Get data pointer saved in PPM Handle
* @param Register
* @retval Pointer of data
*/
uint8_t *USBPD_PPM_GetDataPointer(UCSI_REG_t Register)
{
#if defined(_TRACE)
if (Register < UcsiRegDataStructureUnkown)
{
PPM_TRACE(PPM_DEBUG_LEVEL_2, UCSI_TRACE_PPM_GET_REG, (uint8_t *)&Register, 1U);
}
#endif /* _TRACE */
uint8_t *ptr = NULL;
switch (Register)
{
case UcsiRegDataStructureVersion:
ptr = (uint8_t *)&PPM_Handle.UcsiVersion;
break;
case UcsiRegDataStructureCci:
ptr = (uint8_t *)&PPM_Handle.AckCCI_Status;
break;
case UcsiRegDataStructureControl:
ptr = (uint8_t *)&PPM_Handle.Control;
break;
case UcsiRegDataStructureMessageIn:
ptr = (uint8_t *)&PPM_Handle.MessageIn;
PPM_TRACE(PPM_DEBUG_LEVEL_1, UCSI_TRACE_PPM_GET_REG, (uint8_t *)&PPM_Handle.MessageIn, UCSI_MAX_DATA_LENGTH);
break;
case UcsiRegDataStructureMessageOut:
ptr = (uint8_t *)&PPM_Handle.MessageOut;
break;
case UcsiRegDataStructureUnkown:
case UcsiRegDataStructureReserved:
default:
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_GET_REG_ERROR_READ, (uint8_t *)&PPM_Handle.AckCCI_Status.AsUInt32, 4U);
break;
}
return ptr;
}
/**
* @brief Callback function called by PE to inform DPM about PE event.
* @param PortNumber The current port number
* @param EventVal USBPD_NotifyEventValue_TypeDef
* @retval None
*/
void USBPD_PPM_Notification(uint8_t PortNumber, uint32_t EventType)
{
/*
Mandatory:
PPM_Handle.SetNotificationEnable.CommandComplete;
PPM_Handle.SetNotificationEnable.PowerOperationModeChange;
PPM_Handle.SetNotificationEnable.BatteryChargingStatusChange;
PPM_Handle.SetNotificationEnable.ConnectorPartnerChange;
PPM_Handle.SetNotificationEnable.PowerDirectionChange;
PPM_Handle.SetNotificationEnable.ConnectChange;
PPM_Handle.SetNotificationEnable.Error;
Optional: */
#if FEATURE_EXTERNALSUPPLYNOTIFICATION_SUPPORTED
PPM_Handle.SetNotificationEnable.ExternalSupplyChange;
#endif /* FEATURE_EXTERNALSUPPLYNOTIFICATION_SUPPORTED */
#if FEATURE_PDODETAILS_SUPPORTED
PPM_Handle.SetNotificationEnable.SupportedProviderCapabilitiesChange = 1U;
PPM_Handle.SetNotificationEnable.NegotiatedPowerLevelChange = 1U;
#endif /* FEATURE_PDODETAILS_SUPPORTED */
#if FEATURE_PDRESETNOTIFICATION_SUPPORTED
PPM_Handle.SetNotificationEnable.PdResetComplete;
#endif /* FEATURE_PDRESETNOTIFICATION_SUPPORTED */
#if FEATURE_ALTERNATEMODEDETAILS_SUPPORTED
PPM_Handle.SetNotificationEnable.SupportedCamChange;
#endif /* FEATURE_ALTERNATEMODEDETAILS_SUPPORTED */
if (PPM_Handle.SetNotificationEnable.ConnectChange)
{
switch (EventType)
{
case USBPD_NOTIFY_DATAROLESWAP_NOT_SUPPORTED:
PPM_Handle.Error = 1U;
/* Event for attach */
case USBPD_NOTIFY_POWER_EXPLICIT_CONTRACT :
/* Event for detach */
case USBPD_NOTIFY_DETACH :
/* Event for Data Role Swap */
case USBPD_NOTIFY_DATAROLESWAP_UFP:
case USBPD_NOTIFY_DATAROLESWAP_DFP:
case USBPD_NOTIFY_USBSTACK_START:
case USBPD_NOTIFY_PE_DISABLED:
/* A notification has been detected */
{
uint32_t event = SET_PORT_EVENT(PortNumber, PPM_USER_EVENT_NOTIFICATION);
ConnectorChange[PortNumber] = UCSI_CONNECTOR_CHANGE;
/* Wake up PPM only if there are no pending Asynck event */
if (AsynchEventPending == 0U)
{
AsynchEventPending = (PortNumber + 1U);
#if defined(_RTOS)
#if (osCMSIS < 0x20000U)
if (osOK != osMessagePut(PPMMsgBox, event, 0U))
{
PPM_DEBUG(PPM_DEBUG_LEVEL_0, PortNumber, "NOTIF1: MB FULL\r\n");
}
#else
(void)osMessageQueuePut(PPMMsgBox, &event, 0U, NULL);
#endif /* osCMSIS < 0x20000U */
#elif defined(USE_STM32_UTILITY_OS)
#else
#warning " NRTOS Version not implemented"
#endif /* _RTOS */
}
}
break;
default:
break;
}
}
/* No notifications match */
return;
}
void USBPD_PPM_PostCommand(void)
{
uint32_t event = PPM_USER_EVENT_COMMAND;
#if defined(_RTOS)
#if (osCMSIS < 0x20000U)
if (osOK != osMessagePut(PPMMsgBox, event, 0U))
{
PPM_DEBUG(PPM_DEBUG_LEVEL_0, 0U, "COMMAND: MB FULL\r\n");
}
#else
(void)osMessageQueuePut(PPMMsgBox, &event, 0U, NULL);
#endif /* osCMSIS < 0x20000U */
#elif defined(USE_STM32_UTILITY_OS)
#else
#warning " NRTOS Version not implemented"
#endif /* _RTOS */
/* PPM Update Command Pending */
CommandPending = 1U;
}
/**
* @}
*/
/** @addtogroup USBPD_CORE_PPM_Private_Functions
* @{
*/
static void PPM_ManageCommandNotReady(PUCSI_CONTROL_t PtrControl)
{
UCSI_COMMAND_t command = (UCSI_COMMAND_t)PtrControl->Command;
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_NOT_READY, (uint8_t *)PtrControl, 8U);
if (UcsiCommandPpmReset == command)
{
/* Disable all the notifications */
PPM_Reset();
}
else if (UcsiCommandSetNotificationEnable == command)
{
PPM_SetNotificationEnable(PtrControl->SetNotificationEnable.Content.NotificationEnable);
PPM_NotifyOPM(1U, 0U);
SWITCH_TO_PPM_STATE(PPM_WAIT_FOR_CMD_COMPL_ACK);
}
/* Clear Command pending */
CommandPending = 0U;
}
static void PPM_ManageCommandReady(PUCSI_CONTROL_t PtrControl)
{
UCSI_COMMAND_t command = (UCSI_COMMAND_t)PtrControl->Command;
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY, (uint8_t *)PtrControl, 8U);
if (UcsiCommandPpmReset == command)
{
/* Disable all the notifications */
PPM_Reset();
}
else if (UcsiCommandCancel == command)
{
/* Cancel current operation if not yet processing */
PPM_Cancel();
/* Notificy OPM */
PPM_NotifyOPM(1U, 0U);
}
else
{
/* if PPM is busy or command need to take more one UCSI_MIN_TIME_TO_RESPOND_WITH_BUSY ms set busy indicator */
if (PPM_BUSY == PPM_State) /* || Cmd Need UCSI_MIN_TIME_TO_RESPOND_WITH_BUSY */
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.BusyIndicator = 1U;
/* Notificy OPM */
PPM_NotifyOPM(1U, 0U);
SWITCH_TO_PPM_STATE(PPM_PROCESSING_COMMAND);
}
else
{
switch (command)
{
case UcsiCommandConnectorReset:
{
CurrentPort = (PtrControl->ConnectorReset.ConnectorNumber - 1U);
PPM_ConnectorReset(CurrentPort, PtrControl->ConnectorReset.HardReset);
}
break;
case UcsiCommandAckCcCi:
CurrentPort = (PtrControl->ConnectorReset.ConnectorNumber - 1U);
PPM_AckCcCi(PtrControl->AckCcCi.ConnectorChangeAcknowledge, PtrControl->AckCcCi.CommandCompletedAcknowledge);
break;
case UcsiCommandSetNotificationEnable:
PPM_SetNotificationEnable(PtrControl->SetNotificationEnable.Content.NotificationEnable);
break;
case UcsiCommandGetCapability:
PPM_GetCapabilityStatus();
break;
case UcsiCommandGetConnectorCapability:
CurrentPort = (PtrControl->GetConnectorCapability.ConnectorNumber - 1U);
PPM_GetConnectorCapabilityStatus(CurrentPort);
break;
case UcsiCommandSetUor:
{
CurrentPort = (PtrControl->SetUor.ConnectorNumber - 1U);
PPM_SetUSB_OperationRole(CurrentPort, (UCSI_USB_OPERATION_ROLE_t)PtrControl->SetUor.UsbOperationRole);
}
break;
case UcsiCommandSetPdr:
{
CurrentPort = (PtrControl->SetPdr.ConnectorNumber - 1U);
PPM_SetPowerDirectionRole(CurrentPort, (UCSI_POWER_DIRECTION_ROLE_t)PtrControl->SetPdr.PowerDirectionRole);
}
break;
case UcsiCommandGetConnectorStatus:
{
CurrentPort = (PtrControl->GetConnectorStatus.ConnectorNumber - 1U);
PPM_GetConnectorStatus(CurrentPort);
}
break;
case UcsiCommandGetErrorStatus:
PPM_GetErrorStatus();
break;
case UcsiCommandSetCCom:
CurrentPort = (PtrControl->SetUom.ConnectorNumber - 1U);
#if FEATURE_SET_UOM_SUPPORTED
PPM_SetCC_OperationMode(CurrentPort, (UCSI_CC_OPERATION_MODE_t)PtrControl->SetUom.UsbOperationMode);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_SET_UOM_SUPPORTED */
break;
/* Obsolete from Rev1.2, Jan20*/
case UcsiCommandSetPdm:
CurrentPort = (PtrControl->SetPdm.ConnectorNumber - 1U);
#if FEATURE_SET_PDM_SUPPORTED
PPM_SetPowerDirectionMode(CurrentPort, (UCSI_POWER_DIRECTION_MODE_t)PtrControl->SetPdm.PowerDirectionMode);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_SET_PDM_SUPPORTED */
break;
case UcsiCommandGetAlternateModes:
CurrentPort = (PtrControl->GetAlternateModes.ConnectorNumber - 1U);
#if FEATURE_ALTERNATEMODEDETAILS_SUPPORTED
PPM_GetAlternateModes(CurrentPort, &PtrControl->GetAlternateModes);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_ALTERNATEMODEDETAILS_SUPPORTED */
break;
case UcsiCommandGetCamSupported:
CurrentPort = (PtrControl->GetCamSupported.ConnectorNumber - 1U);
#if FEATURE_ALTERNATEMODEDETAILS_SUPPORTED
PPM_GetCamSupported(CurrentPort);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_ALTERNATEMODEDETAILS_SUPPORTED */
break;
case UcsiCommandGetCurrentCam:
CurrentPort = (PtrControl->GetCurrentCam.ConnectorNumber - 1U);
#if FEATURE_ALTERNATEMODEDETAILS_SUPPORTED
PPM_GetCurrentCam(CurrentPort);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_ALTERNATEMODEDETAILS_SUPPORTED */
break;
case UcsiCommandSetNewCam:
CurrentPort = (PtrControl->SetNewCam.ConnectorNumber - 1U);
#if FEATURE_ALTERNATEMODEDETAILS_SUPPORTED && FEATURE_ALTERNATEMODEOVERRIDE_SUPPORTED
PPM_SetNewCam(CurrentPort, PtrControl->SetNewCam.EnterOrExit, PtrControl->SetNewCam.NewCam,
PtrControl->SetNewCam.AmSpecific);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_ALTERNATEMODEDETAILS_SUPPORTED && FEATURE_ALTERNATEMODEOVERRIDE_SUPPORTED */
break;
case UcsiCommandGetPdos:
CurrentPort = (PtrControl->GetPdos.ConnectorNumber - 1U);
#if FEATURE_PDODETAILS_SUPPORTED
PPM_GetPDOs(CurrentPort, &PtrControl->GetPdos);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_PDODETAILS_SUPPORTED */
break;
case UcsiCommandGetCableProperty:
CurrentPort = (PtrControl->GetCableProperty.ConnectorNumber - 1U);
#if FEATURE_CABLEDETAILS_SUPPORTED
PPM_GetCableProperty(CurrentPort);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_CABLEDETAILS_SUPPORTED */
break;
case UcsiCommandSetPowerLevel:
CurrentPort = (PtrControl->SetPowerLevel.ConnectorNumber - 1U);
#if FEATURE_SET_POWER_LEVEL_SUPPORTED
PPM_SetPowerLevel(PtrControl->SetPowerLevel);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_SET_POWER_LEVEL_SUPPORTED */
break;
case UcsiCommandGetPDMessage:
CurrentPort = (PtrControl->GetPDMessage.ConnectorNumber - 1U);
#if FEATURE_GET_PD_MESSAGE_SUPPORTED
PPM_GetPDMessage(PtrControl->GetPDMessage);
#else
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
#endif /* FEATURE_GET_PD_MESSAGE_SUPPORTED */
break;
default:
PPM_TRACE(PPM_DEBUG_LEVEL_0, UCSI_TRACE_PPM_READY_UNKNOWN, NULL, 0U);
PPM_UnknownCommand(CurrentPort);
break;
}
/* Notificy OPM */
PPM_NotifyOPM(1U, 0U);
SWITCH_TO_PPM_STATE(PPM_WAIT_FOR_CMD_COMPL_ACK);
}
}
/* Clear Command pending */
CommandPending = 0U;
}
static void PPM_NotifyOPM(uint32_t Command, uint32_t Event)
{
/* Notify OPM only if "Command Completed" notification is enabled */
if (1U == Command)
{
/* Notify only if Command complete is enabled */
if (1U == PPM_Handle.SetNotificationEnable.CommandComplete)
{
USBPD_UCSI_SendNotification(CurrentPort);
}
}
else
{
/* Check if associated events is enabled */
USBPD_UCSI_SendNotification(CurrentPort);
}
/* Cleart the ALERT */
USBPD_UCSI_ClearAlert(CurrentPort);
}
/**
* @brief Manage reception of PPM_RESET message (Required)
* @retval USBPD status
*/
static USBPD_StatusTypeDef PPM_Reset(void)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.ResetCompletedIndicator = 1U;
/* Disable all the notifications */
PPM_Handle.SetNotificationEnable.NotificationEnable = 0x0000U;
/* USB Operation Mode should be reset as default value DRP
(can be changed with Set USB Operation Mode command) */
for (uint32_t index = 0U; index < USBPD_PORT_COUNT; index++)
{
PPM_Handle.CC_Mode[index] = (UCSI_CC_OPERATION_MODE_t)(PPM_Handle.ConnectorCapability[index].OperationMode.AsUInt8 &
0x07U);
}
/* Switch state to PPM Idle (Notifications disabled) */
SWITCH_TO_PPM_STATE(PPM_IDLE_NOT_READY);
return USBPD_OK;
}
/**
* @brief Manage reception of CANCEL message (Required)
* @retval USBPD status
*/
static USBPD_StatusTypeDef PPM_Cancel(void)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.CancelCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.CommandCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
return USBPD_OK;
}
/**
* @brief Manage reception of CONNECTOR_RESET message (Required)
* @param PortNumber Port Number value
* @param HardReset Request an hard or soft reset
* @retval USBPD status
*/
static USBPD_StatusTypeDef PPM_ConnectorReset(uint8_t PortNumber, uint8_t HardReset)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.CommandCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
if (HardReset)
{
/* Send a hard reset */
USBPD_DPM_RequestHardReset(PortNumber);
}
else
{
/* Send a soft reset */
/* Soft reset cannot be initiated by application. This is only a protocol error detected by the stack */
}
return USBPD_OK;
}
/**
* @brief Manage reception of ACK_CC_CCI message (Required)
* @param ConnectorChangeAck Acknowledge a connector change
* @param CommandCompletedAck Acknowledge a command completed
* @retval USBPD status
*/
static USBPD_StatusTypeDef PPM_AckCcCi(uint8_t ConnectorChangeAck, uint8_t CommandCompletedAck)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.AcknowledgeCommandIndicator = 1U;
/* Reset the ConnectorStatusChange */
(&PPM_Handle.MessageIn.ConnectorStatus)->ConnectorStatusChange.AsUInt16 = 0x0000U;
if (ConnectorChangeAck)
{
uint32_t index;
for (index = 0U; index < USBPD_PORT_COUNT; index++)
{
if (ConnectorChange[index] == UCSI_CONNECTOR_CHANGE)
{
AsynchEventPending = (index + 1U);
break;
}
}
}
return USBPD_OK;
}
/**
* @brief Enable Notification (Required)
* @param SetNotificationEnable Value of the SET_NOTIF_ENABLE
* @retval None
*/
static USBPD_StatusTypeDef PPM_SetNotificationEnable(uint16_t SetNotificationEnable)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.CommandCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
PPM_Handle.SetNotificationEnable.NotificationEnable = SetNotificationEnable;
return USBPD_OK;
}
/**
* @brief Acknowledge GET_CAPABILITY message (Required)
* @retval None
*/
static USBPD_StatusTypeDef PPM_GetCapabilityStatus(void)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.DataLength = 0x10U;
PPM_Handle.AckCCI_Status.CommandCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
/* If the command completed successfully then PPM shall copy the
capability in the message_in as described in table 4-13 GET_CAPABILITY Data */
memcpy(&PPM_Handle.MessageIn.Capability, &PPM_Handle.Capability, sizeof(UCSI_GET_CAPABILITY_IN_t));
return USBPD_OK;
}
/**
* @brief Acknowledge GET_CONNECTOR_CAPABILITY message (Required)
* @param PortNumber Port Number value
* @retval None
*/
static USBPD_StatusTypeDef PPM_GetConnectorCapabilityStatus(uint8_t PortNumber)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.DataLength = 2U;
PPM_Handle.AckCCI_Status.CommandCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
/* If the command completed successfully then PPM shall copy the
capability in the message_in as described in table 4-13 GET_CAPABILITY Data */
memcpy(&PPM_Handle.MessageIn.ConnectorCapability, &PPM_Handle.ConnectorCapability[PortNumber],
sizeof(UCSI_GET_CONNECTOR_CAPABILITY_IN_t));
return USBPD_OK;
}
#if FEATURE_SET_UOM_SUPPORTED
/**
* @brief Set CC operation mode in USB-PD stack (Optional)
* @param PortNumber Port Number value
* @param Mode USB Mode can take the following value
* @arg UcsiCCOperationModeRpOnly
* @arg UcsiCCOperationModeRdOnly
* @arg UcsiCCOperationModeDrp
* @retval USBPD status
*/
static USBPD_StatusTypeDef PPM_SetCC_OperationMode(uint8_t PortNumber, UCSI_CC_OPERATION_MODE_t Mode)
{
/* Reset Status variable */
PPM_Handle.AckCCI_Status.AsUInt32 = 0U;
/* Acknowledge the command */
PPM_Handle.AckCCI_Status.CommandCompletedIndicator = 1U;
PPM_Handle.AckCCI_Status.ConnectorChangeIndicator = AsynchEventPending;
/* Possible only if port is DRP */
if (1U == DPM_Settings[PortNumber].PE_RoleSwap)