Skip to content

Commit

Permalink
Added native USB connection detection (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajjjjjjjj authored Nov 30, 2024
1 parent d49cc03 commit ec4da48
Show file tree
Hide file tree
Showing 21 changed files with 19 additions and 68 deletions.
8 changes: 0 additions & 8 deletions radio/src/gui/128x64/view_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,6 @@ void onMainViewMenu(const char *result)
watchdogSuspend(200); // 2s
saveAllData();
}
#if !defined(USB_VBUS)
else if (result == STR_USBCONNECT) {
globalData.usbConnect = 1;
}
#endif
#if !defined(PCBI6X)
else if (result == STR_ABOUT_US) {
chainMenu(menuAboutView);
Expand Down Expand Up @@ -343,9 +338,6 @@ void menuMainView(event_t event)

POPUP_MENU_ADD_ITEM(STR_STATISTICS);

#if !defined(USB_VBUS)
POPUP_MENU_ADD_ITEM(STR_USBCONNECT);
#endif
#if !defined(PCBI6X)
POPUP_MENU_ADD_ITEM(STR_ABOUT_US);
#endif
Expand Down
3 changes: 3 additions & 0 deletions radio/src/gui/128x64/view_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ void menuStatisticsDebug(event_t event)
// lcdDrawText(lcdLastRightPos, MENU_DEBUG_Y_COPROC, "/");
// lcdDrawNumber(lcdLastRightPos, MENU_DEBUG_Y_COPROC, stackSize() * 4, LEFT);

// lcdDrawTextAlignedLeft(MENU_DEBUG_Y_COPROC, "USB Conn");
// lcdDrawNumber(MENU_DEBUG_COL1_OFS, MENU_DEBUG_Y_COPROC, ((GPIOA->IDR & GPIO_IDR_11) ? 1 : 0), LEFT);

lcdDrawTextAlignedLeft(MENU_DEBUG_Y_MIXMAX, STR_TMIXMAXMS);
lcdDrawNumber(MENU_DEBUG_COL1_OFS, MENU_DEBUG_Y_MIXMAX, DURATION_MS_PREC2(maxMixerDuration), PREC2|LEFT);
lcdDrawText(lcdLastRightPos, MENU_DEBUG_Y_MIXMAX, "ms");
Expand Down
1 change: 0 additions & 1 deletion radio/src/opentx.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ void flightReset(uint8_t check=true);
PACK(struct GlobalData {
#if defined(PCBI6X)
uint8_t unexpectedShutdown;
uint8_t usbConnect;
uint8_t cToolRunning;
#else
uint8_t unexpectedShutdown:1;
Expand Down
30 changes: 0 additions & 30 deletions radio/src/targets/common/arm/stm32/usb_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,10 @@ static void CRS_Config(void);

void USB_BSP_Init(USB_CORE_HANDLE *pdev) {

RCC_AHBPeriphClockCmd(USB_RCC_AHBPeriph_GPIO, ENABLE);

#if defined USB_CLOCK_SOURCE_CRS
RCC_USBCLKConfig(RCC_USBCLK_HSI48);

CRS_Config();
#else
RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) {}

/* Enable PLL */
RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}

/* Configure USBCLK from PLL clock */
RCC_USBCLKConfig(RCC_USBCLK_PLLCLK);
#endif /*USB_CLOCK_SOURCE_CRS */

// included in RCC_APB1_LIST
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);

#if defined(USB_GPIO_PIN_VBUS)
/* Configure VBUS Pin */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = USB_GPIO_PIN_VBUS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(USB_GPIO, &GPIO_InitStructure);
#endif
}

Expand Down
20 changes: 12 additions & 8 deletions radio/src/targets/common/arm/stm32/usb_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,16 @@ void setSelectedUsbMode(int mode)

