Enhanced EEPROM 25LCxxx driver
Fork of 25LCxxx_SPI by
Ser25lcxxx.h@3:be69a9f6f738, 2015-07-17 (annotated)
- Committer:
- mariob
- Date:
- Fri Jul 17 09:47:32 2015 +0000
- Revision:
- 3:be69a9f6f738
- Parent:
- 2:3a3404dbd3eb
added fix in writing function; fixed write check; added features (protection bits); changed interfaces
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 | |
mariob | 3:be69a9f6f738 | 29 | |
mariob | 3:be69a9f6f738 | 30 | #define EEPROM_CMD_READ 0x03 |
mariob | 3:be69a9f6f738 | 31 | #define EEPROM_CMD_WRITE 0x02 |
mariob | 3:be69a9f6f738 | 32 | #define EEPROM_CMD_WRDI 0x04 |
mariob | 3:be69a9f6f738 | 33 | #define EEPROM_CMD_WREN 0x06 |
mariob | 3:be69a9f6f738 | 34 | #define EEPROM_CMD_RDSR 0x05 |
mariob | 3:be69a9f6f738 | 35 | #define EEPROM_CMD_WRSR 0x01 |
mariob | 3:be69a9f6f738 | 36 | |
mariob | 3:be69a9f6f738 | 37 | |
mariob | 3:be69a9f6f738 | 38 | #define EEPROM_SPI_SPEED 1000000 |
hlipka | 0:238ca4fdef8c | 39 | |
mariob | 3:be69a9f6f738 | 40 | #define EEPROM_CLEAN_BYTE 0xff |
mariob | 3:be69a9f6f738 | 41 | |
mariob | 3:be69a9f6f738 | 42 | |
mariob | 3:be69a9f6f738 | 43 | /** |
mariob | 3:be69a9f6f738 | 44 | A class to read and write all 25* serial SPI eeprom devices from Microchip |
mariob | 3:be69a9f6f738 | 45 | (from 25xx010 to 25xx1024). |
mariob | 3:be69a9f6f738 | 46 | |
mariob | 3:be69a9f6f738 | 47 | One needs to provide total size and page size, since this cannot be read from |
mariob | 3:be69a9f6f738 | 48 | the devices, and the page size differs even by constant size (look up the data |
mariob | 3:be69a9f6f738 | 49 | sheet for your part!) |
hlipka | 0:238ca4fdef8c | 50 | */ |
hlipka | 0:238ca4fdef8c | 51 | class Ser25LCxxx |
hlipka | 0:238ca4fdef8c | 52 | { |
hlipka | 0:238ca4fdef8c | 53 | public: |
mariob | 3:be69a9f6f738 | 54 | |
hlipka | 0:238ca4fdef8c | 55 | /** |
mariob | 3:be69a9f6f738 | 56 | Create the handler class |
mariob | 3:be69a9f6f738 | 57 | |
mariob | 3:be69a9f6f738 | 58 | @param sck clock pin of the spi where the eeprom is connected |
mariob | 3:be69a9f6f738 | 59 | @param si input pin of the spi where the eeprom is connected |
mariob | 3:be69a9f6f738 | 60 | @param so output pin of the spi where the eeprom is connected |
hlipka | 0:238ca4fdef8c | 61 | @param enable the pin name for the port where /CS is connected |
mariob | 3:be69a9f6f738 | 62 | @param pagenumber number of pages |
mariob | 3:be69a9f6f738 | 63 | @param pagesize the size of each page |
hlipka | 0:238ca4fdef8c | 64 | */ |
mariob | 3:be69a9f6f738 | 65 | Ser25LCxxx(PinName sck, PinName si, PinName so, PinName enable, |
mariob | 3:be69a9f6f738 | 66 | int pagenumber, int pagesize); |
mariob | 3:be69a9f6f738 | 67 | |
mariob | 3:be69a9f6f738 | 68 | /** |
mariob | 3:be69a9f6f738 | 69 | Destroy the handler |
mariob | 3:be69a9f6f738 | 70 | */ |
mariob | 3:be69a9f6f738 | 71 | ~Ser25LCxxx() {}; |
hlipka | 0:238ca4fdef8c | 72 | |
hlipka | 0:238ca4fdef8c | 73 | /** |
mariob | 3:be69a9f6f738 | 74 | Read from the eeprom memory |
mariob | 3:be69a9f6f738 | 75 | |
mariob | 3:be69a9f6f738 | 76 | @param startAdr the adress where to start reading |
mariob | 3:be69a9f6f738 | 77 | @param len the number of bytes to read |
mariob | 3:be69a9f6f738 | 78 | @param buf destination buffer |
mariob | 3:be69a9f6f738 | 79 | @return number of read bytes |
hlipka | 0:238ca4fdef8c | 80 | */ |
mariob | 3:be69a9f6f738 | 81 | unsigned int read(unsigned int startAdr, unsigned int len, |
mariob | 3:be69a9f6f738 | 82 | unsigned char* buf); |
hlipka | 0:238ca4fdef8c | 83 | |
hlipka | 0:238ca4fdef8c | 84 | /** |
mariob | 3:be69a9f6f738 | 85 | Writes the give buffer into the eeprom memory |
mariob | 3:be69a9f6f738 | 86 | |
mariob | 3:be69a9f6f738 | 87 | @param startAdr the eeprom adress where to start writing (it |
mariob | 3:be69a9f6f738 | 88 | doesn't have to match a page boundary) |
mariob | 3:be69a9f6f738 | 89 | @param len number of bytes to write |
mariob | 3:be69a9f6f738 | 90 | @param data data to write |
mariob | 3:be69a9f6f738 | 91 | @return the number of written bytes |
hlipka | 0:238ca4fdef8c | 92 | */ |
mariob | 3:be69a9f6f738 | 93 | unsigned int write(unsigned int startAdr, unsigned int len, |
mariob | 3:be69a9f6f738 | 94 | const unsigned char* data); |
mariob | 3:be69a9f6f738 | 95 | |
mariob | 3:be69a9f6f738 | 96 | /** |
mariob | 3:be69a9f6f738 | 97 | Fills the given page with EEPROM_CLEAN_BYTE |
mariob | 3:be69a9f6f738 | 98 | |
mariob | 3:be69a9f6f738 | 99 | @param pageNum the page to clear |
mariob | 3:be69a9f6f738 | 100 | @return the number of written bytes (it should be the page size) |
mariob | 3:be69a9f6f738 | 101 | */ |
mariob | 3:be69a9f6f738 | 102 | unsigned int clearPage(unsigned int pageNum); |
hlipka | 0:238ca4fdef8c | 103 | |
hlipka | 0:238ca4fdef8c | 104 | /** |
mariob | 3:be69a9f6f738 | 105 | Fills the whole eeprom with EEPROM_CLEAN_BYTE |
mariob | 3:be69a9f6f738 | 106 | |
mariob | 3:be69a9f6f738 | 107 | @return the number of written bytes (it should be the eeprom size) |
mariob | 3:be69a9f6f738 | 108 | */ |
mariob | 3:be69a9f6f738 | 109 | unsigned int clearMem(); |
mariob | 3:be69a9f6f738 | 110 | |
mariob | 3:be69a9f6f738 | 111 | /** |
mariob | 3:be69a9f6f738 | 112 | Return TRUE if a write is performing |
mariob | 3:be69a9f6f738 | 113 | */ |
mariob | 3:be69a9f6f738 | 114 | bool isWriteInProgress() { |
mariob | 3:be69a9f6f738 | 115 | return (readStatus()&0x01)!=0x00; |
mariob | 3:be69a9f6f738 | 116 | } |
mariob | 3:be69a9f6f738 | 117 | |
mariob | 3:be69a9f6f738 | 118 | /** |
mariob | 3:be69a9f6f738 | 119 | Return TRUE if the entire eeprom memory is writable |
mariob | 3:be69a9f6f738 | 120 | */ |
mariob | 3:be69a9f6f738 | 121 | bool isFullyWritable() { |
mariob | 3:be69a9f6f738 | 122 | return (readStatus()&0x0C)==0x00; |
mariob | 3:be69a9f6f738 | 123 | } |
mariob | 3:be69a9f6f738 | 124 | |
mariob | 3:be69a9f6f738 | 125 | /** |
mariob | 3:be69a9f6f738 | 126 | Return TRUE if only the first half of the eeprom memory is writable |
hlipka | 0:238ca4fdef8c | 127 | */ |
mariob | 3:be69a9f6f738 | 128 | bool isHalfWritable() { |
mariob | 3:be69a9f6f738 | 129 | return (readStatus()&0x0C)==0x08; |
mariob | 3:be69a9f6f738 | 130 | } |
mariob | 3:be69a9f6f738 | 131 | |
mariob | 3:be69a9f6f738 | 132 | /** |
mariob | 3:be69a9f6f738 | 133 | Return TRUE if the eeprom memory is not writable |
mariob | 3:be69a9f6f738 | 134 | */ |
mariob | 3:be69a9f6f738 | 135 | bool isNotWritable() { |
mariob | 3:be69a9f6f738 | 136 | return (readStatus()&0x0C)==0x0C; |
mariob | 3:be69a9f6f738 | 137 | } |
mariob | 3:be69a9f6f738 | 138 | |
hlipka | 0:238ca4fdef8c | 139 | /** |
mariob | 3:be69a9f6f738 | 140 | Set the eeprom memory not writable |
hlipka | 0:238ca4fdef8c | 141 | */ |
mariob | 3:be69a9f6f738 | 142 | void setNotWritable () { |
mariob | 3:be69a9f6f738 | 143 | writeStatus(0x0C); |
mariob | 3:be69a9f6f738 | 144 | } |
mariob | 3:be69a9f6f738 | 145 | |
mariob | 3:be69a9f6f738 | 146 | /** |
mariob | 3:be69a9f6f738 | 147 | Set the eeprom memory fully writable |
mariob | 3:be69a9f6f738 | 148 | */ |
mariob | 3:be69a9f6f738 | 149 | void setFullyWritable () { |
mariob | 3:be69a9f6f738 | 150 | writeStatus(0x00); |
mariob | 3:be69a9f6f738 | 151 | } |
hlipka | 0:238ca4fdef8c | 152 | |
hlipka | 0:238ca4fdef8c | 153 | /** |
mariob | 3:be69a9f6f738 | 154 | Set only the first half of the eeprom memory as writable |
hlipka | 0:238ca4fdef8c | 155 | */ |
mariob | 3:be69a9f6f738 | 156 | void setHalfWritable() { |
mariob | 3:be69a9f6f738 | 157 | writeStatus(0x08); |
mariob | 3:be69a9f6f738 | 158 | } |
mariob | 3:be69a9f6f738 | 159 | |
hlipka | 0:238ca4fdef8c | 160 | private: |
mariob | 3:be69a9f6f738 | 161 | |
mariob | 3:be69a9f6f738 | 162 | /** |
mariob | 3:be69a9f6f738 | 163 | Write data within an eeprom page |
mariob | 3:be69a9f6f738 | 164 | |
mariob | 3:be69a9f6f738 | 165 | @param startAdr destination address of the eeprm memory |
mariob | 3:be69a9f6f738 | 166 | @param len number of bytes to write |
mariob | 3:be69a9f6f738 | 167 | @param data data to be written in the eeprom memory |
mariob | 3:be69a9f6f738 | 168 | @return number of written bytes |
mariob | 3:be69a9f6f738 | 169 | */ |
mariob | 3:be69a9f6f738 | 170 | unsigned int writePage(unsigned int startAdr, unsigned int len, |
mariob | 3:be69a9f6f738 | 171 | const unsigned char* data); |
mariob | 3:be69a9f6f738 | 172 | |
mariob | 3:be69a9f6f738 | 173 | /** |
mariob | 3:be69a9f6f738 | 174 | Read the status register of the eeprom memory |
mariob | 3:be69a9f6f738 | 175 | |
mariob | 3:be69a9f6f738 | 176 | @return status register |
mariob | 3:be69a9f6f738 | 177 | */ |
hlipka | 0:238ca4fdef8c | 178 | int readStatus(); |
hlipka | 0:238ca4fdef8c | 179 | |
mariob | 3:be69a9f6f738 | 180 | /** |
mariob | 3:be69a9f6f738 | 181 | Write the status register |
mariob | 3:be69a9f6f738 | 182 | |
mariob | 3:be69a9f6f738 | 183 | @param val status register |
mariob | 3:be69a9f6f738 | 184 | */ |
mariob | 3:be69a9f6f738 | 185 | void writeStatus(unsigned char val); |
hlipka | 0:238ca4fdef8c | 186 | |
mariob | 3:be69a9f6f738 | 187 | /** |
mariob | 3:be69a9f6f738 | 188 | Wait until a write procedure ends or an interval of 5ms is expired |
mariob | 3:be69a9f6f738 | 189 | |
mariob | 3:be69a9f6f738 | 190 | @return TRUE if the write procedure has successfully terminated, |
mariob | 3:be69a9f6f738 | 191 | FALSE if the interval is expired |
mariob | 3:be69a9f6f738 | 192 | */ |
mariob | 3:be69a9f6f738 | 193 | bool waitForWrite(); |
hlipka | 0:238ca4fdef8c | 194 | |
mariob | 3:be69a9f6f738 | 195 | /** |
mariob | 3:be69a9f6f738 | 196 | Enable the write procedure |
mariob | 3:be69a9f6f738 | 197 | */ |
mariob | 3:be69a9f6f738 | 198 | void enableWrite(); |
mariob | 3:be69a9f6f738 | 199 | |
mariob | 3:be69a9f6f738 | 200 | SPI spi; |
mariob | 3:be69a9f6f738 | 201 | DigitalOut cs; |
mariob | 3:be69a9f6f738 | 202 | int size; |
mariob | 3:be69a9f6f738 | 203 | int pageSize; |
mariob | 3:be69a9f6f738 | 204 | int pageNumber; |
hlipka | 0:238ca4fdef8c | 205 | }; |
hlipka | 0:238ca4fdef8c | 206 | |
hlipka | 0:238ca4fdef8c | 207 | #endif |