David Möschwitzer / AT24C64D
Revision:
0:1fd82709703d
Child:
1:3ccf1b297b4f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AT24C64D.cpp	Mon Feb 18 15:51:46 2019 +0000
@@ -0,0 +1,305 @@
+#include "AT24C64D.h"
+#include "math.h"
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+AT24C64D::AT24C64D(I2C *_i2c, uint8_t uiAddr)                            //
+    :   AT24C64D_W( HARD_ADDR| (uiAddr & USER_ADDR_MASK) << 1),               // Initialisation of const WRITE
+        AT24C64D_R( AT24C64D_W | 0x01 ){
+        
+        
+        printf("Contructor of EEPROM AT24C64D...");
+        bAck = NACK;
+        //timer = new Ticker;
+        i2c = _i2c;
+        
+        for(int a; a <= SIZE_PAGE; a++)
+            cBuffer[a] = 0x00;
+        
+        //bReady = false; 
+        //timer->attach_us(callback(this, &AT24C64D::setReady), READY_TIME_US);
+        printf("end\n");
+}
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::setAddrWrite(uint16_t Addr){    
+    if(uiAddrWrite > MEM_ADDR_MAX)  uiAddrWrite = 0;
+    else                            uiAddrWrite = Addr;
+
+}
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::incAddrWrite(){
+    if(uiAddrWrite >= MEM_ADDR_MAX) uiAddrWrite = 0;
+    else                            uiAddrWrite++;
+}
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::decAddrWrite(){
+    if(uiAddrWrite <= 0)            uiAddrWrite = 4095;
+    else                            uiAddrRead--;
+}
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+uint16_t AT24C64D::getAddrWrite(){
+    return uiAddrWrite;
+}
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::setAddrRead(uint16_t Addr){    
+    if(uiAddrRead > MEM_ADDR_MAX)  uiAddrRead = 0;
+    else                            uiAddrRead = Addr;
+
+}
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::incAddrRead(){
+    if(uiAddrRead >= MEM_ADDR_MAX) uiAddrRead = 0;
+    else                            uiAddrRead++;
+}
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::decAddrRead(){
+    if(uiAddrRead <= 0)            uiAddrRead = 4095;
+    else                            uiAddrRead--;
+}
+
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+uint16_t AT24C64D::getAddrRead(){
+    return uiAddrRead;
+}
+
+
+//******************************************************************************//
+// uiAddr:   user address [A2...A0]
+//******************************************************************************//
+void AT24C64D::reset(){
+
+    i2c->start();
+    i2c->write(0x00);
+    i2c->start();
+    i2c->stop();
+    return;
+}
+
+
+
+/******************************************************************************/
+//
+/******************************************************************************/
+bool  AT24C64D::erase(){
+    return ACK;
+}
+
+
+
+//******************************************************************************//
+//  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.         //       
+//******************************************************************************//
+bool AT24C64D::writeByte(uint16_t uiAddr, char * cData){
+    
+    //printf("Write a Byte (0x2%x) to EEPROM  on address 0x%4x\n", *cData, uiAddr);
+    
+    bAck = true;
+    uiAddrWrite = uiAddr & ADDR_MASK;    
+    //while(not bReady);
+        
+            
+    cBuffer[0] = (uiAddrWrite & 0x0F00) >> 8;
+    cBuffer[1] = (uiAddrWrite & 0x00FF);
+    cBuffer[3] = (*cData);    
+    
+    bAck &= not i2c->write(AT24C64D_W, cBuffer, 3);
+    while(i2c->read(AT24C64D_R, NULL, 1));//   wait_us(1000);
+    //bReady = false; 
+    //timer->attach_us(callback(this, &AT24C64D::setReady), READY_TIME_US);
+    
+    if(bAck) incAddrWrite();
+    
+    return bAck;
+}
+
+
+//******************************************************************************//
+//  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.         //       
+//******************************************************************************//
+bool AT24C64D::write(uint16_t uiAddr, char * cData, uint16_t uiLength){
+ 
+    
+    bAck = true;
+    uiAddrWrite = uiAddr & ADDR_MASK;    
+     
+    uint16_t uiDataPos      = 0;
+    uint16_t uiStartPageAddr= (uint16_t) ceil((float)uiAddr/SIZE_PAGE);  
+    uint16_t uiEndPageAddr  = (uint16_t) (uiAddr + uiLength)/SIZE_PAGE;  
+    uint16_t uiNumPage      = (uint16_t) uiEndPageAddr - uiStartPageAddr;
+    
+    uint8_t uiLengthToStart = (uiStartPageAddr * SIZE_PAGE) - uiAddr;
+    uint8_t uiLengthAtEnd   = uiAddr + uiLength - (uiEndPageAddr * SIZE_PAGE);
+    
+
+
+    if(uiLengthToStart > 0){
+        
+        cBuffer[0] = (uiAddrWrite & 0x0F00) >> 8;
+        cBuffer[1] = (uiAddrWrite & 0x00FF);
+        
+        for(int i = 0; i < uiLengthToStart; i++){
+            cBuffer[2 + i] = cData[uiDataPos];
+            uiDataPos++;
+        }
+    
+        //while(not bReady);
+        
+        
+        bAck &= not i2c->write(AT24C64D_W, cBuffer, uiLengthToStart + 2); 
+        //printf("ACK Start: %d\n", bAck);     
+        if(bAck == NACK) return bAck;
+        
+        while(i2c->read(AT24C64D_R, NULL, 1))   wait_us(1);
+        //while();
+        //wait_us(READY_TIME_US); 
+        //bReady = false; 
+        //timer->attach_us(callback(this, &AT24C64D::setReady), READY_TIME_US);        
+    }
+    
+    
+    
+    if(uiNumPage > 0){
+        
+        for(int a = 0; a < uiNumPage; a++){
+            
+            cBuffer[0] = ( ( (uiStartPageAddr + a) * SIZE_PAGE) & 0x0F00) >> 8;
+            cBuffer[1] = ( ( (uiStartPageAddr + a) * SIZE_PAGE) & 0x00FF);
+            
+            for(int b = 0; b < SIZE_PAGE; b++){
+                cBuffer[2 + b + a * SIZE_PAGE] = cData[uiDataPos];
+                uiDataPos++;
+            }
+            //printf("1 ACK Page: %d\n", bAck); 
+            
+            //while(not bReady);
+            bAck &= not i2c->write(AT24C64D_W, cBuffer, SIZE_PAGE + 2); 
+            
+//            printf(" 2 ACK Page: %d\n", bAck);
+            if(bAck == NACK) return bAck;
+            while(i2c->read(AT24C64D_R, NULL, 1))   wait_us(1000);
+            //for(int i = 480e3; i>0; i--);
+            //wait_us(READY_TIME_US); 
+            //bReady = false; 
+            //timer->attach_us(callback(this, &AT24C64D::setReady), READY_TIME_US);   
+           
+        }
+    }
+    
+    
+    if(uiLengthAtEnd > 0){
+        
+        cBuffer[0] = ( (uiEndPageAddr * SIZE_PAGE) & 0x0F00) >> 8;
+        cBuffer[1] = ( (uiEndPageAddr * SIZE_PAGE) & 0x00FF);
+        
+        for(int a = 0; a < uiLengthAtEnd; a++){
+            cBuffer[2 + a] = cData[uiDataPos];
+            uiDataPos++;
+        }
+        
+
+        bAck &= not i2c->write(AT24C64D_W, cBuffer, uiLengthAtEnd + 2);        
+        
+        printf("ACK End: %d\n", bAck);
+        if(bAck == NACK) return bAck;
+        
+         while(i2c->read(AT24C64D_R, NULL, 1))   wait_us(1);
+        //for(int i = 480e3; i>0; i--);
+        //wait_us(READY_TIME_US);
+        //bReady = false; 
+        //timer->attach_us(callback(this, &AT24C64D::setReady), READY_TIME_US);
+            
+
+    }
+     
+    
+    return bAck;
+}
+
+
+
+//******************************************************************************//
+//  Reads data with length uiLength from Memory on address uiAddr. If all       //
+//  transmitions are successful, it will be returned a ACK. Otherwise a NACK.   //
+//  After every successful transmition, the readAddr will new set and at the    //
+//  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){
+    //printf("Read from EEPROM...\n");
+    
+    bAck = true;
+    uiAddrRead = uiAddr & ADDR_MASK;
+    
+    bAck &= not i2c->read(AT24C64D_R, cData, uiLength);
+    
+    if(bAck) uiAddrRead += uiLength;
+    
+    if(uiAddrRead > MEM_ADDR_MAX) uiAddrRead -= MEM_ADDR_MAX;
+    
+    
+    return bAck;
+}
+
+
+/******************************************************************************/
+//
+/******************************************************************************/
+void AT24C64D::setReady(){
+    bReady = true;
+    //timer->detach();
+}
+
+
+
+/******************************************************************************/
+//
+/******************************************************************************/
+bool AT24C64D::isReady(){
+    return bReady;
+}
+
+