int usbPlugged()
{
#if !defined(USB_VBUS)
if (globalData.usbConnect) {
// if connected prevent false reads on USB DM pin
if (usbStarted()) {
return 1;
}
#endif

#if defined(USB_GPIO_PIN_VBUS)
// debounce
static uint8_t debounced_state = 0;
static uint8_t last_state = 0;

if (GPIO_ReadInputDataBit(USB_GPIO, USB_GPIO_PIN_VBUS)) {
if (!GPIO_ReadInputDataBit(USB_GPIO, USB_GPIO_PIN_DM)) {
if (last_state) {
debounced_state = 1;
}
Expand All @@ -73,9 +71,6 @@ int usbPlugged()
last_state = 0;
}
return debounced_state;
#else
return 0;
#endif // USB_GPIO_PIN_VBUS
}

#if defined(STM32F0)
Expand All @@ -99,6 +94,15 @@ extern "C" void OTG_FS_IRQHandler()

void usbInit()
{
// USB DP/DM as connection detect
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = USB_GPIO_PIN_DM;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(USB_GPIO, &GPIO_InitStructure);

// Initialize hardware
#if defined(STM32F0)
USB_BSP_Init(&USB_Device_dev);
Expand Down
5 changes: 0 additions & 5 deletions radio/src/targets/flysky/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ option(PCBI6X_ELRS "Enable ExpressLRS configuration tool" NO) # ~4.0kB
option(CRSF_EXTENDED_TYPES "Enable Support for CRSF STRING and FLOAT types" NO) # ~0.2kB
option(PCBI6X_HELLO "Enable Hello World example tool" NO)
option(PCBI6X_INAV "Enable INAV Lite telemetry screen" NO) # ~2kB
option(USB_VBUS "Removes manual USB connection menu option, requires wiring USB VBUS to PA15 pad" NO)
option(USB_MSD "Enable USB mass storage mode" YES) # 3.2kB
option(AFHDS2A_LQI_CH "Send RSSI at channel 1-17" OFF)
option(DFPLAYER "Enables DFPlayer on AUX3 TX" NO) # ~1.7kB
Expand Down Expand Up @@ -44,10 +43,6 @@ if(NOT AFHDS2A_LQI_CH STREQUAL OFF)
add_definitions(-DAFHDS2A_LQI_CH=${AFHDS2A_LQI_CH})
endif()

if(USB_VBUS STREQUAL YES)
add_definitions(-DUSB_VBUS)
endif()

if(FLYSKY_GIMBAL STREQUAL YES)
add_definitions(-DFLYSKY_GIMBAL)
set(TARGET_SRC
Expand Down
5 changes: 3 additions & 2 deletions radio/src/targets/flysky/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ extern void ISR_TIMER3_CAPT_vect(void);
// USB
#define USB_RCC_AHBPeriph_GPIO RCC_AHBPeriph_GPIOA
#define USB_GPIO GPIOA
#define USB_GPIO_PIN_VBUS GPIO_Pin_15 // PA.15
#define USB_GPIO_PIN_DM GPIO_Pin_11 // PA.11
#define USB_GPIO_PIN_DP GPIO_Pin_12 // PA.12

// Flash (taken from f2)
#define FLASH_CR_SER ((uint32_t)0x00000002)
Expand Down Expand Up @@ -434,7 +435,7 @@ extern void ISR_TIMER3_CAPT_vect(void);
//all used RCC goes here
#define RCC_AHB1_LIST (I2C_RCC_AHB1Periph | BACKLIGHT_STD_RCC_APB1Periph | BACKLIGHT_RCC_AHB1Periph | LCD_RCC_AHB1Periph | KEYS_RCC_AHB1Periph | BUZZER_RCC_AHBPeriph \
| EXTMODULE_RCC_AHBPeriph | CRC_RCC_AHB1Periph | TELEMETRY_RCC_AHB1Periph | AUX_SERIAL_RCC_AHB1Periph \
| AUX3_SERIAL_RCC_AHB1Periph | AUX4_SERIAL_RCC_AHB1Periph | ADC_RCC_AHB1Periph)
| AUX3_SERIAL_RCC_AHB1Periph | AUX4_SERIAL_RCC_AHB1Periph | ADC_RCC_AHB1Periph | USB_RCC_AHBPeriph_GPIO)
#define RCC_APB1_LIST (I2C_RCC_APB1Periph | RCC_APB1Periph_TIM6 /*delays*/ | INTERRUPT_xMS_RCC_APB1Periph | TIMER_2MHz_RCC_APB1Periph \
| TELEMETRY_RCC_APB1Periph | BACKLIGHT_STD_RCC_APB1Periph | BACKLIGHT_RCC_APB1Periph | RCC_APB1Periph_USB \
| AUX3_SERIAL_RCC_APB1Periph | AUX4_SERIAL_RCC_APB1Periph)
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ const char STR_EXTERNALRF[] = TR_EXTERNALRF;
const char STR_MODULE_TELEMETRY[] = TR_MODULE_TELEMETRY;
const char STR_MODULE_TELEM_ON[] = TR_MODULE_TELEM_ON;
const char STR_COUNTRYCODE[] = TR_COUNTRYCODE;
const char STR_USBCONNECT[] = TR_USBCONNECT;
const char STR_USBMODE[] = TR_USBMODE;
const char STR_FAILSAFE[] = TR_FAILSAFE;
const char STR_FAILSAFESET[] = TR_FAILSAFESET;
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@ extern const char STR_NONE[];
extern const char STR_MENUSENSOR[];
extern const char STR_SENSOR[];
extern const char STR_COUNTRYCODE[];
extern const char STR_USBCONNECT[];
extern const char STR_USBMODE[];
extern const char STR_DISABLE_INTERNAL[];

Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/cz.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,6 @@
#define TR_MENUSENSOR "SENZOR"
#define TR_SENSOR "SENZOR"
#define TR_COUNTRYCODE "Kód regionu"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Jazyk hlasu"
#define TR_UNITSSYSTEM "Jednotky"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/de.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Landescode"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Modus"
#define TR_VOICELANG "Sprach-Ansagen"
#define TR_UNITSSYSTEM "Einheiten"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/en.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Country code"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Voice language"
#define TR_UNITSSYSTEM "Units"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/es.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Codigo Pais"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Idioma voces"
#define TR_UNITSSYSTEM "Unidades"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/fi.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Country Code"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Voice Language"
#define TR_UNITSSYSTEM "Units"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/fr.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,6 @@
#define TR_MENUSENSOR "CAPTEUR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE TR("Zone géo.", "Zone géographique")
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG TR("Langue voix", "Langue annonces vocales")
#define TR_UNITSSYSTEM "Unités"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/it.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE TR("Codice Paese","Standard 2.4Ghz")
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Lingua Voce"
#define TR_UNITSSYSTEM "Unità"
Expand Down
2 changes: 1 addition & 1 deletion radio/src/translations/nl.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Landcode"
#define TR_USBCONNECT "USB Connect"
#define "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Taal"
#define TR_UNITSSYSTEM "Eenheden"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/pl.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,6 @@
#define TR_MENUSENSOR "CZUJNIK"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Kod regionu"
#define TR_USBCONNECT "Połącz USB"
#define TR_USBMODE "Tryb USB"
#define TR_VOICELANG "Język głosu"
#define TR_UNITSSYSTEM "Jednostki"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/pt.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Country Code"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Voice Language"
#define TR_UNITSSYSTEM "Units"
Expand Down
1 change: 0 additions & 1 deletion radio/src/translations/se.h.txt
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,6 @@
#define TR_MENUSENSOR "SENSOR"
#define TR_SENSOR "SENSOR"
#define TR_COUNTRYCODE "Landskod"
#define TR_USBCONNECT "USB Connect"
#define TR_USBMODE "USB Mode"
#define TR_VOICELANG "Röstspråk"
#define TR_UNITSSYSTEM "Enheter"
Expand Down
1 change: 0 additions & 1 deletion tools/build-flysky.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"LUA_COMPILER": "NO",
"DISABLE_COMPANION": "YES",
"PPM_UNIT": "PERCENT_PREC1",
"USB_VBUS": "NO",
"USB_MSD": "YES",
"SBUS_TRAINER": "NO",
}
Expand Down

0 comments on commit ec4da48

Please sign in to comment.