PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

85 lines
2.3KB

  1. #include <SerialFlash.h>
  2. #include <SPI.h>
  3. const int FlashChipSelect = 6; // digital pin for flash chip CS pin
  4. //const int FlashChipSelect = 21; // Arduino 101 built-in SPI Flash
  5. SerialFlashFile file;
  6. const unsigned long testIncrement = 4096;
  7. void setup() {
  8. //uncomment these if using Teensy audio shield
  9. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  10. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  11. //uncomment these if you have other SPI chips connected
  12. //to keep them disabled while using only SerialFlash
  13. //pinMode(4, INPUT_PULLUP);
  14. //pinMode(10, INPUT_PULLUP);
  15. Serial.begin(9600);
  16. // wait up to 10 seconds for Arduino Serial Monitor
  17. unsigned long startMillis = millis();
  18. while (!Serial && (millis() - startMillis < 10000)) ;
  19. delay(100);
  20. if (!SerialFlash.begin(FlashChipSelect)) {
  21. while (1) {
  22. Serial.println("Unable to access SPI Flash chip");
  23. delay(1000);
  24. }
  25. }
  26. unsigned char id[5];
  27. SerialFlash.readID(id);
  28. unsigned long size = SerialFlash.capacity(id);
  29. if (size > 0) {
  30. Serial.print("Flash Memory has ");
  31. Serial.print(size);
  32. Serial.println(" bytes.");
  33. Serial.println("Erasing ALL Flash Memory:");
  34. // Estimate the (lengthy) wait time.
  35. Serial.print(" estimated wait: ");
  36. int seconds = (float)size / eraseBytesPerSecond(id) + 0.5;
  37. Serial.print(seconds);
  38. Serial.println(" seconds.");
  39. Serial.println(" Yes, full chip erase is SLOW!");
  40. SerialFlash.eraseAll();
  41. unsigned long dotMillis = millis();
  42. unsigned char dotcount = 0;
  43. while (SerialFlash.ready() == false) {
  44. if (millis() - dotMillis > 1000) {
  45. dotMillis = dotMillis + 1000;
  46. Serial.print(".");
  47. dotcount = dotcount + 1;
  48. if (dotcount >= 60) {
  49. Serial.println();
  50. dotcount = 0;
  51. }
  52. }
  53. }
  54. if (dotcount > 0) Serial.println();
  55. Serial.println("Erase completed");
  56. unsigned long elapsed = millis() - startMillis;
  57. Serial.print(" actual wait: ");
  58. Serial.print(elapsed / 1000ul);
  59. Serial.println(" seconds.");
  60. }
  61. }
  62. float eraseBytesPerSecond(const unsigned char *id) {
  63. if (id[0] == 0x20) return 152000.0; // Micron
  64. if (id[0] == 0x01) return 500000.0; // Spansion
  65. if (id[0] == 0xEF) return 419430.0; // Winbond
  66. if (id[0] == 0xC2) return 279620.0; // Macronix
  67. return 320000.0; // guess?
  68. }
  69. void loop() {
  70. }