1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
|
/*
* cycfx2dev.cc - Cypress FX2 device class: low-level routines.
*
* Copyright (c) 2006--2009 by Wolfgang Wieser ] wwieser (a) gmx <*> de [
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation. (See COPYING.GPL for details.)
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include "cycfx2dev.h"
struct usb_device *USBFindDevice(const char *bus,const char *dev)
{
for(usb_bus *b=usb_busses; b; b=b->next)
{
if(strcmp(b->dirname,bus)) continue;
for(struct usb_device *d=b->devices; d; d=d->next)
{
if(strcmp(d->filename,dev)) continue;
return(d);
}
}
return(NULL);
}
struct usb_device *USBFindDevice(int vendor,int product,int nth)
{
for(usb_bus *b=usb_busses; b; b=b->next)
{
for(struct usb_device *d=b->devices; d; d=d->next)
{
if(d->descriptor.idVendor==vendor &&
d->descriptor.idProduct==product)
{
if(!nth--)
{ return(d); }
}
}
}
return(NULL);
}
//------------------------------------------------------------------------------
int CypressFX2Device::BlockRead(int endpoint,unsigned char *buf,size_t nbytes,
char type)
{
// FIXME: This function is somewhat bugged concerning reliable delivery
// and correct handling of return values for short reads and .
// timeout handling. Reason: Not sure what libusb is meant to
// return and to do in non-standard cases.
if(!IsOpen())
{ fprintf(stderr,"BlockRead: Not connected!\n"); return(-1); }
bool may_be_short=0;
switch(type)
{
case 'b': break;
case 'i': break;
case 'B': may_be_short=1; type='b'; break;
case 'I': may_be_short=1; type='i'; break;
default: assert(0);
}
int interface=0;
int alt_interface=(type=='i' ? 2 : 1);
if(force_alt_interface>=0) alt_interface=force_alt_interface;
if(usb_claim_interface(usbhdl,interface)<0)
{ fprintf(stderr,"Failed to claim interface %d: %s\n",
interface,usb_strerror()); return(-1); }
size_t chunk_size=nbytes;
int error=0;
size_t left=nbytes;
do {
if(usb_set_altinterface(usbhdl,alt_interface)<0)
{
fprintf(stderr,"Failed to set altinterface %d: %s\n",
alt_interface,usb_strerror());
++error; break;
}
int ncalls=0;
while(left)
{
size_t bs = left>chunk_size ? chunk_size : left;
ssize_t rv;
if(type=='i')
{ rv=usb_interrupt_read(usbhdl,endpoint,(char*)buf,bs,
/*timeout=*/1000/*msec*/); }
else
{ rv=usb_bulk_read(usbhdl,endpoint,(char*)buf,bs,
/*timeout=*/1000/*msec*/); }
++ncalls;
if(rv<0)
{
fprintf(stderr,"Reading %zu bytes from EP 0x%02x: "
"USB: %s SYS: %s\n",
bs,endpoint,usb_strerror(),strerror(-rv));
++error;
goto breakout;
}
assert((size_t)rv<=left);
left-=rv;
if((size_t)rv<bs)
{
if(may_be_short) goto breakout;
// Not sure if rv=0 is a valid timeout indication...
//if(!rv)
fprintf(stderr,"Short read (%zd/%zu bytes)%s\n",rv,bs,
rv==0 ? " (timeout?)" : "");
if(rv==0)
{ ++error; goto breakout; }
}
}
} while(0); breakout:;
usb_release_interface(usbhdl,interface);
size_t read=nbytes-left;
return(read ? read : (error ? -1 : 0));
}
int CypressFX2Device::BlockWrite(int endpoint,const unsigned char *buf,
size_t nbytes,char type)
{
// FIXME: This function is somewhat bugged concerning reliable delivery
// and correct handling of return values for short writes and .
// timeout handling. Reason: Not sure what libusb is meant to
// return and to do in non-standard cases.
if(!IsOpen())
{ fprintf(stderr,"BlockWrite: Not connected!\n"); return(-1); }
switch(type)
{
case 'b': break;
case 'i': break;
default: assert(0);
}
int interface=0;
int alt_interface=(type=='i' ? 2 : 1);
if(force_alt_interface>=0) alt_interface=force_alt_interface;
if(usb_claim_interface(usbhdl,interface)<0)
{ fprintf(stderr,"Failed to claim interface %d: %s\n",
interface,usb_strerror()); return(-1); }
size_t chunk_size=nbytes;
int error=0;
size_t left=nbytes;
do {
if(usb_set_altinterface(usbhdl,alt_interface)<0)
{
fprintf(stderr,"Failed to set altinterface %d: %s\n",
alt_interface,usb_strerror());
++error; break;
}
int ncalls=0;
while(left)
{
size_t bs = left>chunk_size ? chunk_size : left;
ssize_t rv;
if(type=='i')
{ rv=usb_interrupt_write(usbhdl,endpoint,(char*)buf,bs,
/*timeout=*/1000/*msec*/); }
else
{ rv=usb_bulk_write(usbhdl,endpoint,(char*)buf,bs,
/*timeout=*/1000/*msec*/); }
++ncalls;
if(rv<0)
{
fprintf(stderr,"Writing %zu bytes to EP 0x%02x: "
"USB: %s SYS: %s\n",
bs,endpoint,usb_strerror(),strerror(-rv));
++error;
goto breakout;
}
assert((size_t)rv<=left);
left-=rv;
if((size_t)rv<bs)
{
// Not sure if rv=0 is a valid timeout indication...
//if(!rv)
fprintf(stderr,"Short write (%zd/%zu bytes)%s\n",rv,bs,
rv==0 ? " (timeout?)" : "");
if(rv==0)
{ ++error; goto breakout; }
}
}
} while(0); breakout:;
usb_release_interface(usbhdl,interface);
size_t read=nbytes-left;
return(read ? read : (error ? -1 : 0));
}
int CypressFX2Device::BenchBlockRead(int endpoint,size_t nbytes,
size_t chunk_size,char type)
{
if(!IsOpen())
{ fprintf(stderr,"BenchBlockRead: Not connected!\n"); return(1); }
assert(type=='b' || type=='i');
int interface=0;
int alt_interface=(type=='i' ? 2 : 1);
if(force_alt_interface>=0) alt_interface=force_alt_interface;
if(usb_claim_interface(usbhdl,interface)<0)
{ fprintf(stderr,"Failed to claim interface %d: %s\n",
interface,usb_strerror()); return(1); }
int error=0;
char *buf=(char*)malloc(chunk_size);
assert(buf);
do {
if(usb_set_altinterface(usbhdl,alt_interface)<0)
{
fprintf(stderr,"Failed to set altinterface %d: %s\n",
alt_interface,usb_strerror());
++error; break;
}
// Start benchmark:
timeval start_tv,end_tv;
gettimeofday(&start_tv,NULL);
size_t left=nbytes;
int ncalls=0;
while(left)
{
size_t bs = left>chunk_size ? chunk_size : left;
ssize_t rv;
if(type=='i')
{ rv=usb_interrupt_read(usbhdl,endpoint,buf,bs,
/*timeout=*/1000/*msec*/); }
else
{ rv=usb_bulk_read(usbhdl,endpoint,buf,bs,
/*timeout=*/1000/*msec*/); }
++ncalls;
if(rv<0)
{
fprintf(stderr,"Reading %zu bytes from EP 0x%02x: "
"USB: %s SYS: %s\n",
bs,endpoint,usb_strerror(),strerror(-rv));
++error;
goto breakout;
}
if((size_t)rv<bs)
{
// Not sure if rv=0 is a valid timeout indication...
//if(!rv)
fprintf(stderr,"Short read (%zd/%zu bytes)%s\n",rv,bs,
rv==0 ? " (timeout?)" : "");
if(rv==0)
{ ++error; goto breakout; }
}
assert((size_t)rv<=left);
left-=rv;
}
// End benchmark:
gettimeofday(&end_tv,NULL);
double seconds =
double(end_tv.tv_sec-start_tv.tv_sec)+
double(end_tv.tv_usec-start_tv.tv_usec)/1000000.0;
printf("Read %zu bytes in %5d msec (chunk size %6zu): "
"%6.3f Mb/sec (%5d calls, %6zu bytes/call)\n",
nbytes,(int)(seconds*1000+0.5),
chunk_size,nbytes/seconds/1024/1024,
ncalls,nbytes/ncalls);
} while(0); breakout:;
if(buf)
{ free(buf); buf=NULL; }
usb_release_interface(usbhdl,interface);
return(error ? -1 : 0);
}
int CypressFX2Device::FX2Reset(bool running)
{
// Reset is accomplished by writing a 1 to address 0xE600.
// Start running by writing a 0 to that address.
const size_t reset_addr=0xe600;
unsigned char val = running ? 0 : 1;
return(WriteRAM(reset_addr,&val,1));
}
int CypressFX2Device::WriteRAM(size_t addr,const unsigned char *data,
size_t nbytes)
{
if(!IsOpen())
{ fprintf(stderr,"WriteRAM: Not connected!\n"); return(1); }
int n_errors=0;
const size_t chunk_size=16;
const unsigned char *d=data;
const unsigned char *dend=data+nbytes;
while(d<dend)
{
size_t bs=dend-d;
if(bs>chunk_size) bs=chunk_size;
size_t dl_addr=addr+(d-data);
int rv=usb_control_msg(usbhdl,0x40,0xa0,
/*addr=*/dl_addr,0,
/*buf=*/(char*)d,/*size=*/bs,
/*timeout=*/1000/*msec*/);
if(rv<0)
{ fprintf(stderr,"Writing %zu bytes at 0x%zx: %s\n",
bs,dl_addr,usb_strerror()); ++n_errors; }
d+=bs;
}
return(n_errors);
}
int CypressFX2Device::ReadRAM(size_t addr,unsigned char *data,size_t nbytes)
{
if(!IsOpen())
{ fprintf(stderr,"ReadRAM: Not connected!\n"); return(1); }
int n_errors=0;
const size_t chunk_size=16;
unsigned char *d=data;
unsigned char *dend=data+nbytes;
while(d<dend)
{
size_t bs=dend-d;
if(bs>chunk_size) bs=chunk_size;
size_t rd_addr=addr+(d-data);
int rv=usb_control_msg(usbhdl,0xc0,0xa0,
/*addr=*/rd_addr,0,
/*buf=*/(char*)d,/*size=*/bs,
/*timeout=*/1000/*msec*/);
if(rv<0)
{ fprintf(stderr,"Reading %zu bytes at 0x%zx: %s\n",
bs,rd_addr,usb_strerror()); ++n_errors; }
d+=bs;
}
return(n_errors);
}
int CypressFX2Device::_ProgramIHexLine(const char *buf,
const char *path,int line)
{
const char *s=buf;
if(*s!=':')
{ fprintf(stderr,"%s:%d: format violation (1)\n",path,line);
return(1); }
++s;
unsigned int nbytes=0,addr=0,type=0;
if(sscanf(s,"%02x%04x%02x",&nbytes,&addr,&type)!=3)
{ fprintf(stderr,"%s:%d: format violation (2)\n",path,line);
return(1); }
s+=8;
if(type==0)
{
//printf(" Writing nbytes=%d at addr=0x%04x\n",nbytes,addr);
assert(nbytes>=0 && nbytes<256);
unsigned char data[nbytes];
unsigned char cksum=nbytes+addr+(addr>>8)+type;
for(unsigned int i=0; i<nbytes; i++)
{
unsigned int d=0;
if(sscanf(s,"%02x",&d)!=1)
{ fprintf(stderr,"%s:%d: format violation (3)\n",path,line);
return(1); }
s+=2;
data[i]=d;
cksum+=d;
}
unsigned int file_cksum=0;
if(sscanf(s,"%02x",&file_cksum)!=1)
{ fprintf(stderr,"%s:%d: format violation (4)\n",path,line);
return(1); }
if((cksum+file_cksum)&0xff)
{ fprintf(stderr,"%s:%d: checksum mismatch (%u/%u)\n",
path,line,cksum,file_cksum); return(1); }
if(WriteRAM(addr,data,nbytes))
{ return(1); }
}
else if(type==1)
{
// EOF marker. Oh well, trust it.
return(-1);
}
else
{
fprintf(stderr,"%s:%d: Unknown entry type %d\n",path,line,type);
return(1);
}
return(0);
}
int CypressFX2Device::ProgramIHexFile(const char *path)
{
if(!IsOpen())
{ fprintf(stderr,"ProgramIHexFile: Not connected!\n"); return(1); }
FILE *fp=fopen(path,"r");
if(!fp)
{ fprintf(stderr,"Failed to open %s: %s\n",path,strerror(errno));
return(2); }
int n_errors=0;
const size_t buflen=1024; // Hopefully much too long for real life...
char buf[buflen];
int line=1;
for(;;++line)
{
*buf='\0';
if(!fgets(buf,buflen,fp))
{
if(feof(fp))
{ break; }
fprintf(stderr,"Reading %s (line %d): %s\n",path,line,
strerror(ferror(fp)));
fclose(fp); fp=NULL;
return(3);
}
int rv=_ProgramIHexLine(buf,path,line);
if(rv<0) break;
if(rv)
{ ++n_errors; }
}
if(fp)
{ fclose(fp); }
return(n_errors ? -1 : 0);
}
int CypressFX2Device::ProgramStaticIHex(const char **ihex)
{
int n_errors=0;
for(int line=0; ihex[line]; line++)
{
int rv=_ProgramIHexLine(ihex[line],"[builtin]",line+1);
if(rv<0) break;
if(rv)
{ ++n_errors; }
}
return(n_errors ? -1 : 0);
}
int CypressFX2Device::ProgramBinFile(const char *path,size_t start_addr)
{
if(!IsOpen())
{ fprintf(stderr,"ProgramIHexFile: Not connected!\n"); return(1); }
int fd=::open(path,O_RDONLY);
if(fd<0)
{ fprintf(stderr,"Failed to open %s: %s\n",path,strerror(errno));
return(2); }
int n_errors=0;
const size_t buflen=1024;
char buf[buflen];
size_t addr=start_addr;
for(;;)
{
ssize_t rd=read(fd,buf,buflen);
if(!rd) break;
if(rd<0)
{
fprintf(stderr,"Reading %s: %s\n",path,strerror(errno));
::close(fd); fd=-1;
return(3);
}
if(WriteRAM(addr,(const unsigned char*)buf,rd))
{ ++n_errors; }
addr+=rd;
}
if(fd>=0)
{ ::close(fd); fd=-1; }
return(n_errors ? -1 : 0);
}
int CypressFX2Device::CtrlMsg(unsigned char requesttype,
unsigned char request,int value,int index,
const unsigned char *ctl_buf,size_t ctl_buf_size)
{
if(!IsOpen())
{ fprintf(stderr,"CtrlMsg: Not connected!\n"); return(1); }
int n_errors=0;
int rv=usb_control_msg(usbhdl,requesttype,request,
value,index,
(char*)ctl_buf,ctl_buf_size,
/*timeout=*/1000/*msec*/);
if(rv<0)
{ fprintf(stderr,"Sending USB control message: %s\n",
usb_strerror()); ++n_errors; }
return(n_errors);
}
int CypressFX2Device::open(struct usb_device *_usbdev)
{
close();
usbdev=_usbdev;
usbhdl=usb_open(usbdev);
if(!usbhdl)
{ fprintf(stderr,"Failed to open device: %s\n",usb_strerror());
return(1); }
return(0);
}
int CypressFX2Device::close()
{
int rv=0;
if(usbhdl)
{
rv=usb_close(usbhdl); usbhdl=NULL;
if(rv)
{ fprintf(stderr,"closing USB device: %s\n",usb_strerror()); }
}
usbdev=NULL;
return(rv);
}
|