Procházet zdrojové kódy

Report Speed Grade to log

Reports speed grade to user log and uses the g_scsi_setting to
store the speed grade.
Morio před 9 měsíci
rodič
revize
0c136e1bb7

+ 2 - 5
lib/ZuluSCSI_platform_GD32F205/ZuluSCSI_platform.h

@@ -122,11 +122,8 @@ uint8_t platform_get_buttons();
 // Return system clock in Hz
 uint32_t platform_sys_clock_in_hz();
 
-// Attempt to reclock the MCU - unsupported
-inline zuluscsi_reclock_status_t platform_reclock(zuluscsi_speed_grade_t speed_grade){return ZULUSCSI_RECLOCK_NOT_SUPPORTED;}
-
-// match string to speed grade - unsupported
-inline zuluscsi_speed_grade_t platform_string_to_speed_grade(const char *speed_grade_str, size_t length){return SPEED_GRADE_DEFAULT;}
+// Return whether device supports reclocking the MCU
+inline bool platform_reclock_supported(){return false;}
 
 // Returns true if reboot was for mass storage - unsupported
 inline bool platform_rebooted_into_mass_storage() {return false;}

+ 2 - 7
lib/ZuluSCSI_platform_GD32F450/ZuluSCSI_platform.h

@@ -113,17 +113,12 @@ uint8_t platform_get_buttons();
 
 uint32_t platform_sys_clock_in_hz();
 
-// Attempt to reclock the MCU - unsupported
-inline zuluscsi_reclock_status_t platform_reclock(zuluscsi_speed_grade_t speed_grade){return ZULUSCSI_RECLOCK_NOT_SUPPORTED;}
-
-// match string to speed grade - unsupported
-inline zuluscsi_speed_grade_t platform_string_to_speed_grade(const char *speed_grade_str, size_t length){return SPEED_GRADE_DEFAULT;}
+// Return whether device supports reclocking the MCU
+inline bool platform_reclock_supported(){return false;}
 
 // Returns true if reboot was for mass storage - unsupported
 inline bool platform_rebooted_into_mass_storage() {return false;}
 
-
-
 // Reinitialize SD card connection and save log from interrupt context.
 // This can be used in crash handlers.
 void platform_emergency_log_save();

+ 33 - 54
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform.cpp

@@ -40,6 +40,7 @@
 #include "custom_timings.h"
 #include <ZuluSCSI_settings.h>
 
+
 #ifndef PIO_FRAMEWORK_ARDUINO_NO_USB
 # include <SerialUSB.h>
 # include <class/cdc/cdc_device.h>
@@ -70,6 +71,9 @@ static bool g_scsi_initiator = false;
 static uint32_t g_flash_chip_size = 0;
 static bool g_uart_initialized = false;
 static bool g_led_blinking = false;
+
+static void usb_log_poll();
+
 /***************/
 /* GPIO init   */
 /***************/
@@ -132,69 +136,43 @@ uint32_t platform_sys_clock_in_hz()
     return clock_get_hz(clk_sys);
 }
 
