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