217 lines
5.6 KiB
C++
217 lines
5.6 KiB
C++
#ifndef __EEPROM__H_
|
|
#define __EEPROM__H_
|
|
|
|
|
|
// Includes
|
|
#include <string>
|
|
|
|
#include "mbed.h"
|
|
|
|
// Defines
|
|
#define EEPROM_Address 0xa0U
|
|
|
|
#define EEPROM_NoError 0x00U
|
|
#define EEPROM_BadAddress 0x01U
|
|
#define EEPROM_I2cError 0x02U
|
|
#define EEPROM_ParamError 0x03U
|
|
#define EEPROM_OutOfRange 0x04U
|
|
#define EEPROM_MallocError 0x05U
|
|
|
|
#define EEPROM_MaxError 6U
|
|
|
|
static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = {
|
|
"",
|
|
"Bad chip address",
|
|
"I2C error (nack)",
|
|
"Invalid parameter",
|
|
"Data address out of range",
|
|
"Memory allocation error"
|
|
};
|
|
|
|
/** EEPROM Class
|
|
*/
|
|
class EEPROM {
|
|
public:
|
|
enum TypeEeprom {
|
|
T24C01 = 128U, T24C02 = 256U, T24C04 = 512U, T24C08 = 1024U, T24C16 = 2048U,
|
|
T24C32 = 4096U, T24C64 = 8192U, T24C128 = 16384U, T24C256 = 32768U,
|
|
T24C512 = 65536U, T24C1024 = 131072U, T24C1025 = 131073U
|
|
} Type;
|
|
|
|
/**
|
|
* Constructor, initialize the eeprom on i2c interface.
|
|
* @param sda sda i2c pin (PinName)
|
|
* @param scl scl i2c pin (PinName)
|
|
* @param address eeprom address, according to eeprom type (uint8_t)
|
|
* @param type eeprom type (TypeEeprom)
|
|
* @return none
|
|
*/
|
|
EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type);
|
|
|
|
/**
|
|
* Random read byte
|
|
* @param address start address (uint32_t)
|
|
* @param data byte to read (int8_t&)
|
|
* @return none
|
|
*/
|
|
void read(uint32_t address, int8_t &data);
|
|
|
|
/**
|
|
* Random read short
|
|
* @param address start address (uint32_t)
|
|
* @param data short to read (int16_t&)
|
|
* @return none
|
|
*/
|
|
void read(uint32_t address, int16_t &data);
|
|
|
|
/**
|
|
* Random read long
|
|
* @param address start address (uint32_t)
|
|
* @param data long to read (int32_t&)
|
|
* @return none
|
|
*/
|
|
void read(uint32_t address, int32_t &data);
|
|
|
|
/**
|
|
* Random read float
|
|
* @param address start address (uint32_t)
|
|
* @param data float to read (float&)
|
|
* @return none
|
|
*/
|
|
void read(uint32_t address, float &data);
|
|
|
|
/**
|
|
* Random read anything
|
|
* @param address start address (uint32_t)
|
|
* @param data data to read (void *)
|
|
* @param size number of bytes to read (uint32_t)
|
|
* @return none
|
|
*/
|
|
void read(uint32_t address, void *data, uint32_t size);
|
|
|
|
/**
|
|
* Current address read byte
|
|
* @param data byte to read (int8_t&)
|
|
* @return none
|
|
*/
|
|
void read(int8_t &data);
|
|
|
|
/**
|
|
* Sequential read byte
|
|
* @param address start address (uint32_t)
|
|
* @param data bytes array to read (int8_t[]&)
|
|
* @param size number of bytes to read (uint32_t)
|
|
* @return none
|
|
*/
|
|
void read(uint32_t address, int8_t *data, uint32_t size);
|
|
|
|
/**
|
|
* Write byte
|
|
* @param address start address (uint32_t)
|
|
* @param data byte to write (int8_t)
|
|
* @return none
|
|
*/
|
|
void write(uint32_t address, int8_t data);
|
|
|
|
/**
|
|
* Write short
|
|
* @param address start address (uint32_t)
|
|
* @param data short to write (int16_t)
|
|
* @return none
|
|
*/
|
|
void write(uint32_t address, int16_t data);
|
|
|
|
/**
|
|
* Write long
|
|
* @param address start address (uint32_t)
|
|
* @param data long to write (int32_t)
|
|
* @return none
|
|
*/
|
|
void write(uint32_t address, int32_t data);
|
|
|
|
/**
|
|
* Write float
|
|
* @param address start address (uint32_t)
|
|
* @param data float to write (float)
|
|
* @return none
|
|
*/
|
|
void write(uint32_t address, float data);
|
|
|
|
/**
|
|
* Write anything (use the page write mode)
|
|
* @param address start address (uint32_t)
|
|
* @param data data to write (void *)
|
|
* @param size number of bytes to write (uint32_t)
|
|
* @return none
|
|
*/
|
|
void write(uint32_t address, void *data, uint32_t size);
|
|
|
|
/**
|
|
* Write array of bytes (use the page mode)
|
|
* @param address start address (uint32_t)
|
|
* @param data bytes array to write (int8_t[])
|
|
* @param size number of bytes to write (uint32_t)
|
|
* @return none
|
|
*/
|
|
void write(uint32_t address, const int8_t data[], uint32_t size);
|
|
|
|
/**
|
|
* Wait eeprom ready
|
|
* @param none
|
|
* @return none
|
|
*/
|
|
void ready();
|
|
|
|
/**
|
|
* Get eeprom size in bytes
|
|
* @param none
|
|
* @return size in bytes (uint32_t)
|
|
*/
|
|
uint32_t getSize();
|
|
|
|
/**
|
|
* Get eeprom name
|
|
* @param none
|
|
* @return name (const char*)
|
|
*/
|
|
const char *getName();
|
|
|
|
/**
|
|
* Clear eeprom (write with 0)
|
|
* @param none
|
|
* @return none
|
|
*/
|
|
void clear();
|
|
|
|
/**
|
|
* Get the current error number (EEPROM_NoError if no error)
|
|
* @param none
|
|
* @return none
|
|
*/
|
|
uint8_t getError();
|
|
|
|
/**
|
|
* Get current error message
|
|
* @param none
|
|
* @return current error message(std::string)
|
|
*/
|
|
std::string getErrorMessage() const {
|
|
return (_ErrorMessageEEPROM[_errnum]);
|
|
}
|
|
|
|
//---------- local variables ----------
|
|
private:
|
|
I2C _i2c; // Local i2c communication interface instance
|
|
int _address; // Local i2c address
|
|
uint8_t _errnum; // Error number
|
|
TypeEeprom _type; // EEPROM type
|
|
uint8_t _page_write; // Page write size
|
|
uint8_t _page_number; // Number of page
|
|
uint32_t _size; // Size in bytes
|
|
bool checkAddress(uint32_t address); // Check address range
|
|
static const char *const _name[]; // eeprom name
|
|
//-------------------------------------
|
|
};
|
|
|
|
#endif
|