sanguino/cores/arduino/USBCore.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 /* Copyright (c) 2010, Peter Barrett
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4 **
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 ** Permission to use, copy, modify, and/or distribute this software for
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 ** any purpose with or without fee is hereby granted, provided that the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 ** above copyright notice and this permission notice appear in all copies.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 **
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 ** SOFTWARE.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 #include "Platform.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 #include "USBAPI.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 #include "USBDesc.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 #if defined(USBCON)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 #define EP_TYPE_CONTROL 0x00
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 #define EP_TYPE_BULK_IN 0x81
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 #define EP_TYPE_BULK_OUT 0x80
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 #define EP_TYPE_INTERRUPT_IN 0xC1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 #define EP_TYPE_INTERRUPT_OUT 0xC0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 #define EP_TYPE_ISOCHRONOUS_IN 0x41
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 #define EP_TYPE_ISOCHRONOUS_OUT 0x40
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 #define TX_RX_LED_PULSE_MS 100
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 //==================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 //==================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 extern const u16 STRING_LANGUAGE[] PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 extern const u16 STRING_IPRODUCT[] PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 extern const u16 STRING_IMANUFACTURER[] PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 const u16 STRING_LANGUAGE[2] = {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 (3<<8) | (2+2),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 0x0409 // English
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 const u16 STRING_IPRODUCT[17] = {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 (3<<8) | (2+2*16),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 #if USB_PID == 0x8036
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 'U','S','B',' ','I','O',' ','B','o','a','r','d',' ',' ',' ',' '
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 const u16 STRING_IMANUFACTURER[12] = {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 (3<<8) | (2+2*11),
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 #if USB_VID == 0x2341
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 'A','r','d','u','i','n','o',' ','L','L','C'
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 'U','n','k','n','o','w','n',' ',' ',' ',' '
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 #ifdef CDC_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 #define DEVICE_CLASS 0x02
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 #define DEVICE_CLASS 0x00
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 // DEVICE DESCRIPTOR
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 const DeviceDescriptor USB_DeviceDescriptor =
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 const DeviceDescriptor USB_DeviceDescriptorA =
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 //==================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 //==================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 volatile u8 _usbConfiguration = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 static inline void WaitIN(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 while (!(UEINTX & (1<<TXINI)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 static inline void ClearIN(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 UEINTX = ~(1<<TXINI);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 static inline void WaitOUT(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 while (!(UEINTX & (1<<RXOUTI)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 ;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 static inline u8 WaitForINOrOUT()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 ;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 return (UEINTX & (1<<RXOUTI)) == 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 static inline void ClearOUT(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 UEINTX = ~(1<<RXOUTI);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 void Recv(volatile u8* data, u8 count)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 while (count--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 *data++ = UEDATX;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 RXLED1; // light the RX LED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 RxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 static inline u8 Recv8()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 RXLED1; // light the RX LED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 RxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 return UEDATX;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 static inline void Send8(u8 d)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 UEDATX = d;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 static inline void SetEP(u8 ep)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 UENUM = ep;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 static inline u8 FifoByteCount()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 return UEBCLX;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 static inline u8 ReceivedSetupInt()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 return UEINTX & (1<<RXSTPI);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 static inline void ClearSetupInt()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 static inline void Stall()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 UECONX = (1<<STALLRQ) | (1<<EPEN);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 static inline u8 ReadWriteAllowed()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 return UEINTX & (1<<RWAL);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 static inline u8 Stalled()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 return UEINTX & (1<<STALLEDI);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 static inline u8 FifoFree()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 return UEINTX & (1<<FIFOCON);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 static inline void ReleaseRX()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 static inline void ReleaseTX()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 static inline u8 FrameNumber()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 return UDFNUML;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 //==================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 //==================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 u8 USBGetConfiguration(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 return _usbConfiguration;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 #define USB_RECV_TIMEOUT
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 class LockEP
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 u8 _sreg;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 public:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 LockEP(u8 ep) : _sreg(SREG)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 cli();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 SetEP(ep & 7);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 ~LockEP()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 SREG = _sreg;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 // Number of bytes, assumes a rx endpoint
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 u8 USB_Available(u8 ep)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 LockEP lock(ep);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 return FifoByteCount();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 // Non Blocking receive
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 // Return number of bytes read
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 int USB_Recv(u8 ep, void* d, int len)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 if (!_usbConfiguration || len < 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 LockEP lock(ep);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 u8 n = FifoByteCount();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 len = min(n,len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 n = len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 u8* dst = (u8*)d;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 while (n--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 *dst++ = Recv8();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 if (len && !FifoByteCount()) // release empty buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 ReleaseRX();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 return len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 // Recv 1 byte if ready
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 int USB_Recv(u8 ep)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 u8 c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 if (USB_Recv(ep,&c,1) != 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 // Space in send EP
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 u8 USB_SendSpace(u8 ep)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 LockEP lock(ep);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 if (!ReadWriteAllowed())
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 return 64 - FifoByteCount();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 // Blocking Send of data to an endpoint
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 int USB_Send(u8 ep, const void* d, int len)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 if (!_usbConfiguration)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 int r = len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 const u8* data = (const u8*)d;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270 u8 zero = ep & TRANSFER_ZERO;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271 u8 timeout = 250; // 250ms timeout on send? TODO
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 while (len)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 u8 n = USB_SendSpace(ep);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 if (n == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277 if (!(--timeout))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 delay(1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 continue;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283 if (n > len)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284 n = len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285 len -= n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287 LockEP lock(ep);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 if (ep & TRANSFER_ZERO)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290 while (n--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 Send8(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293 else if (ep & TRANSFER_PGM)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295 while (n--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296 Send8(pgm_read_byte(data++));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300 while (n--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 Send8(*data++);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303 if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 ReleaseTX();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 TXLED1; // light the TX LED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 TxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309 return r;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312 extern const u8 _initEndpoints[] PROGMEM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313 const u8 _initEndpoints[] =
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315 0,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317 #ifdef CDC_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319 EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320 EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 #ifdef HID_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324 EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
325 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
326 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
327
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
328 #define EP_SINGLE_64 0x32 // EP0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
329 #define EP_DOUBLE_64 0x36 // Other endpoints
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
330
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
331 static
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
332 void InitEP(u8 index, u8 type, u8 size)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
333 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
334 UENUM = index;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
335 UECONX = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
336 UECFG0X = type;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
337 UECFG1X = size;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
338 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
339
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
340 static
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
341 void InitEndpoints()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
342 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
343 for (u8 i = 1; i < sizeof(_initEndpoints); i++)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
344 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
345 UENUM = i;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
346 UECONX = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
347 UECFG0X = pgm_read_byte(_initEndpoints+i);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
348 UECFG1X = EP_DOUBLE_64;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
349 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
350 UERST = 0x7E; // And reset them
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
351 UERST = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
352 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
353
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
354 // Handle CLASS_INTERFACE requests
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
355 static
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
356 bool ClassInterfaceRequest(Setup& setup)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
357 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
358 u8 i = setup.wIndex;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
359
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
360 #ifdef CDC_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
361 if (CDC_ACM_INTERFACE == i)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
362 return CDC_Setup(setup);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
363 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
364
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
365 #ifdef HID_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
366 if (HID_INTERFACE == i)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
367 return HID_Setup(setup);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
368 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
369 return false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
370 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
371
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
372 int _cmark;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
373 int _cend;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
374 void InitControl(int end)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
375 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
376 SetEP(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
377 _cmark = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
378 _cend = end;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
379 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
380
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
381 static
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
382 bool SendControl(u8 d)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
383 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
384 if (_cmark < _cend)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
385 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
386 if (!WaitForINOrOUT())
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
387 return false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
388 Send8(d);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
389 if (!((_cmark + 1) & 0x3F))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
390 ClearIN(); // Fifo is full, release this packet
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
391 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
392 _cmark++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
393 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
394 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
395
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
396 // Clipped by _cmark/_cend
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
397 int USB_SendControl(u8 flags, const void* d, int len)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
398 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
399 int sent = len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
400 const u8* data = (const u8*)d;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
401 bool pgm = flags & TRANSFER_PGM;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
402 while (len--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
403 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
404 u8 c = pgm ? pgm_read_byte(data++) : *data++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
405 if (!SendControl(c))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
406 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
407 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
408 return sent;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
409 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
410
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
411 // Does not timeout or cross fifo boundaries
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
412 // Will only work for transfers <= 64 bytes
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
413 // TODO
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
414 int USB_RecvControl(void* d, int len)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
415 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
416 WaitOUT();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
417 Recv((u8*)d,len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
418 ClearOUT();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
419 return len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
420 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
421
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
422 int SendInterfaces()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
423 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
424 int total = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
425 u8 interfaces = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
426
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
427 #ifdef CDC_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
428 total = CDC_GetInterface(&interfaces);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
429 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
430
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
431 #ifdef HID_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
432 total += HID_GetInterface(&interfaces);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
433 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
434
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
435 return interfaces;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
436 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
437
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
438 // Construct a dynamic configuration descriptor
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
439 // This really needs dynamic endpoint allocation etc
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
440 // TODO
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
441 static
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
442 bool SendConfiguration(int maxlen)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
443 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
444 // Count and measure interfaces
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
445 InitControl(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
446 int interfaces = SendInterfaces();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
447 ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
448
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
449 // Now send them
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
450 InitControl(maxlen);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
451 USB_SendControl(0,&config,sizeof(ConfigDescriptor));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
452 SendInterfaces();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
453 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
454 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
455
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
456 u8 _cdcComposite = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
457
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
458 static
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
459 bool SendDescriptor(Setup& setup)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
460 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
461 u8 t = setup.wValueH;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
462 if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
463 return SendConfiguration(setup.wLength);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
464
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
465 InitControl(setup.wLength);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
466 #ifdef HID_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
467 if (HID_REPORT_DESCRIPTOR_TYPE == t)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
468 return HID_GetDescriptor(t);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
469 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
470
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
471 u8 desc_length = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
472 const u8* desc_addr = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
473 if (USB_DEVICE_DESCRIPTOR_TYPE == t)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
474 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
475 if (setup.wLength == 8)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
476 _cdcComposite = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
477 desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
478 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
479 else if (USB_STRING_DESCRIPTOR_TYPE == t)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
480 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
481 if (setup.wValueL == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
482 desc_addr = (const u8*)&STRING_LANGUAGE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
483 else if (setup.wValueL == IPRODUCT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
484 desc_addr = (const u8*)&STRING_IPRODUCT;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
485 else if (setup.wValueL == IMANUFACTURER)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
486 desc_addr = (const u8*)&STRING_IMANUFACTURER;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
487 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
488 return false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
489 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
490
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
491 if (desc_addr == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
492 return false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
493 if (desc_length == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
494 desc_length = pgm_read_byte(desc_addr);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
495
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
496 USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
497 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
498 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
499
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
500 // Endpoint 0 interrupt
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
501 ISR(USB_COM_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
502 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
503 SetEP(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
504 if (!ReceivedSetupInt())
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
505 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
506
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
507 Setup setup;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
508 Recv((u8*)&setup,8);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
509 ClearSetupInt();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
510
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
511 u8 requestType = setup.bmRequestType;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
512 if (requestType & REQUEST_DEVICETOHOST)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
513 WaitIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
514 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
515 ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
516
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
517 bool ok = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
518 if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
519 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
520 // Standard Requests
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
521 u8 r = setup.bRequest;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
522 if (GET_STATUS == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
523 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
524 Send8(0); // TODO
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
525 Send8(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
526 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
527 else if (CLEAR_FEATURE == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
528 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
529 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
530 else if (SET_FEATURE == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
531 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
532 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
533 else if (SET_ADDRESS == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
534 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
535 WaitIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
536 UDADDR = setup.wValueL | (1<<ADDEN);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
537 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
538 else if (GET_DESCRIPTOR == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
539 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
540 ok = SendDescriptor(setup);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
541 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
542 else if (SET_DESCRIPTOR == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
543 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
544 ok = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
545 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
546 else if (GET_CONFIGURATION == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
547 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
548 Send8(1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
549 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
550 else if (SET_CONFIGURATION == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
551 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
552 if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
553 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
554 InitEndpoints();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
555 _usbConfiguration = setup.wValueL;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
556 } else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
557 ok = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
558 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
559 else if (GET_INTERFACE == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
560 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
561 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
562 else if (SET_INTERFACE == r)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
563 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
564 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
565 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
566 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
567 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
568 InitControl(setup.wLength); // Max length of transfer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
569 ok = ClassInterfaceRequest(setup);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
570 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
571
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
572 if (ok)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
573 ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
574 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
575 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
576 Stall();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
577 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
578 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
579
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
580 void USB_Flush(u8 ep)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
581 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
582 SetEP(ep);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
583 if (FifoByteCount())
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
584 ReleaseTX();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
585 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
586
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
587 // General interrupt
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
588 ISR(USB_GEN_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
589 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
590 u8 udint = UDINT;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
591 UDINT = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
592
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
593 // End of Reset
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
594 if (udint & (1<<EORSTI))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
595 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
596 InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
597 _usbConfiguration = 0; // not configured yet
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
598 UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
599 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
600
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
601 // Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
602 if (udint & (1<<SOFI))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
603 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
604 #ifdef CDC_ENABLED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
605 USB_Flush(CDC_TX); // Send a tx frame if found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
606 while (USB_Available(CDC_RX)) // Handle received bytes (if any)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
607 Serial.accept();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
608 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
609
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
610 // check whether the one-shot period has elapsed. if so, turn off the LED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
611 if (TxLEDPulse && !(--TxLEDPulse))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
612 TXLED0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
613 if (RxLEDPulse && !(--RxLEDPulse))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
614 RXLED0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
615 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
616 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
617
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
618 // VBUS or counting frames
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
619 // Any frame counting?
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
620 u8 USBConnected()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
621 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
622 u8 f = UDFNUML;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
623 delay(3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
624 return f != UDFNUML;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
625 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
626
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
627 //=======================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
628 //=======================================================================
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
629
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
630 USBDevice_ USBDevice;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
631
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
632 USBDevice_::USBDevice_()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
633 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
634 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
635
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
636 void USBDevice_::attach()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
637 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
638 _usbConfiguration = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
639 UHWCON = 0x01; // power internal reg
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
640 USBCON = (1<<USBE)|(1<<FRZCLK); // clock frozen, usb enabled
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
641 PLLCSR = 0x12; // Need 16 MHz xtal
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
642 while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
643 ;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
644
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
645 // Some tests on specific versions of macosx (10.7.3), reported some
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
646 // strange behaviuors when the board is reset using the serial
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
647 // port touch at 1200 bps. This delay fixes this behaviour.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
648 delay(1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
649
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
650 USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
651 UDIEN = (1<<EORSTE)|(1<<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
652 UDCON = 0; // enable attach resistor
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
653
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
654 TX_RX_LED_INIT;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
655 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
656
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
657 void USBDevice_::detach()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
658 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
659 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
660
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
661 // Check for interrupts
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
662 // TODO: VBUS detection
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
663 bool USBDevice_::configured()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
664 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
665 return _usbConfiguration;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
666 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
667
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
668 void USBDevice_::poll()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
669 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
670 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
671
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
672 #endif /* if defined(USBCON) */

mercurial