Skip to content

Commit 57c06ca

Browse files
committed
Test with 1ms timer controlling USB/TransferDescriptor logic
1 parent 7100f26 commit 57c06ca

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

src/USBHost/USBHALHost.h

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class USBHALHost {
3434
*/
3535
USBHALHost();
3636

37+
#if ARC_TICKER_BASED
38+
mbed::Ticker ms_ticker;
39+
40+
void tickerCallback(void);
41+
#endif
42+
3743
/**
3844
* Initialize host controller. Enable USB interrupts. This part is not in the constructor because,
3945
* this function calls a virtual method if a device is already connected

src/USBHost/USBHost.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ void USBHost::usb_process()
180180
res = getDeviceDescriptor(&devices[i], buf, 8);
181181

182182
if (res == USB_TYPE_OK) {
183+
// Call the device connected callback if registered
184+
if (nullptr != mount_fnc) {
185+
mount_fnc();
186+
}
187+
183188
break;
184189
}
185190

@@ -188,10 +193,6 @@ void USBHost::usb_process()
188193

189194
USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
190195

191-
// Call the device connected callback if registered
192-
if (nullptr != mount_fnc) {
193-
mount_fnc();
194-
}
195196

196197
#if MAX_HUB_NB
197198
if (buf[4] == HUB_CLASS) {

src/targets/TARGET_STM/USBHALHost_STM.cpp

+88-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,93 @@ uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chnum)
9191
// - URB_NOTREADY = a NAK, NYET, or not more than a couple of repeats of some of the errors that will
9292
// become URB_ERROR if they repeat several times in a row
9393
//
94+
#if ARC_TICKER_BASED
95+
void USBHALHost::tickerCallback(void)
96+
{
97+
HCD_HandleTypeDef *pHcd = (HCD_HandleTypeDef *)usb_hcca;
98+
if(pHcd->State != HAL_HCD_STATE_BUSY)
99+
{
100+
USBHALHost_Private_t *pPriv = (USBHALHost_Private_t *)(pHcd->pData);
101+
102+
// 10 host channels
103+
for(uint8_t uChannel=0; uChannel <11; uChannel++)
104+
{
105+
USB_OTG_URBStateTypeDef urbState = pHcd->hc[uChannel].urb_state;
106+
107+
HCTD *pTransferDescriptor = (pPriv->addr[uChannel] == 0xffffffff) ? nullptr : (HCTD *)pPriv->addr[uChannel];
108+
uint32_t uEndpointType = pHcd->hc[uChannel].ep_type;
109+
uint32_t uEndpointDirection = pHcd->hc[uChannel].ep_is_in;;
110+
111+
if(pTransferDescriptor)
112+
{
113+
if (uEndpointType == EP_TYPE_INTR)
114+
{
115+
// Disable the channel interupt
116+
pTransferDescriptor->state = USB_TYPE_IDLE;
117+
HAL_HCD_DisableInt(pHcd, uChannel);
118+
}
119+
else
120+
{
121+
// Handle USB NAKs
122+
if(urbState == URB_NOTREADY)
123+
{
124+
// retry Acks imediately
125+
// retry Bulk and Control after uRetryCounts ms
126+
if ((uEndpointType == EP_TYPE_BULK) || (uEndpointType == EP_TYPE_CTRL))
127+
{
128+
constexpr uint32_t uRetryCount = 5;
129+
130+
pTransferDescriptor->retry++;
131+
132+
if(urbState == URB_NOTREADY)
133+
{
134+
volatile uint32_t transferred = HAL_HCD_HC_GetXferCount(pHcd, uChannel);
135+
136+
if((pTransferDescriptor->retry == uRetryCount) || (pTransferDescriptor->size==0))
137+
{
138+
// Submit the same request again, because the device wasn't ready to accept the last one
139+
// we need to be aware of any data that has already been transferred as it wont be again by the look of it.
140+
pTransferDescriptor->currBufPtr += transferred;
141+
pTransferDescriptor->size -= transferred;
142+
pTransferDescriptor->retry = 0;
143+
uint32_t uLength = pTransferDescriptor->size;
144+
145+
HAL_HCD_HC_SubmitRequest(pHcd, uChannel, uEndpointDirection, uEndpointType, !pTransferDescriptor->setup, (uint8_t *) pTransferDescriptor->currBufPtr, uLength, 0);
146+
HAL_HCD_EnableInt(pHcd, uChannel);
147+
}
148+
}
149+
}
150+
}
151+
else
152+
{
153+
// set the transfer descriptor state based on the URB state
154+
switch(urbState)
155+
{
156+
case URB_IDLE: // Guessing that we must have had a URB_DONE if we are idle
157+
case URB_DONE: pTransferDescriptor->state = USB_TYPE_IDLE; break;
158+
case URB_ERROR: pTransferDescriptor->state = USB_TYPE_ERROR; break;
159+
default: pTransferDescriptor->state = USB_TYPE_PROCESSING; break;
160+
}
161+
}
162+
}
163+
164+
if (pTransferDescriptor->state == USB_TYPE_IDLE)
165+
{
166+
// reset retry count
167+
pTransferDescriptor->retry = 0;
168+
169+
// Update transfer descriptor buffer pointer
170+
pTransferDescriptor->currBufPtr += HAL_HCD_HC_GetXferCount(pHcd, uChannel);
171+
172+
// Call transferCompleted on correct object
173+
void (USBHALHost::*func)(volatile uint32_t addr) = pPriv->transferCompleted;
174+
(pPriv->inst->*func)(reinterpret_cast<std::uintptr_t>(pTransferDescriptor));
175+
}
176+
}
177+
}
178+
}
179+
}
180+
#else
94181
void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
95182
{
96183
USBHALHost_Private_t *priv = (USBHALHost_Private_t *)(hhcd->pData);
@@ -120,9 +207,6 @@ void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
120207
uint32_t length;
121208
if ((addr != 0)) {
122209
HCTD *td = (HCTD *)addr;
123-
LogicUint7(0x70 + urb_state);
124-
LogicUint7(0x50 + chnum);
125-
digitalWrite(PA_7, HIGH);
126210

127211
#if ARC_USB_FULL_SIZE
128212
constexpr uint32_t uRetryCount = 10000/20; // (TODO: should be done with timer, investigate)
@@ -217,6 +301,7 @@ void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
217301
}
218302
}
219303
}
304+
#endif
220305

221306
USBHALHost *USBHALHost::instHost;
222307

src/targets/TARGET_STM/USBHALHost_STM.h

+4
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ USBHALHost::USBHALHost()
365365
// Set USB interrupt
366366
HAL_NVIC_SetPriority(USBHAL_IRQn, 0, 0);
367367
NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
368+
369+
#if ARC_TICKER_BASED
370+
ms_ticker.attach_us(mbed::callback(this, &USBHALHost::tickerCallback), 1000);
371+
#endif
368372
}
369373

370374
#endif // USBHALHOST_STM_H

0 commit comments

Comments
 (0)