-zuluscsi_speed_grade_t platform_string_to_speed_grade(const char *speed_grade_str, size_t length)
-{
-    static const char sg_default[] = "Default";
-    zuluscsi_speed_grade_t grade;
-
-#ifdef ENABLE_AUDIO_OUTPUT
-    logmsg("Audio output enabled, reclocking isn't possible");
-    return SPEED_GRADE_DEFAULT;
-#endif
-
-    if (strcasecmp(speed_grade_str, sg_default) == 0)
-      grade = SPEED_GRADE_DEFAULT;
-    else if (strcasecmp(speed_grade_str, "TurboMax") == 0)
-      grade = SPEED_GRADE_MAX;
-    else if (strcasecmp(speed_grade_str, "TurboA") == 0)
-      grade = SPEED_GRADE_A;
-    else if (strcasecmp(speed_grade_str, "TurboB") == 0)
-      grade = SPEED_GRADE_B;
-    else if (strcasecmp(speed_grade_str, "TurboC") == 0)
-      grade = SPEED_GRADE_C;
-    else if (strcasecmp(speed_grade_str, "Custom") == 0)
-      grade = SPEED_GRADE_CUSTOM;
-    else
-    {
-      logmsg("Setting \"", speed_grade_str, "\" does not match any know speed grade, using default");
-      grade = SPEED_GRADE_DEFAULT;
-    }
-    return grade;
-}
-
-zuluscsi_reclock_status_t platform_reclock(zuluscsi_speed_grade_t speed_grade)
+bool platform_reclock(zuluscsi_speed_grade_t speed_grade)
 {
     CustomTimings ct;
-    if (speed_grade == SPEED_GRADE_CUSTOM)
+    bool do_reclock = false;
+    if (speed_grade != SPEED_GRADE_DEFAULT)
     {
-        if (ct.use_custom_timings())
+        if (speed_grade == SPEED_GRADE_CUSTOM)
         {
-            logmsg("Custom timings found in \"", CUSTOM_TIMINGS_FILE, "\" overriding reclocking");
-            logmsg("Initial Clock set to ", (int) platform_sys_clock_in_hz(), "Hz");
-            if (ct.set_timings_from_file())
+            if (ct.use_custom_timings())
             {
-                reclock();
-                logmsg("SDIO clock set to ", (int)((g_zuluscsi_timings->clk_hz / g_zuluscsi_timings->sdio.clk_div_pio + (5 * MHZ / 10)) / MHZ) , "MHz");
-                return ZULUSCSI_RECLOCK_CUSTOM;
+
+                logmsg("Using custom timings found in \"", CUSTOM_TIMINGS_FILE, "\" for reclocking");
+                ct.set_timings_from_file();
+                do_reclock = true;
             }
             else
-                return ZULUSCSI_RECLOCK_FAILED;
+            {
+                logmsg("Custom timings file, \"", CUSTOM_TIMINGS_FILE, "\" not found or disabled");
+            }
         }
-        else
+        else if (set_timings(speed_grade))
+            do_reclock = true;
+
+        if (do_reclock)
         {
-            logmsg("Custom timings file, \"", CUSTOM_TIMINGS_FILE, "\" not found or disabled");
-            return ZULUSCSI_RECLOCK_FAILED;
+            logmsg("Initial Clock set to ", (int) platform_sys_clock_in_hz(), "Hz");
+            logmsg("Attempting reclock the MCU to ",(int) g_zuluscsi_timings->clk_hz, "Hz");
+            logmsg("Attempting to set SDIO clock to ", (int)((g_zuluscsi_timings->clk_hz / g_zuluscsi_timings->sdio.clk_div_pio + (5 * MHZ / 10)) / MHZ) , "MHz");
+            usb_log_poll();
+            reclock();
+            logmsg("After reclocking, system reports clock set to ", (int) platform_sys_clock_in_hz(), "Hz");
         }
-
-    }
-    else if (set_timings(speed_grade))
-    {
-        logmsg("Initial Clock set to ", (int) platform_sys_clock_in_hz(), "Hz");
-        reclock();
-        logmsg("SDIO clock set to ", (int)((g_zuluscsi_timings->clk_hz / g_zuluscsi_timings->sdio.clk_div_pio + (5 * MHZ / 10)) / MHZ) , "MHz");
-        return ZULUSCSI_RECLOCK_SUCCESS;
     }
-    return ZULUSCSI_RECLOCK_FAILED;
+    else
+        logmsg("Speed grade is set to default, reclocking skipped");
+
+    return do_reclock;
 }
 
 bool platform_rebooted_into_mass_storage()
@@ -355,9 +333,10 @@ void platform_init()
 
 #ifdef ENABLE_AUDIO_OUTPUT
     logmsg("SP/DIF audio to expansion header enabled");
-    if (platform_reclock(SPEED_GRADE_AUDIO) == ZULUSCSI_RECLOCK_SUCCESS)
+    logmsg("Reclocking MCU for audio timings");
+    if (platform_reclock(SPEED_GRADE_AUDIO))
     {
-        logmsg("Reclocked for Audio Ouput at ", (int) platform_sys_clock_in_hz(), "Hz");
+        logmsg("Reclocked for Audio Ouput finished");
     }
     else
     {

+ 8 - 5
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform.h

@@ -27,6 +27,7 @@
 #include <Arduino.h>
 #include "ZuluSCSI_config.h"
 #include "ZuluSCSI_platform_network.h"
+#include <ZuluSCSI_settings.h>
 
 #ifdef ZULUSCSI_PICO
 // ZuluSCSI Pico carrier board variant
@@ -131,11 +132,13 @@ uint8_t platform_get_buttons();
 
 uint32_t platform_sys_clock_in_hz();
 
-// Attempt to reclock the MCU
-zuluscsi_reclock_status_t platform_reclock(zuluscsi_speed_grade_t speed_grade);
+// Return whether device supports reclocking the MCU
+inline bool platform_reclock_supported(){return true;}
 
-// convert string to speed grade
-zuluscsi_speed_grade_t platform_string_to_speed_grade(const char *speed_grade_str, size_t length);
+#ifdef RECLOCKING_SUPPORTED
+// reclock the MCU
+bool platform_reclock(zuluscsi_speed_grade_t speed_grade);
+#endif
 
 // Returns true if reboot was for mass storage
 bool platform_rebooted_into_mass_storage();
@@ -151,8 +154,8 @@ void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
 #define PLATFORM_FLASH_TOTAL_SIZE (1024 * 1024)
 #define PLATFORM_FLASH_PAGE_SIZE 4096
 bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE]);
