-
Notifications
You must be signed in to change notification settings - Fork 36
/
usb_host.c
1957 lines (1790 loc) · 46.4 KB
/
usb_host.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
#include <stdint.h>
#include <stddef.h>
#include <sys/cdefs.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "driver/timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/soc.h"
#include "soc/rtc.h"
#include <math.h>
#include "esp_heap_caps.h"
#include "utils.h"
/*******************************
* warning!!!: any copy of this code or his part must include this:
* "The original was written by Dima Samsonov @ Israel [email protected] on 3/2021" *
* Copyright (C) 2021 Dmitry Samsonov *
********************************/
#include "usb_host.h"
#include "report_map.h"
#include "esp_log.h"
#define T_START 0b00000001
#define T_ACK 0b01001011
#define T_NACK 0b01011010
#define T_SOF 0b10100101
#define T_SETUP 0b10110100
#define T_DATA0 0b11000011
#define T_DATA1 0b11010010
#define T_DATA2 0b11100001
#define T_OUT 0b10000111
#define T_IN 0b10010110
#define T_ERR 0b00111100
#define T_PRE 0b00111100
#define T_NYET 0b01101001
#define T_STALL 0b01111000
// local non std
#define T_NEED_ACK 0b01111011
#define T_CHK_ERR 0b01111111
#define USB_LS_K 0
#define USB_LS_J 1
#define USB_LS_S 2
//most counters- uint_8t : so prevents overflow...
// somethins short like ACK
#define SMALL_NO_DATA 36
///cpufreq (must be 240) /8 count = 30MHz convinient number for measure 1.5MHz of low speed USB
//~ static inline uint32_t _getCycleCount32(void) {
//~ uint32_t ccount;
//~ __asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
//~ return ccount;
//~ }
//timing calibrations which depends CPU_FREQ, calibrated in initPins()
int TRANSMIT_TIME_DELAY = 110; //delay each bit transmit
int TIME_MULT = 25; //received time factor delta clocks* TIME_MULT/TIME_SCALE
int TM_OUT = 64; //receive time out no activity on bus
#define TIME_SCALE (1024)
//#define TEST
#ifdef TEST
#define TOUT 1000
#else
#define TOUT (TM_OUT)
#endif
#include "hal/cpu_hal.h"
#include "hal/gpio_hal.h"
static inline uint32_t _getCycleCount32()
{
uint32_t ccount = cpu_hal_get_cycle_count();
return ccount;
}
static inline uint8_t _getCycleCount8d8(void)
{
uint32_t ccount = cpu_hal_get_cycle_count();
return ccount>>3;
}
void led(uint8_t onoff){}
#if CONFIG_IDF_TARGET_ESP32
#define SET_I { PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[DP_PIN]); PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[DM_PIN]); GPIO.enable_w1tc = (1 << DP_PIN) | (1 << DM_PIN); }
#define SET_O { GPIO.enable_w1ts = (1 << DP_PIN) | (1 << DM_PIN); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[DP_PIN]); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[DM_PIN]); }
#define SE_J { *snd[1][0] = (1 << DM_PIN);*snd[1][1] = (1 << DP_PIN); }
#define SE_0 { *snd[2][0] = (1 << DM_PIN);*snd[2][1] = (1 << DP_PIN); }
#define READ_BOTH_PINS (((GPIO.in&RD_MASK)<<8)>>RD_SHIFT)
uint32_t * snd[4][2] = {
{&GPIO.out_w1tc,&GPIO.out_w1ts},
{&GPIO.out_w1ts,&GPIO.out_w1tc},
{&GPIO.out_w1tc,&GPIO.out_w1tc},
{&GPIO.out_w1tc,&GPIO.out_w1tc}
} ;
#else
#define SET_I { PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[DP_PIN]); PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[DM_PIN]); gpio_ll_output_disable(&GPIO,DM_PIN); gpio_ll_output_disable(&GPIO,DP_PIN);}
#define SET_O { GPIO.enable_w1ts.val = (1 << DP_PIN) | (1 << DM_PIN); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[DP_PIN]); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[DM_PIN]); }
#define SE_J { *snd[1][0] = (1 << DM_PIN);*snd[1][1] = (1 << DP_PIN); }
#define SE_0 { *snd[2][0] = (1 << DM_PIN);*snd[2][1] = (1 << DP_PIN); }
#define READ_BOTH_PINS (((GPIO.in.val&RD_MASK)<<8)>>RD_SHIFT)
uint32_t * snd[4][2] = {
{&GPIO.out_w1tc.val,&GPIO.out_w1ts.val},
{&GPIO.out_w1ts.val,&GPIO.out_w1tc.val},
{&GPIO.out_w1tc.val,&GPIO.out_w1tc.val},
{&GPIO.out_w1tc.val,&GPIO.out_w1tc.val}
} ;
#endif
//must be setup ech time with setPins
uint32_t DP_PIN;
uint32_t DM_PIN;
uint32_t DM_PIN_M;
uint32_t DP_PIN_M;
uint16_t M_ONE;
uint16_t P_ONE;
uint32_t RD_MASK;
uint32_t RD_SHIFT;
//end must be setup ech time with setPins
// temporary used insize lowlevel
volatile uint8_t received_NRZI_buffer_bytesCnt;
uint16_t received_NRZI_buffer[DEF_BUFF_SIZE];
volatile uint8_t transmit_bits_buffer_store_cnt;
//uint8_t transmit_bits_buffer_store[DEF_BUFF_SIZE];
// share same memory as received_NRZI_buffer
uint8_t* transmit_bits_buffer_store = (uint8_t*)&received_NRZI_buffer[0];
volatile uint8_t transmit_NRZI_buffer_cnt;
uint8_t transmit_NRZI_buffer[DEF_BUFF_SIZE];
volatile uint8_t decoded_receive_buffer_head;
volatile uint8_t decoded_receive_buffer_tail;
uint8_t decoded_receive_buffer[DEF_BUFF_SIZE];
// end temporary used insize lowlevel
#if 1
void (*delay_pntA)() =NULL;
#define cpuDelay(x) {(*delay_pntA)();}
#if CONFIG_IDF_TARGET_ESP32
void setDelay(uint8_t ticks)
{
// opcodes of void test_delay() {__asm__ (" nop"); __asm__ (" nop"); __asm__ (" nop"); ...}
//36 41 00 3d f0 1d f0 00 // one nop
//36 41 00 3d f0 3d f0 3d f0 3d f0 3d f0 1d f0 00 // five nops
//36 41 00 3d f0 3d f0 3d f0 3d f0 3d f0 3d f0 1d f0 00 00 00 //
int MAX_DELAY_CODE_SIZE = 0x280;
uint8_t* pntS;
// it can't execute but can read & write
if(!delay_pntA)
{
pntS = malloc(MAX_DELAY_CODE_SIZE);
}
else
{
pntS = heap_caps_realloc(delay_pntA, MAX_DELAY_CODE_SIZE, MALLOC_CAP_8BIT);
}
uint8_t* pnt = (uint8_t*)pntS;
//put head of delay procedure
*pnt++ = 0x36;
*pnt++ = 0x41;
*pnt++ = 0;
for(int k=0;k<ticks;k++)
{
//put NOPs
*pnt++ = 0x3d;
*pnt++ = 0xf0;
}
//put tail of delay procedure
*pnt++ = 0x1d;
*pnt++ = 0xf0;
*pnt++ = 0x00;
*pnt++ = 0x00;
// move it to executable memory segment
// it can't write but can read & execute
delay_pntA = heap_caps_realloc(pntS,MAX_DELAY_CODE_SIZE,MALLOC_CAP_EXEC);
if(!delay_pntA)
{
printf("Some goind wrong. Disable memory prot !\n delay_pntA = %p\n",delay_pntA);
}
}
#else
void setDelay(uint8_t ticks)
{
// opcodes of void test_delay() {__asm__ (" nop"); __asm__ (" nop"); __asm__ (" nop"); ...}
//36 41 00 3d f0 1d f0 00 // one nop
//36 41 00 3d f0 3d f0 3d f0 3d f0 3d f0 1d f0 00 // five nops
//36 41 00 3d f0 3d f0 3d f0 3d f0 3d f0 3d f0 1d f0 00 00 00 //
int MAX_DELAY_CODE_SIZE = 0x210;
uint8_t* pntS;
// it can't execute but can read & write
if(!delay_pntA)
{
pntS = heap_caps_aligned_alloc(32,MAX_DELAY_CODE_SIZE, MALLOC_CAP_8BIT);
//~ printf("zero pntS = %p\n",pntS);
}
else
{
pntS = heap_caps_realloc(delay_pntA, MAX_DELAY_CODE_SIZE, MALLOC_CAP_8BIT);
//~ printf("pntS = %p\n",pntS);
}
uint8_t* pnt = (uint8_t*)pntS;
//put head of delay procedure
//~ uint8_t* pnt = (uint8_t*)pntS;
//put head of delay procedure
for(int k=0;k<ticks;k++)
{
//put NOPs
*pnt++ = 0x1;
*pnt++ = 0x0;
}
//put tail of delay procedure
*pnt++ = 0x82;
*pnt++ = 0x80;
// move it to executable memory segment
// it can't write but can read & execute
//delay_pntA = heap_caps_realloc(pntS,MAX_DELAY_CODE_SIZE,MALLOC_CAP_EXEC);
//delay_pntA = (void*)dram_alloc_to_iram_addr(pntS,MAX_DELAY_CODE_SIZE);
delay_pntA = heap_caps_realloc(pntS,MAX_DELAY_CODE_SIZE,MALLOC_CAP_32BIT | MALLOC_CAP_EXEC);
if(!delay_pntA)
{
printf("idf.py menuconfig\n Component config-> ESP System Setting -> Memory protectiom-> Disable.\n memory prot must be disabled!!!\n delay_pntA = %p\n",delay_pntA);
exit(0);
}
}
#endif
#else
void setDelay(uint32_t tick)
{
}
inline void cpuDelayNop(uint32_t ticks)
{
for(int k=0;k<ticks;k++)
{
__asm__ __volatile__(" nop");
}
}
inline void cpuDelay(uint32_t tick)
{
uint32_t stop =_getCycleCount32() + tick;
while((_getCycleCount32() - stop)&0x80000000u);
}
#endif
sUsbContStruct * current;
void parseImmed(sUsbContStruct * pcurrent)
{
static sCfgDesc cfg;
static sIntfDesc sIntf;
static HIDDescriptor hid[4];
static sEPDesc epd;
static int cfgCount = 0;
static int sIntfCount = 0;
static int hidCount = 0;
int pos = 0;
#define STDCLASS 0x00
#define HIDCLASS 0x03
#define HUBCLASS 0x09 /* bDeviceClass, bInterfaceClass */
pcurrent->epCount = 0;
pcurrent->hid_count = 0;
while(pos<pcurrent->descrBufferLen-2)
{
uint8_t len = pcurrent->descrBuffer[pos];
uint8_t type = pcurrent->descrBuffer[pos+1];
if(len==0)
{
//printf("pos = %02x type = %02x cfg.wLength = %02x pcurrent->acc_decoded_resp_counter = %02x\n ",pos,type,cfg.wLength,pcurrent->acc_decoded_resp_counter);
pos = pcurrent->descrBufferLen;
}
if(pos+len<=pcurrent->descrBufferLen)
{
if(type == 0x2)
{
//memcpy(&cfg,&pcurrent->descrBuffer[pos],len);
}
else if (type == 0x4)
{
//memcpy(&sIntf,&pcurrent->descrBuffer[pos],len);
}
else if (type == 0x21)
{
/*
hidCount++;
int i = hidCount-1;
memcpy(&hid[i],&pcurrent->descrBuffer[pos],len);*/
//usbMess(1, len, &pcurrent->descrBuffer[pos]);
//int llen = (int)len;
//printf("hid desc size\n");
//print_hex_dump("HID描述符", &pcurrent->descrBuffer[pos], llen);
if(pcurrent->hid_count <= 3){
memcpy(&pcurrent->hid[pcurrent->hid_count],&pcurrent->descrBuffer[pos],len);
pcurrent->hid_count ++;
}else{
//printf("More than 4 HID Descriptors!");
}
}
else if (type == 0x5)
{
pcurrent->epCount++;
//memcpy(&epd,&pcurrent->descrBuffer[pos],len);
}
}
pos+=len;
}
}
// received data from ep0,ep1!!!!
//uint8_t current->Resp0[DEF_BUFF_SIZE];
//uint8_t current->R0Bytes;
//uint8_t current->Resp1[DEF_BUFF_SIZE];
//uint8_t current->R1Bytes;
#ifdef WR_SIMULTA
uint32_t sndA[4] = {0,0,0,0};
#endif
inline void restart()
{
transmit_NRZI_buffer_cnt = 0;
}
void decoded_receive_buffer_clear()
{
decoded_receive_buffer_tail = decoded_receive_buffer_head;
}
inline void decoded_receive_buffer_put(uint8_t val)
{
decoded_receive_buffer[decoded_receive_buffer_head] = val;
decoded_receive_buffer_head++;
}
uint8_t decoded_receive_buffer_get()
{
return decoded_receive_buffer[decoded_receive_buffer_tail++];
}
uint8_t decoded_receive_buffer_size()
{
return (uint8_t )(decoded_receive_buffer_head-decoded_receive_buffer_tail);
}
uint8_t cal5()
{
uint8_t crcb;
uint8_t rem;
crcb = 0b00101;
rem = 0b11111;
for(int k=16;k<transmit_bits_buffer_store_cnt;k++)
{
int rb = (rem>>4)&1;
rem = (rem<<1)&0b11111;
if(rb^(transmit_bits_buffer_store[k]&1))
{
rem ^= crcb;
}
}
return (~rem)&0b11111;
}
uint32_t cal16()
{
uint32_t crcb;
uint32_t rem;
crcb = 0b1000000000000101;
rem = 0b1111111111111111;
for(int k=16;k<transmit_bits_buffer_store_cnt;k++)
{
int rb = (rem>>15)&1;
rem = (rem<<1)&0b1111111111111111;
if(rb^(transmit_bits_buffer_store[k]&1))
{
rem ^= crcb;
}
}
return (~rem)&0b1111111111111111;
}
inline void seB(int bit)
{
transmit_bits_buffer_store[transmit_bits_buffer_store_cnt++] = bit;
}
inline void pu_MSB(uint16_t msg,int N)
{
for(int k=0;k<N;k++)
{
seB(msg&(1<<(N-1-k))?1:0);
}
}
inline void pu_LSB(uint16_t msg,int N)
{
for(int k=0;k<N;k++)
{
seB(msg&(1<<(k))?1:0);
}
}
void repack()
{
int last = USB_LS_J;
int cntOnes = 0;
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_J;
for(int k=0;k<transmit_bits_buffer_store_cnt;k++)
{
if(transmit_bits_buffer_store[k]==0)
{
if(last==USB_LS_J||last==USB_LS_S)
{
last = USB_LS_K;
}
else
{
last = USB_LS_J;
}
cntOnes = 0;
}
else if(transmit_bits_buffer_store[k]==1)
{
cntOnes++;
if(cntOnes==6)
{
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt] = last;
transmit_NRZI_buffer_cnt++;
if(last==USB_LS_J)
{
last = USB_LS_K;
}
else
{
last = USB_LS_J;
}
cntOnes = 0;
}
if(last==USB_LS_S)
{
last = USB_LS_J;
}
}
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = last;
}
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_S;
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_S;
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_J;
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_J;
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_J;
transmit_NRZI_buffer[transmit_NRZI_buffer_cnt++] = USB_LS_J;
transmit_bits_buffer_store_cnt = 0;
}
uint8_t rev8(uint8_t j)
{
uint8_t res = 0;
for(int i=0;i<8;i++)
{
res<<=1;
res|=(j>>i)&1;
}
return res;
}
uint16_t rev16(uint16_t j)
{
uint16_t res = 0;
for(int i=0;i<16;i++)
{
res<<=1;
res|=(j>>i)&1;
}
return res;
}
#ifdef DEBUG_ALL
uint16_t debug_buff[0x100];
#endif
int parse_received_NRZI_buffer()
{
if(!received_NRZI_buffer_bytesCnt) return 0;
uint32_t crcb;
uint32_t rem;
crcb = 0b1000000000000101;
rem = 0b1111111111111111;
int res = 0;
int cntOnes = 0;
int terr = 0;
uint8_t current_res = 0xfe;
uint16_t prev = received_NRZI_buffer[0];
int start = -1;
uint8_t prev_smb = M_ONE;
#ifdef DEBUG_ALL
debug_buff[0] = received_NRZI_buffer_bytesCnt;
uint8_t rcnt = 1;
debug_buff[received_NRZI_buffer_bytesCnt] = 0xff;
#endif
for(int i = 1;i<received_NRZI_buffer_bytesCnt;i++)
{
//define 2.5
uint16_t curr = (prev&0xff00) + (((received_NRZI_buffer[i] - prev))&0xff);
prev = received_NRZI_buffer[i];
uint8_t smb = curr>>8;
int tm = (curr&0xff);
//debug_buff[i] = tm | (smb<<8);
if( tm<2 || (smb == 0) )
{
//terr+=tm<4?tm : 4;
terr+=tm;
}
else
{
//terr = 0;
int delta = ((((curr+terr)&0xff))*TIME_MULT+TIME_SCALE/2)/TIME_SCALE;
for(int k=0;k<delta;k++)
{
int incc = 1;
{
if(prev_smb!=smb)
{
if(cntOnes!=6)
{
current_res = current_res*2+0;
}
else
{
incc = 0;
}
cntOnes = 0;
}
else
{
current_res = current_res*2+1;
cntOnes++;
}
if(start>=0)
{
start+=incc;
}
if(current_res==0x1 && start<0 )
{
start = 0;
}
if( (start&0x7) == 0 && incc)
{
if(start==8)
{
res = current_res;
}
#ifdef DEBUG_ALL
debug_buff[rcnt++] = current_res;
#endif
decoded_receive_buffer_put(current_res);
if(start>8)
{
for(int bt =0;bt<8;bt++)
{
int rb = (rem>>15)&1;
rem = (rem<<1)&0b1111111111111111;
if(rb^((current_res>>(7-bt))&1))
{
rem ^= crcb;
}
}
}
}
}
prev_smb = smb;
}
terr = 0;
}
}
#ifdef DEBUG_ALL
debug_buff[rcnt++] = 0xff;
#endif
rem &=0b1111111111111111;
if (rem==0b1111111111111111)
{
return res;
}
if(rem==0x800d)
{
return T_NEED_ACK;
}
else
{
return T_CHK_ERR;
}
}
//#define WR_SIMULTA
void sendOnly()
{
uint8_t k;
SET_O;
#ifdef WR_SIMULTA
uint32_t out_base = GPIO.out;
sndA[0] = (out_base | DP) &~DM;
sndA[1] = (out_base | DM) &~DP;
sndA[2] = (out_base )&~(DP | DM);
sndA[3] = out_base | (DM | DP);
#endif
for(k=0;k<transmit_NRZI_buffer_cnt;k++)
{
//usb_transmit_delay(10);
cpuDelay(TRANSMIT_TIME_DELAY);
#ifdef WR_SIMULTA
GPIO.out = sndA[transmit_NRZI_buffer[k]];
#else
//*snd[transmit_NRZI_buffer[k]][0] = (1 << DM_PIN);
//*snd[transmit_NRZI_buffer[k]][1] = (1 << DP_PIN);
*snd[transmit_NRZI_buffer[k]][0] = DM_PIN_M;
*snd[transmit_NRZI_buffer[k]][1] = DP_PIN_M;
#endif
}
restart();
SET_I;
}
#if 0
// safety option, but slower and works with high cpu freq >=160MHz .
void sendRecieveNParse()
{
uint8_t locRec = 0;
uint32_t val = 0xff;//DM_GPIO_Port->IDR&(0x3*DP_Pin);
uint32_t nval = 0xff;
int32_t act = TOUT;
// portDISABLE_INTERRUPTS();
sendOnly();
while(act>0 && (val||nval) )
{
val = nval;
nval = READ_BOTH_PINS;
received_NRZI_buffer[locRec] = _getCycleCount8d8() | nval;
if(val!=nval)
{
locRec++;
act = TOUT;
}
else act--;
//~ int flag = val!=nval;
//~ locRec += flag;
//~ act = (flag)?TOUT:(act-1);
}
// portENABLE_INTERRUPTS();
received_NRZI_buffer_bytesCnt = locRec;
}
#else
// dangerous option, but faster and works with low cpu freq ~80MHz . If we have noise on bus it can overflow received_NRZI_buffer[]
void sendRecieveNParse()
{
register uint32_t R3;
register uint16_t *STORE = received_NRZI_buffer;
//__disable_irq();
sendOnly();
register uint32_t R4;// = READ_BOTH_PINS;
START:
R4 = READ_BOTH_PINS;
*STORE = R4 | _getCycleCount8d8();
STORE++;
R3 = R4;
//R4 = READ_BOTH_PINS;
//if(R4!=R3) goto START;
if( R3 )
{
for(int k=0;k<TOUT;k++)
{
R4 = READ_BOTH_PINS;
if(R4!=R3) goto START;
}
}
//__enable_irq();
received_NRZI_buffer_bytesCnt = STORE-received_NRZI_buffer;
}
#endif
int sendRecieve()
{
sendRecieveNParse();
return parse_received_NRZI_buffer();
}
void SOF()
{
#if 1
if(1)
{
//reB();
repack();
}
// else
// {
//static int frameN = 0;
// frameN++;
// reB();
// pu_MSB(T_START,8);
// pu_MSB(T_SOF,8);// ack
// pu_LSB(frameN,11);
// pu_MSB(cal5(),5);
// repack();
// }
sendOnly();
#endif
}
void pu_Addr(uint8_t cmd,uint8_t addr,uint8_t eop)
{
//reB();
pu_MSB(T_START,8);
pu_MSB(cmd,8);//setup
pu_LSB(addr,7);
pu_LSB(eop,4);
pu_MSB(cal5(),5);
repack();
}
void pu_ShortCmd(uint8_t cmd)
{
//reB();
pu_MSB(T_START,8);
pu_MSB(cmd,8);//setup
//pu_MSB(cal16(),16);
pu_MSB(0,16);
repack();
}
void pu_Cmd(uint8_t cmd,uint8_t bmRequestType, uint8_t bmRequest,uint16_t wValue,uint16_t wIndex,uint16_t wLen)
{
//reB();
pu_MSB(T_START,8);
pu_MSB(cmd,8);//setup
pu_LSB(bmRequestType,8);
pu_LSB(bmRequest,8);
pu_LSB(wValue,16);
pu_LSB(wIndex,16);
pu_LSB(wLen,16);
pu_MSB(cal16(),16);
repack();
}
uint8_t ACK_BUFF[0x20];
int ACK_BUFF_CNT = 0;
void ACK()
{
transmit_NRZI_buffer_cnt =0;
if(ACK_BUFF_CNT==0)
{
//reB();
//repack();
//reB();
pu_MSB(T_START,8);
pu_MSB(T_ACK,8);// ack
repack();
memcpy(ACK_BUFF,transmit_NRZI_buffer,transmit_NRZI_buffer_cnt);
ACK_BUFF_CNT = transmit_NRZI_buffer_cnt;
}
else
{
memcpy(transmit_NRZI_buffer,ACK_BUFF,ACK_BUFF_CNT);
transmit_NRZI_buffer_cnt = ACK_BUFF_CNT;
}
sendOnly();
}
//enum CallbackCmd {CB_CHECK,CB_RESET,CB_WAIT0,CB_POWER,CB_TICK,CB_2,CB_3,CB_4,CB_5,CB_6,CB_7,CB_8,CB_9,CB_WAIT1} ;
//char* CallbackCmdCtr[] = {"CB_CHECK","CB_RESET","CB_WAIT0","CB_POWER","CB_TICK","CB_2","CB_3","CB_4","CB_5","CB_6","CB_7","CB_8","CB_9","CB_WAIT1"} ;
void IRAM_ATTR timerCallBack()
{
decoded_receive_buffer_clear();
if(current->cb_Cmd==CB_CHECK)
{
SET_I;
current->wires_last_state = READ_BOTH_PINS>>8;
if(current->wires_last_state==M_ONE)
{
// low speed
}
else if(current->wires_last_state==P_ONE)
{
//high speed
}
else if(current->wires_last_state==0x00)
{
// not connected
}
else if(current->wires_last_state== (M_ONE + P_ONE) )
{
//????
}
current->bComplete = 1;
}
else if (current->cb_Cmd==CB_RESET)
{
SOF();
sendRecieveNParse();
SET_O;
SE_0;
current->cmdTimeOut = 31;
current->cb_Cmd = CB_WAIT0;
}
else if (current->cb_Cmd==CB_WAIT0)
{
if(current->cmdTimeOut>0)
{
current->cmdTimeOut--;
}
else
{
//sendRecieveNParse();
current->bComplete = 1;
}
}
else if (current->cb_Cmd==CB_WAIT1)
{
SOF();
if(current->cmdTimeOut>0)
{
current->cmdTimeOut--;
}
else
{
sendRecieveNParse();
current->wires_last_state = READ_BOTH_PINS>>8;
current->bComplete = 1;
}
}
else if (current->cb_Cmd==CB_POWER)
{
// for TEST
#ifdef TEST
SOF();
sendRecieve();
SOF();
SOF();
#else
SET_O;
SE_J;
SET_I;
current->cmdTimeOut = 2;
current->cb_Cmd = CB_WAIT1;
#endif
}
else if (current->cb_Cmd==CB_TICK)
{
SOF();
current->bComplete = 1;
}
else if(current->cb_Cmd==CB_3)
{
SOF();
pu_Addr(current->rq.cmd,current->rq.addr,current->rq.eop);
pu_Cmd(current->rq.dataCmd, current->rq.bmRequestType, current->rq.bmRequest,current->rq.wValue, current->rq.wIndex, current->rq.wLen);
int res = sendRecieve();
if(res==T_ACK)
{
current->cb_Cmd=CB_4;
current->numb_reps_errors_allowed = 8;
return ;
}
else
{
current->numb_reps_errors_allowed--;
if(current->numb_reps_errors_allowed>0)
{
return ;
}
else
{
current->cb_Cmd=CB_TICK;
current->bComplete = 1;
}
}
}
else if(current->cb_Cmd==CB_4)
{
SOF();
pu_Addr(T_OUT,current->rq.addr,current->rq.eop);
//reB();
pu_MSB(T_START,8);
pu_MSB(T_DATA1,8);//setup
for(int k=0;k<current->transmitL1Bytes;k++)
{
pu_LSB(current->transmitL1[k],8);
}
pu_MSB(cal16(),16);
repack();
sendRecieveNParse();
pu_Addr(T_IN,current->rq.addr,current->rq.eop);
//setup
sendRecieveNParse();
if(received_NRZI_buffer_bytesCnt<SMALL_NO_DATA && received_NRZI_buffer_bytesCnt >SMALL_NO_DATA/4)
{
ACK();
}
else
{
current->numb_reps_errors_allowed--;
if(current->numb_reps_errors_allowed>0)
{
return ;
}
else
{
}
}
current->cb_Cmd=CB_TICK;
current->bComplete = 1;
}
else if(current->cb_Cmd==CB_5)
{
SOF();
pu_Addr(current->rq.cmd,current->rq.addr,current->rq.eop);
pu_Cmd(current->rq.dataCmd, current->rq.bmRequestType, current->rq.bmRequest,current->rq.wValue, current->rq.wIndex, current->rq.wLen);
sendRecieveNParse();
//int res = sendRecieve(current->asckedReceiveBytes>8?8:current->asckedReceiveBytes);
int res = parse_received_NRZI_buffer();
if(res==T_ACK)
{
current->cb_Cmd = CB_6;
current->in_data_flip_flop = 1;
current->numb_reps_errors_allowed = 4;
current->counterAck ++;
return ;
}