Исправления
This commit is contained in:
91
eeprom.cpp
91
eeprom.cpp
@@ -42,8 +42,7 @@ const char * const EEPROM::_name[] = {"24C01","24C02","24C04","24C08","24C16","2
|
|||||||
* @param type eeprom type (TypeEeprom)
|
* @param type eeprom type (TypeEeprom)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
EEPROM::EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl)
|
EEPROM::EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl) {
|
||||||
{
|
|
||||||
|
|
||||||
_errnum = EEPROM_NoError;
|
_errnum = EEPROM_NoError;
|
||||||
_type = type;
|
_type = type;
|
||||||
@@ -143,8 +142,7 @@ EEPROM::EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2
|
|||||||
* @param data byte to write (int8_t)
|
* @param data byte to write (int8_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::write(uint32_t address, int8_t data)
|
void EEPROM::write(uint32_t address, int8_t data) {
|
||||||
{
|
|
||||||
uint8_t page;
|
uint8_t page;
|
||||||
uint8_t addr;
|
uint8_t addr;
|
||||||
uint8_t cmd[3];
|
uint8_t cmd[3];
|
||||||
@@ -177,8 +175,7 @@ void EEPROM::write(uint32_t address, int8_t data)
|
|||||||
|
|
||||||
// Data
|
// Data
|
||||||
cmd[1] = (uint8_t) data;
|
cmd[1] = (uint8_t) data;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
len = 3;
|
len = 3;
|
||||||
|
|
||||||
// First word address (MSB)
|
// First word address (MSB)
|
||||||
@@ -211,8 +208,7 @@ void EEPROM::write(uint32_t address, int8_t data)
|
|||||||
* @param size number of bytes to write (uint32_t)
|
* @param size number of bytes to write (uint32_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
|
void EEPROM::write(uint32_t address, const int8_t data[], uint32_t length) {
|
||||||
{
|
|
||||||
uint8_t page;
|
uint8_t page;
|
||||||
uint8_t addr = 0;
|
uint8_t addr = 0;
|
||||||
uint8_t blocs, remain;
|
uint8_t blocs, remain;
|
||||||
@@ -273,8 +269,7 @@ void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
|
|||||||
|
|
||||||
// Increment address
|
// Increment address
|
||||||
address += _page_write;
|
address += _page_write;
|
||||||
}
|
} else { // Data on 2 pages. We must split the write
|
||||||
else { // Data on 2 pages. We must split the write
|
|
||||||
// Number of bytes in current page
|
// Number of bytes in current page
|
||||||
fpart = (page + 1) * 256 - address;
|
fpart = (page + 1) * 256 - address;
|
||||||
|
|
||||||
@@ -329,8 +324,7 @@ void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
|
|||||||
address += lpart;
|
address += lpart;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// First word address (MSB)
|
// First word address (MSB)
|
||||||
cmd[0] = (uint8_t) (address >> 8);
|
cmd[0] = (uint8_t) (address >> 8);
|
||||||
|
|
||||||
@@ -383,8 +377,7 @@ void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
|
|||||||
|
|
||||||
// Wait end of write
|
// Wait end of write
|
||||||
ready();
|
ready();
|
||||||
}
|
} else { // Data on 2 pages. We must split the write
|
||||||
else { // Data on 2 pages. We must split the write
|
|
||||||
// Number of bytes in current page
|
// Number of bytes in current page
|
||||||
fpart = (page + 1) * 256 - address;
|
fpart = (page + 1) * 256 - address;
|
||||||
|
|
||||||
@@ -436,8 +429,7 @@ void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
|
|||||||
ready();
|
ready();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Fist word address (MSB)
|
// Fist word address (MSB)
|
||||||
cmd[0] = (uint8_t) (address >> 8);
|
cmd[0] = (uint8_t) (address >> 8);
|
||||||
|
|
||||||
@@ -470,8 +462,7 @@ void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
|
|||||||
* @param data short to write (int16_t)
|
* @param data short to write (int16_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::write(uint32_t address, int16_t data)
|
void EEPROM::write(uint32_t address, int16_t data) {
|
||||||
{
|
|
||||||
int8_t cmd[2];
|
int8_t cmd[2];
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
@@ -498,8 +489,7 @@ void EEPROM::write(uint32_t address, int16_t data)
|
|||||||
* @param data long to write (int32_t)
|
* @param data long to write (int32_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::write(uint32_t address, int32_t data)
|
void EEPROM::write(uint32_t address, int32_t data) {
|
||||||
{
|
|
||||||
int8_t cmd[4];
|
int8_t cmd[4];
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
@@ -526,8 +516,7 @@ void EEPROM::write(uint32_t address, int32_t data)
|
|||||||
* @param data float to write (float)
|
* @param data float to write (float)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::write(uint32_t address, float data)
|
void EEPROM::write(uint32_t address, float data) {
|
||||||
{
|
|
||||||
int8_t cmd[4];
|
int8_t cmd[4];
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
@@ -555,9 +544,8 @@ void EEPROM::write(uint32_t address, float data)
|
|||||||
* @param size number of bytes to write (uint32_t)
|
* @param size number of bytes to write (uint32_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::write(uint32_t address, void *data, uint32_t size)
|
void EEPROM::write(uint32_t address, void *data, uint32_t size) {
|
||||||
{
|
int8_t *cmd = nullptr;
|
||||||
int8_t *cmd = NULL;
|
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
if (_errnum)
|
if (_errnum)
|
||||||
@@ -570,7 +558,7 @@ void EEPROM::write(uint32_t address, void *data, uint32_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd = (int8_t *) malloc(size);
|
cmd = (int8_t *) malloc(size);
|
||||||
if(cmd == NULL) {
|
if (cmd == nullptr) {
|
||||||
_errnum = EEPROM_MallocError;
|
_errnum = EEPROM_MallocError;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -591,8 +579,7 @@ void EEPROM::write(uint32_t address, void *data, uint32_t size)
|
|||||||
* @param data byte to read (int8_t&)
|
* @param data byte to read (int8_t&)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(uint32_t address, int8_t& data)
|
void EEPROM::read(uint32_t address, int8_t &data) {
|
||||||
{
|
|
||||||
uint8_t page;
|
uint8_t page;
|
||||||
uint8_t addr;
|
uint8_t addr;
|
||||||
uint8_t cmd[2];
|
uint8_t cmd[2];
|
||||||
@@ -622,8 +609,7 @@ void EEPROM::read(uint32_t address, int8_t& data)
|
|||||||
|
|
||||||
// Word address
|
// Word address
|
||||||
cmd[0] = (uint8_t) (address - page * 256);
|
cmd[0] = (uint8_t) (address - page * 256);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
len = 2;
|
len = 2;
|
||||||
|
|
||||||
// First word address (MSB)
|
// First word address (MSB)
|
||||||
@@ -658,8 +644,7 @@ void EEPROM::read(uint32_t address, int8_t& data)
|
|||||||
* @param size number of bytes to read (uint32_t)
|
* @param size number of bytes to read (uint32_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(uint32_t address, int8_t *data, uint32_t size)
|
void EEPROM::read(uint32_t address, int8_t *data, uint32_t size) {
|
||||||
{
|
|
||||||
uint8_t page;
|
uint8_t page;
|
||||||
uint8_t addr;
|
uint8_t addr;
|
||||||
uint8_t cmd[2];
|
uint8_t cmd[2];
|
||||||
@@ -695,8 +680,7 @@ void EEPROM::read(uint32_t address, int8_t *data, uint32_t size)
|
|||||||
|
|
||||||
// Word address
|
// Word address
|
||||||
cmd[0] = (uint8_t) (address - page * 256);
|
cmd[0] = (uint8_t) (address - page * 256);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
len = 2;
|
len = 2;
|
||||||
|
|
||||||
// First word address (MSB)
|
// First word address (MSB)
|
||||||
@@ -714,7 +698,7 @@ void EEPROM::read(uint32_t address, int8_t *data, uint32_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sequential read
|
// Sequential read
|
||||||
ack = _i2c.read((int)addr,(char *)data,size);
|
ack = _i2c.read((int) addr, (char *) data, static_cast<int>(size));
|
||||||
if (ack != 0) {
|
if (ack != 0) {
|
||||||
_errnum = EEPROM_I2cError;
|
_errnum = EEPROM_I2cError;
|
||||||
return;
|
return;
|
||||||
@@ -729,8 +713,7 @@ void EEPROM::read(uint32_t address, int8_t *data, uint32_t size)
|
|||||||
* @param data byte to read (int8_t&)
|
* @param data byte to read (int8_t&)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(int8_t& data)
|
void EEPROM::read(int8_t &data) {
|
||||||
{
|
|
||||||
uint8_t addr;
|
uint8_t addr;
|
||||||
int ack;
|
int ack;
|
||||||
|
|
||||||
@@ -758,8 +741,7 @@ void EEPROM::read(int8_t& data)
|
|||||||
* @param data short to read (int16_t&)
|
* @param data short to read (int16_t&)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(uint32_t address, int16_t& data)
|
void EEPROM::read(uint32_t address, int16_t &data) {
|
||||||
{
|
|
||||||
int8_t cmd[2];
|
int8_t cmd[2];
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
@@ -786,8 +768,7 @@ void EEPROM::read(uint32_t address, int16_t& data)
|
|||||||
* @param data long to read (int32_t&)
|
* @param data long to read (int32_t&)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(uint32_t address, int32_t& data)
|
void EEPROM::read(uint32_t address, int32_t &data) {
|
||||||
{
|
|
||||||
int8_t cmd[4];
|
int8_t cmd[4];
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
@@ -814,8 +795,7 @@ void EEPROM::read(uint32_t address, int32_t& data)
|
|||||||
* @param data float to read (float&)
|
* @param data float to read (float&)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(uint32_t address, float& data)
|
void EEPROM::read(uint32_t address, float &data) {
|
||||||
{
|
|
||||||
int8_t cmd[4];
|
int8_t cmd[4];
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
@@ -843,9 +823,8 @@ void EEPROM::read(uint32_t address, float& data)
|
|||||||
* @param size number of bytes to read (uint32_t)
|
* @param size number of bytes to read (uint32_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::read(uint32_t address, void *data, uint32_t size)
|
void EEPROM::read(uint32_t address, void *data, uint32_t size) {
|
||||||
{
|
int8_t *cmd = nullptr;
|
||||||
int8_t *cmd = NULL;
|
|
||||||
|
|
||||||
// Check error
|
// Check error
|
||||||
if (_errnum)
|
if (_errnum)
|
||||||
@@ -859,7 +838,7 @@ void EEPROM::read(uint32_t address, void *data, uint32_t size)
|
|||||||
|
|
||||||
cmd = (int8_t *) malloc(size);
|
cmd = (int8_t *) malloc(size);
|
||||||
|
|
||||||
if(cmd == NULL) {
|
if (cmd == nullptr) {
|
||||||
_errnum = EEPROM_MallocError;
|
_errnum = EEPROM_MallocError;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -879,8 +858,7 @@ void EEPROM::read(uint32_t address, void *data, uint32_t size)
|
|||||||
* @param none
|
* @param none
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::clear(void)
|
void EEPROM::clear() {
|
||||||
{
|
|
||||||
int32_t data;
|
int32_t data;
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
|
||||||
@@ -898,8 +876,7 @@ void EEPROM::clear(void)
|
|||||||
* @param none
|
* @param none
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void EEPROM::ready(void)
|
void EEPROM::ready() {
|
||||||
{
|
|
||||||
int ack;
|
int ack;
|
||||||
uint8_t addr;
|
uint8_t addr;
|
||||||
uint8_t cmd[2];
|
uint8_t cmd[2];
|
||||||
@@ -928,8 +905,7 @@ void EEPROM::ready(void)
|
|||||||
* @param none
|
* @param none
|
||||||
* @return size in bytes (uint32_t)
|
* @return size in bytes (uint32_t)
|
||||||
*/
|
*/
|
||||||
uint32_t EEPROM::getSize(void)
|
uint32_t EEPROM::getSize() {
|
||||||
{
|
|
||||||
return (_size);
|
return (_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -940,8 +916,7 @@ uint32_t EEPROM::getSize(void)
|
|||||||
* @param none
|
* @param none
|
||||||
* @return name (const char*)
|
* @return name (const char*)
|
||||||
*/
|
*/
|
||||||
const char* EEPROM::getName(void)
|
const char *EEPROM::getName() {
|
||||||
{
|
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
|
|
||||||
switch (_type) {
|
switch (_type) {
|
||||||
@@ -993,8 +968,7 @@ const char* EEPROM::getName(void)
|
|||||||
* @param none
|
* @param none
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
uint8_t EEPROM::getError(void)
|
uint8_t EEPROM::getError() {
|
||||||
{
|
|
||||||
return (_errnum);
|
return (_errnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1005,8 +979,7 @@ uint8_t EEPROM::getError(void)
|
|||||||
* @param address address to check (uint32_t)
|
* @param address address to check (uint32_t)
|
||||||
* @return true if in eeprom range, overwise false (bool)
|
* @return true if in eeprom range, overwise false (bool)
|
||||||
*/
|
*/
|
||||||
bool EEPROM::checkAddress(uint32_t address)
|
bool EEPROM::checkAddress(uint32_t address) {
|
||||||
{
|
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
switch (_type) {
|
switch (_type) {
|
||||||
|
|||||||
22
eeprom.h
22
eeprom.h
@@ -32,9 +32,11 @@ static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = {
|
|||||||
*/
|
*/
|
||||||
class EEPROM {
|
class EEPROM {
|
||||||
public:
|
public:
|
||||||
enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048,
|
enum TypeEeprom {
|
||||||
|
T24C01 = 128, T24C02 = 256, T24C04 = 512, T24C08 = 1024, T24C16 = 2048,
|
||||||
T24C32 = 4096, T24C64 = 8192, T24C128 = 16384, T24C256 = 32768,
|
T24C32 = 4096, T24C64 = 8192, T24C128 = 16384, T24C256 = 32768,
|
||||||
T24C512=65536,T24C1024=131072,T24C1025=131073} Type;
|
T24C512 = 65536, T24C1024 = 131072, T24C1025 = 131073
|
||||||
|
} Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor, initialize the eeprom on i2c interface.
|
* Constructor, initialize the eeprom on i2c interface.
|
||||||
@@ -151,50 +153,49 @@ public:
|
|||||||
* @param size number of bytes to write (uint32_t)
|
* @param size number of bytes to write (uint32_t)
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void write(uint32_t address, int8_t data[], uint32_t size);
|
void write(uint32_t address, const int8_t data[], uint32_t size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait eeprom ready
|
* Wait eeprom ready
|
||||||
* @param none
|
* @param none
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void ready(void);
|
void ready();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get eeprom size in bytes
|
* Get eeprom size in bytes
|
||||||
* @param none
|
* @param none
|
||||||
* @return size in bytes (uint32_t)
|
* @return size in bytes (uint32_t)
|
||||||
*/
|
*/
|
||||||
uint32_t getSize(void);
|
uint32_t getSize();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get eeprom name
|
* Get eeprom name
|
||||||
* @param none
|
* @param none
|
||||||
* @return name (const char*)
|
* @return name (const char*)
|
||||||
*/
|
*/
|
||||||
const char* getName(void);
|
const char *getName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear eeprom (write with 0)
|
* Clear eeprom (write with 0)
|
||||||
* @param none
|
* @param none
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void clear(void);
|
void clear();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current error number (EEPROM_NoError if no error)
|
* Get the current error number (EEPROM_NoError if no error)
|
||||||
* @param none
|
* @param none
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
uint8_t getError(void);
|
uint8_t getError();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get current error message
|
* Get current error message
|
||||||
* @param none
|
* @param none
|
||||||
* @return current error message(std::string)
|
* @return current error message(std::string)
|
||||||
*/
|
*/
|
||||||
std::string getErrorMessage(void)
|
std::string getErrorMessage(void) const {
|
||||||
{
|
|
||||||
return (_ErrorMessageEEPROM[_errnum]);
|
return (_ErrorMessageEEPROM[_errnum]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,4 +212,5 @@ private:
|
|||||||
static const char *const _name[]; // eeprom name
|
static const char *const _name[]; // eeprom name
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user