diff options
| author | joe.dumoulin <joe.dumoulin@gmail.com> | 2017-07-30 14:28:05 -0700 |
|---|---|---|
| committer | joe.dumoulin <joe.dumoulin@gmail.com> | 2017-07-30 14:28:05 -0700 |
| commit | f17634478969895d92b062877c04df5e3e90317f (patch) | |
| tree | 5ce59ceac9ec24e5c0946d23f6e218694d81d1ee /m0EEPROM.cpp | |
| parent | 801bf60c7911078a94f48c3f3c25b988e8b4ac21 (diff) | |
Diffstat (limited to 'm0EEPROM.cpp')
| -rw-r--r-- | m0EEPROM.cpp | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/m0EEPROM.cpp b/m0EEPROM.cpp index 10761a7..306e017 100644 --- a/m0EEPROM.cpp +++ b/m0EEPROM.cpp @@ -5,7 +5,7 @@ #include "Wire.h" #include "RingBuffer.h" // for SERIAL_BUFFER_SIZE -#include "m0EEPROM.h +#include "m0EEPROM.h" #define PAGE_SIZE 64 // page size of the 24LC256 in bytes @@ -23,7 +23,7 @@ uint8_t eeprom::read_byte(uint16_t address) { Wire.beginTransmission(_device_address); Wire.write((int)(address >> 8)); // MSB Wire.write((int)(address & 0xFF)); // LSB - + Wire.endTransmission(); Wire.requestFrom(_device_address,1); if (Wire.available()) rdata = Wire.read(); @@ -39,10 +39,15 @@ void eeprom::write_byte(uint16_t address, uint8_t value) { Wire.write((int)(address >> 8)); // MSB Wire.write((int)(address & 0xFF)); // LSB - Wire.write(data); + size_t wresult = Wire.write((int)value); + if (wresult == 0){ +// Serial.print("wire write error = "); Serial.println(wresult); + } - Wire.endTransmission(); - + uint8_t result = Wire.endTransmission(); + if (result){ +// Serial.print("wire endTransmission error = "); Serial.println(result); + } delay(6); // at least 5ms for quiescence } @@ -50,58 +55,57 @@ void eeprom::write_byte(uint16_t address, uint8_t value) { // the size of the structure must be less than 64K-address. // returns a byte array of memory specified. The byte array data // must be preallocated to the expected size. -uint8_t* eeprom::read_bytes(uint16_t start_address, uint8_t* data, uint16_t size) { +uint8_t* eeprom::read_bytes(uint16_t address, uint8_t* data, uint16_t size) { unsigned char i=0; - Wire.beginTransmission(deviceaddress); - Wire.write((int)(eeaddress >> 8)); // MSB - Wire.write((int)(eeaddress & 0xFF)); // LSB + Wire.beginTransmission(_device_address); + Wire.write((int)(address >> 8)); // MSB + Wire.write((int)(address & 0xFF)); // LSB Wire.endTransmission(); - Wire.requestFrom(deviceaddress,num_chars); + Wire.requestFrom(_device_address, size); while(Wire.available()) data[i++] = Wire.read(); + return data; } // write a structure of arbitrary size to memory at address. -// the size of the structure must be less than 64K-address. +// the size of the structure must be less than 64K-address - start_address. // The structure is passed as a byte array. -void eeprom::write_bytes(uint16_t address, uint8_t* data, uint16_t size) { +void eeprom::write_bytes(uint16_t start_address, uint8_t* data, uint16_t size) { // Uses Page Write for 24LC256 // Allows for EEPROM_PAGE_SIZE byte page boundary // Splits string into max BUFFER_SIZE byte writes -// uint8_t counter=0; - unsigned int address; - uint32_t first_page_len; + uint32_t first_page_size; unsigned int num_writes; -// unsigned int data_len=0; - unsigned char first_write_size; - unsigned char last_write_size; - unsigned char write_size; + uint8_t first_write_size=0; + uint8_t last_write_size=0; + uint16_t write_size=0; // get the amount of memory to write in the first page of eeprom - uint8_t page_bits = log2uint(EEPROM_PAGE_SIZE); - first_page = ((start_address >> page_bits) +1) * EEPROM_PAGE_SIZE -startaddress; + uint8_t page_bits = log2uint(PAGE_SIZE); + first_page_size = ((start_address >> page_bits) +1) * PAGE_SIZE - start_address; // get the size of the first write based on the I2C buffer size uint8_t buffer_bits = log2uint(SERIAL_BUFFER_SIZE); - if (first_page > SERIAL_BUFFER_SIZE){ - first_write_size = first_page - (first_page >> buffer_bits) * SERIAL_BUFFER_SIZE; + if (first_page_size > SERIAL_BUFFER_SIZE){ + first_write_size = first_page_size - (first_page_size >> buffer_bits) * SERIAL_BUFFER_SIZE; if (first_write_size == 0) first_write_size = SERIAL_BUFFER_SIZE; } else - first_write_size = first_page; + first_write_size = first_page_size; // now get the size of the last write in the final page // and the total number of pages to write. if (size > first_write_size){ last_write_size = (size - first_write_size) % SERIAL_BUFFER_SIZE; - num_writes = ((data_len - first_write_size) >> (buffer_bits)) + 2; + num_writes = ((size - first_write_size) >> (buffer_bits)) + 2; } else num_writes = 1; // Write loop + uint16_t address = start_address; uint8_t i = 0; for (uint32_t page = 0; page < num_writes; ++page){ if(page==0) @@ -121,8 +125,8 @@ void eeprom::write_bytes(uint16_t address, uint8_t* data, uint16_t size) { address += write_size; delay(6); // needs 5ms for page write + } } - - +eeprom Eeprom; |
