-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
1382 lines (1254 loc) · 67.5 KB
/
main.py
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
from machine import Pin, I2C, ADC
from time import sleep, ticks_us, ticks_diff
import sys
is_source = False
if is_source:
# non-documented hardware stuff.
# This is starting to get to the point that things need to be pulled out into a separate library.
# glad I've gotten to that point ^~^
p_5_a = Pin(2, Pin.OUT, value=0)
p_5_m = Pin(7, Pin.OUT, value=0)
p_vin = Pin(3, Pin.OUT, value=0)
p_discharge = Pin(6, Pin.OUT, value=0)
p_5_m.off()
p_5_a.off()
p_vin.off()
p_discharge.on()
p_led_1 = Pin(9, Pin.OUT, value=0)
p_led_2 = Pin(15, Pin.OUT, value=0)
i2c = I2C(sda=Pin(18), scl=Pin(19), id=1, freq=400000)
print(i2c.scan())
a = ADC(Pin(28))
print(a.read_u16())
int_p = Pin(20, Pin.IN, Pin.PULL_UP)
int_g = int_p.value
def get_adc_vbus():
return (3.3*11*a.read_u16())/65536
print(get_adc_vbus(), "V")
if is_source:
# sanity check
# currently, both 5V and VIN pins are supposed to be shut off
vbus_v = get_adc_vbus()
if vbus_v > 1:
# enable discharge FET and wait for VBUS to discharge
print("VBUS is at {}, has to be discharged".format(vbus_v))
p_discharge.on()
sleep(0.3)
vbus_v = get_adc_vbus()
if vbus_v > 1:
# blink and enter error state
# maybe a FET is borked, maybe something else
## remove all pullups and pulldowns? TODO
print("VBUS is still at {}, can't be discharged".format(vbus_v))
while True:
p_led_1.toggle()
sleep(0.3)
# infinite loop; TODO: add checks in case of longer discharge
else:
print("VBUS is at {}".format(vbus_v))
def set_power_rail(rail):
rail = rail.lower()
p_led_1.off(); p_led_2.off()
if rail == "off":
p_5_m.off()
p_5_a.off()
p_vin.off()
p_discharge.on()
elif rail == "5v":
p_vin.off()
p_discharge.on()
p_5_a.on()
p_5_m.on()
p_discharge.off()
elif rail == "vin":
p_5_m.off()
p_discharge.off()
p_5_a.on()
p_vin.on()
p_5_a.off()
p_led_1.on(); p_led_2.off()
else:
# catch-all:
p_5_m.off()
p_5_a.off()
p_vin.off()
p_discharge.on()
raise Exception("rail has to be one of 'off', '5v' or 'vin', was '{}'".format(rail))
########################
#
# FUSB-specific code
#
########################
FUSB302_I2C_SLAVE_ADDR = 0x22
TCPC_REG_DEVICE_ID = 0x01
TCPC_REG_SWITCHES0 = 0x02
TCPC_REG_SWITCHES1 = 0x03
TCPC_REG_MEASURE = 0x04
TCPC_REG_CONTROL0 = 0x06
TCPC_REG_CONTROL1 = 0x07
TCPC_REG_CONTROL2 = 0x08
TCPC_REG_CONTROL3 = 0x09
TCPC_REG_MASK = 0x0A
TCPC_REG_POWER = 0x0B
TCPC_REG_RESET = 0x0C
TCPC_REG_MASKA = 0x0E
TCPC_REG_MASKB = 0x0F
TCPC_REG_STATUS0A = 0x3C
TCPC_REG_STATUS1A = 0x3D
TCPC_REG_INTERRUPTA = 0x3E
TCPC_REG_INTERRUPTB = 0x3F
TCPC_REG_STATUS0 = 0x40
TCPC_REG_STATUS1 = 0x41
TCPC_REG_INTERRUPT = 0x42
TCPC_REG_FIFOS = 0x43
def reset():
# reset the entire FUSB
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_RESET, bytes([0b1]))
def reset_pd():
# resets the FUSB PD logic
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_RESET, bytes([0b10]))
def unmask_all():
# unmasks all interrupts
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_MASK, bytes([0b0]))
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_MASKA, bytes([0b0]))
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_MASKB, bytes([0b0]))
def cc_current():
# show measured CC level interpreted as USB-C current levels
return i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_STATUS0, 1)[0] & 0b11
def read_cc(cc):
# enable a CC pin for reading
assert(cc in [0, 1, 2])
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES0, 1)[0]
x1 = x
clear_mask = ~0b1100 & 0xFF
x &= clear_mask
mask = [0b0, 0b100, 0b1000][cc]
x |= mask
#print('TCPC_REG_SWITCHES0: ', bin(x1), bin(x), cc)
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES0, bytes((x,)) )
def enable_pullups():
# enable host pullups on CC pins, disable pulldowns
x = i2c.readfrom_mem(0x22, 0x02, 1)[0]
x |= 0b11000000
i2c.writeto_mem(0x22, 0x02, bytes((x,)) )
def set_mdac(value):
x = i2c.readfrom_mem(0x22, 0x04, 1)[0]
x &= 0b11000000
x |= value
i2c.writeto_mem(0x22, 0x04, bytes((x,)) )
def enable_sop():
# enable reception of SOP'/SOP" messages
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL1, 1)[0]
mask = 0b1100011
x |= mask
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL1, bytes((x,)) )
def disable_pulldowns():
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES0, 1)[0]
clear_mask = ~0b11 & 0xFF
x &= clear_mask
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES0, bytes((x,)) )
def enable_pulldowns():
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES0, 1)[0]
x |= 0b11
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES0, bytes((x,)) )
def measure_sink(debug=False):
# read CC pins and see which one senses the pullup
read_cc(1)
sleep(0.001)
cc1_c = cc_current()
read_cc(2)
sleep(0.001)
cc2_c = cc_current()
# picking the CC pin depending on which pin can detect a pullup
cc = [1, 2][cc1_c < cc2_c]
if debug: print('m', bin(cc1_c), bin(cc2_c), cc)
if cc1_c == cc2_c:
return 0
return cc
def measure_source(debug=False):
# read CC pins and see which one senses the correct host current
read_cc(1)
sleep(0.001)
cc1_c = cc_current()
read_cc(2)
sleep(0.001)
cc2_c = cc_current()
if cc1_c == host_current:
cc = 1
elif cc2_c == host_current:
cc = 2
else:
cc = 0
if debug: print('m', bin(cc1_c), bin(cc2_c), cc)
return cc
def set_controls_sink():
# boot: 0b00100100
ctrl0 = 0b00000000 # unmask all interrupts; don't autostart TX.. disable pullup current
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL0, bytes((ctrl0,)) )
# boot: 0b00000110
ctrl3 = 0b00000111 # enable automatic packet retries
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL3, bytes((ctrl3,)) )
host_current=0b10
def set_controls_source():
# boot: 0b00100100
ctrl0 = 0b00000000 # unmask all interrupts; don't autostart TX
ctrl0 |= host_current << 2 # set host current advertisement pullups
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL0, bytes((ctrl0,)) )
i2c.writeto_mem(0x22, 0x06, bytes((ctrl0,)) )
# boot: 0b00000110
ctrl3 = 0b00000110 # no automatic packet retries
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL3, bytes((ctrl3,)) )
# boot: 0b00000010
#ctrl2 = 0b00000000 # disable DRP toggle. setting it to Do Not Use o_o ???
#i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL2, bytes((ctrl2,)) )
def set_wake(state):
# boot: 0b00000010
ctrl2 = i2c.readfrom_mem(0x22, 0x08, 1)[0]
clear_mask = ~(1 << 3) & 0xFF
ctrl2 &= clear_mask
if state:
ctrl2 | (1 << 3)
i2c.writeto_mem(0x22, 0x08, bytes((ctrl2,)) )
def flush_receive():
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL1, 1)[0]
mask = 0b100 # flush receive
x |= mask
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL1, bytes((x,)) )
def flush_transmit():
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL0, 1)[0]
mask = 0b01000000 # flush transmit
x |= mask
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL0, bytes((x,)) )
def enable_tx(cc):
# enables switch on either CC1 or CC2
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES1, 1)[0]
x1 = x
mask = 0b10 if cc == 2 else 0b1
x &= 0b10011100 # clearing both TX bits and revision bits
x |= mask
x |= 0b100
x |= 0b10 << 5 # revision 3.0
#print('et', bin(x1), bin(x), cc)
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_SWITCHES1, bytes((x,)) )
def set_roles(power_role = 0, data_role = 0):
x = i2c.readfrom_mem(0x22, 0x03, 1)[0]
x &= 0b01101111 # clearing both role bits
x |= power_role << 7
x |= data_role << 7
i2c.writeto_mem(0x22, 0x03, bytes((x,)) )
def power():
# enables all power circuits
x = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_POWER, 1)[0]
mask = 0b1111
x |= mask
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_POWER, bytes((x,)) )
def polarity():
# reads polarity and role bits from STATUS1A
return (i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_STATUS1A, 1)[0] >> 3) & 0b111
#'0b110001'
def interrupts():
# return all interrupt registers
return i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_INTERRUPTA, 2)+i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_INTERRUPT, 1)
# interrupts are cleared just by reading them, it seems
#def clear_interrupts():
# # clear interrupt
# i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_INTERRUPTA, bytes([0]))
# i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_INTERRUPT, bytes([0]))
# this is a way better way to do things than the following function -
# the read loop should be ported to this function, and the next ome deleted
def rxb_state():
# get read buffer interrupt states - (rx buffer empty, rx buffer full)
st = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_STATUS1, 1)[0]
return ((st & 0b100000) >> 5, (st & 0b10000) >> 4)
# TODO: yeet
def rxb_state():
st = i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_STATUS1, 1)[0]
return ((st & 0b110000) >> 4, (st & 0b11000000) >> 6)
def get_rxb(l=80):
# read from FIFO
return i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_FIFOS, l)
def hard_reset():
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL3, bytes([0b1000000]))
return i2c.readfrom_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_CONTROL3, 1)
def find_cc(fn=measure_sink, debug=False):
cc = fn(debug=debug)
flush_receive()
enable_tx(cc)
read_cc(cc)
flush_transmit()
flush_receive()
#import gc; gc.collect()
reset_pd()
return cc
# FUSB toggle logic shorthands
# currently unused
polarity_values = (
(0, 0), # 000: logic still running
(1, 0), # 001: cc1, src
(2, 0), # 010: cc2, src
(-1, -1), # 011: unknown
(-1, -1), # 100: unknown
(1, 1), # 101: cc1, snk
(2, 1), # 110: cc2, snk
(0, 2), # 111: audio accessory
)
current_values = (
"Ra/low",
"Rd-Default",
"Rd-1.5",
"Rd-3.0"
)
def p_pol():
return polarity_values[polarity()]
def p_int(a=None):
if a is None:
a = interrupts()
return [bin(x) for x in a]
def p_cur():
return current_values[cc_current()]
########################
#
# USB-C stacc code
#
########################
pdo_requested = False
pdos = []
timing_start = 0
timing_end = 0
# set to -1 because it's incremented before each command is sent out
msg_id = -1
def increment_msg_id():
global msg_id
msg_id += 1
if msg_id == 8: msg_id = 0
return msg_id
def reset_msg_id():
global msg_id
msg_id = -1
sent_messages = []
def source_flow():
global psu_advertisement, advertisement_counter, sent_messages
psu_advertisement = create_pdo('fixed', 5000, 1500, 0, 8) + \
create_pdo('fixed', 19000, 5000, 0, 0)
counter = 0
reset_msg_id()
sleep(0.3)
print("sending advertisement")
send_advertisement(psu_advertisement)
advertisement_counter = 1
profile_selected = False
try:
timeout = 0.00001
while True:
if rxb_state()[0] == 0: # buffer non-empty
d = get_message()
msg_types = control_message_types if d["c"] else data_message_types
msg_name = msg_types[d["t"]]
# now we do things depending on the message type that we received
if msg_name == "GoodCRC": # example
print("GoodCRC")
elif msg_name == "Request":
profile_selected = True
process_psu_request(psu_advertisement, d)
show_msg(d)
for message in sent_messages:
sys.stdout.write('> ')
sys.stdout.write(myhex(message))
sys.stdout.write('\n')
sent_messages = []
sleep(timeout) # so that ctrlc works
counter += 1
if counter == 10000:
counter = 0
if not profile_selected and advertisement_counter < 30:
print("sending advertisement")
send_advertisement(psu_advertisement)
advertisement_counter += 1
if int_g() == 0:
i = interrupts()
print(i)
i_reg = i[2]
if i_reg & 0x80: # I_VBUSOK
print("I_VBUSOK")
#pass # just a side effect of vbus being attached
if i_reg & 0x40: # I_ACTIVITY
print("I_ACTIVITY")
pass # just a side effect of CC comms I think?
if i_reg & 0x20: # I_COMP_CHNG
print("I_COMP_CHNG")
# this is where detach can occur, let's check
cc = find_cc(fn=measure_source)
if cc == 0:
print("Disconnect detected!")
return # we exiting this
if i_reg & 0x10: # I_CRC_CHK
pass # new CRC, just a side effect of CC comms
if i_reg & 0x8: # I_ALERT
print("I_ALERT")
x = i2c.readfrom_mem(0x22, 0x41, 1)[0]
print(bin(x))
if i_reg & 0x4: # I_WAKE
print("I_WAKE")
if i_reg & 0x2: # I_COLLISION
print("I_COLLISION")
if i_reg & 0x1: # I_BC_LVL
print("I_BC_LVL")
except KeyboardInterrupt:
print("CtrlC")
sleep(1)
raise
def sink_flow():
global pdo_requested, pdos, sent_messages
reset_msg_id()
try:
timeout = 0.00001
while True:
if rxb_state()[0] == 0: # buffer non-empty
d = get_message()
msg_types = control_message_types if d["c"] else data_message_types
msg_name = msg_types[d["t"]]
# now we do things depending on the message type that we received
if msg_name == "GoodCRC": # example
pass # print("GoodCRC")
elif msg_name == "Source_Capabilities":
# need to request a PDO!
pdos = get_pdos(d)
pdo_i, current = select_pdo(pdos)
# sending a message, need to increment message id
request_fixed_pdo(pdo_i, current, current)
# print("PDO requested!")
pdo_requested = True
sys.stdout.write(str(pdos))
sys.stdout.write('\n')
elif msg_name in ["Accept", "PS_RDY"]:
print(get_adc_vbus(), "V")
elif msg_name == "Vendor_Defined":
parse_vdm(d)
react_vdm(d)
show_msg(d)
for message in sent_messages:
sys.stdout.write('> ')
sys.stdout.write(myhex(message))
sys.stdout.write('\n')
sent_messages = []
sleep(timeout) # so that ctrlc works
if int_g() == 0:
# needs sink detach processing here lmao
i = interrupts()
print(i)
i_reg = i[2]
if i_reg & 0x80: # I_VBUSOK
pass # just a side effect of vbus being attached
if i_reg & 0x40: # I_ACTIVITY
print("I_ACTIVITY")
pass # just a side effect of CC comms I think?
if i_reg & 0x20: # I_COMP_CHNG
print("I_COMP_CHNG")
cc = find_cc(fn=measure_sink)
if cc == 0:
print("Disconnect detected!")
return # we exiting this
if i_reg & 0x10: # I_CRC_CHK
pass # new CRC, just a side effect of CC comms
if i_reg & 0x8: # I_ALERT
print("I_ALERT")
if i_reg & 0x4: # I_WAKE
print("I_WAKE")
if i_reg & 0x2: # I_COLLISION
print("I_COLLISION")
if i_reg & 0x1: # I_BC_LVL
print("I_BC_LVL")
except KeyboardInterrupt:
print("CtrlC")
sleep(1)
raise
########################
#
# Packet reception
# and parsing code
#
########################
control_message_types = [
"Reserved",
"GoodCRC",
"GotoMin",
"Accept",
"Reject",
"Ping",
"PS_RDY",
"Get_Source_Cap",
"Get_Sink_Cap",
"DR_Swap",
"PR_Swap",
"VCONN_Swap",
"Wait",
"Soft_Reset",
"Data_Reset",
"Data_Reset_Complete",
"Not_Supported",
"Get_Source_Cap_Extended",
"Get_Status",
"FR_Swap",
"Get_PPS_Status",
"Get_Country_Codes",
"Get_Sink_Cap_Extended",
"Get_Source_Info",
"Get_Revision",
]
data_message_types = [
"Reserved",
"Source_Capabilities",
"Request",
"BIST",
"Sink_Capabilities",
"Battery_Status",
"Alert",
"Get_Country_Info",
"Enter_USB",
"EPR_Request",
"EPR_Mode",
"Source_Info",
"Revision",
"Reserved",
"Reserved",
"Vendor_Defined",
]
header_starts = [0xe0, 0xc0]
def get_message(get_rxb=get_rxb):
header = 0
d = {}
# we might have to get through some message data!
while header not in header_starts:
header = get_rxb(1)[0]
if header == 0:
return
if header not in header_starts:
# this will be printed, eventually.
# the aim is that it doesn't delay code in the way that print() seems to
sys.stdout.write("disc {}\n".format(hex(header)))
d["o"] = False # incoming message
d["h"] = header
b1, b0 = get_rxb(2)
d["b0"] = b0
d["b1"] = b1
sop = 1 if header == 0xe0 else 0
d["st"] = sop
# parsing the packet header
prole = b0 & 1
d["pr"] = prole
drole = b1 >> 5 & 1
d["dr"] = drole
msg_type = b1 & 0b11111
pdo_count = (b0 >> 4) & 0b111
d["dc"] = pdo_count
d["t"] = msg_type
d["c"] = pdo_count == 0 # control if True else data
msg_index = int((b0 >> 1) & 0b111)
d["i"] = msg_index
if pdo_count:
read_len = pdo_count*4
pdos = get_rxb(read_len)
d["d"] = pdos
_ = get_rxb(4) # crc
rev = b1 >> 6
d["r"] = rev
is_ext = b0 >> 7 # extended
d["e"] = is_ext
msg_types = control_message_types if pdo_count == 0 else data_message_types
msg_name = msg_types[d["t"]]
d["tn"] = msg_name
if msg_name == "Vendor_Defined":
parse_vdm(d)
return d
def show_msg(d):
## d["h"] = header
## sop = 1 if header == 0xe0 else 0
## d["st"] = sop
sop_str = "" if d["st"] else "'"
# parsing the packet header
## d["pr"] = prole
prole_str = "NC"[d["pr"]] if d["st"] else "R"
drole_str = "UD"[d["dr"]] if d["st"] else "R"
## d["dc"] = pdo_count
## d["t"] = msg_type
## d["c"] = pdo_count == 0 # control if True else data
message_types = control_message_types if d["c"] else data_message_types
## d["i"] = msg_index
msg_type_str = message_types[d["t"]] if d["t"] < len(message_types) else "Reserved"
## if pdo_count:
## d["d"] = pdos
## d["r"] = rev
rev_str = "123"[d["r"]]
## d["e"] = is_ext
ext_str = ["std", "ext"][d["e"]]
# msg direction
dir_str = ">" if d["o"] else "<"
if d["dc"]:
# converting "41 80 00 FF A4 25 00 2C" to "FF008041 2C0025A4"
pdo_strs = []
pdo_data = myhex(d["d"]).split(' ')
for i in range(len(pdo_data)//4):
pdo_strs.append(''.join(reversed(pdo_data[(i*4):][:4])))
pdo_str = " ".join(pdo_strs)
else:
pdo_str = ""
sys.stdout.write("{} {}{}: {}; p{} d{} r{}, {}, p{}, {} {}\n".format(dir_str, d["i"], sop_str, msg_type_str, prole_str, drole_str, rev_str, ext_str, d["dc"], myhex((d["b0"], d["b1"])).replace(' ', ''), pdo_str))
# extra parsing where possible
if msg_type_str == "Vendor_Defined":
print_vdm(d)
#sys.stdout.write(str(d["d"]))
#sys.stdout.write('\n')
elif msg_type_str == "Source_Capabilities":
sys.stdout.write(str(get_pdos(d)))
sys.stdout.write('\n')
return d
########################
#
# PDO parsing code
#
########################
pdo_types = ['fixed', 'batt', 'var', 'pps']
pps_types = ['spr', 'epr', 'res', 'res']
def parse_pdo(pdo):
pdo_t = pdo_types[pdo[3] >> 6]
if pdo_t == 'fixed':
current_h = pdo[1] & 0b11
current_b = ( current_h << 8 ) | pdo[0]
current = current_b * 10
voltage_h = pdo[2] & 0b1111
voltage_b = ( voltage_h << 6 ) | (pdo[1] >> 2)
voltage = voltage_b * 50
peak_current = (pdo[2] >> 4) & 0b11
return (pdo_t, voltage, current, peak_current, pdo[3])
elif pdo_t == 'batt':
# TODO
return ('batt', pdo)
elif pdo_t == 'var':
current_h = pdo[1] & 0b11
current = ( current_h << 8 ) | pdo[0]*10
# TODO
return ('var', current, pdo)
elif pdo_t == 'pps':
t = (pdo[3] >> 4) & 0b11
limited = (pdo[3] >> 5) & 0b1
max_voltage_h = pdo[3] & 0b1
max_voltage_b = (max_voltage_h << 7) | pdo[2] >> 1
max_voltage = max_voltage_b * 100
min_voltage = pdo[1] * 100
max_current_b = pdo[0] & 0b1111111
max_current = max_current_b * 50
return ('pps', pps_types[t], max_voltage, min_voltage, max_current, limited)
def create_pdo(pdo_t, *args):
print(pdo_t, *args)
assert(pdo_t in pdo_types)
pdo = [0 for i in range(4)]
if pdo_t == 'fixed':
voltage, current, peak_current, pdo3 = args
current_v = current // 10
current_h = (current_v >> 8) & 0b11
current_l = current_v & 0xFF
pdo[1] = current_h
pdo[0] = current_l
"""
current_h = pdo[1] & 0b11
current_b = ( current_h << 8 ) | pdo[0]
current = current_b * 10
"""
voltage_v = voltage // 50
pdo[2] = (voltage_v >> 6) & 0b1111
pdo[1] |= (voltage_v & 0b111111) << 2
"""
voltage_h = pdo[2] & 0b1111
voltage_b = ( voltage_h << 6 ) | (pdo[1] >> 2)
voltage = voltage_b * 50
"""
pdo[2] |= (peak_current & 0b11) << 4
peak_current = (pdo[2] >> 4) & 0b11
pdo[3] = pdo3
pdo[3] |= pdo_types.index(pdo_t) << 6
elif pdo_t == 'batt':
raise Exception("Batt PDO formation not implemented yet!")
elif pdo_t == 'var':
raise Exception("Variable PDO formation not implemented yet!")
elif pdo_t == 'pps':
"""t = (pdo[3] >> 4) & 0b11
limited = (pdo[3] >> 5) & 0b1
max_voltage_h = pdo[3] & 0b1
max_voltage_b = (max_voltage_h << 7) | pdo[2] >> 1
max_voltage = max_voltage_b * 100
min_voltage = pdo[1] * 100
max_current_b = pdo[0] & 0b1111111
max_current = max_current_b * 50
return ('pps', pps_types[t], max_voltage, min_voltage, max_current, limited)"""
raise Exception("PPS PDO formation not implemented yet!")
print(parse_pdo(bytes(pdo)))
return pdo
def get_pdos(d):
pdo_list = []
pdos = d["d"]
for pdo_i in range(d["dc"]):
pdo_bytes = pdos[(pdo_i*4):][:4]
#print(myhex(pdo_bytes))
parsed_pdo = parse_pdo(pdo_bytes)
pdo_list.append(parsed_pdo)
return pdo_list
########################
#
# Command sending code
# and simple commands
#
########################
def send_command(command, data, msg_id=None, rev=0b10, power_role=0, data_role=0):
msg_id = increment_msg_id() if msg_id is None else msg_id
sop_seq = [0x12, 0x12, 0x12, 0x13, 0x80]
eop_seq = [0xff, 0x14, 0xfe, 0xa1]
obj_count = len(data) // 4
header = [0, 0] # hoot hoot !
header[0] |= rev << 6 # PD revision
header[0] |= (data_role & 0b1) << 5 # PD revision
header[0] |= (command & 0b11111)
header[1] = power_role & 0b1
header[1] |= (msg_id & 0b111) << 1 # message ID
header[1] |= obj_count << 4
message = header+data
sop_seq[4] |= len(message)
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_FIFOS, bytes(sop_seq) )
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_FIFOS, bytes(message) )
i2c.writeto_mem(FUSB302_I2C_SLAVE_ADDR, TCPC_REG_FIFOS, bytes(eop_seq) )
sent_messages.append(message)
def soft_reset():
send_command(0b01101, [])
reset_msg_id()
########################
#
# PSU request processing code
#
########################
def send_advertisement(psu_advertisement):
#data = [bytes(a) for a in psu_advertisement]
data = psu_advertisement
send_command(0b1, data, power_role=1, data_role=1)
def process_psu_request(psu_advertisement, d):
print(d)
profile = ((d["d"][3] >> 4)&0b111)-1
print("Selected profile", profile)
if profile not in range(len(psu_advertisement)):
set_power_rail('off')
else:
send_command(0b11, [], power_role=1, data_role=1) # Accept
sleep(0.1)
if profile == 0:
set_power_rail('5V')
elif profile == 1:
set_power_rail('VIN')
send_command(0b110, [], power_role=1, data_role=1) # PS_RDY
########################
#
# PDO request code
#
########################
def request_fixed_pdo(num, current, max_current):
pdo = [0 for i in range(4)]
max_current_b = max_current // 10
max_current_l = max_current_b & 0xff
max_current_h = max_current_b >> 8
pdo[0] = max_current_l
pdo[1] |= max_current_h
current_b = current // 10
current_l = current_b & 0x3f
current_h = current_b >> 6
pdo[1] |= current_l << 2
pdo[2] |= current_h
pdo[3] |= (num+1) << 4 # object position
pdo[3] |= 0b1 # no suspend
send_command(0b00010, pdo)
def request_pps_pdo(num, voltage, current):
pdo = [0 for i in range(4)]
current = current // 50
pdo[0] = current & 0x7f
voltage = voltage // 20
voltage_l = (voltage & 0x7f)
voltage_h = (voltage >> 7) & 0x1f
pdo[1] |= voltage_l << 1
pdo[2] = voltage_h
pdo[3] |= (num+1) << 4 # object position
pdo[3] |= 0b1 # no suspend
send_command(0b00010, pdo)
########################
#
# VDM parsing and response code
#
########################
vdm_commands = [
"Reserved",
"Discover Identity",
"Discover SVIDs",
"Discover Modes",
"Enter Mode",
"Exit Mode",
"Attention"]
svids = {
0xff00: 'SID',
0xff01: 'DisplayPort',
}
dp_commands = {
0x10: "DP Status Update",
0x11: "DP Configure"}
vdm_cmd_types = ["REQ", "ACK", "NAK", "BUSY"]
# reply-with-hardcoded code
def react_vdm(d):
if d["vdm_s"]:
cmd_type = d["vdm_ct"]
command_name = d["vdm_cn"]
# response vdm params
rd = {}
# all same params as the incoming message, save for the command type
for key in ["vdm_s", "vdm_sv", "vdm_c", "vdm_v", "vdm_o"]:
rd[key] = d[key]
# command type is ACK and not REQ for all command replies
rd["vdm_ct"] = 1 # ACK
if command_name == "Discover Identity":
# discover identity response with "we are an altmode adapter yesyes"
data = list(b'A\xA0\x00\xff\xa4%\x00,\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x11')
r = create_vdm_data(rd, data[4:])
print(r)
print(data)
send_command(d["t"], r)
#sys.stdout.write("a") # debug stuff
elif command_name == "Discover SVIDs":
data = list(b'B\xA0\x00\xff\x00\x00\x01\xff')
r = create_vdm_data(rd, data[4:])
print(r)
print(data)
send_command(d["t"], r)
#sys.stdout.write("b")
elif command_name == "Discover Modes":
#data = list(b'C\xA0\x01\xff\x45\x04\x00\x00')
data = list(b'C\xA0\x01\xff\x05\x0c\x00\x00')
r = create_vdm_data(rd, data[4:])
print(r)
print(data)
send_command(d["t"], r)
#sys.stdout.write("c")
elif command_name == "Enter Mode":
data = list(b'D\xA1\x01\xff')
r = create_vdm_data(rd, [])
print(r)
print(data)
send_command(d["t"], r)
#sys.stdout.write("d")
elif command_name == "DP Status Update":
#data = list(b'P\xA1\x01\xff\x1a\x00\x00\x00')
data = list(b'P\xA1\x01\xff\x9a\x00\x00\x00')
r = create_vdm_data(rd, data[4:])
print(r)
print(data)
send_command(d["t"], r)
#sys.stdout.write("e")
elif command_name == "DP Configure":
data = list(b'Q\xA1\x01\xff')
r = create_vdm_data(rd, [])
print(r)
print(data)
send_command(d["t"], r)
#sys.stdout.write("f")
# no unstructured vdm processing at this time
def create_vdm_data(d, data):
"""
Creates the VDM header (PDO) from a dict with pre-supplied data and an additional data list.
"""
l = 4 + len(data)
vdm = bytearray(l)
for i in data:
vdm[i+4] = i
# most basic vdm flags
vdm_s = d["vdm_s"]
vdm[1] |= vdm_s << 7
vdm_sv = d["vdm_sv"]
vdm[2] = vdm_sv & 0xff
vdm[3] = vdm_sv >> 8
# can't build unstructured vdms yet
if vdm_s:
# building structured vdm
# vdm command
vdm_c = d["vdm_c"]
vdm[0] |= (vdm_c & 0b11111)
# vdm command type
vdm_ct = d["vdm_ct"]
vdm[0] |= (vdm_ct & 0b11) << 6
# default version codes set to 0b01; 0b00
vdm_v = d.get("vdm_v", 0b0100)
vdm[1] |= (vdm_v & 0b1111) << 3
# object position
vdm_o = d.get("vdm_o", 0)
vdm[1] |= vdm_o & 0b111
else:
raise NotImplementedError
return bytes(vdm)
def parse_vdm(d):
data = d['d']
is_structured = data[1] >> 7
d["vdm_s"] = is_structured
svid = (data[3] << 8) + data[2]
d["vdm_sv"] = svid
svid_name = svids.get(svid, "Unknown ({})".format(hex(svid)))
d["vdm_svn"] = svid_name
if is_structured:
# version: major and minor
version_bin = (data[1] >> 3) & 0xf
d["vdm_v"] = version_bin
obj_pos = data[1] & 0b111
d["vdm_o"] = obj_pos
cmd_type = data[0]>>6