automatic switch between internal and MSD SD card (#2349)

* auto enable usb msd

* guard and turn on for proteus
This commit is contained in:
Matthew Kennedy 2021-02-14 05:10:39 -08:00 committed by GitHub
parent 56b7db8e9c
commit 89391919dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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: