From f17634478969895d92b062877c04df5e3e90317f Mon Sep 17 00:00:00 2001 From: "joe.dumoulin" Date: Sun, 30 Jul 2017 14:28:05 -0700 Subject: working library with tests. Lacks error tracking. --- examples/m0eepromtest/m0eepromtest.ino | 189 +++++++++++++++++++++++++-------- 1 file changed, 147 insertions(+), 42 deletions(-) (limited to 'examples') diff --git a/examples/m0eepromtest/m0eepromtest.ino b/examples/m0eepromtest/m0eepromtest.ino index 52b1f6b..d4547e9 100644 --- a/examples/m0eepromtest/m0eepromtest.ino +++ b/examples/m0eepromtest/m0eepromtest.ino @@ -1,9 +1,20 @@ // m0eepromtest.ino - some simple tests of the m0eeprom library -#include "m0eeprom.h" +#include "m0EEPROM.h" uint8_t rdata[32]; +// prototypes +//void read_after_reset(); +void test_simple_read_write_one_byte(); +void test_simple_read_write_two_bytes(); +void test_write_one_byte_doesnt_erase_second_byte_in_memory(); +void test_write_and_read_a_string(); +void test_multiple_string_writes_over_page_boundaries(); +void test_multiple_string_writes_over_page_boundaries(uint32_t offset); +void write_an_arbitrary_structure_to_eeprom(); +void clear_eeprom_bytes(uint16_t offset); + void setup(void) { Serial.begin(9600); @@ -11,37 +22,145 @@ void setup(void) delay( 1 ); } Serial.flush(); + Eeprom.begin(); + +// read_after_reset(); + clear_eeprom_bytes(0x40); + test_simple_read_write_one_byte(); + test_simple_read_write_two_bytes(); + test_write_one_byte_doesnt_erase_second_byte_in_memory(); + test_write_and_read_a_string(); + + // offset for write start = 0 + test_multiple_string_writes_over_page_boundaries(); + // offset for write start = 27 + test_multiple_string_writes_over_page_boundaries(27); + write_an_arbitrary_structure_to_eeprom(); - Wire.begin(); +} - size_t i; - // define large string of data to be written - cleareeprom(); +void loop(){ +} + +/* start tests */ +// two one-byte reads to be used after a reset to ensure that eeprom is stable. +// enable after you run tests once. +/* +void read_after_reset() { + unsigned int address = 0; + Serial.println("read_after_reset begin"); + Serial.println(Eeprom.read_byte(address), DEC); + Serial.println(Eeprom.read_byte(address+1), DEC); + + Serial.println("read_after_reset end"); +} +*/ +void test_simple_read_write_one_byte() { + unsigned int address = 0x40; + Serial.println("test_simple_read_write_one_byte begin"); +// Eeprom.write_byte(address, 0); + uint8_t w = 123; + Eeprom.write_byte(address, w); + + Serial.println(Eeprom.read_byte(address), DEC); + Serial.println("test_simple_read_write_one_byte end"); +} + +void test_simple_read_write_two_bytes() { + unsigned int address = 0; + Serial.println("test_simple_read_write_two_bytes begin"); + Eeprom.write_byte(address, (uint8_t) 123); + Eeprom.write_byte(address+1, 66); + + Serial.println(Eeprom.read_byte(address), DEC); + Serial.println(Eeprom.read_byte(address+1), DEC); + + Serial.println("test_simple_read_write_two_bytes end"); +} + +// The second byte in memory is preserved here +void test_write_one_byte_doesnt_erase_second_byte_in_memory() { + uint16_t address = 0; + Serial.println("test_write_one_byte_doesnt_erase_second_byte_in_memory begin"); + Eeprom.write_byte(address, 123); + Eeprom.write_byte(address+1, 66); + + Eeprom.write_byte(address, 1); + + Serial.println(Eeprom.read_byte(address), DEC); + Serial.println(Eeprom.read_byte(address+1), DEC); + + Serial.println("test_write_one_byte_doesnt_erase_second_byte_in_memory end"); + +} + +void test_write_and_read_a_string() { + char p[] = "Once again."; + uint16_t psize = strlen(p)+1; + uint8_t recv[psize]; // where to put the result + uint16_t address = 0; + Serial.println("test_write_and_read_a_string begin"); + + Serial.println(p); + uint8_t* rp = (uint8_t*)p; + + for (uint8_t i=0; ix = ");Serial.print(bar->x);Serial.println(); @@ -84,21 +188,22 @@ void setup(void) // print rdbuf size for (size_t i = 0; i < len; ++i){ sprintf(hexchr, "%02X", rdbuf[i]); - Serial.print("rdbuf[");Serial.print(i);Serial.print("] = ");Serial.print(hexchr);Serial.println(); + Serial.print(hexchr); } + Serial.println(); } -void cleareeprom() { - char Clear[] ={"000000000000000000000000000000000000000000000000000000000000000"}; - // Work out length of data - char str_len=0; - do{ str_len++; } while(Clear[str_len]); +void clear_eeprom_bytes(uint16_t offset){ + // write zeros for 10 bytes starting at offset. + Serial.println("clear_eeprom_bytes begin"); + uint8_t buff[10] = {0}; + Eeprom.write_bytes(offset, buff, 10); - // Write out data several times consecutively starting at address 0 - for(size_t i=0;i