a simple wrapper above I2C to provider EEPROM access API
Dependents: ou_mbed_oled ou_mbed_eeprom ou_mbed_tmp102
Diff: eeprom.cpp
- Revision:
- 0:3de36cc169a3
- Child:
- 1:0c876c06b026
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eeprom.cpp Fri Jun 15 13:02:30 2018 +0000 @@ -0,0 +1,109 @@ +#include "eeprom.h" + +eeprom::eeprom(I2C &i2c_obj, char address) + : i2c(i2c_obj), adr(address) +{ + init(); +} + +void eeprom::init(void) +{ +} + +void eeprom::write_address(int address, int address_size, bool repeated) +{ /* + uint8_t mem_address[] = {0, 0, 0}; + + switch (address_size) { + case ONE_BYTE_ADDRESS: + mem_address[0] = address & 0xFF; + break; + case TWO_BYTES_ADDRESS: + mem_address[0] = (address >> 8) & 0xFF; + mem_address[1] = address & 0xFF; + break; + case THREE_BYTES_ADDRESS: + mem_address[0] = (address >> 16) & 0xFF; + mem_address[1] = (address >> 8) & 0xFF; + mem_address[2] = address & 0xFF; + break; + } + i2c.write((int)adr,(char *)mem_address,address_size,repeated); + */ + uint8_t buffer[4] = { 0, 0, 0, 0 }; + page_write(address, address_size, buffer, 0); +} + +void eeprom::current_read(uint8_t *data) +{ + i2c.read((int)adr,(char *)data,1); +} + +void eeprom::sequential_read(uint8_t *buffer, int buffer_size) +{ + i2c.read((int)adr, (char *)buffer, buffer_size); +} + +void eeprom::random_read(int address, int address_size, uint8_t *buffer, int buffer_size) +{ + write_address(address, address_size, true); + i2c.read((int)adr, (char *)buffer, buffer_size); +} + +void eeprom::byte_write(int address, int address_size, uint8_t data, bool repeated) +{ /* + uint8_t mem_address[] = {0, 0, 0, 0}; + mem_address[3] = data; + + switch (address_size) { + case ONE_BYTE_ADDRESS: + mem_address[2] = address & 0xFF; + break; + case TWO_BYTES_ADDRESS: + mem_address[1] = (address >> 8) & 0xFF; + mem_address[2] = address & 0xFF; + break; + case THREE_BYTES_ADDRESS: + mem_address[0] = (address >> 16) & 0xFF; + mem_address[1] = (address >> 8) & 0xFF; + mem_address[2] = address & 0xFF; + break; + } + i2c.write((int)adr,(char *)(mem_address + (3 - address_size)),address_size+1,repeated); + */ + uint8_t buffer[4] = { 0, 0, 0, 0 }; + buffer[3] = data; + page_write(address, address_size, buffer, 1); +} + +void eeprom::page_write(int address, int address_size, uint8_t *buffer, int buffer_size, bool repeated) +{ + // for simplify, buffer must prefix 3 bytes for memory address + switch (address_size) { + case ONE_BYTE_ADDRESS: + buffer[2] = address & 0xFF; + break; + case TWO_BYTES_ADDRESS: + buffer[1] = (address >> 8) & 0xFF; + buffer[2] = address & 0xFF; + break; + case THREE_BYTES_ADDRESS: + buffer[0] = (address >> 16) & 0xFF; + buffer[1] = (address >> 8) & 0xFF; + buffer[2] = address & 0xFF; + break; + } + i2c.write((int)adr,(char *)(buffer + (3 - address_size)),address_size + buffer_size,repeated); +} + +void eeprom::ack_polling(void) +{ + uint8_t buffer[] = { 0 }; + int ack; + + do { + ack = i2c.write((int)adr, (char *)buffer, 1); + } while(ack == 1); +} + +