Skip to content

Commit

Permalink
feat(fw): Only show SD card in USB storage mode (#3045)
Browse files Browse the repository at this point in the history
* Only attach SD card in USB storage mode

* Added compile time option FWDRIVE (default OFF) to enable attaching also the firmware drive via USB, when attached in mass storage mode.

* Re-trigger GitHub Actions

* chore: Trailing whitespace cleanup
  • Loading branch information
rotorman authored Jan 26, 2023
1 parent d952333 commit 5acaa79
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 69 deletions.
5 changes: 5 additions & 0 deletions radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ option(TBS_RELEASE "Used to build TBS released firmware" OFF)
option(IMRC_RELEASE "Used to build IMRC released firmware" OFF)
option(HARDWARE_TRAINER_MULTI "Allow multi trainer" OFF)
option(BOOTLOADER "Include Bootloader" ON)
option(FWDRIVE "Attach also firmware drive with USB" OFF)

# since we reset all default CMAKE compiler flags for firmware builds, provide an alternate way for user to specify additional flags.
set(FIRMWARE_C_FLAGS "" CACHE STRING "Additional flags for firmware target c compiler (note: all CMAKE_C_FLAGS[_*] are ignored for firmware/bootloader).")
Expand Down Expand Up @@ -453,6 +454,10 @@ else()
endif()
add_definitions(-DDEFAULT_TEMPLATE_SETUP=${DEFAULT_TEMPLATE_SETUP})

if(FWDRIVE)
add_definitions(-DFWDRIVE)
endif()

set(SRC
${SRC}
opentx.cpp
Expand Down
4 changes: 2 additions & 2 deletions radio/src/targets/common/arm/stm32/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#define USBD_VID_PID_CODES 0x1209 // https://pid.codes

#define USBD_LANGID_STRING 0x409
#define USBD_MANUFACTURER_STRING "OpenTX"
#define USBD_MANUFACTURER_STRING "EdgeTX"
#define USBD_SERIALNUMBER_FS_STRING "00000000001B"

#if defined(BOOT)
Expand Down Expand Up @@ -294,4 +294,4 @@ uint8_t * USBD_USR_InterfaceStrDescriptor( uint8_t speed , uint16_t *length)
return USBD_StrDesc;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
148 changes: 81 additions & 67 deletions radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,68 +44,74 @@ extern "C" {

enum MassstorageLuns {
STORAGE_SDCARD_LUN,
#if defined(FWDRIVE)
STORAGE_EEPROM_LUN,
#endif
STORAGE_LUN_NBR
};

/* USB Mass storage Standard Inquiry Data */
const unsigned char STORAGE_Inquirydata[] = { //36
const unsigned char STORAGE_Inquirydata[] = {
/* LUN 0 */
0x00,
0x80,
0x02,
0x00,
0x80,
0x02,
0x02,
(USBD_STD_INQUIRY_LENGTH - 5),
0x00,
0x00,
0x00,
0x00,
USB_MANUFACTURER, /* Manufacturer : 8 bytes */
USB_PRODUCT, /* Product : 16 Bytes */
'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ',
'1', '.', '0', '0', /* Version : 4 Bytes */
#if defined(FWDRIVE)
/* LUN 1 */
0x00,
0x80,
0x02,
0x00,
0x80,
0x02,
0x02,
(USBD_STD_INQUIRY_LENGTH - 5),
0x00,
0x00,
0x00,
0x00,
USB_MANUFACTURER, /* Manufacturer : 8 bytes */
USB_PRODUCT, /* Product : 16 Bytes */
'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ',
'1', '.', '0' ,'0', /* Version : 4 Bytes */
#endif
};

#define RESERVED_SECTORS (1 /*Boot*/ + 2 /*Fat table */ + 1 /*Root dir*/ + 8 /* one cluster for firmware.txt */)
#if defined(FWDRIVE)
#define RESERVED_SECTORS (1 /*Boot*/ + 2 /*Fat table */ + 1 /*Root dir*/ + 8 /* one cluster for firmware.txt */)

int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count);
int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count );
int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count);
int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count );
#endif

int8_t STORAGE_Init (uint8_t lun);

int8_t STORAGE_GetCapacity (uint8_t lun,
uint32_t *block_num,
int8_t STORAGE_GetCapacity (uint8_t lun,
uint32_t *block_num,
uint32_t *block_size);

int8_t STORAGE_IsReady (uint8_t lun);
int8_t STORAGE_IsReady (uint8_t lun);

int8_t STORAGE_IsWriteProtected (uint8_t lun);
int8_t STORAGE_IsWriteProtected (uint8_t lun);

int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len);

