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: Ser25lcxxx.h
- Revision:
- 3:be69a9f6f738
- Parent:
- 2:3a3404dbd3eb
--- a/Ser25lcxxx.h Sat Feb 19 18:29:20 2011 +0000
+++ b/Ser25lcxxx.h Fri Jul 17 09:47:32 2015 +0000
@@ -26,70 +26,182 @@
#include "mbed.h"
-/**
-A class to read and write all 25* serial SPI eeprom devices from Microchip (from 25xx010 to 25xx1024).
+
+#define EEPROM_CMD_READ 0x03
+#define EEPROM_CMD_WRITE 0x02
+#define EEPROM_CMD_WRDI 0x04
+#define EEPROM_CMD_WREN 0x06
+#define EEPROM_CMD_RDSR 0x05
+#define EEPROM_CMD_WRSR 0x01
+
+
+#define EEPROM_SPI_SPEED 1000000
-One needs to provide total size and page size, since this cannot be read from the devices,
-and the page size differs even by constant size (look up the data sheet for your part!)
+#define EEPROM_CLEAN_BYTE 0xff
+
+
+/**
+A class to read and write all 25* serial SPI eeprom devices from Microchip
+(from 25xx010 to 25xx1024).
+
+One needs to provide total size and page size, since this cannot be read from
+the devices, and the page size differs even by constant size (look up the data
+sheet for your part!)
*/
class Ser25LCxxx
{
public:
+
/**
- create the handler class
- @param spi the SPI port where the eeprom is connected. Must be set to format(8,3), and with a speed matching the one of your device (up to 5MHz should work)
+ Create the handler class
+
+ @param sck clock pin of the spi where the eeprom is connected
+ @param si input pin of the spi where the eeprom is connected
+ @param so output pin of the spi where the eeprom is connected
@param enable the pin name for the port where /CS is connected
- @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
+ @param pagenumber number of pages
+ @param pagesize the size of each page
*/
- Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize);
+ Ser25LCxxx(PinName sck, PinName si, PinName so, PinName enable,
+ int pagenumber, int pagesize);
+
+ /**
+ Destroy the handler
+ */
+ ~Ser25LCxxx() {};
/**
- destroys the handler, and frees the /CS pin
+ Read from the eeprom memory
+
+ @param startAdr the adress where to start reading
+ @param len the number of bytes to read
+ @param buf destination buffer
+ @return number of read bytes
*/
- ~Ser25LCxxx();
+ unsigned int read(unsigned int startAdr, unsigned int len,
+ unsigned char* buf);
/**
- read a part of the eeproms memory. The buffer will be allocated here, and must be freed by the user
- @param startAdr the adress where to start reading. Doesn't need to match a page boundary
- @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
+ Writes the give buffer into the eeprom memory
+
+ @param startAdr the eeprom adress where to start writing (it
+ doesn't have to match a page boundary)
+ @param len number of bytes to write
+ @param data data to write
+ @return the number of written bytes
*/
- char* read(unsigned int startAdr, unsigned int len);
+ unsigned int write(unsigned int startAdr, unsigned int len,
+ const unsigned char* data);
+
+ /**
+ Fills the given page with EEPROM_CLEAN_BYTE
+
+ @param pageNum the page to clear
+ @return the number of written bytes (it should be the page size)
+ */
+ unsigned int clearPage(unsigned int pageNum);
/**
- writes the give buffer into the memory. This function handles dividing the write into
- pages, and waites until the phyiscal write has finished
- @param startAdr the adress where to start writing. Doesn't need to match a page boundary
- @param len the number of bytes to read (must not exceed the end of memory)
- @return false if the adresses are out of range
+ Fills the whole eeprom with EEPROM_CLEAN_BYTE
+
+ @return the number of written bytes (it should be the eeprom size)
+ */
+ unsigned int clearMem();
+
+ /**
+ Return TRUE if a write is performing
+ */
+ bool isWriteInProgress() {
+ return (readStatus()&0x01)!=0x00;
+ }
+
+ /**
+ Return TRUE if the entire eeprom memory is writable
+ */
+ bool isFullyWritable() {
+ return (readStatus()&0x0C)==0x00;
+ }
+
+ /**
+ Return TRUE if only the first half of the eeprom memory is writable
*/
- bool write(unsigned int startAdr, unsigned int len, const char* data);
-
+ bool isHalfWritable() {
+ return (readStatus()&0x0C)==0x08;
+ }
+
+ /**
+ Return TRUE if the eeprom memory is not writable
+ */
+ bool isNotWritable() {
+ return (readStatus()&0x0C)==0x0C;
+ }
+
/**
- fills the given page with 0xFF
- @param pageNum the page number to clear
- @return if the pageNum is out of range
+ Set the eeprom memory not writable
*/
- bool clearPage(unsigned int pageNum);
+ void setNotWritable () {
+ writeStatus(0x0C);
+ }
+
+ /**
+ Set the eeprom memory fully writable
+ */
+ void setFullyWritable () {
+ writeStatus(0x00);
+ }
/**
- fills the while eeprom with 0xFF
+ Set only the first half of the eeprom memory as writable
*/
- void clearMem();
+ void setHalfWritable() {
+ writeStatus(0x08);
+ }
+
private:
- bool writePage(unsigned int startAdr, unsigned int len, const char* data);
+
+ /**
+ Write data within an eeprom page
+
+ @param startAdr destination address of the eeprm memory
+ @param len number of bytes to write
+ @param data data to be written in the eeprom memory
+ @return number of written bytes
+ */
+ unsigned int writePage(unsigned int startAdr, unsigned int len,
+ const unsigned char* data);
+
+ /**
+ Read the status register of the eeprom memory
+
+ @return status register
+ */
int readStatus();
- void waitForWrite();
- void enableWrite();
+ /**
+ Write the status register
+
+ @param val status register
+ */
+ void writeStatus(unsigned char val);
- SPI* _spi;
- DigitalOut* _enable;
- int _size,_pageSize;
+ /**
+ Wait until a write procedure ends or an interval of 5ms is expired
+
+ @return TRUE if the write procedure has successfully terminated,
+ FALSE if the interval is expired
+ */
+ bool waitForWrite();
+ /**
+ Enable the write procedure
+ */
+ void enableWrite();
+
+ SPI spi;
+ DigitalOut cs;
+ int size;
+ int pageSize;
+ int pageNumber;
};
-
-
#endif