1+ /*
2+ Reading and writing test of the EEPROM functions on the Artemis
3+ By: Nathan Seidle
4+ SparkFun Electronics
5+ Date: June 24th, 2019
6+ This example code is in the public domain.
7+
8+ SparkFun labored with love to create this code. Feel like supporting open source hardware?
9+ Buy a board from SparkFun! https://www.sparkfun.com/products/15376
10+
11+ Page erase takes 15ms
12+ Write byte takes 30ms - This is much longer than Arduino that takes 3.3ms
13+ Float write across two words takes 30ms
14+ Update (no write) takes 1ms
15+ */
16+
17+ #include < EEPROM.h>
18+
19+ void setup ()
20+ {
21+ Serial.begin (9600 );
22+ Serial.println (" EEPROM Examples" );
23+
24+ randomSeed (analogRead (A0));
25+
26+ long startTime;
27+ long endTime;
28+ uint16_t randomLocation;
29+
30+ // Test erase time
31+ startTime = millis ();
32+ EEPROM.erase ();
33+ endTime = millis ();
34+ Serial.printf (" Time to erase all EEPROM: %dms\n " , endTime - startTime);
35+
36+ // Byte sequential test
37+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
38+ Serial.println (" " );
39+ Serial.println (" 8 bit tests" );
40+ byte myValue1 = 200 ;
41+ byte myValue2 = 23 ;
42+ randomLocation = random (0 , FLASH_EEPROM_SIZE);
43+
44+ startTime = millis ();
45+ EEPROM.write (randomLocation, myValue1); // (location, data)
46+ endTime = millis ();
47+ EEPROM.put (randomLocation + 1 , myValue2);
48+
49+ Serial.printf (" Write byte time: %dms\n " , endTime - startTime);
50+
51+ byte response1 = EEPROM.read (randomLocation);
52+ byte response2 = EEPROM.read (randomLocation + 1 );
53+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation, myValue1, response1);
54+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation + 1 , myValue2, response2);
55+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
56+
57+ Serial.println (" " );
58+ Serial.println (" 16 bit tests" );
59+
60+ // int16_t and uint16_t sequential test
61+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
62+ uint16_t myValue3 = 3411 ;
63+ int16_t myValue4 = -366 ;
64+ randomLocation = random (0 , FLASH_EEPROM_SIZE);
65+
66+ EEPROM.put (randomLocation, myValue3);
67+ EEPROM.put (randomLocation + 2 , myValue4);
68+
69+ uint16_t response3;
70+ int16_t response4;
71+ EEPROM.get (randomLocation, response3);
72+ EEPROM.get (randomLocation + 2 , response4);
73+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation, myValue3, response3);
74+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation + 2 , myValue4, response4);
75+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
76+
77+ Serial.println (" " );
78+ Serial.println (" 32 bit tests" );
79+
80+ // int and unsigned int (32) sequential test
81+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
82+ Serial.printf (" Size of int: %d\n " , sizeof (int ));
83+ int myValue5 = -245000 ;
84+ unsigned int myValue6 = 400123 ;
85+ randomLocation = random (0 , FLASH_EEPROM_SIZE);
86+
87+ EEPROM.put (randomLocation, myValue5);
88+ EEPROM.put (randomLocation + 4 , myValue6);
89+
90+ int response5;
91+ unsigned int response6;
92+ EEPROM.get (randomLocation, response5);
93+ EEPROM.get (randomLocation + 4 , response6);
94+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation, myValue5, response5);
95+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation + 4 , myValue6, response6);
96+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
97+
98+ // int32_t and uint32_t sequential test
99+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
100+ int32_t myValue7 = -341002 ;
101+ uint32_t myValue8 = 241544 ;
102+ randomLocation = random (0 , FLASH_EEPROM_SIZE);
103+
104+ EEPROM.update (randomLocation, myValue7);
105+ EEPROM.update (randomLocation + 4 , myValue8);
106+
107+ int32_t response7;
108+ uint32_t response8;
109+ EEPROM.get (randomLocation, response7);
110+ EEPROM.get (randomLocation + 4 , response8);
111+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation, myValue7, response7);
112+ Serial.printf (" Location %d should be %d: %d\n\r " , randomLocation + 4 , myValue8, response8);
113+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
114+
115+ // float (32) sequential test
116+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
117+ Serial.printf (" Size of float: %d\n " , sizeof (float ));
118+ float myValue9 = -7.35 ;
119+ float myValue10 = 5.22 ;
120+ randomLocation = random (0 , FLASH_EEPROM_SIZE);
121+
122+ EEPROM.update (randomLocation, myValue9);
123+ EEPROM.update (randomLocation + 4 , myValue10);
124+
125+ float response9;
126+ float response10;
127+ EEPROM.get (randomLocation, response9);
128+ EEPROM.get (randomLocation + 4 , response10);
129+ Serial.printf (" Location %d should be %f: %f\n\r " , randomLocation, myValue9, response9);
130+ Serial.printf (" Location %d should be %f: %f\n\r " , randomLocation + 4 , myValue10, response10);
131+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
132+
133+ Serial.println (" " );
134+ Serial.println (" 64 bit tests" );
135+
136+ // double (64) sequential test
137+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
138+ Serial.printf (" Size of double: %d\n " , sizeof (double ));
139+ double myValue11 = -290.3485723409857 ;
140+ double myValue12 = 384.95734987 ;
141+ randomLocation = random (0 , FLASH_EEPROM_SIZE);
142+
143+ EEPROM.update (randomLocation, myValue11);
144+ EEPROM.update (randomLocation + 8 , myValue12);
145+
146+ double response11;
147+ double response12;
148+ EEPROM.get (randomLocation, response11);
149+ EEPROM.get (randomLocation + 8 , response12);
150+ Serial.printf (" Location %d should be %lf: %lf\n " , randomLocation, myValue11, response11);
151+ Serial.printf (" Location %d should be %lf: %lf\n " , randomLocation + 8 , myValue12, response12);
152+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
153+
154+ Serial.println ();
155+ Serial.print (" Flash Contents:" );
156+ for (uint16_t x = 0 ; x < 8 * 4 ; x += 4 )
157+ {
158+ if (x % 32 == 0 )
159+ Serial.println ();
160+ Serial.printf (" 0x%08X " , *(uint32_t *)(FLASH_EEPROM_START + x));
161+ }
162+ Serial.println ();
163+ }
164+
165+ void loop ()
166+ {
167+ }
0 commit comments