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