|
@@ -22,7 +22,10 @@
|
22
|
22
|
* Web : https://www.circuitsathome.com
|
23
|
23
|
* e-mail : support@circuitsathome.com
|
24
|
24
|
*/
|
25
|
|
-/* USB functions */
|
|
25
|
+
|
|
26
|
+//
|
|
27
|
+// USB functions supporting Flash Drive
|
|
28
|
+//
|
26
|
29
|
|
27
|
30
|
#include "../../../inc/MarlinConfigPre.h"
|
28
|
31
|
|
|
@@ -35,7 +38,7 @@ static uint8_t usb_task_state;
|
35
|
38
|
|
36
|
39
|
/* constructor */
|
37
|
40
|
USB::USB() : bmHubPre(0) {
|
38
|
|
- usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; //set up state machine
|
|
41
|
+ usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // Set up state machine
|
39
|
42
|
init();
|
40
|
43
|
}
|
41
|
44
|
|
|
@@ -45,13 +48,8 @@ void USB::init() {
|
45
|
48
|
bmHubPre = 0;
|
46
|
49
|
}
|
47
|
50
|
|
48
|
|
-uint8_t USB::getUsbTaskState() {
|
49
|
|
- return usb_task_state;
|
50
|
|
-}
|
51
|
|
-
|
52
|
|
-void USB::setUsbTaskState(uint8_t state) {
|
53
|
|
- usb_task_state = state;
|
54
|
|
-}
|
|
51
|
+uint8_t USB::getUsbTaskState() { return usb_task_state; }
|
|
52
|
+void USB::setUsbTaskState(uint8_t state) { usb_task_state = state; }
|
55
|
53
|
|
56
|
54
|
EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) {
|
57
|
55
|
UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
|
|
@@ -70,9 +68,11 @@ EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) {
|
70
|
68
|
return nullptr;
|
71
|
69
|
}
|
72
|
70
|
|
73
|
|
-/* set device table entry */
|
74
|
|
-
|
75
|
|
-/* each device is different and has different number of endpoints. This function plugs endpoint record structure, defined in application, to devtable */
|
|
71
|
+/**
|
|
72
|
+ * Set device table entry
|
|
73
|
+ * Each device is different and has different number of endpoints.
|
|
74
|
+ * This function plugs endpoint record structure, defined in application, to devtable
|
|
75
|
+ */
|
76
|
76
|
uint8_t USB::setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr) {
|
77
|
77
|
if (!eprecord_ptr)
|
78
|
78
|
return USB_ERROR_INVALID_ARGUMENT;
|
|
@@ -112,7 +112,7 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_l
|
112
|
112
|
USBTRACE2(" NAK Limit: ", nak_limit);
|
113
|
113
|
USBTRACE("\r\n");
|
114
|
114
|
*/
|
115
|
|
- regWr(rPERADDR, addr); //set peripheral address
|
|
115
|
+ regWr(rPERADDR, addr); // Set peripheral address
|
116
|
116
|
|
117
|
117
|
uint8_t mode = regRd(rMODE);
|
118
|
118
|
|
|
@@ -121,8 +121,6 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_l
|
121
|
121
|
//Serial.print("\r\nLS: ");
|
122
|
122
|
//Serial.println(p->lowspeed, HEX);
|
123
|
123
|
|
124
|
|
-
|
125
|
|
-
|
126
|
124
|
// Set bmLOWSPEED and bmHUBPRE in case of low-speed device, reset them otherwise
|
127
|
125
|
regWr(rMODE, (p->lowspeed) ? mode | bmLOWSPEED | bmHubPre : mode & ~(bmHUBPRE | bmLOWSPEED));
|
128
|
126
|
|
|
@@ -133,11 +131,10 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_l
|
133
|
131
|
/* depending on request. Actual requests are defined as inlines */
|
134
|
132
|
/* return codes: */
|
135
|
133
|
/* 00 = success */
|
136
|
|
-
|
137
|
134
|
/* 01-0f = non-zero HRSLT */
|
138
|
135
|
uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
|
139
|
136
|
uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p) {
|
140
|
|
- bool direction = false; //request direction, IN or OUT
|
|
137
|
+ bool direction = false; // Request direction, IN or OUT
|
141
|
138
|
uint8_t rcode;
|
142
|
139
|
SETUP_PKT setup_pkt;
|
143
|
140
|
|
|
@@ -157,15 +154,15 @@ uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bReque
|
157
|
154
|
setup_pkt.wIndex = wInd;
|
158
|
155
|
setup_pkt.wLength = total;
|
159
|
156
|
|
160
|
|
- bytesWr(rSUDFIFO, 8, (uint8_t*) & setup_pkt); //transfer to setup packet FIFO
|
|
157
|
+ bytesWr(rSUDFIFO, 8, (uint8_t*) & setup_pkt); // Transfer to setup packet FIFO
|
161
|
158
|
|
162
|
|
- rcode = dispatchPkt(tokSETUP, ep, nak_limit); //dispatch packet
|
|
159
|
+ rcode = dispatchPkt(tokSETUP, ep, nak_limit); // Dispatch packet
|
163
|
160
|
if (rcode) return rcode; // Return HRSLT if not zero
|
164
|
161
|
|
165
|
|
- if (dataptr != nullptr) { //data stage, if present
|
166
|
|
- if (direction) { //IN transfer
|
|
162
|
+ if (dataptr) { // Data stage, if present
|
|
163
|
+ if (direction) { // IN transfer
|
167
|
164
|
uint16_t left = total;
|
168
|
|
- pep->bmRcvToggle = 1; //bmRCVTOG1;
|
|
165
|
+ pep->bmRcvToggle = 1; // BmRCVTOG1;
|
169
|
166
|
|
170
|
167
|
while (left) {
|
171
|
168
|
// Bytes read into buffer
|
|
@@ -174,7 +171,7 @@ uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bReque
|
174
|
171
|
|
175
|
172
|
rcode = InTransfer(pep, nak_limit, &read, dataptr);
|
176
|
173
|
if (rcode == hrTOGERR) {
|
177
|
|
- // yes, we flip it wrong here so that next time it is actually correct!
|
|
174
|
+ // Yes, we flip it wrong here so that next time it is actually correct!
|
178
|
175
|
pep->bmRcvToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
|
179
|
176
|
continue;
|
180
|
177
|
}
|
|
@@ -189,21 +186,21 @@ uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bReque
|
189
|
186
|
if (read < nbytes) break;
|
190
|
187
|
}
|
191
|
188
|
}
|
192
|
|
- else { //OUT transfer
|
193
|
|
- pep->bmSndToggle = 1; //bmSNDTOG1;
|
|
189
|
+ else { // OUT transfer
|
|
190
|
+ pep->bmSndToggle = 1; // BmSNDTOG1;
|
194
|
191
|
rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
|
195
|
192
|
}
|
196
|
|
- if (rcode) return rcode; // return error
|
|
193
|
+ if (rcode) return rcode; // Return error
|
197
|
194
|
}
|
198
|
195
|
// Status stage
|
199
|
|
- return dispatchPkt((direction) ? tokOUTHS : tokINHS, ep, nak_limit); //GET if direction
|
|
196
|
+ return dispatchPkt((direction) ? tokOUTHS : tokINHS, ep, nak_limit); // GET if direction
|
200
|
197
|
}
|
201
|
198
|
|
202
|
|
-/* IN transfer to arbitrary endpoint. Assumes PERADDR is set. Handles multiple packets if necessary. Transfers 'nbytes' bytes. */
|
203
|
|
-/* Keep sending INs and writes data to memory area pointed by 'data' */
|
204
|
|
-
|
205
|
|
-/* rcode 0 if no errors. rcode 01-0f is relayed from dispatchPkt(). Rcode f0 means RCVDAVIRQ error,
|
206
|
|
- fe USB xfer timeout */
|
|
199
|
+/**
|
|
200
|
+ * IN transfer to arbitrary endpoint. Assumes PERADDR is set. Handles multiple packets if necessary. Transfers 'nbytes' bytes.
|
|
201
|
+ * Keep sending INs and writes data to memory area pointed by 'data'
|
|
202
|
+ * rcode 0 if no errors. rcode 01-0f is relayed from dispatchPkt(). Rcode f0 means RCVDAVIRQ error, fe = USB xfer timeout
|
|
203
|
+ */
|
207
|
204
|
uint8_t USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval /*= 0*/) {
|
208
|
205
|
EpInfo *pep = nullptr;
|
209
|
206
|
uint16_t nak_limit = 0;
|
|
@@ -227,29 +224,29 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui
|
227
|
224
|
uint8_t maxpktsize = pep->maxPktSize;
|
228
|
225
|
|
229
|
226
|
*nbytesptr = 0;
|
230
|
|
- regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); //set toggle value
|
|
227
|
+ regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); // Set toggle value
|
231
|
228
|
|
232
|
|
- // use a 'break' to exit this loop
|
|
229
|
+ // Use a 'break' to exit this loop
|
233
|
230
|
for (;;) {
|
234
|
|
- rcode = dispatchPkt(tokIN, pep->epAddr, nak_limit); //IN packet to EP-'endpoint'. Function takes care of NAKS.
|
|
231
|
+ rcode = dispatchPkt(tokIN, pep->epAddr, nak_limit); // IN packet to EP-'endpoint'. Function takes care of NAKS.
|
235
|
232
|
if (rcode == hrTOGERR) {
|
236
|
|
- // yes, we flip it wrong here so that next time it is actually correct!
|
|
233
|
+ // Yes, we flip it wrong here so that next time it is actually correct!
|
237
|
234
|
pep->bmRcvToggle = (regRd(rHRSL) & bmRCVTOGRD) ? 0 : 1;
|
238
|
|
- regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); //set toggle value
|
|
235
|
+ regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); // Set toggle value
|
239
|
236
|
continue;
|
240
|
237
|
}
|
241
|
238
|
if (rcode) {
|
242
|
239
|
//printf(">>>>>>>> Problem! dispatchPkt %2.2x\r\n", rcode);
|
243
|
|
- break; //should be 0, indicating ACK. Else return error code.
|
|
240
|
+ break; // Should be 0, indicating ACK. Else return error code.
|
244
|
241
|
}
|
245
|
242
|
/* check for RCVDAVIRQ and generate error if not present */
|
246
|
243
|
/* the only case when absence of RCVDAVIRQ makes sense is when toggle error occurred. Need to add handling for that */
|
247
|
244
|
if ((regRd(rHIRQ) & bmRCVDAVIRQ) == 0) {
|
248
|
245
|
//printf(">>>>>>>> Problem! NO RCVDAVIRQ!\r\n");
|
249
|
|
- rcode = 0xF0; //receive error
|
|
246
|
+ rcode = 0xF0; // Receive error
|
250
|
247
|
break;
|
251
|
248
|
}
|
252
|
|
- pktsize = regRd(rRCVBC); //number of received bytes
|
|
249
|
+ pktsize = regRd(rRCVBC); // Number of received bytes
|
253
|
250
|
//printf("Got %i bytes \r\n", pktsize);
|
254
|
251
|
// This would be OK, but...
|
255
|
252
|
//assert(pktsize <= nbytes);
|
|
@@ -266,7 +263,7 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui
|
266
|
263
|
data = bytesRd(rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
|
267
|
264
|
|
268
|
265
|
regWr(rHIRQ, bmRCVDAVIRQ); // Clear the IRQ & free the buffer
|
269
|
|
- *nbytesptr += pktsize; // add this packet's byte count to total transfer length
|
|
266
|
+ *nbytesptr += pktsize; // Add this packet's byte count to total transfer length
|
270
|
267
|
|
271
|
268
|
/* The transfer is complete under two conditions: */
|
272
|
269
|
/* 1. The device sent a short packet (L.T. maxPacketSize) */
|
|
@@ -284,10 +281,11 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui
|
284
|
281
|
return rcode;
|
285
|
282
|
}
|
286
|
283
|
|
287
|
|
-/* OUT transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers 'nbytes' bytes. */
|
288
|
|
-/* Handles NAK bug per Maxim Application Note 4000 for single buffer transfer */
|
289
|
|
-
|
290
|
|
-/* rcode 0 if no errors. rcode 01-0f is relayed from HRSL */
|
|
284
|
+/**
|
|
285
|
+ * OUT transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers 'nbytes' bytes.
|
|
286
|
+ * Handles NAK bug per Maxim Application Note 4000 for single buffer transfer
|
|
287
|
+ * rcode 0 if no errors. rcode 01-0f is relayed from HRSL
|
|
288
|
+ */
|
291
|
289
|
uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data) {
|
292
|
290
|
EpInfo *pep = nullptr;
|
293
|
291
|
uint16_t nak_limit = 0;
|
|
@@ -300,7 +298,7 @@ uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dat
|
300
|
298
|
|
301
|
299
|
uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
|
302
|
300
|
uint8_t rcode = hrSUCCESS, retry_count;
|
303
|
|
- uint8_t *data_p = data; //local copy of the data pointer
|
|
301
|
+ uint8_t *data_p = data; // Local copy of the data pointer
|
304
|
302
|
uint16_t bytes_tosend, nak_count;
|
305
|
303
|
uint16_t bytes_left = nbytes;
|
306
|
304
|
|
|
@@ -311,17 +309,17 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
|
311
|
309
|
|
312
|
310
|
uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT;
|
313
|
311
|
|
314
|
|
- regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value
|
|
312
|
+ regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); // Set toggle value
|
315
|
313
|
|
316
|
314
|
while (bytes_left) {
|
317
|
315
|
retry_count = 0;
|
318
|
316
|
nak_count = 0;
|
319
|
317
|
bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
|
320
|
|
- bytesWr(rSNDFIFO, bytes_tosend, data_p); //filling output FIFO
|
321
|
|
- regWr(rSNDBC, bytes_tosend); //set number of bytes
|
322
|
|
- regWr(rHXFR, (tokOUT | pep->epAddr)); //dispatch packet
|
323
|
|
- while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ
|
324
|
|
- regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
|
|
318
|
+ bytesWr(rSNDFIFO, bytes_tosend, data_p); // Filling output FIFO
|
|
319
|
+ regWr(rSNDBC, bytes_tosend); // Set number of bytes
|
|
320
|
+ regWr(rHXFR, (tokOUT | pep->epAddr)); // Dispatch packet
|
|
321
|
+ while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); // Wait for the completion IRQ
|
|
322
|
+ regWr(rHIRQ, bmHXFRDNIRQ); // Clear IRQ
|
325
|
323
|
rcode = (regRd(rHRSL) & 0x0F);
|
326
|
324
|
|
327
|
325
|
while (rcode && ((int32_t)((uint32_t)millis() - timeout) < 0L)) {
|
|
@@ -330,18 +328,18 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
|
330
|
328
|
nak_count++;
|
331
|
329
|
if (nak_limit && (nak_count == nak_limit))
|
332
|
330
|
goto breakout;
|
333
|
|
- //return ( rcode);
|
|
331
|
+ //return rcode;
|
334
|
332
|
break;
|
335
|
333
|
case hrTIMEOUT:
|
336
|
334
|
retry_count++;
|
337
|
335
|
if (retry_count == USB_RETRY_LIMIT)
|
338
|
336
|
goto breakout;
|
339
|
|
- //return ( rcode);
|
|
337
|
+ //return rcode;
|
340
|
338
|
break;
|
341
|
339
|
case hrTOGERR:
|
342
|
|
- // yes, we flip it wrong here so that next time it is actually correct!
|
|
340
|
+ // Yes, we flip it wrong here so that next time it is actually correct!
|
343
|
341
|
pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
|
344
|
|
- regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value
|
|
342
|
+ regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); // Set toggle value
|
345
|
343
|
break;
|
346
|
344
|
default:
|
347
|
345
|
goto breakout;
|
|
@@ -351,26 +349,27 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
|
351
|
349
|
regWr(rSNDBC, 0);
|
352
|
350
|
regWr(rSNDFIFO, *data_p);
|
353
|
351
|
regWr(rSNDBC, bytes_tosend);
|
354
|
|
- regWr(rHXFR, (tokOUT | pep->epAddr)); //dispatch packet
|
355
|
|
- while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ
|
356
|
|
- regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
|
|
352
|
+ regWr(rHXFR, (tokOUT | pep->epAddr)); // Dispatch packet
|
|
353
|
+ while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); // Wait for the completion IRQ
|
|
354
|
+ regWr(rHIRQ, bmHXFRDNIRQ); // Clear IRQ
|
357
|
355
|
rcode = (regRd(rHRSL) & 0x0F);
|
358
|
|
- } // while rcode && ....
|
|
356
|
+ } // While rcode && ....
|
359
|
357
|
bytes_left -= bytes_tosend;
|
360
|
358
|
data_p += bytes_tosend;
|
361
|
|
- } // while bytes_left...
|
|
359
|
+ } // While bytes_left...
|
362
|
360
|
breakout:
|
363
|
361
|
|
364
|
|
- pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 1 : 0; //bmSNDTOG1 : bmSNDTOG0; //update toggle
|
365
|
|
- return ( rcode); //should be 0 in all cases
|
|
362
|
+ pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 1 : 0; // BmSNDTOG1 : bmSNDTOG0; // Update toggle
|
|
363
|
+ return ( rcode); // Should be 0 in all cases
|
366
|
364
|
}
|
367
|
365
|
|
368
|
|
-/* dispatch USB packet. Assumes peripheral address is set and relevant buffer is loaded/empty */
|
369
|
|
-/* If NAK, tries to re-send up to nak_limit times */
|
370
|
|
-/* If nak_limit == 0, do not count NAKs, exit after timeout */
|
371
|
|
-/* If bus timeout, re-sends up to USB_RETRY_LIMIT times */
|
372
|
|
-
|
373
|
|
-/* return codes 0x00-0x0F are HRSLT( 0x00 being success ), 0xFF means timeout */
|
|
366
|
+/**
|
|
367
|
+ * Dispatch USB packet. Assumes peripheral address is set and relevant buffer is loaded/empty
|
|
368
|
+ * If NAK, tries to re-send up to nak_limit times
|
|
369
|
+ * If nak_limit == 0, do not count NAKs, exit after timeout
|
|
370
|
+ * If bus timeout, re-sends up to USB_RETRY_LIMIT times
|
|
371
|
+ * return codes 0x00-0x0F are HRSLT( 0x00 being success ), 0xFF means timeout
|
|
372
|
+ */
|
374
|
373
|
uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
|
375
|
374
|
uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT;
|
376
|
375
|
uint8_t tmpdata;
|
|
@@ -380,29 +379,28 @@ uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
|
380
|
379
|
|
381
|
380
|
while ((int32_t)((uint32_t)millis() - timeout) < 0L) {
|
382
|
381
|
#if defined(ESP8266) || defined(ESP32)
|
383
|
|
- yield(); // needed in order to reset the watchdog timer on the ESP8266
|
|
382
|
+ yield(); // Needed in order to reset the watchdog timer on the ESP8266
|
384
|
383
|
#endif
|
385
|
|
- regWr(rHXFR, (token | ep)); //launch the transfer
|
|
384
|
+ regWr(rHXFR, (token | ep)); // Launch the transfer
|
386
|
385
|
rcode = USB_ERROR_TRANSFER_TIMEOUT;
|
387
|
386
|
|
388
|
|
- while ((int32_t)((uint32_t)millis() - timeout) < 0L) { //wait for transfer completion
|
|
387
|
+ while ((int32_t)((uint32_t)millis() - timeout) < 0L) { // Wait for transfer completion
|
389
|
388
|
#if defined(ESP8266) || defined(ESP32)
|
390
|
|
- yield(); // needed to reset the watchdog timer on the ESP8266
|
|
389
|
+ yield(); // Needed to reset the watchdog timer on the ESP8266
|
391
|
390
|
#endif
|
392
|
391
|
tmpdata = regRd(rHIRQ);
|
393
|
392
|
|
394
|
393
|
if (tmpdata & bmHXFRDNIRQ) {
|
395
|
|
- regWr(rHIRQ, bmHXFRDNIRQ); //clear the interrupt
|
|
394
|
+ regWr(rHIRQ, bmHXFRDNIRQ); // Clear the interrupt
|
396
|
395
|
rcode = 0x00;
|
397
|
396
|
break;
|
398
|
397
|
}
|
399
|
398
|
|
400
|
|
- } // while millis() < timeout
|
|
399
|
+ } // While millis() < timeout
|
401
|
400
|
|
402
|
|
- //if (rcode != 0x00) //exit if timeout
|
403
|
|
- // return ( rcode);
|
|
401
|
+ //if (rcode != 0x00) return rcode; // Exit if timeout
|
404
|
402
|
|
405
|
|
- rcode = (regRd(rHRSL) & 0x0F); //analyze transfer result
|
|
403
|
+ rcode = (regRd(rHRSL) & 0x0F); // Analyze transfer result
|
406
|
404
|
|
407
|
405
|
switch (rcode) {
|
408
|
406
|
case hrNAK:
|
|
@@ -419,12 +417,12 @@ uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
|
419
|
417
|
return (rcode);
|
420
|
418
|
}
|
421
|
419
|
|
422
|
|
- } // while timeout > millis()
|
|
420
|
+ } // While timeout > millis()
|
423
|
421
|
return rcode;
|
424
|
422
|
}
|
425
|
423
|
|
426
|
|
-/* USB main task. Performs enumeration/cleanup */
|
427
|
|
-void USB::Task() { //USB state machine
|
|
424
|
+// USB main task. Performs enumeration/cleanup
|
|
425
|
+void USB::Task() { // USB state machine
|
428
|
426
|
uint8_t rcode;
|
429
|
427
|
uint8_t tmpdata;
|
430
|
428
|
static uint32_t delay = 0;
|
|
@@ -437,19 +435,19 @@ void USB::Task() { //USB state machine
|
437
|
435
|
|
438
|
436
|
/* modify USB task state if Vbus changed */
|
439
|
437
|
switch (tmpdata) {
|
440
|
|
- case SE1: //illegal state
|
|
438
|
+ case SE1: // Illegal state
|
441
|
439
|
usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
|
442
|
440
|
lowspeed = false;
|
443
|
441
|
break;
|
444
|
|
- case SE0: //disconnected
|
|
442
|
+ case SE0: // Disconnected
|
445
|
443
|
if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED)
|
446
|
444
|
usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
|
447
|
445
|
lowspeed = false;
|
448
|
446
|
break;
|
449
|
447
|
case LSHOST:
|
450
|
448
|
lowspeed = true;
|
451
|
|
- //intentional fallthrough
|
452
|
|
- case FSHOST: //attached
|
|
449
|
+ // Intentional fallthrough
|
|
450
|
+ case FSHOST: // Attached
|
453
|
451
|
if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) {
|
454
|
452
|
delay = (uint32_t)millis() + USB_SETTLE_DELAY;
|
455
|
453
|
usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
|
|
@@ -470,31 +468,31 @@ void USB::Task() { //USB state machine
|
470
|
468
|
|
471
|
469
|
usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
|
472
|
470
|
break;
|
473
|
|
- case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: //just sit here
|
|
471
|
+ case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: // Just sit here
|
474
|
472
|
break;
|
475
|
|
- case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here
|
|
473
|
+ case USB_DETACHED_SUBSTATE_ILLEGAL: // Just sit here
|
476
|
474
|
break;
|
477
|
|
- case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device
|
|
475
|
+ case USB_ATTACHED_SUBSTATE_SETTLE: // Settle time for just attached device
|
478
|
476
|
if ((int32_t)((uint32_t)millis() - delay) >= 0L)
|
479
|
477
|
usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
|
480
|
|
- else break; // don't fall through
|
|
478
|
+ else break; // Don't fall through
|
481
|
479
|
case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
|
482
|
|
- regWr(rHCTL, bmBUSRST); //issue bus reset
|
|
480
|
+ regWr(rHCTL, bmBUSRST); // Issue bus reset
|
483
|
481
|
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE;
|
484
|
482
|
break;
|
485
|
483
|
case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
|
486
|
484
|
if ((regRd(rHCTL) & bmBUSRST) == 0) {
|
487
|
|
- tmpdata = regRd(rMODE) | bmSOFKAENAB; //start SOF generation
|
|
485
|
+ tmpdata = regRd(rMODE) | bmSOFKAENAB; // Start SOF generation
|
488
|
486
|
regWr(rMODE, tmpdata);
|
489
|
487
|
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
|
490
|
|
- //delay = (uint32_t)millis() + 20; //20ms wait after reset per USB spec
|
|
488
|
+ //delay = (uint32_t)millis() + 20; // 20ms wait after reset per USB spec
|
491
|
489
|
}
|
492
|
490
|
break;
|
493
|
|
- case USB_ATTACHED_SUBSTATE_WAIT_SOF: //todo: change check order
|
|
491
|
+ case USB_ATTACHED_SUBSTATE_WAIT_SOF: // Todo: change check order
|
494
|
492
|
if (regRd(rHIRQ) & bmFRAMEIRQ) {
|
495
|
|
- //when first SOF received _and_ 20ms has passed we can continue
|
|
493
|
+ // When first SOF received _and_ 20ms has passed we can continue
|
496
|
494
|
/*
|
497
|
|
- if (delay < (uint32_t)millis()) //20ms passed
|
|
495
|
+ if (delay < (uint32_t)millis()) // 20ms passed
|
498
|
496
|
usb_task_state = USB_STATE_CONFIGURING;
|
499
|
497
|
*/
|
500
|
498
|
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET;
|
|
@@ -503,7 +501,7 @@ void USB::Task() { //USB state machine
|
503
|
501
|
break;
|
504
|
502
|
case USB_ATTACHED_SUBSTATE_WAIT_RESET:
|
505
|
503
|
if ((int32_t)((uint32_t)millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
|
506
|
|
- else break; // don't fall through
|
|
504
|
+ else break; // Don't fall through
|
507
|
505
|
case USB_STATE_CONFIGURING:
|
508
|
506
|
|
509
|
507
|
//Serial.print("\r\nConf.LS: ");
|
|
@@ -565,11 +563,11 @@ again:
|
565
|
563
|
if (rcode == USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET) {
|
566
|
564
|
if (parent == 0) {
|
567
|
565
|
// Send a bus reset on the root interface.
|
568
|
|
- regWr(rHCTL, bmBUSRST); //issue bus reset
|
569
|
|
- delay(102); // delay 102ms, compensate for clock inaccuracy.
|
|
566
|
+ regWr(rHCTL, bmBUSRST); // Issue bus reset
|
|
567
|
+ delay(102); // Delay 102ms, compensate for clock inaccuracy.
|
570
|
568
|
}
|
571
|
569
|
else {
|
572
|
|
- // reset parent port
|
|
570
|
+ // Reset parent port
|
573
|
571
|
devConfig[parent]->ResetHubPort(port);
|
574
|
572
|
}
|
575
|
573
|
}
|
|
@@ -592,11 +590,11 @@ again:
|
592
|
590
|
// Issue a bus reset, because the device may be in a limbo state
|
593
|
591
|
if (parent == 0) {
|
594
|
592
|
// Send a bus reset on the root interface.
|
595
|
|
- regWr(rHCTL, bmBUSRST); //issue bus reset
|
596
|
|
- delay(102); // delay 102ms, compensate for clock inaccuracy.
|
|
593
|
+ regWr(rHCTL, bmBUSRST); // Issue bus reset
|
|
594
|
+ delay(102); // Delay 102ms, compensate for clock inaccuracy.
|
597
|
595
|
}
|
598
|
596
|
else {
|
599
|
|
- // reset parent port
|
|
597
|
+ // Reset parent port
|
600
|
598
|
devConfig[parent]->ResetHubPort(port);
|
601
|
599
|
}
|
602
|
600
|
}
|
|
@@ -623,19 +621,19 @@ again:
|
623
|
621
|
* 4: set address
|
624
|
622
|
* 5: pUsb->setEpInfoEntry(bAddress, 1, epInfo), exit on fail
|
625
|
623
|
* 6: while (configurations) {
|
626
|
|
- * for (each configuration) {
|
627
|
|
- * for (each driver) {
|
628
|
|
- * 6a: Ask device if it likes configuration. Returns 0 on OK.
|
629
|
|
- * If successful, the driver configured device.
|
630
|
|
- * The driver now owns the endpoints, and takes over managing them.
|
631
|
|
- * The following will need codes:
|
632
|
|
- * Everything went well, instance consumed, exit with success.
|
633
|
|
- * Instance already in use, ignore it, try next driver.
|
634
|
|
- * Not a supported device, ignore it, try next driver.
|
635
|
|
- * Not a supported configuration for this device, ignore it, try next driver.
|
636
|
|
- * Could not configure device, fatal, exit with fail.
|
637
|
|
- * }
|
638
|
|
- * }
|
|
624
|
+ * for (each configuration) {
|
|
625
|
+ * for (each driver) {
|
|
626
|
+ * 6a: Ask device if it likes configuration. Returns 0 on OK.
|
|
627
|
+ * If successful, the driver configured device.
|
|
628
|
+ * The driver now owns the endpoints, and takes over managing them.
|
|
629
|
+ * The following will need codes:
|
|
630
|
+ * Everything went well, instance consumed, exit with success.
|
|
631
|
+ * Instance already in use, ignore it, try next driver.
|
|
632
|
+ * Not a supported device, ignore it, try next driver.
|
|
633
|
+ * Not a supported configuration for this device, ignore it, try next driver.
|
|
634
|
+ * Could not configure device, fatal, exit with fail.
|
|
635
|
+ * }
|
|
636
|
+ * }
|
639
|
637
|
* }
|
640
|
638
|
* 7: for (each driver) {
|
641
|
639
|
* 7a: Ask device if it knows this VID/PID. Acts exactly like 6a, but using VID/PID
|
|
@@ -671,7 +669,7 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
|
671
|
669
|
oldep_ptr = p->epinfo;
|
672
|
670
|
|
673
|
671
|
// Temporary assign new pointer to epInfo to p->epinfo in order to
|
674
|
|
- // avoid toggle inconsistence
|
|
672
|
+ // Avoid toggle inconsistence
|
675
|
673
|
|
676
|
674
|
p->epinfo = &epInfo;
|
677
|
675
|
|
|
@@ -687,7 +685,7 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
|
687
|
685
|
return rcode;
|
688
|
686
|
}
|
689
|
687
|
|
690
|
|
- // to-do?
|
|
688
|
+ // To-do?
|
691
|
689
|
// Allocate new address according to device class
|
692
|
690
|
//bAddress = addrPool.AllocAddress(parent, false, port);
|
693
|
691
|
|
|
@@ -698,11 +696,11 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
|
698
|
696
|
// Qualify with subclass too.
|
699
|
697
|
//
|
700
|
698
|
// VID/PID & class tests default to false for drivers not yet ported
|
701
|
|
- // subclass defaults to true, so you don't have to define it if you don't have to.
|
|
699
|
+ // Subclass defaults to true, so you don't have to define it if you don't have to.
|
702
|
700
|
//
|
703
|
701
|
for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
|
704
|
|
- if (!devConfig[devConfigIndex]) continue; // no driver
|
705
|
|
- if (devConfig[devConfigIndex]->GetAddress()) continue; // consumed
|
|
702
|
+ if (!devConfig[devConfigIndex]) continue; // No driver
|
|
703
|
+ if (devConfig[devConfigIndex]->GetAddress()) continue; // Consumed
|
706
|
704
|
if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) {
|
707
|
705
|
rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
|
708
|
706
|
if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED)
|
|
@@ -712,20 +710,20 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
|
712
|
710
|
|
713
|
711
|
if (devConfigIndex < USB_NUMDEVICES) return rcode;
|
714
|
712
|
|
715
|
|
- // blindly attempt to configure
|
|
713
|
+ // Blindly attempt to configure
|
716
|
714
|
for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
|
717
|
715
|
if (!devConfig[devConfigIndex]) continue;
|
718
|
|
- if (devConfig[devConfigIndex]->GetAddress()) continue; // consumed
|
|
716
|
+ if (devConfig[devConfigIndex]->GetAddress()) continue; // Consumed
|
719
|
717
|
if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) continue; // If this is true it means it must have returned USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED above
|
720
|
718
|
rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
|
721
|
719
|
|
722
|
720
|
//printf("ERROR ENUMERATING %2.2x\r\n", rcode);
|
723
|
721
|
if (!(rcode == USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED || rcode == USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE)) {
|
724
|
|
- // in case of an error dev_index should be reset to 0
|
725
|
|
- // in order to start from the very beginning the
|
726
|
|
- // next time the program gets here
|
|
722
|
+ // In case of an error dev_index should be reset to 0
|
|
723
|
+ // in order to start from the very beginning the
|
|
724
|
+ // next time the program gets here
|
727
|
725
|
//if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
|
728
|
|
- // devConfigIndex = 0;
|
|
726
|
+ //devConfigIndex = 0;
|
729
|
727
|
return rcode;
|
730
|
728
|
}
|
731
|
729
|
}
|
|
@@ -744,20 +742,22 @@ uint8_t USB::ReleaseDevice(uint8_t addr) {
|
744
|
742
|
return 0;
|
745
|
743
|
}
|
746
|
744
|
|
747
|
|
-#if 1 //!defined(USB_METHODS_INLINE)
|
748
|
|
-//get device descriptor
|
749
|
|
-
|
|
745
|
+// Get device descriptor
|
750
|
746
|
uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) {
|
751
|
747
|
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, nullptr);
|
752
|
748
|
}
|
753
|
|
-//get configuration descriptor
|
754
|
749
|
|
|
750
|
+// Get configuration descriptor
|
755
|
751
|
uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
|
756
|
752
|
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, nullptr);
|
757
|
753
|
}
|
758
|
754
|
|
759
|
|
-/* Requests Configuration Descriptor. Sends two Get Conf Descr requests. The first one gets the total length of all descriptors, then the second one requests this
|
760
|
|
- total length. The length of the first request can be shorter ( 4 bytes ), however, there are devices which won't work unless this length is set to 9 */
|
|
755
|
+/**
|
|
756
|
+ * Requests Configuration Descriptor. Sends two Get Conf Descr requests.
|
|
757
|
+ * The first one gets the total length of all descriptors, then the second one requests this
|
|
758
|
+ * total length. The length of the first request can be shorter (4 bytes), however, there are
|
|
759
|
+ * devices which won't work unless this length is set to 9.
|
|
760
|
+ */
|
761
|
761
|
uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p) {
|
762
|
762
|
const uint8_t bufSize = 64;
|
763
|
763
|
uint8_t buf[bufSize];
|
|
@@ -773,25 +773,23 @@ uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser
|
773
|
773
|
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p);
|
774
|
774
|
}
|
775
|
775
|
|
776
|
|
-//get string descriptor
|
777
|
|
-
|
|
776
|
+// Get string descriptor
|
778
|
777
|
uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
|
779
|
778
|
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, nullptr);
|
780
|
779
|
}
|
781
|
|
-//set address
|
782
|
780
|
|
|
781
|
+// Set address
|
783
|
782
|
uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) {
|
784
|
783
|
uint8_t rcode = ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
|
785
|
|
- //delay(2); //per USB 2.0 sect.9.2.6.3
|
|
784
|
+ //delay(2); // Per USB 2.0 sect.9.2.6.3
|
786
|
785
|
delay(300); // Older spec says you should wait at least 200ms
|
787
|
786
|
return rcode;
|
788
|
787
|
//return ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
|
789
|
788
|
}
|
790
|
|
-//set configuration
|
791
|
789
|
|
|
790
|
+// Set configuration
|
792
|
791
|
uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) {
|
793
|
792
|
return ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
|
794
|
793
|
}
|
795
|
794
|
|
796
|
|
-#endif // defined(USB_METHODS_INLINE)
|
797
|
795
|
#endif // USB_FLASH_DRIVE_SUPPORT
|