diff --git a/firmware/config/boards/proteus/board.mk b/firmware/config/boards/proteus/board.mk index 0aba58bf19..36f08789d3 100644 --- a/firmware/config/boards/proteus/board.mk +++ b/firmware/config/boards/proteus/board.mk @@ -21,6 +21,9 @@ DDEFS += -DSTM32_ADC_USE_ADC3=TRUE DDEFS += -DEFI_SOFTWARE_KNOCK=TRUE DDEFS += -DEFI_CONSOLE_TX_BRAIN_PIN=GPIO_UNASSIGNED -DEFI_CONSOLE_RX_BRAIN_PIN=GPIO_UNASSIGNED +# USB mass storage support +DDEFS += -DHAL_USE_COMMUNITY=TRUE -DHAL_USE_USB_MSD=TRUE + # We are running on Proteus hardware! DDEFS += -DHW_PROTEUS=1 diff --git a/firmware/config/stm32f4ems/efifeatures.h b/firmware/config/stm32f4ems/efifeatures.h index a7c98f7a5a..39f4c350e3 100644 --- a/firmware/config/stm32f4ems/efifeatures.h +++ b/firmware/config/stm32f4ems/efifeatures.h @@ -37,7 +37,9 @@ #define EFI_HPFP TRUE +#ifndef HAL_USE_USB_MSD #define HAL_USE_USB_MSD FALSE +#endif #define EFI_ENABLE_CRITICAL_ENGINE_STOP TRUE #define EFI_ENABLE_ENGINE_WARNING TRUE diff --git a/firmware/hw_layer/mmc_card.cpp b/firmware/hw_layer/mmc_card.cpp index d087ddc4c1..b1e077e197 100644 --- a/firmware/hw_layer/mmc_card.cpp +++ b/firmware/hw_layer/mmc_card.cpp @@ -352,6 +352,13 @@ static const scsi_inquiry_response_t scsi_inquiry_response = { "SD Card", {'v',CH_KERNEL_MAJOR+'0','.',CH_KERNEL_MINOR+'0'} }; + +static binary_semaphore_t usbConnectedSemaphore; + +void onUsbConnectedNotifyMmcI() { + chBSemSignalI(&usbConnectedSemaphore); +} + #endif /* HAL_USE_USB_MSD */ /* @@ -397,18 +404,25 @@ static bool mountMmc() { auto cardBlockDevice = initializeMmcBlockDevice(); #if HAL_USE_USB_MSD + // Wait for the USB stack to wake up, or a 5 second timeout, whichever occurs first + msg_t usbResult = chBSemWaitTimeout(&usbConnectedSemaphore, TIME_MS2I(5000)); + + bool hasUsb = usbResult == MSG_OK; + msdObjectInit(&USBMSD1); - - if (cardBlockDevice) { + + // If we have a device AND USB is connected, mount the card to USB, otherwise + // mount the null device and try to mount the filesystem ourselves + if (cardBlockDevice && hasUsb) { // Mount the real card to USB msdStart(&USBMSD1, usb_driver, cardBlockDevice, blkbuf, &scsi_inquiry_response, NULL); + + // At this point we're done: don't try to write files ourselves + return false; } else { // Mount a "no media" device to USB msdMountNullDevice(&USBMSD1, usb_driver, blkbuf, &scsi_inquiry_response); } - - // TODO: local mount and log if USB not connected - return false; #endif // if no card, don't try to mount FS @@ -416,7 +430,7 @@ static bool mountMmc() { return false; } - // if Ok - mount FS now + // We were able to connect the SD card, mount the filesystem memset(&MMC_FS, 0, sizeof(FATFS)); if (f_mount(&MMC_FS, "/", 1) == FR_OK) { sdStatus = SD_STATE_MOUNTED; @@ -473,7 +487,12 @@ static SdLogBufferWriter logBuffer MAIN_RAM; static THD_FUNCTION(MMCmonThread, arg) { (void)arg; - chRegSetThreadName("MMC_Monitor"); + chRegSetThreadName("MMC Card Logger"); + + if (!mountMmc()) { + // no card present (or mounted via USB), don't do internal logging + return; + } while (true) { // if the SPI device got un-picked somehow, cancel SD card @@ -509,10 +528,9 @@ bool isSdCardAlive(void) { void initMmcCard(void) { logName[0] = 0; - if (!mountMmc()) { - // no card present, don't start thread - return; - } +#if HAL_USE_USB_MSD + chBSemObjectInit(&usbConnectedSemaphore, true); +#endif chThdCreateStatic(mmcThreadStack, sizeof(mmcThreadStack), LOWPRIO, (tfunc_t)(void*) MMCmonThread, NULL); diff --git a/firmware/hw_layer/mmc_card.h b/firmware/hw_layer/mmc_card.h index d0eddd5a7a..5ef1a0ecac 100644 --- a/firmware/hw_layer/mmc_card.h +++ b/firmware/hw_layer/mmc_card.h @@ -9,6 +9,7 @@ #pragma once +#ifdef __cplusplus #include "tunerstudio_io.h" #define DOT_MLG ".mlg" @@ -25,3 +26,7 @@ void handleTsW(ts_channel_s *tsChannel, char *input); extern spi_device_e mmcSpiDevice; #define LOCK_SD_SPI lockSpi(mmcSpiDevice) #define UNLOCK_SD_SPI unlockSpi(mmcSpiDevice) + +extern "C" +#endif +void onUsbConnectedNotifyMmcI(void); diff --git a/firmware/hw_layer/ports/stm32/serial_over_usb/usbcfg.c b/firmware/hw_layer/ports/stm32/serial_over_usb/usbcfg.c index 44845aa0e1..6923b7dd8a 100644 --- a/firmware/hw_layer/ports/stm32/serial_over_usb/usbcfg.c +++ b/firmware/hw_layer/ports/stm32/serial_over_usb/usbcfg.c @@ -17,6 +17,7 @@ #include "global.h" #include "os_access.h" +#include "mmc_card.h" #if HAL_USE_SERIAL_USB @@ -376,6 +377,11 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { /* Resetting the state of the CDC subsystem.*/ sduConfigureHookI(&SDU1); + #if HAL_USE_USB_MSD + // Tell the MMC thread to wake up and mount the card as a USB device + onUsbConnectedNotifyMmcI(); + #endif + chSysUnlockFromISR(); return; case USB_EVENT_RESET: