|
| 1 | +const int pin_microSD_CS = 25; |
| 2 | + |
| 3 | +typedef struct struct_settings { |
| 4 | + bool enableSD = true; |
| 5 | + uint16_t spiFrequency = 16; //By default, use 16MHz SPI |
| 6 | +} Settings; |
| 7 | + |
| 8 | +Settings settings; |
| 9 | + |
| 10 | +const TickType_t fatSemaphore_shortWait_ms = 10 / portTICK_PERIOD_MS; |
| 11 | +const TickType_t fatSemaphore_longWait_ms = 200 / portTICK_PERIOD_MS; |
| 12 | +SemaphoreHandle_t xFATSemaphore; |
| 13 | + |
| 14 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 15 | +void displaySDFail(uint16_t displayTime) |
| 16 | +{ |
| 17 | + Serial.println ("SD card failed to initialize!"); |
| 18 | +} |
| 19 | + |
| 20 | +//Create a test file in file structure to make sure we can |
| 21 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 22 | +bool createTestFile() |
| 23 | +{ |
| 24 | + SdFile testFile; |
| 25 | + char testFileName[40] = "testfile.txt"; |
| 26 | + |
| 27 | + if (xFATSemaphore == NULL) |
| 28 | + { |
| 29 | + log_d("xFATSemaphore is Null"); |
| 30 | + return (false); |
| 31 | + } |
| 32 | + |
| 33 | + //Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask() |
| 34 | + if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS) |
| 35 | + { |
| 36 | + if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) == true) |
| 37 | + { |
| 38 | + testFile.close(); |
| 39 | + |
| 40 | + if (sd.exists(testFileName)) |
| 41 | + sd.remove(testFileName); |
| 42 | + xSemaphoreGive(xFATSemaphore); |
| 43 | + return (true); |
| 44 | + } |
| 45 | + xSemaphoreGive(xFATSemaphore); |
| 46 | + } |
| 47 | + |
| 48 | + return (false); |
| 49 | +} |
| 50 | + |
| 51 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 52 | +void beginSD() |
| 53 | +{ |
| 54 | + pinMode(pin_microSD_CS, OUTPUT); |
| 55 | + digitalWrite(pin_microSD_CS, HIGH); //Be sure SD is deselected |
| 56 | + |
| 57 | + if (settings.enableSD == true) |
| 58 | + { |
| 59 | + //Do a quick test to see if a card is present |
| 60 | + int tries = 0; |
| 61 | + int maxTries = 5; |
| 62 | + while (tries < maxTries) |
| 63 | + { |
| 64 | + if (sdPresent() == true) break; |
| 65 | + log_d("SD present failed. Trying again %d out of %d", tries + 1, maxTries); |
| 66 | + |
| 67 | + //Max power up time is 250ms: https://www.kingston.com/datasheets/SDCIT-specsheet-64gb_en.pdf |
| 68 | + //Max current is 200mA average across 1s, peak 300mA |
| 69 | + delay(10); |
| 70 | + tries++; |
| 71 | + } |
| 72 | + if (tries == maxTries) return; |
| 73 | + |
| 74 | + //If an SD card is present, allow SdFat to take over |
| 75 | + log_d("SD card detected"); |
| 76 | + |
| 77 | + if (settings.spiFrequency > 16) |
| 78 | + { |
| 79 | + Serial.println("Error: SPI Frequency out of range. Default to 16MHz"); |
| 80 | + settings.spiFrequency = 16; |
| 81 | + } |
| 82 | + |
| 83 | + if (sd.begin(SdSpiConfig(pin_microSD_CS, SHARED_SPI, SD_SCK_MHZ(settings.spiFrequency))) == false) |
| 84 | + { |
| 85 | + tries = 0; |
| 86 | + maxTries = 1; |
| 87 | + for ( ; tries < maxTries ; tries++) |
| 88 | + { |
| 89 | + log_d("SD init failed. Trying again %d out of %d", tries + 1, maxTries); |
| 90 | + |
| 91 | + delay(250); //Give SD more time to power up, then try again |
| 92 | + if (sd.begin(SdSpiConfig(pin_microSD_CS, SHARED_SPI, SD_SCK_MHZ(settings.spiFrequency))) == true) break; |
| 93 | + } |
| 94 | + |
| 95 | + if (tries == maxTries) |
| 96 | + { |
| 97 | + Serial.println(F("SD init failed. Is card present? Formatted?")); |
| 98 | + digitalWrite(pin_microSD_CS, HIGH); //Be sure SD is deselected |
| 99 | + online.microSD = false; |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //Change to root directory. All new file creation will be in root. |
| 105 | + if (sd.chdir() == false) |
| 106 | + { |
| 107 | + Serial.println(F("SD change directory failed")); |
| 108 | + online.microSD = false; |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + //Setup FAT file access semaphore |
| 113 | + if (xFATSemaphore == NULL) |
| 114 | + { |
| 115 | + xFATSemaphore = xSemaphoreCreateMutex(); |
| 116 | + if (xFATSemaphore != NULL) |
| 117 | + xSemaphoreGive(xFATSemaphore); //Make the file system available for use |
| 118 | + } |
| 119 | + |
| 120 | + if (createTestFile() == false) |
| 121 | + { |
| 122 | + Serial.println(F("Failed to create test file. Format SD card with 'SD Card Formatter'.")); |
| 123 | + displaySDFail(5000); |
| 124 | + online.microSD = false; |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + online.microSD = true; |
| 129 | + |
| 130 | + Serial.println(F("microSD online")); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + online.microSD = false; |
| 135 | + } |
| 136 | +} |
0 commit comments