Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

SPIFlash_ReadWrite.ino 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. {
  59. Serial.begin(SERIAL_BAUD);
  60. Serial.print("Start...");
  61. if (flash.initialize()) {
  62. Serial.println("Init OK!");
  63. Blink(LED, 20, 10);
  64. } else {
  65. Serial.println("Init FAIL!");
  66. }
  67. delay(1000);
  68. }
  69. void loop()
  70. {
  71. // Handle serial input (to allow basic DEBUGGING of FLASH chip)
  72. // ie: display first 256 bytes in FLASH, erase chip, write bytes at first 10 positions, etc
  73. if (Serial.available() > 0) {
  74. input = Serial.read();
  75. if (input == 'd') { //d=dump flash area
  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. } else if (input == 'e') {
  84. Serial.print("Erasing Flash chip ... ");
  85. flash.chipErase();
  86. while(flash.busy());
  87. Serial.println("DONE");
  88. } else if (input == 'i') {
  89. Serial.print("DeviceID: ");
  90. Serial.println(flash.readDeviceId(), HEX);
  91. } else if (input >= 48 && input <= 57) { //0-9
  92. Serial.print("\nWriteByte(");
  93. Serial.print(input);
  94. Serial.print(")");
  95. flash.writeByte(input-48, (millis()%2) ? 0xaa : 0xbb);
  96. }
  97. }
  98. // Periodically blink the onboard LED while listening for serial commands
  99. if ((int)(millis()/500) > lastPeriod) {
  100. lastPeriod++;
  101. pinMode(LED, OUTPUT);
  102. digitalWrite(LED, lastPeriod%2);
  103. }
  104. }
  105. void Blink(byte PIN, int DELAY_MS, byte loops)
  106. {
  107. pinMode(PIN, OUTPUT);
  108. while (loops--) {
  109. digitalWrite(PIN,HIGH);
  110. delay(DELAY_MS);
  111. digitalWrite(PIN,LOW);
  112. delay(DELAY_MS);
  113. }
  114. }