| 1 | #include <errno.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | #include <stdio.h> |
|---|
| 4 | #include <stdlib.h> |
|---|
| 5 | #include <unistd.h> |
|---|
| 6 | #include <fcntl.h> |
|---|
| 7 | #include <time.h> |
|---|
| 8 | #include "i2c-dev.h" |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | #define MAX_BLK_SIZE 64 |
|---|
| 12 | #define EEPROM_SIZE 32768 |
|---|
| 13 | #define READ 1 |
|---|
| 14 | #define WRITE 0 |
|---|
| 15 | #define ERASE 2 |
|---|
| 16 | #define PHEADER 3 |
|---|
| 17 | #define VER "eepromer v 0.4 (c) Daniel Smolik 2001\n" |
|---|
| 18 | #define HEAD_SIZE sizeof(struct mini_inode) |
|---|
| 19 | #define START_ADDR 0 |
|---|
| 20 | #define FORCE 1 |
|---|
| 21 | /* |
|---|
| 22 | To disable startup warning #undef WARNINC |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | #define WARNINC |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | int block_write(int file,int dev_addr,int eeprom_addr,unsigned char *buf,int lenght);int block_write(int file,int dev_addr,int eeprom_addr,unsigned char *buf,int lenght); |
|---|
| 31 | int block_read(int file,int dev_addr,int eeprom_addr,unsigned char *buf); |
|---|
| 32 | |
|---|
| 33 | /* block_read read block 64 bytes length and returns actual length of data*/ |
|---|
| 34 | void help(void); |
|---|
| 35 | int init(char *device,int addr); |
|---|
| 36 | int content_write(int file, int addr); |
|---|
| 37 | int content_read(int file, int addr); |
|---|
| 38 | int inode_write(int file, int dev_addr, int lenght); |
|---|
| 39 | int inode_read(int file, int dev_addr, void *p_inode); |
|---|
| 40 | void pheader(int file, int addr); |
|---|
| 41 | void erase(int file,int addr,int eeprom_size); |
|---|
| 42 | void made_address(int addr,unsigned char *buf); |
|---|
| 43 | void warn(void); |
|---|
| 44 | void bar(void); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | static int stav=0; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | static struct mini_inode { |
|---|
| 52 | |
|---|
| 53 | time_t timestamp; |
|---|
| 54 | int data_len; |
|---|
| 55 | char data[56]; |
|---|
| 56 | |
|---|
| 57 | } m_ind,*p_ind; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | void help(void) |
|---|
| 62 | { |
|---|
| 63 | FILE *fptr; |
|---|
| 64 | char s[100]; |
|---|
| 65 | |
|---|
| 66 | fprintf(stderr,"Syntax: eepromer [-r|-w|-e|-p] -f /dev/i2c-X ADDRESS \n\n"); |
|---|
| 67 | fprintf(stderr," ADDRESS is address of i2c device eg. 0x51\n"); |
|---|
| 68 | |
|---|
| 69 | if((fptr = fopen("/proc/bus/i2c", "r"))) { |
|---|
| 70 | fprintf(stderr," Installed I2C busses:\n"); |
|---|
| 71 | while(fgets(s, 100, fptr)) |
|---|
| 72 | fprintf(stderr, " %s", s); |
|---|
| 73 | fclose(fptr); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | int main(int argc, char *argv[]){ |
|---|
| 82 | |
|---|
| 83 | int i, file, addr; |
|---|
| 84 | int action; //in this variable will be (-r,-w,-e) |
|---|
| 85 | char device[45]; |
|---|
| 86 | int force; |
|---|
| 87 | |
|---|
| 88 | p_ind=&m_ind; |
|---|
| 89 | force=0; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | for(i=1; i < argc;i++){ |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | if(!strcmp("-r",argv[i])) { |
|---|
| 98 | action=READ; |
|---|
| 99 | break; |
|---|
| 100 | } |
|---|
| 101 | if(!strcmp("-e",argv[i])) { |
|---|
| 102 | action=ERASE; |
|---|
| 103 | break; |
|---|
| 104 | } |
|---|
| 105 | if(!strcmp("-w",argv[i])) { |
|---|
| 106 | action=WRITE; |
|---|
| 107 | break; |
|---|
| 108 | } |
|---|
| 109 | if(!strcmp("-p",argv[i])) { |
|---|
| 110 | action=PHEADER; |
|---|
| 111 | break; |
|---|
| 112 | } |
|---|
| 113 | if(!strcmp("-force",argv[i])) { |
|---|
| 114 | force=FORCE; |
|---|
| 115 | break; |
|---|
| 116 | } |
|---|
| 117 | if(!strcmp("-v",argv[i])) { |
|---|
| 118 | fprintf(stderr,VER); |
|---|
| 119 | exit(0); |
|---|
| 120 | break; |
|---|
| 121 | } |
|---|
| 122 | else { |
|---|
| 123 | |
|---|
| 124 | fprintf(stderr,"Error: No action specified !\n"); |
|---|
| 125 | help(); |
|---|
| 126 | exit(1); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | #ifdef WARNINC |
|---|
| 133 | |
|---|
| 134 | if(force!=FORCE) warn(); |
|---|
| 135 | |
|---|
| 136 | #endif |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | if(argc < 5) { |
|---|
| 140 | fprintf(stderr,"Error: No i2c address specified !\n"); |
|---|
| 141 | help(); |
|---|
| 142 | exit(1); |
|---|
| 143 | |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | for(i=1; i < argc;i++){ |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | if(!strcmp("-f",argv[i])) { |
|---|
| 151 | strcpy(device,argv[i+1]); |
|---|
| 152 | break; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | if(!strlen(device)) { |
|---|
| 158 | |
|---|
| 159 | fprintf(stderr,"Error: No device specified !\n"); |
|---|
| 160 | help(); |
|---|
| 161 | exit(1); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | if(! (addr=strtol(argv[4],NULL,16))) { |
|---|
| 166 | |
|---|
| 167 | fprintf(stderr,"Error: Bad device address !\n"); |
|---|
| 168 | help(); |
|---|
| 169 | exit(1); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if(! (file=init(device,addr))){ |
|---|
| 173 | |
|---|
| 174 | fprintf(stderr,"Error: Init failed !\n"); |
|---|
| 175 | exit(1); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | switch(action){ |
|---|
| 180 | |
|---|
| 181 | case READ: |
|---|
| 182 | content_read(file,addr); |
|---|
| 183 | break; |
|---|
| 184 | |
|---|
| 185 | case WRITE: |
|---|
| 186 | content_write(file,addr); |
|---|
| 187 | break; |
|---|
| 188 | |
|---|
| 189 | case ERASE: erase(file,addr,EEPROM_SIZE); |
|---|
| 190 | break; |
|---|
| 191 | case PHEADER: pheader(file,addr); |
|---|
| 192 | break; |
|---|
| 193 | |
|---|
| 194 | default: |
|---|
| 195 | fprintf(stderr,"Internal error!\n"); |
|---|
| 196 | exit(1); break; |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | close(file); |
|---|
| 202 | exit(0); |
|---|
| 203 | |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | /****************************************************************************/ |
|---|
| 209 | /* Low level function */ |
|---|
| 210 | /* */ |
|---|
| 211 | /****************************************************************************/ |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | int block_write(int file,int dev_addr,int eeprom_addr,unsigned char *buf,int lenght){ |
|---|
| 218 | |
|---|
| 219 | unsigned char buff[2]; |
|---|
| 220 | struct i2c_msg msg[2]; |
|---|
| 221 | |
|---|
| 222 | struct i2c_ioctl_rdwr_data { |
|---|
| 223 | |
|---|
| 224 | struct i2c_msg *msgs; /* ptr to array of simple messages */ |
|---|
| 225 | int nmsgs; /* number of messages to exchange */ |
|---|
| 226 | } msgst; |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | if ( lenght > (MAX_BLK_SIZE) ) { |
|---|
| 231 | |
|---|
| 232 | fprintf(stderr, |
|---|
| 233 | "Error: Block too large:\n"); |
|---|
| 234 | |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | //bar(); |
|---|
| 239 | |
|---|
| 240 | made_address(eeprom_addr,buff); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | msg[0].addr = dev_addr; |
|---|
| 244 | msg[0].flags = 0; |
|---|
| 245 | msg[0].len = 2; |
|---|
| 246 | msg[0].buf = buff; |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | msg[1].addr = dev_addr; |
|---|
| 250 | msg[1].flags = I2C_M_NOSTART; |
|---|
| 251 | msg[1].len = lenght; |
|---|
| 252 | msg[1].buf = buf; |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | msgst.msgs = msg; |
|---|
| 256 | msgst.nmsgs = 2; |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | if (ioctl(file,I2C_RDWR,&msgst) < 0){ |
|---|
| 260 | |
|---|
| 261 | fprintf(stderr, |
|---|
| 262 | "Error: Transaction failed: %s\n", |
|---|
| 263 | strerror(errno)); |
|---|
| 264 | |
|---|
| 265 | return 1; |
|---|
| 266 | |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | return 0; |
|---|
| 270 | |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | int block_read(int file,int dev_addr,int eeprom_addr,unsigned char *buf){ |
|---|
| 277 | |
|---|
| 278 | int ln; |
|---|
| 279 | char buff[2]; //={0x0,0x0}; |
|---|
| 280 | |
|---|
| 281 | struct i2c_msg msg[2]; |
|---|
| 282 | |
|---|
| 283 | struct i2c_ioctl_rdwr_data { |
|---|
| 284 | |
|---|
| 285 | struct i2c_msg *msgs; /* ptr to array of simple messages */ |
|---|
| 286 | int nmsgs; /* number of messages to exchange */ |
|---|
| 287 | } msgst; |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | made_address(eeprom_addr,buff); |
|---|
| 292 | ln=0; |
|---|
| 293 | //bar(); |
|---|
| 294 | |
|---|
| 295 | msg[0].addr = dev_addr; |
|---|
| 296 | msg[0].flags = 0; |
|---|
| 297 | msg[0].len = 2; |
|---|
| 298 | msg[0].buf = buff; |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | msg[1].addr = dev_addr; |
|---|
| 302 | msg[1].flags = I2C_M_RD; |
|---|
| 303 | msg[1].len = MAX_BLK_SIZE; |
|---|
| 304 | msg[1].buf = buf; |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | msgst.msgs = msg; |
|---|
| 310 | msgst.nmsgs = 2; |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | if ((ln = ioctl(file, I2C_RDWR, &msgst)) < 0) { |
|---|
| 316 | |
|---|
| 317 | fprintf(stderr, |
|---|
| 318 | "Error: Read error:%d\n",ln); |
|---|
| 319 | return ln; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | return ln; |
|---|
| 323 | |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | void made_address(int addr,unsigned char *buf){ |
|---|
| 342 | |
|---|
| 343 | int k; |
|---|
| 344 | |
|---|
| 345 | //addr = addr & 0xFFFF; /*odstranim nepoterbne bity*/ |
|---|
| 346 | |
|---|
| 347 | k=addr; |
|---|
| 348 | buf[1]=(unsigned char) (k & 0xFF); //vyrobim druhy byte adresy |
|---|
| 349 | k=addr & 0xFF00 ; |
|---|
| 350 | buf[0]= ((unsigned char) (k >> 8)) & 0x7F; |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | return; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | int init(char *device,int addr) { |
|---|
| 358 | |
|---|
| 359 | int file; |
|---|
| 360 | unsigned long funcs; |
|---|
| 361 | |
|---|
| 362 | if ((file = open(device,O_RDWR)) < 0) { |
|---|
| 363 | |
|---|
| 364 | fprintf(stderr,"Error: Could not open file %s\n", |
|---|
| 365 | device); |
|---|
| 366 | |
|---|
| 367 | return 0; |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | /* check adapter functionality */ |
|---|
| 372 | if (ioctl(file,I2C_FUNCS,&funcs) < 0) { |
|---|
| 373 | fprintf(stderr, |
|---|
| 374 | "Error: Could not get the adapter functionality matrix: %s\n", |
|---|
| 375 | strerror(errno)); |
|---|
| 376 | close(file); |
|---|
| 377 | return 0; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | /* The I2C address */ |
|---|
| 381 | if (ioctl(file,I2C_SLAVE,addr) < 0) { |
|---|
| 382 | /* ERROR HANDLING; you can check errno to see what went wrong */ |
|---|
| 383 | fprintf(stderr, |
|---|
| 384 | "Error: Cannot communicate with slave: %s\n", |
|---|
| 385 | strerror(errno)); |
|---|
| 386 | |
|---|
| 387 | close(file); |
|---|
| 388 | return 0; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | return file; |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | int content_write(int file, int addr){ |
|---|
| 396 | |
|---|
| 397 | unsigned char buf[MAX_BLK_SIZE]; |
|---|
| 398 | unsigned char pom; |
|---|
| 399 | int i, j, k, delka, addr_cnt; |
|---|
| 400 | |
|---|
| 401 | delka=0; |
|---|
| 402 | addr_cnt=HEAD_SIZE; |
|---|
| 403 | k=0; |
|---|
| 404 | |
|---|
| 405 | for(j=0;j<=MAX_BLK_SIZE;j++)buf[j]=0; |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | i=0; |
|---|
| 410 | |
|---|
| 411 | for(;;) { |
|---|
| 412 | |
|---|
| 413 | delka=fread(&pom,1,1,stdin); |
|---|
| 414 | |
|---|
| 415 | if( delka > 0 ){ |
|---|
| 416 | buf[i]=pom; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | if(i==(MAX_BLK_SIZE-1) || (delka < 1)) { |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | if(block_write(file,addr,addr_cnt,buf,delka<1?i:(i+1)) !=0) { |
|---|
| 424 | |
|---|
| 425 | fprintf(stderr,"Block write failed\n"); |
|---|
| 426 | return 1; |
|---|
| 427 | |
|---|
| 428 | } |
|---|
| 429 | //printf("i:%d\n",i); |
|---|
| 430 | addr_cnt=addr_cnt + i + (delka==1?1:0); //+i |
|---|
| 431 | |
|---|
| 432 | for(j=0;j<=MAX_BLK_SIZE;j++)buf[j]=0; |
|---|
| 433 | |
|---|
| 434 | i=0; |
|---|
| 435 | if(delka<1) { |
|---|
| 436 | |
|---|
| 437 | //pisu EOF |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | if(inode_write(file,addr,(addr_cnt-HEAD_SIZE)) !=0) { |
|---|
| 441 | |
|---|
| 442 | fprintf(stderr,"Inode write failed\n"); |
|---|
| 443 | return 1; |
|---|
| 444 | |
|---|
| 445 | } |
|---|
| 446 | break; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | } else i++; |
|---|
| 451 | |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | return 0; |
|---|
| 455 | |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | int content_read(int file, int addr){ |
|---|
| 460 | |
|---|
| 461 | unsigned char buf[MAX_BLK_SIZE]; |
|---|
| 462 | int i, j, k, delka; |
|---|
| 463 | |
|---|
| 464 | delka=0; |
|---|
| 465 | k=0; |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | inode_read(file,addr,p_ind ); |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | for(i=HEAD_SIZE;i<= (HEAD_SIZE + p_ind->data_len);i=i+MAX_BLK_SIZE ) { |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | if(block_read(file,addr,i,buf) !=0) { |
|---|
| 475 | |
|---|
| 476 | fprintf(stderr,"Block read failed\n"); |
|---|
| 477 | return 1; |
|---|
| 478 | |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | if( (HEAD_SIZE + p_ind->data_len - i) < MAX_BLK_SIZE ) { |
|---|
| 482 | k= HEAD_SIZE + p_ind->data_len - i; |
|---|
| 483 | }else { |
|---|
| 484 | k=MAX_BLK_SIZE; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | for(j=0;j<k ;j++){ |
|---|
| 489 | |
|---|
| 490 | putc(buf[j],stdout); |
|---|
| 491 | |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | return 0; |
|---|
| 498 | |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | void erase(int file, int addr,int eeprom_size){ |
|---|
| 504 | |
|---|
| 505 | unsigned char buf[MAX_BLK_SIZE]; |
|---|
| 506 | int i, j, k, delka; |
|---|
| 507 | |
|---|
| 508 | delka=0; |
|---|
| 509 | k=0; |
|---|
| 510 | |
|---|
| 511 | for(j=0;j<=MAX_BLK_SIZE;j++)buf[j]=0; |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | for(i=0;i<eeprom_size;i=i+MAX_BLK_SIZE) { |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | if(block_write(file,addr,i,buf,MAX_BLK_SIZE) !=0) { |
|---|
| 521 | |
|---|
| 522 | fprintf(stderr,"Block write failed\n"); |
|---|
| 523 | return; |
|---|
| 524 | |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | return; |
|---|
| 530 | |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | void bar(void){ |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | if( stav > 70 ) stav=0; |
|---|
| 539 | |
|---|
| 540 | |
|---|
| 541 | switch(stav) { |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | case 10: fwrite("\\",1,1,stderr); |
|---|
| 545 | fflush(stderr); |
|---|
| 546 | rewind(stderr); |
|---|
| 547 | break; |
|---|
| 548 | case 20: fwrite("|",1,1,stderr); |
|---|
| 549 | fflush(stderr); |
|---|
| 550 | rewind(stderr); |
|---|
| 551 | break; |
|---|
| 552 | case 30: fwrite("/",1,1,stderr); |
|---|
| 553 | fflush(stderr); |
|---|
| 554 | rewind(stderr); |
|---|
| 555 | break; |
|---|
| 556 | case 40: fwrite("-",1,1,stderr); |
|---|
| 557 | fflush(stderr); |
|---|
| 558 | rewind(stderr); |
|---|
| 559 | break; |
|---|
| 560 | case 50: fwrite("\\",1,1,stderr); |
|---|
| 561 | fflush(stderr); |
|---|
| 562 | rewind(stderr); |
|---|
| 563 | break; |
|---|
| 564 | case 60: fwrite("|",1,1,stderr); |
|---|
| 565 | fflush(stderr); |
|---|
| 566 | rewind(stderr); |
|---|
| 567 | break; |
|---|
| 568 | case 70: fwrite("/",1,1,stderr); |
|---|
| 569 | fflush(stderr); |
|---|
| 570 | rewind(stderr); |
|---|
| 571 | break; |
|---|
| 572 | } |
|---|
| 573 | stav++; |
|---|
| 574 | |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | int inode_write(int file,int dev_addr,int lenght){ |
|---|
| 582 | |
|---|
| 583 | unsigned char buff[2]; |
|---|
| 584 | struct i2c_msg msg[2]; |
|---|
| 585 | |
|---|
| 586 | struct i2c_ioctl_rdwr_data { |
|---|
| 587 | |
|---|
| 588 | struct i2c_msg *msgs; /* ptr to array of simple messages */ |
|---|
| 589 | int nmsgs; /* number of messages to exchange */ |
|---|
| 590 | } msgst; |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | m_ind.timestamp=time(NULL); |
|---|
| 594 | m_ind.data_len=lenght; |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | //bar(); |
|---|
| 601 | |
|---|
| 602 | made_address(START_ADDR,buff); |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | msg[0].addr = dev_addr; |
|---|
| 606 | msg[0].flags = 0; |
|---|
| 607 | msg[0].len = 2; |
|---|
| 608 | msg[0].buf = buff; |
|---|
| 609 | |
|---|
| 610 | |
|---|
| 611 | msg[1].addr = dev_addr; |
|---|
| 612 | msg[1].flags = I2C_M_NOSTART; |
|---|
| 613 | msg[1].len = sizeof(struct mini_inode); |
|---|
| 614 | msg[1].buf = (char *) &m_ind; |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | msgst.msgs = msg; |
|---|
| 619 | msgst.nmsgs = 2; |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | if (ioctl(file,I2C_RDWR,&msgst) < 0){ |
|---|
| 623 | |
|---|
| 624 | fprintf(stderr, |
|---|
| 625 | "Error: Transaction failed: %s\n", |
|---|
| 626 | strerror(errno)); |
|---|
| 627 | |
|---|
| 628 | return 1; |
|---|
| 629 | |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | return 0; |
|---|
| 633 | |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | int inode_read(int file,int dev_addr,void *p_inode ){ |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | #define POK 32 |
|---|
| 642 | int ln; |
|---|
| 643 | char buff[2]; //={0x0,0x0}; |
|---|
| 644 | |
|---|
| 645 | struct i2c_msg msg[2]; |
|---|
| 646 | |
|---|
| 647 | struct i2c_ioctl_rdwr_data { |
|---|
| 648 | |
|---|
| 649 | struct i2c_msg *msgs; /* ptr to array of simple messages */ |
|---|
| 650 | int nmsgs; /* number of messages to exchange */ |
|---|
| 651 | } msgst; |
|---|
| 652 | |
|---|
| 653 | made_address(START_ADDR,buff); |
|---|
| 654 | |
|---|
| 655 | ln=0; |
|---|
| 656 | //bar(); |
|---|
| 657 | |
|---|
| 658 | msg[0].addr = dev_addr; |
|---|
| 659 | msg[0].flags = 0; |
|---|
| 660 | msg[0].len = 2; |
|---|
| 661 | msg[0].buf = buff; |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | msg[1].addr = dev_addr; |
|---|
| 665 | msg[1].flags = I2C_M_RD; |
|---|
| 666 | msg[1].len = sizeof(struct mini_inode); |
|---|
| 667 | msg[1].buf = p_inode; |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | msgst.msgs = msg; |
|---|
| 673 | msgst.nmsgs = 2; |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | if ((ln = ioctl(file, I2C_RDWR, &msgst)) < 0) { |
|---|
| 677 | |
|---|
| 678 | fprintf(stderr, |
|---|
| 679 | "Error: Read error:%d\n",ln); |
|---|
| 680 | return ln; |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | return ln; |
|---|
| 686 | |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | void pheader(int file,int dev_addr){ |
|---|
| 691 | |
|---|
| 692 | struct tm *p_tm; |
|---|
| 693 | char time_buf[15],*p_buf; |
|---|
| 694 | |
|---|
| 695 | p_buf=time_buf; |
|---|
| 696 | inode_read(file,dev_addr,p_ind ); |
|---|
| 697 | p_tm=localtime(&p_ind->timestamp); |
|---|
| 698 | strftime(p_buf,sizeof(time_buf),"%Y%m%d%H%M%S",p_tm); |
|---|
| 699 | printf("LEN=%d,TIME=%s\n",p_ind->data_len,p_buf); |
|---|
| 700 | return; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | #ifdef WARNINC |
|---|
| 707 | void warn(void) |
|---|
| 708 | { |
|---|
| 709 | |
|---|
| 710 | fprintf(stderr,"\n\n!!!!!!!!!!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!!!!!\n"); |
|---|
| 711 | fprintf(stderr,"This program is intended for use on eeproms\nusing external busses such as i2c-pport.\n"); |
|---|
| 712 | fprintf(stderr,"Do not use this on your SDRAM DIMM EEPROMS\nunless you REALLY REALLY know what you are\ndoing!!! Doing so will render your SDRAM\nUSELESS and leave your system UNBOOTABLE!!!\n"); |
|---|
| 713 | fprintf(stderr,"To disable this warning use -force\n"); |
|---|
| 714 | fprintf(stderr,"\n\nPress ENTER to continue or hit Control-C NOW !!!!\n\n\n"); |
|---|
| 715 | |
|---|
| 716 | getchar(); |
|---|
| 717 | } |
|---|
| 718 | #endif |
|---|