1Mbit EEPROMを便利に使うためのライブラリ

Dependents:   EEPROM_Reset_Program EEPROM_Encode_Program SWAN_IZU2019_v1

Committer:
Sigma884
Date:
Mon Feb 11 15:41:56 2019 +0000
Revision:
0:a945081a4a9e
Child:
1:66c2c82f4b34
EEPROM library alpha

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sigma884 0:a945081a4a9e 1 #include "mbed.h"
Sigma884 0:a945081a4a9e 2 #include "EEPROM_lib.h"
Sigma884 0:a945081a4a9e 3
Sigma884 0:a945081a4a9e 4 EEPROM_lib::EEPROM_lib(I2C &user_i2c, int num){
Sigma884 0:a945081a4a9e 5 i2c = &user_i2c;
Sigma884 0:a945081a4a9e 6 i2c -> frequency(400000);
Sigma884 0:a945081a4a9e 7
Sigma884 0:a945081a4a9e 8 //max_ptr = 1024 * 1024 * num - 1;
Sigma884 0:a945081a4a9e 9 max_num = num;
Sigma884 0:a945081a4a9e 10 buff_ptr = 0;
Sigma884 0:a945081a4a9e 11 //eeprom_ptr = 0;
Sigma884 0:a945081a4a9e 12 ptr[3] = ptr[2] = ptr[1] = ptr[0] = 0x00;
Sigma884 0:a945081a4a9e 13 }
Sigma884 0:a945081a4a9e 14
Sigma884 0:a945081a4a9e 15 void EEPROM_lib::writeByte(int num, int block, char ADDR_H, char ADDR_L, char DATA){
Sigma884 0:a945081a4a9e 16 if(num > max_num){
Sigma884 0:a945081a4a9e 17 return;
Sigma884 0:a945081a4a9e 18 }
Sigma884 0:a945081a4a9e 19 switch(num){
Sigma884 0:a945081a4a9e 20 case 1:
Sigma884 0:a945081a4a9e 21 slave = EEPROM_ADDR_00_0;
Sigma884 0:a945081a4a9e 22 break;
Sigma884 0:a945081a4a9e 23
Sigma884 0:a945081a4a9e 24 case 2:
Sigma884 0:a945081a4a9e 25 slave = EEPROM_ADDR_01_0;
Sigma884 0:a945081a4a9e 26 break;
Sigma884 0:a945081a4a9e 27
Sigma884 0:a945081a4a9e 28 case 3:
Sigma884 0:a945081a4a9e 29 slave = EEPROM_ADDR_10_0;
Sigma884 0:a945081a4a9e 30 break;
Sigma884 0:a945081a4a9e 31
Sigma884 0:a945081a4a9e 32 case 4:
Sigma884 0:a945081a4a9e 33 slave = EEPROM_ADDR_11_0;
Sigma884 0:a945081a4a9e 34 break;
Sigma884 0:a945081a4a9e 35 }
Sigma884 0:a945081a4a9e 36 if(block == 1){
Sigma884 0:a945081a4a9e 37 slave = slave | 0x08;
Sigma884 0:a945081a4a9e 38 }
Sigma884 0:a945081a4a9e 39 cmd[0] = ADDR_H;
Sigma884 0:a945081a4a9e 40 cmd[1] = ADDR_L;
Sigma884 0:a945081a4a9e 41 cmd[2] = DATA;
Sigma884 0:a945081a4a9e 42
Sigma884 0:a945081a4a9e 43 i2c -> write(slave, cmd, 3);
Sigma884 0:a945081a4a9e 44 wait_us(EEPROM_WAIT_US);
Sigma884 0:a945081a4a9e 45 }
Sigma884 0:a945081a4a9e 46
Sigma884 0:a945081a4a9e 47 void EEPROM_lib::writePage(int num, int block, char ADDR_H, char ADDR_L, char *DATA){
Sigma884 0:a945081a4a9e 48 if(num > max_num){
Sigma884 0:a945081a4a9e 49 return;
Sigma884 0:a945081a4a9e 50 }
Sigma884 0:a945081a4a9e 51 switch(num){
Sigma884 0:a945081a4a9e 52 case 1:
Sigma884 0:a945081a4a9e 53 slave = EEPROM_ADDR_00_0;
Sigma884 0:a945081a4a9e 54 break;
Sigma884 0:a945081a4a9e 55
Sigma884 0:a945081a4a9e 56 case 2:
Sigma884 0:a945081a4a9e 57 slave = EEPROM_ADDR_01_0;
Sigma884 0:a945081a4a9e 58 break;
Sigma884 0:a945081a4a9e 59
Sigma884 0:a945081a4a9e 60 case 3:
Sigma884 0:a945081a4a9e 61 slave = EEPROM_ADDR_10_0;
Sigma884 0:a945081a4a9e 62 break;
Sigma884 0:a945081a4a9e 63
Sigma884 0:a945081a4a9e 64 case 4:
Sigma884 0:a945081a4a9e 65 slave = EEPROM_ADDR_11_0;
Sigma884 0:a945081a4a9e 66 break;
Sigma884 0:a945081a4a9e 67 }
Sigma884 0:a945081a4a9e 68 if(block == 1){
Sigma884 0:a945081a4a9e 69 slave = slave | 0x08;
Sigma884 0:a945081a4a9e 70 }
Sigma884 0:a945081a4a9e 71 cmd[0] = ADDR_H;
Sigma884 0:a945081a4a9e 72 cmd[1] = ADDR_L;
Sigma884 0:a945081a4a9e 73 for(int i = 2; i < 130; i ++){
Sigma884 0:a945081a4a9e 74 cmd[i] = DATA[i-2];
Sigma884 0:a945081a4a9e 75 }
Sigma884 0:a945081a4a9e 76
Sigma884 0:a945081a4a9e 77 i2c -> write(slave, cmd, 130);
Sigma884 0:a945081a4a9e 78 wait_us(EEPROM_WAIT_US);
Sigma884 0:a945081a4a9e 79 }
Sigma884 0:a945081a4a9e 80
Sigma884 0:a945081a4a9e 81 void EEPROM_lib::setWriteAddr(int num, int block, char ADDR_H, char ADDR_L){
Sigma884 0:a945081a4a9e 82 if(num > max_num){
Sigma884 0:a945081a4a9e 83 return;
Sigma884 0:a945081a4a9e 84 }
Sigma884 0:a945081a4a9e 85 switch(num){
Sigma884 0:a945081a4a9e 86 case 1:
Sigma884 0:a945081a4a9e 87 slave = EEPROM_ADDR_00_0;
Sigma884 0:a945081a4a9e 88 break;
Sigma884 0:a945081a4a9e 89
Sigma884 0:a945081a4a9e 90 case 2:
Sigma884 0:a945081a4a9e 91 slave = EEPROM_ADDR_01_0;
Sigma884 0:a945081a4a9e 92 break;
Sigma884 0:a945081a4a9e 93
Sigma884 0:a945081a4a9e 94 case 3:
Sigma884 0:a945081a4a9e 95 slave = EEPROM_ADDR_10_0;
Sigma884 0:a945081a4a9e 96 break;
Sigma884 0:a945081a4a9e 97
Sigma884 0:a945081a4a9e 98 case 4:
Sigma884 0:a945081a4a9e 99 slave = EEPROM_ADDR_11_0;
Sigma884 0:a945081a4a9e 100 break;
Sigma884 0:a945081a4a9e 101 }
Sigma884 0:a945081a4a9e 102 if(block == 1){
Sigma884 0:a945081a4a9e 103 slave = slave | 0x08;
Sigma884 0:a945081a4a9e 104 }
Sigma884 0:a945081a4a9e 105 cmd[0] = ADDR_H;
Sigma884 0:a945081a4a9e 106 cmd[1] = ADDR_L;
Sigma884 0:a945081a4a9e 107 }
Sigma884 0:a945081a4a9e 108
Sigma884 0:a945081a4a9e 109 int EEPROM_lib::chargeBuff(char DATA){
Sigma884 0:a945081a4a9e 110 //if(eeprom_ptr < max_ptr && buff_ptr < 128){
Sigma884 0:a945081a4a9e 111 if(buff_ptr < 128){
Sigma884 0:a945081a4a9e 112 EEPROM_cast.c = DATA;
Sigma884 0:a945081a4a9e 113 cmd[buff_ptr + 2] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 114
Sigma884 0:a945081a4a9e 115 buff_ptr ++;
Sigma884 0:a945081a4a9e 116 //eeprom_ptr ++;
Sigma884 0:a945081a4a9e 117 }
Sigma884 0:a945081a4a9e 118 return buff_ptr;
Sigma884 0:a945081a4a9e 119 }
Sigma884 0:a945081a4a9e 120
Sigma884 0:a945081a4a9e 121 int EEPROM_lib::chargeBuff(bool DATA){
Sigma884 0:a945081a4a9e 122 //if(eeprom_ptr < max_ptr && buff_ptr < 128){
Sigma884 0:a945081a4a9e 123 if(buff_ptr < 128){
Sigma884 0:a945081a4a9e 124 EEPROM_cast.b = DATA;
Sigma884 0:a945081a4a9e 125 cmd[buff_ptr + 2] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 126
Sigma884 0:a945081a4a9e 127 buff_ptr ++;
Sigma884 0:a945081a4a9e 128 //eeprom_ptr ++;
Sigma884 0:a945081a4a9e 129 }
Sigma884 0:a945081a4a9e 130 return buff_ptr;
Sigma884 0:a945081a4a9e 131 }
Sigma884 0:a945081a4a9e 132
Sigma884 0:a945081a4a9e 133 int EEPROM_lib::chargeBuff(int DATA){
Sigma884 0:a945081a4a9e 134 //if(eeprom_ptr < max_ptr - 3 && buff_ptr < 128 - 3){
Sigma884 0:a945081a4a9e 135 if(buff_ptr < 128 - 3){
Sigma884 0:a945081a4a9e 136 EEPROM_cast.i = DATA;
Sigma884 0:a945081a4a9e 137 cmd[buff_ptr + 2] = EEPROM_cast.cast[3];
Sigma884 0:a945081a4a9e 138 cmd[buff_ptr + 3] = EEPROM_cast.cast[2];
Sigma884 0:a945081a4a9e 139 cmd[buff_ptr + 4] = EEPROM_cast.cast[1];
Sigma884 0:a945081a4a9e 140 cmd[buff_ptr + 5] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 141
Sigma884 0:a945081a4a9e 142 buff_ptr += 4;
Sigma884 0:a945081a4a9e 143 //eeprom_ptr += 4;
Sigma884 0:a945081a4a9e 144 }
Sigma884 0:a945081a4a9e 145 return buff_ptr;
Sigma884 0:a945081a4a9e 146 }
Sigma884 0:a945081a4a9e 147
Sigma884 0:a945081a4a9e 148 int EEPROM_lib::chargeBuff(short DATA){
Sigma884 0:a945081a4a9e 149 //if(eeprom_ptr < max_ptr - 1 && buff_ptr < 128 - 1){
Sigma884 0:a945081a4a9e 150 if(buff_ptr < 128 - 1){
Sigma884 0:a945081a4a9e 151 EEPROM_cast.s = DATA;
Sigma884 0:a945081a4a9e 152 cmd[buff_ptr + 2] = EEPROM_cast.cast[1];
Sigma884 0:a945081a4a9e 153 cmd[buff_ptr + 3] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 154
Sigma884 0:a945081a4a9e 155 buff_ptr += 2;
Sigma884 0:a945081a4a9e 156 //eeprom_ptr += 2;
Sigma884 0:a945081a4a9e 157 }
Sigma884 0:a945081a4a9e 158 return buff_ptr;
Sigma884 0:a945081a4a9e 159 }
Sigma884 0:a945081a4a9e 160
Sigma884 0:a945081a4a9e 161 int EEPROM_lib::chargeBuff(long long DATA){
Sigma884 0:a945081a4a9e 162 //if(eeprom_ptr < max_ptr - 7 && buff_ptr < 128 - 7){
Sigma884 0:a945081a4a9e 163 if(buff_ptr < 128 - 7){
Sigma884 0:a945081a4a9e 164 EEPROM_cast.ll = DATA;
Sigma884 0:a945081a4a9e 165 cmd[buff_ptr + 2] = EEPROM_cast.cast[7];
Sigma884 0:a945081a4a9e 166 cmd[buff_ptr + 3] = EEPROM_cast.cast[6];
Sigma884 0:a945081a4a9e 167 cmd[buff_ptr + 4] = EEPROM_cast.cast[5];
Sigma884 0:a945081a4a9e 168 cmd[buff_ptr + 5] = EEPROM_cast.cast[4];
Sigma884 0:a945081a4a9e 169 cmd[buff_ptr + 6] = EEPROM_cast.cast[3];
Sigma884 0:a945081a4a9e 170 cmd[buff_ptr + 7] = EEPROM_cast.cast[2];
Sigma884 0:a945081a4a9e 171 cmd[buff_ptr + 8] = EEPROM_cast.cast[1];
Sigma884 0:a945081a4a9e 172 cmd[buff_ptr + 9] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 173
Sigma884 0:a945081a4a9e 174 buff_ptr += 8;
Sigma884 0:a945081a4a9e 175 //eeprom_ptr += 8;
Sigma884 0:a945081a4a9e 176 }
Sigma884 0:a945081a4a9e 177 return buff_ptr;
Sigma884 0:a945081a4a9e 178 }
Sigma884 0:a945081a4a9e 179
Sigma884 0:a945081a4a9e 180 int EEPROM_lib::chargeBuff(float DATA){
Sigma884 0:a945081a4a9e 181 //if(eeprom_ptr < max_ptr - 3 && buff_ptr < 128 - 3){
Sigma884 0:a945081a4a9e 182 if(buff_ptr < 128 - 3){
Sigma884 0:a945081a4a9e 183 EEPROM_cast.f = DATA;
Sigma884 0:a945081a4a9e 184 cmd[buff_ptr + 2] = EEPROM_cast.cast[3];
Sigma884 0:a945081a4a9e 185 cmd[buff_ptr + 3] = EEPROM_cast.cast[2];
Sigma884 0:a945081a4a9e 186 cmd[buff_ptr + 4] = EEPROM_cast.cast[1];
Sigma884 0:a945081a4a9e 187 cmd[buff_ptr + 5] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 188
Sigma884 0:a945081a4a9e 189 buff_ptr += 4;
Sigma884 0:a945081a4a9e 190 //eeprom_ptr += 4;
Sigma884 0:a945081a4a9e 191 }
Sigma884 0:a945081a4a9e 192 return buff_ptr;
Sigma884 0:a945081a4a9e 193 }
Sigma884 0:a945081a4a9e 194
Sigma884 0:a945081a4a9e 195 int EEPROM_lib::chargeBuff(double DATA){
Sigma884 0:a945081a4a9e 196 //if(eeprom_ptr < max_ptr - 7 && buff_ptr < 128 - 7){
Sigma884 0:a945081a4a9e 197 if(buff_ptr < 128 - 7){
Sigma884 0:a945081a4a9e 198 EEPROM_cast.d = DATA;
Sigma884 0:a945081a4a9e 199 cmd[buff_ptr + 2] = EEPROM_cast.cast[7];
Sigma884 0:a945081a4a9e 200 cmd[buff_ptr + 3] = EEPROM_cast.cast[6];
Sigma884 0:a945081a4a9e 201 cmd[buff_ptr + 4] = EEPROM_cast.cast[5];
Sigma884 0:a945081a4a9e 202 cmd[buff_ptr + 5] = EEPROM_cast.cast[4];
Sigma884 0:a945081a4a9e 203 cmd[buff_ptr + 6] = EEPROM_cast.cast[3];
Sigma884 0:a945081a4a9e 204 cmd[buff_ptr + 7] = EEPROM_cast.cast[2];
Sigma884 0:a945081a4a9e 205 cmd[buff_ptr + 8] = EEPROM_cast.cast[1];
Sigma884 0:a945081a4a9e 206 cmd[buff_ptr + 9] = EEPROM_cast.cast[0];
Sigma884 0:a945081a4a9e 207
Sigma884 0:a945081a4a9e 208 buff_ptr += 8;
Sigma884 0:a945081a4a9e 209 //eeprom_ptr += 8;
Sigma884 0:a945081a4a9e 210 }
Sigma884 0:a945081a4a9e 211 return buff_ptr;
Sigma884 0:a945081a4a9e 212 }
Sigma884 0:a945081a4a9e 213
Sigma884 0:a945081a4a9e 214 void EEPROM_lib::writeBuff(){
Sigma884 0:a945081a4a9e 215 for(int i = buff_ptr+2; i < 130; i ++){
Sigma884 0:a945081a4a9e 216 cmd[i] = 0x00;
Sigma884 0:a945081a4a9e 217 }
Sigma884 0:a945081a4a9e 218 i2c -> write(slave, cmd, 130);
Sigma884 0:a945081a4a9e 219 wait_us(EEPROM_WAIT_US);
Sigma884 0:a945081a4a9e 220
Sigma884 0:a945081a4a9e 221 buff_ptr = 0;
Sigma884 0:a945081a4a9e 222 }
Sigma884 0:a945081a4a9e 223
Sigma884 0:a945081a4a9e 224 int EEPROM_lib::setNextPage(){
Sigma884 0:a945081a4a9e 225 if(cmd[1] == 0x00){
Sigma884 0:a945081a4a9e 226 cmd[1] = 0x80;
Sigma884 0:a945081a4a9e 227 ptr[3] = cmd[1];
Sigma884 0:a945081a4a9e 228 }
Sigma884 0:a945081a4a9e 229 else{
Sigma884 0:a945081a4a9e 230 cmd[1] = 0x00;
Sigma884 0:a945081a4a9e 231 ptr[3] = cmd[1];
Sigma884 0:a945081a4a9e 232 if(cmd[0] == 0xFF){
Sigma884 0:a945081a4a9e 233 cmd[0] = 0x00;
Sigma884 0:a945081a4a9e 234 ptr[2] = cmd[0];
Sigma884 0:a945081a4a9e 235 switch(slave){
Sigma884 0:a945081a4a9e 236 case EEPROM_ADDR_00_0:
Sigma884 0:a945081a4a9e 237 slave = EEPROM_ADDR_00_1;
Sigma884 0:a945081a4a9e 238 ptr[0] = 0x00;
Sigma884 0:a945081a4a9e 239 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 240 break;
Sigma884 0:a945081a4a9e 241
Sigma884 0:a945081a4a9e 242 case EEPROM_ADDR_00_1:
Sigma884 0:a945081a4a9e 243 slave = EEPROM_ADDR_01_0;
Sigma884 0:a945081a4a9e 244 ptr[0] = 0x01;
Sigma884 0:a945081a4a9e 245 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 246 break;
Sigma884 0:a945081a4a9e 247
Sigma884 0:a945081a4a9e 248 case EEPROM_ADDR_01_0:
Sigma884 0:a945081a4a9e 249 slave = EEPROM_ADDR_01_1;
Sigma884 0:a945081a4a9e 250 ptr[0] = 0x01;
Sigma884 0:a945081a4a9e 251 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 252 break;
Sigma884 0:a945081a4a9e 253
Sigma884 0:a945081a4a9e 254 case EEPROM_ADDR_01_1:
Sigma884 0:a945081a4a9e 255 slave = EEPROM_ADDR_10_0;
Sigma884 0:a945081a4a9e 256 ptr[0] = 0x02;
Sigma884 0:a945081a4a9e 257 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 258 break;
Sigma884 0:a945081a4a9e 259
Sigma884 0:a945081a4a9e 260 case EEPROM_ADDR_10_0:
Sigma884 0:a945081a4a9e 261 slave = EEPROM_ADDR_10_1;
Sigma884 0:a945081a4a9e 262 ptr[0] = 0x02;
Sigma884 0:a945081a4a9e 263 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 264 break;
Sigma884 0:a945081a4a9e 265
Sigma884 0:a945081a4a9e 266 case EEPROM_ADDR_10_1:
Sigma884 0:a945081a4a9e 267 slave = EEPROM_ADDR_11_0;
Sigma884 0:a945081a4a9e 268 ptr[0] = 0x03;
Sigma884 0:a945081a4a9e 269 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 270 break;
Sigma884 0:a945081a4a9e 271
Sigma884 0:a945081a4a9e 272 case EEPROM_ADDR_11_0:
Sigma884 0:a945081a4a9e 273 slave = EEPROM_ADDR_11_1;
Sigma884 0:a945081a4a9e 274 ptr[0] = 0x03;
Sigma884 0:a945081a4a9e 275 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 276 break;
Sigma884 0:a945081a4a9e 277
Sigma884 0:a945081a4a9e 278 case EEPROM_ADDR_11_1:
Sigma884 0:a945081a4a9e 279 slave = 0x00;
Sigma884 0:a945081a4a9e 280 ptr[0] = 0x04;
Sigma884 0:a945081a4a9e 281 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 282 }
Sigma884 0:a945081a4a9e 283 }
Sigma884 0:a945081a4a9e 284 else{
Sigma884 0:a945081a4a9e 285 cmd[0] ++;
Sigma884 0:a945081a4a9e 286 ptr[2] = cmd[0];
Sigma884 0:a945081a4a9e 287 switch(slave){
Sigma884 0:a945081a4a9e 288 case EEPROM_ADDR_00_0:
Sigma884 0:a945081a4a9e 289 ptr[0] = 0x00;
Sigma884 0:a945081a4a9e 290 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 291 break;
Sigma884 0:a945081a4a9e 292
Sigma884 0:a945081a4a9e 293 case EEPROM_ADDR_00_1:
Sigma884 0:a945081a4a9e 294 ptr[0] = 0x00;
Sigma884 0:a945081a4a9e 295 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 296 break;
Sigma884 0:a945081a4a9e 297
Sigma884 0:a945081a4a9e 298 case EEPROM_ADDR_01_0:
Sigma884 0:a945081a4a9e 299 ptr[0] = 0x01;
Sigma884 0:a945081a4a9e 300 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 301 break;
Sigma884 0:a945081a4a9e 302
Sigma884 0:a945081a4a9e 303 case EEPROM_ADDR_01_1:
Sigma884 0:a945081a4a9e 304 ptr[0] = 0x01;
Sigma884 0:a945081a4a9e 305 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 306 break;
Sigma884 0:a945081a4a9e 307
Sigma884 0:a945081a4a9e 308 case EEPROM_ADDR_10_0:
Sigma884 0:a945081a4a9e 309 ptr[0] = 0x02;
Sigma884 0:a945081a4a9e 310 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 311 break;
Sigma884 0:a945081a4a9e 312
Sigma884 0:a945081a4a9e 313 case EEPROM_ADDR_10_1:
Sigma884 0:a945081a4a9e 314 ptr[0] = 0x02;
Sigma884 0:a945081a4a9e 315 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 316 break;
Sigma884 0:a945081a4a9e 317
Sigma884 0:a945081a4a9e 318 case EEPROM_ADDR_11_0:
Sigma884 0:a945081a4a9e 319 ptr[0] = 0x03;
Sigma884 0:a945081a4a9e 320 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 321 break;
Sigma884 0:a945081a4a9e 322
Sigma884 0:a945081a4a9e 323 case EEPROM_ADDR_11_1:
Sigma884 0:a945081a4a9e 324 ptr[0] = 0x03;
Sigma884 0:a945081a4a9e 325 ptr[1] = 0x01;
Sigma884 0:a945081a4a9e 326 break;
Sigma884 0:a945081a4a9e 327 }
Sigma884 0:a945081a4a9e 328 }
Sigma884 0:a945081a4a9e 329 }
Sigma884 0:a945081a4a9e 330
Sigma884 0:a945081a4a9e 331 switch(max_num){
Sigma884 0:a945081a4a9e 332 case 1:
Sigma884 0:a945081a4a9e 333 if(ptr[0] > 0x00){
Sigma884 0:a945081a4a9e 334 slave = 0x00;
Sigma884 0:a945081a4a9e 335 cmd[0] = 0x00;
Sigma884 0:a945081a4a9e 336 cmd[1] = 0x00;
Sigma884 0:a945081a4a9e 337 ptr[0] = 0x01;
Sigma884 0:a945081a4a9e 338 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 339 ptr[2] = 0x00;
Sigma884 0:a945081a4a9e 340 ptr[3] = 0x00;
Sigma884 0:a945081a4a9e 341 }
Sigma884 0:a945081a4a9e 342 break;
Sigma884 0:a945081a4a9e 343
Sigma884 0:a945081a4a9e 344 case 2:
Sigma884 0:a945081a4a9e 345 if(ptr[0] > 0x01){
Sigma884 0:a945081a4a9e 346 slave = 0x00;
Sigma884 0:a945081a4a9e 347 cmd[0] = 0x00;
Sigma884 0:a945081a4a9e 348 cmd[1] = 0x00;
Sigma884 0:a945081a4a9e 349 ptr[0] = 0x02;
Sigma884 0:a945081a4a9e 350 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 351 ptr[2] = 0x00;
Sigma884 0:a945081a4a9e 352 ptr[3] = 0x00;
Sigma884 0:a945081a4a9e 353 }
Sigma884 0:a945081a4a9e 354 break;
Sigma884 0:a945081a4a9e 355
Sigma884 0:a945081a4a9e 356 case 3:
Sigma884 0:a945081a4a9e 357 if(ptr[0] > 0x02){
Sigma884 0:a945081a4a9e 358 slave = 0x00;
Sigma884 0:a945081a4a9e 359 cmd[0] = 0x00;
Sigma884 0:a945081a4a9e 360 cmd[1] = 0x00;
Sigma884 0:a945081a4a9e 361 ptr[0] = 0x03;
Sigma884 0:a945081a4a9e 362 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 363 ptr[2] = 0x00;
Sigma884 0:a945081a4a9e 364 ptr[3] = 0x00;
Sigma884 0:a945081a4a9e 365 }
Sigma884 0:a945081a4a9e 366 break;
Sigma884 0:a945081a4a9e 367
Sigma884 0:a945081a4a9e 368 case 4:
Sigma884 0:a945081a4a9e 369 if(ptr[0] > 0x03){
Sigma884 0:a945081a4a9e 370 slave = 0x00;
Sigma884 0:a945081a4a9e 371 cmd[0] = 0x00;
Sigma884 0:a945081a4a9e 372 cmd[1] = 0x00;
Sigma884 0:a945081a4a9e 373 ptr[0] = 0x04;
Sigma884 0:a945081a4a9e 374 ptr[1] = 0x00;
Sigma884 0:a945081a4a9e 375 ptr[2] = 0x00;
Sigma884 0:a945081a4a9e 376 ptr[3] = 0x00;
Sigma884 0:a945081a4a9e 377 }
Sigma884 0:a945081a4a9e 378 break;
Sigma884 0:a945081a4a9e 379 }
Sigma884 0:a945081a4a9e 380
Sigma884 0:a945081a4a9e 381 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
Sigma884 0:a945081a4a9e 382 }
Sigma884 0:a945081a4a9e 383
Sigma884 0:a945081a4a9e 384 char EEPROM_lib::readByte(int num, int block, char ADDR_H, char ADDR_L){
Sigma884 0:a945081a4a9e 385 if(num > max_num){
Sigma884 0:a945081a4a9e 386 return 0x00;
Sigma884 0:a945081a4a9e 387 }
Sigma884 0:a945081a4a9e 388 switch(num){
Sigma884 0:a945081a4a9e 389 case 1:
Sigma884 0:a945081a4a9e 390 slave = EEPROM_ADDR_00_0;
Sigma884 0:a945081a4a9e 391 break;
Sigma884 0:a945081a4a9e 392
Sigma884 0:a945081a4a9e 393 case 2:
Sigma884 0:a945081a4a9e 394 slave = EEPROM_ADDR_01_0;
Sigma884 0:a945081a4a9e 395 break;
Sigma884 0:a945081a4a9e 396
Sigma884 0:a945081a4a9e 397 case 3:
Sigma884 0:a945081a4a9e 398 slave = EEPROM_ADDR_10_0;
Sigma884 0:a945081a4a9e 399 break;
Sigma884 0:a945081a4a9e 400
Sigma884 0:a945081a4a9e 401 case 4:
Sigma884 0:a945081a4a9e 402 slave = EEPROM_ADDR_11_0;
Sigma884 0:a945081a4a9e 403 break;
Sigma884 0:a945081a4a9e 404 }
Sigma884 0:a945081a4a9e 405 if(block == 1){
Sigma884 0:a945081a4a9e 406 slave = slave | 0x08;
Sigma884 0:a945081a4a9e 407 }
Sigma884 0:a945081a4a9e 408 cmd[0] = ADDR_H;
Sigma884 0:a945081a4a9e 409 cmd[1] = ADDR_L;
Sigma884 0:a945081a4a9e 410
Sigma884 0:a945081a4a9e 411 i2c -> write(slave, cmd, 2);
Sigma884 0:a945081a4a9e 412 i2c -> read(slave | 1, cmd, 1);
Sigma884 0:a945081a4a9e 413
Sigma884 0:a945081a4a9e 414 return cmd[0];
Sigma884 0:a945081a4a9e 415 }
Sigma884 0:a945081a4a9e 416
Sigma884 0:a945081a4a9e 417 void EEPROM_lib::readMultiByte(int num, int block, char ADDR_H, char ADDR_L, char *DATA, int size){
Sigma884 0:a945081a4a9e 418 if(num > max_num){
Sigma884 0:a945081a4a9e 419 return;
Sigma884 0:a945081a4a9e 420 }
Sigma884 0:a945081a4a9e 421 switch(num){
Sigma884 0:a945081a4a9e 422 case 1:
Sigma884 0:a945081a4a9e 423 slave = EEPROM_ADDR_00_0;
Sigma884 0:a945081a4a9e 424 break;
Sigma884 0:a945081a4a9e 425
Sigma884 0:a945081a4a9e 426 case 2:
Sigma884 0:a945081a4a9e 427 slave = EEPROM_ADDR_01_0;
Sigma884 0:a945081a4a9e 428 break;
Sigma884 0:a945081a4a9e 429
Sigma884 0:a945081a4a9e 430 case 3:
Sigma884 0:a945081a4a9e 431 slave = EEPROM_ADDR_10_0;
Sigma884 0:a945081a4a9e 432 break;
Sigma884 0:a945081a4a9e 433
Sigma884 0:a945081a4a9e 434 case 4:
Sigma884 0:a945081a4a9e 435 slave = EEPROM_ADDR_11_0;
Sigma884 0:a945081a4a9e 436 break;
Sigma884 0:a945081a4a9e 437 }
Sigma884 0:a945081a4a9e 438 if(block == 1){
Sigma884 0:a945081a4a9e 439 slave = slave | 0x08;
Sigma884 0:a945081a4a9e 440 }
Sigma884 0:a945081a4a9e 441 cmd[0] = ADDR_H;
Sigma884 0:a945081a4a9e 442 cmd[1] = ADDR_L;
Sigma884 0:a945081a4a9e 443
Sigma884 0:a945081a4a9e 444 i2c -> write(slave, cmd, 2);
Sigma884 0:a945081a4a9e 445 i2c -> read(slave | 1, DATA, size);
Sigma884 0:a945081a4a9e 446 }