Skip to content

Commit

Permalink
Change C-style casts to static_cast.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Feb 8, 2022
1 parent bc0c6b9 commit fd425a3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 37 deletions.
67 changes: 42 additions & 25 deletions examples/eepromTest/eepromTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// Two 24LC256 EEPROMs on the bus
JC_EEPROM eep(JC_EEPROM::kbits_256, 2, 64); // device size, number of devices, page size

constexpr bool erase {false}; // true writes 0xFF to all addresses,
// false performs write/read test
constexpr uint32_t totalKBytes {64}; // for read and write test functions
constexpr uint8_t btnStart {6}; // pin for start button

Expand All @@ -30,24 +32,27 @@ void setup()
Serial << endl << F("extEEPROM.begin() failed, status = ") << eepStatus << endl;
while (1);
}

Serial << endl << F("Press button to start...") << endl;
while (digitalRead(btnStart) == HIGH) delay(10); // wait for button push

// chunkSize can be changed, but must be a multiple of 4 since we're writing 32-bit integers
uint8_t chunkSize = 64;
//eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
eeWrite(chunkSize);
eeRead(chunkSize);

dump(0, 16); // the first 16 bytes
dump(32752, 32); // across the device boundary
dump(65520, 16); // the last 16 bytes
uint8_t chunkSize = 64;
if (erase) {
eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
dump(0, totalKBytes * 1024); // dump everything
}
else {
eeWrite(chunkSize);
eeRead(chunkSize);
dump(0, 16); // the first 16 bytes
dump(32752, 32); // across the device boundary
dump(65520, 16); // the last 16 bytes
}
Serial << "\nDone.\n";
}

void loop()
{
}
void loop() {}

// write test data (32-bit integers) to eeprom, "chunk" bytes at a time
void eeWrite(uint8_t chunk)
Expand All @@ -57,7 +62,7 @@ void eeWrite(uint8_t chunk)
uint32_t val = 0;
Serial << F("Writing...") << endl;
uint32_t msStart = millis();