int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len);

int8_t STORAGE_GetMaxLun (void);

const USBD_STORAGE_cb_TypeDef USBD_MICRO_SDIO_fops = // modified my OpenTX
const USBD_STORAGE_cb_TypeDef USBD_MICRO_SDIO_fops =
{
STORAGE_Init,
STORAGE_GetCapacity,
Expand All @@ -117,7 +123,7 @@ const USBD_STORAGE_cb_TypeDef USBD_MICRO_SDIO_fops = // modified my OpenTX
(int8_t *)STORAGE_Inquirydata,
};

const USBD_STORAGE_cb_TypeDef * const USBD_STORAGE_fops = &USBD_MICRO_SDIO_fops; // modified my OpenTX
const USBD_STORAGE_cb_TypeDef * const USBD_STORAGE_fops = &USBD_MICRO_SDIO_fops;

#if defined(__cplusplus) && !defined(SIMU)
}
Expand All @@ -134,8 +140,8 @@ int8_t STORAGE_Init (uint8_t lun)

/* TODO if no SD ... if( SD_Init() != 0)
{
return (-1);
}
return (-1);
}
*/
return (0);
}
Expand All @@ -149,19 +155,21 @@ int8_t STORAGE_Init (uint8_t lun)
*/
int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
{
#if defined(FWDRIVE)
if (lun == STORAGE_EEPROM_LUN) {
*block_size = BLOCK_SIZE;
#if defined(EEPROM)
#if defined(EEPROM)
*block_num = RESERVED_SECTORS + EEPROM_SIZE/BLOCK_SIZE + FLASHSIZE/BLOCK_SIZE;
#else
#else
*block_num = RESERVED_SECTORS + FLASHSIZE/BLOCK_SIZE;
#endif
#endif
return 0;
}
#endif

if (!SD_CARD_PRESENT())
return -1;

*block_size = BLOCK_SIZE;

static DWORD sector_count = 0;
Expand All @@ -182,7 +190,9 @@ uint8_t lunReady[STORAGE_LUN_NBR];
void usbInitLUNs()
{
lunReady[STORAGE_SDCARD_LUN] = 1;
#if defined(FWDRIVE)
lunReady[STORAGE_EEPROM_LUN] = 1;
#endif
}

/**
Expand All @@ -191,13 +201,12 @@ void usbInitLUNs()
* @retval Status
*/
int8_t STORAGE_IsReady (uint8_t lun)
{
#if defined(EEPROM)
{
#if defined(FWDRIVE) && defined(EEPROM)
if (lun == STORAGE_EEPROM_LUN) {
return (lunReady[STORAGE_EEPROM_LUN] != 0) ? 0 : -1;
}
#endif

return (lunReady[STORAGE_SDCARD_LUN] != 0 && SD_CARD_PRESENT()) ? 0 : -1;
}

Expand All @@ -220,16 +229,18 @@ int8_t STORAGE_IsWriteProtected (uint8_t lun)
* @retval Status
*/

int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
WATCHDOG_SUSPEND(100/*1s*/);


#if defined(FWDRIVE)
if (lun == STORAGE_EEPROM_LUN) {
return (fat12Read(buf, blk_addr, blk_len) == 0) ? 0 : -1;
}
#endif

// read without cache
return (__disk_read(0, buf, blk_addr, blk_len) == RES_OK) ? 0 : -1;
Expand All @@ -243,16 +254,18 @@ int8_t STORAGE_Read (uint8_t lun,
* @retval Status
*/

int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
WATCHDOG_SUSPEND(100/*1s*/);


#if defined(FWDRIVE)
if (lun == STORAGE_EEPROM_LUN) {
return (fat12Write(buf, blk_addr, blk_len) == 0) ? 0 : -1;
}
#endif

// write without cache
return (__disk_write(0, buf, blk_addr, blk_len) == RES_OK) ? 0 : -1;
Expand All @@ -269,39 +282,39 @@ int8_t STORAGE_GetMaxLun (void)
return STORAGE_LUN_NBR - 1;
}


#if defined(FWDRIVE)
/* Firmware.txt */
const char firmware_txt[] =
#if defined(BOOT)
#if defined(BOOT)
"EdgeTX Bootloader"
#else
#else
"EdgeTX Firmware"
#endif
#endif
" for " FLAVOUR "\r\n\r\n"
#if defined(BOOT)
#if defined(BOOT)
"BOOTVER "
#else
#else
"FWVERSION "
#endif
#endif
"edgetx-" FLAVOUR "-" VERSION " (" GIT_STR ")\r\n"
"DATE " DATE "\r\n"
"TIME " TIME "\r\n"
#if !defined(BOOT)
#if !defined(BOOT)
"BOOTVER "
#else
#else
"FWVERSION "
#endif
#endif
;

//------------------------------------------------------------------------------
/**
* FAT12 boot sector partition.
*/
#if defined(EEPROM)
#define TOTALSECTORS (RESERVED_SECTORS + (EEPROM_SIZE/BLOCK_SIZE) + (FLASHSIZE/BLOCK_SIZE))
#else
#define TOTALSECTORS (RESERVED_SECTORS + (FLASHSIZE/BLOCK_SIZE))
#endif
#if defined(EEPROM)
#define TOTALSECTORS (RESERVED_SECTORS + (EEPROM_SIZE/BLOCK_SIZE) + (FLASHSIZE/BLOCK_SIZE))
#else
#define TOTALSECTORS (RESERVED_SECTORS + (FLASHSIZE/BLOCK_SIZE))
#endif
const char g_FATboot[BLOCK_SIZE] =
{
0xeb, 0x3c, 0x90, // Jump instruction.
Expand Down Expand Up @@ -424,11 +437,11 @@ const FATDirEntry_t g_DIRroot[] =
{
{ 'F', 'I', 'R', 'M', 'W', 'A', 'R', 'E'},
{ 'B', 'I', 'N'},
#if defined(BOOT)
#if defined(BOOT)
0x20, // Archive
#else
#else
0x21, // Readonly+Archive
#endif
#endif
0x00,
0x3E,
0xA301,
Expand All @@ -440,7 +453,7 @@ const FATDirEntry_t g_DIRroot[] =
0x0003,
FLASHSIZE
},
#if defined(EEPROM)
#if defined(EEPROM)
{
{ 'E', 'E', 'P', 'R', 'O', 'M', ' ', ' '},
{ 'B', 'I', 'N'},
Expand All @@ -456,7 +469,7 @@ const FATDirEntry_t g_DIRroot[] =
0x0003 + (FLASHSIZE/BLOCK_SIZE)/8,
EEPROM_SIZE
},
#endif
#endif
// Emty entries are 0x00, omitted here. Up to 16 entries can be defined here
};

Expand Down Expand Up @@ -508,12 +521,12 @@ int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count)
pushCluster (buffer, sector, cluster, rest, cluster+1);
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0xFFF);

