A library to read and write all 25* serial SPI eeprom devices from Microchip (from 25xx010 to 25xx1024).

Dependents:   RobotRic VITI2_ihm_2

Ser25lcxxx.h

Committer:
hlipka
Date:
2011-01-26
Revision:
0:238ca4fdef8c
Child:
2:3a3404dbd3eb

File content as of revision 0:238ca4fdef8c:

#ifndef __SER25LCXXX_H__
#define __SER25LCXXX_H__

#include "mbed.h"

/**
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)
            @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
        */
        Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize);
        
        /**
            destroys the handler, and frees the /CS pin        
        */
        ~Ser25LCxxx();
        
        /**
            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
        */
        char* read(unsigned int startAdr, unsigned int len);
        
        /**
            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
        */
        bool write(unsigned int startAdr, unsigned int len, const char* 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);
        
        /**
            fills the while eeprom with 0xFF
        */
        void clearMem();
    private:
        bool writePage(unsigned int startAdr, unsigned int len, const char* data);
        int readStatus();
        void waitForWrite();
        void enableWrite();
        

        SPI* _spi;
        DigitalOut* _enable;
        int _size,_pageSize;
        
};



#endif