-void platform_boot_to_main_firmware();
 #endif
+void platform_boot_to_main_firmware();
 
 // ROM drive in the unused external flash area
 #ifndef RP2040_DISABLE_ROMDRIVE

+ 1 - 1
lib/ZuluSCSI_platform_RP2MCU/custom_timings.cpp

@@ -72,7 +72,7 @@ bool CustomTimings::set_timings_from_file()
 
     char speed_grade_str[10];
     ini_gets(settings_section, "extends_speed_grade", "Default", speed_grade_str, sizeof(speed_grade_str), CUSTOM_TIMINGS_FILE);
-    zuluscsi_speed_grade_t speed_grade =  platform_string_to_speed_grade(speed_grade_str, sizeof(speed_grade_str));
+    zuluscsi_speed_grade_t speed_grade =  g_scsi_settings.stringToSpeedGrade(speed_grade_str, sizeof(speed_grade_str));
     set_timings(speed_grade);
 
     int32_t number_setting = ini_getl(settings_section, "boot_with_sync_value", 0, CUSTOM_TIMINGS_FILE);

+ 3 - 4
lib/ZuluSCSI_platform_RP2MCU/custom_timings.h

@@ -22,10 +22,9 @@
 
 #define CUSTOM_TIMINGS_FILE "zuluscsi_timings.ini"
 
