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