Skip to content

Commit

Permalink
Rerun the setup after USB MSC device is ejected
Browse files Browse the repository at this point in the history
Rerun setup once the USB mass storage device is ejected.
This is done because the `zuluscsi.ini` file may be edited
over the mass storage interface on the USB host computer.
Or SCSI device images may have been renamed, deleted, or uploaded.

Added the Copyright headers for zigzagjoe contributions.
Renamed card reader function names and log entries to mass storage
device.
  • Loading branch information
morio committed Dec 11, 2023
1 parent bdccb4a commit 2450cff
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/ZuluSCSI_platform_RP2040/msc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* This file is part of the TinyUSB stack.
*/
#ifdef PLATFORM_CARDREADER
#ifdef PLATFORM_MASS_STORAGE

#include "tusb_option.h"

Expand Down Expand Up @@ -951,4 +951,4 @@ static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint3
}

#endif
#endif // PLATFORM_CARDREADER
#endif // PLATFORM_MASS_STORAGE
34 changes: 27 additions & 7 deletions lib/ZuluSCSI_platform_RP2040/rp_msc_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
/* TODO - Header. By ZZJ. */
#ifdef PLATFORM_CARDREADER
/**
* Copyright (c) 2023 zigzagjoe
*
* ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
*
* https://www.gnu.org/licenses/gpl-3.0.html
* ----
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. 
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details. 
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <https://www.gnu.org/licenses/>.
**/

#ifdef PLATFORM_MASS_STORAGE

#include <SdFat.h>
#include <device/usbd.h>
Expand Down Expand Up @@ -56,7 +76,7 @@ volatile bool unitReady = false;

// returns true if card reader mode should be entered
// this probably become part of zuluscsi proper, if this code is upstreamed
bool shouldEnterReader() {
bool shouldStartMassStorage() {
#ifdef ZULUSCSI_PICO
// check if we're USB powered, if not, exit immediately
// pin on the wireless module, see https://github.com/earlephilhower/arduino-pico/discussions/835
Expand All @@ -78,7 +98,7 @@ bool shouldEnterReader() {

// card reader operation loop
// assumption that SD card was enumerated and is working
void runCardReader() {
void startMassStorage() {

// blink twice to indicate entering card reader mode.
LED_ON();
Expand All @@ -89,7 +109,7 @@ void runCardReader() {
delay(50);
LED_OFF();

logmsg("SD card reader mode entered.");
logmsg("SD card exposed as a mass storage device.");
logmsg("USB MSC buffer size: ", CFG_TUD_MSC_EP_BUFSIZE);

// MSC is ready for read/write
Expand Down Expand Up @@ -120,7 +140,7 @@ void runCardReader() {
delay(30);
}

logmsg("Card ejected, leaving cardreader mode.");
logmsg("Mass storage device ejected, booting ZuluSCSI as normal.");

// blink twice to indicate we are leaving reader mode
LED_OFF();
Expand Down Expand Up @@ -292,4 +312,4 @@ extern "C" void tud_msc_write10_complete_cb(uint8_t lun) {
SD.card()->syncDevice();
}

#endif // PLATFORM_CARDREADER
#endif // PLATFORM_MASS_STORAGE
31 changes: 25 additions & 6 deletions lib/ZuluSCSI_platform_RP2040/rp_msc_reader.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
/* TODO - Header. By ZZJ. */
#ifdef PLATFORM_CARDREADER
/**
* Copyright (c) 2023 zigzagjoe
*
* ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
*
* https://www.gnu.org/licenses/gpl-3.0.html
* ----
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. 
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details. 
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <https://www.gnu.org/licenses/>.
**/
#ifdef PLATFORM_MASS_STORAGE
#ifndef RP_MSC_READER_H
#define RP_MSC_READER_H

// wait up to this long during init sequence for USB enumeration to enter card reader
#define CR_ENUM_TIMEOUT 1000

// true if we should enter cardreader mode
bool shouldEnterReader();
// true if we should start the mass storage device mode
bool shouldStartMassStorage();

// run cardreader mode. does not return currently
void runCardReader();
// exposed the SD card as a mass storage device. does not return currently
void startMassStorage();

#endif
#endif
2 changes: 1 addition & 1 deletion lib/ZuluSCSI_platform_RP2040/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
#define CFG_TUD_CDC_EP_BUFSIZE (64)


#ifndef PLATFORM_CARDREADER
#ifndef PLATFORM_MASS_STORAGE
// standard value.
#define CFG_TUD_MSC_EP_BUFSIZE (64)
#else
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ build_flags =
-DCYW43_LWIP=0
-DCYW43_USE_OTP_MAC=0
; -DPIO_FRAMEWORK_ARDUINO_NO_USB
-DPLATFORM_CARDREADER
-DPLATFORM_MASS_STORAGE

; Variant of RP2040 platform, based on Raspberry Pico board and a carrier PCB
; Part of the ZuluSCSI_RP2040 platform, but with different pins.
Expand Down Expand Up @@ -234,7 +234,7 @@ build_flags =
-DCYW43_LWIP=0
-DCYW43_USE_OTP_MAC=0
; -DPIO_FRAMEWORK_ARDUINO_NO_USB
-DPLATFORM_CARDREADER
-DPLATFORM_MASS_STORAGE

; Variant of RP2040 platform, based on Raspberry Pico board and a carrier PCB
; Differs in pinout from ZuluSCSI_RP2040 platform, but shares most of the code.
Expand Down
31 changes: 18 additions & 13 deletions src/ZuluSCSI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* This work incorporates work by following
* Copyright (c) 2023 joshua stein <[email protected]>
* Copyright (c) 2023 zigzagjoe
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -751,19 +752,6 @@ extern "C" void zuluscsi_setup(void)
ini_gets("SCSI", "System", "", presetName, sizeof(presetName), CONFIGFILE);
scsi_system_settings_t *cfg = g_scsi_settings.initSystem(presetName);

#ifdef PLATFORM_CARDREADER
if (g_scsi_settings.getSystem()->enableUSBMassStorage)
{
// right now, we initialize early to allow config file edits to take effect immediately on eject
// perform checks (USB power, have we been enumerated, etc?) and waits up to a second for enumeration
if (shouldEnterReader())
{
logmsg("SD Card opened as a mass storage device");
runCardReader();
}
}
#endif

int boot_delay_ms = cfg->initPreDelay;
if (boot_delay_ms > 0)
{
Expand Down Expand Up @@ -802,6 +790,23 @@ extern "C" void zuluscsi_main_loop(void)
static uint32_t sd_card_check_time = 0;
static uint32_t last_request_time = 0;

#ifdef PLATFORM_MASS_STORAGE
static bool check_mass_storage = true;
if (check_mass_storage && g_scsi_settings.getSystem()->enableUSBMassStorage)
{
check_mass_storage = false;
// right now, we initialize early to allow config file edits to take effect immediately on eject
// perform checks (USB power, have we been enumerated, etc?) and waits up to a second for enumeration
if (shouldStartMassStorage())
{
logmsg("SD Card opened as a mass storage device");
startMassStorage();
zuluscsi_setup();
}
}
#endif


platform_reset_watchdog();
platform_poll();
diskEjectButtonUpdate(true);
Expand Down

0 comments on commit 2450cff

Please sign in to comment.