-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCCHandshake.c
executable file
·1538 lines (1208 loc) · 39 KB
/
CCHandshake.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
/**
* usbc-pd-fusb302-d: Library for ONSEMI FUSB302-D (USB-C Controller) for PD negotiation
* Copyright (C) 2020 Philip Tschiemer https://filou.se
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "CCHandshake.h"
#include "HW_i2c.h"
static FUSB302_D_t Driver;
#if ONSEMI_LIBRARY==true
#include "Platform_ARM/app/HWIO.h"
#include "Platform_ARM/app/TimeDelay.h"
#include "Platform_ARM/app/Timing.h"
#include "Platform_ARM/app/PlatformI2C.h"
//#include "core/TypeC.h"
#ifdef PWR
#undef PWR
#endif
#ifdef COMP
#undef COMP
#endif
#include "core/fusb30X.h"
extern DeviceReg_t Registers;
#else
// #include "main.h"
#include <string.h>
#include "PD.h"
#include "util-string.h" // https://github.com/tschiemer/c-utils
typedef enum {
PD_State_Disabled,
PD_State_HardReset,
PD_State_Reset,
PD_State_Idle,
PD_State_Rx,
PD_State_AwaitGoodCRC,
PD_State_Resend,
} PD_State_t;
//static uint8_t * regPtr( FUSB302_D_Register_t reg );
static bool read( FUSB302_D_Register_t reg, uint8_t * value );
static bool write( FUSB302_D_Register_t reg, uint8_t value );
static bool readAll( void );
static bool readStatus( void );
static bool configure( void );
static void typeC_core( void );
static bool readCCVoltageLevel( uint8_t * bc_lvl );
static CCHandshake_CC_t detectCCPinSink( void );
static bool enableSink( CCHandshake_CC_t cc );
static bool disableSink();
static void pd_core( void );
static void pd_init( void );
static void pd_deinit( void );
static bool pd_hasMessage( void );
static bool pd_getMessage( PD_Message_t * message );
static bool pd_sendMessage( PD_Message_t * message, PD_State_t (*onAcknowledged)( PD_Message_t * message) );
static PD_State_t pd_processMessage( PD_Message_t * message );
static PD_State_t pd_onSourceCapabilities( PD_Message_t * message );
static void pd_createRequest( PD_Message_t * message );
static void pd_hardreset( void );
static void pd_reset( void );
//static void pd_setRoles( CCHandshake_PD_Role_t powerRole, CCHandshake_PD_Role_t dataRole )
//static void pd_setAutoGoodCrc( bool enabled );
static void pd_flushRxFifo( void );
static void pd_flushTxFifo( void );
static void pd_startTx( void );
static FUSB302_D_Registers_st Registers;
static volatile CCHandshake_CC_t ConnectedCC;
static struct {
volatile PD_State_t State;
struct {
uint8_t MessageId;
PD_Message_t Message;
bool HasData;
} Rx;
struct {
uint8_t MessageId;
PD_Message_t Message;
PD_State_t (*OnAcknowledged)( PD_Message_t * message );
TimerTime_t SentTs;
uint8_t SendAttempts;
} Tx;
struct {
uint8_t NSourceCapabilities;
PD_DataObject_t SourceCapabilities[PD_MESSAGE_MAX_OBJECTS];
uint8_t BestCapIndex;
// PD_DataObject_t BestCap;
} Power;
} PD;
static inline void pd_clearTx( void )
{
memset( (uint8_t*)&PD.Tx.Message, 0, sizeof(PD_Message_t) );
}
static inline uint8_t pd_nextTxMessageId( void )
{
// uint8_t mid = PD.Rx.MessageId + 1;
uint8_t mid = PD.Tx.MessageId;
PD.Tx.MessageId = (PD.Tx.MessageId + 1) % PD_MESSAGE_MAX_MID;
return mid;
}
#endif
void CCHandshake_init( void )
{
FUSB302_D_Init( &Driver, HW_I2C_Handle(), I2CX_IRQn, FUSB302_D_DEFAULT_ADDRESS );
if (FUSB302_D_Probe( &Driver, 3, 100 ) == FUSB302_D_ERROR )
{
Error_Handler( ErrorCodeCCHandshakeFail );
}
#if ONSEMI_LIBRARY==true
// InitializeBoard();
// InitializeDelay();
InitializeCoreTimer();
core_initialize();
DBG( "CC pid=%d v=%d rev=%d\n", Registers.DeviceID.PRODUCT_ID, Registers.DeviceID.VERSION_ID, Registers.DeviceID.REVISION_ID);
core_enable_typec( TRUE ); // Enable the state machine by default
// SetStateUnattached();
#else
ConnectedCC = CCHandshake_CC_None;
// read( FUSB302_D_Register_Reset, );
// Registers.Reset |= FUSB302_D_Reset_SW_RES;
write( FUSB302_D_Register_Reset, FUSB302_D_Reset_SW_RES );
// Registers.Reset &= ~FUSB302_D_Reset_SW_RES;
if (readAll() == false)
{
Error_Handler( ErrorCodeCCHandshakeFail );
}
if (configure() == false)
{
Error_Handler( ErrorCodeCCHandshakeFail );
}
PD.State = PD_State_Disabled;
#endif
}
void CCHandshake_deinit( void )
{
#if ONSEMI_LIBRARY==false
ConnectedCC = CCHandshake_CC_None;
PD.State = PD_State_Disabled;
#endif
}
CCHandshake_CC_t CCHandshake_getOrientation( void )
{
#if ONSEMI_LIBRARY==true
return CCHandshake_CC_None;
#else
return ConnectedCC;
#endif
}
void CCHandshake_core( void )
{
#if ONSEMI_LIBRARY==true
core_state_machine();
#else
#if CCHANDSHAKE_AUTONOMOUS==true
#else
// while(1){
// read all essential registers
readStatus();
// if ( Registers.Interrupt != 0 ){
// DBG("Interrupt %02x\n", Registers.Interrupt);
// }
// if ( Registers.Interrupta != 0 ){
// DBG("Interrupt %02x\n", Registers.Interrupta);
// }
// if ( Registers.Interruptb != 0 ){
// DBG("Interrupt %02x\n", Registers.Interruptb);
// }
typeC_core();
pd_core();
// }
#endif
#endif /* ONSEMI_LIBRARY */
}
#if ONSEMI_LIBRARY==true
FSC_BOOL platform_get_device_irq_state( void )
{
uint8_t reg = 0;
DeviceRead(regInterrupta, 2, &Registers.Status.InterruptAdv);
DeviceRead(regInterrupt, 1, &Registers.Status.Interrupt1);
if (Registers.Status.InterruptAdv != 0 || Registers.Status.Interrupt1 != 0)
{
// DBG("interruptAdv %04X \t interrupt1 %04X\n", Registers.Status.InterruptAdv, Registers.Status.Interrupt1 );
if (Registers.Status.I_OCP_TEMP){ DBG("I_OCP_TEMP " ); }
if (Registers.Status.I_TOGDONE){ DBG("I_TOGDONE " ); }
if (Registers.Status.I_SOFTFAIL){ DBG("I_SOFTFAIL " ); }
if (Registers.Status.I_RETRYFAIL){ DBG("I_RETRYFAIL " ); }
if (Registers.Status.I_HARDSENT){ DBG("I_HARDSENT " ); }
if (Registers.Status.I_TXSENT){ DBG("I_TXSENT " ); }
if (Registers.Status.I_SOFTRST){ DBG("I_SOFTRST " ); }
if (Registers.Status.I_HARDRST){ DBG("I_HARDRST " ); }
if (Registers.Status.I_GCRCSENT){ DBG("I_GCRCSENT " ); }
if (Registers.Status.I_VBUSOK){ DBG("I_VBUSOK " ); }
if (Registers.Status.I_ACTIVITY){ DBG("I_ACTIVITY " ); }
if (Registers.Status.I_COMP_CHNG){ DBG("I_COMP_CHNG " ); }
if (Registers.Status.I_CRC_CHK){ DBG("I_CRC_CHK " ); }
if (Registers.Status.I_ALERT){ DBG("I_ALERT " ); }
if (Registers.Status.I_WAKE){ DBG("I_WAKE " ); }
if (Registers.Status.I_COLLISION){ DBG("I_COLLISION " ); }
if (Registers.Status.I_BC_LVL){ DBG("I_BC_LVL " ); }
DBG("\n");
}
return (Registers.Status.Interrupt1 != 0 || Registers.Status.InterruptAdv != 0);
//
// if (FUSB302_D_Read( &Driver, FUSB302_D_Register_Interrupta, ®) == FUSB302_D_OK)
// {
// if (reg != interrupta)
// {
// interrupta = reg;
// return TRUE;
// }
// }
//
// if (FUSB302_D_Read( &Driver, FUSB302_D_Register_Interruptb, ®) == FUSB302_D_OK)
// {
// if (reg != interruptb)
// {
// interruptb = reg;
// return TRUE;
// }
// }
//
// if (FUSB302_D_Read( &Driver, FUSB302_D_Register_Status0, ®) == FUSB302_D_OK)
// {
// if (reg != status0)
// {
// status0 = reg;
// return TRUE;
// }
// }
//
// if (FUSB302_D_Read( &Driver, FUSB302_D_Register_Status1, ®) == FUSB302_D_OK)
// {
// if (reg != status1)
// {
// status1 = reg;
// return TRUE;
// }
// }
//
// if (FUSB302_D_Read( &Driver, FUSB302_D_Register_Interrupt, ®) == FUSB302_D_OK)
// {
// if (reg != interrupt)
// {
// interrupt = reg;
// return TRUE;
// }
// }
}
#else
static void typeC_core( void )
{
// if not connected check if there is one
if (ConnectedCC == CCHandshake_CC_None)
{
CCHandshake_CC_t detected = detectCCPinSink();
// if none detected, just quit
if (detected == CCHandshake_CC_None)
{
return;
}
// DBG("typeC Switches1 %02x\n", Registers.Switches1 );
if (enableSink( detected ) == false)
{
return;
}
// DBG("typeC Switches1 %02x\n", Registers.Switches1 );
pd_init();
ConnectedCC = detected;
// DBG("typeC Switches1 %02x\n", Registers.Switches1 );
DBG("CC detected %d\n", detected);
}
else // check if it's still connected
{
// if (read( FUSB302_D_Register_Interrupt ) == false)
// {
// return;
// }
if ( (Registers.Interrupt & FUSB302_D_Interrupt_I_BC_LVL) != FUSB302_D_Interrupt_I_BC_LVL )
{
return;
}
uint8_t bc_lvl = 0;
// DBG("typeC Switches1 %02x\n", Registers.Switches1 );
if (readCCVoltageLevel( &bc_lvl ) == false)
{
return;
}
// DBG("typeC Switches1 %02x\n", Registers.Switches1 );
// above threshold?
if (bc_lvl >= CCHANDSHAKE_REQUIRE_BC_LVL)
{
return; // then we're still good
}
if ( disableSink() == false )
{
}
pd_deinit();
ConnectedCC = CCHandshake_CC_None;
DBG("CC lost\n");
}
}
//inline static uint8_t * regPtr( FUSB302_D_Register_t reg )
//{
// switch (reg){
// case FUSB302_D_Register_DeviceID: return &Registers.DeviceID;
// case FUSB302_D_Register_Switches0: return &Registers.Switches0;
// case FUSB302_D_Register_Switches1: return &Registers.Switches1;
// case FUSB302_D_Register_Measure: return &Registers.Measure;
// case FUSB302_D_Register_Slice: return &Registers.Slice;
// case FUSB302_D_Register_Control0: return &Registers.Control0;
// case FUSB302_D_Register_Control1: return &Registers.Control1;
// case FUSB302_D_Register_Control2: return &Registers.Control2;
// case FUSB302_D_Register_Control3: return &Registers.Control3;
// case FUSB302_D_Register_Mask1: return &Registers.Mask1;
// case FUSB302_D_Register_Power: return &Registers.Power;
// case FUSB302_D_Register_Reset: return &Registers.Reset;
// case FUSB302_D_Register_OCPreg: return &Registers.OCPreg;
// case FUSB302_D_Register_Maska: return &Registers.Maska;
// case FUSB302_D_Register_Maskb: return &Registers.Maskb;
// case FUSB302_D_Register_Control4: return &Registers.Control4;
// case FUSB302_D_Register_Status0a: return &Registers.Status0a;
// case FUSB302_D_Register_Status1a: return &Registers.Status1a;
// case FUSB302_D_Register_Interrupta: return &Registers.Interrupta;
// case FUSB302_D_Register_Interruptb: return &Registers.Interruptb;
// case FUSB302_D_Register_Status0: return &Registers.Status0;
// case FUSB302_D_Register_Status1: return &Registers.Status1;
// case FUSB302_D_Register_Interrupt: return &Registers.Interrupt;
// case FUSB302_D_Register_FIFOs: return &Registers.FIFOs;
//
// default: return NULL;
// }
//}
static bool read( FUSB302_D_Register_t reg, uint8_t * value )
{
// uint8_t * r = regPtr( reg );
//
// if (r == NULL)
// {
// return false;
// }
uint8_t r[2] = {0,0};
if (FUSB302_D_Read( &Driver, reg, &r[0] ) == FUSB302_D_ERROR)
{
return false;
}
*value = r[0];
return true;
}
static bool write( FUSB302_D_Register_t reg, uint8_t value )
{
// uint8_t * r = regPtr( reg );
//
// if (r == NULL)
// {
// return false;
// }
if (FUSB302_D_Write( &Driver, reg, value ) == FUSB302_D_ERROR)
{
return false;
}
return true;
}
static bool readAll( void )
{
if (read(FUSB302_D_Register_DeviceID, &Registers.DeviceID) == false) return false;
if (read(FUSB302_D_Register_Switches0, &Registers.Switches0 ) == false) return false;
if (read(FUSB302_D_Register_Switches1, &Registers.Switches1) == false) return false;
if (read(FUSB302_D_Register_Measure, &Registers.Measure ) == false) return false;
if (read(FUSB302_D_Register_Slice, &Registers.Slice) == false) return false;
if (read(FUSB302_D_Register_Control0, &Registers.Control0) == false) return false;
if (read(FUSB302_D_Register_Control1, &Registers.Control1) == false) return false;
if (read(FUSB302_D_Register_Control2, &Registers.Control2 ) == false) return false;
if (read(FUSB302_D_Register_Control3, &Registers.Control3) == false) return false;
if (read(FUSB302_D_Register_Mask1, &Registers.Mask1) == false) return false;
if (read(FUSB302_D_Register_Power, &Registers.Power) == false) return false;
// if (read(FUSB302_D_Register_Reset, &Registers.Reset) == false) return false; // no point in reading this
if (read(FUSB302_D_Register_OCPreg, &Registers.OCPreg) == false) return false;
if (read(FUSB302_D_Register_Maska, &Registers.Maska) == false) return false;
if (read(FUSB302_D_Register_Maskb, &Registers.Maskb) == false) return false;
if (read(FUSB302_D_Register_Control4, &Registers.Control4) == false) return false;
if (read(FUSB302_D_Register_Status0a, &Registers.Status0a) == false) return false;
if (read(FUSB302_D_Register_Status1a, &Registers.Status1a) == false) return false;
if (read(FUSB302_D_Register_Status0, &Registers.Status0) == false) return false;
if (read(FUSB302_D_Register_Status1, &Registers.Status1) == false) return false;
if (read(FUSB302_D_Register_FIFOs, &Registers.FIFOs) == false) return false;
return true;
}
static bool readStatus( void )
{
uint8_t buf[5] = {0,0,0,0,0};
if (FUSB302_D_ReadN( &Driver, FUSB302_D_Register_Interrupta, &buf[0], 5 ) == FUSB302_D_ERROR)
{
DBG("failed read\n");
return false;
}
Registers.Interrupta = buf[0];
Registers.Interruptb = buf[1];
Registers.Status0 = buf[2];
Registers.Status1 = buf[3];
Registers.Interrupt = buf[4];
// if (read(FUSB302_D_Register_Interrupta, &Registers.Interrupta) == false) return false;
// if (read(FUSB302_D_Register_Interruptb, &Registers.Interruptb) == false) return false;
// if (read(FUSB302_D_Register_Status0, &Registers.Status0) == false) return false;
// if (read(FUSB302_D_Register_Status1, &Registers.Status1) == false) return false;
// if (read(FUSB302_D_Register_Interrupt, &Registers.Interrupt) == false) return false;
return true;
}
static bool configure( void )
{
#if CCHANDSHAKE_AUTONOMOUS==true
#else
// enable all power except internal oscillator
Registers.Power = FUSB302_D_Power_PWR_MASK;// & ~FUSB302_D_Power_PWR_InternalOscillator;
if (write( FUSB302_D_Register_Power, Registers.Power ) == false) return false;
// enable high current mode
// if we were using the interrupt pin, also set FUSB302_D_Control0_INT_MASK (don't forget to optionally set TOG_RD_ONLY)
// Registers.Control0 = (Registers.Control0 & ~FUSB302_D_Control0_HOST_CUR_MASK) | FUSB302_D_Control0_HOST_CUR_HighCurrentMode;
// Registers.Control0 &= ~FUSB302_D_Control0_AUTO_PRE; // is 0 by default
// if (write( FUSB302_D_Register_Control0, Registers.Control0 ) == false) return false;
// ON SEMI also sets TOC_USRC_EXIT of "undocumented control 4"
// enable sink polling (NOTE, we're not using it right now, disabled by default)
// read( FUSB302_D_Register_Control2 );
// Registers.Control2 = FUSB302_D_Control2_MODE_SnkPolling | FUSB302_D_Control2_TOGGLE;
// if (write( FUSB302_D_Register_Control2 );
Registers.Switches1 = FUSB302_D_Switches1_POWERROLE_Sink | FUSB302_D_Switches1_DATAROLE_Sink | FUSB302_D_Switches1_SPECREV_Rev2_0 | FUSB302_D_Switches1_AUTO_CRC;
if (write( FUSB302_D_Register_Switches1, Registers.Switches1 ) == false) return false;
// DBG("configure Switches1 %02x\n", Registers.Switches1 );
Registers.Control3 |= FUSB302_D_Control3_AUTO_HARDRESET | FUSB302_D_Control3_AUTO_SOFTRESET | FUSB302_D_Control3_AUTO_RETRY | (0xFF & FUSB302_D_Control3_N_RETRIES_MASK);
if (write( FUSB302_D_Register_Control3, Registers.Control3 ) == false) return false;
// disable all interrupts
Registers.Mask1 = FUSB302_D_Mask1_ALL;
if (write( FUSB302_D_Register_Mask1, Registers.Mask1 ) == false) return false;
Registers.Maska = FUSB302_D_Maska_ALL;
if (write( FUSB302_D_Register_Maska, Registers.Maska ) == false) return false;
Registers.Maskb = FUSB302_D_Maskb_ALL;
if (write( FUSB302_D_Register_Maskb, Registers.Maskb ) == false) return false;
// DBG("configure Switches1 %02x\n", Registers.Switches1 );
#endif
return true;
}
/**
* Tries to get fresh value of measurement (the currently selected cc pin)
*/
static bool readCCVoltageLevel( uint8_t * bc_lvl )
{
if (read(FUSB302_D_Register_Status0, &Registers.Status0) == false)
{
return false;
}
*bc_lvl = Registers.Status0 & FUSB302_D_Status0_BC_LVL_MASK;
return true;
}
/**
* Tries to detect which cc pin has voltage
* WARNING: resets Switches0 register
*/
static CCHandshake_CC_t detectCCPinSink( void )
{
uint8_t BC_LVL;
// read( FUSB302_D_Register_Switches0 );
// enable measurement on cc1
Registers.Switches0 = (Registers.Switches0 & ~FUSB302_D_Switches0_MEAS_CC_MASK) | FUSB302_D_Switches0_MEAS_CC1;// | FUSB302_D_Switches0_PDWN2 | FUSB302_D_Switches0_PDWN1;
write( FUSB302_D_Register_Switches0, Registers.Switches0 );
// wait a bit
DelayMs(250);
// get measurement
if (readCCVoltageLevel( &BC_LVL ) == false)
{
return CCHandshake_CC_None;
}
if ( BC_LVL > FUSB302_D_Status0_BC_LVL_LessThan200mV )
{
return CCHandshake_CC_1;
}
// enable measurement on cc2
Registers.Switches0 = (Registers.Switches0 & ~FUSB302_D_Switches0_MEAS_CC_MASK) | FUSB302_D_Switches0_MEAS_CC2;// | FUSB302_D_Switches0_PDWN2 | FUSB302_D_Switches0_PDWN1;
write( FUSB302_D_Register_Switches0, Registers.Switches0 );
// wait a bit
DelayMs(250);
// get measurement
if (readCCVoltageLevel( &BC_LVL ) == false)
{
return CCHandshake_CC_None;
}
if ( BC_LVL > FUSB302_D_Status0_BC_LVL_LessThan200mV )
{
return CCHandshake_CC_2;
}
return CCHandshake_CC_None;
}
static bool enableSink( CCHandshake_CC_t cc )
{
if (cc == CCHandshake_CC_None)
{
DBG("Trying to enable none!");
return false;
}
// DBG("enableSink Switches1 %02x\n", Registers.Switches1 );
// read( FUSB302_D_Register_Switches1 );
// read( FUSB302_D_Register_Power );
// DBG("switches1 %02x\n", Registers.Switches1 );
// enable meas and txcc
if (cc == CCHandshake_CC_1)
{
Registers.Switches0 = ((Registers.Switches0 & ~FUSB302_D_Switches0_MEAS_CC_MASK) | FUSB302_D_Switches0_MEAS_CC1);
Registers.Switches1 = (Registers.Switches1 & ~FUSB302_D_Switches1_TXCC_MASK) | FUSB302_D_Switches1_TXCC1;
}
else if (cc == CCHandshake_CC_2)
{
Registers.Switches0 = ((Registers.Switches0 & ~FUSB302_D_Switches0_MEAS_CC_MASK) | FUSB302_D_Switches0_MEAS_CC2);
Registers.Switches1 = (Registers.Switches1 & ~(FUSB302_D_Switches1_TXCC_MASK)) | FUSB302_D_Switches1_TXCC2;
}
if (write( FUSB302_D_Register_Switches0, Registers.Switches0 ) == false) return false;
if (write( FUSB302_D_Register_Switches1, Registers.Switches1 ) == false) return false;
// read( FUSB302_D_Register_Switches1, &Registers.Switches1 );
// DBG("enableSink Switches1 %02x\n", Registers.Switches1 );
// enable oscillator for PD
// if ( (Registers.Power & FUSB302_D_Power_PWR_InternalOscillator) != FUSB302_D_Power_PWR_InternalOscillator )
// {
// DBG("activating internal oscillator\n");
// Registers.Power |= FUSB302_D_Power_PWR_InternalOscillator;
// if (write( FUSB302_D_Register_Power, Registers.Power ) == false) return false;
// }
// Registers.Switches
// TXCCx = 1
// MEAS_CCx = 1
// TXCCy = 0
// MEAS_CCy = 0
// Registers.Switches1 // by default we don't have to set this
// POWERROLE = 0
// DATAROLE = 0
// Registers.Control
// ENSOP1 = 0
// ENSOP1DP = 0
// ENSOP2 = 0
// ENSOP2DP = 0
// Registers.Power enable internal oscillator
return true;
}
static bool disableSink()
{
// DBG("disableSink()\n");
// read( FUSB302_D_Register_Switches1 );
// read( FUSB302_D_Register_Power );
// DBG("disableSink Switches1 %02x\n", Registers.Switches1 );
// disable TXCCx
Registers.Switches1 &= ~FUSB302_D_Switches1_TXCC_MASK;
write( FUSB302_D_Register_Switches1, Registers.Switches1 );
// disable CC
Registers.Switches0 &= ~FUSB302_D_Switches0_MEAS_CC_MASK;
write( FUSB302_D_Register_Switches0, Registers.Switches0 );
// DBG("disableSink Switches1 %02x\n", Registers.Switches1 );
// disable oscillator for PD
// Registers.Power &= ~FUSB302_D_Power_PWR_InternalOscillator;
return true;
}
static void pd_core( void )
{
if (PD.State == PD_State_Disabled)
{
// DBG("PD State Disabled\n");
return;
}
// static bool first = true;
// if (first){
// DBG("FIRST\n");
// first = false;
// }
if ( (Registers.Status1 & FUSB302_D_Status1_RX_EMPTY ) == FUSB302_D_Status1_RX_EMPTY){
// DBG("RX_EMPTY\n");
// PD.Rx.HasData |= PD.Rx.HasData;
// PD.State = PD_State_Rx;
}
if ( (Registers.Status1 & FUSB302_D_Status1_RX_FULL ) == FUSB302_D_Status1_RX_FULL){
DBG("RX_FULL\n");
}
if ( (Registers.Status1 & FUSB302_D_Status1_TX_EMPTY ) == FUSB302_D_Status1_TX_EMPTY){
// DBG("TX_EMPTY\n");
}
if ( (Registers.Status1 & FUSB302_D_Status1_TX_FULL ) == FUSB302_D_Status1_TX_FULL){
DBG("TX_FULL\n");
}
if ( (Registers.Interrupt & FUSB302_D_Interrupt_I_COLLISION) == FUSB302_D_Interrupt_I_COLLISION )
{
DBG("I_COLLISION\n");
}
if ( (Registers.Interrupt & FUSB302_D_Interrupt_I_ACTIVITY) == FUSB302_D_Interrupt_I_ACTIVITY )
{
// DBG("I_ACTIVITY\n");
}
if ( (Registers.Interrupt & FUSB302_D_Interrupt_I_ALERT) == FUSB302_D_Interrupt_I_ALERT )
{
DBG("I_ALERT\n");
}
if ( (Registers.Interrupta & FUSB302_D_Interrupta_I_SOFTFAIL ) == FUSB302_D_Interrupta_I_SOFTFAIL){
DBG("I_SOFTFAIL\n");
}
if ( (Registers.Interrupta & FUSB302_D_Interrupta_I_RETRYFAIL ) == FUSB302_D_Interrupta_I_RETRYFAIL){
DBG("I_RETRYFAIL\n");
}
if ( (Registers.Interrupta & FUSB302_D_Interrupta_I_HARDSENT ) == FUSB302_D_Interrupta_I_HARDSENT){
DBG("I_HARDSENT\n");
}
if ( (Registers.Interrupta & FUSB302_D_Interrupta_I_TXSENT ) == FUSB302_D_Interrupta_I_TXSENT){
DBG("I_TXSENT\n");
}
if ( (Registers.Interrupta & FUSB302_D_Interrupta_I_SOFTRST ) == FUSB302_D_Interrupta_I_SOFTRST){
DBG("I_SOFTRST\n");
}
if ( (Registers.Interrupta & FUSB302_D_Interrupta_I_HARDRST ) == FUSB302_D_Interrupta_I_HARDRST){
DBG("I_HARDRST\n");
// PD.State = PD_State_Reset;
}
if ( (Registers.Interruptb & FUSB302_D_Interruptb_I_GCRCSENT ) == FUSB302_D_Interruptb_I_GCRCSENT){
DBG("I_GCRCSENT\n");
PD.State = PD_State_Rx;
}
PD_DataObject_t request = { .Value = 0 };
switch( PD.State )
{
// this is only here to suppress the compiler warning
case PD_State_Disabled:
{
// do nothing
break;
}
case PD_State_HardReset:
{
DBG("PD State HardReset\n");
pd_hardreset();
DelayMs(1);
PD.State = PD_State_Reset;
break;
}
case PD_State_Reset:
{
DBG("PD State Reset\n");
pd_reset();
pd_flushRxFifo();
pd_flushTxFifo();
// try to get source capabilities
pd_clearTx();
PD_newMessage( &PD.Tx.Message, 0, pd_nextTxMessageId(), PD_HeaderWord_PowerRole_Sink, PD_HeaderWord_SpecRev_2_0, PD_HeaderWord_DataRole_Sink, PD_ControlCommand_GetSourceCap, NULL );
pd_sendMessage( &PD.Tx.Message, NULL );
PD.Tx.SendAttempts = 0;
break;
}
case PD_State_Idle:
{
// DBG("PD State Idle\n");
// if neither empty nor full there is data pending
if ((Registers.Status1 & FUSB302_D_Status1_TX_EMPTY ) != FUSB302_D_Status1_TX_EMPTY &&
(Registers.Status1 & FUSB302_D_Status1_TX_FULL ) != FUSB302_D_Status1_TX_FULL )
// (Registers.Interrupt & FUSB302_D_Interrupt_I_ACTIVITY) != FUSB302_D_Interrupt_I_ACTIVITY)
{
// DBG("Resending\n");
// pd_startTx();
// pd_flushTxFifo();
// pd_sendMessage( &PD.Tx.Message, NULL );
}
break;
}
case PD_State_Rx:
{
// DBG("PD State Rx\n");
// if (pd_hasMessage() == false)
// {
// PD.State = PD_State_Idle;
// return;
// }
// DBG("PD has message\n");
// memset( &PD.Rx.Message, 0, sizeof(PD_Message_t) );
if (pd_getMessage( &PD.Rx.Message ) == false)
{
PD.State = PD_State_Idle;
return;
}
// DBG("PD process\n");
PD.State = pd_processMessage( &PD.Rx.Message );
break;
}
// case PD_State_AwaitGoodCRC:
// {
//// DBG("PD State AwaitGoodCRC\n");
//
// if (TimerGetElapsedTime(PD.Tx.SentTs) > 1000){
// if (PD.Tx.SendAttempts > 3)
// {
// PD.State = PD_State_HardReset;
// }
// else
// {
// PD.State = PD_State_Resend;
// }
// break;
// }
//
// if ( (Registers.Interruptb & FUSB302_D_Interruptb_I_GCRCSENT ) == FUSB302_D_Interruptb_I_GCRCSENT){
// PD.State = PD_State_Idle;
// break;
// }
////
//// if (pd_hasMessage() == false)
//// {
//// return;
//// }
////
//// memset( &PD.Rx.Message, 0, sizeof(PD_Message_t) );
////
//// if (pd_getMessage( &PD.Rx.Message ) == false)
//// {
//// return;
//// }
//
// break;
// }
//
// case PD_State_Resend:
// {
// DBG("PD State Resend\n");
//
// pd_sendMessage( &PD.Tx.Message, PD.Tx.OnAcknowledged );
// PD.Tx.SentTs = TimerGetCurrentTime();
// PD.Tx.SendAttempts++;
//
// PD.State = PD_State_AwaitGoodCRC;
//
// break;
// }
default:
PD.State = PD_State_Reset;
}
}
static void pd_init( void )
{
// DBG("pd_init Switches1 %02x\n", Registers.Switches1 );
PD.Rx.HasData = false;
PD.Tx.MessageId = 2;
PD.State = PD_State_Idle;
PD.Power.NSourceCapabilities = 0;
memset( &PD.Power.SourceCapabilities[0], 0, PD_MESSAGE_MAX_OBJECTS * sizeof(PD_DataObject_t) );
PD.Power.BestCapIndex = 0;
// enable auto goodCRC
// DBG("Switches1 %d\n", Registers.Switches1 );
// read( FUSB302_D_Register_Switches1 );
// DBG("Switches1 %d\n", Registers.Switches1 );
// Registers.Switches1 = FUSB302_D_Switches1_SPECREV_Rev2_0 | FUSB302_D_Switches1_AUTO_CRC;
// Registers.Switches1 = (Registers.Switches1 & ~FUSB302_D_Switches1_SPECREV_MASK) | FUSB302_D_Switches1_SPECREV_Rev2_0;
// Registers.Switches1 |= FUSB302_D_Switches1_AUTO_CRC;
// enable meas and txcc
// if (ConnectedCC == CCHandshake_CC_1)
// {
//// Registers.Switches0 = (Registers.Switches0 & ~)
// Registers.Switches1 |= FUSB302_D_Switches1_TXCC1;
// }
// else if (ConnectedCC == CCHandshake_CC_2)
// {
// Registers.Switches1 |= FUSB302_D_Switches1_TXCC2;
//
// }
// DBG("Switches1 %d\n", Registers.Switches1 );
// write( FUSB302_D_Register_Switches1 );
//
// DBG("Switches1 %d\n", Registers.Switches1 );
//
// read( FUSB302_D_Register_Switches1 );
// DBG("Switches1 %d\n", Registers.Switches1 );
// read( FUSB302_D_Register_Control0, Registers.Control0 );
// Registers.Control0 &= ~FUSB302_D_Control0_AUTO_PRE;
// write( FUSB302_D_Register_Control0, Registers.Control0 );
// enable interrupts
// read( FUSB302_D_Register_Maska );
// Registers.Maska |= FUSB302_D_Maska_M_TXSENT;
// write( FUSB302_D_Register_Maska, Registers.Maska );
// read( FUSB302_D_Register_Maskb );
// Registers.Maskb |= FUSB302_D_Maskb_M_GCRCSENT;
// write( FUSB302_D_Register_Maskb, Registers.Maskb );
// pd_reset();
// pd_flushRxFifo();
// pd_flushTxFifo();
// DBG("pd_init Switches1 %02x\n", Registers.Switches1 );
}
static void pd_deinit( void )
{
// DBG("pd_deinit Switches1 %02x\n", Registers.Switches1 );