Enhanced EEPROM 25LCxxx driver

Dependents:   eeprom_test

Fork of 25LCxxx_SPI by Hendrik Lipka

Committer:
hlipka
Date:
Wed Jan 26 22:14:41 2011 +0000
Revision:
0:238ca4fdef8c
Child:
2:3a3404dbd3eb
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 0:238ca4fdef8c 1 #ifndef __SER25LCXXX_H__
hlipka 0:238ca4fdef8c 2 #define __SER25LCXXX_H__
hlipka 0:238ca4fdef8c 3
hlipka 0:238ca4fdef8c 4 #include "mbed.h"
hlipka 0:238ca4fdef8c 5
hlipka 0:238ca4fdef8c 6 /**
hlipka 0:238ca4fdef8c 7 A class to read and write all 25* serial SPI eeprom devices from Microchip (from 25xx010 to 25xx1024).
hlipka 0:238ca4fdef8c 8
hlipka 0:238ca4fdef8c 9 One needs to provide total size and page size, since this cannot be read from the devices,
hlipka 0:238ca4fdef8c 10 and the page size differs even by constant size (look up the data sheet for your part!)
hlipka 0:238ca4fdef8c 11 */
hlipka 0:238ca4fdef8c 12 class Ser25LCxxx
hlipka 0:238ca4fdef8c 13 {
hlipka 0:238ca4fdef8c 14 public:
hlipka 0:238ca4fdef8c 15 /**
hlipka 0:238ca4fdef8c 16 create the handler class
hlipka 0:238ca4fdef8c 17 @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)
hlipka 0:238ca4fdef8c 18 @param enable the pin name for the port where /CS is connected
hlipka 0:238ca4fdef8c 19 @param bytes the size of you eeprom in bytes (NOT bits, eg. a 25LC010 has 128 bytes)
hlipka 0:238ca4fdef8c 20 @param pagesize the size of a single page, to provide overruns
hlipka 0:238ca4fdef8c 21 */
hlipka 0:238ca4fdef8c 22 Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize);
hlipka 0:238ca4fdef8c 23
hlipka 0:238ca4fdef8c 24 /**
hlipka 0:238ca4fdef8c 25 destroys the handler, and frees the /CS pin
hlipka 0:238ca4fdef8c 26 */
hlipka 0:238ca4fdef8c 27 ~Ser25LCxxx();
hlipka 0:238ca4fdef8c 28
hlipka 0:238ca4fdef8c 29 /**
hlipka 0:238ca4fdef8c 30 read a part of the eeproms memory. The buffer will be allocated here, and must be freed by the user
hlipka 0:238ca4fdef8c 31 @param startAdr the adress where to start reading. Doesn't need to match a page boundary
hlipka 0:238ca4fdef8c 32 @param len the number of bytes to read (must not exceed the end of memory)
hlipka 0:238ca4fdef8c 33 @return NULL if the adresses are out of range, the pointer to the data otherwise
hlipka 0:238ca4fdef8c 34 */
hlipka 0:238ca4fdef8c 35 char* read(unsigned int startAdr, unsigned int len);
hlipka 0:238ca4fdef8c 36
hlipka 0:238ca4fdef8c 37 /**
hlipka 0:238ca4fdef8c 38 writes the give buffer into the memory. This function handles dividing the write into
hlipka 0:238ca4fdef8c 39 pages, and waites until the phyiscal write has finished
hlipka 0:238ca4fdef8c 40 @param startAdr the adress where to start writing. Doesn't need to match a page boundary
hlipka 0:238ca4fdef8c 41 @param len the number of bytes to read (must not exceed the end of memory)
hlipka 0:238ca4fdef8c 42 @return false if the adresses are out of range
hlipka 0:238ca4fdef8c 43 */
hlipka 0:238ca4fdef8c 44 bool write(unsigned int startAdr, unsigned int len, const char* data);
hlipka 0:238ca4fdef8c 45
hlipka 0:238ca4fdef8c 46 /**
hlipka 0:238ca4fdef8c 47 fills the given page with 0xFF
hlipka 0:238ca4fdef8c 48 @param pageNum the page number to clear
hlipka 0:238ca4fdef8c 49 @return if the pageNum is out of range
hlipka 0:238ca4fdef8c 50 */
hlipka 0:238ca4fdef8c 51 bool clearPage(unsigned int pageNum);
hlipka 0:238ca4fdef8c 52
hlipka 0:238ca4fdef8c 53 /**
hlipka 0:238ca4fdef8c 54 fills the while eeprom with 0xFF
hlipka 0:238ca4fdef8c 55 */
hlipka 0:238ca4fdef8c 56 void clearMem();
hlipka 0:238ca4fdef8c 57 private:
hlipka 0:238ca4fdef8c 58 bool writePage(unsigned int startAdr, unsigned int len, const char* data);
hlipka 0:238ca4fdef8c 59 int readStatus();
hlipka 0:238ca4fdef8c 60 void waitForWrite();
hlipka 0:238ca4fdef8c 61 void enableWrite();
hlipka 0:238ca4fdef8c 62
hlipka 0:238ca4fdef8c 63
hlipka 0:238ca4fdef8c 64 SPI* _spi;
hlipka 0:238ca4fdef8c 65 DigitalOut* _enable;
hlipka 0:238ca4fdef8c 66 int _size,_pageSize;
hlipka 0:238ca4fdef8c 67
hlipka 0:238ca4fdef8c 68 };
hlipka 0:238ca4fdef8c 69
hlipka 0:238ca4fdef8c 70
hlipka 0:238ca4fdef8c 71
hlipka 0:238ca4fdef8c 72 #endif