Parcourir la source

Try clock speeds from 50 -> 32mhz for SD communication
Helps compatibility with SD and different STM32 chips.
Add warning if under 40mhz - testing showed 40-50mhz gave almost no change in speed

Thanks @marcelv-3 via https://github.com/erichelgeson/BlueSCSI/discussions/58

Eric Helgeson il y a 3 ans
Parent
commit
551faf72b2
1 fichiers modifiés avec 22 ajouts et 6 suppressions
  1. 22 6
      src/BlueSCSI.cpp

+ 22 - 6
src/BlueSCSI.cpp

@@ -62,7 +62,6 @@
 #define MAX_BLOCKSIZE 2048     // Maximum BLOCK size
 
 // SDFAT
-#define SD1_CONFIG SdSpiConfig(PA4, DEDICATED_SPI, SD_SCK_MHZ(SPI_FULL_SPEED), &SPI)
 SdFs SD;
 
 #if DEBUG == 1
@@ -251,7 +250,7 @@ byte SCSI_INFO_BUF[36] = {
 void onFalseInit(void);
 void noSDCardFound(void);
 void onBusReset(void);
-void initFileLog(void);
+void initFileLog(int);
 void finalizeFileLog(void);
 
 /*
@@ -421,16 +420,27 @@ void setup()
   //Occurs when the RST pin state changes from HIGH to LOW
   //attachInterrupt(RST, onBusReset, FALLING);
 
+  // Try different clock speeds till we find one that is stable.
   LED_ON();
+  int mhz = 50;
+  bool sd_ready = false;
+  while (mhz >= 32 && !sd_ready) {
+    if(SD.begin(SdSpiConfig(PA4, DEDICATED_SPI, SD_SCK_MHZ(mhz), &SPI))) {
+      sd_ready = true;
+    }
+    else {
+      mhz--;
+    }
+  }
+  LED_OFF();
 
-  // clock = 36MHz , about 4Mbytes/sec
-  if(!SD.begin(SD1_CONFIG)) {
+  if(!sd_ready) {
 #if DEBUG > 0
     Serial.println("SD initialization failed!");
 #endif
     noSDCardFound();
   }
-  initFileLog();
+  initFileLog(mhz);
   readSCSIDeviceConfig();
   readSDCardInfo();
 
@@ -528,7 +538,7 @@ void setup()
 /*
  * Setup initialization logfile
  */
-void initFileLog() {
+void initFileLog(int success_mhz) {
   LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
   LOG_FILE.println("BlueSCSI <-> SD - https://github.com/erichelgeson/BlueSCSI");
   LOG_FILE.print("VERSION: ");
@@ -541,6 +551,12 @@ void initFileLog() {
   LOG_FILE.println(SDFAT_FILE_TYPE);
   LOG_FILE.print("SdFat version: ");
   LOG_FILE.println(SD_FAT_VERSION_STR);
+  LOG_FILE.print("SPI speed: ");
+  LOG_FILE.print(success_mhz);
+  LOG_FILE.println("Mhz");
+  if(success_mhz < 40) {
+    LOG_FILE.println("SPI under 40Mhz - read https://github.com/erichelgeson/BlueSCSI/wiki/Slow-SPI");
+  }
   LOG_FILE.print("SdFat Max FileName Length: ");
   LOG_FILE.println(MAX_FILE_PATH);
   LOG_FILE.println("Initialized SD Card - lets go!");