|
| 1 | +#include "QSPIFBlockDevice.h" |
| 2 | +#include "MBRBlockDevice.h" |
| 3 | +#include "LittleFileSystem.h" |
| 4 | +#include "FATFileSystem.h" |
| 5 | + |
| 6 | +#ifndef CORE_CM7 |
| 7 | + #error Format QSPI flash by uploading the sketch to the M7 core instead of the M4 core. |
| 8 | +#endif |
| 9 | + |
| 10 | + |
| 11 | +QSPIFBlockDevice root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000); |
| 12 | +mbed::MBRBlockDevice wifi_data(&root, 1); |
| 13 | +mbed::MBRBlockDevice ota_data(&root, 2); |
| 14 | +mbed::MBRBlockDevice user_data(&root, 3); |
| 15 | +mbed::FATFileSystem wifi_data_fs("wlan"); |
| 16 | +mbed::FATFileSystem ota_data_fs("fs"); |
| 17 | +mbed::FileSystem * user_data_fs; |
| 18 | + |
| 19 | +bool waitResponse() { |
| 20 | + bool confirmation = false; |
| 21 | + while (confirmation == false) { |
| 22 | + if (Serial.available()) { |
| 23 | + char choice = Serial.read(); |
| 24 | + switch (choice) { |
| 25 | + case 'y': |
| 26 | + case 'Y': |
| 27 | + confirmation = true; |
| 28 | + return true; |
| 29 | + break; |
| 30 | + case 'n': |
| 31 | + case 'N': |
| 32 | + confirmation = true; |
| 33 | + return false; |
| 34 | + break; |
| 35 | + default: |
| 36 | + continue; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void setup() { |
| 43 | + |
| 44 | + Serial.begin(115200); |
| 45 | + while (!Serial); |
| 46 | + |
| 47 | + Serial.println("Available partition schemes:"); |
| 48 | + Serial.println("\nPartition scheme 1"); |
| 49 | + Serial.println("Partition 1: WiFi firmware and certificates 1MB"); |
| 50 | + Serial.println("Partition 2: OTA and user data 14MB"); |
| 51 | + Serial.println("\nPartition scheme 2"); |
| 52 | + Serial.println("Partition 1: WiFi firmware and certificates 1MB"); |
| 53 | + Serial.println("Partition 2: OTA 5MB"); |
| 54 | + Serial.println("Partition 3: User data 9MB"), |
| 55 | + Serial.println("\nDo you want to use partition scheme 1? Y/[n]"); |
| 56 | + Serial.println("If No, partition scheme 2 will be used."); |
| 57 | + bool default_scheme = waitResponse(); |
| 58 | + |
| 59 | + Serial.println("\nWARNING! Running the sketch all the content of the QSPI flash will be erased."); |
| 60 | + Serial.println("Do you want to proceed? Y/[n]"); |
| 61 | + |
| 62 | + if (true == waitResponse()) { |
| 63 | + mbed::MBRBlockDevice::partition(&root, 1, 0x0B, 0, 1024 * 1024); |
| 64 | + if(default_scheme) { |
| 65 | + mbed::MBRBlockDevice::partition(&root, 3, 0x0B, 6 * 1024 * 1024, 0); |
| 66 | + mbed::MBRBlockDevice::partition(&root, 2, 0x0B, 1024 * 1024, 14 * 1024 * 1024); |
| 67 | + // use space from 15.5MB to 16 MB for another fw, memory mapped |
| 68 | + } else { |
| 69 | + mbed::MBRBlockDevice::partition(&root, 2, 0x0B, 1024 * 1024, 5 * 1024 * 1024); |
| 70 | + mbed::MBRBlockDevice::partition(&root, 3, 0x0B, 6 * 1024 * 1024, 9 * 1024 * 1024); |
| 71 | + // use space from 15.5MB to 16 MB for another fw, memory mapped |
| 72 | + } |
| 73 | + |
| 74 | + int err = wifi_data_fs.reformat(&wifi_data); |
| 75 | + if (err) { |
| 76 | + Serial.println("Error formatting WiFi partition"); |
| 77 | + } |
| 78 | + |
| 79 | + err = ota_data_fs.reformat(&ota_data); |
| 80 | + if (err) { |
| 81 | + Serial.println("Error formatting OTA partition"); |
| 82 | + } |
| 83 | + |
| 84 | + if(!default_scheme) { |
| 85 | + Serial.println("\nDo you want to use LittleFS to format user data partition? Y/[n]"); |
| 86 | + Serial.println("If No, FatFS will be used to format user partition."); |
| 87 | + |
| 88 | + if (true == waitResponse()) { |
| 89 | + Serial.println("Formatting user partition with LittleFS."); |
| 90 | + user_data_fs = new mbed::LittleFileSystem("user"); |
| 91 | + } else { |
| 92 | + Serial.println("Formatting user partition with FatFS."); |
| 93 | + user_data_fs = new mbed::FATFileSystem("user"); |
| 94 | + } |
| 95 | + |
| 96 | + err = user_data_fs->reformat(&user_data); |
| 97 | + if (err) { |
| 98 | + Serial.println("Error formatting user partition"); |
| 99 | + } |
| 100 | + } |
| 101 | + Serial.println("\nQSPI Flash formatted!"); |
| 102 | + } |
| 103 | + |
| 104 | + Serial.println("It's now safe to reboot or disconnect your board."); |
| 105 | +} |
| 106 | + |
| 107 | +void loop() { |
| 108 | + |
| 109 | +} |
0 commit comments