PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 line
4.5KB

  1. // **********************************************************************************
  2. // This sketch is an example of using the SPIFlash library with a Moteino
  3. // that has an onboard SPI Flash chip. This sketch listens to a few serial commands
  4. // Hence type the following commands to interact with the SPI flash memory array:
  5. // - 'd' dumps the first 256bytes of the flash chip to screen
  6. // - 'e' erases the entire memory chip
  7. // - 'i' print manufacturer/device ID
  8. // - [0-9] writes a random byte to addresses [0-9] (either 0xAA or 0xBB)
  9. // Get the SPIFlash library from here: https://github.com/LowPowerLab/SPIFlash
  10. // **********************************************************************************
  11. // Copyright Felix Rusu, LowPowerLab.com
  12. // Library and code by Felix Rusu - felix@lowpowerlab.com
  13. // **********************************************************************************
  14. // License
  15. // **********************************************************************************
  16. // This program is free software; you can redistribute it
  17. // and/or modify it under the terms of the GNU General
  18. // Public License as published by the Free Software
  19. // Foundation; either version 3 of the License, or
  20. // (at your option) any later version.
  21. //
  22. // This program is distributed in the hope that it will
  23. // be useful, but WITHOUT ANY WARRANTY; without even the
  24. // implied warranty of MERCHANTABILITY or FITNESS FOR A
  25. // PARTICULAR PURPOSE. See the GNU General Public
  26. // License for more details.
  27. //
  28. // You should have received a copy of the GNU General
  29. // Public License along with this program.
  30. // If not, see <http://www.gnu.org/licenses/>.
  31. //
  32. // Licence can be viewed at
  33. // http://www.gnu.org/licenses/gpl-3.0.txt
  34. //
  35. // Please maintain this license information along with authorship
  36. // and copyright notices in any redistribution of this code
  37. // **********************************************************************************
  38. #include <SPIFlash.h> //get it here: https://github.com/LowPowerLab/SPIFlash
  39. #include <SPI.h>
  40. #define SERIAL_BAUD 115200
  41. char input = 0;
  42. long lastPeriod = -1;
  43. #ifdef __AVR_ATmega1284P__
  44. #define LED 15 // Moteino MEGAs have LEDs on D15
  45. #define FLASH_SS 23 // and FLASH SS on D23
  46. #else
  47. #define LED 9 // Moteinos have LEDs on D9
  48. #define FLASH_SS 8 // and FLASH SS on D8
  49. #endif
  50. //////////////////////////////////////////
  51. // flash(SPI_CS, MANUFACTURER_ID)
  52. // SPI_CS - CS pin attached to SPI flash chip (8 in case of Moteino)
  53. // MANUFACTURER_ID - OPTIONAL, 0x1F44 for adesto(ex atmel) 4mbit flash
  54. // 0xEF30 for windbond 4mbit flash
  55. //////////////////////////////////////////
  56. SPIFlash flash(FLASH_SS, 0xEF30);
  57. void setup(){
  58. Serial.begin(SERIAL_BAUD);
  59. Serial.print("Start...");
  60. if (flash.initialize())
  61. {
  62. Serial.println("Init OK!");
  63. Blink(LED, 20, 10);
  64. }
  65. else
  66. Serial.println("Init FAIL!");
  67. delay(1000);
  68. }
  69. void loop(){
  70. // Handle serial input (to allow basic DEBUGGING of FLASH chip)
  71. // ie: display first 256 bytes in FLASH, erase chip, write bytes at first 10 positions, etc
  72. if (Serial.available() > 0) {
  73. input = Serial.read();
  74. if (input == 'd') //d=dump flash area
  75. {
  76. Serial.println("Flash content:");
  77. int counter = 0;
  78. while(counter<=256){
  79. Serial.print(flash.readByte(counter++), HEX);
  80. Serial.print('.');
  81. }
  82. Serial.println();
  83. }
  84. else if (input == 'e')
  85. {
  86. Serial.print("Erasing Flash chip ... ");
  87. flash.chipErase();
  88. while(flash.busy());
  89. Serial.println("DONE");
  90. }
  91. else if (input == 'i')
  92. {
  93. Serial.print("DeviceID: ");
  94. Serial.println(flash.readDeviceId(), HEX);
  95. }
  96. else if (input >= 48 && input <= 57) //0-9
  97. {
  98. Serial.print("\nWriteByte("); Serial.print(input); Serial.print(")");
  99. flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb);
  100. }
  101. }
  102. // Periodically blink the onboard LED while listening for serial commands
  103. if ((int)(millis()/500) > lastPeriod)
  104. {
  105. lastPeriod++;
  106. pinMode(LED, OUTPUT);
  107. digitalWrite(LED, lastPeriod%2);
  108. }
  109. }
  110. void Blink(byte PIN, int DELAY_MS, byte loops)
  111. {
  112. pinMode(PIN, OUTPUT);
  113. while (loops--)
  114. {
  115. digitalWrite(PIN,HIGH);
  116. delay(DELAY_MS);
  117. digitalWrite(PIN,LOW);
  118. delay(DELAY_MS);
  119. }
  120. }