|
1 | 1 | /*
|
2 | 2 | * EEPROM Clear
|
3 | 3 | *
|
4 |
| - * This sketch will erase the EEPROM memory. An erased |
5 |
| - * EEPROM has all of the bytes set to 0xFF (255 decimal). |
| 4 | + * Sets all of the bytes of the EEPROM to 0. |
| 5 | + * Please see eeprom_iteration for a more in depth |
| 6 | + * look at how to traverse the EEPROM. |
6 | 7 | *
|
7 |
| - * Description |
8 |
| - * Unlike the AVR micrcontrollers used on the Arduino boards, |
9 |
| - * the PIC32 microcontrollers used on the chipKIT boards don't |
10 |
| - * have internal EEPROM memory. Instead, FLASH memory is used |
11 |
| - * to simulate EEPROM. There are two major differences between |
12 |
| - * FLASH and EEPROM. 1) EEPROM is byte erasable and rewritable. |
13 |
| - * With FLASH an entire page must be erased before any byte can |
14 |
| - * be rewritten. 2) FLASH can't be erased and rewritten as many |
15 |
| - * times as EEPROM. After a limited number of erase/write |
16 |
| - * cycles, the FLASH page will 'wear out', and additional |
17 |
| - * writes may not store the correct value. EEPROM can also |
18 |
| - * wear out, but it typically takes many more writes to wear |
19 |
| - * it out. |
20 |
| - * To address these issues, the chipKIT EEPROM library uses a |
21 |
| - * page of FLASH memory to simulate EEPROM. |
22 |
| - * The FLASH is used as content addressable memory. A 32-bit |
23 |
| - * word is used for each simulated EEPROM byte. The word stores |
24 |
| - * both the EEPROM address and the byte value. When a write is |
25 |
| - * performed, the page is searched for an unused location, when |
26 |
| - * one is found, the address/data is written to that word. When |
27 |
| - * reading, the page is searched for the address, and then the |
28 |
| - * value is returned. If a location is being rewritten, the old |
29 |
| - * location is marked as invalid and a new location written as |
30 |
| - * described above. |
31 |
| - * If there are no unused locations available in the page when a |
32 |
| - * write is being attempted, then the page must be erased before |
33 |
| - * the value can be written. The contents of the page is copied |
34 |
| - * to a memory buffer, the page erased, and then the old values |
35 |
| - * plus new value are written back to the FLASH page. This |
36 |
| - * method reduces the number of times that the page must be |
37 |
| - * erased and rewritten. |
38 |
| - * The PIC32 FLASH page size is 4K (4096) bytes, which allows for |
39 |
| - * simulation of up to 1K (1024) bytes of EEPROM. |
40 |
| - * There is a degenerate case, however, that can lead to rapid |
41 |
| - * wearout of the FLASH page being used. If all of the simulated |
42 |
| - * EEPROM locations have been written (i.e. all simulated EEPROM |
43 |
| - * addresses have been written to), then each additional write |
44 |
| - * results in the page being erased and rewritten, eliminating |
45 |
| - * the benefit of the wear leveling algorithm being used. |
46 |
| - * This library uses two methods to help reduce this problem. |
47 |
| - * The first is adding the clear() method. This method erases |
48 |
| - * the flash page, erasing all simulated EEPROM. If a sketch has |
49 |
| - * been run that uses a large amount of the simulated EEPROM, |
50 |
| - * clear() should be used when that EEPROM content is not longer |
51 |
| - * needed. This will reduce the number of values in the EEPROM |
52 |
| - * that need to be preserved, reducing the number of times that |
53 |
| - * the FLASH page will have to be erased and rewritten. The |
54 |
| - * second method used to reduce the liklihood of premature FLASH |
55 |
| - * wearout is to limit the default EEPROM memory size. By default |
56 |
| - * this library implements 512 bytes of simulated EEPROM. This |
57 |
| - * reserves at least half of the FLASH page to handle rewrites. |
58 |
| - * It is possible to increase the simulated EEPROM size if a |
59 |
| - * larger amount is needed. Two new methods have been added to |
60 |
| - * accomodate this. getMaxAddress returns the current size of |
61 |
| - * the simulated EEPROM. setMaxAddress allows setting it to |
62 |
| - * a different size up to 1024. |
| 8 | + * This example code is in the public domain. |
63 | 9 | */
|
64 | 10 |
|
65 | 11 | #include <EEPROM.h>
|
66 | 12 |
|
67 |
| -void setup() |
68 |
| -{ |
69 |
| - // Clear entire eeprom |
70 |
| - EEPROM.clear(); |
71 |
| - |
| 13 | +void setup() { |
| 14 | + // initialize the LED pin as an output. |
| 15 | + pinMode(13, OUTPUT); |
| 16 | + |
| 17 | + /*** |
| 18 | + Iterate through each byte of the EEPROM storage. |
| 19 | +
|
| 20 | + Larger AVR processors have larger EEPROM sizes, E.g: |
| 21 | + - Arduno Duemilanove: 512b EEPROM storage. |
| 22 | + - Arduino Uno: 1kb EEPROM storage. |
| 23 | + - Arduino Mega: 4kb EEPROM storage. |
| 24 | +
|
| 25 | + Rather than hard-coding the length, you should use the pre-provided length function. |
| 26 | + This will make your code portable to all AVR processors. |
| 27 | + ***/ |
| 28 | + |
| 29 | + for (int i = 0 ; i < EEPROM.length() ; i++) { |
| 30 | + EEPROM.write(i, 0); |
| 31 | + } |
| 32 | + |
72 | 33 | // turn the LED on when we're done
|
73 | 34 | digitalWrite(13, HIGH);
|
74 | 35 | }
|
75 | 36 |
|
76 |
| -void loop() |
77 |
| -{ |
| 37 | +void loop() { |
| 38 | + /** Empty loop. **/ |
78 | 39 | }
|
0 commit comments