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
|
/*
* cycfx2prog.cc - Cypress FX2(LP) programmer.
*
* Copyright (c) 2006 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 <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "cycfx2dev.h"
// The version is set in Makefile.
#ifndef CYCFX2PROG_VERSION
# error CYCFX2PROG_VERSION undefined
#endif
// Global FX2 device we're connected to.
CypressFX2Device cycfx2;
static void *CheckMalloc(void *ptr)
{
if(!ptr)
{
fprintf(stderr,"malloc failed\n");
exit(1);
}
return(ptr);
}
// FIXME: What to do with these:
// HMMM.... There is usb_device::num_children and **usb_device::children.
void USBDumpBusses(FILE *out)
{
for(usb_bus *b=usb_busses; b; b=b->next)
{
for(struct usb_device *d=b->devices; d; d=d->next)
{
bool is_fx2_dev = (d->descriptor.idVendor==0x4b4 &&
d->descriptor.idProduct==0x8613);
fprintf(out,"Bus %s Device %s: ID %04x:%04x%s\n",
b->dirname,d->filename,
d->descriptor.idVendor,d->descriptor.idProduct,
is_fx2_dev ? " <--" : "");
}
}
}
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(b->dirname,bus)) continue;
return(d);
}
}
return(NULL);
}
struct usb_device *USBFindDevice(int vendor,int product)
{
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)
{ return(d); }
}
}
return(NULL);
}
static inline void _HexdumpPutChar(FILE *out,unsigned char x)
{
if(isprint(x))
{ fprintf(out,"%c",x); }
else
{ fprintf(out,"."); }
}
static void HexDumpBuffer(FILE *out,const unsigned char *data,size_t size,
int with_ascii)
{
ssize_t skip_start=-1;
for(size_t i=0; i<size; )
{
size_t j=0;
#if 0
// Do not dump lines which are only ffffff....
int do_skip=1;
for(size_t ii=i; j<16 && ii<size; j++,ii++)
{
if(data[ii]!=0xffU)
{ do_skip=0; break; }
}
if(do_skip)
{
if(skip_start<0) skip_start=i;
i+=16;
continue;
}
else if(skip_start>=0)
{
printf(" [skipping 0x%04x..0x%04x: "
"%u words 0xffff]\n",
size_t(skip_start),i-1,i-size_t(skip_start));
skip_start=-1;
}
#endif
printf(" 0x%04x ",i);
size_t oldi=i;
for(j=0; j<32 && i<size; j++,i++)
{
if(j && !(j%8)) printf(" ");
printf("%02x",(unsigned int)data[i]);
}
// This adds a plaintext column (printable chars only):
if(with_ascii)
{
for(; j<32; j++)
{ printf((j && !(j%8)) ? " " : " "); }
printf(" ");
i=oldi;
for(j=0; j<32 && i<size; j++,i++)
{ _HexdumpPutChar(out,data[i]); }
}
printf("\n");
}
#if 0
if(skip_start>=0 && size>0)
{
printf(" [skipping 0x%04x..0x%04x: "
"%u words 0xffff]\n",
size_t(skip_start),size-1,size-size_t(skip_start));
skip_start=-1;
}
#endif
fflush(out);
}
static void PrintHelp()
{
fprintf(stderr,
"Usage: cycfx2prog [-d=BUS.DEV] [commands...]\n"
"Options:\n"
" --help print this and then exit\n"
" --version print version information and then exit\n"
" --list list devices and busses and then exit\n"
" -d= set device to use e.g. 006.003; if not specified, first\n"
" unconfigured Cypress FX2 is used.\n"
"Commands: Must be specified after all options.\n"
" reset reset 8051 by putting reset low\n"
" run start the 8051 by putting reset high\n"
" prg:FILE program 8051; FILE is an Intel hex file (.ihx); will\n"
" reset the 8051 before download; use \"run\" afterwards\n"
" delay:NN make a delay for NN msec\n"
" set:ADR,VAL set byte at address ADR to value VAL\n"
" dram:ADR,LEN dump RAM content: LEN bytes starting at ADR\n"
" dbulk:EP,L[,N] bulk read N (default: 1) buffers of size L from endpoint\n"
" EP (1,2,4,6,8) and dump them; L<0 to allow short reads\n"
" sbulk:EP,STR send string STR as bulk messate to endpoint EP (1,2,4,6,8)\n"
" bench_bulk:EP,L[,CS] bench reading L bytes from endpoint EP (chunk size CS)\n"
" NOTE: This uses libusb is slow on the host side!\n"
"Cypress FX2(LP) programmer tool v%s copyright (c) 2006 by Wolfgang Wieser\n"
,CYCFX2PROG_VERSION);
}
int main(int argc,char **arg)
{
int errors=0;
const char *arg_bus_dev=NULL;
bool do_list=0;
for(int i=1; i<argc; i++)
{
if(!strcmp(arg[i],"--help"))
{ PrintHelp(); return(0); }
if(!strcmp(arg[i],"--version"))
{
printf("cycfx2prog version %s\n",CYCFX2PROG_VERSION);
fflush(stdout);
return(0);
}
else if(!strcmp(arg[i],"--list"))
{ do_list=1; }
else if(!strncmp(arg[i],"-d=",3))
{ arg_bus_dev=arg[i]+3; }
else if(*arg[i]=='-')
{ fprintf(stderr,"Illegal option \"%s\".\n",arg[i]); ++errors; }
}
if(errors)
{ return(1); }
// Initialize the USB library...
usb_init();
int rv=usb_find_busses();
if(rv<0)
{ fprintf(stderr,"usb_find_busses failed (rv=%d)\n",rv); return(1); }
rv=usb_find_devices();
if(rv<0)
{ fprintf(stderr,"usb_find_devices failed (rv=%d)\n",rv); return(1); }
if(do_list)
{ USBDumpBusses(stdout); return(0); }
// Look for device.
struct usb_device *usbdev=NULL;
if(arg_bus_dev)
{
char bus[16];
char dev[16];
do {
if(strlen(arg_bus_dev)>=16) break; // Prevents buffer overflows.
const char *p=strchr(arg_bus_dev,'.');
if(!p) break;
strncpy(bus,arg_bus_dev,p-arg_bus_dev);
bus[p-arg_bus_dev]='\0';
strcpy(dev,p+1);
usbdev=USBFindDevice(bus,dev);
} while(0);
if(!usbdev)
{ fprintf(stderr,"Illegal/nonexistant device %s.\n",arg_bus_dev);
return(1); }
}
else
{
usbdev=USBFindDevice(0x4b4,0x8613);
if(!usbdev)
{ fprintf(stderr,"No unconfigured Cypress FX2 attached.\n");
return(1); }
}
fprintf(stderr,"Using ID %04x:%04x on %s.%s.\n",
usbdev->descriptor.idVendor,usbdev->descriptor.idProduct,
usbdev->bus->dirname,usbdev->filename);
if(cycfx2.open(usbdev))
{ return(1); }
// Execute all the user commands.
for(int argc_i=1; argc_i<argc; argc_i++)
{
if(*arg[argc_i]=='-') continue;
// Copy command and args so that we can manipulate it.
char *cmd=(char*)CheckMalloc(malloc(strlen(arg[argc_i])+1));
strcpy(cmd,arg[argc_i]);
// Cut into command and arguments:
const int MAXARGS=16;
char *a[MAXARGS];
for(int j=0; j<MAXARGS; j++) a[j]=NULL;
char *first_arg=strchr(cmd,':');
int nargs=0;
if(first_arg)
{
do {
*(first_arg++)='\0';
if(nargs>=MAXARGS)
{
fprintf(stderr,"Too many arguments for command \"%s\" "
"(further args ignored)\n",cmd);
++errors;
break;
}
a[nargs++]=first_arg;
first_arg=strchr(first_arg,',');
} while(first_arg);
}
#if 0
// Debug:
printf("Command: <%s>",cmd);
for(int j=0; j<nargs; j++)
{ printf(" <%s>",a[j]); }
printf("\n");
#endif
if(!strcmp(cmd,"reset"))
{
fprintf(stderr,"Putting 8051 into reset.\n");
errors+=cycfx2.FX2Reset(/*running=*/0);
}
else if(!strcmp(cmd,"run"))
{
fprintf(stderr,"Putting 8051 out of reset.\n");
errors+=cycfx2.FX2Reset(/*running=*/1);
}
else if(!strcmp(cmd,"prg"))
{
// NOTE: We put the 8051 into reset prior to downloading but
// we won't put it out of reset afterwards.
fprintf(stderr,"Putting 8051 into reset.\n");
errors+=cycfx2.FX2Reset(/*running=*/0);
const char *file=a[0];
if(!file)
{ fprintf(stderr,"Command \"dl\" requires file to download.\n");
++errors; }
else
{
fprintf(stderr,"Programming 8051 using \"%s\".\n",file);
errors+=cycfx2.ProgramIHexFile(file);
}
}
else if(!strcmp(cmd,"delay"))
{
long delay=-1;
if(a[0] && *a[0])
{ delay=strtol(a[0],NULL,0); }
if(delay<0) delay=250;
fprintf(stderr,"Delay: %ld msec\n",delay);
usleep(delay*1000);
}
else if(!strcmp(cmd,"dram"))
{
int adr=0;
int len=1;
if(a[0] && *a[0])
{ adr=strtol(a[0],NULL,0); }
if(adr<0) adr=0;
if(a[1] && *a[1])
{ len=strtol(a[1],NULL,0); }
if(len<1) len=1;
if(len>1024*1024) len=1024*1024;
fprintf(stderr,"Dumping %u bytes of RAM at 0x%x:\n",len,adr);
unsigned char *buf=(unsigned char*)CheckMalloc(malloc(len));
memset(buf,0,len);
errors+=cycfx2.ReadRAM(adr,buf,len);
HexDumpBuffer(stdout,buf,len,/*with_ascii=*/1);
if(buf); free(buf);
}
else if(!strcmp(cmd,"set"))
{
int adr=-1;
int val=-1;
if(a[0] && *a[0]) { adr=strtol(a[0],NULL,0); }
if(a[1] && *a[1]) { val=strtol(a[1],NULL,0); }
if(adr<0 || val<0 || val>=256)
{ fprintf(stderr,"Command set: Illegal/missing address "
"and/or value.\n"); ++errors; }
else
{
fprintf(stderr,"Setting value at 0x%x to 0x%x\n",adr,val);
unsigned char cval=val;
errors+=cycfx2.WriteRAM(adr,&cval,1);
}
}
else if(!strcmp(cmd,"dbulk"))
{
int ep=-1;
int len=512;
int num=1;
char type='b';
if(a[0] && *a[0]) { ep=strtol(a[0],NULL,0); }
if(a[1] && *a[1]) { len=strtol(a[1],NULL,0); }
if(a[2] && *a[2]) { num=strtol(a[2],NULL,0); }
// If len<0, allow short reads.
if(len<0)
{ type='B'; len=-len; }
if(ep<0 || ep>=127 || len<=0 || len>32*1024*1024 || num<1)
{ fprintf(stderr,"Command dbulk: Illegal/missing "
"endpoint/length/number.\n"); ++errors; }
else
{
// IN endpoints have the bit 7 set.
ep|=0x80;
unsigned char *buf=(unsigned char*)CheckMalloc(malloc(len));
memset(buf,0,len);
for(int i=0; i<num; i++)
{
fprintf(stderr,"Reading %s%d bytes from EP adr 0x%02x\n",
type=='B' ? "<=" : "",len,ep);
int rv=cycfx2.BlockRead(ep,buf,len,type);
errors += rv<0 ? 1 : 0;
if(rv>0)
{ HexDumpBuffer(stdout,buf,rv,/*with_ascii=*/1); }
}
if(buf) free(buf);
}
}
else if(!strcmp(cmd,"sbulk"))
{
int ep=-1;
char type='b';
const char *str=a[1];
int len=str ? strlen(str) : 0;
if(a[0] && *a[0]) { ep=strtol(a[0],NULL,0); }
if(ep<0 || ep>=127)
{ fprintf(stderr,"Command sbulk: Illegal/missing "
"endpoint.\n"); ++errors; }
fprintf(stderr,"Sending %d bytes to EP adr 0x%02x\n",len,ep);
int rv=cycfx2.BlockWrite(ep,(const unsigned char*)str,len,type);
errors += rv<0 ? 1 : 0;
}
else if(!strcmp(cmd,"bench_bulk"))
{
int ep=-1;
int len=1024*1024;
int cs=65536;
if(a[0] && *a[0]) { ep=strtol(a[0],NULL,0); }
if(a[1] && *a[1]) { len=strtol(a[1],NULL,0); }
if(a[2] && *a[2]) { cs=strtol(a[2],NULL,0); }
// If len<0, allow short reads.
if(ep<0 || ep>=127 || len<=0 || len>32*1024*1024 ||
cs<1 || cs>32*1024*1024)
{ fprintf(stderr,"Command bench_bulk: Illegal/missing "
"endpoint/length/number.\n"); ++errors; }
else
{
// IN endpoints have the bit 7 set.
ep|=0x80;
int rv=cycfx2.BenchBlockRead(ep,len,cs,'b');
errors += rv ? 1 : 0;
}
}
else
{
fprintf(stderr,"Ignoring unknown command \"%s\".\n",cmd);
++errors;
}
if(cmd)
{ free(cmd); cmd=NULL; }
}
return(errors ? 1 : 0);
}
|