diff --git a/radio/src/Makefile b/radio/src/Makefile index 84319127d..e5286531a 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -272,6 +272,12 @@ LUA = NO # Values = NO, READONLY, NOMENUS MENUS_LOCK = NO +# USB options in the main image +# Values = JOYSTICK, DRIVES +# JOYSTICK: The TX will appear as a joystick +# MASS: The TX will appear as a drive (as occurred pre-V2.0) +USB = JOYSTICK + #------- END BUILD OPTIONS --------------------------- # Define programs and commands. @@ -673,7 +679,7 @@ ifeq ($(PCB), TARANIS) SRC += targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_pwr.c SRC += targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_usart.c SRC += targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx/Source/Templates/gcc_ride7/startup_stm32f2xx.s - SRC += targets/taranis/usb_bsp.c targets/taranis/usbd_desc.c targets/taranis/usbd_hid_joystick.c + SRC += targets/taranis/usb_bsp.c targets/taranis/usbd_desc.c SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_OTG_Driver/src/usb_core.c SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_OTG_Driver/src/usb_dcd.c SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_OTG_Driver/src/usb_dcd_int.c @@ -682,6 +688,17 @@ ifeq ($(PCB), TARANIS) SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Core/src/usbd_req.c EXTRABOARDSRC += targets/taranis/usbd_usr.cpp SRC += targets/sky9x/syscalls.c + ifeq ($(USB), JOYSTICK) + CPPDEFS += -DUSB_JOYSTICK + SRC += targets/taranis/usbd_hid_joystick.c + else + CPPDEFS += -DUSB_MASS_STORAGE + SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_data.c + SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c + SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_bot.c + SRC += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_core.c + EXTRABOARDSRC += targets/taranis/usbd_storage_msd.cpp + endif ifneq ($(LUA), NO) ifeq ($(LUA), YES) diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 345ad6ecd..7d18a506a 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -2492,7 +2492,7 @@ void opentxClose() #endif -#if defined(PCBTARANIS) && !defined(SIMU) +#if defined(USB_JOYSTICK) && defined(PCBTARANIS) && !defined(SIMU) extern USB_OTG_CORE_HANDLE USB_OTG_dev; /* @@ -2526,7 +2526,7 @@ void usbJoystickUpdate(void) USBD_HID_SendReport (&USB_OTG_dev, HID_Buffer, HID_IN_PACKET ); } -#endif //#if defined(PCBTARANIS) && !defined(SIMU) +#endif //#if defined(USB_JOYSTICK) && defined(PCBTARANIS) && !defined(SIMU) void perMain() @@ -2683,10 +2683,17 @@ void perMain() #if defined(PCBTARANIS) && !defined(SIMU) static bool usbStarted = false; if (!usbStarted && usbPlugged()) { +#if defined(USB_MASS_STORAGE) + opentxClose(); +#endif usbStart(); +#if defined(USB_MASS_STORAGE) + usbPluggedIn(); +#endif usbStarted = true; } +#if defined(USB_JOYTICK) if (usbStarted) { if (!usbPlugged()) { //disable USB @@ -2697,6 +2704,8 @@ void perMain() usbJoystickUpdate(); } } +#endif + #endif //#if defined(PCBTARANIS) && !defined(SIMU) #if defined(NAVIGATION_STICKS) @@ -2763,29 +2772,38 @@ void perMain() StickScrollAllowed = 1 ; #endif - const char *warn = s_warning; - uint8_t menu = s_menu_count; - - if (!LCD_LOCKED()) { +#if defined(USB_MASS_STORAGE) + if (usbPlugged()) { lcd_clear(); - g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + menuMainView(0); } + else +#endif + { + const char *warn = s_warning; + uint8_t menu = s_menu_count; + + if (!LCD_LOCKED()) { + lcd_clear(); + g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + } #if defined(LUA) - luaTask(evt); + luaTask(evt); #endif - if (!LCD_LOCKED()) { - if (warn) DISPLAY_WARNING(evt); + if (!LCD_LOCKED()) { + if (warn) DISPLAY_WARNING(evt); #if defined(NAVIGATION_MENUS) - if (menu) { - const char * result = displayMenu(evt); - if (result) { - menuHandler(result); - putEvent(EVT_MENU_UP); + if (menu) { + const char * result = displayMenu(evt); + if (result) { + menuHandler(result); + putEvent(EVT_MENU_UP); + } } - } #endif + } } drawStatusLine(); diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 5c440e041..13a2b6127 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -1700,4 +1700,8 @@ void varioWakeup(); extern const pm_uchar logo_taranis[]; #endif +#if defined(USB_MASS_STORAGE) + extern void usbPluggedIn(); +#endif + #endif diff --git a/radio/src/targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c b/radio/src/targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c index 86680f352..95f27c962 100644 --- a/radio/src/targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c +++ b/radio/src/targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c @@ -97,7 +97,7 @@ static int8_t SCSI_ReadFormatCapacity(uint8_t lun, uint8_t *params); static int8_t SCSI_ReadCapacity10(uint8_t lun, uint8_t *params); static int8_t SCSI_RequestSense (uint8_t lun, uint8_t *params); static int8_t SCSI_StartStopUnit(uint8_t lun, uint8_t *params); -static int8_t SCSI_AllowRemoval(uint8_t lun, uint8_t *params); +static int8_t SCSI_AllowRemoval(uint8_t lun, uint8_t *params); static int8_t SCSI_ModeSense6 (uint8_t lun, uint8_t *params); static int8_t SCSI_ModeSense10 (uint8_t lun, uint8_t *params); static int8_t SCSI_Write10(uint8_t lun , uint8_t *params); @@ -147,7 +147,7 @@ int8_t SCSI_ProcessCmd(USB_OTG_CORE_HANDLE *pdev, return SCSI_StartStopUnit(lun, params); case SCSI_ALLOW_MEDIUM_REMOVAL: - return SCSI_AllowRemoval( lun, params); + return SCSI_AllowRemoval( lun, params); case SCSI_MODE_SENSE6: return SCSI_ModeSense6 (lun, params); @@ -433,24 +433,29 @@ void SCSI_SenseCode(uint8_t lun, uint8_t sKey, uint8_t ASC) * @param params: Command parameters * @retval status */ - -extern uint8_t lunReady[] ; - + +extern uint8_t lunReady[] ; + static int8_t SCSI_StartStopUnit(uint8_t lun, uint8_t *params) { MSC_BOT_DataLen = 0; - if (lun < 2) { - if (params[4] & 1) { - // lun to be active - lunReady[lun] = 1 ; - } - else { - // lun to be ejected - lunReady[lun] = 0 ; - } - } - +#if defined(BOOT) + if (lun < 2) +#else + if (lun < 1) +#endif + { + if (params[4] & 1) { + // lun to be active + lunReady[lun] = 1 ; + } + else { + // lun to be ejected + lunReady[lun] = 0 ; + } + } + return 0; } diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index 7a6c7fe9e..9d3805b18 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -224,7 +224,9 @@ void turnBacklightOff(void); int usbPlugged(void); void usbInit(void); void usbStart(void); +#if defined(USB_JOYSTICK) void usbStop(void); +#endif #ifdef __cplusplus } diff --git a/radio/src/targets/taranis/usb_driver.c b/radio/src/targets/taranis/usb_driver.c index 60edcc6fe..5e172071f 100755 --- a/radio/src/targets/taranis/usb_driver.c +++ b/radio/src/targets/taranis/usb_driver.c @@ -43,6 +43,7 @@ int usbPlugged(void) //debounce static uint8_t debounced_state = 0; static uint8_t last_state = 0; + if ( GPIO_ReadInputDataBit(GPIOA, PIN_FS_VBUS) ) { if (last_state) debounced_state = 1; last_state = 1; @@ -69,16 +70,23 @@ void usbInit(void) void usbStart(void) { #if !defined(BOOT) +#if defined(USB_JOYSTICK) //intialize USB as HID device USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_HID_cb, &USR_cb); +#elif defined(USB_MASS_STORAGE) + //intialize USB as MSC device + USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_MSC_cb, &USR_cb); +#endif #else //intialize USB as MSC device USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_MSC_cb, &USR_cb); #endif //!defined(BOOT) } - +#if defined(USB_JOYSTICK) void usbStop(void) { USBD_DeInit(&USB_OTG_dev); } +#endif + diff --git a/radio/src/targets/taranis/usbd_desc.c b/radio/src/targets/taranis/usbd_desc.c index 32fe20de5..9e476b161 100644 --- a/radio/src/targets/taranis/usbd_desc.c +++ b/radio/src/targets/taranis/usbd_desc.c @@ -68,10 +68,17 @@ #define USBD_CONFIGURATION_FS_STRING "MSC Config" #define USBD_INTERFACE_FS_STRING "MSC Interface" #else +#if defined(USB_JOYSTICK) #define USBD_PID 0x5710 #define USBD_PRODUCT_FS_STRING "FrSky Taranis Joystick" #define USBD_CONFIGURATION_FS_STRING "HID Config" #define USBD_INTERFACE_FS_STRING "HID Interface" +#elif defined(USB_MASS_STORAGE) + #define USBD_PID 0x5720 + #define USBD_PRODUCT_FS_STRING "FrSky Taranis Mass Storage" + #define USBD_CONFIGURATION_FS_STRING "MSC Config" + #define USBD_INTERFACE_FS_STRING "MSC Interface" +#endif #endif USBD_DEVICE USR_desc = diff --git a/radio/src/targets/taranis/usbd_storage_msd.cpp b/radio/src/targets/taranis/usbd_storage_msd.cpp index df40bbd9b..4aa0c3fb3 100644 --- a/radio/src/targets/taranis/usbd_storage_msd.cpp +++ b/radio/src/targets/taranis/usbd_storage_msd.cpp @@ -35,7 +35,12 @@ extern "C" { #include "usbd_msc_mem.h" #include "usb_conf.h" +#if defined(BOOT) #define STORAGE_LUN_NBR 2 +#else +/* SD card only when not running bootloader */ +#define STORAGE_LUN_NBR 1 +#endif #define BLOCKSIZE 512 /* USB Mass storage Standard Inquiry Data */ @@ -54,6 +59,7 @@ const unsigned char STORAGE_Inquirydata[] = {//36 'T', 'a', 'r', 'a', 'n', 'i', 's', ' ', /* Product : 16 Bytes */ 'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ', '1', '.', '0', '0', /* Version : 4 Bytes */ +#if defined(BOOT) /* LUN 1 */ 0x00, 0x80, @@ -67,10 +73,13 @@ const unsigned char STORAGE_Inquirydata[] = {//36 'T', 'a', 'r', 'a', 'n', 'i', 's', ' ', /* Product : 16 Bytes */ 'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ', '1', '.', '0' ,'0', /* Version : 4 Bytes */ +#endif }; +#if defined(BOOT) int32_t fat12Write( const uint8_t *buffer, uint16_t sector, uint32_t count ) ; int32_t fat12Read( uint8_t *buffer, uint16_t sector, uint16_t count ) ; +#endif int8_t STORAGE_Init (uint8_t lun); @@ -137,11 +146,14 @@ int8_t STORAGE_Init (uint8_t lun) */ int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size) { +#if defined(BOOT) if (lun == 1) { *block_size = BLOCKSIZE; *block_num = 3 + EESIZE/BLOCKSIZE + FLASHSIZE/BLOCKSIZE; } - else { + else +#endif + { if (!SD_CARD_PRESENT()) return -1; @@ -161,17 +173,22 @@ int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_si return 0; } +#if defined(BOOT) uint8_t lunReady[2] ; +#else +uint8_t lunReady[1] ; +#endif void usbPluggedIn() { if (lunReady[0] == 0) { lunReady[0] = 1; } - +#if defined(BOOT) if (lunReady[1] == 0) { lunReady[1] = 1; } +#endif } /** @@ -181,13 +198,16 @@ void usbPluggedIn() */ int8_t STORAGE_IsReady (uint8_t lun) { +#if defined(BOOT) if (lun == 1) { if (lunReady[1] == 0) { return -1 ; } return 0 ; } - else { + else +#endif + { if (lunReady[0] == 0) { return -1 ; } @@ -221,11 +241,14 @@ int8_t STORAGE_Read (uint8_t lun, uint32_t blk_addr, uint16_t blk_len) { +#if defined(BOOT) if (lun == 1) { if (fat12Read(buf, blk_addr, blk_len) != 0) return -1; } - else { + else +#endif + { if (SD_ReadSectors(buf, blk_addr, blk_len) != 0) { return -1; } @@ -249,11 +272,14 @@ int8_t STORAGE_Write (uint8_t lun, uint32_t blk_addr, uint16_t blk_len) { +#if defined(BOOT) if (lun == 1) { if (fat12Write(buf, blk_addr, blk_len) != 0) return -1; } - else { + else +#endif + { if (SD_WriteSectors(buf, blk_addr, blk_len) != 0) return -1; } @@ -269,10 +295,10 @@ int8_t STORAGE_Write (uint8_t lun, int8_t STORAGE_GetMaxLun (void) { - return (STORAGE_LUN_NBR - 1); + return STORAGE_LUN_NBR - 1; } - +#if defined(BOOT) //------------------------------------------------------------------------------ /** * FAT12 boot sector partition. @@ -766,5 +792,5 @@ int32_t fat12Write(const uint8_t *buffer, uint16_t sector, uint32_t count ) } return 0 ; } - +#endif /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/radio/src/targets/taranis/usbd_usr.cpp b/radio/src/targets/taranis/usbd_usr.cpp index 5c443baf6..28717401e 100644 --- a/radio/src/targets/taranis/usbd_usr.cpp +++ b/radio/src/targets/taranis/usbd_usr.cpp @@ -119,6 +119,9 @@ void USBD_USR_DeviceConnected (void) */ void USBD_USR_DeviceDisconnected (void) { +#if !defined(BOOT) && defined(USB_MASS_STORAGE) + NVIC_SystemReset(); +#endif }