Skip to content

Commit e99b8d2

Browse files
authored
Merge pull request #24 from alrvid/master
Fix crash bugs on connect and disconnect
2 parents 891b0d9 + dba59a0 commit e99b8d2

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/USBHost/USBHost.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,12 @@ bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint
697697
default:
698698
return false;
699699
}
700-
701700
#endif
702701
ep->dev = dev;
703-
dev->addEndpoint(intf_nb, ep);
704-
702+
if (nullptr != dev)
703+
{
704+
dev->addEndpoint(intf_nb, ep);
705+
}
705706
return true;
706707
}
707708

src/targets/TARGET_STM/USBEndpoint_STM.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ void USBEndpoint::setState(USB_TYPE st)
146146
/* small speed device with hub not supported
147147
if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
148148

149-
// Notice that dev->getAddress() must be used instead of device_address, because the latter will contain
150-
// incorrect values in certain cases
151-
HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, dev->getAddress(), hcd_speed, type, size);
152-
// HAL_HCD_HC_Init() doesn't fully enable the channel after disable above, so we do it here -->
153-
USBx_HC(hced->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
154-
USBx_HC(hced->ch_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
155-
// <--
149+
// If the device seems to be gone, there is no use trying to enable the channel again
150+
if (nullptr != dev)
151+
{
152+
// Notice that dev->getAddress() must be used instead of device_address, because the latter will contain
153+
// incorrect values in certain cases
154+
HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, dev->getAddress(), hcd_speed, type, size);
155+
// HAL_HCD_HC_Init() doesn't fully enable the channel after disable above, so we do it here -->
156+
USBx_HC(hced->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
157+
USBx_HC(hced->ch_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
158+
// <--
159+
}
156160
}
157161
}
158162

0 commit comments

Comments
 (0)