Library to use 25LCxxx chips
Fork of 25LCxxx_SPI by
Ser25lcxxx.h@2:3a3404dbd3eb, 2011-02-19 (annotated)
- Committer:
- hlipka
- Date:
- Sat Feb 19 18:29:20 2011 +0000
- Revision:
- 2:3a3404dbd3eb
- Parent:
- 0:238ca4fdef8c
- Child:
- 3:d9429070ea6f
added license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 2:3a3404dbd3eb | 1 | /* |
hlipka | 2:3a3404dbd3eb | 2 | * Ser25lcxxx library |
hlipka | 2:3a3404dbd3eb | 3 | * Copyright (c) 2010 Hendrik Lipka |
hlipka | 2:3a3404dbd3eb | 4 | * |
hlipka | 2:3a3404dbd3eb | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hlipka | 2:3a3404dbd3eb | 6 | * of this software and associated documentation files (the "Software"), to deal |
hlipka | 2:3a3404dbd3eb | 7 | * in the Software without restriction, including without limitation the rights |
hlipka | 2:3a3404dbd3eb | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hlipka | 2:3a3404dbd3eb | 9 | * copies of the Software, and to permit persons to whom the Software is |
hlipka | 2:3a3404dbd3eb | 10 | * furnished to do so, subject to the following conditions: |
hlipka | 2:3a3404dbd3eb | 11 | * |
hlipka | 2:3a3404dbd3eb | 12 | * The above copyright notice and this permission notice shall be included in |
hlipka | 2:3a3404dbd3eb | 13 | * all copies or substantial portions of the Software. |
hlipka | 2:3a3404dbd3eb | 14 | * |
hlipka | 2:3a3404dbd3eb | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hlipka | 2:3a3404dbd3eb | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hlipka | 2:3a3404dbd3eb | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hlipka | 2:3a3404dbd3eb | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hlipka | 2:3a3404dbd3eb | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hlipka | 2:3a3404dbd3eb | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hlipka | 2:3a3404dbd3eb | 21 | * THE SOFTWARE. |
hlipka | 2:3a3404dbd3eb | 22 | */ |
hlipka | 2:3a3404dbd3eb | 23 | |
hlipka | 0:238ca4fdef8c | 24 | #ifndef __SER25LCXXX_H__ |
hlipka | 0:238ca4fdef8c | 25 | #define __SER25LCXXX_H__ |
hlipka | 0:238ca4fdef8c | 26 | |
hlipka | 0:238ca4fdef8c | 27 | #include "mbed.h" |
hlipka | 0:238ca4fdef8c | 28 | |
hlipka | 0:238ca4fdef8c | 29 | /** |
hlipka | 0:238ca4fdef8c | 30 | A class to read and write all 25* serial SPI eeprom devices from Microchip (from 25xx010 to 25xx1024). |
hlipka | 0:238ca4fdef8c | 31 | |
hlipka | 0:238ca4fdef8c | 32 | One needs to provide total size and page size, since this cannot be read from the devices, |
hlipka | 0:238ca4fdef8c | 33 | and the page size differs even by constant size (look up the data sheet for your part!) |
hlipka | 0:238ca4fdef8c | 34 | */ |
hlipka | 0:238ca4fdef8c | 35 | class Ser25LCxxx |
hlipka | 0:238ca4fdef8c | 36 | { |
hlipka | 0:238ca4fdef8c | 37 | public: |
hlipka | 0:238ca4fdef8c | 38 | /** |
hlipka | 0:238ca4fdef8c | 39 | create the handler class |
hlipka | 0:238ca4fdef8c | 40 | @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 | 41 | @param enable the pin name for the port where /CS is connected |
hlipka | 0:238ca4fdef8c | 42 | @param bytes the size of you eeprom in bytes (NOT bits, eg. a 25LC010 has 128 bytes) |
hlipka | 0:238ca4fdef8c | 43 | @param pagesize the size of a single page, to provide overruns |
hlipka | 0:238ca4fdef8c | 44 | */ |
hlipka | 0:238ca4fdef8c | 45 | Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize); |
hlipka | 0:238ca4fdef8c | 46 | |
hlipka | 0:238ca4fdef8c | 47 | /** |
hlipka | 0:238ca4fdef8c | 48 | destroys the handler, and frees the /CS pin |
hlipka | 0:238ca4fdef8c | 49 | */ |
hlipka | 0:238ca4fdef8c | 50 | ~Ser25LCxxx(); |
hlipka | 0:238ca4fdef8c | 51 | |
hlipka | 0:238ca4fdef8c | 52 | /** |
hlipka | 0:238ca4fdef8c | 53 | read a part of the eeproms memory. The buffer will be allocated here, and must be freed by the user |
hlipka | 0:238ca4fdef8c | 54 | @param startAdr the adress where to start reading. Doesn't need to match a page boundary |
hlipka | 0:238ca4fdef8c | 55 | @param len the number of bytes to read (must not exceed the end of memory) |
hlipka | 0:238ca4fdef8c | 56 | @return NULL if the adresses are out of range, the pointer to the data otherwise |
hlipka | 0:238ca4fdef8c | 57 | */ |
hlipka | 0:238ca4fdef8c | 58 | char* read(unsigned int startAdr, unsigned int len); |
hlipka | 0:238ca4fdef8c | 59 | |
hlipka | 0:238ca4fdef8c | 60 | /** |
hlipka | 0:238ca4fdef8c | 61 | writes the give buffer into the memory. This function handles dividing the write into |
hlipka | 0:238ca4fdef8c | 62 | pages, and waites until the phyiscal write has finished |
hlipka | 0:238ca4fdef8c | 63 | @param startAdr the adress where to start writing. Doesn't need to match a page boundary |
hlipka | 0:238ca4fdef8c | 64 | @param len the number of bytes to read (must not exceed the end of memory) |
hlipka | 0:238ca4fdef8c | 65 | @return false if the adresses are out of range |
hlipka | 0:238ca4fdef8c | 66 | */ |
hlipka | 0:238ca4fdef8c | 67 | bool write(unsigned int startAdr, unsigned int len, const char* data); |
hlipka | 0:238ca4fdef8c | 68 | |
hlipka | 0:238ca4fdef8c | 69 | /** |
hlipka | 0:238ca4fdef8c | 70 | fills the given page with 0xFF |
hlipka | 0:238ca4fdef8c | 71 | @param pageNum the page number to clear |
hlipka | 0:238ca4fdef8c | 72 | @return if the pageNum is out of range |
hlipka | 0:238ca4fdef8c | 73 | */ |
hlipka | 0:238ca4fdef8c | 74 | bool clearPage(unsigned int pageNum); |
hlipka | 0:238ca4fdef8c | 75 | |
hlipka | 0:238ca4fdef8c | 76 | /** |
hlipka | 0:238ca4fdef8c | 77 | fills the while eeprom with 0xFF |
hlipka | 0:238ca4fdef8c | 78 | */ |
hlipka | 0:238ca4fdef8c | 79 | void clearMem(); |
hlipka | 0:238ca4fdef8c | 80 | private: |
hlipka | 0:238ca4fdef8c | 81 | bool writePage(unsigned int startAdr, unsigned int len, const char* data); |
hlipka | 0:238ca4fdef8c | 82 | int readStatus(); |
hlipka | 0:238ca4fdef8c | 83 | void waitForWrite(); |
hlipka | 0:238ca4fdef8c | 84 | void enableWrite(); |
hlipka | 0:238ca4fdef8c | 85 | |
hlipka | 0:238ca4fdef8c | 86 | |
hlipka | 0:238ca4fdef8c | 87 | SPI* _spi; |
hlipka | 0:238ca4fdef8c | 88 | DigitalOut* _enable; |
hlipka | 0:238ca4fdef8c | 89 | int _size,_pageSize; |
hlipka | 0:238ca4fdef8c | 90 | |
hlipka | 0:238ca4fdef8c | 91 | }; |
hlipka | 0:238ca4fdef8c | 92 | |
hlipka | 0:238ca4fdef8c | 93 | |
hlipka | 0:238ca4fdef8c | 94 | |
hlipka | 0:238ca4fdef8c | 95 | #endif |