Library to use 25LCxxx chips

Dependents:   loststone

Fork of 25LCxxx_SPI by Hendrik Lipka

Files at this revision

API Documentation at this revision

Comitter:
xxann5
Date:
Thu Mar 14 13:19:40 2013 +0000
Parent:
3:d9429070ea6f
Commit message:
Changed int to the appropriate u?int(8|16|32)_t types. I like being explicit.

Changed in this revision

Ser25lcxxx.cpp Show annotated file Show diff for this revision Revisions of this file
Ser25lcxxx.h Show annotated file Show diff for this revision Revisions of this file
diff -r d9429070ea6f -r 0c31e878a076 Ser25lcxxx.cpp
--- a/Ser25lcxxx.cpp	Mon Jan 14 04:26:13 2013 +0000
+++ b/Ser25lcxxx.cpp	Thu Mar 14 13:19:40 2013 +0000
@@ -27,7 +27,7 @@
 #define HIGH(x) ((x&0xff00)>>8)
 #define LOW(x) (x&0xff)
 
-Ser25LCxxx::Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize) {
+Ser25LCxxx::Ser25LCxxx(SPI *spi, PinName enable, uint32_t bytes, uint32_t pagesize) {
     _spi=spi;
     _enable=new DigitalOut(enable);
     _size=bytes;
@@ -39,11 +39,11 @@
     delete _enable;
 }
 
-int* Ser25LCxxx::read(unsigned int startAdr, unsigned int len) {
+uint8_t* Ser25LCxxx::read( uint32_t startAdr,  uint32_t len) {
     // assertion
     if (startAdr+len>_size)
         return NULL;
-    int* ret=(int*)malloc(len);
+    uint8_t* ret=(uint8_t*)malloc(len);
     _enable->write(0);
     wait_us(1);
     // send address
@@ -64,7 +64,7 @@
         _spi->write(LOW(startAdr));
     }
     // read data into buffer
-    for (int i=0;i<len;i++) {
+    for (uint8_t i=0;i<len;i++) {
         ret[i]=_spi->write(0);
     }
     wait_us(1);
@@ -72,14 +72,14 @@
     return ret;
 }
 
-bool Ser25LCxxx::write(unsigned int startAdr, unsigned int len, int* data) {
+bool Ser25LCxxx::write( uint32_t startAdr,  uint32_t len, const uint8_t* data) {
     if (startAdr+len>_size)
         return -1;
 
-    int ofs=0;
+    uint8_t ofs=0;
     while (ofs<len) {
         // calculate amount of data to write into current page
-        int pageLen=_pageSize-((startAdr+ofs)%_pageSize);
+        uint8_t pageLen=_pageSize-((startAdr+ofs)%_pageSize);
         if (ofs+pageLen>len)
             pageLen=len-ofs;
         // write single page
@@ -92,7 +92,7 @@
     return true;
 }
 
-bool Ser25LCxxx::writePage(unsigned int startAdr, unsigned int len, int* data) {
+bool Ser25LCxxx::writePage( uint32_t startAdr,  uint32_t len, const uint8_t* data) {
     enableWrite();
 
     _enable->write(0);
@@ -116,7 +116,7 @@
     }
 
     // do real write
-    for (int i=0;i<len;i++) {
+    for (uint8_t i=0;i<len;i++) {
         _spi->write(data[i]);
     }
     wait_us(1);
@@ -128,11 +128,11 @@
     return true;
 }
 
-bool Ser25LCxxx::clearPage(unsigned int pageNum) {
+bool Ser25LCxxx::clearPage( uint32_t pageNum) {
     enableWrite();
     if (_size<65535) {
-        int* s=(int*)malloc(_pageSize);
-        for (int i=0;i<_pageSize;i++) {
+        uint8_t* s=(uint8_t*)malloc(_pageSize);
+        for (uint8_t i=0;i<_pageSize;i++) {
             s[i]=0xff;
         }
         bool b=writePage(_pageSize*pageNum,_pageSize,s);
@@ -155,7 +155,7 @@
 void Ser25LCxxx::clearMem() {
     enableWrite();
     if (_size<65535) {
-        for (int i=0;i<_size/_pageSize;i++) {
+        for (uint8_t i=0;i<_size/_pageSize;i++) {
             if (!clearPage(i))
                 break;
         }
@@ -172,11 +172,11 @@
     }
 }
 
-int Ser25LCxxx::readStatus() {
+uint8_t Ser25LCxxx::readStatus() {
     _enable->write(0);
     wait_us(1);
     _spi->write(0x5);
-    int status=_spi->write(0x00);
+    uint8_t status=_spi->write(0x00);
     wait_us(1);
     _enable->write(1);
     return status;
diff -r d9429070ea6f -r 0c31e878a076 Ser25lcxxx.h
--- a/Ser25lcxxx.h	Mon Jan 14 04:26:13 2013 +0000
+++ b/Ser25lcxxx.h	Thu Mar 14 13:19:40 2013 +0000
@@ -42,7 +42,7 @@
             @param bytes the size of you eeprom in bytes (NOT bits, eg. a 25LC010 has 128 bytes)
             @param pagesize the size of a single page, to provide overruns
         */
-        Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize);
+        Ser25LCxxx(SPI *spi, PinName enable, uint32_t bytes, uint32_t pagesize);
         
         /**
             destroys the handler, and frees the /CS pin        
@@ -55,7 +55,7 @@
             @param len the number of bytes to read (must not exceed the end of memory)
             @return NULL if the adresses are out of range, the pointer to the data otherwise
         */
-        int* read(unsigned int startAdr, unsigned int len);
+        uint8_t* read( uint32_t startAdr,  uint32_t len);
         
         /**
             writes the give buffer into the memory. This function handles dividing the write into 
@@ -64,29 +64,29 @@
             @param len the number of bytes to read (must not exceed the end of memory)
             @return false if the adresses are out of range
         */
-        bool write(unsigned int startAdr, unsigned int len, int* data);
+        bool write( uint32_t startAdr,  uint32_t len, const uint8_t* data);
         
         /**
             fills the given page with 0xFF
             @param pageNum the page number to clear
             @return if the pageNum is out of range        
         */
-        bool clearPage(unsigned int pageNum);
+        bool clearPage( uint32_t pageNum);
         
         /**
             fills the while eeprom with 0xFF
         */
         void clearMem();
     private:
-        bool writePage(unsigned int startAdr, unsigned int len, int* data);
-        int readStatus();
+        bool writePage( uint32_t startAdr,  uint32_t len, const uint8_t* data);
+        uint8_t readStatus();
         void waitForWrite();
         void enableWrite();
         
 
         SPI* _spi;
         DigitalOut* _enable;
-        int _size,_pageSize;
+        uint32_t _size,_pageSize;
         
 };