Skip to content

Commit ed04b05

Browse files
committed
WIP - USB requests span multiple packets
arduino-libraries#33 Mostly the code from this issue
1 parent 93aab88 commit ed04b05

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

src/USBHost/USBDeviceConnected.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ INTERFACE * USBDeviceConnected::getInterface(uint8_t index) {
5858
}
5959

6060
bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) {
61+
USB_DBG("addInterface(%u %u %u %u) %p", intf_nb, intf_class, intf_subclass, intf_protocol, this);
6162
if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use)) {
6263
return false;
6364
}
@@ -70,11 +71,13 @@ bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8
7071
}
7172

7273
bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) {
74+
USB_DBG("addEndpoint(%u %p) %p %u %u", intf_nb, ept, this, intf[intf_nb].in_use, intf[intf_nb].nb_endpoint);
7375
if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use == false) || (intf[intf_nb].nb_endpoint >= MAX_ENDPOINT_PER_INTERFACE)) {
7476
return false;
7577
}
7678
intf[intf_nb].nb_endpoint++;
7779

80+
7881
for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
7982
if (intf[intf_nb].ep[i] == NULL) {
8083
intf[intf_nb].ep[i] = ept;
@@ -101,10 +104,19 @@ void USBDeviceConnected::disconnect() {
101104

102105

103106
USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) {
107+
USB_DBG("getEndpoint(%u %u %u %u) %p %u %u", intf_nb, type, dir, index, this, intf[intf_nb].in_use, intf[intf_nb].nb_endpoint);
104108
if (intf_nb >= MAX_INTF) {
105109
return NULL;
106110
}
111+
#if (DEBUG > 3)
112+
for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
113+
USB_DBG("index: %d %p", i, intf[intf_nb].ep[i]);
114+
}
115+
#endif
116+
107117
for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
118+
USB_DBG("index: %d %p", i, intf[intf_nb].ep[i]);
119+
if ( intf[intf_nb].ep[i] == nullptr) continue; // don't walk through NULL
108120
if ((intf[intf_nb].ep[i]->getType() == type) && (intf[intf_nb].ep[i]->getDir() == dir)) {
109121
if(index) {
110122
index--;

src/USBHost/USBHost.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ USB_TYPE USBHost::resetDevice(USBDeviceConnected * dev)
644644
bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep)
645645
{
646646

647+
USB_DBG("USBHost::addEndpoint(%p %u %p) %p", dev, intf_nb, ep, this);
647648
if (ep == NULL) {
648649
return false;
649650
}
@@ -877,11 +878,15 @@ USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t *
877878

878879
USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]);
879880

880-
return controlRead( dev,
881+
res = controlRead( dev,
881882
USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
882883
GET_DESCRIPTOR,
883884
(CONFIGURATION_DESCRIPTOR << 8) | (0),
884885
0, buf, total_conf_descr_length);
886+
if (res != USB_TYPE_OK) {
887+
USB_ERR("controlRead FAILED(%u)", res);
888+
return res;
889+
}
885890
}
886891

887892

@@ -931,6 +936,7 @@ USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerato
931936
// don't enumerate a device which all interfaces are registered to a specific driver
932937
int index = findDevice(dev);
933938

939+
USB_DBG("Enumerate dev: %p index: %d", dev, index);
934940
if (index == -1) {
935941
return USB_TYPE_ERROR;
936942
}
@@ -946,12 +952,20 @@ USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerato
946952
USB_DBG("Enumerate dev: %p", dev);
947953

948954
// third step: get the whole device descriptor to see vid, pid
949-
res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
955+
uint16_t cb_read;
956+
res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH, &cb_read);
950957

951958
if (res != USB_TYPE_OK) {
952959
USB_DBG("GET DEV DESCR FAILED");
953960
return res;
954961
}
962+
#if (DEBUG > 3)
963+
USB_DBG("DEVICE DESCRIPTOR(%u):\r\n", cb_read);
964+
for (int i = 0; i < DEVICE_DESCRIPTOR_LENGTH; i++) {
965+
printf("%02X ", data[i]);
966+
}
967+
printf("\r\n\r\n");
968+
#endif
955969

956970
dev->setClass(data[4]);
957971
dev->setSubClass(data[5]);

src/USBHost/USBHostConf.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#ifndef USBHOST_CONF_H
1818
#define USBHOST_CONF_H
1919

20-
#undef MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL
21-
#define MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL 0
22-
#include "Callback.h"
20+
//#undef MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL
21+
//#define MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL 0
22+
//#include "Callback.h"
2323
#include "Arduino.h"
2424

2525
#if defined(TARGET_STM)

src/USBHost/dbg.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define USB_DEBUG_H
1919

2020
//Debug is disabled by default
21-
#define DEBUG 0 /*INFO,ERR,WARN*/
21+
#define DEBUG 2 /*INFO,ERR,WARN*/
2222
#define DEBUG_TRANSFER 0
2323
#define DEBUG_EP_STATE 0
2424
#define DEBUG_EVENT 0

src/targets/TARGET_STM/USBEndpoint_STM.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ USB_TYPE USBEndpoint::queueTransfer()
178178
}
179179
ep_queue.get(0);
180180
MBED_ASSERT(*addr == 0);
181+
#ifdef USE_RELEASED_CODE
181182
transfer_len = td_current->size <= max_size ? td_current->size : max_size;
183+
#else
184+
transfer_len = td_current->size;
185+
#endif
182186
buf_start = (uint8_t *)td_current->currBufPtr;
183187

184188
//Now add this free TD at this end of the queue
@@ -193,6 +197,9 @@ USB_TYPE USBEndpoint::queueTransfer()
193197
/* dir /setup is inverted for ST */
194198
/* token is useful only ctrl endpoint */
195199
/* last parameter is ping ? */
200+
USB_DBG_TRANSFER("HAL_HCD_HC_SubmitRequest(%p, %u, %u, %u, %u, %p, %u, %u)\n\r",
201+
(HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, dir - 1, type, !setup, (uint8_t *) td_current->currBufPtr, transfer_len, 1);
202+
196203
HAL_HCD_HC_SubmitRequest((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, dir - 1, type, !setup, (uint8_t *) td_current->currBufPtr, transfer_len, 1);
197204
HAL_HCD_EnableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
198205

src/targets/TARGET_STM/USBHALHost_STM.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
104104
uint32_t length;
105105
if ((addr != 0)) {
106106
HCTD *td = (HCTD *)addr;
107-
107+
#ifdef USE_RELEASED_CODE
108108
if ((type == EP_TYPE_BULK) || (type == EP_TYPE_CTRL)) {
109109
switch (urb_state) {
110110
case URB_DONE:
@@ -139,6 +139,8 @@ void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
139139
break;
140140
}
141141
}
142+
#endif // USE_RELEASED_CODE
143+
142144
if ((type == EP_TYPE_INTR)) {
143145
/* reply a packet of length NULL, this will be analyze in call back
144146
* for mouse or hub */

0 commit comments

Comments
 (0)