├── Makefile ├── WinTypes.h ├── busbasp.c ├── busbasp.h ├── cfi.h ├── ftd2xx.h ├── ftd2xx.h.old ├── ftd2xx_w.h ├── ftdixx.c ├── ftdixx.h ├── j-link.c ├── j-link.h ├── libusb.c ├── libusb.h ├── readme.txt ├── stmhid.c ├── stmhid.h ├── usb.h ├── usb_w.h ├── winbuild.bat ├── zjtag.c └── zjtag.h /Makefile: -------------------------------------------------------------------------------- 1 | 2 | #CFLAGS = -Wall -O2 -I. -I./lnx 3 | CFLAGS = -Wall -g -I. -I./lnx 4 | 5 | #gcc link to *.so & *.a libs 6 | #app running needs *.so.0 libs 7 | #for dynamic make, *.so libs placed in /usr/local/lib 8 | #for static make, *.a libs placed app-dir/libs 9 | #so need pre-install libusb and ftd2xx libs 10 | #add lib curses for getch() -lcurses 11 | 12 | DL_PATH = -Wl,-rpath /usr/local/lib 13 | L_LIBS = -L/usr/local/lib -lftd2xx -lusb -ldl -lrt 14 | S_LIBS = lnx/libftd2xx.a lnx/libusb.a -ldl -lpthread -lrt 15 | 16 | OBJS = zjtag.o busbasp.o ftdixx.o j-link.o libusb.o stmhid.o 17 | 18 | 19 | all: zjtag zjtag.a 20 | 21 | zjtag: $(OBJS) 22 | gcc $(CFLAGS) -o zjtag $(OBJS) $(L_LIBS) $(DL_PATH) 23 | 24 | zjtag.a: $(OBJS) 25 | gcc $(CFLAGS) -o zjtag.a $(OBJS) $(S_LIBS) $(DL_PATH) 26 | clean: 27 | rm -rf *.o *~ zjtag zjtag.a 28 | -------------------------------------------------------------------------------- /WinTypes.h: -------------------------------------------------------------------------------- 1 | #ifndef __WINDOWS_TYPES__ 2 | #define __WINDOWS_TYPES__ 3 | 4 | #define WINAPI 5 | 6 | #define MAX_NUM_DEVICES 50 7 | #include 8 | 9 | typedef unsigned int DWORD; 10 | typedef unsigned int ULONG; 11 | typedef unsigned short USHORT; 12 | typedef unsigned short SHORT; 13 | typedef unsigned char UCHAR; 14 | typedef unsigned short WORD; 15 | typedef unsigned char BYTE; 16 | typedef BYTE *LPBYTE; 17 | typedef unsigned int BOOL; 18 | typedef unsigned char BOOLEAN; 19 | typedef unsigned char CHAR; 20 | typedef BOOL *LPBOOL; 21 | typedef UCHAR *PUCHAR; 22 | typedef const char *LPCSTR; 23 | typedef char *PCHAR; 24 | typedef void *PVOID; 25 | typedef void *HANDLE; 26 | typedef unsigned int LONG; 27 | typedef int INT; 28 | typedef unsigned int UINT; 29 | typedef char *LPSTR; 30 | typedef char *LPTSTR; 31 | typedef const char *LPCTSTR; 32 | typedef DWORD *LPDWORD; 33 | typedef WORD *LPWORD; 34 | typedef ULONG *PULONG; 35 | typedef LONG *LPLONG; 36 | typedef PVOID LPVOID; 37 | typedef void VOID; 38 | typedef unsigned long long int ULONGLONG; 39 | 40 | typedef struct _OVERLAPPED { 41 | DWORD Internal; 42 | DWORD InternalHigh; 43 | DWORD Offset; 44 | DWORD OffsetHigh; 45 | HANDLE hEvent; 46 | } OVERLAPPED, *LPOVERLAPPED; 47 | 48 | typedef struct _SECURITY_ATTRIBUTES { 49 | DWORD nLength; 50 | LPVOID lpSecurityDescriptor; 51 | BOOL bInheritHandle; 52 | } SECURITY_ATTRIBUTES , *LPSECURITY_ATTRIBUTES; 53 | 54 | typedef struct timeval SYSTEMTIME; 55 | typedef struct timeval FILETIME; 56 | #ifndef TRUE 57 | #define TRUE 1 58 | #endif 59 | #ifndef FALSE 60 | #define FALSE 0 61 | #endif 62 | 63 | // 64 | // Modem Status Flags 65 | // 66 | #define MS_CTS_ON ((DWORD)0x0010) 67 | #define MS_DSR_ON ((DWORD)0x0020) 68 | #define MS_RING_ON ((DWORD)0x0040) 69 | #define MS_RLSD_ON ((DWORD)0x0080) 70 | 71 | // 72 | // Error Flags 73 | // 74 | #define CE_RXOVER 0x0001 // Receive Queue overflow 75 | #define CE_OVERRUN 0x0002 // Receive Overrun Error 76 | #define CE_RXPARITY 0x0004 // Receive Parity Error 77 | #define CE_FRAME 0x0008 // Receive Framing error 78 | #define CE_BREAK 0x0010 // Break Detected 79 | #define CE_TXFULL 0x0100 // TX Queue is full 80 | #define CE_PTO 0x0200 // LPTx Timeout 81 | #define CE_IOE 0x0400 // LPTx I/O Error 82 | #define CE_DNS 0x0800 // LPTx Device not selected 83 | #define CE_OOP 0x1000 // LPTx Out-Of-Paper 84 | #define CE_MODE 0x8000 // Requested mode unsupported 85 | 86 | // 87 | // Events 88 | // 89 | #define EV_RXCHAR 0x0001 // Any Character received 90 | #define EV_RXFLAG 0x0002 // Received certain character 91 | #define EV_TXEMPTY 0x0004 // Transmit Queue Empty 92 | #define EV_CTS 0x0008 // CTS changed state 93 | #define EV_DSR 0x0010 // DSR changed state 94 | #define EV_RLSD 0x0020 // RLSD changed state 95 | #define EV_BREAK 0x0040 // BREAK received 96 | #define EV_ERR 0x0080 // Line status error occurred 97 | #define EV_RING 0x0100 // Ring signal detected 98 | #define EV_PERR 0x0200 // Printer error occured 99 | #define EV_RX80FULL 0x0400 // Receive buffer is 80 percent full 100 | #define EV_EVENT1 0x0800 // Provider specific event 1 101 | #define EV_EVENT2 0x1000 // Provider specific event 2 102 | 103 | // 104 | // Escape Functions 105 | // 106 | #define SETXOFF 1 // Simulate XOFF received 107 | #define SETXON 2 // Simulate XON received 108 | #define SETRTS 3 // Set RTS high 109 | #define CLRRTS 4 // Set RTS low 110 | #define SETDTR 5 // Set DTR high 111 | #define CLRDTR 6 // Set DTR low 112 | #define RESETDEV 7 // Reset device if possible 113 | #define SETBREAK 8 // Set the device break line. 114 | #define CLRBREAK 9 // Clear the device break line. 115 | 116 | // 117 | // PURGE function flags. 118 | // 119 | #define PURGE_TXABORT 0x0001 // Kill the pending/current writes to the comm port. 120 | #define PURGE_RXABORT 0x0002 // Kill the pending/current reads to the comm port. 121 | #define PURGE_TXCLEAR 0x0004 // Kill the transmit queue if there. 122 | #define PURGE_RXCLEAR 0x0008 // Kill the typeahead buffer if there. 123 | 124 | #ifndef INVALID_HANDLE_VALUE 125 | #define INVALID_HANDLE_VALUE 0xFFFFFFFF 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /busbasp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 17/9/2010 Brjtag rev1.9m 3 | * 4 | * 5 | * Brjtag application for HID-BRJTAG v1.xx MCU firmware. 6 | * fw v1.0 only implemented USB EP0 7 | * 8 | * Copyright (C) 2010 Hugebird 9 | * 10 | * This code is covered by the GPL v2. 11 | * 12 | * 13 | */ 14 | 15 | 16 | 17 | #if ( defined(_MSC_VER) ) 18 | #define WINDOWS_VERSION 19 | #endif 20 | 21 | #define BUSBASP 22 | 23 | 24 | #include "zjtag.h" 25 | #include "busbasp.h" 26 | #include "libusb.h" 27 | 28 | 29 | #define DEBUGFLUSH 0 30 | #define DEBUGTXBUF 0 31 | #define DEBUGRXBUF 0 32 | #define DEBUGINBUF 0 33 | #define DEBUGOTBUF 0 34 | #define DEBUGBLK 0 35 | #define DEBUGMSG 0 36 | #define DEBUGFETCH 0 // check dma fetch 37 | #define DEBUGTRAINING 0 38 | 39 | #if (DEBUGFLUSH || DEBUGTXBUF ||DEBUGRXBUF || DEBUGINBUF || DEBUGOTBUF \ 40 | || DEBUGBLK || DEBUGMSG || DEBUGFETCH || DEBUGTRAINING ) 41 | #define DBG(x) printf x 42 | #else 43 | #define DBG(x) 44 | #endif 45 | 46 | 47 | static BYTE cmdbuf[250]; 48 | static int gIndex = 0; 49 | 50 | static DWORD tmpbuf[100]; 51 | 52 | static int lowspeed = 0; 53 | static int skip = 0; 54 | 55 | #define BigEndian (endian == __BE) 56 | 57 | 58 | 59 | ///////////////////////////////extern global//////////////////////////////////////////////////// 60 | extern int instruction_length; 61 | extern int endian; 62 | extern DWORD FLASH_MEMORY_START; 63 | extern DWORD cmd_type; 64 | extern int safemode; 65 | extern int showgpio; 66 | extern int silent_mode; 67 | extern int bypass; 68 | extern int flsPrgTimeout; 69 | extern int ejtag_speed; 70 | 71 | extern DWORD LL1; 72 | extern BYTE LL2; 73 | extern DWORD LL3; 74 | extern DWORD LL4; 75 | extern DWORD LL5; 76 | extern DWORD LL6; 77 | extern DWORD LL7; 78 | extern DWORD LL8; 79 | extern DWORD LL9; 80 | 81 | extern DWORD USBID; 82 | 83 | 84 | static void u_getconfig(void) 85 | { 86 | // performance fine tune parameters 87 | //L1: clk 88 | //L2: FT2232 USB read latency>>> 2 ~ 255ms, default 2ms 89 | //L3: DMA xfer Polling timeout>>> 0 ~ 48 us, defualt 1 us 90 | //L4: FLASH Write Polling timeout>>> 1 ~ 127 us, defualt 16 us 91 | //L5: USB Buffer size 92 | //L6: Allowed DMA Bulk xfer count>>> 93 | //L7: Block Size on FLASH DWORD Read in x32Bits Mode>>> 94 | //L8: Block Size on FLASH DWORD Write in x16Bits Mode>>> 95 | //L9: default profile>>> 96 | 97 | if(LL4==0xFFFF) LL4 = FLASH_POLLING; 98 | if(LL1>5) LL1 =5; //limit to 5 cycle delay max 99 | if(!LL7 || LL7 >16) LL7 = 16; 100 | if(!LL8 || LL8 >8) LL8 = 8; 101 | if(LL9 == 1) //safe mode 102 | { LL1 = 1; 103 | LL7 = 16; 104 | LL8 = 4; 105 | LL4 = 128; 106 | } 107 | else if(LL9 == 2) //risk read mode 108 | { 109 | LL1 = 0; 110 | LL7 = 16; 111 | LL8 = 8; 112 | } 113 | 114 | if(!USBID) USBID = 0x16C005DF; 115 | // if(!USBID) USBID = 0x04835750; 116 | // printf(" LL1[%d],LL2[%d],LL3[%d],LL4[%d],LL5[%d],LL6[%d],LL7[%d],LL8[%d],LL9[%d]\n",LL1,LL2,LL3,LL4,LL5,LL6,LL7,LL8,LL9); 117 | 118 | } 119 | 120 | static void u_set_speed(int cy) 121 | { 122 | 123 | int cnt = 0; 124 | 125 | cnt = libusb_msg_read(REQ_MCU_SETSPD, cy, 0, cmdbuf, 8); 126 | 127 | if(cnt!=1 || cmdbuf[0] ) 128 | printf("MCU set speed error!\n"); 129 | } 130 | 131 | static void u_get_mcuversion(void) 132 | { 133 | 134 | int cnt = 0; 135 | 136 | cnt = libusb_msg_read(REQ_MCU_HWVER, 0, 0, cmdbuf, 8); 137 | 138 | // printf(" get HW ver, reply msg len = %d\n", cnt); 139 | 140 | if(cnt == 4 && cmdbuf[0] == 'B' && cmdbuf[1] == 'r') 141 | printf("HID-Brjtag MCU ROM version: %d.%02d on USBASP hardware!\n",cmdbuf[3],cmdbuf[2]); 142 | else 143 | printf("HID-Brjtag MCU ROM version not fetched!!\n"); 144 | } 145 | 146 | static int u_get_mcustate(void) 147 | { 148 | 149 | int cnt = 0; 150 | 151 | cnt = libusb_msg_read(REQ_MCU_GetSTT, 0, 0, cmdbuf, 8); 152 | 153 | if(cnt==1 ) 154 | { printf("MCU st = %d\n",cmdbuf[0]); 155 | return (cmdbuf[0] | 0x80); 156 | } 157 | else 158 | { 159 | printf("MCU st fetch error\n"); 160 | return 0; 161 | } 162 | } 163 | 164 | static void fill_cable_prop(void *p) 165 | { 166 | cable_prop_type *pcbl = (cable_prop_type*)p; 167 | 168 | pcbl->feature = 0; 169 | pcbl->close = uclose; 170 | pcbl->test_reset = utest_reset; 171 | pcbl->det_instr = udet_instr; 172 | pcbl->set_instr = uset_instr; 173 | pcbl->ReadWriteData = uReadWriteData; 174 | pcbl->ReadData = uReadData; 175 | pcbl->WriteData = uWriteData; 176 | 177 | pcbl->feature |= CBL_DMA_RD|CBL_DMA_WR; 178 | pcbl->ejtag_dma_read_x = uejtag_dma_read_x; 179 | pcbl->ejtag_dma_write_x = uejtag_dma_write_x; 180 | 181 | pcbl->ejtag_pracc_read_x = 0; 182 | pcbl->ejtag_pracc_write_x = 0; 183 | 184 | pcbl->sflash_blkread = u_sflash_blkread; 185 | pcbl->sflash_blkwrite = u_sflash_blkwrite; 186 | } 187 | 188 | ////////////////////// iNIT USB device ///////////////////////////////////////////////////// 189 | int uinit(void *p) 190 | { 191 | int ii=1; 192 | int vref; 193 | DWORD cap, tmp; 194 | char vname[]="Brjtag"; 195 | char pname[]="HID-Brjtag"; 196 | int oo; 197 | 198 | unsigned char buf[255]; 199 | int cmd; 200 | int cnt; 201 | DWORD * ptr; 202 | 203 | fill_cable_prop(p); 204 | 205 | u_getconfig(); 206 | 207 | libusb_open(USBID, 0 ,0, 0, vname,NULL,NULL); 208 | 209 | u_get_mcuversion(); 210 | 211 | u_set_speed(LL1); 212 | 213 | return 1; 214 | 215 | } 216 | 217 | 218 | void uclose(void) 219 | { 220 | libusb_close(); 221 | } 222 | 223 | 224 | void utest_reset(void) 225 | { 226 | 227 | int cnt = 0; 228 | cnt = libusb_msg_read(REQ_MCU_RESET, 0, 0, cmdbuf, 8); 229 | 230 | if(cnt!=1 || cmdbuf[0] ) 231 | printf("MCU reset error!\n"); 232 | } 233 | 234 | 235 | 236 | 237 | DWORD udet_instr(void) 238 | { 239 | 240 | int cnt = 0; 241 | DWORD * dd; 242 | 243 | 244 | cmdbuf[0] = 0x03; //det ir 245 | cmdbuf[1] = 32; //32 246 | dd = (DWORD*)(cmdbuf +2); 247 | *dd = 0xFFFFFFFF; 248 | 249 | cnt = libusb_msg_write(REQ_MCU_CMDSEQ, 0, 0, cmdbuf, 6); 250 | 251 | if(cnt !=6) 252 | { 253 | printf(" cmd [detir] write to usb error! len = %d\n", cnt); 254 | return 0; 255 | } 256 | 257 | 258 | cnt = libusb_msg_read(REQ_MCU_GetDAT, 0, 0, cmdbuf, 4); 259 | 260 | if(cnt !=4) 261 | {printf(" cmd [detir] read from usb error! len = %d\n", cnt); 262 | return 0; 263 | } 264 | 265 | dd = (DWORD*)cmdbuf; 266 | // printf(" det ir 0x%08X\n",*dd); 267 | return *dd; 268 | 269 | } 270 | 271 | 272 | DWORD uset_instr( DWORD instr) 273 | { 274 | 275 | int cnt = 0; 276 | DWORD* dd; 277 | int dlen; 278 | 279 | //getmcust(); 280 | 281 | cmdbuf[0] = CMD_TAP_SETIR; //det ir 282 | cmdbuf[1] = instruction_length; //32 283 | dlen = (cmdbuf[1]+7)>>3; 284 | dd = (DWORD*)(cmdbuf +2); 285 | *dd = instr; 286 | 287 | 288 | cnt = libusb_msg_write(REQ_MCU_CMDSEQ, 0, 0, cmdbuf, 2+dlen); 289 | 290 | if(cnt != (2+dlen)) 291 | { 292 | printf(" cmd [setir] write to usb error! len = %d\n", cnt); 293 | } 294 | // ussleep(200); 295 | 296 | cnt = libusb_msg_read(REQ_MCU_GetDAT, 0, 0, cmdbuf, 4); 297 | 298 | if(cnt !=0) 299 | {printf(" cmd [setir] read from usb error! len = %d\n", cnt); 300 | } 301 | 302 | dd = (DWORD*)cmdbuf; 303 | // printf(" set ir 0x%08X\n",*dd); 304 | return *dd; 305 | 306 | } 307 | 308 | 309 | ////////////////////////////////////////////////////////////////////////////////////// 310 | // RW 32bits 311 | DWORD uReadWriteData(DWORD data) 312 | { 313 | int cnt = 0; 314 | DWORD* dd; 315 | 316 | 317 | cmdbuf[0] = CMD_TAP_DR32; //det ir 318 | dd = (DWORD*)(cmdbuf +1); 319 | *dd = data; 320 | 321 | 322 | cnt = libusb_msg_write(REQ_MCU_CMDSEQ, 0, 0, cmdbuf, 5); 323 | 324 | if(cnt !=5) 325 | { 326 | printf(" cmd [rwdata] write to usb error! len = %d\n", cnt); 327 | return 0; 328 | } 329 | 330 | // ussleep(500); 331 | 332 | cnt = libusb_msg_read(REQ_MCU_GetDAT, 0, 0, cmdbuf, 4); 333 | 334 | if(cnt !=4) 335 | {printf(" cmd [rwdata] read from usb error! len = %d\n", cnt); 336 | return 0; 337 | } 338 | 339 | dd = (DWORD*)cmdbuf; 340 | // printf(" RW data 0x%08X\n",*dd); 341 | return *dd; 342 | } 343 | 344 | // RO 32bits 345 | DWORD uReadData(void) 346 | { 347 | return uReadWriteData(0); 348 | } 349 | 350 | // WO 32bits 351 | void uWriteData(DWORD data) 352 | { 353 | 354 | uReadWriteData(data); 355 | 356 | } 357 | 358 | 359 | 360 | /////////////////////////////////////////////////////////////////////////// 361 | 362 | DWORD uejtag_dma_read_x(DWORD addr, int mode) 363 | { 364 | DWORD data; 365 | int k; 366 | 367 | int cnt = 0; 368 | DWORD* dd; 369 | 370 | cmdbuf[0] = CMD_TAP_DMAREAD; 371 | cmdbuf[1] = (mode&0x03); //| (instruction_length <<4); 372 | dd = (DWORD*)(cmdbuf +2); 373 | *dd = addr; 374 | 375 | // for(k =0; k<6;k++) printf("rdbuf [%d] = 0x%02X\n",k, cmdbuf[k]); 376 | 377 | cnt = libusb_msg_write(REQ_MCU_CMDSEQ, 0, 0, cmdbuf, 6); 378 | 379 | if(cnt !=6) 380 | { 381 | printf(" cmd [dmard] write to usb error! len = %d\n", cnt); 382 | return 0xFFFFFFFF; 383 | } 384 | 385 | 386 | // ussleep(2000); 387 | cnt = libusb_msg_read(REQ_MCU_GetDAT, 0, 0, cmdbuf, 4); 388 | 389 | if(cnt !=4) 390 | {printf(" cmd [dmard] read from usb error! len = %d\n", cnt); 391 | return 0xFFFFFFFF; 392 | } 393 | data = *(DWORD*)(cmdbuf); 394 | 395 | // dd =(DWORD*) (cmdbuf); 396 | // data = *dd; 397 | 398 | // printf("RD return data %08x\n", data); 399 | 400 | 401 | switch(mode) 402 | { 403 | case MIPS_WORD: 404 | break; 405 | 406 | case MIPS_HALFWORD: 407 | k = addr & 0x2; 408 | if (BigEndian) 409 | data = (data >> (8*(2-k))) & 0xffff; //low 16 at high 410 | else //little 411 | data = (data >> (8*k)) & 0xffff; //low 16 at low 412 | break; 413 | 414 | case MIPS_BYTE: 415 | k = addr & 0x3; 416 | if (BigEndian) 417 | data = (data >> (8*(3-k))) & 0xff; //low 8 at high 418 | else //little 419 | data = (data >> (8*k)) & 0xff; //low 8 at low 420 | break; 421 | 422 | default: //not supported mode 423 | data = 0xFFFFFFFF; 424 | break; 425 | } 426 | // printf("return data %08x\n", data); 427 | return(data); 428 | } 429 | 430 | 431 | void uejtag_dma_write_x(DWORD addr, DWORD data, int mode) 432 | { 433 | 434 | int cnt = 0; 435 | DWORD* dd; 436 | 437 | cmdbuf[0] = CMD_TAP_DMAWRITE; 438 | cmdbuf[1] = (mode&0x03); // | (instruction_length <<4); 439 | dd = (DWORD*)(cmdbuf +2); 440 | *dd = addr; 441 | dd = (DWORD*)(cmdbuf +6); 442 | *dd = data; 443 | 444 | cnt = libusb_msg_write(REQ_MCU_CMDSEQ, 0, 0, cmdbuf, 10); 445 | 446 | if(cnt !=10) 447 | { 448 | printf(" cmd [dmawr] write to usb error! len = %d\n", cnt); 449 | return; 450 | } 451 | 452 | // mssleep(50); 453 | cnt = libusb_msg_read(REQ_MCU_GetDAT, 0, 0, cmdbuf, 4); 454 | 455 | if(cnt !=0) 456 | {printf(" cmd [dmawr] read from usb error! len = %d\n", cnt); 457 | return; 458 | } 459 | 460 | 461 | } 462 | 463 | 464 | //////////////////////////////////////////////////////////////////// 465 | 466 | int u_sflash_blkread(DWORD Inaddr, DWORD* pbuff, int len) 467 | { 468 | int ii; 469 | DWORD data; 470 | int k; 471 | int cnt = 0; 472 | DWORD* dd; 473 | 474 | len = (len > 16) ? 16 : len; 475 | 476 | 477 | cmdbuf[0] = CMD_TAP_DMABLKRD32; 478 | cmdbuf[1] = MIPS_WORD; // | (instruction_length <<4); 479 | dd = (DWORD*)(cmdbuf +2); 480 | *dd = Inaddr; 481 | cmdbuf[6] = len; 482 | 483 | // for(k =0; k<7;k++) printf("rdbuf [%d] = 0x%02X\n",k, cmdbuf[k]); 484 | 485 | cnt = libusb_msg_write(REQ_MCU_CMDSEQ, 0, 0, cmdbuf, 7); 486 | 487 | if(cnt !=7) 488 | { 489 | printf(" cmd [dmablkrd] write to usb error! len = %d\n", cnt); 490 | return 0; 491 | } 492 | 493 | 494 | // mssleep(20); 495 | 496 | cnt = libusb_msg_read(REQ_MCU_GetDAT, 0, 0, cmdbuf, 4*len); 497 | 498 | if(cnt <4*len) 499 | {printf(" cmd [dmablkrd] read from usb error! len = %d\n", cnt); 500 | return 0; 501 | } 502 | // for(k =0; k<4;k++) printf("rdbuf [%d] = 0x%02X\n",k, cmdbuf[k]); 503 | 504 | for( ii =0; iiilen) len = ilen; 531 | 532 | gIndex = 0; //buffer clean 533 | xfer_op = flpg_x8?MIPS_BYTE:MIPS_HALFWORD; 534 | ilen = flpg_x8?4:2; 535 | 536 | // file cmd head 537 | cmdbuf[0] = CMD_TAP_FLSHBLKWR; 538 | cmdbuf[1] = xfer_op ; //| (instruction_length <<4); 539 | dd = (DWORD*)(cmdbuf +2); 540 | *dd = FLASH_MEMORY_START; 541 | cmdbuf[6] = len*ilen; 542 | dd = (DWORD*)(cmdbuf +7); 543 | *dd = Inaddr; 544 | gIndex +=11; 545 | 546 | for(ii = 0; ii < len; ii ++) 547 | { 548 | data = pbuff[ii]; 549 | addr = Inaddr + 4*ii; 550 | 551 | // printf("write data at addr[%08X] = %08X\n", addr,data); 552 | 553 | if (flpg_x8) 554 | { 555 | u_buf_write_x8((addr& (~3)), data); 556 | u_buf_delayus(LL4); 557 | 558 | u_buf_write_x8((addr & (~3))+ 1, data); 559 | u_buf_delayus(LL4); 560 | 561 | u_buf_write_x8((addr & (~3))+ 2, data); 562 | u_buf_delayus(LL4); 563 | 564 | u_buf_write_x8((addr & (~3))+ 3, data); 565 | u_buf_delayus(LL4); 566 | 567 | } else { 568 | 569 | u_buf_write_x16((addr& (~3)), data); 570 | u_buf_delayus(LL4); 571 | 572 | u_buf_write_x16((addr& (~3))+2, data); 573 | u_buf_delayus(LL4); 574 | } 575 | 576 | } 577 | 578 | if ( (cmd_type != CMD_TYPE_AMD) || !bypass ) 579 | { 580 | // fetch polling data 581 | cmdbuf[gIndex] = CMD_TAP_DMABLKRD32; 582 | cmdbuf[gIndex+1] = MIPS_WORD; // | (instruction_length <<4); 583 | dd = (DWORD*)(cmdbuf +gIndex +2); 584 | *dd = Inaddr; 585 | cmdbuf[gIndex+6] = len; 586 | gIndex +=7; 587 | 588 | } 589 | 590 | // for(k =0; k>1; 639 | 640 | if (BigEndian) 641 | { 642 | odata = rev_endian(data) & BEMASK16(k); 643 | ldata = odata >>(16*(1-k)); 644 | } 645 | else 646 | { 647 | odata = data & LEMASK16(k); 648 | ldata = odata >>(16*k); 649 | } 650 | 651 | if( ldata == 0xffff) {skip =1;return;} // no need to program 652 | switch( cmd_type ) 653 | { 654 | case CMD_TYPE_AMD: 655 | if (!bypass) 656 | { 657 | u_buf_write_I(AMD16_I1); 658 | u_buf_write_I(AMD16_I2); 659 | } 660 | u_buf_write_I(AMD16_I3); 661 | break; 662 | case CMD_TYPE_SST: 663 | u_buf_write_I(SST16_I1); 664 | u_buf_write_I(SST16_I2); 665 | u_buf_write_I(SST16_I3); 666 | break; 667 | case CMD_TYPE_BCS: 668 | case CMD_TYPE_SCS: 669 | default: 670 | u_buf_write_I(INTL16_I1); 671 | u_buf_write_I(INTL16_I2); 672 | } 673 | 674 | u_buf_write(odata); 675 | } 676 | 677 | #define LEMASK8(k) (0xff<<(8*(k))) 678 | #define BEMASK8(k) (0xff<<(8*(3-(k)))) 679 | 680 | static void u_buf_write_x8(DWORD addr, DWORD data) 681 | { 682 | int k; 683 | DWORD odata,ldata; 684 | 685 | k = addr & 0x3; 686 | 687 | ldata = (data >> (8*k))&0xFF; 688 | odata = ldata | (ldata <<8); 689 | odata |= (odata<<16); 690 | 691 | // printf(" I am in x8, addr, data = %08x, %08x, %08x\n", addr, data,ldata); 692 | if( ldata == 0xff) {skip =1;return;} // no need to program 693 | 694 | switch( cmd_type ) 695 | { 696 | case CMD_TYPE_AMD: 697 | if (!bypass) 698 | { 699 | u_buf_write_I(AMD8_I1); 700 | u_buf_write_I(AMD8_I2); 701 | } 702 | u_buf_write_I(AMD8_I3); 703 | break; 704 | case CMD_TYPE_SST: 705 | return; //SST 39 don't support x8 706 | break; 707 | case CMD_TYPE_BCS: 708 | case CMD_TYPE_SCS: 709 | default: 710 | u_buf_write_I(INTL8_I1); 711 | u_buf_write_I(INTL8_I2); 712 | } 713 | 714 | u_buf_write(odata); 715 | 716 | } 717 | 718 | static void u_buf_write(DWORD data) 719 | { 720 | DWORD *dd; 721 | 722 | cmdbuf[gIndex +0] = 0; 723 | dd = (DWORD*)(cmdbuf + gIndex +1); 724 | *dd = data; 725 | 726 | gIndex +=5; 727 | 728 | } 729 | 730 | static void u_buf_write_I(DWORD id) 731 | { 732 | 733 | cmdbuf[gIndex+0] = (BYTE)(0x80|id); 734 | gIndex ++; 735 | 736 | } 737 | 738 | 739 | static void u_buf_delayus(DWORD us) 740 | { 741 | 742 | cmdbuf[gIndex +0] = 0xFF; 743 | if(skip) 744 | cmdbuf[gIndex +1] = 0; 745 | else 746 | cmdbuf[gIndex +1] = (BYTE)(us); 747 | gIndex +=2; 748 | skip = 0; 749 | 750 | } 751 | 752 | static void u_bufcmd_delayus(DWORD us) 753 | { 754 | 755 | cmdbuf[gIndex +0] = CMD_TAP_DELAY; 756 | cmdbuf[gIndex +1] = (BYTE)(us); 757 | gIndex +=2; 758 | 759 | } 760 | -------------------------------------------------------------------------------- /busbasp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 17/9/2010 Brjtag rev1.9m 3 | * 4 | * 5 | * Brjtag application for HID-BRJTAG v1.xx MCU firmware. 6 | * Copyright (C) 2010 Hugebird 7 | * 8 | * This code is covered by the GPL v2. 9 | * 10 | * 11 | * 12 | */ 13 | 14 | #if ( defined(_MSC_VER) ) 15 | #define WINDOWS_VERSION 16 | #endif 17 | 18 | #ifdef WINDOWS_VERSION 19 | #include "windows.h" 20 | 21 | #else 22 | 23 | #ifndef MY_TYPE 24 | #define MY_TYPE 25 | typedef unsigned uint32_t DWORD; 26 | typedef unsigned uint16_t WORD; 27 | typedef unsigned char BYTE; 28 | typedef int bool; 29 | #endif 30 | 31 | #endif 32 | 33 | #ifdef BUSBASP 34 | 35 | typedef enum _JtagStates { 36 | JTAG_TLR = 0, 37 | JTAG_RTI = 1, 38 | JTAG_PDR = 2, 39 | JTAG_PIR = 3, 40 | JTAG_SDR = 4, 41 | JTAG_SIR = 5, 42 | Undefined = 6} JtagStates; 43 | 44 | #define MAX_JTAG_STATES 6 45 | 46 | static const BYTE JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 47 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 48 | /* from TLR 0-> */ {'\x01', '\x00', '\x0A', '\x16', '\x02', '\x06'}, 49 | /* from RTI 1-> */ {'\x07', '\x00', '\x05', '\x0B', '\x01', '\x03'}, 50 | /* from PDR 2-> */ {'\x1F', '\x03', '\x00', '\x2F', '\x01', '\x0F'}, 51 | /* from PIR 3-> */ {'\x1F', '\x03', '\x17', '\x00', '\x07', '\x01'}, 52 | /* from SDR 4-> */ {'\x1F', '\x03', '\x01', '\x2F', '\x00', '\x0F'}, 53 | /* from SIR 5-> */ {'\x1F', '\x03', '\x17', '\x01', '\x07', '\x00'} }; 54 | 55 | static const BYTE TMSCLK_JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 56 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 57 | /* from TLR 0-> */ {1, 1, 5, 6, 4, 5}, 58 | /* from RTI 1-> */ {3, 1, 4, 5, 3, 4}, 59 | /* from PDR 2-> */ {5, 3, 1, 7, 2, 6}, 60 | /* from PIR 3-> */ {5, 3, 6, 1, 5, 2}, 61 | /* from SDR 4-> */ {5, 3, 2, 7, 0, 6}, 62 | /* from SIR 5-> */ {5, 3, 6, 2, 5, 0} }; 63 | 64 | #define JSTPATH_EXR_TO_RTI 0x01 65 | #define JSTPLEN_EXR_TO_RTI 4 66 | 67 | // flsh cmd id 68 | #define AMD16_I1 0 69 | #define AMD16_I2 1 70 | #define AMD16_I3 2 71 | #define AMD8_I1 3 72 | #define AMD8_I2 4 73 | #define AMD8_I3 5 74 | #define SST16_I1 6 75 | #define SST16_I2 7 76 | #define SST16_I3 8 77 | #define INTL16_I1 9 78 | #define INTL16_I2 10 79 | #define INTL8_I1 11 80 | #define INTL8_I2 12 81 | 82 | 83 | //--------------------------------------------------------- 84 | // _____ _______ _______ 85 | // | DUT |___________|TAP/MCU|__________|PC/HOST| 86 | // ----- JTAG ------- USB ------- 87 | // BCM MCU FW BRJTAG 88 | //--------------------------------------------------------- 89 | 90 | /* MCU Processing State Machine */ 91 | #define ST_MCU_IDLE 0x01 //ideal, can accept any cmd 92 | #define ST_MCU_XFER 0x02 //on USB data xfer, not accept any new cmd 93 | #define ST_MCU_BUSY 0x03 //on JTAG process 94 | #define ST_MCU_RDY 0x04 //DMA Read/write complete, data reply buffer ready 95 | 96 | // idle -> xfer -> busy -> ready -> xfer -> idle 97 | 98 | 99 | /* Host <-> MCU COMMAND ID rq->bRequest */ 100 | #define REQ_MCU_HWVER 0x11 //EPIN,get MCU firmware version 101 | #define REQ_MCU_RESET 0x12 //EPIN,reset tap to idle 102 | #define REQ_MCU_SETSPD 0x13 //EPIN,set jtag speed 103 | #define REQ_MCU_GetSTT 0x14 //EPIN,get MCU current state 104 | #define REQ_MCU_CMDSEQ 0x21 //EPOUT,send cmd sequence to mcu 105 | #define REQ_MCU_BITSEQ 0x22 //EPOUT,send bit sequence to mcu 106 | #define REQ_MCU_GetDAT 0x23 //EPIN,get back sequence response data 107 | 108 | //cmd sequence format (total length <= 201 bytes) 109 | //[cmd id 0][cmd data 0][cmd id 1][cmd data 1] ... 110 | // 111 | // Command ID: 112 | #define CMD_TAP_DELAY 0x01 //pause DUT <-> TAP 113 | //In: [ 0:id ][1 : us] out:none 114 | #define CMD_TAP_CLKIO 0x02 //clock shift in/out 115 | //In: [0: id][1:bit len][2,3,4,5] out:[0,..., (len+7)>>3] 116 | #define CMD_TAP_DETIR 0x03 //IR shift/scan a instruction and return 117 | //In: [0:id][1:ir bit len][2,3,4,5 ir data] out:[0,1,2,3] 118 | #define CMD_TAP_SETIR 0x04 //IR shift/scan a instruction 119 | //In: [0:id][1:ir bit len][2,3,4,5 ir data] out:none 120 | #define CMD_TAP_DR32 0x05 //DR shift/scan a 32b Data 121 | //In: [0:id][1,2,3,4 ir data] out:[0,1,2,3] 122 | #define CMD_TAP_DMAREAD 0x06 //DMA Read a 32b data 123 | //In: [0:1d][1:type][2,3,4,5:ADDR] out:[0,1,2,3] 124 | #define CMD_TAP_DMAWRITE 0x07 //DMA write a 32b data 125 | //In: [0:1d][1:type][2,3,4,5:ADDR][6,7,8,9:data] 126 | #define CMD_TAP_DMABLKRD32 0x08 //DMA Read a data block 127 | //In: [0:1d][1:type][2,3,4,5:ADDR][6:len, mode in type] out:[len in data mode] 128 | #define CMD_TAP_FLSHBLKWR 0x09 //flash blk write 129 | //In: ... 130 | //: <[0:1d][1:type][2,3,4,5:BASE ADDR][6:len][7,8,9,10:WR Target ADDR]> 131 | //:<0:I1><1:I2><2:I3><3:D><4,5,6,7:data><8:L><9:us> 132 | //I: 0x80~0x8F, flash unlock cmd, I1~I3 for AMD is 0xAA, 0x55, 0xA0. 133 | // if open bypass mode, only I3 used 0xA0 at datax seq offset 0 134 | //D: 0x00, follow 4 bytes are data, whatever the write type(x8,x16) 135 | //L: 0xFF, means flash program waiting, 136 | // waiting time is give in follow us bytes 137 | //for a word program, maximum write step is 5, (I1,I2,I3,D,L) 138 | //for bypass mode, write step is 3, (I3,D,L) 139 | // 140 | //: None 141 | // 142 | // 143 | //type: 144 | #define CMDT_MOD 0x03 //op type mask, word, halfw, byte,tripw 145 | #define CMDT_END (1<<2) //endian mask 146 | #define CMDT_DMA (1<<3) //1:DMA 2:Pracc 147 | #define CMDT_IRLEN(x) ((x &0x0F)<<4) //ir len mask 148 | // 149 | //**************************************************************************** 150 | //**************************************************************************** 151 | //Bit sequence scan: bit by bit scan TDI/TMS to DUT and return TDO(len<=800 bits) 152 | //<=202 bytes) 153 | // 154 | //In Byte: [ 0,1 ] [ 2,3,4...xxx][xxx+1,...,2xxx-1] 155 | // bit LEN TDI TMS 156 | // 157 | //Out Byte: [0,1,2,3,4...xxx-2] 158 | // TDO 159 | //**************************************************************************** 160 | 161 | // Speed *KHz 162 | #define JL_SPEED_12M 12000 163 | #define JL_SPEED_1M 1000 164 | 165 | // usb endpoint 166 | #define JL_EP1 0x81 //for read, v3,v4 also use for write 167 | #define JL_EP2 0x02 //for write, ver >=5 168 | 169 | // USB Output/Input Buffer byte length 170 | #define MAX_NUM_BYTES_USB_BUFFER 4096 171 | #define JL_MAX_BUFFER_SIZE 3000 //v8 has enough room to keep 3000bytes, eg total out buffer 3000*2+4 = 6004 bytes 172 | // speced in/out total buffer len is 2048bytes, total 4+1000*2 = 2004 bytes. 173 | // tms_delay sequence need limit to 10 bytes. 174 | 175 | // TX/RX data buffer 176 | #define MAX_DATA_BUFFER_SIZE 65536 177 | #define MAX_DATA_BLOCK_SIZE 20000 178 | 179 | 180 | #define MAX_DMA_BLOCK_SIZE 400 //Max L9 DMA xfer block size for 65536 bytes buffer. 181 | #define DMA_BLOCK_SIZE 256 //L6 defult DMA xfer block size 182 | #define DMA_POLLING 2 //L3 defult polling retry time on DMA xfering 183 | #define FLASH_POLLING 16 //L4 defult polling retry time on flash writing 184 | 185 | 186 | // port scan data type 187 | #define TAP_RD 0x80 // need return data 188 | #define TAP_IR 1 189 | #define TAP_DATA 2 190 | #define TAP_TMS_DELAY 3 191 | #define TAP_TMS_MOVE 4 192 | 193 | // DMA xfer type 194 | #define XFER_TX 1 // blcok xfer, add cmd to queue, internal flag 195 | #define XFER_RX 2 // block xfer, get data from queue, internal flag 196 | 197 | #define XFER_ADD 3 // add DMA xfer block to queue, for caller 198 | #define XFER_QRY 4 // find next read data block,fetch out, for caller 199 | #define XFER_NOOP 5 // no operate, return free dma xfer block count 200 | 201 | #define DMA_WR 1 // dma write operate 202 | #define DMA_RD 2 // dma read operate 203 | 204 | 205 | 206 | //--------------private------------------------------------------- 207 | 208 | 209 | static void u_getconfig(void); 210 | static int u_get_mcustate(void); 211 | static void u_get_mcuversion(void); 212 | static void u_set_speed(int cy); 213 | static void u_buf_write_x16(DWORD addr, DWORD data); 214 | static void u_buf_write_x8(DWORD addr, DWORD data); 215 | static void u_buf_delayus(DWORD us); 216 | static void u_buf_write(DWORD data); 217 | static void u_bufcmd_delayus(DWORD us); 218 | static void u_buf_write_I(DWORD id); 219 | static void fill_cable_prop(void *p); 220 | 221 | //--------------------pub------------------------------------- 222 | int uinit(void *); 223 | void uclose(void); 224 | void utest_reset(void); 225 | DWORD udet_instr(void); 226 | DWORD uset_instr( DWORD instr); 227 | DWORD uReadWriteData(DWORD data); 228 | DWORD uReadData(void); 229 | void uWriteData(DWORD data); 230 | DWORD uejtag_dma_read_x(DWORD addr, int mode); 231 | void uejtag_dma_write_x(DWORD addr, DWORD data, int mode); 232 | int u_sflash_blkread(DWORD Inaddr, DWORD* pbuff, int len); 233 | int u_sflash_blkwrite(DWORD Inaddr, DWORD* pbuff, int len, int flpg_x8); 234 | 235 | #endif 236 | 237 | 238 | 239 | 240 | 241 | 242 | -------------------------------------------------------------------------------- /cfi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 11/7/2009 rev1.8c 3 | * 4 | * Common Flash Interface suport data 5 | * Refer to Kernel mtd/cfi.h 6 | * 7 | * This code is covered by the GPL. 8 | * This program is copyright (C) 2009 hugebird 9 | */ 10 | 11 | 12 | 13 | 14 | #define CFI_QRY_OK 1 15 | #define CFI_QRY_ERR 0 16 | 17 | 18 | /* define manufacture ID */ 19 | #define MFR_AMD 0x0001 //AMD Spansion 20 | #define MFR_FUJITSU 0x0004 21 | #define MFR_NEC 0x0010 22 | #define MFR_EON_LEG 0x001C 23 | #define MFR_ATMEL 0x001F 24 | #define MFR_ST 0x0020 //ST Nymonyx 25 | #define MFR_EON 0x007F 26 | #define MFR_INTEL 0x0089 27 | #define MFR_TOSHIBA 0x0098 28 | #define MFR_PMC 0x009D 29 | #define MFR_HYUNDAI 0x00AD 30 | #define MFR_SHARP 0x00B0 31 | #define MFR_SST 0x00BF 32 | #define MFR_MACRONIX 0x00C2 //MXIC 33 | #define MFR_WINBOND 0x00DA 34 | #define MFR_SAMSUNG 0x00EC 35 | #define MFR_UNKNOW 0x00AA //unregisted manufacturer, but have CFI structure 36 | 37 | 38 | typedef struct _mfr_type { 39 | unsigned short id; 40 | char * name; 41 | } mfr_type; 42 | 43 | static mfr_type cfi_mfr_list[]={ 44 | {MFR_UNKNOW ,"Unknow" }, 45 | {MFR_AMD ,"AMD/Spansion"}, 46 | {MFR_FUJITSU ,"Fujitshu" }, 47 | {MFR_NEC ,"NEC" }, 48 | {MFR_EON_LEG ,"EON(alt)" }, 49 | {MFR_ATMEL ,"Atmel" }, 50 | {MFR_ST ,"ST/Numonyx" }, 51 | {MFR_EON ,"EON" }, 52 | {MFR_INTEL ,"Intel" }, 53 | {MFR_TOSHIBA ,"Toshiba" }, 54 | {MFR_PMC ,"PMC" }, 55 | {MFR_HYUNDAI ,"Hyundai" }, 56 | {MFR_SHARP ,"Sharp" }, 57 | {MFR_SST ,"SST" }, 58 | {MFR_MACRONIX ,"Macronix" }, 59 | {MFR_WINBOND ,"Winbond" }, 60 | {MFR_SAMSUNG ,"Samsung" }, 61 | {0,0}}; 62 | 63 | /* define command set ID */ 64 | #define P_ID_NONE 0x0000 65 | #define P_ID_INTEL_EXT 0x0001 66 | #define P_ID_AMD_STD 0x0002 67 | #define P_ID_INTEL_STD 0x0003 68 | #define P_ID_AMD_EXT 0x0004 69 | #define P_ID_WINBOND 0x0006 70 | #define P_ID_ST_ADV 0x0020 71 | #define P_ID_MITSUBISHI_STD 0x0100 72 | #define P_ID_MITSUBISHI_EXT 0x0101 73 | #define P_ID_SST_PAGE 0x0102 74 | #define P_ID_INTEL_PERFORMANCE 0x0200 75 | #define P_ID_INTEL_DATA 0x0210 76 | #define P_ID_RESERVED 0xffff 77 | #define P_ID_SST 0x0701 78 | 79 | /* AMD flsh boot location */ 80 | #define BOT_BOOT 2 81 | #define TOP_BOOT 3 82 | #define UBOT_BOOT 4 83 | #define UTOP_BOOT 5 84 | 85 | 86 | 87 | #define CFI_QRY_BUSWIDTH sizeof(unsigned short) 88 | 89 | 90 | 91 | 92 | /* Basic Query Structure */ 93 | typedef struct _cfi_qry_type { 94 | unsigned short qry[3]; 95 | unsigned short P_ID[2]; 96 | unsigned short P_ADR[2]; 97 | unsigned short A_ID[2]; 98 | unsigned short A_ADR[2]; 99 | unsigned short VccMin; 100 | unsigned short VccMax; 101 | unsigned short VppMin; 102 | unsigned short VppMax; 103 | unsigned short WordWriteTimeoutTyp; 104 | unsigned short BufWriteTimeoutTyp; 105 | unsigned short BlockEraseTimeoutTyp; 106 | unsigned short ChipEraseTimeoutTyp; 107 | unsigned short WordWriteTimeoutMax; 108 | unsigned short BufWriteTimeoutMax; 109 | unsigned short BlockEraseTimeoutMax; 110 | unsigned short ChipEraseTimeoutMax; 111 | unsigned short DevSize; 112 | unsigned short InterfaceDesc[2]; 113 | unsigned short MaxBufWriteSize[2]; 114 | unsigned short NumEraseRegions; 115 | unsigned short EraseRegionInfo[4][4]; 116 | } cfi_qry_type ; 117 | 118 | /* Extended Query Structure for both PRI and ALT */ 119 | 120 | typedef struct _cfi_pri_type { 121 | unsigned short pri[3]; 122 | unsigned short MajorVersion; 123 | unsigned short MinorVersion; 124 | unsigned short Features; //for Intel 125 | } cfi_pri_type; 126 | 127 | #define FULL_CHIP_ERASE 0x1 // intel chip feature 128 | 129 | /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */ 130 | 131 | typedef struct _cfi_pri_amdstd_type { 132 | unsigned short pri[3]; 133 | unsigned short MajorVersion; 134 | unsigned short MinorVersion; 135 | unsigned short SiliconRevision; /* bits 1-0: Address Sensitive Unlock */ 136 | unsigned short EraseSuspend; 137 | unsigned short BlkProt; 138 | unsigned short TmpBlkUnprotect; 139 | unsigned short BlkProtUnprot; 140 | unsigned short SimultaneousOps; 141 | unsigned short BurstMode; 142 | unsigned short PageMode; 143 | unsigned short VppMin; 144 | unsigned short VppMax; 145 | unsigned short TopBottom; 146 | } cfi_pri_amdstd_type; 147 | 148 | /* Vendor-Specific PRI for Atmel chips (command set 0x0002) */ 149 | 150 | typedef struct _cfi_pri_atmel_type { 151 | unsigned short pri[3]; 152 | unsigned short MajorVersion; 153 | unsigned short MinorVersion; 154 | unsigned short Features; 155 | unsigned short BottomBoot; 156 | unsigned short BurstMode; 157 | unsigned short PageMode; 158 | } cfi_pri_atmel_type; 159 | 160 | 161 | /* define functions prototype */ 162 | static void cfi_read_array(unsigned short *ptr, int ofs, int length); 163 | static int cfi_qry_request(); 164 | 165 | /* End of line */ 166 | -------------------------------------------------------------------------------- /ftd2xx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zoobab/zjtag/b67628b98d4181cb891ece804c0b00c1db98a0cb/ftd2xx.h -------------------------------------------------------------------------------- /ftd2xx.h.old: -------------------------------------------------------------------------------- 1 | /*++ 2 | 3 | Copyright (c) 2001-2006 Future Technology Devices International Ltd. 4 | 5 | Module Name: 6 | 7 | ftd2xx.h 8 | 9 | Abstract: 10 | 11 | Native USB interface for FTDI FT8U232/245/2232C 12 | FTD2XX library definitions 13 | 14 | Environment: 15 | 16 | kernel & user mode 17 | 18 | Revision History: 19 | 20 | 13/03/01 awm Created. 21 | 13/01/03 awm Added device information support. 22 | 19/03/03 awm Added FT_W32_CancelIo. 23 | 12/06/03 awm Added FT_StopInTask and FT_RestartInTask. 24 | 18/09/03 awm Added FT_SetResetPipeRetryCount. 25 | 10/10/03 awm Added FT_ResetPort. 26 | /03/04 st modified for linux users 27 | 12/10/04 st added FT_SetVIDPID 28 | 29 | 30 | --*/ 31 | 32 | 33 | #ifndef FTD2XX_H 34 | #define FTD2XX_H 35 | 36 | #ifndef _WINDOWS 37 | #include 38 | #define WINAPI 39 | #endif 40 | 41 | // The following ifdef block is the standard way of creating macros 42 | // which make exporting from a DLL simpler. All files within this DLL 43 | // are compiled with the FTD2XX_EXPORTS symbol defined on the command line. 44 | // This symbol should not be defined on any project that uses this DLL. 45 | // This way any other project whose source files include this file see 46 | // FTD2XX_API functions as being imported from a DLL, whereas this DLL 47 | // sees symbols defined with this macro as being exported. 48 | 49 | #ifdef FTD2XX_EXPORTS 50 | #define FTD2XX_API __declspec(dllexport) 51 | #else 52 | #define FTD2XX_API __declspec(dllimport) 53 | #endif 54 | 55 | #ifndef _WINDOWS 56 | #include "WinTypes.h" 57 | 58 | #ifdef FTD2XX_API 59 | #undef FTD2XX_API 60 | #define FTD2XX_API 61 | #endif 62 | #endif 63 | typedef struct _EVENT_HANDLE{ 64 | pthread_cond_t eCondVar; 65 | pthread_mutex_t eMutex; 66 | int iVar; 67 | } EVENT_HANDLE; 68 | 69 | typedef DWORD *FT_HANDLE; 70 | 71 | typedef ULONG FT_STATUS; 72 | 73 | // 74 | // Device status 75 | // 76 | enum { 77 | FT_OK, 78 | FT_INVALID_HANDLE, 79 | FT_DEVICE_NOT_FOUND, 80 | FT_DEVICE_NOT_OPENED, 81 | FT_IO_ERROR, 82 | FT_INSUFFICIENT_RESOURCES, 83 | FT_INVALID_PARAMETER, 84 | FT_INVALID_BAUD_RATE, //7 85 | 86 | FT_DEVICE_NOT_OPENED_FOR_ERASE, 87 | FT_DEVICE_NOT_OPENED_FOR_WRITE, 88 | FT_FAILED_TO_WRITE_DEVICE, 89 | FT_EEPROM_READ_FAILED, 90 | FT_EEPROM_WRITE_FAILED, 91 | FT_EEPROM_ERASE_FAILED, 92 | FT_EEPROM_NOT_PRESENT, 93 | FT_EEPROM_NOT_PROGRAMMED, 94 | FT_INVALID_ARGS, 95 | FT_NOT_SUPPORTED, 96 | FT_OTHER_ERROR 97 | }; 98 | 99 | 100 | #define FT_SUCCESS(status) ((status) == FT_OK) 101 | 102 | // 103 | // FT_OpenEx Flags 104 | // 105 | 106 | #define FT_OPEN_BY_SERIAL_NUMBER 1 107 | #define FT_OPEN_BY_DESCRIPTION 2 108 | 109 | // 110 | // FT_ListDevices Flags (used in conjunction with FT_OpenEx Flags 111 | // 112 | 113 | #define FT_LIST_NUMBER_ONLY 0x80000000 114 | #define FT_LIST_BY_INDEX 0x40000000 115 | #define FT_LIST_ALL 0x20000000 116 | 117 | #define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) 118 | 119 | // 120 | // Baud Rates 121 | // 122 | 123 | #define FT_BAUD_300 300 124 | #define FT_BAUD_600 600 125 | #define FT_BAUD_1200 1200 126 | #define FT_BAUD_2400 2400 127 | #define FT_BAUD_4800 4800 128 | #define FT_BAUD_9600 9600 129 | #define FT_BAUD_14400 14400 130 | #define FT_BAUD_19200 19200 131 | #define FT_BAUD_38400 38400 132 | #define FT_BAUD_57600 57600 133 | #define FT_BAUD_115200 115200 134 | #define FT_BAUD_230400 230400 135 | #define FT_BAUD_460800 460800 136 | #define FT_BAUD_921600 921600 137 | 138 | // 139 | // Word Lengths 140 | // 141 | 142 | #define FT_BITS_8 (UCHAR) 8 143 | #define FT_BITS_7 (UCHAR) 7 144 | #define FT_BITS_6 (UCHAR) 6 145 | #define FT_BITS_5 (UCHAR) 5 146 | 147 | // 148 | // Stop Bits 149 | // 150 | 151 | #define FT_STOP_BITS_1 (UCHAR) 0 152 | #define FT_STOP_BITS_1_5 (UCHAR) 1 153 | #define FT_STOP_BITS_2 (UCHAR) 2 154 | 155 | // 156 | // Parity 157 | // 158 | 159 | #define FT_PARITY_NONE (UCHAR) 0 160 | #define FT_PARITY_ODD (UCHAR) 1 161 | #define FT_PARITY_EVEN (UCHAR) 2 162 | #define FT_PARITY_MARK (UCHAR) 3 163 | #define FT_PARITY_SPACE (UCHAR) 4 164 | 165 | // 166 | // Flow Control 167 | // 168 | 169 | #define FT_FLOW_NONE 0x0000 170 | #define FT_FLOW_RTS_CTS 0x0100 171 | #define FT_FLOW_DTR_DSR 0x0200 172 | #define FT_FLOW_XON_XOFF 0x0400 173 | 174 | // 175 | // Purge rx and tx buffers 176 | // 177 | #define FT_PURGE_RX 1 178 | #define FT_PURGE_TX 2 179 | 180 | // 181 | // Events 182 | // 183 | 184 | typedef void (*PFT_EVENT_HANDLER)(DWORD,DWORD); 185 | 186 | #define FT_EVENT_RXCHAR 1 187 | #define FT_EVENT_MODEM_STATUS 2 188 | #define FT_EVENT_LINE_STATUS 4 189 | 190 | // 191 | // Timeouts 192 | // 193 | 194 | #define FT_DEFAULT_RX_TIMEOUT 300 195 | #define FT_DEFAULT_TX_TIMEOUT 300 196 | 197 | // 198 | // Device types 199 | // 200 | 201 | typedef ULONG FT_DEVICE; 202 | 203 | enum { 204 | FT_DEVICE_BM, 205 | FT_DEVICE_AM, 206 | FT_DEVICE_100AX, 207 | FT_DEVICE_UNKNOWN, 208 | FT_DEVICE_2232C, 209 | FT_DEVICE_232R, 210 | FT_DEVICE_2232H, 211 | FT_DEVICE_4232H 212 | }; 213 | 214 | 215 | #ifdef __cplusplus 216 | extern "C" { 217 | #endif 218 | 219 | FTD2XX_API 220 | FT_STATUS WINAPI FT_Open( 221 | int deviceNumber, 222 | FT_HANDLE *pHandle 223 | ); 224 | 225 | FTD2XX_API 226 | FT_STATUS WINAPI FT_OpenEx( 227 | PVOID pArg1, 228 | DWORD Flags, 229 | FT_HANDLE *pHandle 230 | ); 231 | 232 | FTD2XX_API 233 | FT_STATUS WINAPI FT_ListDevices( 234 | PVOID pArg1, 235 | PVOID pArg2, 236 | DWORD Flags 237 | ); 238 | 239 | FTD2XX_API 240 | FT_STATUS FT_SetVIDPID( 241 | DWORD dwVID, 242 | DWORD dwPID 243 | ); 244 | 245 | FTD2XX_API 246 | FT_STATUS FT_GetVIDPID( 247 | DWORD * pdwVID, 248 | DWORD * pdwPID 249 | ); 250 | 251 | FTD2XX_API 252 | FT_STATUS WINAPI FT_Close( 253 | FT_HANDLE ftHandle 254 | ); 255 | 256 | FTD2XX_API 257 | FT_STATUS WINAPI FT_Read( 258 | FT_HANDLE ftHandle, 259 | LPVOID lpBuffer, 260 | DWORD nBufferSize, 261 | LPDWORD lpBytesReturned 262 | ); 263 | 264 | FTD2XX_API 265 | FT_STATUS WINAPI FT_Write( 266 | FT_HANDLE ftHandle, 267 | LPVOID lpBuffer, 268 | DWORD nBufferSize, 269 | LPDWORD lpBytesWritten 270 | ); 271 | 272 | FTD2XX_API 273 | FT_STATUS WINAPI FT_IoCtl( // Linux, OS X: Not supported 274 | FT_HANDLE ftHandle, 275 | DWORD dwIoControlCode, 276 | LPVOID lpInBuf, 277 | DWORD nInBufSize, 278 | LPVOID lpOutBuf, 279 | DWORD nOutBufSize, 280 | LPDWORD lpBytesReturned, 281 | LPOVERLAPPED lpOverlapped 282 | ); 283 | 284 | FTD2XX_API 285 | FT_STATUS WINAPI FT_SetBaudRate( 286 | FT_HANDLE ftHandle, 287 | ULONG BaudRate 288 | ); 289 | 290 | FTD2XX_API 291 | FT_STATUS WINAPI FT_SetDivisor( 292 | FT_HANDLE ftHandle, 293 | USHORT Divisor 294 | ); 295 | 296 | FTD2XX_API 297 | FT_STATUS WINAPI FT_SetDataCharacteristics( 298 | FT_HANDLE ftHandle, 299 | UCHAR WordLength, 300 | UCHAR StopBits, 301 | UCHAR Parity 302 | ); 303 | 304 | FTD2XX_API 305 | FT_STATUS WINAPI FT_SetFlowControl( 306 | FT_HANDLE ftHandle, 307 | USHORT FlowControl, 308 | UCHAR XonChar, 309 | UCHAR XoffChar 310 | ); 311 | 312 | FTD2XX_API 313 | FT_STATUS WINAPI FT_ResetDevice( 314 | FT_HANDLE ftHandle 315 | ); 316 | 317 | FTD2XX_API 318 | FT_STATUS WINAPI FT_SetDtr( 319 | FT_HANDLE ftHandle 320 | ); 321 | 322 | FTD2XX_API 323 | FT_STATUS WINAPI FT_ClrDtr( 324 | FT_HANDLE ftHandle 325 | ); 326 | 327 | FTD2XX_API 328 | FT_STATUS WINAPI FT_SetRts( 329 | FT_HANDLE ftHandle 330 | ); 331 | 332 | FTD2XX_API 333 | FT_STATUS WINAPI FT_ClrRts( 334 | FT_HANDLE ftHandle 335 | ); 336 | 337 | FTD2XX_API 338 | FT_STATUS WINAPI FT_GetModemStatus( 339 | FT_HANDLE ftHandle, 340 | ULONG *pModemStatus 341 | ); 342 | 343 | FTD2XX_API 344 | FT_STATUS WINAPI FT_SetChars( 345 | FT_HANDLE ftHandle, 346 | UCHAR EventChar, 347 | UCHAR EventCharEnabled, 348 | UCHAR ErrorChar, 349 | UCHAR ErrorCharEnabled 350 | ); 351 | 352 | FTD2XX_API 353 | FT_STATUS WINAPI FT_Purge( 354 | FT_HANDLE ftHandle, 355 | ULONG Mask 356 | ); 357 | 358 | FTD2XX_API 359 | FT_STATUS WINAPI FT_SetTimeouts( 360 | FT_HANDLE ftHandle, 361 | ULONG ReadTimeout, 362 | ULONG WriteTimeout 363 | ); 364 | 365 | FTD2XX_API 366 | FT_STATUS WINAPI FT_GetQueueStatus( 367 | FT_HANDLE ftHandle, 368 | DWORD *dwRxBytes 369 | ); 370 | 371 | FTD2XX_API 372 | FT_STATUS WINAPI FT_SetEventNotification( 373 | FT_HANDLE ftHandle, 374 | DWORD Mask, 375 | PVOID Param 376 | ); 377 | 378 | FTD2XX_API 379 | FT_STATUS WINAPI FT_GetStatus( 380 | FT_HANDLE ftHandle, 381 | DWORD *dwRxBytes, 382 | DWORD *dwTxBytes, 383 | DWORD *dwEventDWord 384 | ); 385 | 386 | FTD2XX_API 387 | FT_STATUS WINAPI FT_SetBreakOn( 388 | FT_HANDLE ftHandle 389 | ); 390 | 391 | FTD2XX_API 392 | FT_STATUS WINAPI FT_SetBreakOff( 393 | FT_HANDLE ftHandle 394 | ); 395 | 396 | FTD2XX_API 397 | FT_STATUS WINAPI FT_SetWaitMask( // Linux, OS X: Not supported 398 | FT_HANDLE ftHandle, 399 | DWORD Mask 400 | ); 401 | 402 | FTD2XX_API 403 | FT_STATUS WINAPI FT_WaitOnMask( // Linux, OS X: Not supported 404 | FT_HANDLE ftHandle, 405 | DWORD *Mask 406 | ); 407 | 408 | FTD2XX_API 409 | FT_STATUS WINAPI FT_GetEventStatus( 410 | FT_HANDLE ftHandle, 411 | DWORD *dwEventDWord 412 | ); 413 | 414 | FTD2XX_API 415 | FT_STATUS WINAPI FT_ReadEE( 416 | FT_HANDLE ftHandle, 417 | DWORD dwWordOffset, 418 | LPWORD lpwValue 419 | ); 420 | 421 | FTD2XX_API 422 | FT_STATUS WINAPI FT_WriteEE( 423 | FT_HANDLE ftHandle, 424 | DWORD dwWordOffset, 425 | WORD wValue 426 | ); 427 | 428 | FTD2XX_API 429 | FT_STATUS WINAPI FT_EraseEE( 430 | FT_HANDLE ftHandle 431 | ); 432 | 433 | // 434 | // structure to hold program data for FT_Program function 435 | // 436 | typedef struct ft_program_data { 437 | 438 | DWORD Signature1; // Header - must be 0x00000000 439 | DWORD Signature2; // Header - must be 0xffffffff 440 | DWORD Version; // Header - FT_PROGRAM_DATA version 441 | // 0 = original 442 | // 1 = FT2232C extensions 443 | // 2 = FT232R extensions 444 | // 3 = FT2232H extensions 445 | // 4 = FT4232H extensions 446 | 447 | WORD VendorId; // 0x0403 448 | WORD ProductId; // 0x6001 449 | char *Manufacturer; // "FTDI" 450 | char *ManufacturerId; // "FT" 451 | char *Description; // "USB HS Serial Converter" 452 | char *SerialNumber; // "FT000001" if fixed, or NULL 453 | WORD MaxPower; // 0 < MaxPower <= 500 454 | WORD PnP; // 0 = disabled, 1 = enabled 455 | WORD SelfPowered; // 0 = bus powered, 1 = self powered 456 | WORD RemoteWakeup; // 0 = not capable, 1 = capable 457 | 458 | // 459 | // Rev4 extensions 460 | // 461 | UCHAR Rev4; // non-zero if Rev4 chip, zero otherwise 462 | UCHAR IsoIn; // non-zero if in endpoint is isochronous 463 | UCHAR IsoOut; // non-zero if out endpoint is isochronous 464 | UCHAR PullDownEnable; // non-zero if pull down enabled 465 | UCHAR SerNumEnable; // non-zero if serial number to be used 466 | UCHAR USBVersionEnable; // non-zero if chip uses USBVersion 467 | WORD USBVersion; // BCD (0x0200 => USB2) 468 | 469 | // 470 | // FT2232 extensions 471 | // 472 | UCHAR Rev5; // non-zero if Rev5 chip, zero otherwise 473 | UCHAR IsoInA; // non-zero if in endpoint is isochronous 474 | UCHAR IsoInB; // non-zero if in endpoint is isochronous 475 | UCHAR IsoOutA; // non-zero if out endpoint is isochronous 476 | UCHAR IsoOutB; // non-zero if out endpoint is isochronous 477 | UCHAR PullDownEnable5; // non-zero if pull down enabled 478 | UCHAR SerNumEnable5; // non-zero if serial number to be used 479 | UCHAR USBVersionEnable5; // non-zero if chip uses USBVersion 480 | WORD USBVersion5; // BCD (0x0200 => USB2) 481 | UCHAR AIsHighCurrent; // non-zero if interface is high current 482 | UCHAR BIsHighCurrent; // non-zero if interface is high current 483 | UCHAR IFAIsFifo; // non-zero if interface is 245 FIFO 484 | UCHAR IFAIsFifoTar; // non-zero if interface is 245 FIFO CPU target 485 | UCHAR IFAIsFastSer; // non-zero if interface is Fast serial 486 | UCHAR AIsVCP; // non-zero if interface is to use VCP drivers 487 | UCHAR IFBIsFifo; // non-zero if interface is 245 FIFO 488 | UCHAR IFBIsFifoTar; // non-zero if interface is 245 FIFO CPU target 489 | UCHAR IFBIsFastSer; // non-zero if interface is Fast serial 490 | UCHAR BIsVCP; // non-zero if interface is to use VCP drivers 491 | 492 | // 493 | // FT232R extensions 494 | // 495 | UCHAR UseExtOsc; // Use External Oscillator 496 | UCHAR HighDriveIOs; // High Drive I/Os 497 | UCHAR EndpointSize; // Endpoint size 498 | UCHAR PullDownEnableR; // non-zero if pull down enabled 499 | UCHAR SerNumEnableR; // non-zero if serial number to be used 500 | UCHAR InvertTXD; // non-zero if invert TXD 501 | UCHAR InvertRXD; // non-zero if invert RXD 502 | UCHAR InvertRTS; // non-zero if invert RTS 503 | UCHAR InvertCTS; // non-zero if invert CTS 504 | UCHAR InvertDTR; // non-zero if invert DTR 505 | UCHAR InvertDSR; // non-zero if invert DSR 506 | UCHAR InvertDCD; // non-zero if invert DCD 507 | UCHAR InvertRI; // non-zero if invert RI 508 | UCHAR Cbus0; // Cbus Mux control 509 | UCHAR Cbus1; // Cbus Mux control 510 | UCHAR Cbus2; // Cbus Mux control 511 | UCHAR Cbus3; // Cbus Mux control 512 | UCHAR Cbus4; // Cbus Mux control 513 | UCHAR RIsD2XX; // non-zero if using D2XX drivers 514 | 515 | // 516 | // Rev 7 (FT2232H) Extensions 517 | // 518 | UCHAR PullDownEnable7; // non-zero if pull down enabled 519 | UCHAR SerNumEnable7; // non-zero if serial number to be used 520 | UCHAR ALSlowSlew; // non-zero if AL pins have slow slew 521 | UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input 522 | UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 523 | UCHAR AHSlowSlew; // non-zero if AH pins have slow slew 524 | UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input 525 | UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 526 | UCHAR BLSlowSlew; // non-zero if BL pins have slow slew 527 | UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input 528 | UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 529 | UCHAR BHSlowSlew; // non-zero if BH pins have slow slew 530 | UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input 531 | UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 532 | UCHAR IFAIsFifo7; // non-zero if interface is 245 FIFO 533 | UCHAR IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target 534 | UCHAR IFAIsFastSer7; // non-zero if interface is Fast serial 535 | UCHAR AIsVCP7; // non-zero if interface is to use VCP drivers 536 | UCHAR IFBIsFifo7; // non-zero if interface is 245 FIFO 537 | UCHAR IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target 538 | UCHAR IFBIsFastSer7; // non-zero if interface is Fast serial 539 | UCHAR BIsVCP7; // non-zero if interface is to use VCP drivers 540 | UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs 541 | // 542 | // Rev 8 (FT4232H) Extensions 543 | // 544 | UCHAR PullDownEnable8; // non-zero if pull down enabled 545 | UCHAR SerNumEnable8; // non-zero if serial number to be used 546 | UCHAR ASlowSlew; // non-zero if AL pins have slow slew 547 | UCHAR ASchmittInput; // non-zero if AL pins are Schmitt input 548 | UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 549 | UCHAR BSlowSlew; // non-zero if AH pins have slow slew 550 | UCHAR BSchmittInput; // non-zero if AH pins are Schmitt input 551 | UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 552 | UCHAR CSlowSlew; // non-zero if BL pins have slow slew 553 | UCHAR CSchmittInput; // non-zero if BL pins are Schmitt input 554 | UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 555 | UCHAR DSlowSlew; // non-zero if BH pins have slow slew 556 | UCHAR DSchmittInput; // non-zero if BH pins are Schmitt input 557 | UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA 558 | UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN 559 | UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN 560 | UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN 561 | UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN 562 | UCHAR AIsVCP8; // non-zero if interface is to use VCP drivers 563 | UCHAR BIsVCP8; // non-zero if interface is to use VCP drivers 564 | UCHAR CIsVCP8; // non-zero if interface is to use VCP drivers 565 | UCHAR DIsVCP8; // non-zero if interface is to use VCP drivers 566 | 567 | } FT_PROGRAM_DATA, *PFT_PROGRAM_DATA; 568 | 569 | 570 | FTD2XX_API 571 | FT_STATUS WINAPI FT_EE_Program( 572 | FT_HANDLE ftHandle, 573 | PFT_PROGRAM_DATA pData 574 | ); 575 | 576 | FTD2XX_API 577 | FT_STATUS WINAPI FT_EE_ProgramEx( 578 | FT_HANDLE ftHandle, 579 | PFT_PROGRAM_DATA lpData, 580 | char *Manufacturer, 581 | char *ManufacturerId, 582 | char *Description, 583 | char *SerialNumber 584 | ); 585 | 586 | FTD2XX_API 587 | FT_STATUS WINAPI FT_EE_Read( 588 | FT_HANDLE ftHandle, 589 | PFT_PROGRAM_DATA pData 590 | ); 591 | 592 | FTD2XX_API 593 | FT_STATUS WINAPI FT_EE_ReadEx( 594 | FT_HANDLE ftHandle, 595 | PFT_PROGRAM_DATA lpData, 596 | char *Manufacturer, 597 | char *ManufacturerId, 598 | char *Description, 599 | char *SerialNumber 600 | ); 601 | 602 | FTD2XX_API 603 | FT_STATUS WINAPI FT_EE_UASize( 604 | FT_HANDLE ftHandle, 605 | LPDWORD lpdwSize 606 | ); 607 | 608 | FTD2XX_API 609 | FT_STATUS WINAPI FT_EE_UAWrite( 610 | FT_HANDLE ftHandle, 611 | PUCHAR pucData, 612 | DWORD dwDataLen 613 | ); 614 | 615 | FTD2XX_API 616 | FT_STATUS WINAPI FT_EE_UARead( 617 | FT_HANDLE ftHandle, 618 | PUCHAR pucData, 619 | DWORD dwDataLen, 620 | LPDWORD lpdwBytesRead 621 | ); 622 | 623 | FTD2XX_API 624 | FT_STATUS WINAPI FT_SetLatencyTimer( 625 | FT_HANDLE ftHandle, 626 | UCHAR ucLatency 627 | ); 628 | 629 | FTD2XX_API 630 | FT_STATUS WINAPI FT_GetLatencyTimer( 631 | FT_HANDLE ftHandle, 632 | PUCHAR pucLatency 633 | ); 634 | 635 | FTD2XX_API 636 | FT_STATUS WINAPI FT_SetBitMode( 637 | FT_HANDLE ftHandle, 638 | UCHAR ucMask, 639 | UCHAR ucEnable 640 | ); 641 | 642 | FTD2XX_API 643 | FT_STATUS WINAPI FT_GetBitMode( 644 | FT_HANDLE ftHandle, 645 | PUCHAR pucMode 646 | ); 647 | 648 | FTD2XX_API 649 | FT_STATUS WINAPI FT_SetUSBParameters( 650 | FT_HANDLE ftHandle, 651 | ULONG ulInTransferSize, 652 | ULONG ulOutTransferSize 653 | ); 654 | 655 | FTD2XX_API 656 | FT_STATUS WINAPI FT_SetDeadmanTimeout( 657 | FT_HANDLE ftHandle, 658 | ULONG ulDeadmanTimeout // -1 for infinite (2.6 kernels only). High +ve number for 2.4 kernels 659 | ); 660 | 661 | FTD2XX_API 662 | FT_STATUS WINAPI FT_GetDeviceInfo( 663 | FT_HANDLE ftHandle, 664 | FT_DEVICE *lpftDevice, 665 | LPDWORD lpdwID, 666 | PCHAR SerialNumber, 667 | PCHAR Description, 668 | LPVOID Dummy 669 | ); 670 | 671 | FTD2XX_API 672 | FT_STATUS WINAPI FT_StopInTask( 673 | FT_HANDLE ftHandle 674 | ); 675 | 676 | FTD2XX_API 677 | FT_STATUS WINAPI FT_RestartInTask( 678 | FT_HANDLE ftHandle 679 | ); 680 | 681 | FTD2XX_API 682 | FT_STATUS WINAPI FT_SetResetPipeRetryCount( // Linux, OS X: Not supported 683 | FT_HANDLE ftHandle, 684 | DWORD dwCount 685 | ); 686 | 687 | FTD2XX_API 688 | FT_STATUS WINAPI FT_ResetPort( // Linux, OS X: Not supported 689 | FT_HANDLE ftHandle 690 | ); 691 | 692 | FTD2XX_API 693 | FT_STATUS WINAPI FT_CyclePort( // Linux, OS X: Not supported 694 | FT_HANDLE ftHandle 695 | ); 696 | 697 | 698 | // 699 | // Win32-type functions 700 | // 701 | 702 | FTD2XX_API 703 | FT_HANDLE WINAPI FT_W32_CreateFile( 704 | LPCSTR lpszName, 705 | DWORD dwAccess, 706 | DWORD dwShareMode, 707 | LPSECURITY_ATTRIBUTES lpSecurityAttributes, 708 | DWORD dwCreate, 709 | DWORD dwAttrsAndFlags, 710 | HANDLE hTemplate 711 | ); 712 | 713 | FTD2XX_API 714 | BOOL WINAPI FT_W32_CloseHandle( 715 | FT_HANDLE ftHandle 716 | ); 717 | 718 | FTD2XX_API 719 | BOOL WINAPI FT_W32_ReadFile( 720 | FT_HANDLE ftHandle, 721 | LPVOID lpBuffer, 722 | DWORD nBufferSize, 723 | LPDWORD lpBytesReturned, 724 | LPOVERLAPPED lpOverlapped 725 | ); 726 | 727 | FTD2XX_API 728 | BOOL WINAPI FT_W32_WriteFile( 729 | FT_HANDLE ftHandle, 730 | LPVOID lpBuffer, 731 | DWORD nBufferSize, 732 | LPDWORD lpBytesWritten, 733 | LPOVERLAPPED lpOverlapped 734 | ); 735 | 736 | FTD2XX_API 737 | DWORD WINAPI FT_W32_GetLastError( 738 | FT_HANDLE ftHandle 739 | ); 740 | 741 | FTD2XX_API 742 | BOOL WINAPI FT_W32_GetOverlappedResult( // Linux, OS X: Not supported 743 | FT_HANDLE ftHandle, 744 | LPOVERLAPPED lpOverlapped, 745 | LPDWORD lpdwBytesTransferred, 746 | BOOL bWait 747 | ); 748 | 749 | FTD2XX_API 750 | BOOL WINAPI FT_W32_CancelIo( // Linux, OS X: Not supported 751 | FT_HANDLE ftHandle 752 | ); 753 | 754 | 755 | // 756 | // Win32 COMM API type functions 757 | // 758 | typedef struct _FTCOMSTAT { 759 | DWORD fCtsHold : 1; 760 | DWORD fDsrHold : 1; 761 | DWORD fRlsdHold : 1; 762 | DWORD fXoffHold : 1; 763 | DWORD fXoffSent : 1; 764 | DWORD fEof : 1; 765 | DWORD fTxim : 1; 766 | DWORD fReserved : 25; 767 | DWORD cbInQue; 768 | DWORD cbOutQue; 769 | } FTCOMSTAT, *LPFTCOMSTAT; 770 | 771 | typedef struct _FTDCB { 772 | DWORD DCBlength; /* sizeof(FTDCB) */ 773 | DWORD BaudRate; /* Baudrate at which running */ 774 | DWORD fBinary: 1; /* Binary Mode (skip EOF check) */ 775 | DWORD fParity: 1; /* Enable parity checking */ 776 | DWORD fOutxCtsFlow:1; /* CTS handshaking on output */ 777 | DWORD fOutxDsrFlow:1; /* DSR handshaking on output */ 778 | DWORD fDtrControl:2; /* DTR Flow control */ 779 | DWORD fDsrSensitivity:1; /* DSR Sensitivity */ 780 | DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */ 781 | DWORD fOutX: 1; /* Enable output X-ON/X-OFF */ 782 | DWORD fInX: 1; /* Enable input X-ON/X-OFF */ 783 | DWORD fErrorChar: 1; /* Enable Err Replacement */ 784 | DWORD fNull: 1; /* Enable Null stripping */ 785 | DWORD fRtsControl:2; /* Rts Flow control */ 786 | DWORD fAbortOnError:1; /* Abort all reads and writes on Error */ 787 | DWORD fDummy2:17; /* Reserved */ 788 | WORD wReserved; /* Not currently used */ 789 | WORD XonLim; /* Transmit X-ON threshold */ 790 | WORD XoffLim; /* Transmit X-OFF threshold */ 791 | BYTE ByteSize; /* Number of bits/byte, 4-8 */ 792 | BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */ 793 | BYTE StopBits; /* 0,1,2 = 1, 1.5, 2 */ 794 | char XonChar; /* Tx and Rx X-ON character */ 795 | char XoffChar; /* Tx and Rx X-OFF character */ 796 | char ErrorChar; /* Error replacement char */ 797 | char EofChar; /* End of Input character */ 798 | char EvtChar; /* Received Event character */ 799 | WORD wReserved1; /* Fill for now. */ 800 | } FTDCB, *LPFTDCB; 801 | 802 | typedef struct _FTTIMEOUTS { 803 | DWORD ReadIntervalTimeout; /* Maximum time between read chars. */ 804 | DWORD ReadTotalTimeoutMultiplier; /* Multiplier of characters. */ 805 | DWORD ReadTotalTimeoutConstant; /* Constant in milliseconds. */ 806 | DWORD WriteTotalTimeoutMultiplier; /* Multiplier of characters. */ 807 | DWORD WriteTotalTimeoutConstant; /* Constant in milliseconds. */ 808 | } FTTIMEOUTS,*LPFTTIMEOUTS; 809 | 810 | 811 | FTD2XX_API 812 | BOOL WINAPI FT_W32_ClearCommBreak( 813 | FT_HANDLE ftHandle 814 | ); 815 | 816 | FTD2XX_API 817 | BOOL WINAPI FT_W32_ClearCommError( 818 | FT_HANDLE ftHandle, 819 | LPDWORD lpdwErrors, 820 | LPFTCOMSTAT lpftComstat 821 | ); 822 | 823 | FTD2XX_API 824 | BOOL WINAPI FT_W32_EscapeCommFunction( 825 | FT_HANDLE ftHandle, 826 | DWORD dwFunc 827 | ); 828 | 829 | FTD2XX_API 830 | BOOL WINAPI FT_W32_GetCommModemStatus( 831 | FT_HANDLE ftHandle, 832 | LPDWORD lpdwModemStatus 833 | ); 834 | 835 | FTD2XX_API 836 | BOOL WINAPI FT_W32_GetCommState( 837 | FT_HANDLE ftHandle, 838 | LPFTDCB lpftDcb 839 | ); 840 | 841 | FTD2XX_API 842 | BOOL WINAPI FT_W32_GetCommTimeouts( 843 | FT_HANDLE ftHandle, 844 | FTTIMEOUTS *pTimeouts 845 | ); 846 | 847 | FTD2XX_API 848 | BOOL WINAPI FT_W32_PurgeComm( 849 | FT_HANDLE ftHandle, 850 | DWORD dwMask 851 | ); 852 | 853 | FTD2XX_API 854 | BOOL WINAPI FT_W32_SetCommBreak( 855 | FT_HANDLE ftHandle 856 | ); 857 | 858 | FTD2XX_API 859 | BOOL WINAPI FT_W32_SetCommMask( 860 | FT_HANDLE ftHandle, 861 | ULONG ulEventMask 862 | ); 863 | 864 | FTD2XX_API 865 | BOOL WINAPI FT_W32_SetCommState( 866 | FT_HANDLE ftHandle, 867 | LPFTDCB lpftDcb 868 | ); 869 | 870 | FTD2XX_API 871 | BOOL WINAPI FT_W32_SetCommTimeouts( 872 | FT_HANDLE ftHandle, 873 | FTTIMEOUTS *pTimeouts 874 | ); 875 | 876 | FTD2XX_API 877 | BOOL WINAPI FT_W32_SetupComm( 878 | FT_HANDLE ftHandle, 879 | DWORD dwReadBufferSize, 880 | DWORD dwWriteBufferSize 881 | ); 882 | 883 | FTD2XX_API 884 | BOOL WINAPI FT_W32_WaitCommEvent( 885 | FT_HANDLE ftHandle, 886 | PULONG pulEvent, 887 | LPOVERLAPPED lpOverlapped 888 | ); 889 | 890 | // 891 | // Device information 892 | // 893 | 894 | typedef struct _ft_device_list_info_node { 895 | ULONG Flags; 896 | ULONG Type; 897 | ULONG ID; 898 | DWORD LocId; 899 | char SerialNumber[16]; 900 | char Description[64]; 901 | FT_HANDLE ftHandle; 902 | } FT_DEVICE_LIST_INFO_NODE; 903 | 904 | // Device information flags 905 | 906 | enum { 907 | FT_FLAGS_OPENED = 1, 908 | FT_FLAGS_HISPEED = 2 909 | }; 910 | 911 | 912 | FTD2XX_API 913 | FT_STATUS WINAPI FT_CreateDeviceInfoList( 914 | LPDWORD lpdwNumDevs 915 | ); 916 | 917 | FTD2XX_API 918 | FT_STATUS WINAPI FT_GetDeviceInfoList( 919 | FT_DEVICE_LIST_INFO_NODE *pDest, 920 | LPDWORD lpdwNumDevs 921 | ); 922 | 923 | FTD2XX_API 924 | FT_STATUS WINAPI FT_GetDeviceInfoDetail( 925 | DWORD dwIndex, 926 | LPDWORD lpdwFlags, 927 | LPDWORD lpdwType, 928 | LPDWORD lpdwID, 929 | LPDWORD lpdwLocId, 930 | LPVOID lpSerialNumber, 931 | LPVOID lpDescription, 932 | FT_HANDLE *pftHandle 933 | ); 934 | 935 | FTD2XX_API 936 | FT_STATUS WINAPI FT_GetDriverVersion( 937 | FT_HANDLE ftHandle, 938 | LPDWORD lpdwVersion 939 | ); 940 | 941 | FTD2XX_API 942 | FT_STATUS WINAPI FT_GetLibraryVersion( 943 | LPDWORD lpdwVersion 944 | ); 945 | 946 | // 947 | // Events 948 | // 949 | 950 | #define EV_RXCHAR 0x0001 // Any Character received 951 | #define EV_RXFLAG 0x0002 // Received certain character 952 | #define EV_TXEMPTY 0x0004 // Transmitt Queue Empty 953 | #define EV_CTS 0x0008 // CTS changed state 954 | #define EV_DSR 0x0010 // DSR changed state 955 | #define EV_RLSD 0x0020 // RLSD changed state 956 | #define EV_BREAK 0x0040 // BREAK received 957 | #define EV_ERR 0x0080 // Line status error occurred 958 | #define EV_RING 0x0100 // Ring signal detected 959 | #define EV_PERR 0x0200 // Printer error occured 960 | #define EV_RX80FULL 0x0400 // Receive buffer is 80 percent full 961 | #define EV_EVENT1 0x0800 // Provider specific event 1 962 | #define EV_EVENT2 0x1000 // Provider specific event 2 963 | 964 | // 965 | // Escape Functions 966 | // 967 | 968 | #define SETXOFF 1 // Simulate XOFF received 969 | #define SETXON 2 // Simulate XON received 970 | #define SETRTS 3 // Set RTS high 971 | #define CLRRTS 4 // Set RTS low 972 | #define SETDTR 5 // Set DTR high 973 | #define CLRDTR 6 // Set DTR low 974 | #define RESETDEV 7 // Reset device if possible 975 | #define SETBREAK 8 // Set the device break line. 976 | #define CLRBREAK 9 // Clear the device break line. 977 | 978 | // 979 | // PURGE function flags. 980 | // 981 | #define PURGE_TXABORT 0x0001 // Kill the pending/current writes to the comm port. 982 | #define PURGE_RXABORT 0x0002 // Kill the pending/current reads to the comm port. 983 | #define PURGE_TXCLEAR 0x0004 // Kill the transmit queue if there. 984 | #define PURGE_RXCLEAR 0x0008 // Kill the typeahead buffer if there. 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | #ifdef __cplusplus 1000 | } 1001 | #endif 1002 | 1003 | 1004 | #endif /* FTD2XX_H */ 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | -------------------------------------------------------------------------------- /ftd2xx_w.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zoobab/zjtag/b67628b98d4181cb891ece804c0b00c1db98a0cb/ftd2xx_w.h -------------------------------------------------------------------------------- /ftdixx.h: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 1/5/2010 rev1.9a 3 | * 4 | * 5 | * FTDI FT2232D JTAG application in MPSSE mode for Brjtag. 6 | * Copyright (C) 2010 Hugebird 7 | * 8 | * This code is covered by the GPL v2. 9 | * 10 | * 11 | * 12 | */ 13 | 14 | 15 | #if ( defined(_MSC_VER) ) 16 | #define WINDOWS_VERSION 17 | #endif 18 | 19 | #ifdef WINDOWS_VERSION 20 | #include "windows.h" 21 | 22 | #else 23 | 24 | #ifndef MY_TYPE 25 | #define MY_TYPE 26 | typedef uint32_t DWORD; 27 | typedef uint16_t WORD; 28 | typedef unsigned char BYTE; 29 | typedef int bool; 30 | #endif 31 | 32 | #endif 33 | 34 | #ifdef FTDIXXX 35 | 36 | typedef DWORD FTC_HANDLE; 37 | typedef DWORD FTC_STATUS; 38 | 39 | 40 | typedef enum _JtagStates { 41 | JTAG_TLR = 0, 42 | JTAG_RTI = 1, 43 | JTAG_PDR = 2, 44 | JTAG_PIR = 3, 45 | JTAG_SDR = 4, 46 | JTAG_SIR = 5, 47 | Undefined = 6} JtagStates; 48 | 49 | #define MAX_JTAG_STATES 6 50 | #define NO_TAILBIT 0 51 | 52 | 53 | static const BYTE JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 54 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 55 | /* from TLR 0-> */ {'\x01', '\x00', '\x0A', '\x16', '\x02', '\x06'}, 56 | /* from RTI 1-> */ {'\x07', '\x00', '\x05', '\x0B', '\x01', '\x03'}, 57 | /* from PDR 2-> */ {'\x1F', '\x03', '\x00', '\x2F', '\x01', '\x0F'}, 58 | /* from PIR 3-> */ {'\x1F', '\x03', '\x17', '\x00', '\x07', '\x01'}, 59 | /* from SDR 4-> */ {'\x1F', '\x03', '\x01', '\x2F', '\x00', '\x0F'}, 60 | /* from SIR 5-> */ {'\x1F', '\x03', '\x17', '\x01', '\x07', '\x00'} }; 61 | 62 | static const BYTE TMSCLK_JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 63 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 64 | /* from TLR 0-> */ {1, 1, 5, 6, 4, 5}, 65 | /* from RTI 1-> */ {3, 1, 4, 5, 3, 4}, 66 | /* from PDR 2-> */ {5, 3, 1, 7, 2, 6}, 67 | /* from PIR 3-> */ {5, 3, 6, 1, 5, 2}, 68 | /* from SDR 4-> */ {5, 3, 2, 7, 0, 6}, 69 | /* from SIR 5-> */ {5, 3, 6, 2, 5, 0} }; 70 | 71 | // TMS shift max limit to 7 tcks 72 | 73 | 74 | // MPSSE Data Shifting Commands bit setting 75 | #define FTC_WR_CLKN 0x01 //bit0, Write -ve 76 | #define FTC_BIT 0x02 //bit1, 1 bit mode, 0 byte mode 77 | #define FTC_RD_CLKN 0x04 //bit2, Read -ve 78 | #define FTC_LSB 0x08 //bit3, 1 LSB Shift 1st, 0 MSB Shift 1st 79 | #define FTC_WR 0x10 //bit4, Write TDI/DO 80 | #define FTC_RD 0x20 //bit5, Read TDO/DI 81 | #define FTC_TMS 0x40 //bit6, Write TDI/DO tail bit, Write TMS xN clk 82 | 83 | 84 | // EJTAG require 85 | // LSB shift first 86 | // TCK initial state low 87 | // TDI/DO clock out on -ve edge 88 | // TDO/DI clock in on +ve edge 89 | // TMS clock out on -ve edge, TMS initial state high 90 | 91 | // MPSSE Data Shift Command 92 | #define DSC_BYTES_OUTN_LSB 0x19 93 | #define DSC_BITS_OUTN_LSB 0x1B 94 | 95 | #define DSC_BYTES_IN_LSB 0x28 96 | #define DSC_BITS_IN_LSB 0x2A 97 | 98 | #define DSC_BYTES_OUTN_IN_LSB 0x39 99 | #define DSC_BITS_OUTN_IN_LSB 0x3B 100 | 101 | #define DSC_TMSN_LSB 0x4B 102 | #define DSC_TMSN_IN_LSB 0x6B 103 | 104 | #define DSC_SET_GPIOL_BITS 0x80 105 | #define DSC_SET_GPIOH_BITS 0x82 106 | #define DSC_GET_GPIOL_BITS 0x81 107 | #define DSC_GET_GPIOH_BITS 0x83 108 | 109 | #define DSC_SET_CLK_FREQ 0x86 110 | #define DSC_SEND_FDBK_IMMD 0x87 111 | 112 | #define DSC_LOOPBACK_ON 0x84 113 | #define DSC_LOOPBACK_OFF 0x85 114 | 115 | // disable divide by 5 116 | #define DSC_DISABLE_DIVIDE_5_CMD 0x8A 117 | 118 | // bit and mask definitions for JTAG signals 119 | #define FT_TCK 0 120 | #define FT_TDI 1 121 | #define FT_TDO 2 122 | #define FT_TMS 3 123 | #define FT_MASK_TDO (1 << FT_TDO) 124 | #define FT_MASK_TDI (1 << FT_TDI) 125 | #define FT_MASK_TCK (1 << FT_TCK) 126 | #define FT_MASK_TMS (1 << FT_TMS) 127 | 128 | 129 | #define FT_JTAG_nOE 4 130 | // OpenMoko GPIO definition 131 | #define FT_Moko_nTRST_nOE 0 132 | #define FT_Moko_nTRST_OUT 1 133 | #define FT_Moko_nSRST_nOE 2 134 | #define FT_Moko_nSRST_OUT 3 135 | 136 | #define FT_Moko_EMU_IN 4 137 | #define FT_Moko_SRST_IN 7 138 | 139 | #define FT_DIR_Moko_nTRST_nOE (1 << FT_Moko_nTRST_nOE) 140 | #define FT_DIR_Moko_nTRST_OUT (1 << FT_Moko_nTRST_OUT) 141 | #define FT_DIR_Moko_nSRST_nOE (1 << FT_Moko_nSRST_nOE) 142 | #define FT_DIR_Moko_nSRST_OUT (1 << FT_Moko_nSRST_OUT) 143 | 144 | 145 | // FT2322 USB Output/Input Buffer byte length 146 | #define MAX_NUM_BYTES_USB_WRITE 65536 147 | #define MAX_NUM_BYTES_USB_WRITE_READ 65536 148 | #define MAX_NUM_BYTES_USB_READ 65536 149 | 150 | // TX/RX data buffer 151 | #define MAX_DATA_BLK_SIZE 16384 //Tx Data blk 152 | #define MAX_DSC_BLOCK_SIZE 8000 //Max DSC block reg count, 9B/IR or 16B/DWORD, 8K enough for 64KB buffer 153 | #define MAX_DMA_BLOCK_SIZE 1100 //Max DMA xfer block size, x10 Data blk per DMA blk 154 | #define DMA_BLOCK_SIZE 1100 //L6 defult DMA xfer block size, real used DMA blk size 155 | #define DMA_POLLING 2 //L3 defult polling retry time on DMA xfering 156 | #define FLASH_POLLING 16 //L4 defult polling retry time on flash writing 157 | #define FT_LATENCY 2 //L2 default latency timer in ms 158 | 159 | 160 | // port scan data type 161 | #define TAP_WO 0x10 162 | #define TAP_RO 0x20 163 | #define TAP_RW 0x30 164 | 165 | #define TAP_IR 1 166 | #define TAP_DATA 2 167 | #define TAP_TMS_DELAY 3 168 | #define TAP_TMS_MOVE 4 169 | 170 | // DMA xfer type 171 | #define XFER_TX 1 // blcok xfer, add cmd to queue, internal flag 172 | #define XFER_RX 2 // block xfer, get data from queue, internal flag 173 | 174 | #define XFER_ADD 3 // add DMA xfer block to queue, for caller 175 | #define XFER_QRY 4 // find next read data block,fetch out, for caller 176 | #define XFER_NOOP 5 // no operate, return free dma xfer block count 177 | 178 | #define DMA_WR 1 // dma write operate 179 | #define DMA_RD 2 // dma read operate 180 | 181 | //---------------------pub----------------------------------------- 182 | int ftinit(void *); 183 | void ftclose(void); 184 | void fttest_reset(void); 185 | DWORD ftset_instr(DWORD); 186 | DWORD ftdet_instr(void); 187 | DWORD ftReadWriteData(DWORD); 188 | DWORD ftReadData(void); 189 | void ftWriteData(DWORD); 190 | DWORD ft_dma_blkfetch(DWORD, DWORD, int, int, int*); 191 | void ft_sflash_write_word(DWORD, DWORD,int); 192 | int ft_sflash_blkread(DWORD, DWORD*, int); 193 | int ft_sflash_blkwrite(DWORD, DWORD*, int,int); 194 | void ft_dma_rdtraining(DWORD,DWORD); 195 | void ft_dma_wrtraining(DWORD,DWORD); 196 | 197 | //------------------private----------------------------------------- 198 | static DWORD bitmask32(int); 199 | static void FTDI_AddByteToOutputBuffer(DWORD, int); 200 | static void FTDI_AddNBytesToOutputBuffer(BYTE* , DWORD , int); 201 | static DWORD FTDI_AddDSCBlkToOutBuffer(int, int, BYTE*, DWORD, DWORD); 202 | static void FTDI_CalcBlkBytesToRead(DWORD, DWORD* , DWORD* ); 203 | static DWORD FTDI_MoveJTAGStateTo(JtagStates, DWORD, int); 204 | static FT_STATUS FTDI_WriteReadWithDevice( int , DWORD , DWORD* ); 205 | static int FTDI_scan_blk_post(int, int,BYTE*, DWORD, int, DWORD,int); 206 | static int FTDI_scan_flush(int); 207 | static void FTDI_scan_blk (int ); 208 | static int FTDI_scan_xfer (void); 209 | static void FTDI_CleanFlushIndex(void); 210 | static void FTDI_AddBlkToTXDataBuffer(DWORD, int,int,int); 211 | static DWORD getDWORD(BYTE *); 212 | static void FTDI_AddBlkDelay(int); 213 | static void FTDI_WaitInRTI(int); 214 | static void FTDI_WaitInTLR(int); 215 | static void FTDI_ClockOutTMS(DWORD, DWORD, int); 216 | static void ft_sflash_write_x16(DWORD, DWORD, int*); 217 | static void ft_sflash_write_x8(DWORD, DWORD, int*); 218 | static void fill_cable_prop(void *p); 219 | static void ft_purgebuffer(void); 220 | static void ft_getconfig(void); 221 | 222 | #endif 223 | 224 | -------------------------------------------------------------------------------- /j-link.h: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 1/5/2010 rev1.9a 3 | * 4 | * 5 | * J-Link JTAG application in bit-wise mode for Brjtag. 6 | * Copyright (C) 2010 Hugebird 7 | * 8 | * This code is covered by the GPL v2. 9 | * 10 | * 11 | * 12 | */ 13 | 14 | 15 | #if ( defined(_MSC_VER) ) 16 | #define WINDOWS_VERSION 17 | #endif 18 | 19 | #ifdef WINDOWS_VERSION 20 | #include "windows.h" 21 | 22 | #else 23 | 24 | #ifndef MY_TYPE 25 | #define MY_TYPE 26 | typedef uint32_t DWORD; 27 | typedef uint16_t WORD; 28 | typedef unsigned char BYTE; 29 | typedef int bool; 30 | #endif 31 | 32 | #endif 33 | 34 | #ifdef BJLINK 35 | 36 | typedef enum _JtagStates { 37 | JTAG_TLR = 0, 38 | JTAG_RTI = 1, 39 | JTAG_PDR = 2, 40 | JTAG_PIR = 3, 41 | JTAG_SDR = 4, 42 | JTAG_SIR = 5, 43 | Undefined = 6} JtagStates; 44 | 45 | #define MAX_JTAG_STATES 6 46 | 47 | static const BYTE JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 48 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 49 | /* from TLR 0-> */ {'\x01', '\x00', '\x0A', '\x16', '\x02', '\x06'}, 50 | /* from RTI 1-> */ {'\x07', '\x00', '\x05', '\x0B', '\x01', '\x03'}, 51 | /* from PDR 2-> */ {'\x1F', '\x03', '\x00', '\x2F', '\x01', '\x0F'}, 52 | /* from PIR 3-> */ {'\x1F', '\x03', '\x17', '\x00', '\x07', '\x01'}, 53 | /* from SDR 4-> */ {'\x1F', '\x03', '\x01', '\x2F', '\x00', '\x0F'}, 54 | /* from SIR 5-> */ {'\x1F', '\x03', '\x17', '\x01', '\x07', '\x00'} }; 55 | 56 | static const BYTE TMSCLK_JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 57 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 58 | /* from TLR 0-> */ {1, 1, 5, 6, 4, 5}, 59 | /* from RTI 1-> */ {3, 1, 4, 5, 3, 4}, 60 | /* from PDR 2-> */ {5, 3, 1, 7, 2, 6}, 61 | /* from PIR 3-> */ {5, 3, 6, 1, 5, 2}, 62 | /* from SDR 4-> */ {5, 3, 2, 7, 0, 6}, 63 | /* from SIR 5-> */ {5, 3, 6, 2, 5, 0} }; 64 | 65 | #define JSTPATH_EXR_TO_RTI 0x01 66 | #define JSTPLEN_EXR_TO_RTI 4 67 | 68 | // j-link Protocal Command 69 | // Get system information functions 70 | #define EMU_CMD_VERSION 0x01 71 | #define EMU_CMD_GET_SPEED 0xC0 72 | #define EMU_CMD_GET_CAPS 0xE8 73 | #define EMU_CMD_GET_CAPS_EX 0xED 74 | #define EMU_CMD_GET_SPEED 0xC0 75 | #define EMU_CMD_GET_HW_VERSION 0xF0 76 | 77 | // Get state information functions 78 | #define EMU_CMD_GET_STATE 0x07 79 | #define EMU_CMD_GET_HW_INFO 0xC1 80 | #define EMU_CMD_GET_COUNTERS 0xC2 81 | 82 | // JTAG & Hardware functions 83 | #define EMU_CMD_RESET_TRST 0x02 84 | #define EMU_CMD_SET_SPEED 0x05 85 | #define EMU_CMD_HW_CLOCK 0xC8 //Generates one clock and retrieves data from TDI 86 | #define EMU_CMD_HW_TMS0 0xC9 //Clear TMS 87 | #define EMU_CMD_HW_TMS1 0xCA //Sets TMS 88 | #define EMU_CMD_HW_DATA0 0xCB //Clears TDI signal 89 | #define EMU_CMD_HW_DATA1 0xCC //Sets TDI signal 90 | #define EMU_CMD_HW_JTAG 0xCD //It receives data for TDI and TMS and sends TDO data back 91 | //xfer No Bits, obsolete 92 | #define EMU_CMD_HW_JTAG2 0xCE //It receives data for TDI and TMS and sends TDO data back 93 | //xfer No Bits, ver >=5, obsolete 94 | #define EMU_CMD_HW_JTAG3 0xCF //It receives data for TDI and TMS and sends TDO data back 95 | //xfer No Bits. ver >=5 96 | #define EMU_CMD_HW_JTAG_WRITE 0xD5 //It receives data for TDI and TMS. No TDO data is sent back 97 | // via the EMU_CMD_HW_JTAG_GET_RESULT get xfer status 98 | #define EMU_CMD_HW_JTAG_GET_RESULT 0xCF 99 | #define EMU_CMD_HW_SELECT_IF 0xC7 //JTAG:0, SWD:1, RESET:0xFF 100 | //JTAG: ifreset->ifjtag->reset1->trst1->speed 101 | 102 | // Target functions 103 | #define EMU_CMD_HW_RESET0 0xDC 104 | #define EMU_CMD_HW_RESET1 0xDD 105 | #define EMU_CMD_HW_TRST0 0xDE 106 | #define EMU_CMD_HW_TRST1 0xDF 107 | 108 | // Retrieves capabilities of the emulator, flag bit mask 109 | #define EMU_CAP_GET_HW_VERSION (1 << 1) 110 | #define EMU_CAP_SPEED_INFO (1 << 9) 111 | #define EMU_CAP_GET_HW_INFO (1 << 12) 112 | #define EMU_CAP_SELECT_IF (1 << 17) 113 | #define EMU_CAP_GET_COUNTERS (1 << 19) 114 | #define EMU_CAP_REGISTER (1 << 27) 115 | 116 | // Speed *KHz 117 | #define JL_SPEED_12M 12000 118 | #define JL_SPEED_1M 1000 119 | 120 | // usb endpoint 121 | #define JL_EP1 0x81 //for read, v3,v4 also use for write 122 | #define JL_EP2 0x02 //for write, ver >=5 123 | 124 | // USB Output/Input Buffer byte length 125 | #define MAX_NUM_BYTES_USB_BUFFER 4096 126 | #define JL_MAX_BUFFER_SIZE 3000 //v8 has enough room to keep 3000bytes, eg total out buffer 3000*2+4 = 6004 bytes 127 | // speced in/out total buffer len is 2048bytes, total 4+1000*2 = 2004 bytes. 128 | // tms_delay sequence need limit to 10 bytes. 129 | 130 | // TX/RX data buffer 131 | #define MAX_DATA_BUFFER_SIZE 65536 132 | #define MAX_DATA_BLOCK_SIZE 20000 133 | 134 | 135 | #define MAX_DMA_BLOCK_SIZE 400 //Max L9 DMA xfer block size for 65536 bytes buffer. 136 | #define DMA_BLOCK_SIZE 256 //L6 defult DMA xfer block size 137 | #define DMA_POLLING 2 //L3 defult polling retry time on DMA xfering 138 | #define FLASH_POLLING 16 //L4 defult polling retry time on flash writing 139 | 140 | 141 | // port scan data type 142 | #define TAP_RD 0x80 // need return data 143 | #define TAP_IR 1 144 | #define TAP_DATA 2 145 | #define TAP_TMS_DELAY 3 146 | #define TAP_TMS_MOVE 4 147 | 148 | // DMA xfer type 149 | #define XFER_TX 1 // blcok xfer, add cmd to queue, internal flag 150 | #define XFER_RX 2 // block xfer, get data from queue, internal flag 151 | 152 | #define XFER_ADD 3 // add DMA xfer block to queue, for caller 153 | #define XFER_QRY 4 // find next read data block,fetch out, for caller 154 | #define XFER_NOOP 5 // no operate, return free dma xfer block count 155 | 156 | #define DMA_WR 1 // dma write operate 157 | #define DMA_RD 2 // dma read operate 158 | 159 | 160 | 161 | //--------------private------------------------------------------- 162 | 163 | static int JL_scan_oneclk (int tms, int tdi); 164 | static int JL_scan_blk (int BlkIndex); 165 | static int JL_scan_flush (void); 166 | static int JL_scan_xfer (void); 167 | static void JL_Wait(int n); 168 | static void JL_ResetToRTI (void); 169 | static void JL_AddBlkToTxDataBuffer(DWORD data, int type, int bitlen, int Is_clear); 170 | static void JL_ADDJTAGStateMoveTo(JtagStates JtagST2); 171 | static void JL_simplecmd ( BYTE cmd ); 172 | static void JL_setspeed ( WORD khz ); 173 | static void JL_seleJtag (void); 174 | static int JL_getstate(void); 175 | static DWORD JL_getversion(void); 176 | static DWORD JL_getcaps(void); 177 | static DWORD bitmask32(int len); 178 | static DWORD getDWORD(BYTE * pAddr); 179 | static void JL_sflash_write_x16(DWORD, DWORD, int*); 180 | static void JL_sflash_write_x8(DWORD, DWORD, int*); 181 | static void fill_cable_prop(void *p); 182 | static void JL_purgebuffer(void); 183 | static void JL_getconfig(void); 184 | 185 | //--------------------pub------------------------------------- 186 | int jlinit(void *); 187 | void jlclose(void); 188 | void jltest_reset(void); 189 | DWORD jlset_instr(DWORD); 190 | DWORD jldet_instr(void); 191 | DWORD jlReadWriteData(DWORD); 192 | DWORD jlReadData(void); 193 | void jlWriteData(DWORD); 194 | DWORD jl_dma_blkfetch(DWORD, DWORD, int, int, int*); 195 | void jl_sflash_write_word(DWORD, DWORD, int); 196 | int jl_sflash_blkread(DWORD, DWORD*, int); 197 | int jl_sflash_blkwrite(DWORD, DWORD*, int, int); 198 | 199 | void jl_dma_rdtraining(DWORD,DWORD); 200 | void jl_dma_wrtraining(DWORD,DWORD); 201 | 202 | #endif 203 | 204 | 205 | -------------------------------------------------------------------------------- /libusb.c: -------------------------------------------------------------------------------- 1 | /* Libusb supporting, Brjtag 1.9m 2 | * Copyright (c) 2010 hugebird @chinadsl.net 3 | * 4 | * 5 | */ 6 | 7 | 8 | 9 | #if ( defined(_MSC_VER) ) 10 | #define WINDOWS_VERSION //windows make with: cl brjtag.c xxx.c 11 | #endif 12 | 13 | #define BRLIBUSB 14 | 15 | #ifdef WINDOWS_VERSION 16 | 17 | #include // Only for Windows Compile 18 | #include "usb_w.h" 19 | #define strcasecmp stricmp 20 | #define strncasecmp strnicmp 21 | #define mssleep(s) Sleep(s) 22 | 23 | #else //For linux 24 | 25 | #include "usb.h" 26 | #define mssleep(s) usleep((s) *1000) 27 | 28 | #ifndef MY_TYPE 29 | #define MY_TYPE 30 | typedef unsigned long DWORD; 31 | typedef unsigned short WORD; 32 | typedef unsigned char BYTE; 33 | typedef int bool; 34 | #endif //MY_TYPE 35 | 36 | #endif 37 | 38 | 39 | #include "libusb.h" 40 | 41 | #define MATCH_SUCCESS 1 42 | #define MATCH_FAILED 0 43 | #define MATCH_ABORT -1 44 | 45 | static int usbGetStringAscii(usb_dev_handle *dev, int index, char *buf, int buflen); 46 | static int shellStyleMatch(char *text, char *pattern); 47 | static int _shellStyleMatch(char *text, char *p); 48 | static usb_dev_handle *open_dev(void); 49 | 50 | #define USB_TIMEOUT 1000 51 | static usb_dev_handle *handle = NULL; /* the device handle */ 52 | static int ep_in = 0x81, ep_out = 0x02; 53 | static WORD my_vid, my_pid; 54 | static int usb_timeout = USB_TIMEOUT; 55 | static char v_name[256], p_name[256], s_name[256]; 56 | 57 | 58 | #ifdef WINDOWS_VERSION 59 | 60 | typedef void (__cdecl *tinit) (void); 61 | typedef int (__cdecl *tfindbus)(void); 62 | typedef int (__cdecl *tfinddev)(void); 63 | typedef struct usb_device* (__cdecl *tdevice)(usb_dev_handle*); 64 | typedef struct usb_bus* (__cdecl *tgetbus)(void); 65 | typedef int (__cdecl *treset) (usb_dev_handle*); 66 | typedef int (__cdecl *tsetconfig) (usb_dev_handle*, int); 67 | typedef int (__cdecl *tclaimintf) (usb_dev_handle*, int); 68 | typedef int (__cdecl *tsetaltintf)(usb_dev_handle *, int); 69 | typedef usb_dev_handle* (__cdecl *topen)(struct usb_device*); 70 | typedef int (__cdecl *treleaseintf)(usb_dev_handle*, int); 71 | typedef int (__cdecl *tclose) (usb_dev_handle*); 72 | typedef int (__cdecl *tbulkwrite) (usb_dev_handle*, int, char*,int, int); 73 | typedef int (__cdecl *tbulkread) (usb_dev_handle*, int, char*,int, int); 74 | typedef int (__cdecl *tgetstrs) (usb_dev_handle*, int, char*,size_t); 75 | typedef int (__cdecl *tctrlmsg) (usb_dev_handle*, int, int, int, int, char*,int, int); 76 | 77 | 78 | static tinit _usb_init; 79 | static tfindbus _usb_find_busses; 80 | static tfinddev _usb_find_devices; 81 | static tdevice _usb_device; 82 | static tgetbus _usb_get_busses; 83 | static treset _usb_reset; 84 | static tsetconfig _usb_set_configuration; 85 | static tclaimintf _usb_claim_interface; 86 | static tsetaltintf _usb_set_altinterface; 87 | static topen _usb_open = NULL; 88 | static treleaseintf _usb_release_interface; 89 | static tclose _usb_close; 90 | static tbulkwrite _usb_bulk_write; 91 | static tbulkread _usb_bulk_read; 92 | static tgetstrs _usb_get_string_simple; 93 | static tctrlmsg _usb_control_msg; 94 | 95 | 96 | HINSTANCE libusb_dll; 97 | static void load_libusbdll(void); 98 | 99 | #else 100 | 101 | #define _usb_init usb_init 102 | #define _usb_find_busses usb_find_busses 103 | #define _usb_find_devices usb_find_devices 104 | #define _usb_device usb_device 105 | #define _usb_get_busses usb_get_busses 106 | #define _usb_reset usb_reset 107 | #define _usb_set_configuration usb_set_configuration 108 | #define _usb_claim_interface usb_claim_interface 109 | #define _usb_set_altinterface usb_set_altinterface 110 | #define _usb_open usb_open 111 | #define _usb_release_interface usb_release_interface 112 | #define _usb_close usb_close 113 | #define _usb_bulk_write usb_bulk_write 114 | #define _usb_bulk_read usb_bulk_read 115 | #define _usb_get_string_simple usb_get_string_simple 116 | #define _usb_control_msg usb_control_msg 117 | 118 | #endif 119 | 120 | 121 | 122 | #ifdef WINDOWS_VERSION 123 | static void load_libusbdll(void) 124 | { 125 | 126 | libusb_dll = LoadLibrary("libusb0.dll"); 127 | if (!libusb_dll) 128 | { 129 | printf("Couldn't load libusb liberary\n"); 130 | exit (1); 131 | } 132 | 133 | _usb_init = (tinit) GetProcAddress(libusb_dll, "usb_init"); 134 | _usb_find_busses = (tfindbus) GetProcAddress(libusb_dll, "usb_find_busses"); 135 | _usb_find_devices = (tfinddev) GetProcAddress(libusb_dll, "usb_find_devices"); 136 | _usb_device = (tdevice) GetProcAddress(libusb_dll, "usb_device"); 137 | _usb_get_busses = (tgetbus) GetProcAddress(libusb_dll, "usb_get_busses"); 138 | _usb_reset = (treset) GetProcAddress(libusb_dll, "usb_reset"); 139 | _usb_set_configuration = (tsetconfig) GetProcAddress(libusb_dll, "usb_set_configuration"); 140 | _usb_claim_interface = (tclaimintf) GetProcAddress(libusb_dll, "usb_claim_interface"); 141 | _usb_set_altinterface = (tsetaltintf) GetProcAddress(libusb_dll, "usb_set_altinterface"); 142 | 143 | _usb_open = (topen)GetProcAddress(libusb_dll, "usb_open"); 144 | _usb_release_interface = (treleaseintf) GetProcAddress(libusb_dll, "usb_release_interface"); 145 | _usb_close = (tclose) GetProcAddress(libusb_dll, "usb_close"); 146 | _usb_bulk_write = (tbulkwrite) GetProcAddress(libusb_dll, "usb_bulk_write"); 147 | _usb_bulk_read = (tbulkread) GetProcAddress(libusb_dll, "usb_bulk_read"); 148 | 149 | _usb_get_string_simple = (tgetstrs) GetProcAddress(libusb_dll, "usb_get_string_simple"); 150 | _usb_control_msg = (tctrlmsg) GetProcAddress(libusb_dll, "usb_control_msg"); 151 | 152 | 153 | if ( !_usb_init || !_usb_find_busses || !_usb_find_devices || !_usb_device || 154 | !_usb_get_busses || !_usb_reset || !_usb_set_configuration || !_usb_claim_interface || 155 | !_usb_set_altinterface || !_usb_open || !_usb_release_interface || !_usb_close || 156 | !_usb_bulk_write || !_usb_bulk_read || !_usb_get_string_simple || !_usb_control_msg ) 157 | 158 | { 159 | printf("Couldn't link to libusb liberary\n"); 160 | exit (1); 161 | } 162 | } 163 | 164 | #endif 165 | 166 | 167 | static usb_dev_handle *open_dev(void) 168 | { 169 | struct usb_bus *bus; 170 | struct usb_device *dev; 171 | for (bus = _usb_get_busses(); bus; bus = bus->next) 172 | { 173 | for (dev = bus->devices; dev; dev = dev->next) 174 | { 175 | if ( (my_vid == 0 || dev->descriptor.idVendor == my_vid) 176 | && (my_pid==0 || dev->descriptor.idProduct == my_pid) ) 177 | { 178 | return _usb_open(dev);; 179 | } 180 | } 181 | } 182 | return NULL; 183 | } 184 | 185 | 186 | void libusb_open(DWORD id, int epin, int epout, int timeout, char *vendorName, char *productName, char *serialNO) 187 | { 188 | 189 | struct usb_bus *bus; 190 | struct usb_device *dev; 191 | int len; 192 | int err; 193 | 194 | if(epin) ep_in = epin; 195 | if(epout) ep_out = epout; 196 | my_vid = (id>>16)&0xFFFF; 197 | my_pid = id & 0xFFFF; 198 | if(timeout) usb_timeout = timeout; 199 | 200 | #ifdef WINDOWS_VERSION 201 | load_libusbdll(); 202 | #endif 203 | 204 | _usb_init(); /* initialize the library */ 205 | _usb_find_busses(); /* find all busses */ 206 | _usb_find_devices(); /* find all connected devices */ 207 | 208 | 209 | for (bus = _usb_get_busses(); bus; bus = bus->next) 210 | { 211 | for (dev = bus->devices; dev; dev = dev->next) 212 | { 213 | err = 0; 214 | if ( dev->descriptor.idVendor == my_vid 215 | && dev->descriptor.idProduct == my_pid ) 216 | 217 | { 218 | handle = _usb_open(dev); // open dev to query strings 219 | if(!handle){ err=1; continue; } 220 | 221 | 222 | //match Vendor Name String 223 | len = 0; 224 | v_name[0] = 0; 225 | 226 | if(vendorName != NULL && dev->descriptor.iManufacturer > 0) 227 | { 228 | len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, v_name, sizeof(v_name)); 229 | if( (len < 0) || (shellStyleMatch(v_name, vendorName) != MATCH_SUCCESS) ) 230 | {err = 2; goto target1; } 231 | 232 | } 233 | 234 | //match Production Name String 235 | len = 0; 236 | p_name[0] = 0; 237 | 238 | if(productName != NULL && dev->descriptor.iProduct > 0) 239 | { 240 | len = usbGetStringAscii(handle, dev->descriptor.iProduct, p_name, sizeof(p_name)); 241 | if( (len < 0) || (shellStyleMatch(p_name, productName) != MATCH_SUCCESS) ) 242 | {err = 3; goto target1; } 243 | } 244 | 245 | //match Serial Number String 246 | len = 0; 247 | s_name[0] = 0; 248 | 249 | if(serialNO != NULL && dev->descriptor.iSerialNumber > 0) 250 | { 251 | len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, s_name, sizeof(s_name)); 252 | if( (len < 0) || (shellStyleMatch(s_name, serialNO) != MATCH_SUCCESS) ) 253 | {err = 4; goto target1; } 254 | } 255 | 256 | target1: 257 | if (err) 258 | { 259 | _usb_close(handle); 260 | handle = NULL; 261 | } 262 | break; 263 | } 264 | 265 | } 266 | if(handle) break; 267 | } 268 | if(!handle) 269 | { 270 | if(err ==0) printf("libusb error: open find device %04X:%04X !\n",my_vid,my_pid); 271 | if(err ==1) printf("libusb error: open device %04X:%04X error!\n",my_vid,my_pid); 272 | if(err ==2) printf("libusb error: NOT find matching device with vendor name [%s]\n",vendorName); 273 | if(err ==3) printf("libusb error: NOT find matching device with production name [%s]\n",productName); 274 | if(err ==4) printf("libusb error: NOT find matching device with serial no [%s]\n",serialNO); 275 | exit(1); 276 | } 277 | 278 | if(handle) 279 | { 280 | 281 | usbGetStringAscii(handle, dev->descriptor.iManufacturer, v_name, sizeof(v_name)); 282 | usbGetStringAscii(handle, dev->descriptor.iProduct, p_name, sizeof(p_name)); 283 | usbGetStringAscii(handle, dev->descriptor.iSerialNumber, s_name, sizeof(s_name)); 284 | printf(" Open USB device: 0x%04X:%04X\n",my_vid,my_pid); 285 | printf(" Vendor Name: [%s]\n",v_name); 286 | printf(" Produc Name: [%s]\n",p_name); 287 | printf(" Serial No : [%s]\n\n",s_name); 288 | } 289 | 290 | 291 | /* 292 | // reset found device 293 | 294 | _usb_reset(handle); 295 | handle = NULL; 296 | 297 | timeout = 10; 298 | while (handle == NULL && timeout) 299 | { 300 | mssleep(100); 301 | _usb_find_devices(); // find all connected devices 302 | 303 | handle = open_dev(); 304 | timeout -- ; 305 | } 306 | 307 | if (!handle) 308 | { 309 | printf("libusb error: USB TAP device reset error!\n"); 310 | exit(1); 311 | } 312 | 313 | */ 314 | 315 | 316 | dev = _usb_device(handle); 317 | if (_usb_set_configuration(handle, dev->config[0].bConfigurationValue) < 0) 318 | { 319 | printf("libusb error: setting config failed\n"); 320 | _usb_close(handle); 321 | exit(1); 322 | } 323 | 324 | if (_usb_claim_interface(handle, 0) < 0) 325 | { 326 | printf("libusb error: claiming interface failed\n"); 327 | _usb_close(handle); 328 | exit(1); 329 | } 330 | #if 0 331 | else 332 | { 333 | _usb_set_altinterface(handle, 0); 334 | } 335 | #endif 336 | } 337 | 338 | 339 | void libusb_close(void) 340 | { 341 | _usb_release_interface(handle, 0); 342 | _usb_close(handle); 343 | 344 | #ifdef WINDOWS_VERSION 345 | if (libusb_dll != NULL) 346 | FreeLibrary (libusb_dll); 347 | #endif 348 | } 349 | 350 | 351 | int libusb_bulk_read(BYTE* buf, int bytelen) 352 | { 353 | return _usb_bulk_read(handle, ep_in, (char*)buf, bytelen, usb_timeout); 354 | } 355 | 356 | int libusb_bulk_write(BYTE* buf, int bytelen) 357 | { 358 | return _usb_bulk_write(handle, ep_out, (char*)buf, bytelen, usb_timeout); 359 | } 360 | 361 | 362 | /* 363 | int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 364 | int value, int index, char *bytes, int size, 365 | int timeout); 366 | 367 | 368 | typedef union usbWord{ 369 | unsigned word; 370 | uint8_t bytes[2]; 371 | }usbWord_t; 372 | 373 | typedef struct usbRequest{ 374 | uint8_t bmRequestType; // data[0] 375 | uint8_t bRequest; // data[1] 376 | usbWord_t wValue; // data[2],[3] [3]<<8 | [2] 377 | usbWord_t wIndex; // data[4],[5] 378 | usbWord_t wLength; // data[6],[7] 379 | }usbRequest_t; 380 | 381 | */ 382 | 383 | // read usb data to buffer, len<255 384 | int libusb_msg_read(int cmd, int value, int index, BYTE* buf, int bytelen) 385 | { 386 | 387 | return _usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, cmd, value, index, buf, bytelen, usb_timeout); 388 | 389 | } 390 | 391 | // write buffer to usb, len<255 392 | int libusb_msg_write(int cmd, int value, int index, BYTE* buf, int bytelen) 393 | { 394 | 395 | return _usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, cmd, value, index, buf, bytelen, usb_timeout); 396 | 397 | } 398 | 399 | 400 | 401 | /* private interface: match text and p, return MATCH_SUCCESS, MATCH_FAILED, or MATCH_ABORT. */ 402 | static int _shellStyleMatch(char *text, char *p) 403 | { 404 | int last, matched, reverse; 405 | 406 | for(; *p; text++, p++){ 407 | if(*text == 0 && *p != '*') 408 | return MATCH_ABORT; 409 | switch(*p){ 410 | case '\\': 411 | /* Literal match with following character. */ 412 | p++; 413 | /* FALLTHROUGH */ 414 | default: 415 | if(*text != *p) 416 | return MATCH_FAILED; 417 | continue; 418 | case '?': 419 | /* Match anything. */ 420 | continue; 421 | case '*': 422 | while(*++p == '*') 423 | /* Consecutive stars act just like one. */ 424 | continue; 425 | if(*p == 0) 426 | /* Trailing star matches everything. */ 427 | return MATCH_SUCCESS; 428 | while(*text) 429 | if((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED) 430 | return matched; 431 | return MATCH_ABORT; 432 | case '[': 433 | reverse = p[1] == '^'; 434 | if(reverse) /* Inverted character class. */ 435 | p++; 436 | matched = MATCH_FAILED; 437 | if(p[1] == ']' || p[1] == '-') 438 | if(*++p == *text) 439 | matched = MATCH_SUCCESS; 440 | for(last = *p; *++p && *p != ']'; last = *p) 441 | if (*p == '-' && p[1] != ']' ? *text <= *++p && *text >= last : *text == *p) 442 | matched = MATCH_SUCCESS; 443 | if(matched == reverse) 444 | return MATCH_FAILED; 445 | continue; 446 | } 447 | } 448 | return *text == 0; 449 | } 450 | 451 | /* public interface for shell style matching: returns 0 if fails, 1 if matches */ 452 | static int shellStyleMatch(char *text, char *pattern) 453 | { 454 | if(pattern == NULL) /* NULL pattern is synonymous to "*" */ 455 | return MATCH_SUCCESS; 456 | return _shellStyleMatch(text, pattern); 457 | } 458 | 459 | /* ------------------------------------------------------------------------- */ 460 | 461 | static int usbGetStringAscii(usb_dev_handle *dev, int index, char *buf, int buflen) 462 | { 463 | char buffer[256]; 464 | int rval, i; 465 | 466 | if((rval = _usb_get_string_simple(dev, index, buf, buflen)) >= 0) /* use libusb version if it works */ 467 | return rval; 468 | if((rval = _usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, 0x0409, buffer, sizeof(buffer), 5000)) < 0) 469 | return rval; 470 | if(buffer[1] != USB_DT_STRING){ 471 | *buf = 0; 472 | return 0; 473 | } 474 | if((unsigned char)buffer[0] < rval) 475 | rval = (unsigned char)buffer[0]; 476 | rval /= 2; 477 | /* lossy conversion to ISO Latin1: */ 478 | for(i=1;i buflen) /* destination buffer overflow */ 480 | break; 481 | buf[i-1] = buffer[2 * i]; 482 | if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */ 483 | buf[i-1] = '?'; 484 | } 485 | buf[i-1] = 0; 486 | return i-1; 487 | } 488 | 489 | 490 | /* ------------------------------------------------------------------------- */ 491 | 492 | 493 | 494 | 495 | 496 | -------------------------------------------------------------------------------- /libusb.h: -------------------------------------------------------------------------------- 1 | /* Libusb supporting, Brjtag 1.8f 2 | * Copyright (c) 2010 hugebird @chinadsl.net 3 | * 4 | * 5 | */ 6 | 7 | 8 | 9 | int libusb_bulk_read(BYTE* buf, int bytelen); 10 | int libusb_bulk_write(BYTE* buf, int bytelen); 11 | int libusb_msg_read(int cmd, int value, int index, BYTE* buf, int bytelen); 12 | int libusb_msg_write(int cmd, int value, int index, BYTE* buf, int bytelen); 13 | void libusb_close(void); 14 | void libusb_open(DWORD id, int epin, int epout, int timeout, char* vn, char* pn, char* sn); 15 | 16 | 17 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | zJTAG ver.1.4 2 | rebased on; 3 | BrJTAG 1.9o and TJTAG 3.0.1 4 | 5 | ********************************************************************************************* 6 | If you modified based on these SRC codes, I will appreciate if you send a copy 7 | to volkan-k@users.sourceforge.net 8 | ********************************************************************************************* 9 | 10 | Volkan K. 11 | 2013.06 -------------------------------------------------------------------------------- /stmhid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 17/10/2010 Brjtag rev1.9m 3 | * 4 | * 5 | * Brjtag application for HID-BRJTAG v2.xx MCU firmware. 6 | * fw v2.0 on STM32 platform 7 | * 8 | * Copyright (C) 2010 Hugebird 9 | * 10 | * This code is covered by the GPL v2. 11 | * 12 | * 13 | */ 14 | 15 | 16 | 17 | #if ( defined(_MSC_VER) ) 18 | #define WINDOWS_VERSION 19 | #endif 20 | 21 | #define STMHID 22 | 23 | 24 | #include "zjtag.h" 25 | #include "stmhid.h" 26 | #include "libusb.h" 27 | 28 | 29 | #define DEBUGFLUSH 0 30 | #define DEBUGTXBUF 0 31 | #define DEBUGRXBUF 0 32 | #define DEBUGINBUF 0 33 | #define DEBUGOTBUF 0 34 | #define DEBUGBLK 0 35 | #define DEBUGMSG 0 36 | #define DEBUGFETCH 0 // check dma fetch 37 | #define DEBUGTRAINING 0 38 | 39 | #if (DEBUGFLUSH || DEBUGTXBUF ||DEBUGRXBUF || DEBUGINBUF || DEBUGOTBUF \ 40 | || DEBUGBLK || DEBUGMSG || DEBUGFETCH || DEBUGTRAINING ) 41 | #define DBG(x) printf x 42 | #else 43 | #define DBG(x) 44 | #endif 45 | 46 | 47 | static BYTE cmdbuf[4100]; 48 | static DWORD tmpbuf[2100]; 49 | static BYTE st[10]; 50 | static int gIndex = 0; 51 | 52 | 53 | 54 | static int lowspeed = 0; 55 | static int skip = 0; 56 | 57 | #define BigEndian (endian == __BE) 58 | 59 | 60 | 61 | ///////////////////////////////extern global//////////////////////////////////////////////////// 62 | extern int instruction_length; 63 | extern int endian; 64 | extern DWORD FLASH_MEMORY_START; 65 | extern DWORD cmd_type; 66 | extern int safemode; 67 | extern int showgpio; 68 | extern int silent_mode; 69 | extern int bypass; 70 | extern int flsPrgTimeout; 71 | extern int ejtag_speed; 72 | 73 | extern DWORD LL1; 74 | extern BYTE LL2; 75 | extern DWORD LL3; 76 | extern DWORD LL4; 77 | extern DWORD LL5; 78 | extern DWORD LL6; 79 | extern DWORD LL7; 80 | extern DWORD LL8; 81 | extern DWORD LL9; 82 | 83 | extern DWORD USBID; 84 | 85 | 86 | static void u_getconfig(void) 87 | { 88 | // performance fine tune parameters 89 | //L1: clk 90 | //L2: FT2232 USB read latency>>> 2 ~ 255ms, default 2ms 91 | //L3: DMA xfer Polling timeout>>> 0 ~ 48 us, defualt 1 us 92 | //L4: FLASH Write Polling timeout>>> 1 ~ 127 us, defualt 16 us 93 | //L5: USB Buffer size 94 | //L6: Allowed DMA Bulk xfer count>>> 95 | //L7: Block Size on FLASH DWORD Read in x32Bits Mode>>> 96 | //L8: Block Size on FLASH DWORD Write in x16Bits Mode>>> 97 | //L9: default profile>>> 98 | 99 | if(LL4==0xFFFF) LL4 = FLASH_POLLING; 100 | if(LL1>20) LL1 =20; //limit to 20 cycles delay max 101 | if(!LL7 || LL7 >1024) LL7 = 1024; 102 | if(!LL8 ) LL8 = 128; 103 | if(LL8 >200) LL8 = 200; 104 | if(LL9 == 1) //safe mode 105 | { LL1 = 10; 106 | LL7 = 64; 107 | LL8 = 32; 108 | LL4 = 128; 109 | } 110 | else if(LL9 == 2) //risk read mode 111 | { 112 | LL1 = 0; 113 | } 114 | 115 | // if(!USBID) USBID = 0x16C005DF; 116 | if(!USBID) USBID = 0x04835750; 117 | // printf(" LL1[%d],LL2[%d],LL3[%d],LL4[%d],LL5[%d],LL6[%d],LL7[%d],LL8[%d],LL9[%d]\n",LL1,LL2,LL3,LL4,LL5,LL6,LL7,LL8,LL9); 118 | 119 | } 120 | 121 | static void u_set_speed(DWORD cy) 122 | { 123 | 124 | int cnt = 0; 125 | 126 | cmdbuf[0] = 0; 127 | cmdbuf[1] = 0; 128 | cmdbuf[2] = CMD_TAP_SETSPD; 129 | *(DWORD*)(cmdbuf+3) = cy; 130 | 131 | cnt = libusb_bulk_write (cmdbuf, 7); 132 | if (cnt != 7) 133 | printf("STM HID-Brjtag set speed wr error (%d)\n",cnt); 134 | 135 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_RDY) {;} 136 | cnt = libusb_bulk_read (cmdbuf, 200); 137 | if(cnt!=1 || cmdbuf[0] ) 138 | printf("MCU set speed error!\n"); 139 | } 140 | 141 | static void u_get_mcuversion(void) 142 | { 143 | 144 | int cnt = 0; 145 | int iii = 1; 146 | 147 | while(iii--) 148 | { 149 | 150 | cmdbuf[0] = 0; 151 | cmdbuf[1] = 0; 152 | cmdbuf[2] = CMD_TAP_GETHWVER; 153 | cnt = libusb_bulk_write (cmdbuf, 3); 154 | if (cnt != 3) 155 | printf("STM HID-Brjtag get HW version wr error (%d)\n",cnt); 156 | 157 | cnt = libusb_bulk_read (cmdbuf, 200); 158 | if(cnt == 4 && cmdbuf[0] == 'B' && cmdbuf[1] == 'r') 159 | printf("HID-Brjtag MCU ROM version: %d.%02d on STM32!\n",cmdbuf[3],cmdbuf[2]); 160 | else 161 | printf("HID-Brjtag MCU ROM version not fetched!!\n"); 162 | 163 | } 164 | 165 | } 166 | 167 | static int u_get_mcustate(void) 168 | { 169 | 170 | int cnt = 0; 171 | 172 | cnt = libusb_msg_read(CUSTOM_RQ_GET_STS, 0, 0, cmdbuf, 200); 173 | 174 | if(cnt>0 ) 175 | { printf("MCU st = %d\n",cmdbuf[0]); 176 | return (cmdbuf[0] | 0x80); 177 | } 178 | else 179 | { 180 | printf("MCU st fetch error\n"); 181 | return 0; 182 | } 183 | } 184 | 185 | static void fill_cable_prop(void *p) 186 | { 187 | cable_prop_type *pcbl = (cable_prop_type*)p; 188 | 189 | pcbl->feature = 0; 190 | pcbl->close = stclose; 191 | pcbl->test_reset = sttest_reset; 192 | pcbl->det_instr = stdet_instr; 193 | pcbl->set_instr = stset_instr; 194 | pcbl->ReadWriteData = stReadWriteData; 195 | pcbl->ReadData = stReadData; 196 | pcbl->WriteData = stWriteData; 197 | 198 | pcbl->feature |= CBL_DMA_RD|CBL_DMA_WR; 199 | pcbl->ejtag_dma_read_x = stejtag_dma_read_x; 200 | pcbl->ejtag_dma_write_x =stejtag_dma_write_x; 201 | 202 | pcbl->ejtag_pracc_read_x = 0; 203 | pcbl->ejtag_pracc_write_x = 0; 204 | 205 | pcbl->sflash_blkread = st_sflash_blkread; 206 | pcbl->sflash_blkwrite = st_sflash_blkwrite; 207 | } 208 | 209 | ////////////////////// iNIT USB device ///////////////////////////////////////////////////// 210 | int stinit(void *p) 211 | { 212 | int ii=1; 213 | int vref; 214 | DWORD cap, tmp; 215 | char vname[]="Brjtag"; 216 | char pname[]="HID-Brjtag"; 217 | int oo; 218 | 219 | unsigned char buf[255]; 220 | int cmd; 221 | int cnt; 222 | DWORD * ptr; 223 | 224 | fill_cable_prop(p); 225 | 226 | u_getconfig(); 227 | 228 | libusb_open(USBID, JL_EP1 , JL_EP2, 0, vname,NULL,NULL); 229 | 230 | 231 | if(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) 232 | { 233 | libusb_msg_write(CUSTOM_RQ_RESET, 0, 0, st, 0); 234 | } 235 | 236 | u_get_mcuversion(); 237 | 238 | u_set_speed(LL1); 239 | 240 | return 1; 241 | 242 | } 243 | 244 | 245 | void stclose(void) 246 | { 247 | libusb_close(); 248 | } 249 | 250 | 251 | void sttest_reset(void) 252 | { 253 | 254 | int cnt = 0; 255 | int kk ; 256 | 257 | cmdbuf[0] = 0; 258 | cmdbuf[1] = 0; 259 | cmdbuf[2] = CMD_TAP_RESET; 260 | 261 | cnt = libusb_bulk_write (cmdbuf, 3); 262 | if (cnt != 3) 263 | printf("STM HID-Brjtag tap reset wr error (%d)\n",cnt); 264 | 265 | mssleep(1); 266 | cnt = libusb_bulk_read (cmdbuf, 200); 267 | if(cnt!=1 || cmdbuf[0] ) 268 | printf("MCU reset error!\n"); 269 | 270 | } 271 | 272 | 273 | 274 | 275 | DWORD stdet_instr(void) 276 | { 277 | 278 | int cnt = 0; 279 | int kk; 280 | DWORD * dd; 281 | 282 | cmdbuf[0] = 0; 283 | cmdbuf[1] = 0; 284 | cmdbuf[2] = CMD_TAP_DETIR; 285 | cmdbuf[3] = 32; //32 286 | dd = (DWORD*)(cmdbuf + 4); 287 | *dd = 0xFFFFFFFF; 288 | 289 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 290 | cnt = libusb_bulk_write (cmdbuf, 8); 291 | 292 | if(cnt !=8) 293 | { 294 | printf(" cmd [detir] write to usb error! len = %d\n", cnt); 295 | return 0; 296 | } 297 | 298 | // ussleep(100); 299 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_RDY) {;} 300 | cnt = libusb_bulk_read (cmdbuf, 20); 301 | if(cnt !=4) 302 | {printf(" cmd [detir] read from usb error! len = %d\n", cnt); 303 | return 0; 304 | } 305 | 306 | dd = (DWORD*)cmdbuf; 307 | // printf(" det ir 0x%08X\n",*dd); 308 | return *dd; 309 | 310 | } 311 | 312 | 313 | DWORD stset_instr( DWORD instr) 314 | { 315 | 316 | int cnt = 0; 317 | DWORD* dd; 318 | // int dlen; 319 | 320 | cmdbuf[0] = 0; 321 | cmdbuf[1] = 0; 322 | cmdbuf[2] = CMD_TAP_SETIR; 323 | cmdbuf[3] = instruction_length; 324 | dd = (DWORD*)(cmdbuf + 4); 325 | *dd = instr; 326 | // dlen = (cmdbuf[1]+7)>>3; 327 | 328 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 329 | cnt = libusb_bulk_write (cmdbuf, 8); 330 | 331 | if(cnt != 8) 332 | { 333 | printf(" cmd [setir] write to usb error! len = %d\n", cnt); 334 | } 335 | ussleep(100); 336 | 337 | dd = (DWORD*)cmdbuf; 338 | // printf(" set ir 0x%08X\n",*dd); 339 | return *dd; 340 | 341 | } 342 | 343 | 344 | ////////////////////////////////////////////////////////////////////////////////////// 345 | // RW 32bits 346 | DWORD stReadWriteData(DWORD data) 347 | { 348 | int cnt = 0; 349 | DWORD* dd; 350 | int kk; 351 | 352 | cmdbuf[0] = 0; 353 | cmdbuf[1] = 0; 354 | cmdbuf[2] = CMD_TAP_DR32; //det ir 355 | dd = (DWORD*)(cmdbuf +3); 356 | *dd = data; 357 | 358 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 359 | cnt = libusb_bulk_write (cmdbuf, 7); 360 | 361 | if(cnt !=7) 362 | { 363 | printf(" cmd [rwdata] write to usb error! len = %d\n", cnt); 364 | return 0; 365 | } 366 | 367 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_RDY) {;} 368 | cnt = libusb_bulk_read (cmdbuf, 200); 369 | if(cnt !=4) 370 | {printf(" cmd [rwdata] read from usb error! len = %d\n", cnt); 371 | return 0; 372 | } 373 | 374 | dd = (DWORD*)cmdbuf; 375 | return *dd; 376 | } 377 | 378 | // RO 32bits 379 | DWORD stReadData(void) 380 | { 381 | return stReadWriteData(0); 382 | } 383 | 384 | // WO 32bits 385 | void stWriteData(DWORD data) 386 | { 387 | 388 | stReadWriteData(data); 389 | 390 | } 391 | 392 | 393 | 394 | /////////////////////////////////////////////////////////////////////////// 395 | 396 | DWORD stejtag_dma_read_x(DWORD addr, int mode) 397 | { 398 | DWORD data; 399 | int k; 400 | int kk; 401 | int cnt = 0; 402 | DWORD* dd; 403 | 404 | cmdbuf[0] = 0; 405 | cmdbuf[1] = 0; 406 | cmdbuf[2] = CMD_TAP_DMAREAD; 407 | cmdbuf[3] = (mode&0x03); //| (instruction_length <<4); 408 | dd = (DWORD*)(cmdbuf +4); 409 | *dd = addr; 410 | 411 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 412 | cnt = libusb_bulk_write (cmdbuf, 8); 413 | 414 | if(cnt !=8) 415 | { 416 | printf(" cmd [dmard] write to usb error! len = %d\n", cnt); 417 | return 0xFFFFFFFF; 418 | } 419 | 420 | 421 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_RDY) {ussleep(100);} 422 | cnt = libusb_bulk_read (cmdbuf, 200); 423 | if(cnt !=4) 424 | {printf(" cmd [dmard] read from usb error! len = %d\n", cnt); 425 | return 0xFFFFFFFF; 426 | } 427 | data = *(DWORD*)(cmdbuf); 428 | 429 | switch(mode) 430 | { 431 | case MIPS_WORD: 432 | break; 433 | 434 | case MIPS_HALFWORD: 435 | k = addr & 0x2; 436 | if (BigEndian) 437 | data = (data >> (8*(2-k))) & 0xffff; //low 16 at high 438 | else //little 439 | data = (data >> (8*k)) & 0xffff; //low 16 at low 440 | break; 441 | 442 | case MIPS_BYTE: 443 | k = addr & 0x3; 444 | if (BigEndian) 445 | data = (data >> (8*(3-k))) & 0xff; //low 8 at high 446 | else //little 447 | data = (data >> (8*k)) & 0xff; //low 8 at low 448 | break; 449 | 450 | default: //not supported mode 451 | data = 0xFFFFFFFF; 452 | break; 453 | } 454 | return(data); 455 | } 456 | 457 | 458 | void stejtag_dma_write_x(DWORD addr, DWORD data, int mode) 459 | { 460 | 461 | int cnt = 0; 462 | DWORD* dd; 463 | 464 | cmdbuf[0] = 0; 465 | cmdbuf[1] = 0; 466 | cmdbuf[2] = CMD_TAP_DMAWRITE; 467 | cmdbuf[3] = (mode&0x03); // | (instruction_length <<4); 468 | dd = (DWORD*)(cmdbuf +4); 469 | *dd = addr; 470 | dd = (DWORD*)(cmdbuf +8); 471 | *dd = data; 472 | 473 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 474 | cnt = libusb_bulk_write (cmdbuf, 12); 475 | if(cnt !=12) 476 | { 477 | printf(" cmd [dmawr] write to usb error! len = %d\n", cnt); 478 | printf(" cmd [dmawr] addr,data %08X,%08X\n", addr,data); 479 | return; 480 | } 481 | ussleep(100); 482 | } 483 | 484 | 485 | //////////////////////////////////////////////////////////////////// 486 | 487 | int st_sflash_blkread(DWORD Inaddr, DWORD* pbuff, int len) 488 | { 489 | int ii; 490 | DWORD data; 491 | int k; 492 | int cnt = 0; 493 | DWORD* dd; 494 | 495 | len = (len > 512) ? 512 : len; 496 | 497 | cmdbuf[0] = 0; 498 | cmdbuf[1] = 0; 499 | cmdbuf[2] = CMD_TAP_DMABLKRD32; 500 | cmdbuf[3] = MIPS_WORD; // | (instruction_length <<4); 501 | dd = (DWORD*)(cmdbuf +4); 502 | *dd = Inaddr; 503 | *(WORD*)(cmdbuf +8) = len; 504 | 505 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 506 | cnt = libusb_bulk_write (cmdbuf, 10); 507 | 508 | if(cnt !=10) 509 | { 510 | printf(" cmd [dmablkrd] write to usb error! len = %d\n", cnt); 511 | return 0; 512 | } 513 | 514 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_RDY) {ussleep(50);} 515 | cnt = libusb_bulk_read (cmdbuf, 2100); 516 | 517 | if(cnt <4*len) 518 | {printf(" cmd [dmablkrd] read from usb error! len = %d\n", cnt); 519 | return 0; 520 | } 521 | 522 | for( ii =0; ii200) ilen = 200; // supported max x16 mode DWORD number, guarentee buffer not overflow. 547 | 548 | if(len == 0) return ilen; // answer back the caller query result for max supported block size 549 | if(len >ilen) len = ilen; 550 | 551 | gIndex = 0; //buffer clean 552 | xfer_op = flpg_x8?MIPS_BYTE:MIPS_HALFWORD; 553 | ilen = flpg_x8?4:2; 554 | 555 | // file cmd head 556 | cmdbuf[0] = 0; 557 | cmdbuf[1] = 0; 558 | cmdbuf[2] = CMD_TAP_FLSHBLKWR; 559 | cmdbuf[3] = xfer_op ; //| (instruction_length <<4); 560 | dd = (DWORD*)(cmdbuf +4); 561 | *dd = FLASH_MEMORY_START; 562 | dl = (WORD*)(cmdbuf +8); 563 | *dl = len*ilen; 564 | dd = (DWORD*)(cmdbuf +10); 565 | *dd = Inaddr; 566 | gIndex +=14; 567 | 568 | for(ii = 0; ii < len; ii ++) 569 | { 570 | data = pbuff[ii]; 571 | addr = Inaddr + 4*ii; 572 | 573 | 574 | if (flpg_x8) 575 | { 576 | u_buf_write_x8((addr& (~3)), data); 577 | u_buf_delayus(LL4); 578 | 579 | u_buf_write_x8((addr & (~3))+ 1, data); 580 | u_buf_delayus(LL4); 581 | 582 | u_buf_write_x8((addr & (~3))+ 2, data); 583 | u_buf_delayus(LL4); 584 | 585 | u_buf_write_x8((addr & (~3))+ 3, data); 586 | u_buf_delayus(LL4); 587 | 588 | } else { 589 | 590 | u_buf_write_x16((addr& (~3)), data); 591 | u_buf_delayus(LL4); 592 | 593 | u_buf_write_x16((addr& (~3))+2, data); 594 | u_buf_delayus(LL4); 595 | } 596 | 597 | } 598 | 599 | if ( (cmd_type != CMD_TYPE_AMD) || !bypass ) 600 | { 601 | // fetch polling data 602 | cmdbuf[gIndex] = CMD_TAP_DMABLKRD32; 603 | cmdbuf[gIndex+1] = MIPS_WORD; // | (instruction_length <<4); 604 | dd = (DWORD*)(cmdbuf +gIndex +2); 605 | *dd = Inaddr; 606 | dd = (DWORD*)(cmdbuf +gIndex +6); 607 | *dd = (DWORD)len & 0xFF;; 608 | gIndex +=7; 609 | if(gIndex&1) gIndex++; 610 | 611 | } 612 | 613 | // if(gIndex%64 ==0) //mcu rx len can't equal to N*64 614 | *(WORD*)cmdbuf = gIndex; 615 | // cmdbuf[gIndex++] = CMD_TAP_NOOP; 616 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_IDLE) {;} 617 | cnt = libusb_bulk_write (cmdbuf, gIndex); 618 | 619 | if(cnt != gIndex) 620 | { 621 | printf(" cmd [flshwrite] write to usb error! len = %d\n", cnt); 622 | return 0; 623 | } 624 | 625 | 626 | if ( (cmd_type != CMD_TYPE_AMD) || !bypass ) 627 | { 628 | while(libusb_msg_read(CUSTOM_RQ_GET_STATUS, 0, 0, st, 1)==1 && st[0]!=ST_MCU_RDY) {ussleep(50);} 629 | cnt = libusb_bulk_read ((BYTE*)tmpbuf, 2100); 630 | 631 | if(cnt <4*len) 632 | {printf(" cmd [flshwrite] read from usb error! len = %d\n", cnt); 633 | return 0; 634 | exit(1); 635 | } 636 | // check polling data 637 | 638 | for(ii = 0; ii < len; ii ++) 639 | { 640 | data = *(pbuff + ii); 641 | data_poll = *(tmpbuf + ii); 642 | if (BigEndian) data_poll = rev_endian(data_poll); 643 | if (data_poll != data) 644 | {printf("\n dma write not correctly !!\n[%d] %08X - %08X\n",ii,data,data_poll); 645 | } 646 | } 647 | 648 | } 649 | 650 | 651 | return len ; 652 | } 653 | 654 | 655 | #define LEMASK16(k) (0xffff<<(16*(k))) 656 | #define BEMASK16(k) (0xffff<<(16*(1-(k)))) 657 | 658 | static void u_buf_write_x16(DWORD addr, DWORD data) 659 | { 660 | int k; 661 | DWORD odata,ldata; 662 | 663 | k = (addr & 0x2)>>1; 664 | 665 | if (BigEndian) 666 | { 667 | odata = rev_endian(data) & BEMASK16(k); 668 | ldata = odata >>(16*(1-k)); 669 | } 670 | else 671 | { 672 | odata = data & LEMASK16(k); 673 | ldata = odata >>(16*k); 674 | } 675 | 676 | if( ldata == 0xffff) {skip =1;return;} // no need to program 677 | switch( cmd_type ) 678 | { 679 | case CMD_TYPE_AMD: 680 | if (!bypass) 681 | { 682 | u_buf_write_I(AMD16_I1); 683 | u_buf_write_I(AMD16_I2); 684 | } 685 | u_buf_write_I(AMD16_I3); 686 | break; 687 | case CMD_TYPE_SST: 688 | u_buf_write_I(SST16_I1); 689 | u_buf_write_I(SST16_I2); 690 | u_buf_write_I(SST16_I3); 691 | break; 692 | case CMD_TYPE_BCS: 693 | case CMD_TYPE_SCS: 694 | default: 695 | u_buf_write_I(INTL16_I1); 696 | u_buf_write_I(INTL16_I2); 697 | } 698 | 699 | u_buf_write(odata); 700 | } 701 | 702 | #define LEMASK8(k) (0xff<<(8*(k))) 703 | #define BEMASK8(k) (0xff<<(8*(3-(k)))) 704 | 705 | static void u_buf_write_x8(DWORD addr, DWORD data) 706 | { 707 | int k; 708 | DWORD odata,ldata; 709 | 710 | k = addr & 0x3; 711 | 712 | ldata = (data >> (8*k))&0xFF; 713 | odata = ldata | (ldata <<8); 714 | odata |= (odata<<16); 715 | 716 | // printf(" I am in x8, addr, data = %08x, %08x, %08x\n", addr, data,ldata); 717 | if( ldata == 0xff) {skip =1;return;} // no need to program 718 | 719 | switch( cmd_type ) 720 | { 721 | case CMD_TYPE_AMD: 722 | if (!bypass) 723 | { 724 | u_buf_write_I(AMD8_I1); 725 | u_buf_write_I(AMD8_I2); 726 | } 727 | u_buf_write_I(AMD8_I3); 728 | break; 729 | case CMD_TYPE_SST: 730 | return; //SST 39 don't support x8 731 | break; 732 | case CMD_TYPE_BCS: 733 | case CMD_TYPE_SCS: 734 | default: 735 | u_buf_write_I(INTL8_I1); 736 | u_buf_write_I(INTL8_I2); 737 | } 738 | 739 | u_buf_write(odata); 740 | 741 | } 742 | 743 | static void u_buf_write(DWORD data) 744 | { 745 | DWORD *dd; 746 | 747 | cmdbuf[gIndex +0] = 0; 748 | dd = (DWORD*)(cmdbuf + gIndex +1); 749 | *dd = data; 750 | 751 | gIndex +=5; 752 | 753 | } 754 | 755 | static void u_buf_write_I(DWORD id) 756 | { 757 | 758 | cmdbuf[gIndex+0] = (BYTE)(0x80|id); 759 | gIndex ++; 760 | 761 | } 762 | 763 | 764 | static void u_buf_delayus(DWORD us) 765 | { 766 | 767 | cmdbuf[gIndex +0] = 0xFF; 768 | if(skip) 769 | cmdbuf[gIndex +1] = 0; 770 | else 771 | cmdbuf[gIndex +1] = (BYTE)(us); 772 | gIndex +=2; 773 | skip = 0; 774 | 775 | } 776 | 777 | static void u_bufcmd_delayus(DWORD us) 778 | { 779 | 780 | cmdbuf[gIndex +0] = CMD_TAP_DELAY; 781 | cmdbuf[gIndex +1] = (BYTE)(us); 782 | gIndex +=2; 783 | 784 | } 785 | -------------------------------------------------------------------------------- /stmhid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * created by hugebird @ chinadsl.net 17/9/2010 Brjtag rev1.9m 3 | * 4 | * 5 | * Brjtag application for HID-BRJTAG v1.xx MCU firmware. 6 | * Copyright (C) 2010 Hugebird 7 | * 8 | * This code is covered by the GPL v2. 9 | * 10 | * 11 | * 12 | */ 13 | 14 | #if ( defined(_MSC_VER) ) 15 | #define WINDOWS_VERSION 16 | #endif 17 | 18 | #ifdef WINDOWS_VERSION 19 | #include "windows.h" 20 | 21 | #else 22 | 23 | #ifndef MY_TYPE 24 | #define MY_TYPE 25 | typedef uint32_t DWORD; 26 | typedef uint16_t WORD; 27 | typedef unsigned char BYTE; 28 | typedef int bool; 29 | #endif 30 | 31 | #endif 32 | 33 | #ifdef STMHID 34 | 35 | typedef enum _JtagStates { 36 | JTAG_TLR = 0, 37 | JTAG_RTI = 1, 38 | JTAG_PDR = 2, 39 | JTAG_PIR = 3, 40 | JTAG_SDR = 4, 41 | JTAG_SIR = 5, 42 | Undefined = 6} JtagStates; 43 | 44 | #define MAX_JTAG_STATES 6 45 | 46 | static const BYTE JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 47 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 48 | /* from TLR 0-> */ {'\x01', '\x00', '\x0A', '\x16', '\x02', '\x06'}, 49 | /* from RTI 1-> */ {'\x07', '\x00', '\x05', '\x0B', '\x01', '\x03'}, 50 | /* from PDR 2-> */ {'\x1F', '\x03', '\x00', '\x2F', '\x01', '\x0F'}, 51 | /* from PIR 3-> */ {'\x1F', '\x03', '\x17', '\x00', '\x07', '\x01'}, 52 | /* from SDR 4-> */ {'\x1F', '\x03', '\x01', '\x2F', '\x00', '\x0F'}, 53 | /* from SIR 5-> */ {'\x1F', '\x03', '\x17', '\x01', '\x07', '\x00'} }; 54 | 55 | static const BYTE TMSCLK_JTAGST_FromTo[MAX_JTAG_STATES][MAX_JTAG_STATES] = { 56 | /* 0TLR 1RTI 2PDR 3PIR 4SDR 5SIR */ 57 | /* from TLR 0-> */ {1, 1, 5, 6, 4, 5}, 58 | /* from RTI 1-> */ {3, 1, 4, 5, 3, 4}, 59 | /* from PDR 2-> */ {5, 3, 1, 7, 2, 6}, 60 | /* from PIR 3-> */ {5, 3, 6, 1, 5, 2}, 61 | /* from SDR 4-> */ {5, 3, 2, 7, 0, 6}, 62 | /* from SIR 5-> */ {5, 3, 6, 2, 5, 0} }; 63 | 64 | #define JSTPATH_EXR_TO_RTI 0x01 65 | #define JSTPLEN_EXR_TO_RTI 4 66 | 67 | // flsh cmd id 68 | #define AMD16_I1 0 69 | #define AMD16_I2 1 70 | #define AMD16_I3 2 71 | #define AMD8_I1 3 72 | #define AMD8_I2 4 73 | #define AMD8_I3 5 74 | #define SST16_I1 6 75 | #define SST16_I2 7 76 | #define SST16_I3 8 77 | #define INTL16_I1 9 78 | #define INTL16_I2 10 79 | #define INTL8_I1 11 80 | #define INTL8_I2 12 81 | 82 | 83 | //--------------------------------------------------------- 84 | // _____ _______ _______ 85 | // | DUT |___________|TAP/MCU|__________|PC/HOST| 86 | // ----- JTAG ------- USB ------- 87 | // BCM MCU FW BRJTAG 88 | //--------------------------------------------------------- 89 | 90 | /* MCU Processing State Machine */ 91 | #define ST_MCU_IDLE 0x01 //ideal, can accept any cmd 92 | #define ST_MCU_XFER 0x02 //on USB data Oxfer, not accept any new cmd 93 | #define ST_MCU_XFERR 0x03 //on USB data Ixfer, not accept any new cmd 94 | #define ST_MCU_BUSY 0x04 //on JTAG process 95 | #define ST_MCU_RDY 0x05 //on JTAG op complete, reply buffer ready 96 | 97 | // idle -> xfer -> busy -> ready -> xferr -> idle 98 | 99 | 100 | /* Host <-> MCU COMMAND ID rq->bRequest */ 101 | 102 | #define CUSTOM_RQ_GET_STATUS 0x21 // host use to check jtag processing is ready 103 | #define CUSTOM_RQ_DUMP_BUFFER 0x22 // dump buffer 104 | #define CUSTOM_RQ_ECHO 0x23 // bulk loopback 105 | #define CUSTOM_RQ_RESET 0x24 // init usb dev 106 | #define CUSTOM_RQ_GET_STS 0x25 // dump st buffer 107 | 108 | //cmd sequence format (total length <= 4100 bytes) 109 | //[0,1 total len]<[cmd id 0][cmd data 0]><[cmd id 1][cmd data 1]><3><4> ... 110 | // 111 | // Command ID: 112 | #define CMD_TAP_DELAY 0x01 //pause DUT <-> TAP 113 | //In: [ 0:id ][1 : us] out:none 114 | #define CMD_TAP_CLKIO 0x02 //bit-wise clock shift in/out, 115 | //In: [0: id][1,2:bit len][3,4,...,x+2][x+3,...,2x+2] out:[0,..., x-1], x=(bit+7)/8 116 | #define CMD_TAP_DETIR 0x03 //IR shift/scan a instruction and return 117 | //In: [0:id][1:ir bit len][2,3,4,5 ir data] out:[0,1,2,3] 118 | #define CMD_TAP_SETIR 0x04 //IR shift/scan a instruction 119 | //In: [0:id][1:ir bit len][2,3,4,5 ir data] out:none 120 | #define CMD_TAP_DR32 0x05 //DR shift/scan a 32b Data 121 | //In: [0:id][1,2,3,4 ir data] out:[0,1,2,3] 122 | #define CMD_TAP_DMAREAD 0x06 //DMA Read a 32b data 123 | //In: [0:1d][1:type][2,3,4,5:ADDR] out:[0,1,2,3] 124 | #define CMD_TAP_DMAWRITE 0x07 //DMA write a 32b data 125 | //In: [0:1d][1:type][2,3,4,5:ADDR][6,7,8,9:data] out:None 126 | #define CMD_TAP_DMABLKRD32 0x08 //DMA Read a data block 127 | //In: [0:1d][1:type][2,3,4,5:ADDR][6:len, mode in type] out:[len*4bytes,each 4bytes in set mode,need host post process] 128 | #define CMD_TAP_FLSHBLKWR 0x09 //flash blk write 129 | //In: ... 130 | // : <[0:1d][1:type][2,3,4,5:BASE ADDR][6,7:len][8,9,10,11:WR Target ADDR]> , len = 12 131 | // :<0:I1><1:I2><2:I3><3:D><4,5,6,7:data><8:L><9:us> , len = 10 132 | // Ix: 0x80~0x8F, flash unlock cmd, I1~I3 for AMD is 0xAA, 0x55, 0xA0. 133 | // if open bypass mode, only I3 used 0xA0 at datax seq offset 0 134 | // D: 0x00, follow 4 bytes are data, whatever the write type(x8,x16) 135 | // L: 0xFF, means flash program waiting, 136 | // waiting time is give in follow us bytes 137 | // for a word program, maximum write step is 5, (I1,I2,I3,D,L) 138 | // for bypass mode, write step is 3, (I3,D,L) 139 | // 140 | //: None 141 | // 142 | // 143 | //type: 144 | #define CMDT_MOD 0x03 //op type mask, word, halfw, byte,tripw 145 | #define CMDT_END (1<<2) //endian mask 146 | #define CMDT_DMA (1<<3) //1:DMA 2:Pracc 147 | 148 | // other command 149 | #define CMD_TAP_RESET 0x20 //reset tap 150 | //In: [0:id] out:[0] [0] =0 success 151 | #define CMD_TAP_GETHWVER 0x21 //get hardware version 152 | //In: [0:id] out:[0,1,2,3] len =4 [0,1] = 'Br' 153 | #define CMD_TAP_SETSPD 0x22 //set speed 154 | //In: [0:id][1,2,3,4: cycle delay] out:[0] [0] =0 success 155 | #define CMD_TAP_GETSTT 0x23 //get mcu_st 156 | //In: [0:id] out:[0~99] 157 | 158 | #define CMD_TAP_NOOP 0xFE //no op, padding a byte to seq 159 | //In: [0:id] 160 | // 161 | //**************************************************************************** 162 | //**************************************************************************** 163 | //**************************************************************************** 164 | //Bit sequence scan: bit by bit scan TDI/TMS to DUT and return TDO(len<=800 bits) 165 | //<=202 bytes) 166 | // 167 | //In Byte: [ 0,1 ] [ 2,3,4...xxx][xxx+1,...,2xxx-1] 168 | // bit LEN TDI TMS 169 | // 170 | //Out Byte: [0,1,2,3,4...xxx-2] 171 | // TDO 172 | //**************************************************************************** 173 | 174 | // Speed *KHz 175 | #define JL_SPEED_12M 12000 176 | #define JL_SPEED_1M 1000 177 | 178 | // usb endpoint 179 | #define JL_EP1 0x81 //for read 180 | #define JL_EP2 0x02 //for write 181 | 182 | // USB Output/Input Buffer byte length 183 | #define MAX_NUM_BYTES_USB_BUFFER 4096 184 | #define JL_MAX_BUFFER_SIZE 3000 //v8 has enough room to keep 3000bytes, eg total out buffer 3000*2+4 = 6004 bytes 185 | // speced in/out total buffer len is 2048bytes, total 4+1000*2 = 2004 bytes. 186 | // tms_delay sequence need limit to 10 bytes. 187 | 188 | // TX/RX data buffer 189 | #define MAX_DATA_BUFFER_SIZE 65536 190 | #define MAX_DATA_BLOCK_SIZE 20000 191 | 192 | 193 | #define MAX_DMA_BLOCK_SIZE 400 //Max L9 DMA xfer block size for 65536 bytes buffer. 194 | #define DMA_BLOCK_SIZE 256 //L6 defult DMA xfer block size 195 | #define DMA_POLLING 2 //L3 defult polling retry time on DMA xfering 196 | #define FLASH_POLLING 16 //L4 defult polling retry time on flash writing 197 | 198 | 199 | // port scan data type 200 | #define TAP_RD 0x80 // need return data 201 | #define TAP_IR 1 202 | #define TAP_DATA 2 203 | #define TAP_TMS_DELAY 3 204 | #define TAP_TMS_MOVE 4 205 | 206 | // DMA xfer type 207 | #define XFER_TX 1 // blcok xfer, add cmd to queue, internal flag 208 | #define XFER_RX 2 // block xfer, get data from queue, internal flag 209 | 210 | #define XFER_ADD 3 // add DMA xfer block to queue, for caller 211 | #define XFER_QRY 4 // find next read data block,fetch out, for caller 212 | #define XFER_NOOP 5 // no operate, return free dma xfer block count 213 | 214 | #define DMA_WR 1 // dma write operate 215 | #define DMA_RD 2 // dma read operate 216 | 217 | 218 | 219 | //--------------private------------------------------------------- 220 | 221 | 222 | static void u_getconfig(void); 223 | static int u_get_mcustate(void); 224 | static void u_get_mcuversion(void); 225 | static void u_set_speed(DWORD cy); 226 | static void u_buf_write_x16(DWORD addr, DWORD data); 227 | static void u_buf_write_x8(DWORD addr, DWORD data); 228 | static void u_buf_delayus(DWORD us); 229 | static void u_buf_write(DWORD data); 230 | static void u_bufcmd_delayus(DWORD us); 231 | static void u_buf_write_I(DWORD id); 232 | static void fill_cable_prop(void *p); 233 | 234 | //--------------------pub------------------------------------- 235 | int stinit(void *); 236 | void stclose(void); 237 | void sttest_reset(void); 238 | DWORD stdet_instr(void); 239 | DWORD stset_instr( DWORD instr); 240 | DWORD stReadWriteData(DWORD data); 241 | DWORD stReadData(void); 242 | void stWriteData(DWORD data); 243 | DWORD stejtag_dma_read_x(DWORD addr, int mode); 244 | void stejtag_dma_write_x(DWORD addr, DWORD data, int mode); 245 | int st_sflash_blkread(DWORD Inaddr, DWORD* pbuff, int len); 246 | int st_sflash_blkwrite(DWORD Inaddr, DWORD* pbuff, int len, int flpg_x8); 247 | 248 | #endif 249 | 250 | 251 | 252 | 253 | 254 | 255 | -------------------------------------------------------------------------------- /usb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Prototypes, structure definitions and macros. 3 | * 4 | * Copyright (c) 2000-2003 Johannes Erdfelt 5 | * 6 | * This library is covered by the LGPL, read LICENSE for details. 7 | * 8 | * This file (and only this file) may alternatively be licensed under the 9 | * BSD license as well, read LICENSE for details. 10 | */ 11 | #ifndef __USB_H__ 12 | #define __USB_H__ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | /* 21 | * USB spec information 22 | * 23 | * This is all stuff grabbed from various USB specs and is pretty much 24 | * not subject to change 25 | */ 26 | 27 | /* 28 | * Device and/or Interface Class codes 29 | */ 30 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ 31 | #define USB_CLASS_AUDIO 1 32 | #define USB_CLASS_COMM 2 33 | #define USB_CLASS_HID 3 34 | #define USB_CLASS_PRINTER 7 35 | #define USB_CLASS_PTP 6 36 | #define USB_CLASS_MASS_STORAGE 8 37 | #define USB_CLASS_HUB 9 38 | #define USB_CLASS_DATA 10 39 | #define USB_CLASS_VENDOR_SPEC 0xff 40 | 41 | /* 42 | * Descriptor types 43 | */ 44 | #define USB_DT_DEVICE 0x01 45 | #define USB_DT_CONFIG 0x02 46 | #define USB_DT_STRING 0x03 47 | #define USB_DT_INTERFACE 0x04 48 | #define USB_DT_ENDPOINT 0x05 49 | 50 | #define USB_DT_HID 0x21 51 | #define USB_DT_REPORT 0x22 52 | #define USB_DT_PHYSICAL 0x23 53 | #define USB_DT_HUB 0x29 54 | 55 | /* 56 | * Descriptor sizes per descriptor type 57 | */ 58 | #define USB_DT_DEVICE_SIZE 18 59 | #define USB_DT_CONFIG_SIZE 9 60 | #define USB_DT_INTERFACE_SIZE 9 61 | #define USB_DT_ENDPOINT_SIZE 7 62 | #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 63 | #define USB_DT_HUB_NONVAR_SIZE 7 64 | 65 | /* All standard descriptors have these 2 fields in common */ 66 | struct usb_descriptor_header { 67 | u_int8_t bLength; 68 | u_int8_t bDescriptorType; 69 | }; 70 | 71 | /* String descriptor */ 72 | struct usb_string_descriptor { 73 | u_int8_t bLength; 74 | u_int8_t bDescriptorType; 75 | u_int16_t wData[1]; 76 | }; 77 | 78 | /* HID descriptor */ 79 | struct usb_hid_descriptor { 80 | u_int8_t bLength; 81 | u_int8_t bDescriptorType; 82 | u_int16_t bcdHID; 83 | u_int8_t bCountryCode; 84 | u_int8_t bNumDescriptors; 85 | /* u_int8_t bReportDescriptorType; */ 86 | /* u_int16_t wDescriptorLength; */ 87 | /* ... */ 88 | }; 89 | 90 | /* Endpoint descriptor */ 91 | #define USB_MAXENDPOINTS 32 92 | struct usb_endpoint_descriptor { 93 | u_int8_t bLength; 94 | u_int8_t bDescriptorType; 95 | u_int8_t bEndpointAddress; 96 | u_int8_t bmAttributes; 97 | u_int16_t wMaxPacketSize; 98 | u_int8_t bInterval; 99 | u_int8_t bRefresh; 100 | u_int8_t bSynchAddress; 101 | 102 | unsigned char *extra; /* Extra descriptors */ 103 | int extralen; 104 | }; 105 | 106 | #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 107 | #define USB_ENDPOINT_DIR_MASK 0x80 108 | 109 | #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ 110 | #define USB_ENDPOINT_TYPE_CONTROL 0 111 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 112 | #define USB_ENDPOINT_TYPE_BULK 2 113 | #define USB_ENDPOINT_TYPE_INTERRUPT 3 114 | 115 | /* Interface descriptor */ 116 | #define USB_MAXINTERFACES 32 117 | struct usb_interface_descriptor { 118 | u_int8_t bLength; 119 | u_int8_t bDescriptorType; 120 | u_int8_t bInterfaceNumber; 121 | u_int8_t bAlternateSetting; 122 | u_int8_t bNumEndpoints; 123 | u_int8_t bInterfaceClass; 124 | u_int8_t bInterfaceSubClass; 125 | u_int8_t bInterfaceProtocol; 126 | u_int8_t iInterface; 127 | 128 | struct usb_endpoint_descriptor *endpoint; 129 | 130 | unsigned char *extra; /* Extra descriptors */ 131 | int extralen; 132 | }; 133 | 134 | #define USB_MAXALTSETTING 128 /* Hard limit */ 135 | struct usb_interface { 136 | struct usb_interface_descriptor *altsetting; 137 | 138 | int num_altsetting; 139 | }; 140 | 141 | /* Configuration descriptor information.. */ 142 | #define USB_MAXCONFIG 8 143 | struct usb_config_descriptor { 144 | u_int8_t bLength; 145 | u_int8_t bDescriptorType; 146 | u_int16_t wTotalLength; 147 | u_int8_t bNumInterfaces; 148 | u_int8_t bConfigurationValue; 149 | u_int8_t iConfiguration; 150 | u_int8_t bmAttributes; 151 | u_int8_t MaxPower; 152 | 153 | struct usb_interface *interface; 154 | 155 | unsigned char *extra; /* Extra descriptors */ 156 | int extralen; 157 | }; 158 | 159 | /* Device descriptor */ 160 | struct usb_device_descriptor { 161 | u_int8_t bLength; 162 | u_int8_t bDescriptorType; 163 | u_int16_t bcdUSB; 164 | u_int8_t bDeviceClass; 165 | u_int8_t bDeviceSubClass; 166 | u_int8_t bDeviceProtocol; 167 | u_int8_t bMaxPacketSize0; 168 | u_int16_t idVendor; 169 | u_int16_t idProduct; 170 | u_int16_t bcdDevice; 171 | u_int8_t iManufacturer; 172 | u_int8_t iProduct; 173 | u_int8_t iSerialNumber; 174 | u_int8_t bNumConfigurations; 175 | }; 176 | 177 | struct usb_ctrl_setup { 178 | u_int8_t bRequestType; 179 | u_int8_t bRequest; 180 | u_int16_t wValue; 181 | u_int16_t wIndex; 182 | u_int16_t wLength; 183 | }; 184 | 185 | /* 186 | * Standard requests 187 | */ 188 | #define USB_REQ_GET_STATUS 0x00 189 | #define USB_REQ_CLEAR_FEATURE 0x01 190 | /* 0x02 is reserved */ 191 | #define USB_REQ_SET_FEATURE 0x03 192 | /* 0x04 is reserved */ 193 | #define USB_REQ_SET_ADDRESS 0x05 194 | #define USB_REQ_GET_DESCRIPTOR 0x06 195 | #define USB_REQ_SET_DESCRIPTOR 0x07 196 | #define USB_REQ_GET_CONFIGURATION 0x08 197 | #define USB_REQ_SET_CONFIGURATION 0x09 198 | #define USB_REQ_GET_INTERFACE 0x0A 199 | #define USB_REQ_SET_INTERFACE 0x0B 200 | #define USB_REQ_SYNCH_FRAME 0x0C 201 | 202 | #define USB_TYPE_STANDARD (0x00 << 5) 203 | #define USB_TYPE_CLASS (0x01 << 5) 204 | #define USB_TYPE_VENDOR (0x02 << 5) 205 | #define USB_TYPE_RESERVED (0x03 << 5) 206 | 207 | #define USB_RECIP_DEVICE 0x00 208 | #define USB_RECIP_INTERFACE 0x01 209 | #define USB_RECIP_ENDPOINT 0x02 210 | #define USB_RECIP_OTHER 0x03 211 | 212 | /* 213 | * Various libusb API related stuff 214 | */ 215 | 216 | #define USB_ENDPOINT_IN 0x80 217 | #define USB_ENDPOINT_OUT 0x00 218 | 219 | /* Error codes */ 220 | #define USB_ERROR_BEGIN 500000 221 | 222 | /* 223 | * This is supposed to look weird. This file is generated from autoconf 224 | * and I didn't want to make this too complicated. 225 | */ 226 | #if 0 227 | #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0) 228 | #else 229 | #define USB_LE16_TO_CPU(x) 230 | #endif 231 | 232 | /* Data types */ 233 | struct usb_device; 234 | struct usb_bus; 235 | 236 | /* 237 | * To maintain compatibility with applications already built with libusb, 238 | * we must only add entries to the end of this structure. NEVER delete or 239 | * move members and only change types if you really know what you're doing. 240 | */ 241 | struct usb_device { 242 | struct usb_device *next, *prev; 243 | 244 | char filename[PATH_MAX + 1]; 245 | 246 | struct usb_bus *bus; 247 | 248 | struct usb_device_descriptor descriptor; 249 | struct usb_config_descriptor *config; 250 | 251 | void *dev; /* Darwin support */ 252 | 253 | u_int8_t devnum; 254 | 255 | unsigned char num_children; 256 | struct usb_device **children; 257 | }; 258 | 259 | struct usb_bus { 260 | struct usb_bus *next, *prev; 261 | 262 | char dirname[PATH_MAX + 1]; 263 | 264 | struct usb_device *devices; 265 | u_int32_t location; 266 | 267 | struct usb_device *root_dev; 268 | }; 269 | 270 | struct usb_dev_handle; 271 | typedef struct usb_dev_handle usb_dev_handle; 272 | 273 | /* Variables */ 274 | extern struct usb_bus *usb_busses; 275 | 276 | #ifdef __cplusplus 277 | extern "C" { 278 | #endif 279 | 280 | /* Function prototypes */ 281 | 282 | /* usb.c */ 283 | usb_dev_handle *usb_open(struct usb_device *dev); 284 | int usb_close(usb_dev_handle *dev); 285 | int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, 286 | size_t buflen); 287 | int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, 288 | size_t buflen); 289 | 290 | /* descriptors.c */ 291 | int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, 292 | unsigned char type, unsigned char index, void *buf, int size); 293 | int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, 294 | unsigned char index, void *buf, int size); 295 | 296 | /* .c */ 297 | int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, 298 | int timeout); 299 | int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, 300 | int timeout); 301 | int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, 302 | int timeout); 303 | int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, 304 | int timeout); 305 | int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 306 | int value, int index, char *bytes, int size, int timeout); 307 | int usb_set_configuration(usb_dev_handle *dev, int configuration); 308 | int usb_claim_interface(usb_dev_handle *dev, int interface); 309 | int usb_release_interface(usb_dev_handle *dev, int interface); 310 | int usb_set_altinterface(usb_dev_handle *dev, int alternate); 311 | int usb_resetep(usb_dev_handle *dev, unsigned int ep); 312 | int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); 313 | int usb_reset(usb_dev_handle *dev); 314 | 315 | #if 1 316 | #define LIBUSB_HAS_GET_DRIVER_NP 1 317 | int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, 318 | unsigned int namelen); 319 | #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1 320 | int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface); 321 | #endif 322 | 323 | char *usb_strerror(void); 324 | 325 | void usb_init(void); 326 | void usb_set_debug(int level); 327 | int usb_find_busses(void); 328 | int usb_find_devices(void); 329 | struct usb_device *usb_device(usb_dev_handle *dev); 330 | struct usb_bus *usb_get_busses(void); 331 | 332 | #ifdef __cplusplus 333 | } 334 | #endif 335 | 336 | #endif /* __USB_H__ */ 337 | 338 | -------------------------------------------------------------------------------- /usb_w.h: -------------------------------------------------------------------------------- 1 | #ifndef __USB_H__ 2 | #define __USB_H__ 3 | 4 | #include 5 | #include 6 | 7 | /* 8 | * 'interface' is defined somewhere in the Windows header files. This macro 9 | * is deleted here to avoid conflicts and compile errors. 10 | */ 11 | 12 | #ifdef interface 13 | #undef interface 14 | #endif 15 | 16 | /* 17 | * PATH_MAX from limits.h can't be used on Windows if the dll and 18 | * import libraries are build/used by different compilers 19 | */ 20 | 21 | #define LIBUSB_PATH_MAX 512 22 | 23 | 24 | /* 25 | * USB spec information 26 | * 27 | * This is all stuff grabbed from various USB specs and is pretty much 28 | * not subject to change 29 | */ 30 | 31 | /* 32 | * Device and/or Interface Class codes 33 | */ 34 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ 35 | #define USB_CLASS_AUDIO 1 36 | #define USB_CLASS_COMM 2 37 | #define USB_CLASS_HID 3 38 | #define USB_CLASS_PRINTER 7 39 | #define USB_CLASS_MASS_STORAGE 8 40 | #define USB_CLASS_HUB 9 41 | #define USB_CLASS_DATA 10 42 | #define USB_CLASS_VENDOR_SPEC 0xff 43 | 44 | /* 45 | * Descriptor types 46 | */ 47 | #define USB_DT_DEVICE 0x01 48 | #define USB_DT_CONFIG 0x02 49 | #define USB_DT_STRING 0x03 50 | #define USB_DT_INTERFACE 0x04 51 | #define USB_DT_ENDPOINT 0x05 52 | 53 | #define USB_DT_HID 0x21 54 | #define USB_DT_REPORT 0x22 55 | #define USB_DT_PHYSICAL 0x23 56 | #define USB_DT_HUB 0x29 57 | 58 | /* 59 | * Descriptor sizes per descriptor type 60 | */ 61 | #define USB_DT_DEVICE_SIZE 18 62 | #define USB_DT_CONFIG_SIZE 9 63 | #define USB_DT_INTERFACE_SIZE 9 64 | #define USB_DT_ENDPOINT_SIZE 7 65 | #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 66 | #define USB_DT_HUB_NONVAR_SIZE 7 67 | 68 | 69 | /* ensure byte-packed structures */ 70 | #include 71 | 72 | 73 | /* All standard descriptors have these 2 fields in common */ 74 | struct usb_descriptor_header 75 | { 76 | unsigned char bLength; 77 | unsigned char bDescriptorType; 78 | }; 79 | 80 | /* String descriptor */ 81 | struct usb_string_descriptor 82 | { 83 | unsigned char bLength; 84 | unsigned char bDescriptorType; 85 | unsigned short wData[1]; 86 | }; 87 | 88 | /* HID descriptor */ 89 | struct usb_hid_descriptor 90 | { 91 | unsigned char bLength; 92 | unsigned char bDescriptorType; 93 | unsigned short bcdHID; 94 | unsigned char bCountryCode; 95 | unsigned char bNumDescriptors; 96 | }; 97 | 98 | /* Endpoint descriptor */ 99 | #define USB_MAXENDPOINTS 32 100 | struct usb_endpoint_descriptor 101 | { 102 | unsigned char bLength; 103 | unsigned char bDescriptorType; 104 | unsigned char bEndpointAddress; 105 | unsigned char bmAttributes; 106 | unsigned short wMaxPacketSize; 107 | unsigned char bInterval; 108 | unsigned char bRefresh; 109 | unsigned char bSynchAddress; 110 | 111 | unsigned char *extra; /* Extra descriptors */ 112 | int extralen; 113 | }; 114 | 115 | #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 116 | #define USB_ENDPOINT_DIR_MASK 0x80 117 | 118 | #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ 119 | #define USB_ENDPOINT_TYPE_CONTROL 0 120 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 121 | #define USB_ENDPOINT_TYPE_BULK 2 122 | #define USB_ENDPOINT_TYPE_INTERRUPT 3 123 | 124 | /* Interface descriptor */ 125 | #define USB_MAXINTERFACES 32 126 | struct usb_interface_descriptor 127 | { 128 | unsigned char bLength; 129 | unsigned char bDescriptorType; 130 | unsigned char bInterfaceNumber; 131 | unsigned char bAlternateSetting; 132 | unsigned char bNumEndpoints; 133 | unsigned char bInterfaceClass; 134 | unsigned char bInterfaceSubClass; 135 | unsigned char bInterfaceProtocol; 136 | unsigned char iInterface; 137 | 138 | struct usb_endpoint_descriptor *endpoint; 139 | 140 | unsigned char *extra; /* Extra descriptors */ 141 | int extralen; 142 | }; 143 | 144 | #define USB_MAXALTSETTING 128 /* Hard limit */ 145 | 146 | struct usb_interface 147 | { 148 | struct usb_interface_descriptor *altsetting; 149 | 150 | int num_altsetting; 151 | }; 152 | 153 | /* Configuration descriptor information.. */ 154 | #define USB_MAXCONFIG 8 155 | struct usb_config_descriptor 156 | { 157 | unsigned char bLength; 158 | unsigned char bDescriptorType; 159 | unsigned short wTotalLength; 160 | unsigned char bNumInterfaces; 161 | unsigned char bConfigurationValue; 162 | unsigned char iConfiguration; 163 | unsigned char bmAttributes; 164 | unsigned char MaxPower; 165 | 166 | struct usb_interface *interface; 167 | 168 | unsigned char *extra; /* Extra descriptors */ 169 | int extralen; 170 | }; 171 | 172 | /* Device descriptor */ 173 | struct usb_device_descriptor 174 | { 175 | unsigned char bLength; 176 | unsigned char bDescriptorType; 177 | unsigned short bcdUSB; 178 | unsigned char bDeviceClass; 179 | unsigned char bDeviceSubClass; 180 | unsigned char bDeviceProtocol; 181 | unsigned char bMaxPacketSize0; 182 | unsigned short idVendor; 183 | unsigned short idProduct; 184 | unsigned short bcdDevice; 185 | unsigned char iManufacturer; 186 | unsigned char iProduct; 187 | unsigned char iSerialNumber; 188 | unsigned char bNumConfigurations; 189 | }; 190 | 191 | struct usb_ctrl_setup 192 | { 193 | unsigned char bRequestType; 194 | unsigned char bRequest; 195 | unsigned short wValue; 196 | unsigned short wIndex; 197 | unsigned short wLength; 198 | }; 199 | 200 | /* 201 | * Standard requests 202 | */ 203 | #define USB_REQ_GET_STATUS 0x00 204 | #define USB_REQ_CLEAR_FEATURE 0x01 205 | /* 0x02 is reserved */ 206 | #define USB_REQ_SET_FEATURE 0x03 207 | /* 0x04 is reserved */ 208 | #define USB_REQ_SET_ADDRESS 0x05 209 | #define USB_REQ_GET_DESCRIPTOR 0x06 210 | #define USB_REQ_SET_DESCRIPTOR 0x07 211 | #define USB_REQ_GET_CONFIGURATION 0x08 212 | #define USB_REQ_SET_CONFIGURATION 0x09 213 | #define USB_REQ_GET_INTERFACE 0x0A 214 | #define USB_REQ_SET_INTERFACE 0x0B 215 | #define USB_REQ_SYNCH_FRAME 0x0C 216 | 217 | #define USB_TYPE_STANDARD (0x00 << 5) 218 | #define USB_TYPE_CLASS (0x01 << 5) 219 | #define USB_TYPE_VENDOR (0x02 << 5) 220 | #define USB_TYPE_RESERVED (0x03 << 5) 221 | 222 | #define USB_RECIP_DEVICE 0x00 223 | #define USB_RECIP_INTERFACE 0x01 224 | #define USB_RECIP_ENDPOINT 0x02 225 | #define USB_RECIP_OTHER 0x03 226 | 227 | /* 228 | * Various libusb API related stuff 229 | */ 230 | 231 | #define USB_ENDPOINT_IN 0x80 232 | #define USB_ENDPOINT_OUT 0x00 233 | 234 | /* Error codes */ 235 | #define USB_ERROR_BEGIN 500000 236 | 237 | /* 238 | * This is supposed to look weird. This file is generated from autoconf 239 | * and I didn't want to make this too complicated. 240 | */ 241 | #define USB_LE16_TO_CPU(x) 242 | 243 | /* Data types */ 244 | /* struct usb_device; */ 245 | /* struct usb_bus; */ 246 | 247 | struct usb_device 248 | { 249 | struct usb_device *next, *prev; 250 | 251 | char filename[LIBUSB_PATH_MAX]; 252 | 253 | struct usb_bus *bus; 254 | 255 | struct usb_device_descriptor descriptor; 256 | struct usb_config_descriptor *config; 257 | 258 | void *dev; /* Darwin support */ 259 | 260 | unsigned char devnum; 261 | 262 | unsigned char num_children; 263 | struct usb_device **children; 264 | }; 265 | 266 | struct usb_bus 267 | { 268 | struct usb_bus *next, *prev; 269 | 270 | char dirname[LIBUSB_PATH_MAX]; 271 | 272 | struct usb_device *devices; 273 | unsigned long location; 274 | 275 | struct usb_device *root_dev; 276 | }; 277 | 278 | /* Version information, Windows specific */ 279 | struct usb_version 280 | { 281 | struct 282 | { 283 | int major; 284 | int minor; 285 | int micro; 286 | int nano; 287 | } dll; 288 | struct 289 | { 290 | int major; 291 | int minor; 292 | int micro; 293 | int nano; 294 | } driver; 295 | }; 296 | 297 | 298 | struct usb_dev_handle; 299 | typedef struct usb_dev_handle usb_dev_handle; 300 | 301 | /* Variables */ 302 | #ifndef __USB_C__ 303 | #define usb_busses usb_get_busses() 304 | #endif 305 | 306 | 307 | 308 | #include 309 | 310 | 311 | #ifdef __cplusplus 312 | extern "C" 313 | { 314 | #endif 315 | 316 | /* Function prototypes */ 317 | 318 | /* usb.c */ 319 | usb_dev_handle *usb_open(struct usb_device *dev); 320 | int usb_close(usb_dev_handle *dev); 321 | int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, 322 | size_t buflen); 323 | int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, 324 | size_t buflen); 325 | 326 | /* descriptors.c */ 327 | int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, 328 | unsigned char type, unsigned char index, 329 | void *buf, int size); 330 | int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, 331 | unsigned char index, void *buf, int size); 332 | 333 | /* .c */ 334 | int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, 335 | int timeout); 336 | int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, 337 | int timeout); 338 | int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, 339 | int timeout); 340 | int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, 341 | int timeout); 342 | int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 343 | int value, int index, char *bytes, int size, 344 | int timeout); 345 | int usb_set_configuration(usb_dev_handle *dev, int configuration); 346 | int usb_claim_interface(usb_dev_handle *dev, int interface); 347 | int usb_release_interface(usb_dev_handle *dev, int interface); 348 | int usb_set_altinterface(usb_dev_handle *dev, int alternate); 349 | int usb_resetep(usb_dev_handle *dev, unsigned int ep); 350 | int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); 351 | int usb_reset(usb_dev_handle *dev); 352 | 353 | char *usb_strerror(void); 354 | 355 | void usb_init(void); 356 | void usb_set_debug(int level); 357 | int usb_find_busses(void); 358 | int usb_find_devices(void); 359 | struct usb_device *usb_device(usb_dev_handle *dev); 360 | struct usb_bus *usb_get_busses(void); 361 | 362 | 363 | /* Windows specific functions */ 364 | 365 | #define LIBUSB_HAS_INSTALL_SERVICE_NP 1 366 | int usb_install_service_np(void); 367 | void CALLBACK usb_install_service_np_rundll(HWND wnd, HINSTANCE instance, 368 | LPSTR cmd_line, int cmd_show); 369 | 370 | #define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1 371 | int usb_uninstall_service_np(void); 372 | void CALLBACK usb_uninstall_service_np_rundll(HWND wnd, HINSTANCE instance, 373 | LPSTR cmd_line, int cmd_show); 374 | 375 | #define LIBUSB_HAS_INSTALL_DRIVER_NP 1 376 | int usb_install_driver_np(const char *inf_file); 377 | void CALLBACK usb_install_driver_np_rundll(HWND wnd, HINSTANCE instance, 378 | LPSTR cmd_line, int cmd_show); 379 | 380 | #define LIBUSB_HAS_TOUCH_INF_FILE_NP 1 381 | int usb_touch_inf_file_np(const char *inf_file); 382 | void CALLBACK usb_touch_inf_file_np_rundll(HWND wnd, HINSTANCE instance, 383 | LPSTR cmd_line, int cmd_show); 384 | 385 | #define LIBUSB_HAS_INSTALL_NEEDS_RESTART_NP 1 386 | int usb_install_needs_restart_np(void); 387 | 388 | const struct usb_version *usb_get_version(void); 389 | 390 | int usb_isochronous_setup_async(usb_dev_handle *dev, void **context, 391 | unsigned char ep, int pktsize); 392 | int usb_bulk_setup_async(usb_dev_handle *dev, void **context, 393 | unsigned char ep); 394 | int usb_interrupt_setup_async(usb_dev_handle *dev, void **context, 395 | unsigned char ep); 396 | 397 | int usb_submit_async(void *context, char *bytes, int size); 398 | int usb_reap_async(void *context, int timeout); 399 | int usb_reap_async_nocancel(void *context, int timeout); 400 | int usb_cancel_async(void *context); 401 | int usb_free_async(void **context); 402 | 403 | 404 | #ifdef __cplusplus 405 | } 406 | #endif 407 | 408 | #endif /* __USB_H__ */ 409 | 410 | -------------------------------------------------------------------------------- /winbuild.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | IF %1.==. GOTO Build 3 | IF %1==clean ( 4 | GOTO Clean 5 | ) ELSE ( 6 | GOTO Build 7 | ) 8 | :Build 9 | REM cl /Ox /O2 zjtag.c ftdixx.c libusb.c 10 | 11 | call "%VS100COMNTOOLS%vsvars32.bat" 12 | 13 | "%VCINSTALLDIR%bin\cl.exe" /Fe..\ /Ox /O2 zjtag.c ftdixx.c j-link.c libusb.c busbasp.c stmhid.c 14 | 15 | set /P clean_closet=Do you want to clean build files [Y/n]? 16 | if /I "%clean_closet%" NEQ "N" goto :Clean 17 | GOTO Done 18 | :Clean 19 | del *.obj -y 20 | GOTO Done 21 | :Done 22 | echo on 23 | @pause -------------------------------------------------------------------------------- /zjtag.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************************** 2 | // 3 | // zjag.c - Broadcom EJTAG Debrick Utility v1.8 RC3 - by Volkan K. 4 | // 5 | ////////////////////////////////////////////////////////////////////////////////////////// 6 | // Re-baseline the brjtag and tornado version - zjtag is based on brjtag 1.9o 7 | // zjtag supports DIYGADGET's USB Multi Protocol Adapter (TUMPA), TUMPA-Lite and 8 | // DIYGADGET's Parallel JTAG Cables 9 | // 10 | // zJTAG project website: 11 | // http://zjtag.sourceforge.net/ 12 | // zJTAG project downloads: 13 | // http://sourceforge.net/projects/zjtag/files 14 | // 15 | // For more information on TUMPA and other JTAG adapters, please visit: 16 | // http://www.diygadget.com 17 | // For product manuals and tutorials, please visit: 18 | // http://www.tiaowiki.com 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////////////// 21 | // Includes code from brjtag: written by hugebird @ http://www.chinadsl.net/ 22 | // http://www.chinadsl.net/thread-21684-1-1.html 23 | ////////////////////////////////////////////////////////////////////////////////////////// 24 | //========================================================================== 25 | // Includes code from TJTAG: Tornado's modifications 26 | // tornado@odessaua.com 27 | // - Thanks to HDM's great work 28 | // ************************************************************************** 29 | // Includes code written by HairyDairyMaid (a.k.a. - lightbulb) 30 | // hairydairymaid@yahoo.com 31 | // ************************************************************************** 32 | // 33 | // This program is copyright (C) 2004-2006 HairyDairyMaid (a.k.a. Lightbulb), 34 | // 2007-2012 Tornado, 2008-2011 hugebird, 2012-2013 Volkan K. 35 | // This program is free software; you can redistribute it and/or modify it 36 | // under the terms of version 2 the GNU General Public License as published 37 | // by the Free Software Foundation. 38 | // This program is distributed in the hope that it will be useful, but WITHOUT 39 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 40 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 41 | // more details. 42 | // To view a copy of the license go to: 43 | // http://www.fsf.org/copyleft/gpl.html 44 | // To receive a copy of the GNU General Public License write the Free Software 45 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 46 | // 47 | // ************************************************************************** 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | #if ( defined(_MSC_VER) && !defined(WINDOWS_VERSION) ) 54 | #define WINDOWS_VERSION //windows make with: cl xjtag.c xxx.c 55 | #endif 56 | 57 | 58 | #ifdef WINDOWS_VERSION 59 | #include 60 | #define strcasecmp stricmp 61 | #define strncasecmp strnicmp 62 | #define mssleep(s) Sleep(s) 63 | #define ussleep(s) iusleep(s) 64 | 65 | #else //For linux 66 | 67 | #include 68 | #include 69 | 70 | #ifdef __FreeBSD__ 71 | #include 72 | #include 73 | #define PPWDATA PPISDATA 74 | #define PPRSTATUS PPIGSTATUS 75 | #else 76 | #include 77 | #endif 78 | 79 | #ifndef FTDIXXX 80 | #ifndef MY_TYPE 81 | #define MY_TYPE 82 | typedef uint32_t DWORD; 83 | typedef uint16_t WORD; 84 | typedef unsigned char BYTE; 85 | typedef int bool; 86 | #endif 87 | #else 88 | #define MY_TYPE 89 | #endif 90 | 91 | #define mssleep(s) usleep((s)* 1000) 92 | #define ussleep(s) usleep(s) 93 | 94 | #endif 95 | 96 | 97 | #define true 1 98 | #define false 0 99 | 100 | #define MAX_ATTEMPTS 3 101 | #define MAX_TIMEOUT 200 102 | #define MAX_LOOP_CNT 1000 103 | 104 | /* 105 | * The followint table shows the pin assignment of 25-pin Parallel Printer Port. 106 | * please refer to IEEE 1284 standard for detailed description. 107 | * Operate in SPP way, set to ECP mode to get better speed 108 | * data port (Out) (0x378) status port (In) (0x379) 109 | * bit[7] -- pin9 (Out) bit[7] -- pin11 (In), busy (Hardware Inverted) 110 | * bit[6] -- pin8 (Out) bit[6] -- pin10 (In), Ack 111 | * bit[5] -- pin7 (Out) bit[5] -- pin12 (In), Paper out 112 | * bit[4] -- pin6 (Out) bit[4] -- pin13 (In), Select 113 | * bit[3] -- pin5 (Out) bit[3] -- pin15 (In), Error 114 | * bit[2] -- pin4 (Out) bit[2] -- IRQ(Not) 115 | * bit[1] -- pin3 (Out) bit[1] -- Reserved 116 | * bit[0] -- pin2 (Out) bit[0] -- Reserved 117 | */ 118 | #define PORT378 0x378 119 | 120 | /* OOCD paraport cable info 121 | // name tdo trst tms tck tdi srst o_inv i_inv init exit led 122 | { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 }, 123 | { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 }, 124 | { "wig_ntrst_inv", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 }, 125 | { "old_amt_wig", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 }, 126 | { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 }, 127 | { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, 128 | { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 }, 129 | { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, 130 | { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 }, 131 | { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 }, 132 | { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 }, 133 | { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } 134 | 135 | */ 136 | 137 | // --- BlackCat Type Cable --- 138 | #define BTDI 6 //0x40 139 | #define BTCK 7 //0x80 140 | #define BTMS 5 //0x20 141 | #define BTDO 7 //0x80 142 | #define BSRST_N 5 //0x20, pin7 143 | #define BI_INV 0x80 144 | #define BO_INV 0x00 145 | 146 | // --- Xilinx Type Cable DLC5 --- 147 | #define TDI 0 //0x01 148 | #define TCK 1 //0x02 149 | #define TMS 2 //0x04 150 | #define TDO 4 //0x10 151 | #define SRST_N 5 //0x20, pin7 152 | #define I_INV 0x00 153 | #define O_INV 0x20 154 | 155 | // --- Wiggler Type Cable AMD5120--- 156 | #define WTDI 3 //0x08 157 | #define WTCK 2 //0x04 158 | #define WTMS 1 //0x02 159 | #define WTDO 7 //0x80 160 | #define WTRST_N 4 //0x10 161 | #define WSRST_N 0 //0x01 , hardware inverted 162 | #define WI_INV 0x80 163 | #define WO_INV 0x10 164 | 165 | // --- Cable Type --- 166 | #define FT2232H 0 167 | #define XILINX 1 168 | #define BLACKCAT 2 169 | #define WIGGLER 3 170 | #define USBASP 4 171 | #define STM32 5 172 | #define JLINK 6 173 | #define FT2232D 7 174 | 175 | // --- Some EJTAG Instruction Registers --- 176 | #define INSTR_EXTEST 0x00 177 | #define INSTR_IDCODE 0x01 178 | #define INSTR_SAMPLE 0x02 179 | #define INSTR_IMPCODE 0x03 180 | #define INSTR_ADDRESS 0x08 181 | #define INSTR_DATA 0x09 182 | #define INSTR_CONTROL 0x0A 183 | #define INSTR_EJTAGALL 0x0B 184 | #define INSTR_BYPASS 0xFF 185 | #define LV_BYPASS 0xffffffff 186 | #define CCJT_BYPASS 0xff 187 | #define INSTR_EJTAGBOOT 0x0C 188 | #define INSTR_NORMALBOOT 0x0D 189 | // below from bcm1125H 190 | #define INSTR_SYSCTRL 0x20 191 | #define INSTR_TRACE 0x21 192 | #define INSTR_PERF 0x22 193 | #define INSTR_TRACECTRL 0x23 194 | #define INSTR_WAFERID 0x24 195 | #define INSTR_BCMU0O 0x26 //broadcom cpu0 observation chain 196 | #define INSTR_BCMU0D 0x27 //broadcom cpu0 Debug chain 197 | #define INSTR_BCMU0T 0x28 //broadcom cpu0 Test chain 198 | #define INSTR_BCMSCANMC 0x34 //broadcom MC chain 199 | #define INSTR_BCMSCANSCD 0x36 //broadcom SCD chain 200 | #define INSTR_BCMSCANALL 0x38 //broadcom all Agent scan chain in 201 | #define INSTR_BSRMODE 0x3A 202 | #define INSTR_CLAMP 0x3C 203 | #define INSTR_PRELOAD 0x3D 204 | #define INSTR_INTEST 0x3E 205 | #define INSTR_BCMBYPASS 0x3F 206 | 207 | // --- Some EJTAG Control Register Bit Masks --- 208 | #define TOF (1 << 1 ) //ClkEn, permit DCLK out 209 | #define TIF (1 << 2 ) 210 | #define BRKST (1 << 3 ) //Indicate cpu in debug mode 211 | #define DINC (1 << 4 ) //Increase Address auto on DMA 212 | #define DLOCK (1 << 5 ) //lock bus for DMA 213 | #define DRWN (1 << 9 ) // DMA R or W 214 | #define DERR (1 << 10) //Indicate a error occur on DMA 215 | #define DSTRT (1 << 11) //DMA xfer start 216 | #define JTAGBRK (1 << 12) //generate a JTAG debug interupt 217 | #define SETDEV (1 << 14) //prob trap 218 | #define PROBEN (1 << 15) //cpu probe is enabled by deug support block 219 | #define PRRST (1 << 16) //Reset cpu 220 | #define DMAACC (1 << 17) //Request DMA 221 | #define PRACC (1 << 18) //PraCC interactive ext cpu probe. 222 | #define PRNW (1 << 19) //Indicate cpu R or W activity 223 | #define PERRST (1 << 20) 224 | #define SYNC (1 << 23) 225 | #define DNM (1 << 28) 226 | #define ROCC (1 << 31) 227 | 228 | /* define data type for MIPS32 */ 229 | #define MIPS_BYTE 0 230 | #define MIPS_HALFWORD 1 231 | #define MIPS_WORD 2 232 | #define MIPS_TRIPLEBYTE 3 233 | 234 | #define DMASZ(x) ((x) << 7) //make sz flag for DMA xfer 235 | 236 | #define DMA_BYTE (MIPS_BYTE << 7) //DMA tranfser size one byte 0x00000000 237 | #define DMA_HALFWORD (MIPS_HALFWORD << 7) //DMA transfer size double bytes 0x00000080 238 | #define DMA_WORD (MIPS_WORD << 7) //DMA transfer size four bytes 0x00000100 239 | #define DMA_TRIPLEBYTE (MIPS_TRIPLEBYTE << 7) //DMA transfer size three bytes 0x00000180 240 | 241 | #define __LE 0 // Little Endian 242 | #define __BE 1 // Big Endian 243 | 244 | #define size4K 0x1000 245 | #define size8K 0x2000 246 | #define size16K 0x4000 247 | #define size32K 0x8000 248 | #define size64K 0x10000 249 | #define size128K 0x20000 250 | #define size192K 0x30000 // use for re-address tiny CFE and Kernel 251 | 252 | #define size1MB 0x100000 253 | #define size2MB 0x200000 254 | #define size4MB 0x400000 255 | #define size8MB 0x800000 256 | #define size16MB 0x1000000 257 | #define size32MB 0x2000000 258 | #define size64MB 0x4000000 259 | #define size128MB 0x8000000 260 | #define size256MB 0x10000000 261 | #define size512MB 0x20000000 262 | 263 | #define MEM_TOP 0x20000000 264 | #define CFE_LEN 0x40000 265 | #define NVRAM_LEN 0x10000 266 | 267 | #define CMD_TYPE_BCS 0x01 268 | #define CMD_TYPE_SCS 0x02 269 | #define CMD_TYPE_AMD 0x03 270 | #define CMD_TYPE_SST 0x04 271 | #define CMD_TYPE_UND 0xFE 272 | 273 | #define SP_PPB 0x01 //Spansion Advanced Sector Protection 274 | 275 | 276 | #define STATUS_READY 0x0080 //DQ7 277 | 278 | 279 | // EJTAG DEBUG Unit Vector on Debug Break 280 | #define MIPS_DEBUG_VECTOR_ADDRESS 0xFF200200 281 | 282 | // Our 'Pseudo' Virtual Memory Access Registers 283 | #define MIPS_VIRTUAL_ADDRESS_ACCESS 0xFF200000 //not used by new pracc code 284 | #define MIPS_VIRTUAL_DATA_ACCESS 0xFF200004 285 | 286 | typedef struct _cable_prop_type 287 | { 288 | DWORD feature; 289 | void (*close)(void); 290 | void (*test_reset)(void); 291 | int (*det_instr)(void); 292 | DWORD (*set_instr)(DWORD instr); 293 | DWORD (*ReadWriteData)(DWORD in_data); 294 | DWORD (*ReadData)(void); 295 | void (*WriteData)(DWORD in_data); 296 | DWORD (*ejtag_dma_read_x)(DWORD addr, int mode); 297 | void (*ejtag_dma_write_x)(DWORD addr, DWORD data, int mode); 298 | DWORD (*ejtag_pracc_read_x)(DWORD addr, int mode); 299 | void (*ejtag_pracc_write_x)(DWORD addr, DWORD data, int mode); 300 | 301 | int (*sflash_blkread) (DWORD Inaddr, DWORD* pbuff, int len); 302 | int (*sflash_blkwrite)(DWORD Inaddr, DWORD* pbuff, int len, int flpg_x8); 303 | 304 | }cable_prop_type; 305 | 306 | #define CBL_DMA_RD (1UL<<0) 307 | #define CBL_DMA_WR (1UL<<1) 308 | #define CBL_PRACC_RD (1UL<<2) 309 | #define CBL_PRACC_WR (1UL<<3) 310 | 311 | 312 | // --- public functions --- 313 | #ifdef BRJMAIN 314 | 315 | #ifdef WINDOWS_VERSION 316 | 317 | __inline double dbl(LARGE_INTEGER x) 318 | {return (double)(x.HighPart * 4294967296.0 + x.LowPart);} 319 | #endif 320 | 321 | //----- public fn--------- 322 | void ShowData(DWORD); 323 | void ShowData_h(DWORD); 324 | DWORD rev_endian_h(DWORD); 325 | DWORD rev_endian(DWORD); 326 | void iusleep(DWORD); 327 | 328 | //------ private fn------- 329 | 330 | static void chip_detect(void); 331 | static void chip_shutdown(void); 332 | static BYTE clockin(int, int); 333 | static int det_instr(int iz_Total); 334 | static int get_irlen_for_dev(int device_num); 335 | static void define_block(DWORD, DWORD); 336 | static DWORD ejtag_cmd_read(DWORD); 337 | static void ejtag_cmd_write(DWORD, DWORD); 338 | static DWORD *ejtag_fix_writecode( DWORD, DWORD, int); 339 | static DWORD *ejtag_fix_readcode( DWORD, int); 340 | static void identify_flash_part(); 341 | static void lpt_closeport(void); 342 | static void lpt_openport(void); 343 | static void lpt_srst(void); 344 | #ifdef WINDOWS_VERSION 345 | static void _outp64(WORD p, int d); 346 | static BYTE _inp64(WORD p); 347 | #endif 348 | static DWORD ReadData(void); 349 | static DWORD ReadWriteData(DWORD); 350 | static void run_backup(char*, DWORD, DWORD); 351 | static void run_erase(char*, DWORD, DWORD); 352 | static void run_flash(char*, DWORD, DWORD); 353 | static DWORD set_instr(DWORD); 354 | static void sflash_config(void); 355 | static int sflash_erase_area(DWORD, DWORD); 356 | static void sflash_erase_block(DWORD); 357 | static void sflash_probe(void); 358 | static void sflash_reset(void); 359 | static void sflash_write_word(DWORD, DWORD); 360 | static void show_usage(void); 361 | static void show_flashlist(void); 362 | static void chip_filllist(void); 363 | static void test_reset(void); 364 | static void WriteData(DWORD); 365 | static void ExecuteDebugModule(DWORD *); 366 | static void check_ejtag_features(void); 367 | static void sp_exit_cmdset(void); 368 | static void sflash_erase_chip(void); 369 | static void sp_check_ppb(void); 370 | static void g_init_cpu(); 371 | static void g_init_dreg(); 372 | static void sp_unlock_bypass(void); 373 | static void sp_unlock_bypass_reset(void); 374 | static int usb_sflash_blkread(DWORD Inaddr, DWORD* pbuff, int len); 375 | static int usb_sflash_blkwrite(DWORD Inaddr, DWORD* pbuff, int len); 376 | static void usb_sflash_write_word(DWORD addr, DWORD data); 377 | static void usb_dma_rdtraining(DWORD addr,DWORD length); 378 | static void usb_dma_wrtraining(DWORD addr,DWORD length); 379 | static DWORD ejtag_dma_read_x(DWORD addr, int mode); 380 | static void ejtag_dma_write_x(DWORD addr, DWORD data, int mode); 381 | static DWORD ejtag_pracc_read_x(DWORD addr, int mode); 382 | static void ejtag_pracc_write_x(DWORD addr, DWORD data, int mode); 383 | static DWORD ejtag_read_x(DWORD addr, int mode); 384 | static void ejtag_write_x(DWORD addr, DWORD data,int mode); 385 | static void sflash_write_word_x(DWORD addr, DWORD data); 386 | static void sflash_write_x16(DWORD addr, DWORD data); 387 | static void sflash_write_x8(DWORD addr, DWORD data); 388 | static void sflash_poll_x16(DWORD addr, DWORD data); 389 | static void sflash_poll_x8(DWORD addr, DWORD data); 390 | static int getusb_setting(char* choice); 391 | 392 | // **************** hugebird new code ************************ 393 | 394 | DWORD pracc_init_dreg[] = { 395 | // # 396 | // # hugebird: PrAcc init data register 397 | // # 398 | // start: 399 | // # keep $1 for all R/W operation 400 | 0x3C01FF20, // lui $1,0xFF20 # li $1, MIPS_VIRTUAL_DATA_BASE 401 | 0x00000000, 402 | 0x1000FFFD, // b start 403 | 0x00000000}; // nop 404 | 405 | 406 | DWORD pracc_read_x32[] = { 407 | // # 408 | // # hugebird: PrAcc Read Word Routine (5 sets) 409 | // # 410 | // start: 411 | // 412 | // # Load R1 with the address of the pseudo-address register 413 | // 0x3C01FF20,// lui $1, 0xFF20 414 | // 0x34210000,// ori $1, 0x0000 415 | // 416 | // # Load R2 with the address %hi 417 | 0x3C021F01, // lui $2, addr_hi # p+0 418 | // 419 | // # Load R3 with the word @R2 420 | 0x8C438100, // lw $3, addr_lo($2) #p+1 <- 100011 00010 00011 421 | // 422 | // # Store the value into the pseudo-data register 423 | 0xAC230004, // sw $3, 4($1) #p+2 424 | // 425 | 0x00000000, // nop ,delay must have, wait data arrive. 426 | 0x1000FFFB, // b start 427 | 0x00000000}; // nop 428 | 429 | DWORD pracc_read_x16[] = { 430 | // # 431 | // # hugebird: PrAcc Read half Word Routine(5 sets) 432 | // # 433 | // start: 434 | // 435 | // # Load R1 with the address of the pseudo-address register 436 | // 0x3C01FF20,// lui $1, 0xFF20 437 | // 0x34210000,// ori $1, 0x0000 438 | // 439 | // # Load R2 with the address %hi 440 | 0x3C021F01, // lui $2, addr_hi # p+0 441 | // 442 | // # Load R3 with the word @R2 443 | 0x94438100, // lhu $3, addr_lo($2) # p+1 <- 100101 00010 00011 444 | // 445 | // # Store the value into the pseudo-data register 446 | 0xAC230004, // sw $3, 4($1) # p+2 447 | // 448 | 0x00000000, // nop ,delay must have, wait wait data arrive 449 | 0x1000FFFB, // b start 450 | 0x00000000}; // nop 451 | 452 | DWORD pracc_read_x8[] = { 453 | // # 454 | // # hugebird: PrAcc Read byte Routine(5 sets) 455 | // # 456 | // start: 457 | // 458 | // # Load R1 with the address of the pseudo-address register 459 | // 0x3C01FF20,// lui $1, 0xFF20 460 | // 0x34210000,// ori $1, 0x0000 461 | // 462 | // # Load R2 with the address %hi 463 | 0x3C021F01, // lui $2, addr_hi # p+0 464 | // 465 | // # Load R3 with the byte @R2 466 | 0x90438100, // lbu $3, addr_lo($2) # p+1 <- 100100 00010 00011 467 | // 468 | // # Store the value into the pseudo-data register 469 | 0xAC230004, // sw $3, 4($1) # p+2 470 | // 471 | 0x00000000, // nop ,delay must have, wait wait data arrive 472 | 0x1000FFFB, // b start 473 | 0x00000000}; // nop 474 | 475 | DWORD pracc_write_x8[] = { 476 | // # 477 | // # hugebird : PrAcc Write half Word Routine (4 sets) 478 | // # 479 | // start: 480 | // 481 | // # Load R1 with the address of the pseudo-address register 482 | //0x3C01FF20, // lui $1, 0xFF20 483 | //0x34210000, // ori $1, 0x0000 484 | // 485 | // # Load R2 with the address %hi 486 | 0x3C021F01, // lui $2, addr_hi # p+0 487 | // 488 | // # Load R3 with the data 489 | 0x34030010, // li $3, data # p+1 490 | // 491 | // # Store the byte at @R2 (the address) 492 | 0xA0438100, // sb $3, addr_lo($2) # p+2 <- 101000 00010 00011 493 | // 494 | 0x1000FFFC, // beq $0, $0, start 495 | 0x00000000}; // nop 496 | 497 | DWORD pracc_write_x16[] = { 498 | // # 499 | // # hugebird : PrAcc Write half Word Routine (4 sets) 500 | // # 501 | // start: 502 | // 503 | // # Load R1 with the address of the pseudo-address register 504 | //0x3C01FF20, // lui $1, 0xFF20 505 | //0x34210000, // ori $1, 0x0000 506 | // 507 | // # Load R2 with the address %hi 508 | 0x3C021F01, // lui $2, addr_hi # p+0 509 | // 510 | // # Load R3 with the data 511 | 0x34030010, // li $3, data # p+1 512 | // 513 | // # Store the half word at @R2 (the address) 514 | 0xA4438100, // sh $3, addr_lo($2) # p+2 <- 101001 00010 00011 515 | // 516 | 0x1000FFFC, // beq $0, $0, start 517 | 0x00000000}; // nop 518 | 519 | DWORD pracc_write_x32[] = { 520 | // # 521 | // # hugebird : PrAcc Write Word Routine (5 sets) 522 | // # 523 | // start: 524 | // 525 | // # Load R1 with the address of the pseudo-address register 526 | //0x3C01FF20, // lui $1, 0xFF20 527 | //0x34210000, // ori $1, 0x0000 528 | // 529 | // # Load R2 with the address %hi 530 | 0x3C021F01, // lui $2, addr_hi # p+0 531 | // 532 | // # Load R3 with the data 533 | 0x3C030001, // lui $3, data_hi # p+1 534 | 0x34631000, // ori $3,$3,data_lo # p+2 <-001101 00011 00011 535 | // 536 | // # Store the half word at @R2 (the address) 537 | 0xAC438100, // sw $3, addr_lo($2) # p+3 538 | // 539 | 0x1000FFFB, // beq $0, $0, start 540 | 0x00000000}; // nop 541 | 542 | // ************************************************************************** 543 | // hugeird : add cpu init configuration code below. 544 | // don't forget add point to CPU structure. 545 | // ************************************************************************** 546 | DWORD i6358[] = { 547 | // # 548 | // # hugebird: initialize 6358 to fully access 16MB flash 549 | // # 550 | // start: 551 | // 552 | 0x00000000, // nop 553 | 0x0000e021, // move gp,zero 554 | 0x3c09ff40, // lui $9, 0xff40 555 | 0x3529000c, // ori $9, $9, 0x0c 556 | 0x4089b006, // mtc0 $9, $22, 6 557 | // 558 | 0x00000000, // nop 559 | 0x1000FFF9, // beq $0, $0, start 560 | 0x00000000}; // nop 561 | 562 | DWORD i5354[] = { 563 | // # 564 | // # hugebird: initialize 5354 565 | // # 566 | // start: 567 | // 568 | 0x00000000, // nop 569 | 570 | 0x3c081fa0, // li $t0, 0x1FA0000C 571 | 0x3508000c, // mtc0 $t0, $22, 6 572 | 0x4088b006, //lui $t1, 0x1FA0 573 | 0x3c091fa0, //lw $t2, 0x1FA00014 574 | 0x8d2a0014, 575 | 0x3c01c000, //lui $1, 0xC000 576 | 0x01415825, //or $t3, $t2, $1 577 | 0xad2b0014, //sw $t3, 0x1FA00014 578 | 0x3c08ff40, //li $t0, 0xFF40000C 579 | 0x3508000c, 580 | 0x4088b006, //mtc0 $t0, $22, 6 581 | // 582 | 0x00000000, // nop 583 | 0x1000FFF2, // beq $0, $0, start 584 | 0x00000000}; // nop 585 | 586 | #else //BRJMAIN 587 | 588 | #ifdef WINDOWS_VERSION 589 | extern void iusleep(DWORD); 590 | #endif 591 | 592 | #endif //BRJMAIN 593 | 594 | // ************************************************************************** 595 | // End of File 596 | // ************************************************************************** 597 | --------------------------------------------------------------------------------