Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: AT24C64D.cpp
- Revision:
- 3:8a0bfc787463
- Parent:
- 2:7efbd43bfe7c
- Child:
- 4:660801ce718c
--- a/AT24C64D.cpp Fri Feb 22 10:05:29 2019 +0000
+++ b/AT24C64D.cpp Fri Mar 01 11:26:26 2019 +0000
@@ -22,12 +22,16 @@
cBuffer[a] = 0x00;
printf("end\n");
+
+ uiAddrWrite = 0;
+ uiAddrRead = 0;
}
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Sets the address for the writing position. If the address is higher as the //
+// max. memory address, then the writing address will be 0x00. //
//******************************************************************************//
void AT24C64D::setAddrWrite(uint16_t Addr){
if(uiAddrWrite > MEM_ADDR_MAX) uiAddrWrite = 0;
@@ -37,7 +41,8 @@
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Increments the writing address. If the address is higher as the max. memory //
+// adress, it will be roll over. //
//******************************************************************************//
void AT24C64D::incAddrWrite(){
if(uiAddrWrite >= MEM_ADDR_MAX) uiAddrWrite = 0;
@@ -47,7 +52,8 @@
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Decrements the writing address. If the address is smaller or equals to zero, //
+// it will be roll over. //
//******************************************************************************//
void AT24C64D::decAddrWrite(){
if(uiAddrWrite <= 0) uiAddrWrite = 4095;
@@ -57,7 +63,7 @@
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Returns the current writing address. //
//******************************************************************************//
uint16_t AT24C64D::getAddrWrite(){
return uiAddrWrite;
@@ -66,17 +72,19 @@
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Sets the address for the reading position. If the address is higher as the //
+// max. memory address, then the writing address will be 0x00. //
//******************************************************************************//
void AT24C64D::setAddrRead(uint16_t Addr){
- if(uiAddrRead > MEM_ADDR_MAX) uiAddrRead = 0;
+ if(uiAddrRead > MEM_ADDR_MAX) uiAddrRead = 0;
else uiAddrRead = Addr;
}
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Increments the writing address. If the address is higher as the max. memory //
+// adress, it will be roll over. //
//******************************************************************************//
void AT24C64D::incAddrRead(){
if(uiAddrRead >= MEM_ADDR_MAX) uiAddrRead = 0;
@@ -86,17 +94,18 @@
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Decrements the writing address. If the address is smaller or equals to zero, //
+// it will be roll over. //
//******************************************************************************//
void AT24C64D::decAddrRead(){
- if(uiAddrRead <= 0) uiAddrRead = 4095;
+ if(uiAddrRead <= 0) uiAddrRead = 4095;
else uiAddrRead--;
}
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// Returns the current reading address. //
//******************************************************************************//
uint16_t AT24C64D::getAddrRead(){
return uiAddrRead;
@@ -104,10 +113,10 @@
//******************************************************************************//
-// uiAddr: user address [A2...A0]
+// After an interruption in protocol, power loss or system reset, the 2-wire //
+// part can be protocol reset. //
//******************************************************************************//
void AT24C64D::reset(){
-
i2c->start();
i2c->write(0x00);
i2c->start();
@@ -119,7 +128,7 @@
/******************************************************************************/
-//
+// not implemented
/******************************************************************************/
bool AT24C64D::erase(){
return ACK;
@@ -128,39 +137,44 @@
//******************************************************************************//
-// Writes a data byte to address uiAddr. If all transmitions are successful, //
-// it will be returned a ACK. Otherwise a NACK. After every successful //
-// transmition, the writeAddr will new set and at the end incremented. //
+// Writes a data byte to the current write address (uiAddrWrite). If all //
+// transmitions are successful, it will be returned a ACK. Otherwise a NACK. //
+// The uiAddrWrite will be incremented, if the transmition was successfull. //
//******************************************************************************//
-bool AT24C64D::writeByte(uint16_t uiAddr, char * cData){
+bool AT24C64D::write(char * cData){
+ return write(uiAddrWrite, cData);
+}
+
+
+//******************************************************************************//
+// Writes a data byte to the given address uiAddr. If all transmitions are //
+// successful, it will be returned a ACK. Otherwise a NACK. The uiAddrWrite //
+// will be set and incremented, if the transmition was successfull. //
+//******************************************************************************//
+bool AT24C64D::write(uint16_t uiAddr, char * cData){
bAck = NACK;
uiTimeOut = TIMEOUT_VAL;
- while(bAck != ACK){
- i2c->start();
- bAck = i2c->write(AT24C64D_W); // return 0 -> NACK | 1 -> ACK | 2 -> TimeOut
- i2c->stop();
- uiTimeOut--;
-
- if(uiTimeOut == 0) return NACK;
- }
+
+ while(not isReady());
+ ACKpolling(AT24C64D_W);
-
+// printf("TimeOut: %d\n", uiTimeOut);
- uiAddrWrite = uiAddr & ADDR_MASK;
- cBuffer[0] = (uiAddrWrite >> 8) & 0xFF;
- cBuffer[1] = uiAddrWrite & 0xFF;
+ uiAddr = uiAddr & ADDR_MASK;
+ cBuffer[0] = (uiAddr >> 8) & 0xFF;
+ cBuffer[1] = uiAddr & 0xFF;
cBuffer[2] = (*cData);
//printf("Write on EEPROM address 0x%02x %02x the Byte 0x%02x\n", cBuffer[0], cBuffer[1], cBuffer[2]);
- while(not isReady());
bAck &= i2c->write(AT24C64D_W, cBuffer, 3);
- //wait(0.004);
startTimer();
- if(bAck) incAddrWrite();
+
+ uiAddrWrite = uiAddr;
+ incAddrWrite();
return bAck;
}
@@ -171,7 +185,56 @@
// it will be returned a ACK. Otherwise a NACK. After every successful //
// transmition, the writeAddr will new set and at the end incremented. //
//******************************************************************************//
-//bool AT24C64D::write(uint16_t uiAddr, char * cData, uint16_t uiLength){}
+bool AT24C64D::write(uint16_t uiAddr, char * cData, uint16_t uiLength){
+ return NACK;
+}
+
+
+
+
+//******************************************************************************//
+//
+//******************************************************************************//
+bool AT24C64D::read(char *cData){
+
+ //bAck = ACKpolling(AT24C64D_R); // polling for ACK
+
+ //if(bAck == ACK){
+ i2c->start(); // send a start condition
+ i2c->write(AT24C64D_R);
+ *cData = i2c->read(false);
+ i2c->stop();
+ //}
+ return bAck;
+}
+
+
+//******************************************************************************//
+//
+//******************************************************************************//
+bool AT24C64D::read(uint16_t uiAddr, char *cData){
+
+ // bAck = ACKpolling(AT24C64D_R); // polling for ACK
+
+ //if(bAck == ACK){
+
+ uiAddrRead = uiAddr & ADDR_MASK;
+ cBuffer[0] = (uiAddrRead >> 8) & 0xFF;
+ cBuffer[1] = uiAddrRead & 0xFF;
+
+ i2c->start();
+ i2c->write(AT24C64D_W);
+ i2c->write(cBuffer[0]);
+ i2c->write(cBuffer[1]);
+ //i2c->read(AT24C64D_R, cData, 10);
+
+ i2c->start();
+ i2c->write(AT24C64D_R);
+ *cData = i2c->read(false);
+ i2c->stop();
+ //}
+ return bAck;
+}
@@ -182,59 +245,6 @@
// end incremented. When the memory address limit is reached, the data word //
// address will “roll over” and the sequential read will continue. //
//******************************************************************************//
-//bool AT24C64D::read(uint16_t uiAddr, char * cData, uint16_t uiLength){}
-
-
-
-//******************************************************************************//
-//
-//******************************************************************************//
-bool AT24C64D::readByte(char *cData){
-
- bAck = ACKpolling(AT24C64D_R); // polling for ACK
-
- if(bAck == ACK){
- i2c->start(); // send a start condition
- i2c->write(AT24C64D_R);
- *cData = i2c->read(false);
- i2c->stop();
- }
- return bAck;
-}
-
-
-//******************************************************************************//
-//
-//******************************************************************************//
-bool AT24C64D::readByte(uint16_t uiAddr, char *cData){
-
- bAck = ACKpolling(AT24C64D_R); // polling for ACK
-
- if(bAck == ACK){
-
- uiAddrRead = uiAddr & ADDR_MASK;
-
- cBuffer[0] = (uiAddrRead >> 8) & 0xFF;
- cBuffer[1] = uiAddrRead & 0xFF;
- printf("0x%02x 0x%02x\n", cBuffer[0], cBuffer[1]);
- i2c->start();
- i2c->write(AT24C64D_W);
- i2c->write(cBuffer[0]);
- i2c->write(cBuffer[1]);
- i2c->start();
- i2c->write(AT24C64D_R);
- *cData = i2c->read(false);
- i2c->stop();
- }
-
- return bAck;
-}
-
-
-
-//******************************************************************************//
-//
-//******************************************************************************//
bool AT24C64D::read(uint16_t uiAddr, char *cData, uint16_t uiLength){
if(uiLength == 0){
@@ -242,7 +252,7 @@
return NACK;
}
- ACKpolling(AT24C64D_R);
+ //ACKpolling(AT24C64D_R);
@@ -274,7 +284,9 @@
//
/******************************************************************************/
bool AT24C64D::isReady(){
- if(bReady or timer->read_ms() >= READY_TIME_MS){
+
+ if(timer->read_ms() > READY_TIME_MS){
+ //printf("Time: %d\n", timer->read_ms());
bReady = true;
if(bTimerRun){
@@ -282,7 +294,7 @@
bTimerRun = false;
}
}
-
+
return bReady;
}
@@ -319,6 +331,8 @@
printf("Read from EPPROM: timeout\n");
return NACK;
}
+
+ wait(100e-6);
}
return bAck;