-extern "C"
-{
-    #include "timings_RP2MCU.h"
-}
+
+#include "timings_RP2MCU.h"
+
 
 class CustomTimings
 {

+ 0 - 1
lib/ZuluSCSI_platform_RP2MCU/scsi2sd_timings.c

@@ -19,7 +19,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/
 #include "timings.h"
-#include "ZuluSCSI_platform.h"
 #if defined(ZULUSCSI_MCU_RP23XX)
 uint8_t g_max_sync_20_period = 18;
 uint8_t g_max_sync_10_period = 25;

+ 14 - 1
lib/ZuluSCSI_platform_RP2MCU/timings_RP2MCU.h

@@ -20,9 +20,17 @@
 **/
 #ifndef ZULUSCSI_TIMINGS_RP2MCU_H
 #define ZULUSCSI_TIMINGS_RP2MCU_H
+
+#include <ZuluSCSI_settings.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
 #include <stdint.h>
 #include <stdbool.h>
-#include <ZuluSCSI_config.h>
+
 
 typedef struct
 {
@@ -105,4 +113,9 @@ extern  zuluscsi_timings_t *g_zuluscsi_timings;
 
 // Sets timings to the speed_grade, returns false on SPEED_GRADE_DEFAULT and SPEED_GRADE_CUSTOM
 bool set_timings(zuluscsi_speed_grade_t speed_grade);
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif // ZULUSCSI_TIMINGS_RP2MCU_H

+ 1 - 0
platformio.ini

@@ -136,6 +136,7 @@ build_flags =
     -DPICO_FLASH_SPI_CLKDIV=2
     -DPLATFORM_MASS_STORAGE
     -DFILE_COPY_CONSTRUCTOR_SELECT=FILE_COPY_CONSTRUCTOR_PUBLIC
+    -DRECLOCKING_SUPPORTED
 ; build flags mirroring the "framework-arduinopico#x.x.x-DaynaPORT" static library build
     -DPICO_CYW43_ARCH_POLL=1
 	-DCYW43_LWIP=0

+ 22 - 25
src/ZuluSCSI.cpp

@@ -1009,33 +1009,10 @@ static void zuluscsi_setup_sd_card(bool wait_for_card = true)
 
   firmware_update();
 
-  static const char sg_default[] = "Default";
+
   if (g_sdcard_present)
   {
-    char speed_grade_str[10];
-    ini_gets("SCSI", "SpeedGrade", sg_default, speed_grade_str, sizeof(speed_grade_str), CONFIGFILE);
-    zuluscsi_speed_grade_t grade = platform_string_to_speed_grade(speed_grade_str, sizeof(speed_grade_str));
-    if (grade != SPEED_GRADE_DEFAULT)
-    {
-      zuluscsi_reclock_status_t status = platform_reclock(grade);
-      switch (status)
-      {
-        case ZULUSCSI_RECLOCK_NOT_SUPPORTED:
-          logmsg("Reclocking this board is not supported");
-          break;
-        case ZULUSCSI_RECLOCK_FAILED:
-          logmsg("Reclocking failed");
-          break;
-        case ZULUSCSI_RECLOCK_SUCCESS:
-          logmsg("Reclocking was successful");
-          break;
-        case ZULUSCSI_RECLOCK_CUSTOM:
-          logmsg("Custom reclocking timings used");
-          break;
-      }
-      g_sdcard_present = mountSDCard();
-      reinitSCSI();
-    }
+
 
     if (SD.clusterCount() == 0)
     {
@@ -1047,6 +1024,26 @@ static void zuluscsi_setup_sd_card(bool wait_for_card = true)
     char presetName[32];
     ini_gets("SCSI", "System", "", presetName, sizeof(presetName), CONFIGFILE);
     scsi_system_settings_t *cfg = g_scsi_settings.initSystem(presetName);
+
+#ifdef RECLOCKING_SUPPORTED
+    zuluscsi_speed_grade_t speed_grade = (zuluscsi_speed_grade_t) g_scsi_settings.getSystem()->speedGrade;
+    if (speed_grade != zuluscsi_speed_grade_t::SPEED_GRADE_DEFAULT)
+    { 
+      logmsg("Speed grade set to ", g_scsi_settings.getSpeedGradeString(), " reclocking system");
+      if (platform_reclock(speed_grade))
+      {
+        logmsg("======== Reinitializing ZuluSCSI after reclock ========");
+        g_sdcard_present = mountSDCard();
+      }
+    }
+    else
+    {
+#ifndef ENABLE_AUDIO_OUTPUT // if audio is enabled, skip message because reclocking ocurred earlier
+      logmsg("Speed grade set to Default, skipping reclocking");
+#endif
+    }
+#endif
+
     int boot_delay_ms = cfg->initPreDelay;
     if (boot_delay_ms > 0)
     {

+ 0 - 20
src/ZuluSCSI_config.h

@@ -24,7 +24,6 @@
 // Other settings can be set by ini file at runtime.
 
 #pragma once
-
 #include <ZuluSCSI_platform_config.h>
 
 // Use variables for version number
@@ -136,22 +135,3 @@
 // Settings for rebooting
 #define REBOOT_INTO_MASS_STORAGE_MAGIC_NUM 0x5eeded
 
-// Reclocking return status
-typedef enum
-{
-    ZULUSCSI_RECLOCK_SUCCESS,
-    ZULUSCSI_RECLOCK_CUSTOM,
-    ZULUSCSI_RECLOCK_NOT_SUPPORTED,
-    ZULUSCSI_RECLOCK_FAILED
-} zuluscsi_reclock_status_t;
-
-typedef enum
-{
-    SPEED_GRADE_DEFAULT,
-    SPEED_GRADE_MAX,
-    SPEED_GRADE_CUSTOM,
-    SPEED_GRADE_A,
-    SPEED_GRADE_B,
-    SPEED_GRADE_C,
-    SPEED_GRADE_AUDIO,
-} zuluscsi_speed_grade_t;

+ 61 - 0
src/ZuluSCSI_settings.cpp

@@ -37,6 +37,18 @@ ZuluSCSISettings g_scsi_settings;
 const char *systemPresetName[] = {"", "Mac", "MacPlus", "MPC3000", "MegaSTE", "X68000"};
 const char *devicePresetName[] = {"", "ST32430N"};
 
+// must be in the same order as zuluscsi_speed_grade_t in ZuluSCSI_settings.h
+const char * const speed_grade_strings[7] =
+{
+    "Default",
+    "TurboMax",
+    "Custom",
+    "TurboA",
+    "TurboB",
+    "TurboC",
+    "Audio"
+};
+
 // Helper function for case-insensitive string compare
 static bool strequals(const char *a, const char *b)
 {
@@ -302,6 +314,8 @@ scsi_system_settings_t *ZuluSCSISettings::initSystem(const char *presetName)
     cfgSys.usbMassStoragePresentImages = false;
     cfgSys.invertStatusLed = false;
 
+    cfgSys.speedGrade = zuluscsi_speed_grade_t::SPEED_GRADE_DEFAULT;
+
     // setting set for all or specific devices
     cfgDev.deviceType = S2S_CFG_NOT_SET;
     cfgDev.deviceTypeModifier = 0;
@@ -400,6 +414,20 @@ scsi_system_settings_t *ZuluSCSISettings::initSystem(const char *presetName)
     cfgSys.usbMassStoragePresentImages = ini_getbool("SCSI", "USBMassStoragePresentImages", cfgSys.usbMassStoragePresentImages, CONFIGFILE);
 
     cfgSys.invertStatusLed = ini_getbool("SCSI", "InvertStatusLED", cfgSys.invertStatusLed, CONFIGFILE);
+    
+    char tmp[32];
+    ini_gets("SCSI", "SpeedGrade", "", tmp, sizeof(tmp), CONFIGFILE);
+    if (tmp[0] != '\0')
+    {
+        if (platform_reclock_supported())
+        {
+            cfgSys.speedGrade = stringToSpeedGrade(tmp, sizeof(tmp));
+        }
+        else
+        {
+            logmsg("Speed grade setting ignored, reclocking the MCU is not supported by this device");
+        }
+    }
 
     return &cfgSys;
 }
@@ -492,3 +520,36 @@ const char* ZuluSCSISettings::getDevicePresetName(uint8_t scsiId)
 {
     return devicePresetName[m_devPreset[scsiId]];
 }
+
+
+zuluscsi_speed_grade_t ZuluSCSISettings::stringToSpeedGrade(const char *speed_grade_target, size_t length)
+{
+    zuluscsi_speed_grade_t grade = zuluscsi_speed_grade_t::SPEED_GRADE_DEFAULT;
+
+#ifdef ENABLE_AUDIO_OUTPUT
+    logmsg("Audio output enabled, reclocking isn't possible");
+    return SPEED_GRADE_DEFAULT;
+#endif
+    bool found_speed_grade = false;
+    // search the list of speed grade strings for a matching target
+    for (uint8_t i = 0; i < sizeof(speed_grade_strings)/sizeof(speed_grade_strings[0]); i++)
+    {
+        if (strncasecmp(speed_grade_target, speed_grade_strings[i], length) == 0)
+        {
+            grade = (zuluscsi_speed_grade_t)i;
+            found_speed_grade = true;
+            break;
+        }
+    }
+    if (!found_speed_grade)
+    {
+      logmsg("Setting \"", speed_grade_target, "\" does not match any known speed grade, using default");
+      grade = SPEED_GRADE_DEFAULT;
+    }
+    return grade;
+}
+
+const char *ZuluSCSISettings::getSpeedGradeString()
+{
+    return speed_grade_strings[m_sys.speedGrade];
+}

+ 24 - 0
src/ZuluSCSI_settings.h

@@ -20,11 +20,26 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/
 #pragma once
+
+typedef enum
+{
+    SPEED_GRADE_DEFAULT = 0,
+    SPEED_GRADE_MAX,
+    SPEED_GRADE_CUSTOM,
+    SPEED_GRADE_AUDIO,
+    SPEED_GRADE_A,
+    SPEED_GRADE_B,
+    SPEED_GRADE_C,
+
+} zuluscsi_speed_grade_t;
+
 #ifdef __cplusplus
 
 #include <stdint.h>
+#include <stddef.h>
 #include <scsi2sd.h>
 
+
 // Index 8 is the system defaults
 // Index 0-7 represent device settings
 #define SCSI_SETTINGS_SYS_IDX 8
@@ -71,6 +86,8 @@ typedef struct __attribute__((__packed__)) scsi_system_settings_t
     
     bool invertStatusLed;
 
+    uint8_t speedGrade; // memory allocation for zuluscsi_speed_grade_t enum
+
 } scsi_system_settings_t;
 
 // This struct should only have new setting added to the end
@@ -136,6 +153,11 @@ public:
     // return the device preset name
     const char* getDevicePresetName(uint8_t scsiId);
 
+    // convert string to speed grade
+    zuluscsi_speed_grade_t stringToSpeedGrade(const char *speed_grade_str, size_t length);
+
+    const char* getSpeedGradeString();
+
 protected:
     // Set default drive vendor / product info after the image file
     // is loaded and the device type is known.
@@ -158,5 +180,7 @@ protected:
     scsi_device_settings_t m_dev[9];
 } ;
 
+
+
 extern ZuluSCSISettings g_scsi_settings;
 #endif // __cplusplus