Skip to content

Commit a4e2049

Browse files
committed
LP-602 significant change to USB layer. force complete USB stack reset on replug/reset from host device
1 parent ca82317 commit a4e2049

2 files changed

Lines changed: 32 additions & 27 deletions

File tree

flight/pios/stm32f4xx/pios_usb.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <pios_helpers.h>
3939

4040
/* Rx/Tx status */
41-
static uint8_t transfer_possible = 0;
41+
static volatile uint8_t transfer_possible = 0;
4242

4343
#ifdef PIOS_INCLUDE_FREERTOS
4444
static void(*disconnection_cb_list[3]) (void);
@@ -148,6 +148,9 @@ int32_t PIOS_USB_Init(uint32_t *usb_id, const struct pios_usb_cfg *cfg)
148148
*/
149149
int32_t PIOS_USB_ChangeConnectionState(bool connected)
150150
{
151+
#ifdef PIOS_INCLUDE_FREERTOS
152+
static volatile uint8_t lastStatus = 2; // 2 is "no last status"
153+
#endif
151154
// In all cases: re-initialise USB HID driver
152155
if (connected) {
153156
transfer_possible = 1;
@@ -168,6 +171,12 @@ int32_t PIOS_USB_ChangeConnectionState(bool connected)
168171

169172
#ifdef PIOS_INCLUDE_FREERTOS
170173
raiseConnectionStateCallback(connected);
174+
if (lastStatus != transfer_possible) {
175+
if (lastStatus == 1) {
176+
raiseDisconnectionCallbacks();
177+
}
178+
lastStatus = transfer_possible;
179+
}
171180
#endif
172181

173182
return 0;
@@ -187,28 +196,7 @@ bool PIOS_USB_CheckAvailable(__attribute__((unused)) uint32_t id)
187196
return false;
188197
}
189198

190-
usb_found = ((usb_dev->cfg->vsense.gpio->IDR & usb_dev->cfg->vsense.init.GPIO_Pin) != 0) ^ usb_dev->cfg->vsense_active_low;
191-
// Please note that checks of transfer_possible and the reconnection handling is
192-
// suppressed for non freertos mode (aka bootloader) as this is causing problems detecting connection and
193-
// broken communications.
194-
#ifdef PIOS_INCLUDE_FREERTOS
195-
static bool lastStatus = false;
196-
bool status = usb_found != 0 && transfer_possible ? 1 : 0;
197-
bool reconnect = false;
198-
if (xSemaphoreTakeFromISR(usb_dev->statusCheckSemaphore, NULL) == pdTRUE) {
199-
reconnect = (lastStatus && !status);
200-
lastStatus = status;
201-
xSemaphoreGiveFromISR(usb_dev->statusCheckSemaphore, NULL);
202-
}
203-
if (reconnect) {
204-
raiseDisconnectionCallbacks();
205-
}
206-
return status;
207-
208-
#else
209-
return usb_found;
210-
211-
#endif
199+
return transfer_possible;
212200
}
213201

214202
/*

flight/pios/stm32f4xx/pios_usbhook.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ static USBD_DEVICE device_callbacks = {
280280
static void PIOS_USBHOOK_USR_Init(void)
281281
{
282282
PIOS_USB_ChangeConnectionState(false);
283-
reconnect();
283+
// reconnect dev logically on init (previously a call to reconnect())
284+
DCD_DevDisconnect(&pios_usb_otg_core_handle);
285+
DCD_DevConnect(&pios_usb_otg_core_handle);
284286
}
285287

286288
static void PIOS_USBHOOK_USR_DeviceReset(__attribute__((unused)) uint8_t speed)
@@ -491,9 +493,24 @@ static USBD_Class_cb_TypeDef class_callbacks = {
491493

492494
static void reconnect(void)
493495
{
494-
/* Force a physical disconnect/reconnect */
495-
DCD_DevDisconnect(&pios_usb_otg_core_handle);
496-
DCD_DevConnect(&pios_usb_otg_core_handle);
496+
static volatile bool in_reconnect = false;
497+
498+
/* Force a complete device reset. This can trigger a call to reconnect() so prevent recursion */
499+
if (!in_reconnect) {
500+
in_reconnect = true; // save since volatile and STM32F4 is single core
501+
// disable USB device
502+
DCD_DevDisconnect(&pios_usb_otg_core_handle);
503+
USBD_DeInit(&pios_usb_otg_core_handle);
504+
USB_OTG_StopDevice(&pios_usb_otg_core_handle);
505+
// enable USB device
506+
USBD_Init(&pios_usb_otg_core_handle,
507+
USB_OTG_FS_CORE_ID,
508+
&device_callbacks,
509+
&class_callbacks,
510+
&user_callbacks);
511+
512+
in_reconnect = false;
513+
}
497514
}
498515

499516
#endif /* PIOS_INCLUDE_USB */

0 commit comments

Comments
 (0)