diff options
Diffstat (limited to 'src/jtag/drivers/OpenULINK')
-rw-r--r-- | src/jtag/drivers/OpenULINK/include/delay.h | 2 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/include/io.h | 47 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/include/jtag.h | 4 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/include/reg_ezusb.h | 306 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/include/usb.h | 154 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/src/delay.c | 26 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/src/jtag.c | 1057 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/src/main.c | 56 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/src/protocol.c | 322 | ||||
-rw-r--r-- | src/jtag/drivers/OpenULINK/src/usb.c | 769 |
10 files changed, 1369 insertions, 1374 deletions
diff --git a/src/jtag/drivers/OpenULINK/include/delay.h b/src/jtag/drivers/OpenULINK/include/delay.h index 5c36b1d9..c887651a 100644 --- a/src/jtag/drivers/OpenULINK/include/delay.h +++ b/src/jtag/drivers/OpenULINK/include/delay.h @@ -23,7 +23,7 @@ #include <stdint.h> -#define NOP {__asm nop __endasm;} +#define NOP { __asm nop __endasm; } void delay_5us(void); void delay_1ms(void); diff --git a/src/jtag/drivers/OpenULINK/include/io.h b/src/jtag/drivers/OpenULINK/include/io.h index f7119b9a..2dc692ac 100644 --- a/src/jtag/drivers/OpenULINK/include/io.h +++ b/src/jtag/drivers/OpenULINK/include/io.h @@ -51,15 +51,14 @@ /* PORT A */ #define PIN_U_OE OUTA0 -// PA1 Not Connected +/* PA1 Not Connected */ #define PIN_OE OUTA2 -// PA3 Not Connected +/* PA3 Not Connected */ #define PIN_RUN_LED OUTA4 #define PIN_TDO PINA5 #define PIN_BRKOUT PINA6 #define PIN_COM_LED OUTA7 - /* PORT B */ #define PIN_TDI OUTB0 #define PIN_TMS OUTB1 @@ -77,18 +76,18 @@ #define PIN_RXD0 PINC0 #define PIN_TXD0 OUTC1 #define PIN_RESET_2 PINC2 -// PC3 Not Connected -// PC4 Not Connected +/* PC3 Not Connecte */ +/* PC4 Not Connected */ #define PIN_RTCK PINC5 #define PIN_WR OUTC6 -// PC7 Not Connected +/* PC7 Not Connected */ /* LED Macros */ -#define SET_RUN_LED() OUTA &= ~PIN_RUN_LED -#define CLEAR_RUN_LED() OUTA |= PIN_RUN_LED +#define SET_RUN_LED() (OUTA &= ~PIN_RUN_LED) +#define CLEAR_RUN_LED() (OUTA |= PIN_RUN_LED) -#define SET_COM_LED() OUTA &= ~PIN_COM_LED -#define CLEAR_COM_LED() OUTA |= PIN_COM_LED +#define SET_COM_LED() (OUTA &= ~PIN_COM_LED) +#define CLEAR_COM_LED() (OUTA |= PIN_COM_LED) /* JTAG Pin Macros */ #define GET_TMS() (PINSB & PIN_TMS) @@ -99,27 +98,27 @@ #define GET_TRAP() (PINSB & PIN_TRAP) #define GET_RTCK() (PINSC & PIN_RTCK) -#define SET_TMS_HIGH() OUTB |= PIN_TMS -#define SET_TMS_LOW() OUTB &= ~PIN_TMS +#define SET_TMS_HIGH() (OUTB |= PIN_TMS) +#define SET_TMS_LOW() (OUTB &= ~PIN_TMS) -#define SET_TCK_HIGH() OUTB |= PIN_TCK -#define SET_TCK_LOW() OUTB &= ~PIN_TCK +#define SET_TCK_HIGH() (OUTB |= PIN_TCK) +#define SET_TCK_LOW() (OUTB &= ~PIN_TCK) -#define SET_TDI_HIGH() OUTB |= PIN_TDI -#define SET_TDI_LOW() OUTB &= ~PIN_TDI +#define SET_TDI_HIGH() (OUTB |= PIN_TDI) +#define SET_TDI_LOW() (OUTB &= ~PIN_TDI) /* TRST and RESET are low-active and inverted by hardware. SET_HIGH de-asserts * the signal (enabling reset), SET_LOW asserts the signal (disabling reset) */ -#define SET_TRST_HIGH() OUTB |= PIN_TRST -#define SET_TRST_LOW() OUTB &= ~PIN_TRST +#define SET_TRST_HIGH() (OUTB |= PIN_TRST) +#define SET_TRST_LOW() (OUTB &= ~PIN_TRST) -#define SET_RESET_HIGH() OUTB |= PIN_RESET -#define SET_RESET_LOW() OUTB &= ~PIN_RESET +#define SET_RESET_HIGH() (OUTB |= PIN_RESET) +#define SET_RESET_LOW() (OUTB &= ~PIN_RESET) -#define SET_OCDSE_HIGH() OUTB |= PIN_OCDSE -#define SET_OCDSE_LOW() OUTB &= ~PIN_OCDSE +#define SET_OCDSE_HIGH() (OUTB |= PIN_OCDSE) +#define SET_OCDSE_LOW() (OUTB &= ~PIN_OCDSE) -#define SET_BRKIN_HIGH() OUTB |= PIN_BRKIN -#define SET_BRKIN_LOW() OUTB &= ~PIN_BRKIN +#define SET_BRKIN_HIGH() (OUTB |= PIN_BRKIN) +#define SET_BRKIN_LOW() (OUTB &= ~PIN_BRKIN) #endif diff --git a/src/jtag/drivers/OpenULINK/include/jtag.h b/src/jtag/drivers/OpenULINK/include/jtag.h index ca866716..97ec5984 100644 --- a/src/jtag/drivers/OpenULINK/include/jtag.h +++ b/src/jtag/drivers/OpenULINK/include/jtag.h @@ -23,7 +23,7 @@ #include <stdint.h> -#define NOP {__asm nop __endasm;} +#define NOP { __asm nop __endasm; } void jtag_scan_in(uint8_t out_offset, uint8_t in_offset); void jtag_slow_scan_in(uint8_t out_offset, uint8_t in_offset); @@ -43,6 +43,6 @@ uint16_t jtag_get_signals(void); void jtag_set_signals(uint8_t low, uint8_t high); void jtag_configure_tck_delay(uint8_t scan_in, uint8_t scan_out, - uint8_t scan_io, uint8_t tck, uint8_t tms); + uint8_t scan_io, uint8_t tck, uint8_t tms); #endif diff --git a/src/jtag/drivers/OpenULINK/include/reg_ezusb.h b/src/jtag/drivers/OpenULINK/include/reg_ezusb.h index 26ee0186..3d5c279b 100644 --- a/src/jtag/drivers/OpenULINK/include/reg_ezusb.h +++ b/src/jtag/drivers/OpenULINK/include/reg_ezusb.h @@ -51,9 +51,9 @@ #define bmBit6 0x40 #define bmBit7 0x80 -/***************************************************************************/ -/************************ Special Function Registers ***********************/ -/***************************************************************************/ +/************************************************************************** + ************************ Special Function Registers ********************** + ***************************************************************************/ /* See EZ-USB TRM, pp. A-9 - A-10 */ @@ -65,33 +65,33 @@ SFR(DPL2, 0x85); SFR(DPS, 0x86); #define SEL bmBit0 - // Bit 1 read-only, always reads '0' - // Bit 2 read-only, always reads '0' - // Bit 3 read-only, always reads '0' - // Bit 4 read-only, always reads '0' - // Bit 5 read-only, always reads '0' - // Bit 6 read-only, always reads '0' - // Bit 7 read-only, always reads '0' +/* Bit 1 read-only, always reads '0' */ +/* Bit 2 read-only, always reads '0' */ +/* Bit 3 read-only, always reads '0' */ +/* Bit 4 read-only, always reads '0' */ +/* Bit 5 read-only, always reads '0' */ +/* Bit 6 read-only, always reads '0' */ +/* Bit 7 read-only, always reads '0' */ SFR(PCON, 0x87); #define IDLE bmBit0 #define STOP bmBit1 #define GF0 bmBit2 #define GF1 bmBit3 - // Bit 4 read-only, always reads '1' - // Bit 5 read-only, always reads '1' - // Bit 6 unused +/* Bit 4 read-only, always reads '1' */ +/* Bit 5 read-only, always reads '1' */ +/* Bit 6 unused */ #define SMOD0 bmBit7 SFR(TCON, 0x88); - SBIT(IT0, 0x88, 0); - SBIT(IE0, 0x88, 1); - SBIT(IT1, 0x88, 2); - SBIT(IE1, 0x88, 3); - SBIT(TR0, 0x88, 4); - SBIT(TF0, 0x88, 5); - SBIT(TR1, 0x88, 6); - SBIT(TF1, 0x88, 7); +SBIT(IT0, 0x88, 0); +SBIT(IE0, 0x88, 1); +SBIT(IT1, 0x88, 2); +SBIT(IE1, 0x88, 3); +SBIT(TR0, 0x88, 4); +SBIT(TF0, 0x88, 5); +SBIT(TR1, 0x88, 6); +SBIT(TF1, 0x88, 7); SFR(TMOD, 0x89); /* Some bits in this register share the same name in the EZ-USB TRM. Therefore, @@ -117,24 +117,24 @@ SFR(CKCON, 0x8E); #define T0M bmBit3 #define T1M bmBit4 #define T2M bmBit5 - // Bit 6 unused - // Bit 7 unused +/* Bit 6 unused */ +/* Bit 7 unused */ SFR(SPC_FNC, 0x8D); #define bmWRS bmBit0 - // Bit 1 read-only, always reads '0' - // Bit 2 read-only, always reads '0' - // Bit 3 read-only, always reads '0' - // Bit 4 read-only, always reads '0' - // Bit 5 read-only, always reads '0' - // Bit 6 read-only, always reads '0' - // Bit 7 read-only, always reads '0' +/* Bit 1 read-only, always reads '0' */ +/* Bit 2 read-only, always reads '0' */ +/* Bit 3 read-only, always reads '0' */ +/* Bit 4 read-only, always reads '0' */ +/* Bit 5 read-only, always reads '0' */ +/* Bit 6 read-only, always reads '0' */ +/* Bit 7 read-only, always reads '0' */ SFR(EXIF, 0x91); - // Bit 0 read-only, always reads '0' - // Bit 1 read-only, always reads '0' - // Bit 2 read-only, always reads '0' - // Bit 3 read-only, always reads '1' +/* Bit 0 read-only, always reads '0' */ +/* Bit 1 read-only, always reads '0' */ +/* Bit 2 read-only, always reads '0' */ +/* Bit 3 read-only, always reads '1' */ #define USBINT bmBit4 #define I2CINT bmBit5 #define IE4 bmBit6 @@ -146,58 +146,58 @@ SFR(MPAGE, 0x92); SFR(_XPAGE, 0x92); SFR(SCON0, 0x98); - SBIT(RI_0, 0x98, 0); - SBIT(TI_0, 0x98, 1); - SBIT(RB8_0, 0x98, 2); - SBIT(TB8_0, 0x98, 3); - SBIT(REN_0, 0x98, 4); - SBIT(SM2_0, 0x98, 5); - SBIT(SM1_0, 0x98, 6); - SBIT(SM0_0, 0x98, 7); +SBIT(RI_0, 0x98, 0); +SBIT(TI_0, 0x98, 1); +SBIT(RB8_0, 0x98, 2); +SBIT(TB8_0, 0x98, 3); +SBIT(REN_0, 0x98, 4); +SBIT(SM2_0, 0x98, 5); +SBIT(SM1_0, 0x98, 6); +SBIT(SM0_0, 0x98, 7); SFR(SBUF0, 0x99); SFR(IE, 0xA8); - SBIT(EX0, 0xA8, 0); - SBIT(ET0, 0xA8, 1); - SBIT(EX1, 0xA8, 2); - SBIT(ET1, 0xA8, 3); - SBIT(ES0, 0xA8, 4); - SBIT(ET2, 0xA8, 5); - SBIT(ES1, 0xA8, 6); - SBIT(EA, 0xA8, 7); +SBIT(EX0, 0xA8, 0); +SBIT(ET0, 0xA8, 1); +SBIT(EX1, 0xA8, 2); +SBIT(ET1, 0xA8, 3); +SBIT(ES0, 0xA8, 4); +SBIT(ET2, 0xA8, 5); +SBIT(ES1, 0xA8, 6); +SBIT(EA, 0xA8, 7); SFR(IP, 0xB8); - SBIT(PX0, 0xB8, 0); - SBIT(PT0, 0xB8, 1); - SBIT(PX1, 0xB8, 2); - SBIT(PT1, 0xB8, 3); - SBIT(PS0, 0xB8, 4); - SBIT(PT2, 0xB8, 5); - SBIT(PS1, 0xB8, 6); - // Bit 7 read-only, always reads '1' +SBIT(PX0, 0xB8, 0); +SBIT(PT0, 0xB8, 1); +SBIT(PX1, 0xB8, 2); +SBIT(PT1, 0xB8, 3); +SBIT(PS0, 0xB8, 4); +SBIT(PT2, 0xB8, 5); +SBIT(PS1, 0xB8, 6); +/* Bit 7 read-only, always reads '1' */ SFR(SCON1, 0xC0); - SBIT(RI_1, 0xC0, 0); - SBIT(TI_1, 0xC0, 1); - SBIT(RB8_1, 0xC0, 2); - SBIT(TB8_1, 0xC0, 3); - SBIT(REN_1, 0xC0, 4); - SBIT(SM2_1, 0xC0, 5); - SBIT(SM1_1, 0xC0, 6); - SBIT(SM0_1, 0xC0, 7); +SBIT(RI_1, 0xC0, 0); +SBIT(TI_1, 0xC0, 1); +SBIT(RB8_1, 0xC0, 2); +SBIT(TB8_1, 0xC0, 3); +SBIT(REN_1, 0xC0, 4); +SBIT(SM2_1, 0xC0, 5); +SBIT(SM1_1, 0xC0, 6); +SBIT(SM0_1, 0xC0, 7); SFR(SBUF1, 0xC1); SFR(T2CON, 0xC8); - SBIT(CPRL2, 0xC8, 0); - SBIT(CT2, 0xC8, 1); - SBIT(TR2, 0xC8, 2); - SBIT(EXEN2, 0xC8, 3); - SBIT(TCLK, 0xC8, 4); - SBIT(RCLK, 0xC8, 5); - SBIT(EXF2, 0xC8, 6); - SBIT(TF2, 0xC8, 7); +SBIT(CPRL2, 0xC8, 0); +SBIT(CT2, 0xC8, 1); +SBIT(TR2, 0xC8, 2); +SBIT(EXEN2, 0xC8, 3); +SBIT(TCLK, 0xC8, 4); +SBIT(RCLK, 0xC8, 5); +SBIT(EXF2, 0xC8, 6); +SBIT(TF2, 0xC8, 7); SFR(RCAP2L, 0xCA); SFR(RCAP2H, 0xCB); @@ -205,52 +205,52 @@ SFR(TL2, 0xCC); SFR(TH2, 0xCD); SFR(PSW, 0xD0); - SBIT(P, 0xD0, 0); - SBIT(F1, 0xD0, 1); - SBIT(OV, 0xD0, 2); - SBIT(RS0, 0xD0, 3); - SBIT(RS1, 0xD0, 4); - SBIT(F0, 0xD0, 5); - SBIT(AC, 0xD0, 6); - SBIT(CY, 0xD0, 7); +SBIT(P, 0xD0, 0); +SBIT(F1, 0xD0, 1); +SBIT(OV, 0xD0, 2); +SBIT(RS0, 0xD0, 3); +SBIT(RS1, 0xD0, 4); +SBIT(F0, 0xD0, 5); +SBIT(AC, 0xD0, 6); +SBIT(CY, 0xD0, 7); SFR(EICON, 0xD8); - // Bit 0 read-only, always reads '0' - // Bit 1 read-only, always reads '0' - // Bit 2 read-only, always reads '0' - SBIT(INT6, 0xD8, 3); - SBIT(RESI, 0xD8, 4); - SBIT(ERESI, 0xD8, 5); - // Bit 6 read-only, always reads '1' - SBIT(SMOD1, 0xD8, 7); +/* Bit 0 read-only, always reads '0' */ +/* Bit 1 read-only, always reads '0' */ +/* Bit 2 read-only, always reads '0' */ +SBIT(INT6, 0xD8, 3); +SBIT(RESI, 0xD8, 4); +SBIT(ERESI, 0xD8, 5); +/* Bit 6 read-only, always reads '1' */ +SBIT(SMOD1, 0xD8, 7); SFR(ACC, 0xE0); SFR(EIE, 0xE8); - SBIT(EUSB, 0xE8, 0); - SBIT(EI2C, 0xE8, 1); - SBIT(EX4, 0xE8, 2); - SBIT(EX5, 0xE8, 3); - SBIT(EWDI, 0xE8, 4); - // Bit 5 read-only, always reads '1' - // Bit 6 read-only, always reads '1' - // Bit 7 read-only, always reads '1' +SBIT(EUSB, 0xE8, 0); +SBIT(EI2C, 0xE8, 1); +SBIT(EX4, 0xE8, 2); +SBIT(EX5, 0xE8, 3); +SBIT(EWDI, 0xE8, 4); +/* Bit 5 read-only, always reads '1' */ +/* Bit 6 read-only, always reads '1' */ +/* Bit 7 read-only, always reads '1' */ SFR(B, 0xF0); SFR(EIP, 0xF8); - SBIT(PUSB, 0xF8, 0); - SBIT(PI2C, 0xF8, 1); - SBIT(PX4, 0xF8, 2); - SBIT(PX5, 0xF8, 3); - SBIT(PX6, 0xF8, 4); - // Bit 5 read-only, always reads '1' - // Bit 6 read-only, always reads '1' - // Bit 7 read-only, always reads '1' - -/***************************************************************************/ -/***************************** XDATA Registers *****************************/ -/***************************************************************************/ +SBIT(PUSB, 0xF8, 0); +SBIT(PI2C, 0xF8, 1); +SBIT(PX4, 0xF8, 2); +SBIT(PX5, 0xF8, 3); +SBIT(PX6, 0xF8, 4); +/* Bit 5 read-only, always reads '1' */ +/* Bit 6 read-only, always reads '1' */ +/* Bit 7 read-only, always reads '1' */ + +/************************************************************************** + ***************************** XDATA Registers **************************** + ***************************************************************************/ /************************ Endpoint 0-7 Data Buffers ************************/ SFRX(OUT7BUF[64], 0x7B40); @@ -269,7 +269,7 @@ SFRX(OUT1BUF[64], 0x7E40); SFRX(IN1BUF[64], 0x7E80); SFRX(OUT0BUF[64], 0x7EC0); SFRX(IN0BUF[64], 0x7F00); -// 0x7F40 - 0x7F5F reserved +/* 0x7F40 - 0x7F5F reserved */ /**************************** Isochronous Data *****************************/ SFRX(OUT8DATA, 0x7F60); @@ -312,9 +312,9 @@ SFRX(OUT16BCL, 0x7F7F); SFRX(CPUCS, 0x7F92); #define RES8051 bmBit0 #define CLK24OE bmBit1 - // Bit 2 read-only, always reads '0' - // Bit 3 read-only, always reads '0' - // Bits 4...7: Chip Revision +/* Bit 2 read-only, always reads '0' */ +/* Bit 3 read-only, always reads '0' */ +/* Bits 4...7: Chip Revision */ SFRX(PORTACFG, 0x7F93); #define T0OUT bmBit0 @@ -437,7 +437,7 @@ SFRX(OEC, 0x7F9E); #define OEC6 bmBit6 #define OEC7 bmBit7 -// 0x7F9F reserved +/* 0x7F9F reserved */ /****************** Isochronous Control/Status Registers *******************/ SFRX(ISOERR, 0x7FA0); @@ -455,10 +455,10 @@ SFRX(ISOCTL, 0x7FA1); #define MBZ0 bmBit1 #define MBZ1 bmBit2 #define PPSTAT bmBit3 - // Bit 4 unused - // Bit 5 unused - // Bit 6 unused - // Bit 7 unused +/* Bit 4 unused */ +/* Bit 5 unused */ +/* Bit 6 unused */ +/* Bit 7 unused */ SFRX(ZBCOUT, 0x7FA2); #define EP8 bmBit0 @@ -470,8 +470,8 @@ SFRX(ZBCOUT, 0x7FA2); #define EP14 bmBit6 #define EP15 bmBit7 -// 0x7FA3 reserved -// 0x7FA4 reserved +/* 0x7FA3 reserved */ +/* 0x7FA4 reserved */ /****************************** I2C Registers ******************************/ SFRX(I2CS, 0x7FA5); @@ -485,18 +485,18 @@ SFRX(I2CS, 0x7FA5); #define I2C_START bmBit7 SFRX(I2DAT, 0x7FA6); -// 0x7FA7 reserved +/* 0x7FA7 reserved */ /******************************* Interrupts ********************************/ SFRX(IVEC, 0x7FA8); - // Bit 0 read-only, always reads '0' - // Bit 1 read-only, always reads '0' +/* Bit 0 read-only, always reads '0' */ +/* Bit 1 read-only, always reads '0' */ #define IV0 bmBit2 #define IV1 bmBit3 #define IV2 bmBit4 #define IV3 bmBit5 #define IV4 bmBit6 - // Bit 7 read-only, always reads '0' +/* Bit 7 read-only, always reads '0' */ SFRX(IN07IRQ, 0x7FA9); #define IN0IR bmBit0 @@ -524,9 +524,9 @@ SFRX(USBIRQ, 0x7FAB); #define SUTOKIR bmBit2 #define SUSPIR bmBit3 #define URESIR bmBit4 - // Bit 5 unused - // Bit 6 unused - // Bit 7 unused +/* Bit 5 unused */ +/* Bit 6 unused */ +/* Bit 7 unused */ SFRX(IN07IEN, 0x7FAC); #define IN0IEN bmBit0 @@ -554,22 +554,22 @@ SFRX(USBIEN, 0x7FAE); #define SUTOKIE bmBit2 #define SUSPIE bmBit3 #define URESIE bmBit4 - // Bit 5 unused - // Bit 6 unused - // Bit 7 unused +/* Bit 5 unused */ +/* Bit 6 unused */ +/* Bit 7 unused */ SFRX(USBBAV, 0x7FAF); #define AVEN bmBit0 #define BPEN bmBit1 #define BPPULSE bmBit2 #define BREAK bmBit3 - // Bit 4 unused - // Bit 5 unused - // Bit 6 unused - // Bit 7 unused +/* Bit 4 unused */ +/* Bit 5 unused */ +/* Bit 6 unused */ +/* Bit 7 unused */ -// 0x7FB0 reserved -// 0x7FB1 reserved +/* 0x7FB0 reserved */ +/* 0x7FB1 reserved */ SFRX(BPADDRH, 0x7FB2); SFRX(BPADDRL, 0x7FB3); @@ -579,10 +579,10 @@ SFRX(EP0CS, 0x7FB4); #define HSNAK bmBit1 #define IN0BSY bmBit2 #define OUT0BSY bmBit3 - // Bit 4 unused - // Bit 5 unused - // Bit 6 unused - // Bit 7 unused +/* Bit 4 unused */ +/* Bit 5 unused */ +/* Bit 6 unused */ +/* Bit 7 unused */ SFRX(IN0BC, 0x7FB5); SFRX(IN1CS, 0x7FB6); @@ -599,7 +599,7 @@ SFRX(IN6CS, 0x7FC0); SFRX(IN6BC, 0x7FC1); SFRX(IN7CS, 0x7FC2); SFRX(IN7BC, 0x7FC3); -// 0x7FC4 reserved +/* 0x7FC4 reserved */ SFRX(OUT0BC, 0x7FC5); SFRX(OUT1CS, 0x7FC6); SFRX(OUT1BC, 0x7FC7); @@ -630,16 +630,16 @@ SFRX(USBCS, 0x7FD6); #define RENUM bmBit1 #define DISCOE bmBit2 #define DISCON bmBit3 - // Bit 4 unused - // Bit 5 unused - // Bit 6 unused +/* Bit 4 unused */ +/* Bit 5 unused */ +/* Bit 6 unused */ #define WAKESRC bmBit7 SFRX(TOGCTL, 0x7FD7); #define TOG_EP0 bmBit0 #define TOG_EP1 bmBit1 #define TOG_EP2 bmBit2 - // Bit 3 is read-only, always reads '0' +/* Bit 3 is read-only, always reads '0' */ #define TOG_IO bmBit4 #define TOG_R bmBit5 #define TOG_S bmBit6 @@ -647,9 +647,9 @@ SFRX(TOGCTL, 0x7FD7); SFRX(USBFRAMEL, 0x7FD8); SFRX(USBFRAMEH, 0x7FD9); -// 0x7FDA reserved +/* 0x7FDA reserved */ SFRX(FNADDR, 0x7FDB); -// 0x7FDC reserved +/* 0x7FDC reserved */ SFRX(USBPAIR, 0x7FDD); #define PR2IN bmBit0 @@ -658,11 +658,11 @@ SFRX(USBPAIR, 0x7FDD); #define PR2OUT bmBit3 #define PR4OUT bmBit4 #define PR6OUT bmBit5 - // Bit 6 unused +/* Bit 6 unused */ #define ISOSEND0 bmBit7 SFRX(IN07VAL, 0x7FDE); - // Bit 0 is read-only, always reads '1' +/* Bit 0 is read-only, always reads '1' */ #define IN1VAL bmBit1 #define IN2VAL bmBit2 #define IN3VAL bmBit3 @@ -672,7 +672,7 @@ SFRX(IN07VAL, 0x7FDE); #define IN7VAL bmBit7 SFRX(OUT07VAL, 0x7FDF); - // Bit 0 is read-only, always reads '1' +/* Bit 0 is read-only, always reads '1' */ #define OUT1VAL bmBit1 #define OUT2VAL bmBit2 #define OUT3VAL bmBit3 @@ -714,8 +714,8 @@ SFRX(FASTXFR, 0x7FE2); SFRX(AUTOPTRH, 0x7FE3); SFRX(AUTOPTRL, 0x7FE4); SFRX(AUTODATA, 0x7FE5); -// 0x7FE6 reserved -// 0x7FE7 reserved +/* 0x7FE6 reserved */ +/* 0x7FE7 reserved */ /******************************* Setup Data ********************************/ SFRX(SETUPDAT[8], 0x7FE8); diff --git a/src/jtag/drivers/OpenULINK/include/usb.h b/src/jtag/drivers/OpenULINK/include/usb.h index 811d6bf1..3685f7d7 100644 --- a/src/jtag/drivers/OpenULINK/include/usb.h +++ b/src/jtag/drivers/OpenULINK/include/usb.h @@ -26,15 +26,15 @@ #include <stdint.h> #include <stdbool.h> -#define NULL (void*)0; +#define NULL (void *)0; /* High and Low byte of a word (uint16_t) */ #define HI8(word) (uint8_t)(((uint16_t)word >> 8) & 0xff) #define LO8(word) (uint8_t)((uint16_t)word & 0xff) /* Convenience functions */ -#define STALL_EP0() EP0CS |= EP0STALL -#define CLEAR_IRQ() EXIF &= ~USBINT +#define STALL_EP0() (EP0CS |= EP0STALL) +#define CLEAR_IRQ() (EXIF &= ~USBINT) /*********** USB descriptors. See section 9.5 of the USB 1.1 spec **********/ @@ -45,84 +45,84 @@ #define DESCRIPTOR_TYPE_INTERFACE 0x04 #define DESCRIPTOR_TYPE_ENDPOINT 0x05 -#define STR_DESCR(len,...) { len*2+2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } } +#define STR_DESCR(len, ...) { len * 2 + 2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } } /** USB Device Descriptor. See USB 1.1 spec, pp. 196 - 198 */ struct usb_device_descriptor { - uint8_t bLength; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType; ///< DEVICE Descriptor Type. - uint16_t bcdUSB; ///< USB specification release number (BCD). - uint8_t bDeviceClass; ///< Class code. - uint8_t bDeviceSubClass; ///< Subclass code. - uint8_t bDeviceProtocol; ///< Protocol code. - uint8_t bMaxPacketSize0; ///< Maximum packet size for EP0 (8, 16, 32, 64). - uint16_t idVendor; ///< USB Vendor ID. - uint16_t idProduct; ///< USB Product ID. - uint16_t bcdDevice; ///< Device Release Number (BCD). - uint8_t iManufacturer; ///< Index of manufacturer string descriptor. - uint8_t iProduct; ///< Index of product string descriptor. - uint8_t iSerialNumber; ///< Index of string descriptor containing serial #. - uint8_t bNumConfigurations; ///< Number of possible configurations. + uint8_t bLength; /* /< Size of this descriptor in bytes. */ + uint8_t bDescriptorType;/* /< DEVICE Descriptor Type. */ + uint16_t bcdUSB; /* /< USB specification release number (BCD). */ + uint8_t bDeviceClass; /* /< Class code. */ + uint8_t bDeviceSubClass;/* /< Subclass code. */ + uint8_t bDeviceProtocol;/* /< Protocol code. */ + uint8_t bMaxPacketSize0;/* /< Maximum packet size for EP0 (8, 16, 32, 64). */ + uint16_t idVendor; /* /< USB Vendor ID. */ + uint16_t idProduct; /* /< USB Product ID. */ + uint16_t bcdDevice; /* /< Device Release Number (BCD). */ + uint8_t iManufacturer; /* /< Index of manufacturer string descriptor. */ + uint8_t iProduct; /* /< Index of product string descriptor. */ + uint8_t iSerialNumber; /* /< Index of string descriptor containing serial #. */ + uint8_t bNumConfigurations; /* /< Number of possible configurations. */ }; /** USB Configuration Descriptor. See USB 1.1 spec, pp. 199 - 200 */ struct usb_config_descriptor { - uint8_t bLength; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType; ///< CONFIGURATION descriptor type. - uint16_t wTotalLength; ///< Combined total length of all descriptors. - uint8_t bNumInterfaces; ///< Number of interfaces in this configuration. - uint8_t bConfigurationValue;///< Value used to select this configuration. - uint8_t iConfiguration; ///< Index of configuration string descriptor. - uint8_t bmAttributes; ///< Configuration characteristics. - uint8_t MaxPower; ///< Maximum power consumption in 2 mA units. + uint8_t bLength; /* /< Size of this descriptor in bytes. */ + uint8_t bDescriptorType;/* /< CONFIGURATION descriptor type. */ + uint16_t wTotalLength; /* /< Combined total length of all descriptors. */ + uint8_t bNumInterfaces; /* /< Number of interfaces in this configuration. */ + uint8_t bConfigurationValue; /* /< Value used to select this configuration. */ + uint8_t iConfiguration; /* /< Index of configuration string descriptor. */ + uint8_t bmAttributes; /* /< Configuration characteristics. */ + uint8_t MaxPower; /* /< Maximum power consumption in 2 mA units. */ }; /** USB Interface Descriptor. See USB 1.1 spec, pp. 201 - 203 */ struct usb_interface_descriptor { - uint8_t bLength; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType; ///< INTERFACE descriptor type. - uint8_t bInterfaceNumber; ///< Interface number. - uint8_t bAlternateSetting; ///< Value used to select alternate setting. - uint8_t bNumEndpoints; ///< Number of endpoints used by this interface. - uint8_t bInterfaceClass; ///< Class code. - uint8_t bInterfaceSubclass; ///< Subclass code. - uint8_t bInterfaceProtocol; ///< Protocol code. - uint8_t iInterface; ///< Index of interface string descriptor. + uint8_t bLength; /* /< Size of this descriptor in bytes. */ + uint8_t bDescriptorType;/* /< INTERFACE descriptor type. */ + uint8_t bInterfaceNumber; /* /< Interface number. */ + uint8_t bAlternateSetting; /* /< Value used to select alternate setting. */ + uint8_t bNumEndpoints; /* /< Number of endpoints used by this interface. */ + uint8_t bInterfaceClass;/* /< Class code. */ + uint8_t bInterfaceSubclass; /* /< Subclass code. */ + uint8_t bInterfaceProtocol; /* /< Protocol code. */ + uint8_t iInterface; /* /< Index of interface string descriptor. */ }; /** USB Endpoint Descriptor. See USB 1.1 spec, pp. 203 - 204 */ struct usb_endpoint_descriptor { - uint8_t bLength; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType; ///< ENDPOINT descriptor type. - uint8_t bEndpointAddress; ///< Endpoint Address: USB 1.1 spec, table 9-10. - uint8_t bmAttributes; ///< Endpoint Attributes: USB 1.1 spec, table 9-10. - uint16_t wMaxPacketSize; ///< Maximum packet size for this endpoint. - uint8_t bInterval; ///< Polling interval (in ms) for this endpoint. + uint8_t bLength; /* /< Size of this descriptor in bytes. */ + uint8_t bDescriptorType;/* /< ENDPOINT descriptor type. */ + uint8_t bEndpointAddress; /* /< Endpoint Address: USB 1.1 spec, table 9-10. */ + uint8_t bmAttributes; /* /< Endpoint Attributes: USB 1.1 spec, table 9-10. */ + uint16_t wMaxPacketSize;/* /< Maximum packet size for this endpoint. */ + uint8_t bInterval; /* /< Polling interval (in ms) for this endpoint. */ }; /** USB Language Descriptor. See USB 1.1 spec, pp. 204 - 205 */ struct usb_language_descriptor { - uint8_t bLength; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType; ///< STRING descriptor type. - uint16_t wLANGID[]; ///< LANGID codes. + uint8_t bLength; /* /< Size of this descriptor in bytes. */ + uint8_t bDescriptorType;/* /< STRING descriptor type. */ + uint16_t wLANGID[]; /* /< LANGID codes. */ }; /** USB String Descriptor. See USB 1.1 spec, pp. 204 - 205 */ struct usb_string_descriptor { - uint8_t bLength; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType; ///< STRING descriptor type. - uint16_t bString[]; ///< UNICODE encoded string. + uint8_t bLength; /* /< Size of this descriptor in bytes. */ + uint8_t bDescriptorType;/* /< STRING descriptor type. */ + uint16_t bString[]; /* /< UNICODE encoded string. */ }; /********************** USB Control Endpoint 0 related *********************/ /** USB Control Setup Data. See USB 1.1 spec, pp. 183 - 185 */ struct setup_data { - uint8_t bmRequestType; ///< Characteristics of a request. - uint8_t bRequest; ///< Specific request. - uint16_t wValue; ///< Field that varies according to request. - uint16_t wIndex; ///< Field that varies according to request. - uint16_t wLength; ///< Number of bytes to transfer in data stage. + uint8_t bmRequestType; /* /< Characteristics of a request. */ + uint8_t bRequest; /* /< Specific request. */ + uint16_t wValue; /* /< Field that varies according to request. */ + uint16_t wIndex; /* /< Field that varies according to request. */ + uint16_t wLength; /* /< Number of bytes to transfer in data stage. */ }; /* External declarations for variables that need to be accessed outside of @@ -206,9 +206,9 @@ extern volatile __xdata __at 0x7FE8 struct setup_data setup_data; /* USB Requests (bRequest): See USB 1.1 spec, table 9-4 on page 187 */ #define GET_STATUS 0 #define CLEAR_FEATURE 1 -// Value '2' is reserved for future use +/* Value '2' is reserved for future use */ #define SET_FEATURE 3 -// Value '4' is reserved for future use +/* Value '4' is reserved for future use */ #define SET_ADDRESS 5 #define GET_DESCRIPTOR 6 #define SET_DESCRIPTOR 7 @@ -226,33 +226,33 @@ extern volatile __xdata __at 0x7FE8 struct setup_data setup_data; /** USB Interrupts. See AN2131-TRM, page 9-4 for details */ enum usb_isr { - SUDAV_ISR = 13, - SOF_ISR, - SUTOK_ISR, - SUSPEND_ISR, - USBRESET_ISR, - IBN_ISR, - EP0IN_ISR, - EP0OUT_ISR, - EP1IN_ISR, - EP1OUT_ISR, - EP2IN_ISR, - EP2OUT_ISR, - EP3IN_ISR, - EP3OUT_ISR, - EP4IN_ISR, - EP4OUT_ISR, - EP5IN_ISR, - EP5OUT_ISR, - EP6IN_ISR, - EP6OUT_ISR, - EP7IN_ISR, - EP7OUT_ISR + SUDAV_ISR = 13, + SOF_ISR, + SUTOK_ISR, + SUSPEND_ISR, + USBRESET_ISR, + IBN_ISR, + EP0IN_ISR, + EP0OUT_ISR, + EP1IN_ISR, + EP1OUT_ISR, + EP2IN_ISR, + EP2OUT_ISR, + EP3IN_ISR, + EP3OUT_ISR, + EP4IN_ISR, + EP4OUT_ISR, + EP5IN_ISR, + EP5OUT_ISR, + EP6IN_ISR, + EP6OUT_ISR, + EP7IN_ISR, + EP7OUT_ISR }; /*************************** Function Prototypes ***************************/ -__xdata uint8_t* usb_get_endpoint_cs_reg(uint8_t ep); +__xdata uint8_t *usb_get_endpoint_cs_reg(uint8_t ep); void usb_reset_data_toggle(uint8_t ep); bool usb_handle_get_status(void); diff --git a/src/jtag/drivers/OpenULINK/src/delay.c b/src/jtag/drivers/OpenULINK/src/delay.c index 086633ac..d2007981 100644 --- a/src/jtag/drivers/OpenULINK/src/delay.c +++ b/src/jtag/drivers/OpenULINK/src/delay.c @@ -22,30 +22,30 @@ void delay_5us(void) { - NOP; + NOP; } -void delay_1ms(void) { - uint16_t i; +void delay_1ms(void) +{ + uint16_t i; - for (i = 0; i < 598; i++); + for (i = 0; i < 598; i++) + ; } void delay_us(uint16_t delay) { - uint16_t i; - uint16_t maxcount = (delay / 5); + uint16_t i; + uint16_t maxcount = (delay / 5); - for (i = 0; i < maxcount; i++) { - delay_5us(); - } + for (i = 0; i < maxcount; i++) + delay_5us(); } void delay_ms(uint16_t delay) { - uint16_t i; + uint16_t i; - for (i = 0; i < delay; i++) { - delay_1ms(); - } + for (i = 0; i < delay; i++) + delay_1ms(); } diff --git a/src/jtag/drivers/OpenULINK/src/jtag.c b/src/jtag/drivers/OpenULINK/src/jtag.c index 3c2fea29..946fdbe7 100644 --- a/src/jtag/drivers/OpenULINK/src/jtag.c +++ b/src/jtag/drivers/OpenULINK/src/jtag.c @@ -27,19 +27,19 @@ #include <stdbool.h> /** Delay value for SCAN_IN operations with less than maximum TCK frequency */ -uint8_t delay_scan_in = 0; +uint8_t delay_scan_in; /** Delay value for SCAN_OUT operations with less than maximum TCK frequency */ -uint8_t delay_scan_out = 0; +uint8_t delay_scan_out; /** Delay value for SCAN_IO operations with less than maximum TCK frequency */ -uint8_t delay_scan_io = 0; +uint8_t delay_scan_io; /** Delay value for CLOCK_TCK operations with less than maximum frequency */ -uint8_t delay_tck = 0; +uint8_t delay_tck; /** Delay value for CLOCK_TMS operations with less than maximum frequency */ -uint8_t delay_tms = 0; +uint8_t delay_tms; /** * Perform JTAG SCAN-IN operation at maximum TCK frequency. @@ -53,73 +53,69 @@ uint8_t delay_tms = 0; */ void jtag_scan_in(uint8_t out_offset, uint8_t in_offset) { - uint8_t scan_size_bytes, bits_last_byte; - uint8_t tms_count_start, tms_count_end; - uint8_t tms_sequence_start, tms_sequence_end; - uint8_t tdo_data, i, j; - - uint8_t outb_buffer; - - /* Get parameters from OUT2BUF */ - scan_size_bytes = OUT2BUF[out_offset]; - bits_last_byte = OUT2BUF[out_offset + 1]; - tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; - tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; - tms_sequence_start = OUT2BUF[out_offset + 3]; - tms_sequence_end = OUT2BUF[out_offset + 4]; - - if (tms_count_start > 0) { - jtag_clock_tms(tms_count_start, tms_sequence_start); - } - - outb_buffer = OUTB & ~(PIN_TDI | PIN_TCK | PIN_TMS); - - /* Shift all bytes except the last byte */ - for (i = 0; i < scan_size_bytes - 1; i++) { - tdo_data = 0; - - for (j = 0; j < 8; j++) { - OUTB = outb_buffer; /* TCK changes here */ - tdo_data = tdo_data >> 1; - OUTB = (outb_buffer | PIN_TCK); - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - } - - tdo_data = 0; - - /* Shift the last byte */ - for (j = 0; j < bits_last_byte; j++) { - /* Assert TMS signal if requested and this is the last bit */ - if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { - outb_buffer |= PIN_TMS; - tms_count_end--; - tms_sequence_end = tms_sequence_end >> 1; - } - - OUTB = outb_buffer; /* TCK change here */ - tdo_data = tdo_data >> 1; - OUTB = (outb_buffer | PIN_TCK); - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - tdo_data = tdo_data >> (8 - bits_last_byte); - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - - /* Move to correct end state */ - if (tms_count_end > 0) { - jtag_clock_tms(tms_count_end, tms_sequence_end); - } + uint8_t scan_size_bytes, bits_last_byte; + uint8_t tms_count_start, tms_count_end; + uint8_t tms_sequence_start, tms_sequence_end; + uint8_t tdo_data, i, j; + + uint8_t outb_buffer; + + /* Get parameters from OUT2BUF */ + scan_size_bytes = OUT2BUF[out_offset]; + bits_last_byte = OUT2BUF[out_offset + 1]; + tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; + tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; + tms_sequence_start = OUT2BUF[out_offset + 3]; + tms_sequence_end = OUT2BUF[out_offset + 4]; + + if (tms_count_start > 0) + jtag_clock_tms(tms_count_start, tms_sequence_start); + + outb_buffer = OUTB & ~(PIN_TDI | PIN_TCK | PIN_TMS); + + /* Shift all bytes except the last byte */ + for (i = 0; i < scan_size_bytes - 1; i++) { + tdo_data = 0; + + for (j = 0; j < 8; j++) { + OUTB = outb_buffer; /* TCK changes here */ + tdo_data = tdo_data >> 1; + OUTB = (outb_buffer | PIN_TCK); + + if (GET_TDO()) + tdo_data |= 0x80; + } + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + } + + tdo_data = 0; + + /* Shift the last byte */ + for (j = 0; j < bits_last_byte; j++) { + /* Assert TMS signal if requested and this is the last bit */ + if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { + outb_buffer |= PIN_TMS; + tms_count_end--; + tms_sequence_end = tms_sequence_end >> 1; + } + + OUTB = outb_buffer; /* TCK change here */ + tdo_data = tdo_data >> 1; + OUTB = (outb_buffer | PIN_TCK); + + if (GET_TDO()) + tdo_data |= 0x80; + } + tdo_data = tdo_data >> (8 - bits_last_byte); + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + + /* Move to correct end state */ + if (tms_count_end > 0) + jtag_clock_tms(tms_count_end, tms_sequence_end); } /** @@ -134,79 +130,79 @@ void jtag_scan_in(uint8_t out_offset, uint8_t in_offset) */ void jtag_slow_scan_in(uint8_t out_offset, uint8_t in_offset) { - uint8_t scan_size_bytes, bits_last_byte; - uint8_t tms_count_start, tms_count_end; - uint8_t tms_sequence_start, tms_sequence_end; - uint8_t tdo_data, i, j, k; - - uint8_t outb_buffer; - - /* Get parameters from OUT2BUF */ - scan_size_bytes = OUT2BUF[out_offset]; - bits_last_byte = OUT2BUF[out_offset + 1]; - tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; - tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; - tms_sequence_start = OUT2BUF[out_offset + 3]; - tms_sequence_end = OUT2BUF[out_offset + 4]; - - if (tms_count_start > 0) { - jtag_slow_clock_tms(tms_count_start, tms_sequence_start); - } - - outb_buffer = OUTB & ~(PIN_TDI | PIN_TCK | PIN_TMS); - - /* Shift all bytes except the last byte */ - for (i = 0; i < scan_size_bytes - 1; i++) { - tdo_data = 0; - - for (j = 0; j < 8; j++) { - OUTB = outb_buffer; /* TCK changes here */ - for (k = 0; k < delay_scan_in; k++); - tdo_data = tdo_data >> 1; - - OUTB = (outb_buffer | PIN_TCK); - for (k = 0; k < delay_scan_in; k++); - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - } - - tdo_data = 0; - - /* Shift the last byte */ - for (j = 0; j < bits_last_byte; j++) { - /* Assert TMS signal if requested and this is the last bit */ - if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { - outb_buffer |= PIN_TMS; - tms_count_end--; - tms_sequence_end = tms_sequence_end >> 1; - } - - OUTB = outb_buffer; /* TCK change here */ - for (k = 0; k < delay_scan_in; k++); - tdo_data = tdo_data >> 1; - - OUTB = (outb_buffer | PIN_TCK); - for (k = 0; k < delay_scan_in; k++); - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - tdo_data = tdo_data >> (8 - bits_last_byte); - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - - /* Move to correct end state */ - if (tms_count_end > 0) { - jtag_slow_clock_tms(tms_count_end, tms_sequence_end); - } + uint8_t scan_size_bytes, bits_last_byte; + uint8_t tms_count_start, tms_count_end; + uint8_t tms_sequence_start, tms_sequence_end; + uint8_t tdo_data, i, j, k; + + uint8_t outb_buffer; + + /* Get parameters from OUT2BUF */ + scan_size_bytes = OUT2BUF[out_offset]; + bits_last_byte = OUT2BUF[out_offset + 1]; + tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; + tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; + tms_sequence_start = OUT2BUF[out_offset + 3]; + tms_sequence_end = OUT2BUF[out_offset + 4]; + + if (tms_count_start > 0) + jtag_slow_clock_tms(tms_count_start, tms_sequence_start); + + outb_buffer = OUTB & ~(PIN_TDI | PIN_TCK | PIN_TMS); + + /* Shift all bytes except the last byte */ + for (i = 0; i < scan_size_bytes - 1; i++) { + tdo_data = 0; + + for (j = 0; j < 8; j++) { + OUTB = outb_buffer; /* TCK changes here */ + for (k = 0; k < delay_scan_in; k++) + ; + tdo_data = tdo_data >> 1; + + OUTB = (outb_buffer | PIN_TCK); + for (k = 0; k < delay_scan_in; k++) + ; + + if (GET_TDO()) + tdo_data |= 0x80; + } + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + } + + tdo_data = 0; + + /* Shift the last byte */ + for (j = 0; j < bits_last_byte; j++) { + /* Assert TMS signal if requested and this is the last bit */ + if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { + outb_buffer |= PIN_TMS; + tms_count_end--; + tms_sequence_end = tms_sequence_end >> 1; + } + + OUTB = outb_buffer; /* TCK change here */ + for (k = 0; k < delay_scan_in; k++) + ; + tdo_data = tdo_data >> 1; + + OUTB = (outb_buffer | PIN_TCK); + for (k = 0; k < delay_scan_in; k++) + ; + + if (GET_TDO()) + tdo_data |= 0x80; + } + tdo_data = tdo_data >> (8 - bits_last_byte); + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + + /* Move to correct end state */ + if (tms_count_end > 0) + jtag_slow_clock_tms(tms_count_end, tms_sequence_end); } /** @@ -222,72 +218,66 @@ void jtag_slow_scan_in(uint8_t out_offset, uint8_t in_offset) */ void jtag_scan_out(uint8_t out_offset) { - uint8_t scan_size_bytes, bits_last_byte; - uint8_t tms_count_start, tms_count_end; - uint8_t tms_sequence_start, tms_sequence_end; - uint8_t tdi_data, i, j; - - uint8_t outb_buffer; - - /* Get parameters from OUT2BUF */ - scan_size_bytes = OUT2BUF[out_offset]; - bits_last_byte = OUT2BUF[out_offset + 1]; - tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; - tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; - tms_sequence_start = OUT2BUF[out_offset + 3]; - tms_sequence_end = OUT2BUF[out_offset + 4]; - - if (tms_count_start > 0) { - jtag_clock_tms(tms_count_start, tms_sequence_start); - } - - outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); - - /* Shift all bytes except the last byte */ - for (i = 0; i < scan_size_bytes - 1; i++) { - tdi_data = OUT2BUF[i + out_offset + 5]; - - for (j = 0; j < 8; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - tdi_data = tdi_data >> 1; - OUTB = (outb_buffer | PIN_TCK); - } - } - - tdi_data = OUT2BUF[i + out_offset + 5]; - - /* Shift the last byte */ - for (j = 0; j < bits_last_byte; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - /* Assert TMS signal if requested and this is the last bit */ - if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { - outb_buffer |= PIN_TMS; - tms_count_end--; - tms_sequence_end = tms_sequence_end >> 1; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - tdi_data = tdi_data >> 1; - OUTB = (outb_buffer | PIN_TCK); - } - - /* Move to correct end state */ - if (tms_count_end > 0) { - jtag_clock_tms(tms_count_end, tms_sequence_end); - } + uint8_t scan_size_bytes, bits_last_byte; + uint8_t tms_count_start, tms_count_end; + uint8_t tms_sequence_start, tms_sequence_end; + uint8_t tdi_data, i, j; + + uint8_t outb_buffer; + + /* Get parameters from OUT2BUF */ + scan_size_bytes = OUT2BUF[out_offset]; + bits_last_byte = OUT2BUF[out_offset + 1]; + tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; + tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; + tms_sequence_start = OUT2BUF[out_offset + 3]; + tms_sequence_end = OUT2BUF[out_offset + 4]; + + if (tms_count_start > 0) + jtag_clock_tms(tms_count_start, tms_sequence_start); + + outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); + + /* Shift all bytes except the last byte */ + for (i = 0; i < scan_size_bytes - 1; i++) { + tdi_data = OUT2BUF[i + out_offset + 5]; + + for (j = 0; j < 8; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + OUTB = outb_buffer; /* TDI and TCK change here */ + tdi_data = tdi_data >> 1; + OUTB = (outb_buffer | PIN_TCK); + } + } + + tdi_data = OUT2BUF[i + out_offset + 5]; + + /* Shift the last byte */ + for (j = 0; j < bits_last_byte; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + /* Assert TMS signal if requested and this is the last bit */ + if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { + outb_buffer |= PIN_TMS; + tms_count_end--; + tms_sequence_end = tms_sequence_end >> 1; + } + + OUTB = outb_buffer; /* TDI and TCK change here */ + tdi_data = tdi_data >> 1; + OUTB = (outb_buffer | PIN_TCK); + } + + /* Move to correct end state */ + if (tms_count_end > 0) + jtag_clock_tms(tms_count_end, tms_sequence_end); } /** @@ -303,78 +293,76 @@ void jtag_scan_out(uint8_t out_offset) */ void jtag_slow_scan_out(uint8_t out_offset) { - uint8_t scan_size_bytes, bits_last_byte; - uint8_t tms_count_start, tms_count_end; - uint8_t tms_sequence_start, tms_sequence_end; - uint8_t tdi_data, i, j, k; - - uint8_t outb_buffer; - - /* Get parameters from OUT2BUF */ - scan_size_bytes = OUT2BUF[out_offset]; - bits_last_byte = OUT2BUF[out_offset + 1]; - tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; - tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; - tms_sequence_start = OUT2BUF[out_offset + 3]; - tms_sequence_end = OUT2BUF[out_offset + 4]; - - if (tms_count_start > 0) { - jtag_slow_clock_tms(tms_count_start, tms_sequence_start); - } - - outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); - - /* Shift all bytes except the last byte */ - for (i = 0; i < scan_size_bytes - 1; i++) { - tdi_data = OUT2BUF[i + out_offset + 5]; - - for (j = 0; j < 8; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - for (k = 0; k < delay_scan_out; k++); - tdi_data = tdi_data >> 1; - - OUTB = (outb_buffer | PIN_TCK); - for (k = 0; k < delay_scan_out; k++); - } - } - - tdi_data = OUT2BUF[i + out_offset + 5]; - - /* Shift the last byte */ - for (j = 0; j < bits_last_byte; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - /* Assert TMS signal if requested and this is the last bit */ - if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { - outb_buffer |= PIN_TMS; - tms_count_end--; - tms_sequence_end = tms_sequence_end >> 1; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - for (k = 0; k < delay_scan_out; k++); - tdi_data = tdi_data >> 1; - - OUTB = (outb_buffer | PIN_TCK); - for (k = 0; k < delay_scan_out; k++); - } - - /* Move to correct end state */ - if (tms_count_end > 0) { - jtag_slow_clock_tms(tms_count_end, tms_sequence_end); - } + uint8_t scan_size_bytes, bits_last_byte; + uint8_t tms_count_start, tms_count_end; + uint8_t tms_sequence_start, tms_sequence_end; + uint8_t tdi_data, i, j, k; + + uint8_t outb_buffer; + + /* Get parameters from OUT2BUF */ + scan_size_bytes = OUT2BUF[out_offset]; + bits_last_byte = OUT2BUF[out_offset + 1]; + tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; + tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; + tms_sequence_start = OUT2BUF[out_offset + 3]; + tms_sequence_end = OUT2BUF[out_offset + 4]; + + if (tms_count_start > 0) + jtag_slow_clock_tms(tms_count_start, tms_sequence_start); + + outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); + + /* Shift all bytes except the last byte */ + for (i = 0; i < scan_size_bytes - 1; i++) { + tdi_data = OUT2BUF[i + out_offset + 5]; + + for (j = 0; j < 8; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + OUTB = outb_buffer; /* TDI and TCK change here */ + for (k = 0; k < delay_scan_out; k++) + ; + tdi_data = tdi_data >> 1; + + OUTB = (outb_buffer | PIN_TCK); + for (k = 0; k < delay_scan_out; k++) + ; + } + } + + tdi_data = OUT2BUF[i + out_offset + 5]; + + /* Shift the last byte */ + for (j = 0; j < bits_last_byte; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + /* Assert TMS signal if requested and this is the last bit */ + if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { + outb_buffer |= PIN_TMS; + tms_count_end--; + tms_sequence_end = tms_sequence_end >> 1; + } + + OUTB = outb_buffer; /* TDI and TCK change here */ + for (k = 0; k < delay_scan_out; k++) + ; + tdi_data = tdi_data >> 1; + + OUTB = (outb_buffer | PIN_TCK); + for (k = 0; k < delay_scan_out; k++) + ; + } + + /* Move to correct end state */ + if (tms_count_end > 0) + jtag_slow_clock_tms(tms_count_end, tms_sequence_end); } /** @@ -390,91 +378,83 @@ void jtag_slow_scan_out(uint8_t out_offset) */ void jtag_scan_io(uint8_t out_offset, uint8_t in_offset) { - uint8_t scan_size_bytes, bits_last_byte; - uint8_t tms_count_start, tms_count_end; - uint8_t tms_sequence_start, tms_sequence_end; - uint8_t tdi_data, tdo_data, i, j; - - uint8_t outb_buffer; - - /* Get parameters from OUT2BUF */ - scan_size_bytes = OUT2BUF[out_offset]; - bits_last_byte = OUT2BUF[out_offset + 1]; - tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; - tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; - tms_sequence_start = OUT2BUF[out_offset + 3]; - tms_sequence_end = OUT2BUF[out_offset + 4]; - - if (tms_count_start > 0) { - jtag_clock_tms(tms_count_start, tms_sequence_start); - } - - outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); - - /* Shift all bytes except the last byte */ - for (i = 0; i < scan_size_bytes - 1; i++) { - tdi_data = OUT2BUF[i + out_offset + 5]; - tdo_data = 0; - - for (j = 0; j < 8; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - tdi_data = tdi_data >> 1; - OUTB = (outb_buffer | PIN_TCK); - tdo_data = tdo_data >> 1; - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - } - - tdi_data = OUT2BUF[i + out_offset + 5]; - tdo_data = 0; - - /* Shift the last byte */ - for (j = 0; j < bits_last_byte; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - /* Assert TMS signal if requested and this is the last bit */ - if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { - outb_buffer |= PIN_TMS; - tms_count_end--; - tms_sequence_end = tms_sequence_end >> 1; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - tdi_data = tdi_data >> 1; - OUTB = (outb_buffer | PIN_TCK); - tdo_data = tdo_data >> 1; - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - tdo_data = tdo_data >> (8 - bits_last_byte); - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - - /* Move to correct end state */ - if (tms_count_end > 0) { - jtag_clock_tms(tms_count_end, tms_sequence_end); - } + uint8_t scan_size_bytes, bits_last_byte; + uint8_t tms_count_start, tms_count_end; + uint8_t tms_sequence_start, tms_sequence_end; + uint8_t tdi_data, tdo_data, i, j; + + uint8_t outb_buffer; + + /* Get parameters from OUT2BUF */ + scan_size_bytes = OUT2BUF[out_offset]; + bits_last_byte = OUT2BUF[out_offset + 1]; + tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; + tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; + tms_sequence_start = OUT2BUF[out_offset + 3]; + tms_sequence_end = OUT2BUF[out_offset + 4]; + + if (tms_count_start > 0) + jtag_clock_tms(tms_count_start, tms_sequence_start); + + outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); + + /* Shift all bytes except the last byte */ + for (i = 0; i < scan_size_bytes - 1; i++) { + tdi_data = OUT2BUF[i + out_offset + 5]; + tdo_data = 0; + + for (j = 0; j < 8; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + OUTB = outb_buffer; /* TDI and TCK change here */ + tdi_data = tdi_data >> 1; + OUTB = (outb_buffer | PIN_TCK); + tdo_data = tdo_data >> 1; + + if (GET_TDO()) + tdo_data |= 0x80; + } + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + } + + tdi_data = OUT2BUF[i + out_offset + 5]; + tdo_data = 0; + + /* Shift the last byte */ + for (j = 0; j < bits_last_byte; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + /* Assert TMS signal if requested and this is the last bit */ + if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { + outb_buffer |= PIN_TMS; + tms_count_end--; + tms_sequence_end = tms_sequence_end >> 1; + } + + OUTB = outb_buffer; /* TDI and TCK change here */ + tdi_data = tdi_data >> 1; + OUTB = (outb_buffer | PIN_TCK); + tdo_data = tdo_data >> 1; + + if (GET_TDO()) + tdo_data |= 0x80; + } + tdo_data = tdo_data >> (8 - bits_last_byte); + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + + /* Move to correct end state */ + if (tms_count_end > 0) + jtag_clock_tms(tms_count_end, tms_sequence_end); } /** @@ -490,97 +470,93 @@ void jtag_scan_io(uint8_t out_offset, uint8_t in_offset) */ void jtag_slow_scan_io(uint8_t out_offset, uint8_t in_offset) { - uint8_t scan_size_bytes, bits_last_byte; - uint8_t tms_count_start, tms_count_end; - uint8_t tms_sequence_start, tms_sequence_end; - uint8_t tdi_data, tdo_data, i, j, k; - - uint8_t outb_buffer; - - /* Get parameters from OUT2BUF */ - scan_size_bytes = OUT2BUF[out_offset]; - bits_last_byte = OUT2BUF[out_offset + 1]; - tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; - tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; - tms_sequence_start = OUT2BUF[out_offset + 3]; - tms_sequence_end = OUT2BUF[out_offset + 4]; - - if (tms_count_start > 0) { - jtag_slow_clock_tms(tms_count_start, tms_sequence_start); - } - - outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); - - /* Shift all bytes except the last byte */ - for (i = 0; i < scan_size_bytes - 1; i++) { - tdi_data = OUT2BUF[i + out_offset + 5]; - tdo_data = 0; - - for (j = 0; j < 8; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - for (k = 0; k < delay_scan_io; k++); - tdi_data = tdi_data >> 1; - - OUTB = (outb_buffer | PIN_TCK); - for (k = 0; k < delay_scan_io; k++); - tdo_data = tdo_data >> 1; - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - } - - tdi_data = OUT2BUF[i + out_offset + 5]; - tdo_data = 0; - - /* Shift the last byte */ - for (j = 0; j < bits_last_byte; j++) { - if (tdi_data & 0x01) { - outb_buffer |= PIN_TDI; - } - else { - outb_buffer &= ~PIN_TDI; - } - - /* Assert TMS signal if requested and this is the last bit */ - if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { - outb_buffer |= PIN_TMS; - tms_count_end--; - tms_sequence_end = tms_sequence_end >> 1; - } - - OUTB = outb_buffer; /* TDI and TCK change here */ - for (k = 0; k < delay_scan_io; k++); - tdi_data = tdi_data >> 1; - - OUTB = (outb_buffer | PIN_TCK); - for (k = 0; k < delay_scan_io; k++); - tdo_data = tdo_data >> 1; - - if (GET_TDO()) { - tdo_data |= 0x80; - } - } - tdo_data = tdo_data >> (8 - bits_last_byte); - - /* Copy TDO data to IN2BUF */ - IN2BUF[i + in_offset] = tdo_data; - - /* Move to correct end state */ - if (tms_count_end > 0) { - jtag_slow_clock_tms(tms_count_end, tms_sequence_end); - } + uint8_t scan_size_bytes, bits_last_byte; + uint8_t tms_count_start, tms_count_end; + uint8_t tms_sequence_start, tms_sequence_end; + uint8_t tdi_data, tdo_data, i, j, k; + + uint8_t outb_buffer; + + /* Get parameters from OUT2BUF */ + scan_size_bytes = OUT2BUF[out_offset]; + bits_last_byte = OUT2BUF[out_offset + 1]; + tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F; + tms_count_end = OUT2BUF[out_offset + 2] & 0x0F; + tms_sequence_start = OUT2BUF[out_offset + 3]; + tms_sequence_end = OUT2BUF[out_offset + 4]; + + if (tms_count_start > 0) + jtag_slow_clock_tms(tms_count_start, tms_sequence_start); + + outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS); + + /* Shift all bytes except the last byte */ + for (i = 0; i < scan_size_bytes - 1; i++) { + tdi_data = OUT2BUF[i + out_offset + 5]; + tdo_data = 0; + + for (j = 0; j < 8; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + OUTB = outb_buffer; /* TDI and TCK change here */ + for (k = 0; k < delay_scan_io; k++) + ; + tdi_data = tdi_data >> 1; + + OUTB = (outb_buffer | PIN_TCK); + for (k = 0; k < delay_scan_io; k++) + ; + tdo_data = tdo_data >> 1; + + if (GET_TDO()) + tdo_data |= 0x80; + } + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + } + + tdi_data = OUT2BUF[i + out_offset + 5]; + tdo_data = 0; + + /* Shift the last byte */ + for (j = 0; j < bits_last_byte; j++) { + if (tdi_data & 0x01) + outb_buffer |= PIN_TDI; + else + outb_buffer &= ~PIN_TDI; + + /* Assert TMS signal if requested and this is the last bit */ + if ((j == bits_last_byte - 1) && (tms_count_end > 0)) { + outb_buffer |= PIN_TMS; + tms_count_end--; + tms_sequence_end = tms_sequence_end >> 1; + } + + OUTB = outb_buffer; /* TDI and TCK change here */ + for (k = 0; k < delay_scan_io; k++) + ; + tdi_data = tdi_data >> 1; + + OUTB = (outb_buffer | PIN_TCK); + for (k = 0; k < delay_scan_io; k++) + ; + tdo_data = tdo_data >> 1; + + if (GET_TDO()) + tdo_data |= 0x80; + } + tdo_data = tdo_data >> (8 - bits_last_byte); + + /* Copy TDO data to IN2BUF */ + IN2BUF[i + in_offset] = tdo_data; + + /* Move to correct end state */ + if (tms_count_end > 0) + jtag_slow_clock_tms(tms_count_end, tms_sequence_end); } /** @@ -592,13 +568,13 @@ void jtag_slow_scan_io(uint8_t out_offset, uint8_t in_offset) */ void jtag_clock_tck(uint16_t count) { - uint16_t i; - uint8_t outb_buffer = OUTB & ~(PIN_TCK); + uint16_t i; + uint8_t outb_buffer = OUTB & ~(PIN_TCK); - for ( i = 0; i < count; i++ ) { - OUTB = outb_buffer; - OUTB = outb_buffer | PIN_TCK; - } + for (i = 0; i < count; i++) { + OUTB = outb_buffer; + OUTB = outb_buffer | PIN_TCK; + } } /** @@ -610,16 +586,18 @@ void jtag_clock_tck(uint16_t count) */ void jtag_slow_clock_tck(uint16_t count) { - uint16_t i; - uint8_t j; - uint8_t outb_buffer = OUTB & ~(PIN_TCK); - - for ( i = 0; i < count; i++ ) { - OUTB = outb_buffer; - for (j = 0; j < delay_tck; j++); - OUTB = outb_buffer | PIN_TCK; - for (j = 0; j < delay_tck; j++); - } + uint16_t i; + uint8_t j; + uint8_t outb_buffer = OUTB & ~(PIN_TCK); + + for (i = 0; i < count; i++) { + OUTB = outb_buffer; + for (j = 0; j < delay_tck; j++) + ; + OUTB = outb_buffer | PIN_TCK; + for (j = 0; j < delay_tck; j++) + ; + } } /** @@ -633,22 +611,20 @@ void jtag_slow_clock_tck(uint16_t count) */ void jtag_clock_tms(uint8_t count, uint8_t sequence) { - uint8_t outb_buffer = OUTB & ~(PIN_TCK); - uint8_t i; - - for ( i = 0; i < count; i++ ) { - /* Set TMS pin according to sequence parameter */ - if ( sequence & 0x1 ) { - outb_buffer |= PIN_TMS; - } - else { - outb_buffer &= ~PIN_TMS; - } - - OUTB = outb_buffer; - sequence = sequence >> 1; - OUTB = outb_buffer | PIN_TCK; - } + uint8_t outb_buffer = OUTB & ~(PIN_TCK); + uint8_t i; + + for (i = 0; i < count; i++) { + /* Set TMS pin according to sequence parameter */ + if (sequence & 0x1) + outb_buffer |= PIN_TMS; + else + outb_buffer &= ~PIN_TMS; + + OUTB = outb_buffer; + sequence = sequence >> 1; + OUTB = outb_buffer | PIN_TCK; + } } /** @@ -662,24 +638,24 @@ void jtag_clock_tms(uint8_t count, uint8_t sequence) */ void jtag_slow_clock_tms(uint8_t count, uint8_t sequence) { - uint8_t outb_buffer = OUTB & ~(PIN_TCK); - uint8_t i, j; - - for (i = 0; i < count; i++) { - /* Set TMS pin according to sequence parameter */ - if ( sequence & 0x1 ) { - outb_buffer |= PIN_TMS; - } - else { - outb_buffer &= ~PIN_TMS; - } - - OUTB = outb_buffer; - for (j = 0; j < delay_tms; j++); - sequence = sequence >> 1; - OUTB = outb_buffer | PIN_TCK; - for (j = 0; j < delay_tms; j++); - } + uint8_t outb_buffer = OUTB & ~(PIN_TCK); + uint8_t i, j; + + for (i = 0; i < count; i++) { + /* Set TMS pin according to sequence parameter */ + if (sequence & 0x1) + outb_buffer |= PIN_TMS; + else + outb_buffer &= ~PIN_TMS; + + OUTB = outb_buffer; + for (j = 0; j < delay_tms; j++) + ; + sequence = sequence >> 1; + OUTB = outb_buffer | PIN_TCK; + for (j = 0; j < delay_tms; j++) + ; + } } /** @@ -691,31 +667,28 @@ void jtag_slow_clock_tms(uint8_t count, uint8_t sequence) */ uint16_t jtag_get_signals(void) { - uint8_t input_signal_state, output_signal_state; - - input_signal_state = 0; - output_signal_state = 0; - - /* Get states of input pins */ - if (GET_TDO()) { - input_signal_state |= SIGNAL_TDO; - } - if (GET_BRKOUT()) { - input_signal_state |= SIGNAL_BRKOUT; - } - if (GET_TRAP()) { - input_signal_state |= SIGNAL_TRAP; - } - if (GET_RTCK()) { - /* Using RTCK this way would be extremely slow, - * implemented only for the sake of completeness */ - input_signal_state |= SIGNAL_RTCK; - } - - /* Get states of output pins */ - output_signal_state = PINSB & MASK_PORTB_DIRECTION_OUT; - - return ((uint16_t)input_signal_state << 8) | ((uint16_t)output_signal_state); + uint8_t input_signal_state, output_signal_state; + + input_signal_state = 0; + output_signal_state = 0; + + /* Get states of input pins */ + if (GET_TDO()) + input_signal_state |= SIGNAL_TDO; + if (GET_BRKOUT()) + input_signal_state |= SIGNAL_BRKOUT; + if (GET_TRAP()) + input_signal_state |= SIGNAL_TRAP; + if (GET_RTCK()) { + /* Using RTCK this way would be extremely slow, + * implemented only for the sake of completeness */ + input_signal_state |= SIGNAL_RTCK; + } + + /* Get states of output pins */ + output_signal_state = PINSB & MASK_PORTB_DIRECTION_OUT; + + return ((uint16_t)input_signal_state << 8) | ((uint16_t)output_signal_state); } /** @@ -726,8 +699,8 @@ uint16_t jtag_get_signals(void) */ void jtag_set_signals(uint8_t low, uint8_t high) { - OUTB &= ~(low & MASK_PORTB_DIRECTION_OUT); - OUTB |= (high & MASK_PORTB_DIRECTION_OUT); + OUTB &= ~(low & MASK_PORTB_DIRECTION_OUT); + OUTB |= (high & MASK_PORTB_DIRECTION_OUT); } /** @@ -740,11 +713,11 @@ void jtag_set_signals(uint8_t low, uint8_t high) * @param tms number of delay cycles in clock_tms operations. */ void jtag_configure_tck_delay(uint8_t scan_in, uint8_t scan_out, - uint8_t scan_io, uint8_t tck, uint8_t tms) + uint8_t scan_io, uint8_t tck, uint8_t tms) { - delay_scan_in = scan_in; - delay_scan_out = scan_out; - delay_scan_io = scan_io; - delay_tck = tck; - delay_tms = tms; + delay_scan_in = scan_in; + delay_scan_out = scan_out; + delay_scan_io = scan_io; + delay_tck = tck; + delay_tms = tms; } diff --git a/src/jtag/drivers/OpenULINK/src/main.c b/src/jtag/drivers/OpenULINK/src/main.c index 067393bd..5beabd18 100644 --- a/src/jtag/drivers/OpenULINK/src/main.c +++ b/src/jtag/drivers/OpenULINK/src/main.c @@ -49,43 +49,43 @@ extern void ep7out_isr(void) __interrupt; void io_init(void) { - /* PORTxCFG register bits select alternate functions (1 == alternate function, - * 0 == standard I/O) - * OEx register bits turn on/off output buffer (1 == output, 0 == input) - * OUTx register bits determine pin state of output - * PINx register bits reflect pin state (high == 1, low == 0) */ + /* PORTxCFG register bits select alternate functions (1 == alternate function, + * 0 == standard I/O) + * OEx register bits turn on/off output buffer (1 == output, 0 == input) + * OUTx register bits determine pin state of output + * PINx register bits reflect pin state (high == 1, low == 0) */ - /* PORT A */ - PORTACFG = PIN_OE; - OEA = PIN_U_OE | PIN_OE | PIN_RUN_LED | PIN_COM_LED; - OUTA = PIN_RUN_LED | PIN_COM_LED; + /* PORT A */ + PORTACFG = PIN_OE; + OEA = PIN_U_OE | PIN_OE | PIN_RUN_LED | PIN_COM_LED; + OUTA = PIN_RUN_LED | PIN_COM_LED; - /* PORT B */ - PORTBCFG = 0x00; - OEB = PIN_TDI | PIN_TMS | PIN_TCK | PIN_TRST | PIN_BRKIN | PIN_RESET - | PIN_OCDSE; + /* PORT B */ + PORTBCFG = 0x00; + OEB = PIN_TDI | PIN_TMS | PIN_TCK | PIN_TRST | PIN_BRKIN | PIN_RESET + | PIN_OCDSE; - /* TRST and RESET signals are low-active but inverted by hardware, so we clear - * these signals here! */ - OUTB = 0x00; + /* TRST and RESET signals are low-active but inverted by hardware, so we clear + * these signals here! */ + OUTB = 0x00; - /* PORT C */ - PORTCCFG = PIN_WR; - OEC = PIN_TXD0 | PIN_WR; - OUTC = 0x00; + /* PORT C */ + PORTCCFG = PIN_WR; + OEC = PIN_TXD0 | PIN_WR; + OUTC = 0x00; } int main(void) { - io_init(); - usb_init(); + io_init(); + usb_init(); - /* Enable Interrupts */ - EA = 1; + /* Enable Interrupts */ + EA = 1; - /* Begin executing command(s). This function never returns. */ - command_loop(); + /* Begin executing command(s). This function never returns. */ + command_loop(); - /* Never reached, but SDCC complains about missing return statement */ - return 0; + /* Never reached, but SDCC complains about missing return statement */ + return 0; } diff --git a/src/jtag/drivers/OpenULINK/src/protocol.c b/src/jtag/drivers/OpenULINK/src/protocol.c index ae63604f..1034c04f 100644 --- a/src/jtag/drivers/OpenULINK/src/protocol.c +++ b/src/jtag/drivers/OpenULINK/src/protocol.c @@ -34,12 +34,12 @@ * are configured to use the maximum packet size for full-speed transfers, * 64 bytes. Commands always start with a command ID (see msgtypes.h for * command ID definitions) and contain zero or more payload data bytes in both - * transfer directions (IN and OUT). The payload + * transfer directions (IN and OUT). The payload * * Almost all commands contain a fixed number of payload data bytes. The number * of payload data bytes for the IN and OUT direction does not need to be the * same. - * + * * Multiple commands may be sent in one EP2 Bulk-OUT packet. Because the * OpenULINK firmware does not perform bounds checking for EP2 Bulk-IN packets, * the host MUST ensure that the commands sent in the OUT packet require a @@ -57,23 +57,19 @@ volatile uint8_t payload_index_in; */ void execute_set_led_command(void) { - uint8_t led_state = OUT2BUF[cmd_id_index + 1]; + uint8_t led_state = OUT2BUF[cmd_id_index + 1]; - if (led_state & RUN_LED_ON) { - SET_RUN_LED(); - } + if (led_state & RUN_LED_ON) + SET_RUN_LED(); - if (led_state & COM_LED_ON) { - SET_COM_LED(); - } + if (led_state & COM_LED_ON) + SET_COM_LED(); - if (led_state & RUN_LED_OFF) { - CLEAR_RUN_LED(); - } + if (led_state & RUN_LED_OFF) + CLEAR_RUN_LED(); - if (led_state & COM_LED_OFF) { - CLEAR_COM_LED(); - } + if (led_state & COM_LED_OFF) + CLEAR_COM_LED(); } /** @@ -87,123 +83,122 @@ void execute_set_led_command(void) */ bool execute_command(void) { - uint8_t usb_out_bytecount, usb_in_bytecount; - uint16_t signal_state; - uint16_t count; - - /* Most commands do not transfer IN data. To save code space, we write 0 to - * usb_in_bytecount here, then modify it in the switch statement below where - * neccessary */ - usb_in_bytecount = 0; - - switch (OUT2BUF[cmd_id_index] /* Command ID */) { - case CMD_SCAN_IN: - usb_out_bytecount = 5; - usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; - jtag_scan_in(cmd_id_index + 1, payload_index_in); - break; - case CMD_SCAN_OUT: - usb_out_bytecount = OUT2BUF[cmd_id_index + 1] + 5; - jtag_scan_out(cmd_id_index + 1); - break; - case CMD_SCAN_IO: - usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; - usb_out_bytecount = usb_in_bytecount + 5; - jtag_scan_io(cmd_id_index + 1, payload_index_in); - break; - case CMD_CLOCK_TMS: - usb_out_bytecount = 2; - jtag_clock_tms(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]); - break; - case CMD_CLOCK_TCK: - usb_out_bytecount = 2; - count = (uint16_t)OUT2BUF[cmd_id_index + 1]; - count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; - jtag_clock_tck(count); - break; - case CMD_SLOW_SCAN_IN: - usb_out_bytecount = 5; - usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; - jtag_slow_scan_in(cmd_id_index + 1, payload_index_in); - break; - case CMD_SLOW_SCAN_OUT: - usb_out_bytecount = OUT2BUF[cmd_id_index + 1] + 5; - jtag_slow_scan_out(cmd_id_index + 1); - break; - case CMD_SLOW_SCAN_IO: - usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; - usb_out_bytecount = usb_in_bytecount + 5; - jtag_slow_scan_io(cmd_id_index + 1, payload_index_in); - break; - case CMD_SLOW_CLOCK_TMS: - usb_out_bytecount = 2; - jtag_slow_clock_tms(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]); - break; - case CMD_SLOW_CLOCK_TCK: - usb_out_bytecount = 2; - count = (uint16_t)OUT2BUF[cmd_id_index + 1]; - count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; - jtag_slow_clock_tck(count); - break; - case CMD_SLEEP_US: - usb_out_bytecount = 2; - count = (uint16_t)OUT2BUF[cmd_id_index + 1]; - count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; - delay_us(count); - break; - case CMD_SLEEP_MS: - usb_out_bytecount = 2; - count = (uint16_t)OUT2BUF[cmd_id_index + 1]; - count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; - delay_ms(count); - break; - case CMD_GET_SIGNALS: - usb_out_bytecount = 0; - usb_in_bytecount = 2; - signal_state = jtag_get_signals(); - IN2BUF[payload_index_in] = (signal_state >> 8) & 0x00FF; - IN2BUF[payload_index_in + 1] = signal_state & 0x00FF; - break; - case CMD_SET_SIGNALS: - usb_out_bytecount = 2; - jtag_set_signals(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]); - break; - case CMD_CONFIGURE_TCK_FREQ: - usb_out_bytecount = 5; - jtag_configure_tck_delay( - OUT2BUF[cmd_id_index + 1], /* scan_in */ - OUT2BUF[cmd_id_index + 2], /* scan_out */ - OUT2BUF[cmd_id_index + 3], /* scan_io */ - OUT2BUF[cmd_id_index + 4], /* clock_tck */ - OUT2BUF[cmd_id_index + 5]); /* clock_tms */ - break; - case CMD_SET_LEDS: - usb_out_bytecount = 1; - execute_set_led_command(); - break; - case CMD_TEST: - usb_out_bytecount = 1; - /* Do nothing... This command is only used to test if the device is ready - * to accept new commands */ - break; - default: - /* Should never be reached */ - usb_out_bytecount = 0; - break; - } - - /* Update EP2 Bulk-IN data byte count */ - payload_index_in += usb_in_bytecount; - - /* Determine if this was the last command */ - if ((cmd_id_index + usb_out_bytecount + 1) >= OUT2BC) { - return true; - } - else { - /* Not the last command, update cmd_id_index */ - cmd_id_index += (usb_out_bytecount + 1); - return false; - } + uint8_t usb_out_bytecount, usb_in_bytecount; + uint16_t signal_state; + uint16_t count; + + /* Most commands do not transfer IN data. To save code space, we write 0 to + * usb_in_bytecount here, then modify it in the switch statement below where + * neccessary */ + usb_in_bytecount = 0; + + switch (OUT2BUF[cmd_id_index] /* Command ID */) { + case CMD_SCAN_IN: + usb_out_bytecount = 5; + usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; + jtag_scan_in(cmd_id_index + 1, payload_index_in); + break; + case CMD_SCAN_OUT: + usb_out_bytecount = OUT2BUF[cmd_id_index + 1] + 5; + jtag_scan_out(cmd_id_index + 1); + break; + case CMD_SCAN_IO: + usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; + usb_out_bytecount = usb_in_bytecount + 5; + jtag_scan_io(cmd_id_index + 1, payload_index_in); + break; + case CMD_CLOCK_TMS: + usb_out_bytecount = 2; + jtag_clock_tms(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]); + break; + case CMD_CLOCK_TCK: + usb_out_bytecount = 2; + count = (uint16_t)OUT2BUF[cmd_id_index + 1]; + count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; + jtag_clock_tck(count); + break; + case CMD_SLOW_SCAN_IN: + usb_out_bytecount = 5; + usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; + jtag_slow_scan_in(cmd_id_index + 1, payload_index_in); + break; + case CMD_SLOW_SCAN_OUT: + usb_out_bytecount = OUT2BUF[cmd_id_index + 1] + 5; + jtag_slow_scan_out(cmd_id_index + 1); + break; + case CMD_SLOW_SCAN_IO: + usb_in_bytecount = OUT2BUF[cmd_id_index + 1]; + usb_out_bytecount = usb_in_bytecount + 5; + jtag_slow_scan_io(cmd_id_index + 1, payload_index_in); + break; + case CMD_SLOW_CLOCK_TMS: + usb_out_bytecount = 2; + jtag_slow_clock_tms(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]); + break; + case CMD_SLOW_CLOCK_TCK: + usb_out_bytecount = 2; + count = (uint16_t)OUT2BUF[cmd_id_index + 1]; + count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; + jtag_slow_clock_tck(count); + break; + case CMD_SLEEP_US: + usb_out_bytecount = 2; + count = (uint16_t)OUT2BUF[cmd_id_index + 1]; + count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; + delay_us(count); + break; + case CMD_SLEEP_MS: + usb_out_bytecount = 2; + count = (uint16_t)OUT2BUF[cmd_id_index + 1]; + count |= ((uint16_t)OUT2BUF[cmd_id_index + 2]) << 8; + delay_ms(count); + break; + case CMD_GET_SIGNALS: + usb_out_bytecount = 0; + usb_in_bytecount = 2; + signal_state = jtag_get_signals(); + IN2BUF[payload_index_in] = (signal_state >> 8) & 0x00FF; + IN2BUF[payload_index_in + 1] = signal_state & 0x00FF; + break; + case CMD_SET_SIGNALS: + usb_out_bytecount = 2; + jtag_set_signals(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]); + break; + case CMD_CONFIGURE_TCK_FREQ: + usb_out_bytecount = 5; + jtag_configure_tck_delay( + OUT2BUF[cmd_id_index + 1], /* scan_in */ + OUT2BUF[cmd_id_index + 2], /* scan_out */ + OUT2BUF[cmd_id_index + 3], /* scan_io */ + OUT2BUF[cmd_id_index + 4], /* clock_tck */ + OUT2BUF[cmd_id_index + 5]); /* clock_tms */ + break; + case CMD_SET_LEDS: + usb_out_bytecount = 1; + execute_set_led_command(); + break; + case CMD_TEST: + usb_out_bytecount = 1; + /* Do nothing... This command is only used to test if the device is ready + * to accept new commands */ + break; + default: + /* Should never be reached */ + usb_out_bytecount = 0; + break; + } + + /* Update EP2 Bulk-IN data byte count */ + payload_index_in += usb_in_bytecount; + + /* Determine if this was the last command */ + if ((cmd_id_index + usb_out_bytecount + 1) >= OUT2BC) + return true; + else { + /* Not the last command, update cmd_id_index */ + cmd_id_index += (usb_out_bytecount + 1); + return false; + } } /** @@ -211,35 +206,36 @@ bool execute_command(void) */ void command_loop(void) { - bool last_command; - - while (1) { - cmd_id_index = 0; - payload_index_in = 0; - - /* Wait until host sends EP2 Bulk-OUT packet */ - while (!EP2_out); - EP2_out = 0; - - /* Turn on COM LED to indicate command execution */ - SET_COM_LED(); - - /* Execute the commands */ - last_command = false; - while (last_command == false) { - last_command = execute_command(); - } - - CLEAR_COM_LED(); - - /* Send back EP2 Bulk-IN packet if required */ - if (payload_index_in > 0) { - IN2BC = payload_index_in; - while (!EP2_in); - EP2_in = 0; - } - - /* Re-arm EP2-OUT after command execution */ - OUT2BC = 0; - } + bool last_command; + + while (1) { + cmd_id_index = 0; + payload_index_in = 0; + + /* Wait until host sends EP2 Bulk-OUT packet */ + while (!EP2_out) + ; + EP2_out = 0; + + /* Turn on COM LED to indicate command execution */ + SET_COM_LED(); + + /* Execute the commands */ + last_command = false; + while (last_command == false) + last_command = execute_command(); + + CLEAR_COM_LED(); + + /* Send back EP2 Bulk-IN packet if required */ + if (payload_index_in > 0) { + IN2BC = payload_index_in; + while (!EP2_in) + ; + EP2_in = 0; + } + + /* Re-arm EP2-OUT after command execution */ + OUT2BC = 0; + } } diff --git a/src/jtag/drivers/OpenULINK/src/usb.c b/src/jtag/drivers/OpenULINK/src/usb.c index 7a84d2a5..dd791f1b 100644 --- a/src/jtag/drivers/OpenULINK/src/usb.c +++ b/src/jtag/drivers/OpenULINK/src/usb.c @@ -33,14 +33,14 @@ /* Also update external declarations in "include/usb.h" if making changes to * these variables! */ -volatile bool EP2_out = 0; -volatile bool EP2_in = 0; +volatile bool EP2_out; +volatile bool EP2_in; volatile __xdata __at 0x7FE8 struct setup_data setup_data; /* Define number of endpoints (except Control Endpoint 0) in a central place. * Be sure to include the neccessary endpoint descriptors! */ -#define NUM_ENDPOINTS 2 +#define NUM_ENDPOINTS 2 /* * Normally, we would initialize the descriptor structures in C99 style: @@ -57,145 +57,185 @@ volatile __xdata __at 0x7FE8 struct setup_data setup_data; */ __code struct usb_device_descriptor device_descriptor = { - /* .bLength = */ sizeof(struct usb_device_descriptor), - /* .bDescriptorType = */ DESCRIPTOR_TYPE_DEVICE, - /* .bcdUSB = */ 0x0110, /* BCD: 01.00 (Version 1.0 USB spec) */ - /* .bDeviceClass = */ 0xFF, /* 0xFF = vendor-specific */ - /* .bDeviceSubClass = */ 0xFF, - /* .bDeviceProtocol = */ 0xFF, - /* .bMaxPacketSize0 = */ 64, - /* .idVendor = */ 0xC251, - /* .idProduct = */ 0x2710, - /* .bcdDevice = */ 0x0100, - /* .iManufacturer = */ 1, - /* .iProduct = */ 2, - /* .iSerialNumber = */ 3, - /* .bNumConfigurations = */ 1 + /* .bLength = */ sizeof(struct usb_device_descriptor), + /* .bDescriptorType = */ DESCRIPTOR_TYPE_DEVICE, + /* .bcdUSB = */ 0x0110, /* BCD: 01.00 (Version 1.0 USB spec) */ + /* .bDeviceClass = */ 0xFF, /* 0xFF = vendor-specific */ + /* .bDeviceSubClass = */ 0xFF, + /* .bDeviceProtocol = */ 0xFF, + /* .bMaxPacketSize0 = */ 64, + /* .idVendor = */ 0xC251, + /* .idProduct = */ 0x2710, + /* .bcdDevice = */ 0x0100, + /* .iManufacturer = */ 1, + /* .iProduct = */ 2, + /* .iSerialNumber = */ 3, + /* .bNumConfigurations = */ 1 }; /* WARNING: ALL config, interface and endpoint descriptors MUST be adjacent! */ __code struct usb_config_descriptor config_descriptor = { - /* .bLength = */ sizeof(struct usb_config_descriptor), - /* .bDescriptorType = */ DESCRIPTOR_TYPE_CONFIGURATION, - /* .wTotalLength = */ sizeof(struct usb_config_descriptor) + - sizeof(struct usb_interface_descriptor) + - (NUM_ENDPOINTS * - sizeof(struct usb_endpoint_descriptor)), - /* .bNumInterfaces = */ 1, - /* .bConfigurationValue = */ 1, - /* .iConfiguration = */ 4, /* String describing this configuration */ - /* .bmAttributes = */ 0x80, /* Only MSB set according to USB spec */ - /* .MaxPower = */ 50 /* 100 mA */ + /* .bLength = */ sizeof(struct usb_config_descriptor), + /* .bDescriptorType = */ DESCRIPTOR_TYPE_CONFIGURATION, + /* .wTotalLength = */ sizeof(struct usb_config_descriptor) + + sizeof(struct usb_interface_descriptor) + + (NUM_ENDPOINTS * + sizeof(struct usb_endpoint_descriptor)), + /* .bNumInterfaces = */ 1, + /* .bConfigurationValue = */ 1, + /* .iConfiguration = */ 4, /* String describing this configuration */ + /* .bmAttributes = */ 0x80, /* Only MSB set according to USB spec */ + /* .MaxPower = */ 50 /* 100 mA */ }; __code struct usb_interface_descriptor interface_descriptor00 = { - /* .bLength = */ sizeof(struct usb_interface_descriptor), - /* .bDescriptorType = */ DESCRIPTOR_TYPE_INTERFACE, - /* .bInterfaceNumber = */ 0, - /* .bAlternateSetting = */ 0, - /* .bNumEndpoints = */ NUM_ENDPOINTS, - /* .bInterfaceClass = */ 0xFF, - /* .bInterfaceSubclass = */ 0xFF, - /* .bInterfaceProtocol = */ 0xFF, - /* .iInterface = */ 0 + /* .bLength = */ sizeof(struct usb_interface_descriptor), + /* .bDescriptorType = */ DESCRIPTOR_TYPE_INTERFACE, + /* .bInterfaceNumber = */ 0, + /* .bAlternateSetting = */ 0, + /* .bNumEndpoints = */ NUM_ENDPOINTS, + /* .bInterfaceClass = */ 0xFF, + /* .bInterfaceSubclass = */ 0xFF, + /* .bInterfaceProtocol = */ 0xFF, + /* .iInterface = */ 0 }; __code struct usb_endpoint_descriptor Bulk_EP2_IN_Endpoint_Descriptor = { - /* .bLength = */ sizeof(struct usb_endpoint_descriptor), - /* .bDescriptorType = */ 0x05, - /* .bEndpointAddress = */ 2 | USB_DIR_IN, - /* .bmAttributes = */ 0x02, - /* .wMaxPacketSize = */ 64, - /* .bInterval = */ 0 + /* .bLength = */ sizeof(struct usb_endpoint_descriptor), + /* .bDescriptorType = */ 0x05, + /* .bEndpointAddress = */ 2 | USB_DIR_IN, + /* .bmAttributes = */ 0x02, + /* .wMaxPacketSize = */ 64, + /* .bInterval = */ 0 }; __code struct usb_endpoint_descriptor Bulk_EP2_OUT_Endpoint_Descriptor = { - /* .bLength = */ sizeof(struct usb_endpoint_descriptor), - /* .bDescriptorType = */ 0x05, - /* .bEndpointAddress = */ 2 | USB_DIR_OUT, - /* .bmAttributes = */ 0x02, - /* .wMaxPacketSize = */ 64, - /* .bInterval = */ 0 + /* .bLength = */ sizeof(struct usb_endpoint_descriptor), + /* .bDescriptorType = */ 0x05, + /* .bEndpointAddress = */ 2 | USB_DIR_OUT, + /* .bmAttributes = */ 0x02, + /* .wMaxPacketSize = */ 64, + /* .bInterval = */ 0 }; __code struct usb_language_descriptor language_descriptor = { - /* .bLength = */ 4, - /* .bDescriptorType = */ DESCRIPTOR_TYPE_STRING, - /* .wLANGID = */ {0x0409 /* US English */} + /* .bLength = */ 4, + /* .bDescriptorType = */ DESCRIPTOR_TYPE_STRING, + /* .wLANGID = */ {0x0409 /* US English */} }; __code struct usb_string_descriptor strManufacturer = - STR_DESCR(9,'O','p','e','n','U','L','I','N','K'); + STR_DESCR(9, 'O', 'p', 'e', 'n', 'U', 'L', 'I', 'N', 'K'); -__code struct usb_string_descriptor strProduct = - STR_DESCR(9,'O','p','e','n','U','L','I','N','K'); +__code struct usb_string_descriptor strProduct = + STR_DESCR(9, 'O', 'p', 'e', 'n', 'U', 'L', 'I', 'N', 'K'); __code struct usb_string_descriptor strSerialNumber = - STR_DESCR(6, '0','0','0','0','0','1'); + STR_DESCR(6, '0', '0', '0', '0', '0', '1'); __code struct usb_string_descriptor strConfigDescr = - STR_DESCR(12, 'J','T','A','G',' ','A','d','a','p','t','e','r'); + STR_DESCR(12, 'J', 'T', 'A', 'G', ' ', 'A', 'd', 'a', 'p', 't', 'e', 'r'); /* Table containing pointers to string descriptors */ -__code struct usb_string_descriptor* __code en_string_descriptors[4] = { - &strManufacturer, - &strProduct, - &strSerialNumber, - &strConfigDescr +__code struct usb_string_descriptor *__code en_string_descriptors[4] = { + &strManufacturer, + &strProduct, + &strSerialNumber, + &strConfigDescr }; void sudav_isr(void) __interrupt SUDAV_ISR { - CLEAR_IRQ(); + CLEAR_IRQ(); - usb_handle_setup_data(); + usb_handle_setup_data(); - USBIRQ = SUDAVIR; - EP0CS |= HSNAK; + USBIRQ = SUDAVIR; + EP0CS |= HSNAK; } -void sof_isr(void) __interrupt SOF_ISR { } -void sutok_isr(void) __interrupt SUTOK_ISR { } -void suspend_isr(void) __interrupt SUSPEND_ISR { } -void usbreset_isr(void) __interrupt USBRESET_ISR { } -void ibn_isr(void) __interrupt IBN_ISR { } +void sof_isr(void) __interrupt SOF_ISR +{ +} +void sutok_isr(void) __interrupt SUTOK_ISR +{ +} +void suspend_isr(void) __interrupt SUSPEND_ISR +{ +} +void usbreset_isr(void) __interrupt USBRESET_ISR +{ +} +void ibn_isr(void) __interrupt IBN_ISR +{ +} -void ep0in_isr(void) __interrupt EP0IN_ISR { } -void ep0out_isr(void) __interrupt EP0OUT_ISR { } -void ep1in_isr(void) __interrupt EP1IN_ISR { } -void ep1out_isr(void) __interrupt EP1OUT_ISR { } +void ep0in_isr(void) __interrupt EP0IN_ISR +{ +} +void ep0out_isr(void) __interrupt EP0OUT_ISR +{ +} +void ep1in_isr(void) __interrupt EP1IN_ISR +{ +} +void ep1out_isr(void) __interrupt EP1OUT_ISR +{ +} /** * EP2 IN: called after the transfer from uC->Host has finished: we sent data */ -void ep2in_isr(void) __interrupt EP2IN_ISR { - EP2_in = 1; +void ep2in_isr(void) __interrupt EP2IN_ISR +{ + EP2_in = 1; - CLEAR_IRQ(); - IN07IRQ = IN2IR; // Clear OUT2 IRQ + CLEAR_IRQ(); + IN07IRQ = IN2IR;/* Clear OUT2 IRQ */ } /** * EP2 OUT: called after the transfer from Host->uC has finished: we got data */ -void ep2out_isr(void) __interrupt EP2OUT_ISR { - EP2_out = 1; +void ep2out_isr(void) __interrupt EP2OUT_ISR +{ + EP2_out = 1; - CLEAR_IRQ(); - OUT07IRQ = OUT2IR; // Clear OUT2 IRQ + CLEAR_IRQ(); + OUT07IRQ = OUT2IR; /* Clear OUT2 IRQ */ } -void ep3in_isr(void) __interrupt EP3IN_ISR { } -void ep3out_isr(void) __interrupt EP3OUT_ISR { } -void ep4in_isr(void) __interrupt EP4IN_ISR { } -void ep4out_isr(void) __interrupt EP4OUT_ISR { } -void ep5in_isr(void) __interrupt EP5IN_ISR { } -void ep5out_isr(void) __interrupt EP5OUT_ISR { } -void ep6in_isr(void) __interrupt EP6IN_ISR { } -void ep6out_isr(void) __interrupt EP6OUT_ISR { } -void ep7in_isr(void) __interrupt EP7IN_ISR { } -void ep7out_isr(void) __interrupt EP7OUT_ISR { } +void ep3in_isr(void) __interrupt EP3IN_ISR +{ +} +void ep3out_isr(void) __interrupt EP3OUT_ISR +{ +} +void ep4in_isr(void) __interrupt EP4IN_ISR +{ +} +void ep4out_isr(void) __interrupt EP4OUT_ISR +{ +} +void ep5in_isr(void) __interrupt EP5IN_ISR +{ +} +void ep5out_isr(void) __interrupt EP5OUT_ISR +{ +} +void ep6in_isr(void) __interrupt EP6IN_ISR +{ +} +void ep6out_isr(void) __interrupt EP6OUT_ISR +{ +} +void ep7in_isr(void) __interrupt EP7IN_ISR +{ +} +void ep7out_isr(void) __interrupt EP7OUT_ISR +{ +} /** * Return the control/status register for an endpoint @@ -205,60 +245,60 @@ void ep7out_isr(void) __interrupt EP7OUT_ISR { } * specified in \a ep * @return on failure: NULL */ -__xdata uint8_t* usb_get_endpoint_cs_reg(uint8_t ep) +__xdata uint8_t *usb_get_endpoint_cs_reg(uint8_t ep) { - /* Mask direction bit */ - uint8_t ep_num = ep & 0x7F; - - switch (ep_num) { - case 0: - return &EP0CS; - break; - case 1: - return ep & 0x80 ? &IN1CS : &OUT1CS; - break; - case 2: - return ep & 0x80 ? &IN2CS : &OUT2CS; - break; - case 3: - return ep & 0x80 ? &IN3CS : &OUT3CS; - break; - case 4: - return ep & 0x80 ? &IN4CS : &OUT4CS; - break; - case 5: - return ep & 0x80 ? &IN5CS : &OUT5CS; - break; - case 6: - return ep & 0x80 ? &IN6CS : &OUT6CS; - break; - case 7: - return ep & 0x80 ? &IN7CS : &OUT7CS; - break; - } - - return NULL; + /* Mask direction bit */ + uint8_t ep_num = ep & 0x7F; + + switch (ep_num) { + case 0: + return &EP0CS; + break; + case 1: + return ep & 0x80 ? &IN1CS : &OUT1CS; + break; + case 2: + return ep & 0x80 ? &IN2CS : &OUT2CS; + break; + case 3: + return ep & 0x80 ? &IN3CS : &OUT3CS; + break; + case 4: + return ep & 0x80 ? &IN4CS : &OUT4CS; + break; + case 5: + return ep & 0x80 ? &IN5CS : &OUT5CS; + break; + case 6: + return ep & 0x80 ? &IN6CS : &OUT6CS; + break; + case 7: + return ep & 0x80 ? &IN7CS : &OUT7CS; + break; + } + + return NULL; } void usb_reset_data_toggle(uint8_t ep) { - /* TOGCTL register: - +----+-----+-----+------+-----+-------+-------+-------+ - | Q | S | R | IO | 0 | EP2 | EP1 | EP0 | - +----+-----+-----+------+-----+-------+-------+-------+ - - To reset data toggle bits, we have to write the endpoint direction (IN/OUT) - to the IO bit and the endpoint number to the EP2..EP0 bits. Then, in a - separate write cycle, the R bit needs to be set. - */ - uint8_t togctl_value = (ep & 0x80 >> 3) | (ep & 0x7); - - /* First step: Write EP number and direction bit */ - TOGCTL = togctl_value; - - /* Second step: Set R bit */ - togctl_value |= TOG_R; - TOGCTL = togctl_value; + /* TOGCTL register: + +----+-----+-----+------+-----+-------+-------+-------+ + | Q | S | R | IO | 0 | EP2 | EP1 | EP0 | + +----+-----+-----+------+-----+-------+-------+-------+ + + To reset data toggle bits, we have to write the endpoint direction (IN/OUT) + to the IO bit and the endpoint number to the EP2..EP0 bits. Then, in a + separate write cycle, the R bit needs to be set. + */ + uint8_t togctl_value = (ep & 0x80 >> 3) | (ep & 0x7); + + /* First step: Write EP number and direction bit */ + TOGCTL = togctl_value; + + /* Second step: Set R bit */ + togctl_value |= TOG_R; + TOGCTL = togctl_value; } /** @@ -269,49 +309,47 @@ void usb_reset_data_toggle(uint8_t ep) */ bool usb_handle_get_status(void) { - uint8_t *ep_cs; - - switch (setup_data.bmRequestType) { - case GS_DEVICE: - /* Two byte response: Byte 0, Bit 0 = self-powered, Bit 1 = remote wakeup. - * Byte 1: reserved, reset to zero */ - IN0BUF[0] = 0; - IN0BUF[1] = 0; - - /* Send response */ - IN0BC = 2; - break; - case GS_INTERFACE: - /* Always return two zero bytes according to USB 1.1 spec, p. 191 */ - IN0BUF[0] = 0; - IN0BUF[1] = 0; - - /* Send response */ - IN0BC = 2; - break; - case GS_ENDPOINT: - /* Get stall bit for endpoint specified in low byte of wIndex */ - ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex & 0xff); - - if (*ep_cs & EPSTALL) { - IN0BUF[0] = 0x01; - } - else { - IN0BUF[0] = 0x00; - } - - /* Second byte sent has to be always zero */ - IN0BUF[1] = 0; - - /* Send response */ - IN0BC = 2; - break; - default: - return false; - break; - } - - return true; + uint8_t *ep_cs; + + switch (setup_data.bmRequestType) { + case GS_DEVICE: + /* Two byte response: Byte 0, Bit 0 = self-powered, Bit 1 = remote wakeup. + * Byte 1: reserved, reset to zero */ + IN0BUF[0] = 0; + IN0BUF[1] = 0; + + /* Send response */ + IN0BC = 2; + break; + case GS_INTERFACE: + /* Always return two zero bytes according to USB 1.1 spec, p. 191 */ + IN0BUF[0] = 0; + IN0BUF[1] = 0; + + /* Send response */ + IN0BC = 2; + break; + case GS_ENDPOINT: + /* Get stall bit for endpoint specified in low byte of wIndex */ + ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex & 0xff); + + if (*ep_cs & EPSTALL) + IN0BUF[0] = 0x01; + else + IN0BUF[0] = 0x00; + + /* Second byte sent has to be always zero */ + IN0BUF[1] = 0; + + /* Send response */ + IN0BC = 2; + break; + default: + return false; + break; + } + + return true; } /** @@ -322,32 +360,30 @@ bool usb_handle_get_status(void) */ bool usb_handle_clear_feature(void) { - __xdata uint8_t *ep_cs; - - switch (setup_data.bmRequestType) { - case CF_DEVICE: - /* Clear remote wakeup not supported: stall EP0 */ - STALL_EP0(); - break; - case CF_ENDPOINT: - if (setup_data.wValue == 0) { - /* Unstall the endpoint specified in wIndex */ - ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex); - if (!ep_cs) { - return false; - } - *ep_cs &= ~EPSTALL; - } - else { - /* Unsupported feature, stall EP0 */ - STALL_EP0(); - } - break; - default: - /* Vendor commands... */ - } - - return true; + __xdata uint8_t *ep_cs; + + switch (setup_data.bmRequestType) { + case CF_DEVICE: + /* Clear remote wakeup not supported: stall EP0 */ + STALL_EP0(); + break; + case CF_ENDPOINT: + if (setup_data.wValue == 0) { + /* Unstall the endpoint specified in wIndex */ + ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex); + if (!ep_cs) + return false; + *ep_cs &= ~EPSTALL; + } else { + /* Unsupported feature, stall EP0 */ + STALL_EP0(); + } + break; + default: + /* Vendor commands... */ + } + + return true; } /** @@ -358,34 +394,31 @@ bool usb_handle_clear_feature(void) */ bool usb_handle_set_feature(void) { - __xdata uint8_t *ep_cs; - - switch (setup_data.bmRequestType) { - case SF_DEVICE: - if (setup_data.wValue == 2) { - return true; - } - break; - case SF_ENDPOINT: - if (setup_data.wValue == 0) { - /* Stall the endpoint specified in wIndex */ - ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex); - if (!ep_cs) { - return false; - } - *ep_cs |= EPSTALL; - } - else { - /* Unsupported endpoint feature */ - return false; - } - break; - default: - /* Vendor commands... */ - break; - } - - return true; + __xdata uint8_t *ep_cs; + + switch (setup_data.bmRequestType) { + case SF_DEVICE: + if (setup_data.wValue == 2) + return true; + break; + case SF_ENDPOINT: + if (setup_data.wValue == 0) { + /* Stall the endpoint specified in wIndex */ + ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex); + if (!ep_cs) + return false; + *ep_cs |= EPSTALL; + } else { + /* Unsupported endpoint feature */ + return false; + } + break; + default: + /* Vendor commands... */ + break; + } + + return true; } /** @@ -396,43 +429,40 @@ bool usb_handle_set_feature(void) */ bool usb_handle_get_descriptor(void) { - __xdata uint8_t descriptor_type; - __xdata uint8_t descriptor_index; - - descriptor_type = (setup_data.wValue & 0xff00) >> 8; - descriptor_index = setup_data.wValue & 0x00ff; - - switch (descriptor_type) { - case DESCRIPTOR_TYPE_DEVICE: - SUDPTRH = HI8(&device_descriptor); - SUDPTRL = LO8(&device_descriptor); - break; - case DESCRIPTOR_TYPE_CONFIGURATION: - SUDPTRH = HI8(&config_descriptor); - SUDPTRL = LO8(&config_descriptor); - break; - case DESCRIPTOR_TYPE_STRING: - if (setup_data.wIndex == 0) { - /* Supply language descriptor */ - SUDPTRH = HI8(&language_descriptor); - SUDPTRL = LO8(&language_descriptor); - } - else if (setup_data.wIndex == 0x0409 /* US English */) { - /* Supply string descriptor */ - SUDPTRH = HI8(en_string_descriptors[descriptor_index - 1]); - SUDPTRL = LO8(en_string_descriptors[descriptor_index - 1]); - } - else { - return false; - } - break; - default: - /* Unsupported descriptor type */ - return false; - break; - } - - return true; + __xdata uint8_t descriptor_type; + __xdata uint8_t descriptor_index; + + descriptor_type = (setup_data.wValue & 0xff00) >> 8; + descriptor_index = setup_data.wValue & 0x00ff; + + switch (descriptor_type) { + case DESCRIPTOR_TYPE_DEVICE: + SUDPTRH = HI8(&device_descriptor); + SUDPTRL = LO8(&device_descriptor); + break; + case DESCRIPTOR_TYPE_CONFIGURATION: + SUDPTRH = HI8(&config_descriptor); + SUDPTRL = LO8(&config_descriptor); + break; + case DESCRIPTOR_TYPE_STRING: + if (setup_data.wIndex == 0) { + /* Supply language descriptor */ + SUDPTRH = HI8(&language_descriptor); + SUDPTRL = LO8(&language_descriptor); + } else if (setup_data.wIndex == 0x0409 /* US English */) { + /* Supply string descriptor */ + SUDPTRH = HI8(en_string_descriptors[descriptor_index - 1]); + SUDPTRL = LO8(en_string_descriptors[descriptor_index - 1]); + } else + return false; + break; + default: + /* Unsupported descriptor type */ + return false; + break; + } + + return true; } /** @@ -440,16 +470,16 @@ bool usb_handle_get_descriptor(void) */ void usb_handle_set_interface(void) { - /* Reset Data Toggle */ - usb_reset_data_toggle(USB_DIR_IN | 2); - usb_reset_data_toggle(USB_DIR_OUT | 2); - - /* Unstall & clear busy flag of all valid IN endpoints */ - IN2CS = 0 | EPBSY; - - /* Unstall all valid OUT endpoints, reset bytecounts */ - OUT2CS = 0; - OUT2BC = 0; + /* Reset Data Toggle */ + usb_reset_data_toggle(USB_DIR_IN | 2); + usb_reset_data_toggle(USB_DIR_OUT | 2); + + /* Unstall & clear busy flag of all valid IN endpoints */ + IN2CS = 0 | EPBSY; + + /* Unstall all valid OUT endpoints, reset bytecounts */ + OUT2CS = 0; + OUT2BC = 0; } /** @@ -457,95 +487,92 @@ void usb_handle_set_interface(void) */ void usb_handle_setup_data(void) { - switch (setup_data.bRequest) { - case GET_STATUS: - if (!usb_handle_get_status()) { - STALL_EP0(); - } - break; - case CLEAR_FEATURE: - if (!usb_handle_clear_feature()) { - STALL_EP0(); - } - break; - case 2: case 4: - /* Reserved values */ - STALL_EP0(); - break; - case SET_FEATURE: - if (!usb_handle_set_feature()) { - STALL_EP0(); - } - break; - case SET_ADDRESS: - /* Handled by USB core */ - break; - case SET_DESCRIPTOR: - /* Set Descriptor not supported. */ - STALL_EP0(); - break; - case GET_DESCRIPTOR: - if (!usb_handle_get_descriptor()) { - STALL_EP0(); - } - break; - case GET_CONFIGURATION: - /* OpenULINK has only one configuration, return its index */ - IN0BUF[0] = config_descriptor.bConfigurationValue; - IN0BC = 1; - break; - case SET_CONFIGURATION: - /* OpenULINK has only one configuration -> nothing to do */ - break; - case GET_INTERFACE: - /* OpenULINK only has one interface, return its number */ - IN0BUF[0] = interface_descriptor00.bInterfaceNumber; - IN0BC = 1; - break; - case SET_INTERFACE: - usb_handle_set_interface(); - break; - case SYNCH_FRAME: - /* Isochronous endpoints not used -> nothing to do */ - break; - default: - /* Any other requests: do nothing */ - break; - } + switch (setup_data.bRequest) { + case GET_STATUS: + if (!usb_handle_get_status()) + STALL_EP0(); + break; + case CLEAR_FEATURE: + if (!usb_handle_clear_feature()) + STALL_EP0(); + break; + case 2: case 4: + /* Reserved values */ + STALL_EP0(); + break; + case SET_FEATURE: + if (!usb_handle_set_feature()) + STALL_EP0(); + break; + case SET_ADDRESS: + /* Handled by USB core */ + break; + case SET_DESCRIPTOR: + /* Set Descriptor not supported. */ + STALL_EP0(); + break; + case GET_DESCRIPTOR: + if (!usb_handle_get_descriptor()) + STALL_EP0(); + break; + case GET_CONFIGURATION: + /* OpenULINK has only one configuration, return its index */ + IN0BUF[0] = config_descriptor.bConfigurationValue; + IN0BC = 1; + break; + case SET_CONFIGURATION: + /* OpenULINK has only one configuration -> nothing to do */ + break; + case GET_INTERFACE: + /* OpenULINK only has one interface, return its number */ + IN0BUF[0] = interface_descriptor00.bInterfaceNumber; + IN0BC = 1; + break; + case SET_INTERFACE: + usb_handle_set_interface(); + break; + case SYNCH_FRAME: + /* Isochronous endpoints not used -> nothing to do */ + break; + default: + /* Any other requests: do nothing */ + break; + } } /** * USB initialization. Configures USB interrupts, endpoints and performs * ReNumeration. */ -void usb_init(void) { - /* Mark endpoint 2 IN & OUT as valid */ - IN07VAL = IN2VAL; - OUT07VAL = OUT2VAL; - - /* Make sure no isochronous endpoints are marked valid */ - INISOVAL = 0; - OUTISOVAL = 0; - - /* Disable isochronous endpoints. This makes the isochronous data buffers - * available as 8051 XDATA memory at address 0x2000 - 0x27FF */ - ISOCTL = ISODISAB; - - /* Enable USB Autovectoring */ - USBBAV |= AVEN; - - /* Enable SUDAV interrupt */ - USBIEN |= SUDAVIE; - - /* Enable EP2 OUT & IN interrupts */ - OUT07IEN = OUT2IEN; - IN07IEN = IN2IEN; - - /* Enable USB interrupt (EIE register) */ - EUSB = 1; - - /* Perform ReNumeration */ - USBCS = DISCON | RENUM; - delay_ms(200); - USBCS = DISCOE | RENUM; +void usb_init(void) +{ + /* Mark endpoint 2 IN & OUT as valid */ + IN07VAL = IN2VAL; + OUT07VAL = OUT2VAL; + + /* Make sure no isochronous endpoints are marked valid */ + INISOVAL = 0; + OUTISOVAL = 0; + + /* Disable isochronous endpoints. This makes the isochronous data buffers + * available as 8051 XDATA memory at address 0x2000 - 0x27FF */ + ISOCTL = ISODISAB; + + /* Enable USB Autovectoring */ + USBBAV |= AVEN; + + /* Enable SUDAV interrupt */ + USBIEN |= SUDAVIE; + + /* Enable EP2 OUT & IN interrupts */ + OUT07IEN = OUT2IEN; + IN07IEN = IN2IEN; + + /* Enable USB interrupt (EIE register) */ + EUSB = 1; + + /* Perform ReNumeration */ + USBCS = DISCON | RENUM; + delay_ms(200); + USBCS = DISCOE | RENUM; } |