#if defined(EEPROM)
#if defined(EEPROM)
// Entry for eeprom.bin
for (int i=0;i<EEPROM_SIZE/BLOCK_SIZE/8 -1;i++)
pushCluster (buffer, sector, cluster, rest, cluster+1);
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0xFFF);
#endif
#endif

// Ensure last cluster is written if it is the first half
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0x000);
Expand All @@ -537,11 +550,11 @@ int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count)
address += FIRMWARE_ADDRESS;
memcpy(buffer, (uint8_t *)address, BLOCK_SIZE);
}
#if defined(EEPROM)
#if defined(EEPROM)
else if (sector < RESERVED_SECTORS + (EEPROM_SIZE/BLOCK_SIZE) + (FLASHSIZE/BLOCK_SIZE)) {
eepromReadBlock(buffer, (sector - RESERVED_SECTORS - (FLASHSIZE/BLOCK_SIZE))*BLOCK_SIZE, BLOCK_SIZE);
}
#endif
#endif
buffer += BLOCK_SIZE ;
sector++ ;
count-- ;
Expand All @@ -568,9 +581,9 @@ int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count)
// reserved, read-only
}
else if (sector < RESERVED_SECTORS + (FLASHSIZE/BLOCK_SIZE)) {
#if !defined(BOOT) // Don't allow overwrite of running firmware
#if !defined(BOOT) // Don't allow overwrite of running firmware
return -1;
#else
#else
// firmware
uint32_t address;
address = sector - RESERVED_SECTORS;
Expand All @@ -597,9 +610,9 @@ int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count)
operation = FATWRITE_NONE;
}
}
#endif
#endif
}
#if defined(EEPROM)
#if defined(EEPROM)
else if (sector < RESERVED_SECTORS + (EEPROM_SIZE/BLOCK_SIZE) + (FLASHSIZE/BLOCK_SIZE)) {
// eeprom
while (count) {
Expand All @@ -619,6 +632,7 @@ int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count)
}
}
}
#endif
#endif
return 0 ;
}
#endif

0 comments on commit 5acaa79

Please sign in to comment.