lib eeprom
Fork of eeprom by
eeprom.cpp@2:c648c5e93d5e, 2015-10-26 (annotated)
- Committer:
- soulx
- Date:
- Mon Oct 26 16:43:07 2015 +0000
- Revision:
- 2:c648c5e93d5e
- Parent:
- 0:80245aff63ce
edit ack chacking with wait_ms(5) *Application Note:AN1028
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bborredon | 0:80245aff63ce | 1 | /*********************************************************** |
bborredon | 0:80245aff63ce | 2 | Author: Bernard Borredon |
bborredon | 0:80245aff63ce | 3 | Date: 27 december 2011 |
bborredon | 0:80245aff63ce | 4 | Version: 1.0 |
bborredon | 0:80245aff63ce | 5 | ************************************************************/ |
soulx | 2:c648c5e93d5e | 6 | #include "mbed.h" |
bborredon | 0:80245aff63ce | 7 | #include "eeprom.h" |
bborredon | 0:80245aff63ce | 8 | |
bborredon | 0:80245aff63ce | 9 | #define BIT_SET(x,n) (x=x | (0x01<<n)) |
bborredon | 0:80245aff63ce | 10 | #define BIT_TEST(x,n) (x & (0x01<<n)) |
bborredon | 0:80245aff63ce | 11 | #define BIT_CLEAR(x,n) (x=x & ~(0x01<<n)) |
bborredon | 0:80245aff63ce | 12 | |
bborredon | 0:80245aff63ce | 13 | EEPROM::EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl) |
bborredon | 0:80245aff63ce | 14 | { |
bborredon | 0:80245aff63ce | 15 | |
bborredon | 0:80245aff63ce | 16 | _errnum = EEPROM_NoError; |
bborredon | 0:80245aff63ce | 17 | _type = type; |
bborredon | 0:80245aff63ce | 18 | |
bborredon | 0:80245aff63ce | 19 | // Check address range |
bborredon | 0:80245aff63ce | 20 | _address = address; |
bborredon | 0:80245aff63ce | 21 | switch(type) { |
bborredon | 0:80245aff63ce | 22 | case T24C01 : |
bborredon | 0:80245aff63ce | 23 | case T24C02 : |
bborredon | 0:80245aff63ce | 24 | if(address > 7) { |
bborredon | 0:80245aff63ce | 25 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 26 | } |
bborredon | 0:80245aff63ce | 27 | _address = _address << 1; |
bborredon | 0:80245aff63ce | 28 | _page_write = 8; |
bborredon | 0:80245aff63ce | 29 | _page_number = 1; |
bborredon | 0:80245aff63ce | 30 | break; |
bborredon | 0:80245aff63ce | 31 | case T24C04 : |
bborredon | 0:80245aff63ce | 32 | if(address > 7) { |
bborredon | 0:80245aff63ce | 33 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 34 | } |
bborredon | 0:80245aff63ce | 35 | _address = (_address & 0xFE) << 1; |
bborredon | 0:80245aff63ce | 36 | _page_write = 16; |
bborredon | 0:80245aff63ce | 37 | _page_number = 2; |
bborredon | 0:80245aff63ce | 38 | break; |
bborredon | 0:80245aff63ce | 39 | case T24C08 : |
bborredon | 0:80245aff63ce | 40 | if(address > 7) { |
bborredon | 0:80245aff63ce | 41 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 42 | } |
bborredon | 0:80245aff63ce | 43 | _address = (_address & 0xFC) << 1; |
bborredon | 0:80245aff63ce | 44 | _page_write = 16; |
bborredon | 0:80245aff63ce | 45 | _page_number = 4; |
bborredon | 0:80245aff63ce | 46 | break; |
bborredon | 0:80245aff63ce | 47 | case T24C16 : |
bborredon | 0:80245aff63ce | 48 | _address = 0; |
bborredon | 0:80245aff63ce | 49 | _page_write = 16; |
bborredon | 0:80245aff63ce | 50 | _page_number = 8; |
bborredon | 0:80245aff63ce | 51 | break; |
bborredon | 0:80245aff63ce | 52 | case T24C32 : |
bborredon | 0:80245aff63ce | 53 | case T24C64 : |
soulx | 2:c648c5e93d5e | 54 | |
bborredon | 0:80245aff63ce | 55 | if(address > 7) { |
bborredon | 0:80245aff63ce | 56 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 57 | } |
soulx | 2:c648c5e93d5e | 58 | |
bborredon | 0:80245aff63ce | 59 | _address = _address << 1; |
soulx | 2:c648c5e93d5e | 60 | |
soulx | 2:c648c5e93d5e | 61 | |
bborredon | 0:80245aff63ce | 62 | _page_write = 32; |
bborredon | 0:80245aff63ce | 63 | _page_number = 1; |
bborredon | 0:80245aff63ce | 64 | break; |
bborredon | 0:80245aff63ce | 65 | case T24C128 : |
bborredon | 0:80245aff63ce | 66 | case T24C256 : |
bborredon | 0:80245aff63ce | 67 | if(address > 3) { |
bborredon | 0:80245aff63ce | 68 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 69 | } |
bborredon | 0:80245aff63ce | 70 | _address = _address << 1; |
bborredon | 0:80245aff63ce | 71 | _page_write = 64; |
bborredon | 0:80245aff63ce | 72 | _page_number = 1; |
bborredon | 0:80245aff63ce | 73 | break; |
bborredon | 0:80245aff63ce | 74 | case T24C512 : |
bborredon | 0:80245aff63ce | 75 | if(address > 3) { |
bborredon | 0:80245aff63ce | 76 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 77 | } |
bborredon | 0:80245aff63ce | 78 | _address = _address << 1; |
bborredon | 0:80245aff63ce | 79 | _page_write = 128; |
bborredon | 0:80245aff63ce | 80 | _page_number = 1; |
bborredon | 0:80245aff63ce | 81 | break; |
bborredon | 0:80245aff63ce | 82 | case T24C1024 : |
bborredon | 0:80245aff63ce | 83 | if(address > 7) { |
bborredon | 0:80245aff63ce | 84 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 85 | } |
bborredon | 0:80245aff63ce | 86 | _address = (_address & 0xFE) << 1; |
bborredon | 0:80245aff63ce | 87 | _page_write = 128; |
bborredon | 0:80245aff63ce | 88 | _page_number = 2; |
bborredon | 0:80245aff63ce | 89 | break; |
bborredon | 0:80245aff63ce | 90 | case T24C1025 : |
bborredon | 0:80245aff63ce | 91 | if(address > 3) { |
bborredon | 0:80245aff63ce | 92 | _errnum = EEPROM_BadAddress; |
bborredon | 0:80245aff63ce | 93 | } |
bborredon | 0:80245aff63ce | 94 | _address = _address << 1; |
bborredon | 0:80245aff63ce | 95 | _page_write = 128; |
bborredon | 0:80245aff63ce | 96 | _page_number = 2; |
bborredon | 0:80245aff63ce | 97 | break; |
bborredon | 0:80245aff63ce | 98 | } |
bborredon | 0:80245aff63ce | 99 | |
bborredon | 0:80245aff63ce | 100 | // Size in bytes |
bborredon | 0:80245aff63ce | 101 | _size = _type; |
bborredon | 0:80245aff63ce | 102 | if(_type == T24C1025) |
bborredon | 0:80245aff63ce | 103 | _size = T24C1024; |
bborredon | 0:80245aff63ce | 104 | |
bborredon | 0:80245aff63ce | 105 | // Set I2C frequency |
soulx | 2:c648c5e93d5e | 106 | _i2c.frequency(100000); |
bborredon | 0:80245aff63ce | 107 | } |
bborredon | 0:80245aff63ce | 108 | |
bborredon | 0:80245aff63ce | 109 | void EEPROM::write(uint16_t address, int8_t data) |
bborredon | 0:80245aff63ce | 110 | { |
bborredon | 0:80245aff63ce | 111 | uint8_t page; |
bborredon | 0:80245aff63ce | 112 | uint8_t addr; |
bborredon | 0:80245aff63ce | 113 | uint8_t cmd[3]; |
bborredon | 0:80245aff63ce | 114 | int len; |
bborredon | 0:80245aff63ce | 115 | int ack; |
bborredon | 0:80245aff63ce | 116 | |
bborredon | 0:80245aff63ce | 117 | // Check error |
bborredon | 0:80245aff63ce | 118 | if(_errnum) |
bborredon | 0:80245aff63ce | 119 | return; |
bborredon | 0:80245aff63ce | 120 | |
bborredon | 0:80245aff63ce | 121 | // Check address |
bborredon | 0:80245aff63ce | 122 | if(!checkAddress(address)) { |
bborredon | 0:80245aff63ce | 123 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 124 | return; |
bborredon | 0:80245aff63ce | 125 | } |
bborredon | 0:80245aff63ce | 126 | |
bborredon | 0:80245aff63ce | 127 | // Compute page number |
bborredon | 0:80245aff63ce | 128 | page = 0; |
bborredon | 0:80245aff63ce | 129 | if(_type < T24C32) |
bborredon | 0:80245aff63ce | 130 | page = (uint8_t) (address / 256); |
bborredon | 0:80245aff63ce | 131 | |
bborredon | 0:80245aff63ce | 132 | // Device address |
bborredon | 0:80245aff63ce | 133 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 134 | |
bborredon | 0:80245aff63ce | 135 | if(_type < T24C32) { |
bborredon | 0:80245aff63ce | 136 | len = 2; |
bborredon | 0:80245aff63ce | 137 | |
bborredon | 0:80245aff63ce | 138 | // Word address |
bborredon | 0:80245aff63ce | 139 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 140 | |
bborredon | 0:80245aff63ce | 141 | // Data |
bborredon | 0:80245aff63ce | 142 | cmd[1] = (uint8_t) data; |
bborredon | 0:80245aff63ce | 143 | } |
bborredon | 0:80245aff63ce | 144 | else { |
bborredon | 0:80245aff63ce | 145 | len = 3; |
bborredon | 0:80245aff63ce | 146 | |
bborredon | 0:80245aff63ce | 147 | // First word address (MSB) |
bborredon | 0:80245aff63ce | 148 | cmd[0] = (uint8_t) (address >> 8); |
bborredon | 0:80245aff63ce | 149 | |
bborredon | 0:80245aff63ce | 150 | // Second word address (LSB) |
bborredon | 0:80245aff63ce | 151 | cmd[1] = (uint8_t) address; |
bborredon | 0:80245aff63ce | 152 | |
bborredon | 0:80245aff63ce | 153 | // Data |
bborredon | 0:80245aff63ce | 154 | cmd[2] = (uint8_t) data; |
bborredon | 0:80245aff63ce | 155 | } |
bborredon | 0:80245aff63ce | 156 | |
soulx | 2:c648c5e93d5e | 157 | // printf("len %d address %02x cmd[0] %02x cmd[1] %02x cmd[2] %02x\n",len,addr,cmd[0],cmd[1],cmd[2]); |
bborredon | 0:80245aff63ce | 158 | |
bborredon | 0:80245aff63ce | 159 | ack = _i2c.write((int)addr,(char *)cmd,len); |
bborredon | 0:80245aff63ce | 160 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 161 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 162 | return; |
bborredon | 0:80245aff63ce | 163 | } |
bborredon | 0:80245aff63ce | 164 | |
bborredon | 0:80245aff63ce | 165 | // Wait end of write |
bborredon | 0:80245aff63ce | 166 | ready(); |
bborredon | 0:80245aff63ce | 167 | |
bborredon | 0:80245aff63ce | 168 | } |
bborredon | 0:80245aff63ce | 169 | |
bborredon | 0:80245aff63ce | 170 | void EEPROM::write(uint16_t address, int8_t data[], uint16_t length) |
bborredon | 0:80245aff63ce | 171 | { |
bborredon | 0:80245aff63ce | 172 | uint8_t page; |
bborredon | 0:80245aff63ce | 173 | uint8_t addr; |
bborredon | 0:80245aff63ce | 174 | uint8_t blocs,remain; |
bborredon | 0:80245aff63ce | 175 | uint8_t fpart,lpart; |
bborredon | 0:80245aff63ce | 176 | uint8_t i,j,ind; |
bborredon | 0:80245aff63ce | 177 | uint8_t cmd[129]; |
bborredon | 0:80245aff63ce | 178 | int ack; |
bborredon | 0:80245aff63ce | 179 | |
bborredon | 0:80245aff63ce | 180 | // Check error |
bborredon | 0:80245aff63ce | 181 | if(_errnum) |
bborredon | 0:80245aff63ce | 182 | return; |
bborredon | 0:80245aff63ce | 183 | |
bborredon | 0:80245aff63ce | 184 | // Check address |
bborredon | 0:80245aff63ce | 185 | if(!checkAddress(address)) { |
bborredon | 0:80245aff63ce | 186 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 187 | return; |
bborredon | 0:80245aff63ce | 188 | } |
bborredon | 0:80245aff63ce | 189 | |
bborredon | 0:80245aff63ce | 190 | // Check length |
bborredon | 0:80245aff63ce | 191 | if(!checkAddress(address + length - 1)) { |
bborredon | 0:80245aff63ce | 192 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 193 | return; |
bborredon | 0:80245aff63ce | 194 | } |
bborredon | 0:80245aff63ce | 195 | |
bborredon | 0:80245aff63ce | 196 | // Compute blocs numbers |
bborredon | 0:80245aff63ce | 197 | blocs = length / _page_write; |
bborredon | 0:80245aff63ce | 198 | |
bborredon | 0:80245aff63ce | 199 | // Compute remaining bytes |
bborredon | 0:80245aff63ce | 200 | remain = length - blocs * _page_write; |
bborredon | 0:80245aff63ce | 201 | |
bborredon | 0:80245aff63ce | 202 | for(i = 0;i < blocs;i++) { |
bborredon | 0:80245aff63ce | 203 | // Compute page number |
bborredon | 0:80245aff63ce | 204 | page = 0; |
bborredon | 0:80245aff63ce | 205 | if(_type < T24C32) |
bborredon | 0:80245aff63ce | 206 | page = (uint8_t) (address / 256); |
bborredon | 0:80245aff63ce | 207 | |
bborredon | 0:80245aff63ce | 208 | // Device address |
bborredon | 0:80245aff63ce | 209 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 210 | |
bborredon | 0:80245aff63ce | 211 | if(_type < T24C32) { |
bborredon | 0:80245aff63ce | 212 | // Word address |
bborredon | 0:80245aff63ce | 213 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 214 | |
bborredon | 0:80245aff63ce | 215 | if((uint8_t) ((address + _page_write) / 256) == page) { // Data fit in the same page |
bborredon | 0:80245aff63ce | 216 | // Add data |
bborredon | 0:80245aff63ce | 217 | for(j = 0;j < _page_write;j++) |
bborredon | 0:80245aff63ce | 218 | cmd[j + 1] = (uint8_t) data[i * _page_write + j]; |
bborredon | 0:80245aff63ce | 219 | |
bborredon | 0:80245aff63ce | 220 | // Write data |
bborredon | 0:80245aff63ce | 221 | ack = _i2c.write((int)addr,(char *)cmd,_page_write + 1); |
bborredon | 0:80245aff63ce | 222 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 223 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 224 | return; |
bborredon | 0:80245aff63ce | 225 | } |
bborredon | 0:80245aff63ce | 226 | |
bborredon | 0:80245aff63ce | 227 | // Wait end of write |
bborredon | 0:80245aff63ce | 228 | ready(); |
bborredon | 0:80245aff63ce | 229 | |
bborredon | 0:80245aff63ce | 230 | // Increment address |
bborredon | 0:80245aff63ce | 231 | address += _page_write; |
bborredon | 0:80245aff63ce | 232 | } |
bborredon | 0:80245aff63ce | 233 | else { // Data on 2 pages. We must split the write |
bborredon | 0:80245aff63ce | 234 | // Number of bytes in current page |
bborredon | 0:80245aff63ce | 235 | fpart = (page + 1) * 256 - address; |
bborredon | 0:80245aff63ce | 236 | |
bborredon | 0:80245aff63ce | 237 | // Add data for current page |
bborredon | 0:80245aff63ce | 238 | for(j = 0;j < fpart;j++) |
bborredon | 0:80245aff63ce | 239 | cmd[j + 1] = (uint8_t) data[i * _page_write + j]; |
bborredon | 0:80245aff63ce | 240 | |
bborredon | 0:80245aff63ce | 241 | // Write data for current page |
bborredon | 0:80245aff63ce | 242 | ack = _i2c.write((int)addr,(char *)cmd,fpart + 1); |
bborredon | 0:80245aff63ce | 243 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 244 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 245 | return; |
bborredon | 0:80245aff63ce | 246 | } |
bborredon | 0:80245aff63ce | 247 | |
bborredon | 0:80245aff63ce | 248 | // Wait end of write |
bborredon | 0:80245aff63ce | 249 | ready(); |
bborredon | 0:80245aff63ce | 250 | |
bborredon | 0:80245aff63ce | 251 | // Increment address |
bborredon | 0:80245aff63ce | 252 | address += fpart; |
bborredon | 0:80245aff63ce | 253 | |
bborredon | 0:80245aff63ce | 254 | if(page < _page_number - 1) { |
bborredon | 0:80245aff63ce | 255 | // Increment page |
bborredon | 0:80245aff63ce | 256 | page++; |
bborredon | 0:80245aff63ce | 257 | |
bborredon | 0:80245aff63ce | 258 | // Device address |
bborredon | 0:80245aff63ce | 259 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 260 | |
bborredon | 0:80245aff63ce | 261 | // Word address |
bborredon | 0:80245aff63ce | 262 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 263 | |
bborredon | 0:80245aff63ce | 264 | // Data index |
bborredon | 0:80245aff63ce | 265 | ind = i * _page_write + fpart; |
bborredon | 0:80245aff63ce | 266 | |
bborredon | 0:80245aff63ce | 267 | // Number of bytes in next page |
bborredon | 0:80245aff63ce | 268 | lpart = _page_write - fpart; |
bborredon | 0:80245aff63ce | 269 | |
bborredon | 0:80245aff63ce | 270 | // Add data for next page |
bborredon | 0:80245aff63ce | 271 | for(j = 0;j < lpart;j++) |
bborredon | 0:80245aff63ce | 272 | cmd[j + 1] = (uint8_t) data[ind + j]; |
bborredon | 0:80245aff63ce | 273 | |
bborredon | 0:80245aff63ce | 274 | // Write data for next page |
bborredon | 0:80245aff63ce | 275 | ack = _i2c.write((int)addr,(char *)cmd,lpart + 1); |
bborredon | 0:80245aff63ce | 276 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 277 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 278 | return; |
bborredon | 0:80245aff63ce | 279 | } |
bborredon | 0:80245aff63ce | 280 | |
bborredon | 0:80245aff63ce | 281 | // Wait end of write |
bborredon | 0:80245aff63ce | 282 | ready(); |
bborredon | 0:80245aff63ce | 283 | |
bborredon | 0:80245aff63ce | 284 | // Increment address |
bborredon | 0:80245aff63ce | 285 | address += lpart; |
bborredon | 0:80245aff63ce | 286 | } |
bborredon | 0:80245aff63ce | 287 | } |
bborredon | 0:80245aff63ce | 288 | } |
bborredon | 0:80245aff63ce | 289 | else { |
bborredon | 0:80245aff63ce | 290 | // First word address (MSB) |
bborredon | 0:80245aff63ce | 291 | cmd[0] = (uint8_t) (address >> 8); |
bborredon | 0:80245aff63ce | 292 | |
bborredon | 0:80245aff63ce | 293 | // Second word address (LSB) |
bborredon | 0:80245aff63ce | 294 | cmd[1] = (uint8_t) address; |
bborredon | 0:80245aff63ce | 295 | |
bborredon | 0:80245aff63ce | 296 | // Add data |
bborredon | 0:80245aff63ce | 297 | for(j = 0;j < _page_write;j++) |
bborredon | 0:80245aff63ce | 298 | cmd[j + 2] = (uint8_t) data[i * _page_write + j]; |
bborredon | 0:80245aff63ce | 299 | |
bborredon | 0:80245aff63ce | 300 | // Write data |
bborredon | 0:80245aff63ce | 301 | ack = _i2c.write((int)addr,(char *)cmd,_page_write + 2); |
bborredon | 0:80245aff63ce | 302 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 303 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 304 | return; |
bborredon | 0:80245aff63ce | 305 | } |
bborredon | 0:80245aff63ce | 306 | |
bborredon | 0:80245aff63ce | 307 | // Wait end of write |
bborredon | 0:80245aff63ce | 308 | ready(); |
bborredon | 0:80245aff63ce | 309 | |
bborredon | 0:80245aff63ce | 310 | // Increment address |
bborredon | 0:80245aff63ce | 311 | address += _page_write; |
bborredon | 0:80245aff63ce | 312 | } |
bborredon | 0:80245aff63ce | 313 | } |
bborredon | 0:80245aff63ce | 314 | |
bborredon | 0:80245aff63ce | 315 | // Compute page number |
bborredon | 0:80245aff63ce | 316 | page = 0; |
bborredon | 0:80245aff63ce | 317 | if(_type < T24C32) |
bborredon | 0:80245aff63ce | 318 | page = (uint8_t) (address / 256); |
bborredon | 0:80245aff63ce | 319 | |
bborredon | 0:80245aff63ce | 320 | // Device address |
bborredon | 0:80245aff63ce | 321 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 322 | |
bborredon | 0:80245aff63ce | 323 | if(_type < T24C32) { |
bborredon | 0:80245aff63ce | 324 | // Word address |
bborredon | 0:80245aff63ce | 325 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 326 | |
bborredon | 0:80245aff63ce | 327 | if((uint8_t) ((address + remain) / 256) == page) { // Data fit in the same page |
bborredon | 0:80245aff63ce | 328 | // Add data for the current page |
bborredon | 0:80245aff63ce | 329 | for(j = 0;j < remain;j++) |
bborredon | 0:80245aff63ce | 330 | cmd[j + 1] = (uint8_t) data[blocs * _page_write + j]; |
bborredon | 0:80245aff63ce | 331 | |
bborredon | 0:80245aff63ce | 332 | // Write data for the current page |
bborredon | 0:80245aff63ce | 333 | ack = _i2c.write((int)addr,(char *)cmd,remain + 1); |
bborredon | 0:80245aff63ce | 334 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 335 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 336 | return; |
bborredon | 0:80245aff63ce | 337 | } |
bborredon | 0:80245aff63ce | 338 | |
bborredon | 0:80245aff63ce | 339 | // Wait end of write |
bborredon | 0:80245aff63ce | 340 | ready(); |
bborredon | 0:80245aff63ce | 341 | } |
bborredon | 0:80245aff63ce | 342 | else { // Data on 2 pages. We must split the write |
bborredon | 0:80245aff63ce | 343 | // Number of bytes in current page |
bborredon | 0:80245aff63ce | 344 | fpart = (page + 1) * 256 - address; |
bborredon | 0:80245aff63ce | 345 | |
bborredon | 0:80245aff63ce | 346 | // Add data for current page |
bborredon | 0:80245aff63ce | 347 | for(j = 0;j < fpart;j++) |
bborredon | 0:80245aff63ce | 348 | cmd[j + 1] = (uint8_t) data[blocs * _page_write + j]; |
bborredon | 0:80245aff63ce | 349 | |
bborredon | 0:80245aff63ce | 350 | // Write data for current page |
bborredon | 0:80245aff63ce | 351 | ack = _i2c.write((int)addr,(char *)cmd,fpart + 1); |
bborredon | 0:80245aff63ce | 352 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 353 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 354 | return; |
bborredon | 0:80245aff63ce | 355 | } |
bborredon | 0:80245aff63ce | 356 | |
bborredon | 0:80245aff63ce | 357 | // Wait end of write |
bborredon | 0:80245aff63ce | 358 | ready(); |
bborredon | 0:80245aff63ce | 359 | |
bborredon | 0:80245aff63ce | 360 | // Increment address |
bborredon | 0:80245aff63ce | 361 | address += fpart; |
bborredon | 0:80245aff63ce | 362 | |
bborredon | 0:80245aff63ce | 363 | if(page < _page_number - 1) { |
bborredon | 0:80245aff63ce | 364 | // Increment page |
bborredon | 0:80245aff63ce | 365 | page++; |
bborredon | 0:80245aff63ce | 366 | |
bborredon | 0:80245aff63ce | 367 | // Device address |
bborredon | 0:80245aff63ce | 368 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 369 | |
bborredon | 0:80245aff63ce | 370 | // Word address |
bborredon | 0:80245aff63ce | 371 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 372 | |
bborredon | 0:80245aff63ce | 373 | // Data index |
bborredon | 0:80245aff63ce | 374 | ind = blocs * _page_write + fpart; |
bborredon | 0:80245aff63ce | 375 | |
bborredon | 0:80245aff63ce | 376 | // Number of bytes in next page |
bborredon | 0:80245aff63ce | 377 | lpart = remain - fpart; |
bborredon | 0:80245aff63ce | 378 | |
bborredon | 0:80245aff63ce | 379 | // Add data for next page |
bborredon | 0:80245aff63ce | 380 | for(j = 0;j < lpart;j++) |
bborredon | 0:80245aff63ce | 381 | cmd[j + 1] = (uint8_t) data[ind + j]; |
bborredon | 0:80245aff63ce | 382 | |
bborredon | 0:80245aff63ce | 383 | // Write data for next page |
bborredon | 0:80245aff63ce | 384 | ack = _i2c.write((int)addr,(char *)cmd,lpart + 1); |
bborredon | 0:80245aff63ce | 385 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 386 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 387 | return; |
bborredon | 0:80245aff63ce | 388 | } |
bborredon | 0:80245aff63ce | 389 | |
bborredon | 0:80245aff63ce | 390 | // Wait end of write |
bborredon | 0:80245aff63ce | 391 | ready(); |
bborredon | 0:80245aff63ce | 392 | } |
bborredon | 0:80245aff63ce | 393 | } |
bborredon | 0:80245aff63ce | 394 | } |
bborredon | 0:80245aff63ce | 395 | else { |
bborredon | 0:80245aff63ce | 396 | // Fist word address (MSB) |
bborredon | 0:80245aff63ce | 397 | cmd[0] = (uint8_t) (address >> 8); |
bborredon | 0:80245aff63ce | 398 | |
bborredon | 0:80245aff63ce | 399 | // Second word address (LSB) |
bborredon | 0:80245aff63ce | 400 | cmd[1] = (uint8_t) address; |
bborredon | 0:80245aff63ce | 401 | |
bborredon | 0:80245aff63ce | 402 | // Add data for the current page |
bborredon | 0:80245aff63ce | 403 | for(j = 0;j < remain;j++) |
bborredon | 0:80245aff63ce | 404 | cmd[j + 2] = (uint8_t) data[blocs * _page_write + j]; |
bborredon | 0:80245aff63ce | 405 | |
bborredon | 0:80245aff63ce | 406 | // Write data for the current page |
bborredon | 0:80245aff63ce | 407 | ack = _i2c.write((int)addr,(char *)cmd,remain + 2); |
bborredon | 0:80245aff63ce | 408 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 409 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 410 | return; |
bborredon | 0:80245aff63ce | 411 | } |
bborredon | 0:80245aff63ce | 412 | |
bborredon | 0:80245aff63ce | 413 | // Wait end of write |
bborredon | 0:80245aff63ce | 414 | ready(); |
bborredon | 0:80245aff63ce | 415 | } |
bborredon | 0:80245aff63ce | 416 | |
bborredon | 0:80245aff63ce | 417 | } |
bborredon | 0:80245aff63ce | 418 | |
bborredon | 0:80245aff63ce | 419 | void EEPROM::write(uint16_t address, int16_t data) |
bborredon | 0:80245aff63ce | 420 | { |
bborredon | 0:80245aff63ce | 421 | int8_t cmd[2]; |
bborredon | 0:80245aff63ce | 422 | |
bborredon | 0:80245aff63ce | 423 | // Check error |
bborredon | 0:80245aff63ce | 424 | if(_errnum) |
bborredon | 0:80245aff63ce | 425 | return; |
bborredon | 0:80245aff63ce | 426 | |
bborredon | 0:80245aff63ce | 427 | // Check address |
bborredon | 0:80245aff63ce | 428 | if(!checkAddress(address + 1)) { |
bborredon | 0:80245aff63ce | 429 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 430 | return; |
bborredon | 0:80245aff63ce | 431 | } |
bborredon | 0:80245aff63ce | 432 | |
bborredon | 0:80245aff63ce | 433 | memcpy(cmd,&data,2); |
bborredon | 0:80245aff63ce | 434 | |
bborredon | 0:80245aff63ce | 435 | write(address,cmd,2); |
bborredon | 0:80245aff63ce | 436 | |
bborredon | 0:80245aff63ce | 437 | } |
bborredon | 0:80245aff63ce | 438 | |
bborredon | 0:80245aff63ce | 439 | void EEPROM::write(uint16_t address, int32_t data) |
bborredon | 0:80245aff63ce | 440 | { |
bborredon | 0:80245aff63ce | 441 | int8_t cmd[4]; |
bborredon | 0:80245aff63ce | 442 | |
bborredon | 0:80245aff63ce | 443 | // Check error |
bborredon | 0:80245aff63ce | 444 | if(_errnum) |
bborredon | 0:80245aff63ce | 445 | return; |
bborredon | 0:80245aff63ce | 446 | |
bborredon | 0:80245aff63ce | 447 | // Check address |
bborredon | 0:80245aff63ce | 448 | if(!checkAddress(address + 3)) { |
bborredon | 0:80245aff63ce | 449 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 450 | return; |
bborredon | 0:80245aff63ce | 451 | } |
bborredon | 0:80245aff63ce | 452 | |
bborredon | 0:80245aff63ce | 453 | memcpy(cmd,&data,4); |
bborredon | 0:80245aff63ce | 454 | |
bborredon | 0:80245aff63ce | 455 | write(address,cmd,4); |
soulx | 2:c648c5e93d5e | 456 | |
bborredon | 0:80245aff63ce | 457 | } |
bborredon | 0:80245aff63ce | 458 | |
bborredon | 0:80245aff63ce | 459 | void EEPROM::write(uint16_t address, float data) |
bborredon | 0:80245aff63ce | 460 | { |
bborredon | 0:80245aff63ce | 461 | int8_t cmd[4]; |
bborredon | 0:80245aff63ce | 462 | |
bborredon | 0:80245aff63ce | 463 | // Check error |
bborredon | 0:80245aff63ce | 464 | if(_errnum) |
bborredon | 0:80245aff63ce | 465 | return; |
bborredon | 0:80245aff63ce | 466 | |
bborredon | 0:80245aff63ce | 467 | // Check address |
bborredon | 0:80245aff63ce | 468 | if(!checkAddress(address + 3)) { |
bborredon | 0:80245aff63ce | 469 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 470 | return; |
bborredon | 0:80245aff63ce | 471 | } |
bborredon | 0:80245aff63ce | 472 | |
bborredon | 0:80245aff63ce | 473 | memcpy(cmd,&data,4); |
bborredon | 0:80245aff63ce | 474 | |
bborredon | 0:80245aff63ce | 475 | write(address,cmd,4); |
bborredon | 0:80245aff63ce | 476 | |
bborredon | 0:80245aff63ce | 477 | } |
bborredon | 0:80245aff63ce | 478 | |
bborredon | 0:80245aff63ce | 479 | void EEPROM::write(uint16_t address, void *data, uint16_t size) |
bborredon | 0:80245aff63ce | 480 | { |
bborredon | 0:80245aff63ce | 481 | int8_t *cmd = NULL; |
bborredon | 0:80245aff63ce | 482 | |
bborredon | 0:80245aff63ce | 483 | // Check error |
bborredon | 0:80245aff63ce | 484 | if(_errnum) |
bborredon | 0:80245aff63ce | 485 | return; |
bborredon | 0:80245aff63ce | 486 | |
bborredon | 0:80245aff63ce | 487 | // Check address |
bborredon | 0:80245aff63ce | 488 | if(!checkAddress(address + size - 1)) { |
bborredon | 0:80245aff63ce | 489 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 490 | return; |
bborredon | 0:80245aff63ce | 491 | } |
bborredon | 0:80245aff63ce | 492 | |
bborredon | 0:80245aff63ce | 493 | cmd = (int8_t *)malloc(size); |
bborredon | 0:80245aff63ce | 494 | if(cmd == NULL) { |
bborredon | 0:80245aff63ce | 495 | _errnum = EEPROM_MallocError; |
bborredon | 0:80245aff63ce | 496 | return; |
bborredon | 0:80245aff63ce | 497 | } |
bborredon | 0:80245aff63ce | 498 | |
bborredon | 0:80245aff63ce | 499 | memcpy(cmd,data,size); |
bborredon | 0:80245aff63ce | 500 | |
bborredon | 0:80245aff63ce | 501 | write(address,cmd,size); |
bborredon | 0:80245aff63ce | 502 | |
bborredon | 0:80245aff63ce | 503 | free(cmd); |
bborredon | 0:80245aff63ce | 504 | |
bborredon | 0:80245aff63ce | 505 | } |
bborredon | 0:80245aff63ce | 506 | |
bborredon | 0:80245aff63ce | 507 | void EEPROM::read(uint16_t address, int8_t& data) |
bborredon | 0:80245aff63ce | 508 | { |
bborredon | 0:80245aff63ce | 509 | uint8_t page; |
bborredon | 0:80245aff63ce | 510 | uint8_t addr; |
bborredon | 0:80245aff63ce | 511 | uint8_t cmd[2]; |
bborredon | 0:80245aff63ce | 512 | uint8_t len; |
bborredon | 0:80245aff63ce | 513 | int ack; |
bborredon | 0:80245aff63ce | 514 | |
bborredon | 0:80245aff63ce | 515 | // Check error |
bborredon | 0:80245aff63ce | 516 | if(_errnum) |
bborredon | 0:80245aff63ce | 517 | return; |
bborredon | 0:80245aff63ce | 518 | |
bborredon | 0:80245aff63ce | 519 | // Check address |
bborredon | 0:80245aff63ce | 520 | if(!checkAddress(address)) { |
bborredon | 0:80245aff63ce | 521 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 522 | return; |
bborredon | 0:80245aff63ce | 523 | } |
bborredon | 0:80245aff63ce | 524 | |
bborredon | 0:80245aff63ce | 525 | // Compute page number |
bborredon | 0:80245aff63ce | 526 | page = 0; |
bborredon | 0:80245aff63ce | 527 | if(_type < T24C32) |
bborredon | 0:80245aff63ce | 528 | page = (uint8_t) (address / 256); |
bborredon | 0:80245aff63ce | 529 | |
bborredon | 0:80245aff63ce | 530 | // Device address |
bborredon | 0:80245aff63ce | 531 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 532 | |
bborredon | 0:80245aff63ce | 533 | if(_type < T24C32) { |
bborredon | 0:80245aff63ce | 534 | len = 1; |
bborredon | 0:80245aff63ce | 535 | |
bborredon | 0:80245aff63ce | 536 | // Word address |
bborredon | 0:80245aff63ce | 537 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 538 | } |
bborredon | 0:80245aff63ce | 539 | else { |
bborredon | 0:80245aff63ce | 540 | len = 2; |
bborredon | 0:80245aff63ce | 541 | |
bborredon | 0:80245aff63ce | 542 | // First word address (MSB) |
bborredon | 0:80245aff63ce | 543 | cmd[0] = (uint8_t) (address >> 8); |
bborredon | 0:80245aff63ce | 544 | |
bborredon | 0:80245aff63ce | 545 | // Second word address (LSB) |
bborredon | 0:80245aff63ce | 546 | cmd[1] = (uint8_t)address; |
bborredon | 0:80245aff63ce | 547 | } |
bborredon | 0:80245aff63ce | 548 | |
bborredon | 0:80245aff63ce | 549 | // Write command |
bborredon | 0:80245aff63ce | 550 | ack = _i2c.write((int)addr,(char *)cmd,len,true); |
bborredon | 0:80245aff63ce | 551 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 552 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 553 | return; |
bborredon | 0:80245aff63ce | 554 | } |
bborredon | 0:80245aff63ce | 555 | |
bborredon | 0:80245aff63ce | 556 | // Read data |
bborredon | 0:80245aff63ce | 557 | ack = _i2c.read((int)addr,(char *)&data,sizeof(data)); |
bborredon | 0:80245aff63ce | 558 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 559 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 560 | return; |
bborredon | 0:80245aff63ce | 561 | } |
bborredon | 0:80245aff63ce | 562 | |
bborredon | 0:80245aff63ce | 563 | } |
bborredon | 0:80245aff63ce | 564 | |
bborredon | 0:80245aff63ce | 565 | void EEPROM::read(uint16_t address, int8_t *data, uint16_t size) |
bborredon | 0:80245aff63ce | 566 | { |
bborredon | 0:80245aff63ce | 567 | uint8_t page; |
bborredon | 0:80245aff63ce | 568 | uint8_t addr; |
bborredon | 0:80245aff63ce | 569 | uint8_t cmd[2]; |
bborredon | 0:80245aff63ce | 570 | uint8_t len; |
bborredon | 0:80245aff63ce | 571 | int ack; |
bborredon | 0:80245aff63ce | 572 | |
bborredon | 0:80245aff63ce | 573 | // Check error |
bborredon | 0:80245aff63ce | 574 | if(_errnum) |
bborredon | 0:80245aff63ce | 575 | return; |
bborredon | 0:80245aff63ce | 576 | |
bborredon | 0:80245aff63ce | 577 | // Check address |
bborredon | 0:80245aff63ce | 578 | if(!checkAddress(address)) { |
bborredon | 0:80245aff63ce | 579 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 580 | return; |
bborredon | 0:80245aff63ce | 581 | } |
bborredon | 0:80245aff63ce | 582 | |
bborredon | 0:80245aff63ce | 583 | // Check size |
bborredon | 0:80245aff63ce | 584 | if(!checkAddress(address + size - 1)) { |
bborredon | 0:80245aff63ce | 585 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 586 | return; |
bborredon | 0:80245aff63ce | 587 | } |
bborredon | 0:80245aff63ce | 588 | |
bborredon | 0:80245aff63ce | 589 | // Compute page number |
bborredon | 0:80245aff63ce | 590 | page = 0; |
bborredon | 0:80245aff63ce | 591 | if(_type < T24C32) |
bborredon | 0:80245aff63ce | 592 | page = (uint8_t) (address / 256); |
bborredon | 0:80245aff63ce | 593 | |
bborredon | 0:80245aff63ce | 594 | // Device address |
bborredon | 0:80245aff63ce | 595 | addr = EEPROM_Address | _address | (page << 1); |
bborredon | 0:80245aff63ce | 596 | |
bborredon | 0:80245aff63ce | 597 | if(_type < T24C32) { |
bborredon | 0:80245aff63ce | 598 | len = 1; |
bborredon | 0:80245aff63ce | 599 | |
bborredon | 0:80245aff63ce | 600 | // Word address |
bborredon | 0:80245aff63ce | 601 | cmd[0] = (uint8_t) (address - page * 256); |
bborredon | 0:80245aff63ce | 602 | } |
bborredon | 0:80245aff63ce | 603 | else { |
bborredon | 0:80245aff63ce | 604 | len = 2; |
bborredon | 0:80245aff63ce | 605 | |
bborredon | 0:80245aff63ce | 606 | // First word address (MSB) |
bborredon | 0:80245aff63ce | 607 | cmd[0] = (uint8_t) (address >> 8); |
bborredon | 0:80245aff63ce | 608 | |
bborredon | 0:80245aff63ce | 609 | // Second word address (LSB) |
bborredon | 0:80245aff63ce | 610 | cmd[1] = (uint8_t) address; |
bborredon | 0:80245aff63ce | 611 | } |
bborredon | 0:80245aff63ce | 612 | |
bborredon | 0:80245aff63ce | 613 | // Write command |
bborredon | 0:80245aff63ce | 614 | ack = _i2c.write((int)addr,(char *)cmd,len,true); |
bborredon | 0:80245aff63ce | 615 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 616 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 617 | return; |
bborredon | 0:80245aff63ce | 618 | } |
bborredon | 0:80245aff63ce | 619 | |
bborredon | 0:80245aff63ce | 620 | // Sequential read |
bborredon | 0:80245aff63ce | 621 | ack = _i2c.read((int)addr,(char *)data,size); |
bborredon | 0:80245aff63ce | 622 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 623 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 624 | return; |
bborredon | 0:80245aff63ce | 625 | } |
bborredon | 0:80245aff63ce | 626 | |
bborredon | 0:80245aff63ce | 627 | } |
bborredon | 0:80245aff63ce | 628 | |
bborredon | 0:80245aff63ce | 629 | void EEPROM::read(int8_t& data) |
bborredon | 0:80245aff63ce | 630 | { |
bborredon | 0:80245aff63ce | 631 | uint8_t addr; |
bborredon | 0:80245aff63ce | 632 | int ack; |
bborredon | 0:80245aff63ce | 633 | |
bborredon | 0:80245aff63ce | 634 | // Check error |
bborredon | 0:80245aff63ce | 635 | if(_errnum) |
bborredon | 0:80245aff63ce | 636 | return; |
bborredon | 0:80245aff63ce | 637 | |
bborredon | 0:80245aff63ce | 638 | // Device address |
bborredon | 0:80245aff63ce | 639 | addr = EEPROM_Address | _address; |
bborredon | 0:80245aff63ce | 640 | |
bborredon | 0:80245aff63ce | 641 | // Read data |
bborredon | 0:80245aff63ce | 642 | ack = _i2c.read((int)addr,(char *)&data,sizeof(data)); |
bborredon | 0:80245aff63ce | 643 | if(ack != 0) { |
bborredon | 0:80245aff63ce | 644 | _errnum = EEPROM_I2cError; |
bborredon | 0:80245aff63ce | 645 | return; |
bborredon | 0:80245aff63ce | 646 | } |
bborredon | 0:80245aff63ce | 647 | |
bborredon | 0:80245aff63ce | 648 | } |
bborredon | 0:80245aff63ce | 649 | |
bborredon | 0:80245aff63ce | 650 | void EEPROM::read(uint16_t address, int16_t& data) |
bborredon | 0:80245aff63ce | 651 | { |
bborredon | 0:80245aff63ce | 652 | int8_t cmd[2]; |
bborredon | 0:80245aff63ce | 653 | |
bborredon | 0:80245aff63ce | 654 | // Check error |
bborredon | 0:80245aff63ce | 655 | if(_errnum) |
bborredon | 0:80245aff63ce | 656 | return; |
bborredon | 0:80245aff63ce | 657 | |
bborredon | 0:80245aff63ce | 658 | // Check address |
bborredon | 0:80245aff63ce | 659 | if(!checkAddress(address + 1)) { |
bborredon | 0:80245aff63ce | 660 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 661 | return; |
bborredon | 0:80245aff63ce | 662 | } |
bborredon | 0:80245aff63ce | 663 | |
bborredon | 0:80245aff63ce | 664 | read(address,cmd,2); |
bborredon | 0:80245aff63ce | 665 | |
bborredon | 0:80245aff63ce | 666 | memcpy(&data,cmd,2); |
bborredon | 0:80245aff63ce | 667 | |
bborredon | 0:80245aff63ce | 668 | } |
bborredon | 0:80245aff63ce | 669 | |
bborredon | 0:80245aff63ce | 670 | void EEPROM::read(uint16_t address, int32_t& data) |
bborredon | 0:80245aff63ce | 671 | { |
bborredon | 0:80245aff63ce | 672 | int8_t cmd[4]; |
bborredon | 0:80245aff63ce | 673 | |
bborredon | 0:80245aff63ce | 674 | // Check error |
bborredon | 0:80245aff63ce | 675 | if(_errnum) |
bborredon | 0:80245aff63ce | 676 | return; |
bborredon | 0:80245aff63ce | 677 | |
bborredon | 0:80245aff63ce | 678 | // Check address |
bborredon | 0:80245aff63ce | 679 | if(!checkAddress(address + 3)) { |
bborredon | 0:80245aff63ce | 680 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 681 | return; |
bborredon | 0:80245aff63ce | 682 | } |
bborredon | 0:80245aff63ce | 683 | |
bborredon | 0:80245aff63ce | 684 | read(address,cmd,4); |
bborredon | 0:80245aff63ce | 685 | |
bborredon | 0:80245aff63ce | 686 | memcpy(&data,cmd,4); |
bborredon | 0:80245aff63ce | 687 | |
bborredon | 0:80245aff63ce | 688 | } |
bborredon | 0:80245aff63ce | 689 | |
bborredon | 0:80245aff63ce | 690 | void EEPROM::read(uint16_t address, float& data) |
bborredon | 0:80245aff63ce | 691 | { |
bborredon | 0:80245aff63ce | 692 | int8_t cmd[4]; |
bborredon | 0:80245aff63ce | 693 | |
bborredon | 0:80245aff63ce | 694 | // Check error |
bborredon | 0:80245aff63ce | 695 | if(_errnum) |
bborredon | 0:80245aff63ce | 696 | return; |
bborredon | 0:80245aff63ce | 697 | |
bborredon | 0:80245aff63ce | 698 | // Check address |
bborredon | 0:80245aff63ce | 699 | if(!checkAddress(address + 3)) { |
bborredon | 0:80245aff63ce | 700 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 701 | return; |
bborredon | 0:80245aff63ce | 702 | } |
bborredon | 0:80245aff63ce | 703 | |
bborredon | 0:80245aff63ce | 704 | read(address,cmd,4); |
bborredon | 0:80245aff63ce | 705 | |
bborredon | 0:80245aff63ce | 706 | memcpy(&data,cmd,4); |
bborredon | 0:80245aff63ce | 707 | |
bborredon | 0:80245aff63ce | 708 | } |
bborredon | 0:80245aff63ce | 709 | |
bborredon | 0:80245aff63ce | 710 | void EEPROM::read(uint16_t address, void *data, uint16_t size) |
bborredon | 0:80245aff63ce | 711 | { |
bborredon | 0:80245aff63ce | 712 | int8_t *cmd = NULL; |
bborredon | 0:80245aff63ce | 713 | |
bborredon | 0:80245aff63ce | 714 | // Check error |
bborredon | 0:80245aff63ce | 715 | if(_errnum) |
bborredon | 0:80245aff63ce | 716 | return; |
bborredon | 0:80245aff63ce | 717 | |
bborredon | 0:80245aff63ce | 718 | // Check address |
bborredon | 0:80245aff63ce | 719 | if(!checkAddress(address + size - 1)) { |
bborredon | 0:80245aff63ce | 720 | _errnum = EEPROM_OutOfRange; |
bborredon | 0:80245aff63ce | 721 | return; |
bborredon | 0:80245aff63ce | 722 | } |
bborredon | 0:80245aff63ce | 723 | |
bborredon | 0:80245aff63ce | 724 | cmd = (int8_t *)malloc(size); |
bborredon | 0:80245aff63ce | 725 | if(cmd == NULL) { |
bborredon | 0:80245aff63ce | 726 | _errnum = EEPROM_MallocError; |
bborredon | 0:80245aff63ce | 727 | return; |
bborredon | 0:80245aff63ce | 728 | } |
bborredon | 0:80245aff63ce | 729 | |
bborredon | 0:80245aff63ce | 730 | read(address,cmd,size); |
bborredon | 0:80245aff63ce | 731 | |
bborredon | 0:80245aff63ce | 732 | memcpy(data,cmd,size); |
bborredon | 0:80245aff63ce | 733 | |
bborredon | 0:80245aff63ce | 734 | free(cmd); |
bborredon | 0:80245aff63ce | 735 | |
bborredon | 0:80245aff63ce | 736 | } |
bborredon | 0:80245aff63ce | 737 | |
bborredon | 0:80245aff63ce | 738 | void EEPROM::ready(void) |
bborredon | 0:80245aff63ce | 739 | { |
bborredon | 0:80245aff63ce | 740 | int ack; |
bborredon | 0:80245aff63ce | 741 | uint8_t addr; |
bborredon | 0:80245aff63ce | 742 | uint8_t cmd; |
bborredon | 0:80245aff63ce | 743 | |
bborredon | 0:80245aff63ce | 744 | // Check error |
bborredon | 0:80245aff63ce | 745 | if(_errnum) |
bborredon | 0:80245aff63ce | 746 | return; |
bborredon | 0:80245aff63ce | 747 | |
bborredon | 0:80245aff63ce | 748 | // Device address |
bborredon | 0:80245aff63ce | 749 | addr = EEPROM_Address | _address; |
bborredon | 0:80245aff63ce | 750 | |
bborredon | 0:80245aff63ce | 751 | cmd = 0; |
soulx | 2:c648c5e93d5e | 752 | /* |
bborredon | 0:80245aff63ce | 753 | // Wait end of write |
bborredon | 0:80245aff63ce | 754 | do { |
bborredon | 0:80245aff63ce | 755 | ack = _i2c.write((int)addr,(char *)cmd,0); |
bborredon | 0:80245aff63ce | 756 | } while(ack != 0); |
soulx | 2:c648c5e93d5e | 757 | */ |
soulx | 2:c648c5e93d5e | 758 | wait_ms(5); |
bborredon | 0:80245aff63ce | 759 | } |
bborredon | 0:80245aff63ce | 760 | |
bborredon | 0:80245aff63ce | 761 | uint32_t EEPROM::getSize(void) |
bborredon | 0:80245aff63ce | 762 | { |
bborredon | 0:80245aff63ce | 763 | return(_size); |
bborredon | 0:80245aff63ce | 764 | } |
bborredon | 0:80245aff63ce | 765 | |
bborredon | 0:80245aff63ce | 766 | uint8_t EEPROM::getError(void) |
bborredon | 0:80245aff63ce | 767 | { |
bborredon | 0:80245aff63ce | 768 | return(_errnum); |
bborredon | 0:80245aff63ce | 769 | } |
bborredon | 0:80245aff63ce | 770 | |
bborredon | 0:80245aff63ce | 771 | bool EEPROM::checkAddress(uint16_t address) |
bborredon | 0:80245aff63ce | 772 | { |
bborredon | 0:80245aff63ce | 773 | bool ret = true; |
bborredon | 0:80245aff63ce | 774 | |
bborredon | 0:80245aff63ce | 775 | switch(_type) { |
bborredon | 0:80245aff63ce | 776 | case T24C01 : |
bborredon | 0:80245aff63ce | 777 | if(address >= T24C01) |
bborredon | 0:80245aff63ce | 778 | ret = false; |
bborredon | 0:80245aff63ce | 779 | break; |
bborredon | 0:80245aff63ce | 780 | case T24C02 : |
bborredon | 0:80245aff63ce | 781 | if(address >= T24C02) |
bborredon | 0:80245aff63ce | 782 | ret = false; |
bborredon | 0:80245aff63ce | 783 | break; |
bborredon | 0:80245aff63ce | 784 | case T24C04 : |
bborredon | 0:80245aff63ce | 785 | if(address >= T24C04) |
bborredon | 0:80245aff63ce | 786 | ret = false; |
bborredon | 0:80245aff63ce | 787 | break; |
bborredon | 0:80245aff63ce | 788 | case T24C08 : |
bborredon | 0:80245aff63ce | 789 | if(address >= T24C08) |
bborredon | 0:80245aff63ce | 790 | ret = false; |
bborredon | 0:80245aff63ce | 791 | break; |
bborredon | 0:80245aff63ce | 792 | case T24C16 : |
bborredon | 0:80245aff63ce | 793 | if(address >= T24C16) |
bborredon | 0:80245aff63ce | 794 | ret = false; |
bborredon | 0:80245aff63ce | 795 | break; |
bborredon | 0:80245aff63ce | 796 | case T24C32 : |
bborredon | 0:80245aff63ce | 797 | if(address >= T24C32) |
bborredon | 0:80245aff63ce | 798 | ret = false; |
bborredon | 0:80245aff63ce | 799 | break; |
bborredon | 0:80245aff63ce | 800 | case T24C64 : |
bborredon | 0:80245aff63ce | 801 | if(address >= T24C64) |
bborredon | 0:80245aff63ce | 802 | ret = false; |
bborredon | 0:80245aff63ce | 803 | break; |
bborredon | 0:80245aff63ce | 804 | case T24C128 : |
bborredon | 0:80245aff63ce | 805 | if(address >= T24C128) |
bborredon | 0:80245aff63ce | 806 | ret = false; |
bborredon | 0:80245aff63ce | 807 | break; |
bborredon | 0:80245aff63ce | 808 | case T24C256 : |
bborredon | 0:80245aff63ce | 809 | if(address >= T24C256) |
bborredon | 0:80245aff63ce | 810 | ret = false; |
bborredon | 0:80245aff63ce | 811 | break; |
bborredon | 0:80245aff63ce | 812 | case T24C512 : |
bborredon | 0:80245aff63ce | 813 | if(address >= T24C512) |
bborredon | 0:80245aff63ce | 814 | ret = false; |
bborredon | 0:80245aff63ce | 815 | break; |
bborredon | 0:80245aff63ce | 816 | case T24C1024 : |
bborredon | 0:80245aff63ce | 817 | if(address >= T24C1024) |
bborredon | 0:80245aff63ce | 818 | ret = false; |
bborredon | 0:80245aff63ce | 819 | break; |
bborredon | 0:80245aff63ce | 820 | case T24C1025 : |
bborredon | 0:80245aff63ce | 821 | if(address >= T24C1025 - 1) |
bborredon | 0:80245aff63ce | 822 | ret = false; |
bborredon | 0:80245aff63ce | 823 | break; |
bborredon | 0:80245aff63ce | 824 | } |
bborredon | 0:80245aff63ce | 825 | |
bborredon | 0:80245aff63ce | 826 | return(ret); |
bborredon | 0:80245aff63ce | 827 | } |