for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
if ( (addr &0xFFF) == 0 ) Serial << addr << endl;
for (uint8_t c = 0; c < chunk; c += 4) {
Expand All @@ -81,7 +86,7 @@ void eeRead(uint8_t chunk)
uint32_t val = 0, testVal;
Serial << F("Reading...") << endl;
uint32_t msStart = millis();

for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
if ( (addr &0xFFF) == 0 ) Serial << addr << endl;
eep.read(addr, data, chunk);
Expand All @@ -103,7 +108,7 @@ void eeErase(uint8_t chunk, uint32_t startAddr, uint32_t endAddr)
Serial << F("Erasing...") << endl;
for (int16_t i = 0; i < chunk; i++) data[i] = 0xFF;
uint32_t msStart = millis();

for (uint32_t a = startAddr; a <= endAddr; a += chunk) {
if ( (a &0xFFF) == 0 ) Serial << a << endl;
eep.write(a, data, chunk);
Expand All @@ -114,24 +119,36 @@ void eeErase(uint8_t chunk, uint32_t startAddr, uint32_t endAddr)

// dump eeprom contents, 16 bytes at a time.
// always dumps a multiple of 16 bytes.
// duplicate rows are suppressed and indicated with an asterisk.
void dump(uint32_t startAddr, uint32_t nBytes)
{
Serial << endl << F("EEPROM DUMP 0x") << _HEX(startAddr) << F(" 0x") << _HEX(nBytes) << ' ' << startAddr << ' ' << nBytes << endl;
uint32_t nRows = (nBytes + 15) >> 4;

uint8_t d[16];
uint8_t d[16], last[16];
uint32_t aLast {startAddr};
for (uint32_t r = 0; r < nRows; r++) {
uint32_t a = startAddr + 16 * r;
eep.read(a, d, 16);
Serial << "0x";
if ( a < 16 * 16 * 16 ) Serial << '0';
if ( a < 16 * 16 ) Serial << '0';
if ( a < 16 ) Serial << '0';
Serial << _HEX(a) << ' ';
for ( int16_t c = 0; c < 16; c++ ) {
if ( d[c] < 16 ) Serial << '0';
Serial << _HEX( d[c] ) << ( c == 7 ? " " : " " );
bool same {true};
for (int i=0; i<16; ++i) {
if (last[i] != d[i]) same = false;
}
if (!same || r == 0 || r == nRows-1) {
Serial << "0x";
if ( a < 16 * 16 * 16 ) Serial << '0';
if ( a < 16 * 16 ) Serial << '0';
if ( a < 16 ) Serial << '0';
Serial << _HEX(a) << (a == aLast+16 || r == 0 ? " " : "* ");
for ( int16_t c = 0; c < 16; c++ ) {
if ( d[c] < 16 ) Serial << '0';
Serial << _HEX( d[c] ) << ( c == 7 ? " " : " " );
}
Serial << endl;
aLast = a;
}
for (int i=0; i<16; ++i) {
last[i] = d[i];
}
Serial << endl;
}
}
46 changes: 43 additions & 3 deletions examples/struct/struct.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ JC_EEPROM eep(JC_EEPROM::kbits_256, 1, 64); // 24LC256

void setup()
{
constexpr uint32_t addr {61};
eep.begin();
Serial.begin(115200);
Serial << F( "\n" __FILE__ "\n" __DATE__ " " __TIME__ "\n" );

Serial << "Writing\n";
myStruct w { 1, 2, 3, 4, 5, true };
eep.write(0, reinterpret_cast<uint8_t*>(&w), sizeof(w));
Serial << "Writing " << sizeof(w) << " bytes\n";
uint8_t s = eep.write(addr, reinterpret_cast<uint8_t*>(&w), sizeof(w));
Serial << "Write status " << s << endl;
printStruct(w);

Serial << "Reading\n";
myStruct r;
eep.read(0, reinterpret_cast<uint8_t*>(&r), sizeof(r));
eep.read(addr, reinterpret_cast<uint8_t*>(&r), sizeof(r));
printStruct(r);
dump(0, 32 * 1024UL); // dump all eeprom
Serial << "\nDone.\n";
}

void loop() {}
Expand All @@ -43,3 +47,39 @@ void printStruct(myStruct s)
Serial << s.a << ' ' << s.b << ' ' << s.c << ' ' << s.d << ' ';
Serial << s.e << ' ' << s.f << endl;
}

// dump eeprom contents, 16 bytes at a time.
// always dumps a multiple of 16 bytes.
// duplicate rows are suppressed and indicated with an asterisk.
void dump(uint32_t startAddr, uint32_t nBytes)
{
Serial << endl << F("EEPROM DUMP 0x") << _HEX(startAddr) << F(" 0x") << _HEX(nBytes) << ' ' << startAddr << ' ' << nBytes << endl;
uint32_t nRows = (nBytes + 15) >> 4;

uint8_t d[16], last[16];
uint32_t aLast {startAddr};
for (uint32_t r = 0; r < nRows; r++) {
uint32_t a = startAddr + 16 * r;
eep.read(a, d, 16);
bool same {true};
for (int i=0; i<16; ++i) {
if (last[i] != d[i]) same = false;
}
if (!same || r == 0 || r == nRows-1) {
Serial << "0x";
if ( a < 16 * 16 * 16 ) Serial << '0';
if ( a < 16 * 16 ) Serial << '0';
if ( a < 16 ) Serial << '0';
Serial << _HEX(a) << (a == aLast+16 || r == 0 ? " " : "* ");
for ( int16_t c = 0; c < 16; c++ ) {
if ( d[c] < 16 ) Serial << '0';
Serial << _HEX( d[c] ) << ( c == 7 ? " " : " " );
}
Serial << endl;
aLast = a;
}
for (int i=0; i<16; ++i) {
last[i] = d[i];
}
}
}
12 changes: 6 additions & 6 deletions src/JC_EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ uint8_t JC_EEPROM::write(uint32_t addr, uint8_t* values, uint16_t nBytes)
// find min(nBytes, nPage, BUFFER_LENGTH) -- BUFFER_LENGTH is defined in the Wire library.
nWrite = nBytes < nPage ? nBytes : nPage;
nWrite = BUFFER_LENGTH - m_nAddrBytes < nWrite ? BUFFER_LENGTH - m_nAddrBytes : nWrite;
ctrlByte = m_eepromAddr | (byte) (addr >> m_csShift);
ctrlByte = m_eepromAddr | static_cast<uint8_t>(addr >> m_csShift);
Wire.beginTransmission(ctrlByte);
if (m_nAddrBytes == 2) Wire.write( (byte) (addr >> 8) ); // high addr byte
Wire.write( (byte) addr ); // low addr byte
if (m_nAddrBytes == 2) Wire.write(static_cast<uint8_t>(addr >> 8)); // high addr byte
Wire.write(static_cast<uint8_t>(addr)); // low addr byte
Wire.write(values, nWrite);
txStatus = Wire.endTransmission();
if (txStatus != 0) return txStatus;
Expand Down Expand Up @@ -121,10 +121,10 @@ uint8_t JC_EEPROM::read(uint32_t addr, uint8_t* values, uint16_t nBytes)
nPage = m_pageSize - ( addr & (m_pageSize - 1) );
nRead = nBytes < nPage ? nBytes : nPage;
nRead = BUFFER_LENGTH < nRead ? BUFFER_LENGTH : nRead;
ctrlByte = m_eepromAddr | (byte) (addr >> m_csShift);
ctrlByte = m_eepromAddr | static_cast<uint8_t>(addr >> m_csShift);
Wire.beginTransmission(ctrlByte);
if (m_nAddrBytes == 2) Wire.write( (byte) (addr >> 8) ); // high addr byte
Wire.write( (byte) addr ); // low addr byte
if (m_nAddrBytes == 2) Wire.write(static_cast<uint8_t>(addr >> 8)); // high addr byte
Wire.write(static_cast<uint8_t>(addr)); // low addr byte
rxStatus = Wire.endTransmission();
if (rxStatus != 0) return rxStatus; // read error

Expand Down
6 changes: 3 additions & 3 deletions src/JC_EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ class JC_EEPROM
{return (read(addr) == value) ? 0 : write(addr, &value, 1); }

private:
uint8_t m_eepromAddr; // eeprom i2c address
uint8_t m_eepromAddr; // eeprom i2c address
uint16_t m_dvcCapacity; // capacity of one EEPROM device, in kbits
uint8_t m_nDevice; // number of devices on the bus
uint8_t m_nDevice; // number of devices on the bus
uint16_t m_pageSize; // page size in bytes
uint8_t m_csShift; // number of bits to shift address for chip select bits in control byte
uint8_t m_csShift; // number of bits to shift address for chip select bits in control byte
uint16_t m_nAddrBytes; // number of address bytes (1 or 2)
uint32_t m_totalCapacity; // capacity of all EEPROM devices on the bus, in bytes
};
Expand Down

0 comments on commit fd425a3

Please sign in to comment.