td_init.c


1
// EnumHighSpeed: 1=wenn HS, 0=wenn FS
2
// USB_Mode: 1=Bulk, 0=Isoc.
3
4
void TD_Init(void) {
5
  unsigned char PFFlag[2];                    // 64Bytes must fit into the buffer -> Different sizes in HS and FS Mode and also in Bulk or Iso mode
6
  (*(volatile unsigned char xdata *) 0x532) = 0x40;        // EP2 (Out) Packet Size LSB, Don't obey FS or HS because mode is always allowed
7
  (*(volatile unsigned char xdata *) 0x533) = 0x00;        // EP2 (Out) Packet Size MSB
8
  (*(volatile unsigned char xdata *) 0x554) = 0x40;        // EP2 (Out) Packet Size LSB
9
  (*(volatile unsigned char xdata *) 0x555) = 0x00;        // EP2 (Out) Packet Size MSB
10
11
  if (EnumHighSpeed) {                      // HS-Device
12
    (*(volatile unsigned char xdata *) 0x539) = 0x00;      // EP6 (IN) Packet Size LSB, 512Bytes
13
    (*(volatile unsigned char xdata *) 0x53A) = 0x02;      // EP6 (IN) Packet Size MSB
14
    (*(volatile unsigned char xdata *) 0x53B) = 0x04;      // Polling must be slower, set it to 4 Frames
15
    (*(volatile unsigned char xdata *) 0x55B) = 0x00;      // EP6 (IN) Packet Size LSB, 512Bytes
16
    (*(volatile unsigned char xdata *) 0x55C) = 0x02;      // EP6 (IN) Packet Size MSB
17
    (*(volatile unsigned char xdata *) 0x55D) = 0x04;      // Polling must be slower, set it to 4 Frames
18
    if (USB_Mode) {                        // Bulk mode EP6
19
      (*(volatile unsigned char xdata *) 0x538) = 0x02;
20
      (*(volatile unsigned char xdata *) 0x55A) = 0x02;
21
    }
22
    else {                            // ISO mode EP6
23
      (*(volatile unsigned char xdata *) 0x538) = 0x01;
24
      (*(volatile unsigned char xdata *) 0x55A) = 0x01;
25
    }
26
    PFFlag[0] = 0xC0;                      // PF will get zero when last buffer has less than 64bytes free space
27
    PFFlag[1] = 0x19;
28
  }
29
  else {                              // FS Device
30
    (*(volatile unsigned char xdata *) 0x539) = 0x40;      // EP6 (IN) Packet Size LSB, 64Bytes
31
    (*(volatile unsigned char xdata *) 0x53A) = 0x00;      // EP6 (IN) Packet Size MSB
32
    (*(volatile unsigned char xdata *) 0x53B) = 0x02;      // Polling must be faster, set it to 2 Frames
33
    (*(volatile unsigned char xdata *) 0x55B) = 0x40;      // EP6 (IN) Packet Size LSB, 64Bytes
34
    (*(volatile unsigned char xdata *) 0x55C) = 0x00;      // EP6 (IN) Packet Size MSB
35
    (*(volatile unsigned char xdata *) 0x55D) = 0x02;      // Polling must be faster, set it to 2 Frames
36
    if (USB_Mode) {                        // Bulk mode EP6
37
      (*(volatile unsigned char xdata *) 0x538) = 0x02;
38
      (*(volatile unsigned char xdata *) 0x55A) = 0x02;
39
      PFFlag[0] = 0xC0;                    // PF will get zero when last buffer has less than 64bytes free space, which means last buffer is empty
40
      PFFlag[1] = 0x00;
41
    }
42
    else {                            // ISO mode EP6
43
      (*(volatile unsigned char xdata *) 0x538) = 0x01;
44
      (*(volatile unsigned char xdata *) 0x55A) = 0x01;
45
      PFFlag[0] = 0xC0;                    // PF will get zero when last buffer has less than 64bytes free space
46
      PFFlag[1] = 0x19;
47
    }
48
  }
49
50
51
52
  REVCTL = 0x03;
53
                                  // Setup EP2, Data from Host to DSP
54
    EP2CFG = 0xAA;                          // EP2: Valid (Bit7=1); Dir=Out (Bit6=0); Type=BULK (Bit5/4=10); Size=1024 (Bit3=1); Bit 2=0; BUF=Double (Bit1/0=10)
55
    SYNCDELAY;
56
    FIFORESET = 0x80;                        // Reset Fifo EP2
57
    SYNCDELAY;
58
    FIFORESET = 0x82;
59
    SYNCDELAY;
60
    FIFORESET = 0x00;
61
    SYNCDELAY;
62
  OUTPKTEND = 0x82;                        // Flush Buffer0
63
    SYNCDELAY;
64
  OUTPKTEND = 0x82;                        // Flush Buffer1, double buffer completly flushed
65
    SYNCDELAY;
66
    EP2FIFOCFG = 0x11;                        // AUTOOUT=1, WORDWIDE=1
67
    SYNCDELAY;
68
69
  /* Flush is needed during reinizialization by vendorCmd. Unfortunatly there is no way to flush packets
70
  * which have been auto-commited (AUTOIN=1). These packets must be read by host. The easiest way to do
71
  * this is to download them before firmware to dsp upload because in this case there are no commited
72
  * packets when the dsp starts up from reset.
73
  * In most cases FIFO will contain dsp data which haven't been yet commited. This data will be flushed
74
  * with "INPKTEND". Without this flush the first packet send from dsp during startup would contain old data.
75
  */
76
  INPKTEND = 0x86;                        // Flush buffer 0
77
    SYNCDELAY;
78
  INPKTEND = 0x86;                        // Flush buffer 1
79
    SYNCDELAY;
80
  INPKTEND = 0x86;                        // Flush buffer 2
81
    SYNCDELAY;
82
  INPKTEND = 0x86;                        // Flush buffer 3, quad buffer completly flushed
83
    SYNCDELAY;
84
  if (USB_Mode) EP6CFG = 0xE0;                  // EP6: Valid (Bit7=1); Dir=In (Bit6=1); Type=BULK (Bit5/4=10); Size=512 (Bit3=0);
85
                                  // Bit 2=0; BUF=Quad (Bit1/0=00)
86
  else EP6CFG = 0xD0;                        // EP6: Valid (Bit7=1); Dir=In (Bit6=1); Type=ISO (Bit5/4=01); Size=512 (Bit3=0);
87
                                  // Bit 2=0; BUF=Quad (Bit1/0=00)
88
    SYNCDELAY;
89
    FIFORESET = 0x80;                        // Reset Fifo EP6
90
    SYNCDELAY;
91
    FIFORESET = 0x86;
92
    SYNCDELAY;
93
    FIFORESET = 0x00;
94
    SYNCDELAY;
95
  if (!USB_Mode) {                        // Isochron
96
    EP6FIFOCFG = 0x0D;                        // AUTOIN=1, ZEROINLEN=1, WORDWIDE=1
97
  }
98
  else {
99
    EP6FIFOCFG = 0x09;                        // AUTOIN=1, ZEROINLEN=0, WORDWIDE=1
100
  }
101
  if (EnumHighSpeed) {                      // HS-Device, commit after maxpacketsize which is 512
102
    EP6AUTOINLENH = 0x02;
103
    SYNCDELAY;
104
    EP6AUTOINLENL = 0x00;
105
  }
106
  else {
107
    if (USB_Mode) {                        // Bulk mode EP6, commit after maxpacketsize which is 64
108
      EP6AUTOINLENH = 0x00;
109
      SYNCDELAY;
110
      EP6AUTOINLENL = 0x40;
111
    }
112
    else {                            // Iso Mode EP6, commit after maxpacketsize which is 512
113
      EP6AUTOINLENH = 0x02;
114
      SYNCDELAY;
115
      EP6AUTOINLENL = 0x00;
116
    }
117
  }
118
119
    SYNCDELAY;
120
  EP6FIFOPFL = PFFlag[0];
121
  SYNCDELAY;
122
  EP6FIFOPFH = PFFlag[1];
123
  SYNCDELAY;
124
    Rwuen = TRUE;
125
  OEA = 0x0B;                            // Enable output Port A, PA0, PA1, PA2
126
127
128
  Sub1_Reset();
129
}