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