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.

eepromTest_Wire1.ino 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //Test extEEPROM library.
  2. //Writes the EEPROM full of 32-bit integers and reads them back to verify.
  3. //Wire a button from digital pin 6 to ground, this is used as a start button
  4. //so the sketch doesn't do unnecessary EEPROM writes every time it's reset.
  5. //Jack Christensen 09Jul2014
  6. //Paolo Paolucci 17Mar2016 (fix 28Jun2017)
  7. //Mik 03Jan2017 (configured Library to use the Wire1 as I2C channel)
  8. #include <extEEPROM.h> //https://github.com/PaoloP74/extEEPROM
  9. //One 24LC256 EEPROMs on the bus
  10. const uint32_t totalKBytes = 32; //for read and write test functions
  11. extEEPROM eep(kbits_256, 1, 64, 0x57); //device size, number of devices, page size
  12. const uint8_t btnStart = 6; //start button
  13. void setup(void)
  14. {
  15. uint8_t eepStatus;
  16. pinMode(btnStart, INPUT_PULLUP);
  17. Serial.begin(115200);
  18. while (!SerialUSB) {}
  19. bool channelInsert = false;
  20. Serial.println(F("Select the number of Wire channel use the eeprom"));
  21. Serial.println(F("0 = Wire"));
  22. Serial.println(F("1 = Wire1"));
  23. Serial.println(F("...."));
  24. Serial.println(F("x = WIRE_INTERFACES_COUNT"));
  25. do {
  26. if (Serial.available()) {
  27. char I2Cchannel = Serial.read();
  28. // only number that are less than WIRE_INTERFACES_COUNT are allowed
  29. if ((I2Cchannel > '0') && (I2Cchannel < ('0' + WIRE_INTERFACES_COUNT))) {
  30. channelInsert = true;
  31. }
  32. switch ((I2Cchannel - '0')) {
  33. case 0:
  34. Serial.println(F("Using the default Wire interface"));
  35. eepStatus = eep.begin(eep.twiClock400kHz); //go fast!
  36. break;
  37. case 1:
  38. Serial.println(F("Using the Wire1 interface"));
  39. eepStatus = eep.begin(eep.twiClock400kHz, &Wire1); //go fast!
  40. break;
  41. /*
  42. Uncomment till the number of WIRE_INTERFACES_COUNT of your Arduino board
  43. case 2:
  44. Serial.println(F("Using the Wire2 interface"));
  45. eepStatus = eep.begin(eep.twiClock400kHz, &Wire2); //go fast!
  46. break;
  47. case 3:
  48. Serial.println(F("Using the Wire3 interface"));
  49. eepStatus = eep.begin(eep.twiClock400kHz, &Wire3); //go fast!
  50. break;
  51. case 4:
  52. Serial.println(F("Using the Wire4 interface"));
  53. eepStatus = eep.begin(eep.twiClock400kHz, &Wire4); //go fast!
  54. break;
  55. case 5:
  56. Serial.println(F("Using the Wire5 interface"));
  57. eepStatus = eep.begin(eep.twiClock400kHz, &Wire5); //go fast!
  58. break;*/
  59. default:
  60. Serial.println(F("A wrong channel has been inserted (Arduino manage max 5)"));
  61. break;
  62. }
  63. }
  64. } while (!channelInsert);
  65. if (eepStatus) {
  66. Serial.print(F("extEEPROM.begin() failed, status = "));
  67. Serial.println(eepStatus);
  68. while (1);
  69. }
  70. Serial.println(F("Started !!"));
  71. uint8_t chunkSize =
  72. 64; //this can be changed, but must be a multiple of 4 since we're writing 32-bit integers
  73. // eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
  74. eeWrite(chunkSize);
  75. eeRead(chunkSize);
  76. dump(0, 32); //the first 32 bytes
  77. dump(32256, 64); //the last 64 bytes
  78. //dump(32512, 64); //across the device boundary
  79. //dump(65520, 16); //the last 16 bytes
  80. }
  81. void loop(void)
  82. {
  83. }
  84. //write test data (32-bit integers) to eeprom, "chunk" bytes at a time
  85. void eeWrite(uint8_t chunk)
  86. {
  87. chunk &= 0xFC; //force chunk to be a multiple of 4
  88. uint8_t data[chunk];
  89. uint32_t val = 0;
  90. Serial.println(F("Writing..."));
  91. uint32_t msStart = millis();
  92. for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
  93. if ( (addr & 0xFFF) == 0 ) {
  94. Serial.println(addr);
  95. }
  96. for (uint8_t c = 0; c < chunk; c += 4) {
  97. data[c + 0] = val >> 24;
  98. data[c + 1] = val >> 16;
  99. data[c + 2] = val >> 8;
  100. data[c + 3] = val;
  101. ++val;
  102. }
  103. eep.write(addr, data, chunk);
  104. }
  105. uint32_t msLapse = millis() - msStart;
  106. Serial.print(F("Write lapse: "));
  107. Serial.print(msLapse);
  108. Serial.println(F(" ms"));
  109. }
  110. //read test data (32-bit integers) from eeprom, "chunk" bytes at a time
  111. void eeRead(uint8_t chunk)
  112. {
  113. chunk &= 0xFC; //force chunk to be a multiple of 4
  114. uint8_t data[chunk];
  115. uint32_t val = 0, testVal;
  116. Serial.println(F("Reading..."));
  117. uint32_t msStart = millis();
  118. for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
  119. if ( (addr & 0xFFF) == 0 ) {
  120. Serial.println(addr);
  121. }
  122. eep.read(addr, data, chunk);
  123. for (uint8_t c = 0; c < chunk; c += 4) {
  124. testVal = ((uint32_t)data[c + 0] << 24) + ((uint32_t)data[c + 1] << 16) + ((
  125. uint32_t)data[c + 2] << 8) + (uint32_t)data[c + 3];
  126. if (testVal != val) {
  127. Serial.print(F("Error @ addr "));
  128. Serial.print(addr + c);
  129. Serial.print(F(" Expected "));
  130. Serial.print(val);
  131. Serial.print(F(" Read "));
  132. Serial.print(testVal);
  133. Serial.print(F(" 0x"));
  134. Serial.println(testVal, HEX);
  135. }
  136. ++val;
  137. }
  138. }
  139. uint32_t msLapse = millis() - msStart;
  140. Serial.print(F("Last value: "));
  141. Serial.print(val);
  142. Serial.print(F(" Read lapse: "));
  143. Serial.print(msLapse);
  144. Serial.println(F(" ms"));
  145. }
  146. //write 0xFF to eeprom, "chunk" bytes at a time
  147. void eeErase(uint8_t chunk, uint32_t startAddr, uint32_t endAddr)
  148. {
  149. chunk &= 0xFC; //force chunk to be a multiple of 4
  150. uint8_t data[chunk];
  151. Serial.println(F("Erasing..."));
  152. for (int i = 0; i < chunk; i++) {
  153. data[i] = 0xFF;
  154. }
  155. uint32_t msStart = millis();
  156. for (uint32_t a = startAddr; a <= endAddr; a += chunk) {
  157. if ( (a & 0xFFF) == 0 ) {
  158. Serial.println(a);
  159. }
  160. eep.write(a, data, chunk);
  161. }
  162. uint32_t msLapse = millis() - msStart;
  163. Serial.print(F("Erase lapse: "));
  164. Serial.print(msLapse);
  165. Serial.print(F(" ms"));
  166. }
  167. //dump eeprom contents, 16 bytes at a time.
  168. //always dumps a multiple of 16 bytes.
  169. void dump(uint32_t startAddr, uint32_t nBytes)
  170. {
  171. Serial.print(F("EEPROM DUMP 0x"));
  172. Serial.print(startAddr, HEX);
  173. Serial.print(F(" 0x"));
  174. Serial.print(nBytes, HEX);
  175. Serial.print(F(" "));
  176. Serial.print(startAddr);
  177. Serial.print(F(" "));
  178. Serial.println(nBytes);
  179. uint32_t nRows = (nBytes + 15) >> 4;
  180. uint8_t d[16];
  181. for (uint32_t r = 0; r < nRows; r++) {
  182. uint32_t a = startAddr + 16 * r;
  183. eep.read(a, d, 16);
  184. Serial.print(F("0x"));
  185. if ( a < 16 * 16 * 16 ) {
  186. Serial.print(F("0"));
  187. }
  188. if ( a < 16 * 16 ) {
  189. Serial.print(F("0"));
  190. }
  191. if ( a < 16 ) {
  192. Serial.print(F("0"));
  193. }
  194. Serial.print(a, HEX);
  195. Serial.print(F(" "));
  196. for ( int c = 0; c < 16; c++ ) {
  197. if ( d[c] < 16 ) {
  198. Serial.print(F("0"));
  199. Serial.print(d[c], HEX);
  200. Serial.print( c == 7 ? " " : " ");
  201. }
  202. }
  203. Serial.println(F(""));
  204. }
  205. }