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 \SYSTEM\CurrentControlSet\Control\usbflags
28 \SYSTEM\CurrentControlSet\Enum\USB\VID_1209\MainBrain_MZ
29
30
31
32
33
34
35 #include <xc.h>
36 #include <string.h>
37 #include <stdbool.h>
38 #include "MainBrain.h"
39 #include <proc/p32mz2048efh100.h>
40 bool isConnected = false;
41 const uint8_t seqSize = 16;
42
43 union
44 {
45 float fval;
46 uint8_t bytes[4];
47 } converter;
48
49 typedef struct
50 {
51 volatile unsigned char bmRequestType;
52 volatile unsigned char bRequest;
53 volatile unsigned short wValue;
54 volatile unsigned short wIndex;
55 volatile unsigned short wLength;
56 } USB_TRANSACTION;
57
58 USB_TRANSACTION USB_transaction;
59
60 USB_ENDPOINT EP[3];
61
62 uint8_t device_descriptor[] =
63 {
64 0x12,
65 0x01,
66 0x00,0x02,
67 0x00,
68 0x00,
69 0x00,
70 0x40,
71 0x09,0x12,
72 0x01,0x00,
73 0x00,0x02,
74 0x01,
75 0x02,
76 0x02,
77 0x01
78 };
79
80 uint8_t config_descriptor[] =
81 {
82
83 0x09,
84 0x02,
85 0x20,0x00,
86 0x01,
87 0x01,
88 0x00,
89 0xc0,
90 0x32,
91
92
93 0x09,
94 0x04,
95 0x00,
96 0x00,
97 0x02,
98 0x00,
99 0x00,
100 0x00,
101 0x00,
102
103
104
105 0x07,
106 0x05,
107 0x01,
108 0x02,
109 0x40,0x00,
110 0x00,
111
112 0x07,
113 0x05,
114 0x82,
115 0x02,
116 0x40,0x00,
117 0x00
118 };
119
120 uint8_t device_qualifier[] =
121 {
122 0x0a,
123 0x06,
124 0x00, 0x02,
125 0xff,
126 0xff,
127 0xff,
128 0x40,
129 0x01,
130 0x00
131 };
132
133 uint8_t MSOSDescriptor[] =
134 {
135
136 0x0b,
137
138 0x03,
139
140 'M',0,'S',0,'F',0,'T',0,'1',0,'0',0,'0',0,
141
142 0xee,
143
144
145
146 0x00
147 };
148
149
150 uint8_t ExtCompatIDFeatureDescriptor[] =
151 {
152 0x28, 0x00, 0x00, 0x00,
153 0x00, 0x01,
154 0x04, 0x00,
155 0x01,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00,
158 0x01,
159 0x57, 0x49, 0x4E, 0x55, 0x53, 0x42, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
162 };
163
164 uint8_t ExtPropertyFeatureDescriptor[] =
165 {
166
167 0x8e, 0x00, 0x00, 0x00,
168 0x00, 0x01,
169 0x05, 0x00,
170 0x01, 0x00,
171
172 0x84, 0x00, 0x00, 0x00,
173 0x01,0x00, 0x00, 0x00,
174 0x28, 0x00,
175 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0,
176 'e', 0, 'G', 0, 'U', 0, 'I', 0, 'D', 0, 0x00, 0x00,
177 0x4e, 0x00, 0x00, 0x00,
178
179
180 '{', 0, '2', 0, 'b', 0, '8', 0, 'a', 0, '8', 0, '2', 0, '1', 0, '6', 0, '-', 0, 'c', 0, '8', 0, '2', 0, 'a', 0,
181 '-', 0, '4', 0, 'a', 0, '9', 0, '1', 0, '-', 0, 'a', 0, '8', 0, 'b', 0, 'c', 0, '-', 0, 'a', 0, '1', 0, '2',
182 0, '1', 0, '2', 0, '9', 0, 'd', 0, '2', 0, 'd', 0, '7', 0, '0', 0, 'b', 0, '}', 0, 0x00, 0x00
183
184
185
186
187 };
188
189
190 uint8_t string0[] = {4, 0x03, 0x09, 0x04};
191
192
193 uint8_t string1[] = {28, 3, 'A', 0, 'n', 0, 't', 0, 'i', 0, 'm', 0, 'a', 0, 't', 0, 't', 0, 'e', 0, 'r', 0, '.', 0, 'm', 0, 'e', 0};
194
195
196 uint8_t string2[] = {26, 3, 'M', 0, 'a', 0, 'i', 0, 'n', 0, 'B', 0, 'r', 0, 'a', 0, 'i', 0, 'n', 0, ' ', 0, 'M', 0, 'Z', 0};
197
198
199 uint8_t string3[] = {10, 3, '0', 0, '0', 0, '0', 0, '1', 0};
200
201 void EP0_control_transaction(void);
202 void USB_queue_EP0(uint8_t *buffer, int size, int max_size);
203 void EP0_RX(int length);
204 void EP0_TX(void);
205 void Host_CMDs(void);
206
207 int EP1_RX(void);
208 int EP2_TX(volatile uint8_t *tx_buffer);
209 int EP0_Wait_TXRDY(void);
210 int EP2_Wait_TXRDY(void);
211 volatile uint8_t usbAddress;
212 volatile bool SetAddress = true;
213 volatile uint8_t USBState;
214 volatile uint8_t DeviceState;
215
216 void USB_init(void)
217 {
218
219 USBCSR0bits.SOFTCONN = 0;
220
221
222 USBState = DETACHED;
223 DeviceState = DISCONNECTED;
224
225
226 USBCSR0bits.SOFTCONN = 0;
227
228
229
230 USBCSR3bits.ENDPOINT = 1;
231
232
233 USBOTGbits.RXFIFOSZ = 0x06;
234 USBIENCSR1bits.RXMAXP = 64;
235 USBIENCSR3bits.RXFIFOSZ = 0x09;
236 USBFIFOAbits.RXFIFOAD = 0x0280;
237 USBIENCSR3bits.PROTOCOL = 0x02;
238 USBIENCSR3bits.TEP = 0x01;
239 USBIENCSR1bits.FLUSH = 1;
240
241
242
243 USBOTGbits.TXFIFOSZ = 0x06;
244 USBIENCSR0bits.TXMAXP = 64;
245 USBIENCSR3bits.TXFIFOSZ = 0x09;
246 USBFIFOAbits.TXFIFOAD = 0x0080;
247 USBIENCSR2bits.PROTOCOL = 0x02;
248 USBIENCSR2bits.TEP = 0x02;
249 USBIENCSR0bits.FLUSH = 1;
250
251
252 USBE1CSR0bits.MODE = 0;
253
254
255 USBE2CSR0bits.MODE = 1;
256
257
258 USBE0CSR0bits.TXMAXP = 64;
259
260
261 usbAddress = 0;
262 USBCSR0bits.FUNC = 0;
263
264
265 USBCRCONbits.VBUSMONEN = 1;
266
267
268 USBCSR2bits.RESETIE = 1;
269
270
271 IEC4bits.USBIE = 1;
272
273
274 USBCRCONbits.USBIE = 1;
275
276
277 IFS4bits.USBIF = 0;
278
279
280
281
282
283 IPC33bits.USBIP = 7;
284 IPC33bits.USBIS = 3;
285
286
287 USBE1CSR1bits.PIDERR = 1;
288
289
290 USBCSR0bits.HSEN = 1;
291
292
293 USBCSR0bits.SOFTCONN = 1;
294 }
295
296
297 void __attribute__((vector(_USB_VECTOR), interrupt(ipl7srs), nomips16)) USB_handler()
298 {
299
300 if(USBCSR2bits.RESETIF)
301 {
302 USBState = DETACHED;
303
304
305 USBE1CSR0bits.MODE = 1;
306
307
308 USBE0CSR0bits.TXMAXP = 64;
309
310
311 USBE0CSR2bits.SPEED = 1;
312
313
314 USBE1CSR2bits.SPEED = 1;
315
316
317 USBE1CSR0bits.TXMAXP = 64;
318
319
320 USBE1CSR2bits.PROTOCOL = 2;
321
322 <1:0>
323
324
325
326
327
328 USBCSR1bits.EP1TXIE = 1;
329 USBCSR2bits.EP1RXIE = 1;
330
331 USBCSR2bits.RESETIF = 0;
332 }
333
334
335 if(USBCSR0bits.EP0IF == 1)
336 {
337
338 if (SetAddress == true)
339 {
340
341 USBCSR0bits.FUNC = usbAddress & 0x7F;
342 SetAddress = false;
343 }
344
345 if(USBE0CSR0bits.RXRDY)
346 {
347 EP0_RX(USBE0CSR2bits.RXCNT);
348
349 USB_transaction.bmRequestType = EP[0].rx_buffer[0];
350 USB_transaction.bRequest = EP[0].rx_buffer[1];
351 USB_transaction.wValue = (int)(EP[0].rx_buffer[3] << 8) | EP[0].rx_buffer[2];
352 USB_transaction.wIndex = (int)(EP[0].rx_buffer[5] << 8) | EP[0].rx_buffer[4];
353 USB_transaction.wLength = (int)(EP[0].rx_buffer[7] << 8) | EP[0].rx_buffer[6];
354
355 EP0_control_transaction();
356
357
358 if (USB_transaction.wLength == 0)
359 {
360 USBE0CSR0bits.DATAEND = 1;
361 }
362 }
363
364 if (USBE0CSR0bits.SETEND)
365 {
366 USBE0CSR0bits.SETENDC = 1;
367 }
368
369
370 USBCSR0bits.EP0IF = 0;
371 }
372
373
374 if(USBCSR1bits.EP1RXIF == 1)
375 {
376 EP1_RX();
377
378 Host_CMDs();
379
380 USBCSR1bits.EP1RXIF = 0;
381 }
382
383 IFS4bits.USBIF = 0;
384 }
385
386 void Host_CMDs()
387 {
388 uint8_t test;
389 uint8_t SeqNum;
390
391 switch (EP[1].rx_buffer[0])
392 {
393
394 case 0x00:
395 if(EP[1].rx_buffer[1] == 0x02)
396 {
397 Beep();
398 DeviceState = CONNECTED;
399 }
400
401 if(EP[1].rx_buffer[1] == 0x05)
402 {
403 DeviceState = DISCONNECTED;
404 }
405 break;
406
407
408 case 0x01:
409 EP[2].tx_buffer[0] = 0x55;
410 EP2_TX(EP[2].tx_buffer);
411 break;
412
413
414 case 0x02:
415 strcpy(myStr, "USB - Test");
416
417
418 Message = 1;
419 break;
420
421
422 case 0x03:
423 Backlight_Control(EP[1].rx_buffer[1]);
424 break;
425
426
427 case 0x04:
428 Beep();
429 break;
430
431 case 0x05:
432
433 break;
434
435
436 case 0x07:
437
438 current_board_address = EP[1].rx_buffer[1];
439
440
441 int a = current_board_address * 0x400;
442
443
444 REN70V05_WR(a, EP[1].rx_buffer[2]);
445
446
447 REN70V05_WR((a + 23), EP[1].rx_buffer[3]);
448 REN70V05_WR((a + 24), EP[1].rx_buffer[4]);
449
450
451 REN70V05_WR(a + 25, EP[1].rx_buffer[5]);
452
453
454 requestDirective = true;
455 break;
456
457
458 case 0x08:
459
460 current_board_address = EP[1].rx_buffer[1];
461
462
463 a = current_board_address * 0x400;
464
465
466 for(int i=1;i<24;i++)
467 {
468 EP[2].tx_buffer[i + 9] = REN70V05_RD(a + i);
469 }
470
471
472 EP2_TX(EP[2].tx_buffer);
473
474
475 getBoardInfo = true;
476
477 screen = BOARD_SCREEN;
478 NeedsRefresh = true;
479
480 break;
481
482 case 0x09:
483
484 current_board_address = EP[1].rx_buffer[1];
485
486
487 a = current_board_address * 0x400;
488
489
490
491
492
493
494
495
496
497
498 requestDirective = true;
499
500
501 EP2_TX(EP[2].tx_buffer);
502 break;
503
504 case 0x0a:
505
506 break;
507
508 case 0x0b:
509
510 break;
511
512
513
514 case 0x64:
515
516
517 ADC0_result = ADC0_result * 0.00161172 * 342;
518 converter.fval = ADC0_result;
519
520 EP[2].tx_buffer[3] = converter.bytes[0];
521 EP[2].tx_buffer[2] = converter.bytes[1];
522 EP[2].tx_buffer[1] = converter.bytes[2];
523 EP[2].tx_buffer[0] = converter.bytes[3];
524
525
526
527 ADC6_result = (ADC6_result * 0.000805861 * 7.56) + 0.7;
528
529
530 converter.fval = ADC6_result;
531
532 EP[2].tx_buffer[7] = converter.bytes[0];
533 EP[2].tx_buffer[6] = converter.bytes[1];
534 EP[2].tx_buffer[5] = converter.bytes[2];
535 EP[2].tx_buffer[4] = converter.bytes[3];
536
537
538 current_board_address = EP[1].rx_buffer[1];
539
540
541 a = current_board_address * 0x400;
542
543
544
545 <65;i++)
546
547
548
549
550
551 <65;i++)
552
553
554
555
556
557 if(IO_Locked == false)
558 {
559
560 EP[2].tx_buffer[26] = REN70V05_RD(a+26);
561 EP[2].tx_buffer[27] = REN70V05_RD(a+27);
562 EP[2].tx_buffer[28] = REN70V05_RD(a+28);
563 EP[2].tx_buffer[29] = REN70V05_RD(a+29);
564 EP[2].tx_buffer[30] = REN70V05_RD(a+30);
565 EP[2].tx_buffer[31] = REN70V05_RD(a+31);
566
567
568 REN70V05_WR((a + 23), EP[1].rx_buffer[3]);
569 REN70V05_WR((a + 24), EP[1].rx_buffer[4]);
570
571
572
573
574
575 REN70V05_WR(a, 5);
576
577
578 requestDirective = true;
579 }
580
581
582 EP2_TX(EP[2].tx_buffer);
583
584 break;
585 case 0x65:
586 USB_init();
587 break;
588 default:
589
590 break;
591 }
592 updated = true;
593 }
594
595
596 void dumpMem(void)
597 {
598
599 REN70V05_RD((((current_board_address) - 1) * 0x400) + 0);
600 Binary2ASCIIHex(mdata_70V05);
601 WriteChar(40, 10, 'M', black, white);
602 WriteChar(55, 10, d_hex[1], black, white);
603 WriteChar(70, 10, d_hex[0], black, white);
604
605
606 WriteChar(140, 10, 'U', black, white);
607 Binary2ASCIIHex(EP[1].rx_buffer[0]);
608 WriteChar(155, 10, d_hex[1], black, white);
609 WriteChar(170, 10, d_hex[0], black, white);
610
611
612 REN70V05_RD((((current_board_address) - 1) * 0x400) + 1);
613 Binary2ASCIIHex(mdata_70V05);
614 WriteChar(40, 30, d_hex[1], black, white);
615 WriteChar(55, 30, d_hex[0], black, white);
616
617
618 REN70V05_RD((((current_board_address) - 1) * 0x400) + 2);
619 Binary2ASCIIHex(mdata_70V05);
620 WriteChar(70, 50, d_hex[1], black, white);
621 WriteChar(85, 50, d_hex[0], black, white);
622
623
624 REN70V05_RD((((current_board_address) - 1) * 0x400) + 3);
625 Binary2ASCIIHex(mdata_70V05);
626 WriteChar(40, 50, d_hex[1], black, white);
627 WriteChar(55, 50, d_hex[0], black, white);
628
629
630 REN70V05_RD((((current_board_address) - 1) * 0x400) + 4);
631 Binary2ASCIIHex(mdata_70V05);
632 WriteChar(70, 70, d_hex[1], black, white);
633 WriteChar(85, 70, d_hex[0], black, white);
634
635
636 REN70V05_RD((((current_board_address) - 1) * 0x400) + 5);
637 Binary2ASCIIHex(mdata_70V05);
638 WriteChar(40, 70, d_hex[1], black, white);
639 WriteChar(55, 70, d_hex[0], black, white);
640
641
642 REN70V05_RD((((current_board_address) - 1) * 0x400) + 6);
643 Binary2ASCIIHex(mdata_70V05);
644 WriteChar(40, 90, d_hex[1], black, white);
645 WriteChar(55, 90, d_hex[0], black, white);
646 }
647
648 int EP2_TX(volatile uint8_t* tx_buffer)
649 {
650 int cnt = 0;
651
652
653 EP[2].tx_num_bytes = 64;
654
655 for (cnt = 0; cnt < 64; cnt++)
656 {
657 EP[2].tx_buffer[cnt] = tx_buffer[cnt];
658 }
659
660
661 uint8_t *FIFO_buffer;
662
663
664 FIFO_buffer = (uint8_t *)&USBFIFO2;
665
666
667 if (EP2_Wait_TXRDY())
668 {
669 return 0;
670 }
671
672
673 cnt = 0;
674
675
676 while (cnt < EP[2].tx_num_bytes)
677 {
678 *FIFO_buffer = EP[2].tx_buffer[cnt];
679
680 cnt++;
681
682
683 if ((cnt > 0) && (cnt % 64 == 0))
684 {
685
686 USBE2CSR0bits.TXPKTRDY = 1;
687 if(EP2_Wait_TXRDY())
688 {
689 return 0;
690 }
691 }
692 }
693
694 USBE2CSR0bits.TXPKTRDY = 1;
695 }
696
697 int EP1_RX()
698 {
699 unsigned char *FIFO_buffer;
700 int cnt;
701 int rx_bytes;
702
703 rx_bytes = USBE1CSR2bits.RXCNT;
704
705
706 FIFO_buffer = (unsigned char *)&USBFIFO1;
707
708
709 for(cnt = 0; cnt < rx_bytes; cnt++)
710 {
711 EP[1].rx_buffer[cnt] = *(FIFO_buffer + (cnt & 3));
712 }
713
714
715 USBE1CSR1bits.RXPKTRDY = 0;
716
717 return rx_bytes;
718 }
719
720 void EP0_control_transaction()
721 {
722 uint16_t length;
723
724 if ((USB_transaction.bmRequestType == 0xC0) && (USB_transaction.wIndex == 0x04))
725 {
726 length = USB_transaction.wLength;
727 if (length > sizeof(ExtCompatIDFeatureDescriptor))
728 {
729 length = sizeof(ExtCompatIDFeatureDescriptor);
730 }
731
732 USB_queue_EP0(ExtCompatIDFeatureDescriptor, sizeof(ExtCompatIDFeatureDescriptor), length);
733
734 return;
735 }
736
737
738 if(USB_transaction.bmRequestType == 0xc1)
739 {
740
741 if(USB_transaction.bRequest == 0xee)
742 {
743
744 if(USB_transaction.wIndex == 0x05)
745 {
746
747
748 length = sizeof(ExtPropertyFeatureDescriptor);
749 if(USB_transaction.wLength < length)
750 {
751 length = USB_transaction.wLength;
752 }
753
754 USB_queue_EP0(ExtPropertyFeatureDescriptor, sizeof(ExtPropertyFeatureDescriptor), length);
755
756 USBE0CSR0bits.TXRDY = 1;
757
758 return;
759 }
760 }
761 }
762
763
764 switch (USB_transaction.bRequest)
765 {
766 case 0xC:
767 {
768 USBE0CSR0bits.STALL = 1;
769 break;
770
771 }
772 case 0x0:
773 {
774 if (USB_transaction.bmRequestType == 0x80)
775 USB_queue_EP0(device_descriptor, 0, 0);
776 if (USB_transaction.bmRequestType == 0x00)
777 USB_queue_EP0(device_descriptor, 0, 0);
778 break;
779 }
780
781
782 case 0x5:
783 {
784 USBE0CSR0bits.RXRDYC = 1;
785 usbAddress = EP[0].rx_buffer[2];
786
787 SetAddress = true;
788 break;
789 }
790
791
792 case 0xEE:
793 {
794 if (USB_transaction.bmRequestType == 0xC0)
795 {
796 USB_queue_EP0(MSOSDescriptor, sizeof(MSOSDescriptor), USB_transaction.wLength);
797 }
798 break;
799 }
800
801
802 case 0x6:
803 {
804 switch (USB_transaction.wValue >> 8)
805 {
806
807 case 0x1:
808 {
809 USB_queue_EP0(device_descriptor, sizeof(device_descriptor), USB_transaction.wLength);
810 break;
811 }
812
813
814 case 0x2:
815 {
816 USB_queue_EP0(config_descriptor, sizeof(config_descriptor), USB_transaction.wLength);
817 break;
818 }
819
820
821 case 0x3:
822 {
823 switch (USB_transaction.wValue & 0xff)
824 {
825
826 case 0x0:
827 {
828 USB_queue_EP0(string0, sizeof(string0), USB_transaction.wLength);
829 break;
830 }
831
832 case 0x1:
833 {
834 USB_queue_EP0(string1, sizeof(string1), USB_transaction.wLength);
835 break;
836 }
837
838 case 0x2:
839 {
840 USB_queue_EP0(string2, sizeof(string2), USB_transaction.wLength);
841 break;
842 }
843
844 case 0x3:
845 {
846 USB_queue_EP0(string3, sizeof(string3), USB_transaction.wLength);
847 break;
848 }
849
850 case 0xee:
851 {
852 USB_queue_EP0(MSOSDescriptor, sizeof(MSOSDescriptor), USB_transaction.wLength);
853 break;
854 }
855 break;
856 }
857 break;
858 }
859
860 case 0x04:
861 if (USB_transaction.wIndex == 0x0004)
862 {
863 USB_queue_EP0(ExtCompatIDFeatureDescriptor, sizeof(ExtCompatIDFeatureDescriptor), USB_transaction.wLength);
864 }
865 break;
866
867 case 0x05:
868 if (USB_transaction.wIndex == 0x0005)
869 {
870 USB_queue_EP0(ExtPropertyFeatureDescriptor, sizeof(ExtPropertyFeatureDescriptor), USB_transaction.wLength);
871 }
872 break;
873
874
875 case 0x6:
876 {
877 USB_queue_EP0(device_qualifier, sizeof(device_qualifier), USB_transaction.wLength);
878 break;
879 }
880 }
881 break;
882 }
883
884
885 case 0x9:
886 {
887
888 USBState = ATTACHED;
889 break;
890 }
891
892 default:
893 {
894 USBE0CSR0bits.STALL = 1;
895 break;
896 }
897 }
898 }
899
900
901 void USB_queue_EP0(uint8_t *buffer, int size, int max_size)
902 {
903 int cnt;
904
905 if (max_size < size)
906 size = max_size;
907
908 EP[0].tx_num_bytes = size;
909
910 for (cnt = 0; cnt < size; cnt++)
911 {
912 EP[0].tx_buffer[cnt] = buffer[cnt];
913 }
914
915 EP0_TX();
916 }
917
918
919 void EP0_TX()
920 {
921 static int ep0_tx_cnt = 0;
922 uint8_t *FIFO_buffer = (uint8_t *)&USBFIFO0;
923
924 while ((ep0_tx_cnt < EP[0].tx_num_bytes) && !USBE0CSR0bits.TXRDY)
925 {
926 *FIFO_buffer = EP[0].tx_buffer[ep0_tx_cnt++];
927 if ((ep0_tx_cnt % 64) == 0)
928 {
929 USBE0CSR0bits.TXRDY = 1;
930 return;
931 }
932 }
933
934
935 if (ep0_tx_cnt >= EP[0].tx_num_bytes)
936 {
937 USBE0CSR0bits.TXRDY = 1;
938 ep0_tx_cnt = 0;
939 }
940
941 }
942
943 void EP0_RX(int length)
944 {
945 int cnt;
946 uint8_t *FIFO_buffer;
947
948
949 EP[0].rx_num_bytes = USBE0CSR2bits.RXCNT;
950
951
952 FIFO_buffer = (uint8_t *)&USBFIFO0;
953
954 for(cnt = 0; cnt < length; cnt++)
955 {
956
957 EP[0].rx_buffer[cnt] = *(FIFO_buffer + (cnt & 3));
958 }
959
960 USBE0CSR0bits.RXRDYC = 1;
961 }
962
963 int EP0_Wait_TXRDY()
964 {
965 int timeout;
966
967 timeout = 0;
968
969 while (USBE0CSR0bits.TXRDY)
970 {
971 timeout++;
972
973 if (timeout > 5000)
974 {
975 return 1;
976 }
977 };
978
979 return 0;
980 }
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001 int EP2_Wait_TXRDY()
1002 {
1003 int timeout;
1004
1005 timeout = 0;
1006
1007 while (USBE1CSR0bits.TXPKTRDY)
1008 {
1009 timeout++;
1010
1011 if (timeout > 5000)
1012 {
1013 return 1;
1014 }
1015 };
1016
1017 return 0;
1018 }
1019
1020 void setTime(void)
1021 {
1022
1023 RTCCONbits.RTCWREN = 1;
1024
1025
1026
1027
1028
1029
1030
1031
1032 Binary2ASCIIBCD(EP[1].rx_buffer[1]);
1033
1034
1035 RTCTIMEbits.HR10 = d1;
1036 RTCTIMEbits.HR01 = d0;
1037
1038 hchar = hchar + 20;
1039 Binary2ASCIIBCD(EP[1].rx_buffer[2]);
1040
1041
1042
1043 RTCTIMEbits.MIN10 = d1;
1044 RTCTIMEbits.MIN01 = d0;
1045
1046 hchar = hchar + 20;
1047 Binary2ASCIIBCD(EP[1].rx_buffer[3]);
1048
1049
1050
1051
1052 RTCTIMEbits.SEC10 = d1;
1053 RTCTIMEbits.SEC01 = d0;
1054
1055
1056 RTCCONbits.RTCWREN = 0;
1057 }
1058
1059 void SRAM2USB(void)
1060 {
1061
1062 current_board_address = EP[1].rx_buffer[1];
1063
1064 for(int i=4;i<=63;i++)
1065 {
1066 EP[2].tx_buffer[i] = REN70V05_RD((((current_board_address) - 1) * 0x400) + i);
1067 }
1068 }
1069
1070 void BoardData2SRAM(void)
1071 {
1072
1073 current_board_address = EP[1].rx_buffer[1];
1074
1075 for(int i=0;i<=63;i++)
1076 {
1077 REN70V05_WR(((((current_board_address) - 1) * 0x400) + i), EP[1].rx_buffer[i]);
1078 }
1079
1080
1081 }
1082
1083 void USB2SRAM(void)
1084 {
1085
1086 current_board_address = EP[1].rx_buffer[1];
1087
1088 for(int i=0;i<064;i++)
1089 {
1090 REN70V05_WR(((((current_board_address) - 1) * 0x400) + i), EP[1].rx_buffer[i]);
1091 }
1092 }