From de0130a0aad83c1ef692ee4d68ab996a8668424d Mon Sep 17 00:00:00 2001 From: Spencer Oliver Date: Thu, 2 Feb 2012 15:08:51 +0000 Subject: [PATCH] build: cleanup src/jtag/drivers directory Change-Id: I99c08ec0132d5a15250050e718310f1ddd9fe546 Signed-off-by: Spencer Oliver Reviewed-on: http://openocd.zylin.com/425 Tested-by: jenkins --- src/jtag/drivers/OpenULINK/include/delay.h | 2 +- src/jtag/drivers/OpenULINK/include/io.h | 47 +- src/jtag/drivers/OpenULINK/include/jtag.h | 4 +- .../drivers/OpenULINK/include/reg_ezusb.h | 306 +- src/jtag/drivers/OpenULINK/include/usb.h | 154 +- src/jtag/drivers/OpenULINK/src/delay.c | 26 +- src/jtag/drivers/OpenULINK/src/jtag.c | 1057 +++--- src/jtag/drivers/OpenULINK/src/main.c | 56 +- src/jtag/drivers/OpenULINK/src/protocol.c | 322 +- src/jtag/drivers/OpenULINK/src/usb.c | 769 ++--- src/jtag/drivers/amt_jtagaccel.c | 188 +- src/jtag/drivers/arm-jtag-ew.c | 297 +- src/jtag/drivers/at91rm9200.c | 41 +- src/jtag/drivers/bitbang.c | 94 +- src/jtag/drivers/bitbang.h | 1 + src/jtag/drivers/bitq.c | 87 +- src/jtag/drivers/bitq.h | 3 +- src/jtag/drivers/buspirate.c | 25 +- src/jtag/drivers/driver.c | 212 +- src/jtag/drivers/dummy.c | 33 +- src/jtag/drivers/ep93xx.c | 9 +- src/jtag/drivers/ft2232.c | 1979 +++++------- src/jtag/drivers/ftd2xx_common.h | 3 +- src/jtag/drivers/gw16012.c | 117 +- src/jtag/drivers/jlink.c | 335 +- src/jtag/drivers/libusb0_common.c | 12 +- src/jtag/drivers/libusb0_common.h | 12 +- src/jtag/drivers/libusb1_common.c | 10 +- src/jtag/drivers/libusb1_common.h | 14 +- src/jtag/drivers/minidriver_imp.h | 22 +- src/jtag/drivers/parport.c | 89 +- src/jtag/drivers/presto.c | 298 +- src/jtag/drivers/remote_bitbang.c | 71 +- src/jtag/drivers/rlink.c | 1358 ++++---- src/jtag/drivers/rlink_dtc_cmd.h | 21 +- src/jtag/drivers/rlink_speed_table.c | 2 +- src/jtag/drivers/stlink_usb.h | 24 +- src/jtag/drivers/ulink.c | 2841 ++++++++--------- src/jtag/drivers/usb_blaster.c | 150 +- src/jtag/drivers/usb_common.c | 11 +- src/jtag/drivers/usb_common.h | 2 +- src/jtag/drivers/usbprog.c | 296 +- .../drivers/versaloon/usbtoxxx/usbtogpio.c | 37 +- .../drivers/versaloon/usbtoxxx/usbtojtagraw.c | 28 +- .../drivers/versaloon/usbtoxxx/usbtopwr.c | 14 +- .../drivers/versaloon/usbtoxxx/usbtoswd.c | 73 +- .../drivers/versaloon/usbtoxxx/usbtoxxx.c | 444 +-- .../drivers/versaloon/usbtoxxx/usbtoxxx.h | 178 +- .../versaloon/usbtoxxx/usbtoxxx_internal.h | 232 +- src/jtag/drivers/versaloon/versaloon.c | 215 +- src/jtag/drivers/versaloon/versaloon.h | 77 +- .../drivers/versaloon/versaloon_include.h | 39 +- .../drivers/versaloon/versaloon_internal.h | 34 +- src/jtag/drivers/vsllink.c | 252 +- src/jtag/minidriver/minidriver_imp.h | 12 +- src/jtag/minidummy/jtag_minidriver.h | 10 +- src/jtag/minidummy/minidummy.c | 42 +- src/jtag/stlink/stlink_interface.c | 1 + src/jtag/stlink/stlink_interface.h | 1 + src/jtag/stlink/stlink_layout.h | 1 + src/jtag/stlink/stlink_tcl.h | 1 + src/jtag/stlink/stlink_transport.h | 1 + src/jtag/zy1000/jtag_minidriver.h | 139 +- src/jtag/zy1000/zy1000.c | 667 ++-- 64 files changed, 6320 insertions(+), 7578 deletions(-) diff --git a/src/jtag/drivers/OpenULINK/include/delay.h b/src/jtag/drivers/OpenULINK/include/delay.h index 5c36b1d9ee..c887651acc 100644 --- a/src/jtag/drivers/OpenULINK/include/delay.h +++ b/src/jtag/drivers/OpenULINK/include/delay.h @@ -23,7 +23,7 @@ #include -#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 f7119b9a5c..2dc692acf2 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 ca8667162a..97ec598423 100644 --- a/src/jtag/drivers/OpenULINK/include/jtag.h +++ b/src/jtag/drivers/OpenULINK/include/jtag.h @@ -23,7 +23,7 @@ #include -#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 26ee0186d9..3d5c279bc0 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 811d6bf190..3685f7d735 100644 --- a/src/jtag/drivers/OpenULINK/include/usb.h +++ b/src/jtag/drivers/OpenULINK/include/usb.h @@ -26,15 +26,15 @@ #include #include -#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 086633acee..d2007981be 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 3c2fea297c..946fdbe71c 100644 --- a/src/jtag/drivers/OpenULINK/src/jtag.c +++ b/src/jtag/drivers/OpenULINK/src/jtag.c @@ -27,19 +27,19 @@ #include /** 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 067393bda1..5beabd1841 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 ae63604f5b..1034c04f08 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 7a84d2a506..dd791f1b1c 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; } diff --git a/src/jtag/drivers/amt_jtagaccel.c b/src/jtag/drivers/amt_jtagaccel.c index 34a4ae784d..d89e5ffa91 100644 --- a/src/jtag/drivers/amt_jtagaccel.c +++ b/src/jtag/drivers/amt_jtagaccel.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -27,7 +28,7 @@ #include #include #include -#else /* not PARPORT_USE_PPDEV */ +#else /* not PARPORT_USE_PPDEV */ #ifndef _WIN32 #include #endif @@ -53,11 +54,11 @@ static uint16_t amt_jtagaccel_port; /* interface variables */ -static uint8_t aw_control_rst = 0x00; +static uint8_t aw_control_rst; static uint8_t aw_control_fsm = 0x10; static uint8_t aw_control_baudrate = 0x20; -static int rtck_enabled = 0; +static int rtck_enabled; #if PARPORT_USE_PPDEV == 1 static int device_handle; @@ -70,7 +71,7 @@ static const int addr_mode = IEEE1284_MODE_EPP | IEEE1284_ADDR; do { \ int __retval; \ \ - __retval = ioctl(device_handle, PPSETMODE, &addr_mode); \ + __retval = ioctl(device_handle, PPSETMODE, &addr_mode); \ assert(__retval >= 0); \ __retval = write(device_handle, &val, 1); \ assert(__retval >= 0); \ @@ -79,7 +80,7 @@ static const int addr_mode = IEEE1284_MODE_EPP | IEEE1284_ADDR; do { \ int __retval; \ \ - __retval = ioctl(device_handle, PPSETMODE, &addr_mode); \ + __retval = ioctl(device_handle, PPSETMODE, &addr_mode); \ assert(__retval >= 0); \ __retval = read(device_handle, &val, 1); \ assert(__retval >= 0); \ @@ -91,7 +92,7 @@ static const int data_mode = IEEE1284_MODE_EPP | IEEE1284_DATA; do { \ int __retval; \ \ - __retval = ioctl(device_handle, PPSETMODE, &data_mode); \ + __retval = ioctl(device_handle, PPSETMODE, &data_mode); \ assert(__retval >= 0); \ __retval = write(device_handle, &val, 1); \ assert(__retval >= 0); \ @@ -100,7 +101,7 @@ static const int data_mode = IEEE1284_MODE_EPP | IEEE1284_DATA; do { \ int __retval; \ \ - __retval = ioctl(device_handle, PPSETMODE, &data_mode); \ + __retval = ioctl(device_handle, PPSETMODE, &data_mode); \ assert(__retval >= 0); \ __retval = read(device_handle, &val, 1); \ assert(__retval >= 0); \ @@ -108,12 +109,12 @@ static const int data_mode = IEEE1284_MODE_EPP | IEEE1284_DATA; #else -#define AMT_AW(val) do { outb(val, amt_jtagaccel_port + 3); } while (0) -#define AMT_AR(val) do { val = inb(amt_jtagaccel_port + 3); } while (0) -#define AMT_DW(val) do { outb(val, amt_jtagaccel_port + 4); } while (0) -#define AMT_DR(val) do { val = inb(amt_jtagaccel_port + 4); } while (0) +#define AMT_AW(val) do { outb(val, amt_jtagaccel_port + 3); } while (0) +#define AMT_AR(val) do { val = inb(amt_jtagaccel_port + 3); } while (0) +#define AMT_DW(val) do { outb(val, amt_jtagaccel_port + 4); } while (0) +#define AMT_DR(val) do { val = inb(amt_jtagaccel_port + 4); } while (0) -#endif // PARPORT_USE_PPDEV +#endif /* PARPORT_USE_PPDEV */ /* tap_move[i][j]: tap movement command to go from state i to state j * 0: Test-Logic-Reset @@ -123,18 +124,16 @@ static const int data_mode = IEEE1284_MODE_EPP | IEEE1284_DATA; * 4: Shift-IR * 5: Pause-IR */ -static uint8_t amt_jtagaccel_tap_move[6][6][2] = -{ +static uint8_t amt_jtagaccel_tap_move[6][6][2] = { /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ - {{0x1f, 0x00}, {0x0f, 0x00}, {0x05, 0x00}, {0x0a, 0x00}, {0x06, 0x00}, {0x96, 0x00}}, /* RESET */ - {{0x1f, 0x00}, {0x00, 0x00}, {0x04, 0x00}, {0x05, 0x00}, {0x06, 0x00}, {0x0b, 0x00}}, /* IDLE */ - {{0x1f, 0x00}, {0x0d, 0x00}, {0x00, 0x00}, {0x01, 0x00}, {0x8f, 0x09}, {0x8f, 0x01}}, /* DRSHIFT */ - {{0x1f, 0x00}, {0x0c, 0x00}, {0x08, 0x00}, {0x00, 0x00}, {0x8f, 0x09}, {0x8f, 0x01}}, /* DRPAUSE */ - {{0x1f, 0x00}, {0x0d, 0x00}, {0x07, 0x00}, {0x97, 0x00}, {0x00, 0x00}, {0x01, 0x00}}, /* IRSHIFT */ - {{0x1f, 0x00}, {0x0c, 0x00}, {0x07, 0x00}, {0x97, 0x00}, {0x08, 0x00}, {0x00, 0x00}}, /* IRPAUSE */ + { {0x1f, 0x00}, {0x0f, 0x00}, {0x05, 0x00}, {0x0a, 0x00}, {0x06, 0x00}, {0x96, 0x00} }, /* RESET */ + { {0x1f, 0x00}, {0x00, 0x00}, {0x04, 0x00}, {0x05, 0x00}, {0x06, 0x00}, {0x0b, 0x00} }, /* IDLE */ + { {0x1f, 0x00}, {0x0d, 0x00}, {0x00, 0x00}, {0x01, 0x00}, {0x8f, 0x09}, {0x8f, 0x01} }, /* DRSHIFT */ + { {0x1f, 0x00}, {0x0c, 0x00}, {0x08, 0x00}, {0x00, 0x00}, {0x8f, 0x09}, {0x8f, 0x01} }, /* DRPAUSE */ + { {0x1f, 0x00}, {0x0d, 0x00}, {0x07, 0x00}, {0x97, 0x00}, {0x00, 0x00}, {0x01, 0x00} }, /* IRSHIFT */ + { {0x1f, 0x00}, {0x0c, 0x00}, {0x07, 0x00}, {0x97, 0x00}, {0x08, 0x00}, {0x00, 0x00} }, /* IRPAUSE */ }; - static void amt_jtagaccel_reset(int trst, int srst) { if (trst == 1) @@ -163,8 +162,7 @@ static void amt_jtagaccel_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } @@ -179,9 +177,11 @@ static void amt_wait_scan_busy(void) while (((ar_status) & 0x80) && (timeout-- > 0)) AMT_AR(ar_status); - if (ar_status & 0x80) - { - LOG_ERROR("amt_jtagaccel timed out while waiting for end of scan, rtck was %s, last AR_STATUS: 0x%2.2x", (rtck_enabled) ? "enabled" : "disabled", ar_status); + if (ar_status & 0x80) { + LOG_ERROR( + "amt_jtagaccel timed out while waiting for end of scan, rtck was %s, last AR_STATUS: 0x%2.2x", + (rtck_enabled) ? "enabled" : "disabled", + ar_status); exit(-1); } } @@ -191,22 +191,21 @@ static void amt_jtagaccel_state_move(void) uint8_t aw_scan_tms_5; uint8_t tms_scan[2]; - tap_state_t cur_state = tap_get_state(); - tap_state_t end_state = tap_get_end_state(); + tap_state_t cur_state = tap_get_state(); + tap_state_t end_state = tap_get_end_state(); tms_scan[0] = amt_jtagaccel_tap_move[tap_move_ndx(cur_state)][tap_move_ndx(end_state)][0]; tms_scan[1] = amt_jtagaccel_tap_move[tap_move_ndx(cur_state)][tap_move_ndx(end_state)][1]; aw_scan_tms_5 = 0x40 | (tms_scan[0] & 0x1f); AMT_AW(aw_scan_tms_5); - int jtag_speed=0; + int jtag_speed = 0; int retval = jtag_get_speed(&jtag_speed); assert(retval == ERROR_OK); if (jtag_speed > 3 || rtck_enabled) amt_wait_scan_busy(); - if (tms_scan[0] & 0x80) - { + if (tms_scan[0] & 0x80) { aw_scan_tms_5 = 0x40 | (tms_scan[1] & 0x1f); AMT_AW(aw_scan_tms_5); if (jtag_speed > 3 || rtck_enabled) @@ -225,21 +224,18 @@ static void amt_jtagaccel_runtest(int num_cycles) tap_state_t saved_end_state = tap_get_end_state(); /* only do a state_move when we're not already in IDLE */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { amt_jtagaccel_end_state(TAP_IDLE); amt_jtagaccel_state_move(); } - while (num_cycles - i >= 5) - { + while (num_cycles - i >= 5) { aw_scan_tms_5 = 0x40; AMT_AW(aw_scan_tms_5); i += 5; } - if (num_cycles - i > 0) - { + if (num_cycles - i > 0) { aw_scan_tms_1to4 = 0x80 | ((num_cycles - i - 1) & 0x3) << 4; AMT_AW(aw_scan_tms_1to4); } @@ -272,8 +268,7 @@ static void amt_jtagaccel_scan(bool ir_scan, enum scan_type type, uint8_t *buffe amt_jtagaccel_end_state(saved_end_state); /* handle unaligned bits at the beginning */ - if ((scan_size - 1) % 8) - { + if ((scan_size - 1) % 8) { aw_tdi_option = 0x30 | (((scan_size - 1) % 8) - 1); AMT_AW(aw_tdi_option); @@ -282,8 +277,7 @@ static void amt_jtagaccel_scan(bool ir_scan, enum scan_type type, uint8_t *buffe if (jtag_speed_var > 3 || rtck_enabled) amt_wait_scan_busy(); - if ((type == SCAN_IN) || (type == SCAN_IO)) - { + if ((type == SCAN_IN) || (type == SCAN_IO)) { AMT_DR(dr_tdo); dr_tdo = dr_tdo >> (8 - ((scan_size - 1) % 8)); buf_set_u32(buffer, bit_count, (scan_size - 1) % 8, dr_tdo); @@ -293,15 +287,13 @@ static void amt_jtagaccel_scan(bool ir_scan, enum scan_type type, uint8_t *buffe bits_left -= (scan_size - 1) % 8; } - while (bits_left - 1 >= 8) - { + while (bits_left - 1 >= 8) { dw_tdi_scan = buf_get_u32(buffer, bit_count, 8) & 0xff; AMT_DW(dw_tdi_scan); if (jtag_speed_var > 3 || rtck_enabled) amt_wait_scan_busy(); - if ((type == SCAN_IN) || (type == SCAN_IO)) - { + if ((type == SCAN_IN) || (type == SCAN_IO)) { AMT_DR(dr_tdo); buf_set_u32(buffer, bit_count, 8, dr_tdo); } @@ -310,22 +302,22 @@ static void amt_jtagaccel_scan(bool ir_scan, enum scan_type type, uint8_t *buffe bits_left -= 8; } - tms_scan[0] = amt_jtagaccel_tap_move[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())][0]; - tms_scan[1] = amt_jtagaccel_tap_move[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())][1]; + tms_scan[0] = + amt_jtagaccel_tap_move[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())][0]; + tms_scan[1] = + amt_jtagaccel_tap_move[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())][1]; aw_tms_scan = 0x40 | (tms_scan[0] & 0x1f) | (buf_get_u32(buffer, bit_count, 1) << 5); AMT_AW(aw_tms_scan); if (jtag_speed_var > 3 || rtck_enabled) amt_wait_scan_busy(); - if ((type == SCAN_IN) || (type == SCAN_IO)) - { + if ((type == SCAN_IN) || (type == SCAN_IO)) { AMT_DR(dr_tdo); dr_tdo = dr_tdo >> 7; buf_set_u32(buffer, bit_count, 1, dr_tdo); } - if (tms_scan[0] & 0x80) - { + if (tms_scan[0] & 0x80) { aw_tms_scan = 0x40 | (tms_scan[1] & 0x1f); AMT_AW(aw_tms_scan); if (jtag_speed_var > 3 || rtck_enabled) @@ -336,7 +328,7 @@ static void amt_jtagaccel_scan(bool ir_scan, enum scan_type type, uint8_t *buffe static int amt_jtagaccel_execute_queue(void) { - struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ + struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ int scan_size; enum scan_type type; uint8_t *buffer; @@ -347,23 +339,23 @@ static int amt_jtagaccel_execute_queue(void) */ retval = ERROR_OK; - while (cmd) - { - switch (cmd->type) - { + while (cmd) { + switch (cmd->type) { case JTAG_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); + LOG_DEBUG("reset trst: %i srst %i", + cmd->cmd.reset->trst, + cmd->cmd.reset->srst); #endif if (cmd->cmd.reset->trst == 1) - { tap_set_state(TAP_RESET); - } amt_jtagaccel_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); break; case JTAG_RUNTEST: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state); + LOG_DEBUG("runtest %i cycles, end in %i", + cmd->cmd.runtest->num_cycles, + cmd->cmd.runtest->end_state); #endif amt_jtagaccel_end_state(cmd->cmd.runtest->end_state); amt_jtagaccel_runtest(cmd->cmd.runtest->num_cycles); @@ -412,16 +404,22 @@ int amt_jtagaccel_get_giveio_access(void) version.dwOSVersionInfoSize = sizeof version; if (!GetVersionEx(&version)) { - errno = EINVAL; - return -1; + errno = EINVAL; + return -1; } if (version.dwPlatformId != VER_PLATFORM_WIN32_NT) - return 0; - - h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + return 0; + + h = CreateFile("\\\\.\\giveio", + GENERIC_READ, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); if (h == INVALID_HANDLE_VALUE) { - errno = ENODEV; - return -1; + errno = ENODEV; + return -1; } CloseHandle(h); @@ -442,8 +440,7 @@ static int amt_jtagaccel_init(void) uint8_t ar_status; #if PARPORT_USE_PPDEV == 1 - if (device_handle > 0) - { + if (device_handle > 0) { LOG_ERROR("device is already opened"); return ERROR_JTAG_INIT_FAILED; } @@ -451,23 +448,21 @@ static int amt_jtagaccel_init(void) snprintf(buffer, 256, "/dev/parport%d", amt_jtagaccel_port); device_handle = open(buffer, O_RDWR); - if (device_handle < 0) - { - LOG_ERROR("cannot open device. check it exists and that user read and write rights are set"); + if (device_handle < 0) { + LOG_ERROR( + "cannot open device. check it exists and that user read and write rights are set"); return ERROR_JTAG_INIT_FAILED; } i = ioctl(device_handle, PPCLAIM); - if (i < 0) - { + if (i < 0) { LOG_ERROR("cannot claim device"); return ERROR_JTAG_INIT_FAILED; } i = IEEE1284_MODE_EPP; - i = ioctl(device_handle, PPSETMODE, & i); - if (i < 0) - { + i = ioctl(device_handle, PPSETMODE, &i); + if (i < 0) { LOG_ERROR(" cannot set compatible mode to device"); return ERROR_JTAG_INIT_FAILED; } @@ -479,23 +474,22 @@ static int amt_jtagaccel_init(void) i = ioctl(device_handle, PPWCONTROL, &control_port); #else - if (amt_jtagaccel_port == 0) - { + if (amt_jtagaccel_port == 0) { amt_jtagaccel_port = 0x378; LOG_WARNING("No parport port specified, using default '0x378' (LPT1)"); } #if PARPORT_USE_GIVEIO == 1 if (amt_jtagaccel_get_giveio_access() != 0) { -#else /* PARPORT_USE_GIVEIO */ +#else /* PARPORT_USE_GIVEIO */ if (ioperm(amt_jtagaccel_port, 5, 1) != 0) { -#endif /* PARPORT_USE_GIVEIO */ +#endif /* PARPORT_USE_GIVEIO */ LOG_ERROR("missing privileges for direct i/o"); return ERROR_JTAG_INIT_FAILED; } - /* prepare epp port */ - /* clear timeout */ + /* prepare epp port + * clear timeout */ status_port = inb(amt_jtagaccel_port + 1); outb(status_port | 0x1, amt_jtagaccel_port + 1); @@ -504,8 +498,7 @@ static int amt_jtagaccel_init(void) outb(0x04, amt_jtagaccel_port + 2); #endif - if (rtck_enabled) - { + if (rtck_enabled) { /* set RTCK enable bit */ aw_control_fsm |= 0x02; } @@ -542,17 +535,13 @@ static int amt_jtagaccel_quit(void) COMMAND_HANDLER(amt_jtagaccel_handle_parport_port_command) { - if (CMD_ARGC == 1) - { + if (CMD_ARGC == 1) { /* only if the port wasn't overwritten by cmdline */ - if (amt_jtagaccel_port == 0) - { + if (amt_jtagaccel_port == 0) { uint16_t port; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port); amt_jtagaccel_port = port; - } - else - { + } else { LOG_ERROR("The parport port was already configured!"); return ERROR_FAIL; } @@ -565,21 +554,16 @@ COMMAND_HANDLER(amt_jtagaccel_handle_parport_port_command) COMMAND_HANDLER(amt_jtagaccel_handle_rtck_command) { - if (CMD_ARGC == 0) - { - command_print(CMD_CTX, "amt_jtagaccel RTCK feature %s", (rtck_enabled) ? "enabled" : "disabled"); + if (CMD_ARGC == 0) { + command_print(CMD_CTX, + "amt_jtagaccel RTCK feature %s", + (rtck_enabled) ? "enabled" : "disabled"); return ERROR_OK; - } - else - { + } else { if (strcmp(CMD_ARGV[0], "enabled") == 0) - { rtck_enabled = 1; - } else - { rtck_enabled = 0; - } } return ERROR_OK; diff --git a/src/jtag/drivers/arm-jtag-ew.c b/src/jtag/drivers/arm-jtag-ew.c index b0900501b8..35b6b929f1 100644 --- a/src/jtag/drivers/arm-jtag-ew.c +++ b/src/jtag/drivers/arm-jtag-ew.c @@ -59,9 +59,13 @@ static void armjtagew_end_state(tap_state_t state); static void armjtagew_state_move(void); static void armjtagew_path_move(int num_states, tap_state_t *path); static void armjtagew_runtest(int num_cycles); -static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command); +static void armjtagew_scan(bool ir_scan, + enum scan_type type, + uint8_t *buffer, + int scan_size, + struct scan_command *command); static void armjtagew_reset(int trst, int srst); -//static void armjtagew_simple_command(uint8_t command); +/* static void armjtagew_simple_command(uint8_t command); */ static int armjtagew_get_status(void); /* tap buffer functions */ @@ -73,7 +77,7 @@ static void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_c /* ARM-JTAG-EW lowlevel functions */ struct armjtagew { - struct usb_dev_handle* usb_handle; + struct usb_dev_handle *usb_handle; }; static struct armjtagew *armjtagew_usb_open(void); @@ -89,10 +93,10 @@ static int armjtagew_get_version_info(void); static void armjtagew_debug_buffer(uint8_t *buffer, int length); #endif -static struct armjtagew* armjtagew_handle; +static struct armjtagew *armjtagew_handle; -/***************************************************************************/ -/* External interface implementation */ +/************************************************************************** + * External interface implementation */ static int armjtagew_execute_queue(void) { @@ -101,13 +105,12 @@ static int armjtagew_execute_queue(void) enum scan_type type; uint8_t *buffer; - while (cmd != NULL) - { - switch (cmd->type) - { + while (cmd != NULL) { + switch (cmd->type) { case JTAG_RUNTEST: - DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \ - cmd->cmd.runtest->end_state); + DEBUG_JTAG_IO("runtest %i cycles, end in %i", + cmd->cmd.runtest->num_cycles, \ + cmd->cmd.runtest->end_state); armjtagew_end_state(cmd->cmd.runtest->end_state); armjtagew_runtest(cmd->cmd.runtest->num_cycles); @@ -122,10 +125,11 @@ static int armjtagew_execute_queue(void) case JTAG_PATHMOVE: DEBUG_JTAG_IO("pathmove: %i states, end in %i", \ - cmd->cmd.pathmove->num_states, \ - cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); + cmd->cmd.pathmove->num_states, \ + cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); - armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path); + armjtagew_path_move(cmd->cmd.pathmove->num_states, + cmd->cmd.pathmove->path); break; case JTAG_SCAN: @@ -140,18 +144,20 @@ static int armjtagew_execute_queue(void) armjtagew_debug_buffer(buffer, (scan_size + 7) / 8); #endif type = jtag_scan_type(cmd->cmd.scan); - armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan); + armjtagew_scan(cmd->cmd.scan->ir_scan, + type, buffer, + scan_size, cmd->cmd.scan); break; case JTAG_RESET: - DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); + DEBUG_JTAG_IO("reset trst: %i srst %i", + cmd->cmd.reset->trst, + cmd->cmd.reset->srst); armjtagew_tap_execute(); if (cmd->cmd.reset->trst == 1) - { tap_set_state(TAP_RESET); - } armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); break; @@ -183,8 +189,7 @@ static int armjtagew_speed(int speed) result = armjtagew_usb_message(armjtagew_handle, 5, 4); - if (result < 0) - { + if (result < 0) { LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -192,15 +197,11 @@ static int armjtagew_speed(int speed) usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY; result = armjtagew_usb_message(armjtagew_handle, 1, 4); speed_real = (int)buf_get_u32(usb_in_buffer, 0, 32) / 1000; - if (result < 0) - { + if (result < 0) { LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; - } - else - { + } else LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real); - } return ERROR_OK; } @@ -212,7 +213,7 @@ static int armjtagew_khz(int khz, int *jtag_speed) return ERROR_OK; } -static int armjtagew_speed_div(int speed, int* khz) +static int armjtagew_speed_div(int speed, int *khz) { *khz = speed; @@ -225,17 +226,15 @@ static int armjtagew_init(void) armjtagew_handle = armjtagew_usb_open(); - if (armjtagew_handle == 0) - { - LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions."); + if (armjtagew_handle == 0) { + LOG_ERROR( + "Cannot find ARM-JTAG-EW Interface! Please check connection and permissions."); return ERROR_JTAG_INIT_FAILED; } check_cnt = 0; - while (check_cnt < 3) - { - if (armjtagew_get_version_info() == ERROR_OK) - { + while (check_cnt < 3) { + if (armjtagew_get_version_info() == ERROR_OK) { /* attempt to get status */ armjtagew_get_status(); break; @@ -245,9 +244,7 @@ static int armjtagew_init(void) } if (check_cnt == 3) - { LOG_INFO("ARM-JTAG-EW initial read failed, don't worry"); - } /* Initial JTAG speed (for reset and initialization): 32 kHz */ armjtagew_speed(32); @@ -266,17 +263,14 @@ static int armjtagew_quit(void) return ERROR_OK; } -/***************************************************************************/ -/* Queue command implementations */ +/************************************************************************** + * Queue command implementations */ static void armjtagew_end_state(tap_state_t state) { if (tap_is_state_stable(state)) - { tap_set_end_state(state); - } - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } @@ -290,8 +284,7 @@ static void armjtagew_state_move(void) uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state()); int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - for (i = 0; i < tms_count; i++) - { + for (i = 0; i < tms_count; i++) { tms = (tms_scan >> i) & 1; armjtagew_tap_append_step(tms, 0); } @@ -303,24 +296,19 @@ static void armjtagew_path_move(int num_states, tap_state_t *path) { int i; - for (i = 0; i < num_states; i++) - { + for (i = 0; i < num_states; i++) { /* * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode. * Either handle that here, or update the documentation with examples * how to fix that in the configuration files. */ if (path[i] == tap_state_transition(tap_get_state(), false)) - { armjtagew_tap_append_step(0, 0); - } else if (path[i] == tap_state_transition(tap_get_state(), true)) - { armjtagew_tap_append_step(1, 0); - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i])); + else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(tap_get_state()), tap_state_name(path[i])); exit(-1); } @@ -337,27 +325,26 @@ static void armjtagew_runtest(int num_cycles) tap_state_t saved_end_state = tap_get_end_state(); /* only do a state_move when we're not already in IDLE */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { armjtagew_end_state(TAP_IDLE); armjtagew_state_move(); } /* execute num_cycles */ for (i = 0; i < num_cycles; i++) - { armjtagew_tap_append_step(0, 0); - } /* finish in end_state */ armjtagew_end_state(saved_end_state); if (tap_get_state() != tap_get_end_state()) - { armjtagew_state_move(); - } } -static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command) +static void armjtagew_scan(bool ir_scan, + enum scan_type type, + uint8_t *buffer, + int scan_size, + struct scan_command *command) { tap_state_t saved_end_state; @@ -380,9 +367,7 @@ static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, i tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE); if (tap_get_state() != tap_get_end_state()) - { armjtagew_state_move(); - } } static void armjtagew_reset(int trst, int srst) @@ -396,27 +381,21 @@ static void armjtagew_reset(int trst, int srst) LOG_DEBUG("trst: %i, srst: %i", trst, srst); - if (srst == 0) - { + if (srst == 0) { val |= srst_mask; outp_en &= ~srst_mask; /* tristate */ change_mask |= srst_mask; - } - else if (srst == 1) - { + } else if (srst == 1) { val &= ~srst_mask; outp_en |= srst_mask; change_mask |= srst_mask; } - if (trst == 0) - { + if (trst == 0) { val |= trst_mask; outp_en &= ~trst_mask; /* tristate */ change_mask |= trst_mask; - } - else if (trst == 1) - { + } else if (trst == 1) { val &= ~trst_mask; outp_en |= trst_mask; change_mask |= trst_mask; @@ -428,9 +407,7 @@ static void armjtagew_reset(int trst, int srst) usb_out_buffer[3] = change_mask; result = armjtagew_usb_write(armjtagew_handle, 4); if (result != 4) - { LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result); - } } static int armjtagew_get_status(void) @@ -440,27 +417,22 @@ static int armjtagew_get_status(void) usb_out_buffer[0] = CMD_GET_TAPHW_STATE; result = armjtagew_usb_message(armjtagew_handle, 1, 12); - if (result == 0) - { + if (result == 0) { unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16); - LOG_INFO("U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s", - (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)), - (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)), - (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)), - (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)), - usb_in_buffer[9], - usb_in_buffer[11] ? "OVERCURRENT" : "OK", - usb_in_buffer[10] ? "enabled" : "disabled"); + LOG_INFO( + "U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s", + (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)), + (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)), + (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)), + (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)), + usb_in_buffer[9], + usb_in_buffer[11] ? "OVERCURRENT" : "OK", + usb_in_buffer[10] ? "enabled" : "disabled"); if (u_tg < 1500) - { LOG_ERROR("Vref too low. Check Target Power"); - } - } - else - { + } else LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)", result); - } return ERROR_OK; } @@ -475,34 +447,35 @@ static int armjtagew_get_version_info(void) usb_out_buffer[0] = CMD_GET_VERSION; result = armjtagew_usb_message(armjtagew_handle, 1, 4 + 15 + 256); - if (result != 0) - { + if (result != 0) { LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } - memcpy(sn, usb_in_buffer + 4, 15); sn[15] = '\0'; memcpy(auxinfo, usb_in_buffer + 4+15, 256); auxinfo[256] = '\0'; - LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \ - usb_in_buffer[1], usb_in_buffer[0], \ - isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \ - sn, auxinfo); + LOG_INFO( + "ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \ + usb_in_buffer[1], + usb_in_buffer[0], \ + isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \ + sn, + auxinfo); if (1 != usb_in_buffer[1] || 6 != usb_in_buffer[0]) - { - LOG_WARNING("ARM-JTAG-EW firmware version %d.%d is untested with this version of OpenOCD. You might experience unexpected behavior.", usb_in_buffer[1], usb_in_buffer[0]); - } + LOG_WARNING( + "ARM-JTAG-EW firmware version %d.%d is untested with this version of OpenOCD. You might experience unexpected behavior.", + usb_in_buffer[1], + usb_in_buffer[0]); return ERROR_OK; } COMMAND_HANDLER(armjtagew_handle_armjtagew_info_command) { - if (armjtagew_get_version_info() == ERROR_OK) - { + if (armjtagew_get_version_info() == ERROR_OK) { /* attempt to get status */ armjtagew_get_status(); } @@ -532,8 +505,8 @@ struct jtag_interface armjtagew_interface = { .quit = armjtagew_quit, }; -/***************************************************************************/ -/* ARM-JTAG-EW tap functions */ +/************************************************************************** + * ARM-JTAG-EW tap functions */ /* 2048 is the max value we can use here */ #define ARMJTAGEW_TAP_BUFFER_SIZE 2048 @@ -545,8 +518,8 @@ static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE]; struct pending_scan_result { int first; /* First bit position in tdo_buffer to read */ - int length; /* Number of bits to read */ - struct scan_command *command; /* Corresponding scan command */ + int length; /* Number of bits to read */ + struct scan_command *command; /* Corresponding scan command */ uint8_t *buffer; }; @@ -569,9 +542,7 @@ static void armjtagew_tap_ensure_space(int scans, int bits) int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length; if (scans > available_scans || bits > available_bits) - { armjtagew_tap_execute(); - } } static void armjtagew_tap_append_step(int tms, int tdi) @@ -579,40 +550,29 @@ static void armjtagew_tap_append_step(int tms, int tdi) last_tms = tms; int index_local = tap_length / 8; - if (index_local < ARMJTAGEW_TAP_BUFFER_SIZE) - { + if (index_local < ARMJTAGEW_TAP_BUFFER_SIZE) { int bit_index = tap_length % 8; uint8_t bit = 1 << bit_index; if (tms) - { tms_buffer[index_local] |= bit; - } else - { tms_buffer[index_local] &= ~bit; - } if (tdi) - { tdi_buffer[index_local] |= bit; - } else - { tdi_buffer[index_local] &= ~bit; - } tap_length++; - } - else - { + } else LOG_ERROR("armjtagew_tap_append_step, overflow"); - } } void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command) { - struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length]; + struct pending_scan_result *pending_scan_result = + &pending_scan_results_buffer[pending_scan_results_length]; int i; pending_scan_result->first = tap_length; @@ -621,9 +581,7 @@ void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command pending_scan_result->buffer = buffer; for (i = 0; i < length; i++) - { armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1); - } pending_scan_results_length++; } @@ -637,11 +595,9 @@ static int armjtagew_tap_execute(void) int i; int result; - if (tap_length > 0) - { + if (tap_length > 0) { /* Pad last byte so that tap_length is divisible by 8 */ - while (tap_length % 8 != 0) - { + while (tap_length % 8 != 0) { /* More of the last TMS value keeps us in the same state, * analogous to free-running JTAG interfaces. */ armjtagew_tap_append_step(last_tms, 0); @@ -654,36 +610,33 @@ static int armjtagew_tap_execute(void) tms_offset = 3; for (i = 0; i < byte_length; i++) - { - usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8); - } + usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i], 8); tdi_offset = tms_offset + byte_length; for (i = 0; i < byte_length; i++) - { - usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8); - } + usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i], 8); - result = armjtagew_usb_message(armjtagew_handle, 3 + 2 * byte_length, byte_length + 4); + result = armjtagew_usb_message(armjtagew_handle, + 3 + 2 * byte_length, + byte_length + 4); - if (result == 0) - { + if (result == 0) { int stat_local; stat_local = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32); if (stat_local) { - LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat_local); + LOG_ERROR( + "armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", + stat_local); return ERROR_JTAG_QUEUE_FAILED; } for (i = 0; i < byte_length; i++) - { - tdo_buffer[i] = flip_u32(usb_in_buffer[i],8); - } + tdo_buffer[i] = flip_u32(usb_in_buffer[i], 8); - for (i = 0; i < pending_scan_results_length; i++) - { - struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i]; + for (i = 0; i < pending_scan_results_length; i++) { + struct pending_scan_result *pending_scan_result = + &pending_scan_results_buffer[i]; uint8_t *buffer = pending_scan_result->buffer; int length = pending_scan_result->length; int first = pending_scan_result->first; @@ -698,21 +651,18 @@ static int armjtagew_tap_execute(void) armjtagew_debug_buffer(buffer, byte_length); #endif - if (jtag_read_buffer(buffer, command) != ERROR_OK) - { + if (jtag_read_buffer(buffer, command) != ERROR_OK) { armjtagew_tap_init(); return ERROR_JTAG_QUEUE_FAILED; } if (pending_scan_result->buffer != NULL) - { free(pending_scan_result->buffer); - } } - } - else - { - LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length); + } else { + LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", + result, + byte_length); return ERROR_JTAG_QUEUE_FAILED; } @@ -722,10 +672,10 @@ static int armjtagew_tap_execute(void) return ERROR_OK; } -/*****************************************************************************/ -/* JLink USB low-level functions */ +/**************************************************************************** + * JLink USB low-level functions */ -static struct armjtagew* armjtagew_usb_open() +static struct armjtagew *armjtagew_usb_open() { usb_init(); @@ -765,17 +715,15 @@ static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, in int result; result = armjtagew_usb_write(armjtagew, out_length); - if (result == out_length) - { + if (result == out_length) { result = armjtagew_usb_read(armjtagew, in_length); - if (result != in_length) - { - LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result); + if (result != in_length) { + LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", + in_length, + result); return -1; } - } - else - { + } else { LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result); return -1; } @@ -787,14 +735,15 @@ static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length) { int result; - if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE) - { - LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE); + if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE) { + LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)", + out_length, + ARMJTAGEW_OUT_BUFFER_SIZE); return -1; } result = usb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \ - (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT); + (char *)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT); DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result); @@ -808,7 +757,7 @@ static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length) static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length) { int result = usb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN, \ - (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT); + (char *)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT); DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result); @@ -828,17 +777,15 @@ static void armjtagew_debug_buffer(uint8_t *buffer, int length) int i; int j; - for (i = 0; i < length; i += BYTES_PER_LINE) - { + for (i = 0; i < length; i += BYTES_PER_LINE) { snprintf(line, 5, "%04x", i); - for (j = i; j < i + BYTES_PER_LINE && j < length; j++) - { + for (j = i; j < i + BYTES_PER_LINE && j < length; j++) { snprintf(s, 4, " %02x", buffer[j]); strcat(line, s); } LOG_DEBUG("%s", line); - // Prevent GDB timeout (writing to log might take some time) + /* Prevent GDB timeout (writing to log might take some time) */ keep_alive(); } } diff --git a/src/jtag/drivers/at91rm9200.c b/src/jtag/drivers/at91rm9200.c index 6d0dface23..3bba368fab 100644 --- a/src/jtag/drivers/at91rm9200.c +++ b/src/jtag/drivers/at91rm9200.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -26,7 +27,6 @@ #include - /* AT91RM9200 */ #define AT91C_BASE_SYS (0xfffff000) @@ -78,9 +78,8 @@ #define P30 (1 << 30) #define P31 (1 << 31) -struct device_t -{ - char* name; +struct device_t { + char *name; int TDO_PIO; /* PIO holding TDO */ uint32_t TDO_MASK; /* TDO bitmask */ int TRST_PIO; /* PIO holding TRST */ @@ -95,21 +94,20 @@ struct device_t uint32_t SRST_MASK; /* SRST bitmask */ }; -static struct device_t devices[] = -{ +static struct device_t devices[] = { { "rea_ecr", PIOD, P27, PIOA, NC, PIOD, P23, PIOD, P24, PIOD, P26, PIOC, P5 }, { .name = NULL }, }; /* configuration */ -static char* at91rm9200_device; +static char *at91rm9200_device; /* interface variables */ -static struct device_t* device; +static struct device_t *device; static int dev_mem_fd; static void *sys_controller; -static uint32_t* pio_base; +static uint32_t *pio_base; /* low level command set */ @@ -121,8 +119,7 @@ static int at91rm9200_speed(int speed); static int at91rm9200_init(void); static int at91rm9200_quit(void); -static struct bitbang_interface at91rm9200_bitbang = -{ +static struct bitbang_interface at91rm9200_bitbang = { .read = at91rm9200_read, .write = at91rm9200_write, .reset = at91rm9200_reset, @@ -178,8 +175,7 @@ COMMAND_HANDLER(at91rm9200_handle_device_command) return ERROR_COMMAND_SYNTAX_ERROR; /* only if the device name wasn't overwritten by cmdline */ - if (at91rm9200_device == 0) - { + if (at91rm9200_device == 0) { at91rm9200_device = malloc(strlen(CMD_ARGV[0]) + sizeof(char)); strcpy(at91rm9200_device, CMD_ARGV[0]); } @@ -197,12 +193,9 @@ static const struct command_registration at91rm9200_command_handlers[] = { COMMAND_REGISTRATION_DONE }; -struct jtag_interface at91rm9200_interface = -{ +struct jtag_interface at91rm9200_interface = { .name = "at91rm9200", - .execute_queue = bitbang_execute_queue, - .speed = at91rm9200_speed, .commands = at91rm9200_command_handlers, .init = at91rm9200_init, @@ -215,24 +208,20 @@ static int at91rm9200_init(void) cur_device = devices; - if (at91rm9200_device == NULL || at91rm9200_device[0] == 0) - { + if (at91rm9200_device == NULL || at91rm9200_device[0] == 0) { at91rm9200_device = "rea_ecr"; LOG_WARNING("No at91rm9200 device specified, using default 'rea_ecr'"); } - while (cur_device->name) - { - if (strcmp(cur_device->name, at91rm9200_device) == 0) - { + while (cur_device->name) { + if (strcmp(cur_device->name, at91rm9200_device) == 0) { device = cur_device; break; } cur_device++; } - if (!device) - { + if (!device) { LOG_ERROR("No matching device found for %s", at91rm9200_device); return ERROR_JTAG_INIT_FAILED; } @@ -252,7 +241,7 @@ static int at91rm9200_init(void) close(dev_mem_fd); return ERROR_JTAG_INIT_FAILED; } - pio_base = (uint32_t*)sys_controller + 0x100; + pio_base = (uint32_t *)sys_controller + 0x100; /* * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST diff --git a/src/jtag/drivers/bitbang.c b/src/jtag/drivers/bitbang.c index 4548cb531e..8a097ca1b1 100644 --- a/src/jtag/drivers/bitbang.c +++ b/src/jtag/drivers/bitbang.c @@ -20,6 +20,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -38,7 +39,6 @@ */ static void bitbang_stableclocks(int num_cycles); - struct bitbang_interface *bitbang_interface; /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work! @@ -61,14 +61,12 @@ struct bitbang_interface *bitbang_interface; */ #define CLOCK_IDLE() 0 - /* The bitbang driver leaves the TCK 0 when in idle */ static void bitbang_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } @@ -80,8 +78,7 @@ static void bitbang_state_move(int skip) uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state()); int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - for (i = skip; i < tms_count; i++) - { + for (i = skip; i < tms_count; i++) { tms = (tms_scan >> i) & 1; bitbang_interface->write(0, tms, 0); bitbang_interface->write(1, tms, 0); @@ -91,21 +88,19 @@ static void bitbang_state_move(int skip) tap_set_state(tap_get_end_state()); } - /** * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG * (or SWD) state machine. */ static int bitbang_execute_tms(struct jtag_command *cmd) { - unsigned num_bits = cmd->cmd.tms->num_bits; - const uint8_t *bits = cmd->cmd.tms->bits; + unsigned num_bits = cmd->cmd.tms->num_bits; + const uint8_t *bits = cmd->cmd.tms->bits; DEBUG_JTAG_IO("TMS: %d bits", num_bits); int tms = 0; - for (unsigned i = 0; i < num_bits; i++) - { + for (unsigned i = 0; i < num_bits; i++) { tms = ((bits[i/8] >> (i % 8)) & 1); bitbang_interface->write(0, tms, 0); bitbang_interface->write(1, tms, 0); @@ -115,7 +110,6 @@ static int bitbang_execute_tms(struct jtag_command *cmd) return ERROR_OK; } - static void bitbang_path_move(struct pathmove_command *cmd) { int num_states = cmd->num_states; @@ -123,19 +117,15 @@ static void bitbang_path_move(struct pathmove_command *cmd) int tms = 0; state_count = 0; - while (num_states) - { + while (num_states) { if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) - { tms = 0; - } else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count]) - { tms = 1; - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count])); + else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(tap_get_state()), + tap_state_name(cmd->path[state_count])); exit(-1); } @@ -159,15 +149,13 @@ static void bitbang_runtest(int num_cycles) tap_state_t saved_end_state = tap_get_end_state(); /* only do a state_move when we're not already in IDLE */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { bitbang_end_state(TAP_IDLE); bitbang_state_move(0); } /* execute num_cycles */ - for (i = 0; i < num_cycles; i++) - { + for (i = 0; i < num_cycles; i++) { bitbang_interface->write(0, 0, 0); bitbang_interface->write(1, 0, 0); } @@ -179,29 +167,26 @@ static void bitbang_runtest(int num_cycles) bitbang_state_move(0); } - static void bitbang_stableclocks(int num_cycles) { int tms = (tap_get_state() == TAP_RESET ? 1 : 0); int i; /* send num_cycles clocks onto the cable */ - for (i = 0; i < num_cycles; i++) - { + for (i = 0; i < num_cycles; i++) { bitbang_interface->write(1, tms, 0); bitbang_interface->write(0, tms, 0); } } - - static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size) { tap_state_t saved_end_state = tap_get_end_state(); int bit_cnt; - if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) - { + if (!((!ir_scan && + (tap_get_state() == TAP_DRSHIFT)) || + (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) { if (ir_scan) bitbang_end_state(TAP_IRSHIFT); else @@ -211,8 +196,7 @@ static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int bitbang_end_state(saved_end_state); } - for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) - { + for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) { int val = 0; int tms = (bit_cnt == scan_size-1) ? 1 : 0; int tdi; @@ -234,8 +218,7 @@ static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int bitbang_interface->write(1, tms, tdi); - if (type != SCAN_OUT) - { + if (type != SCAN_OUT) { if (val) buffer[bytec] |= bcval; else @@ -243,8 +226,7 @@ static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int } } - if (tap_get_state() != tap_get_end_state()) - { + if (tap_get_state() != tap_get_end_state()) { /* we *KNOW* the above loop transitioned out of * the shift state, so we skip the first state * and move directly to the end state. @@ -255,14 +237,13 @@ static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int int bitbang_execute_queue(void) { - struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ + struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ int scan_size; enum scan_type type; uint8_t *buffer; int retval; - if (!bitbang_interface) - { + if (!bitbang_interface) { LOG_ERROR("BUG: Bitbang interface called, but not yet initialized"); exit(-1); } @@ -275,23 +256,24 @@ int bitbang_execute_queue(void) if (bitbang_interface->blink) bitbang_interface->blink(1); - while (cmd) - { - switch (cmd->type) - { + while (cmd) { + switch (cmd->type) { case JTAG_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); + LOG_DEBUG("reset trst: %i srst %i", + cmd->cmd.reset->trst, + cmd->cmd.reset->srst); #endif - if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) - { + if ((cmd->cmd.reset->trst == 1) || + (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) tap_set_state(TAP_RESET); - } bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); break; case JTAG_RUNTEST: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(cmd->cmd.runtest->end_state)); + LOG_DEBUG("runtest %i cycles, end in %s", + cmd->cmd.runtest->num_cycles, + tap_state_name(cmd->cmd.runtest->end_state)); #endif bitbang_end_state(cmd->cmd.runtest->end_state); bitbang_runtest(cmd->cmd.runtest->num_cycles); @@ -306,21 +288,25 @@ int bitbang_execute_queue(void) case JTAG_TLR_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state)); + LOG_DEBUG("statemove end in %s", + tap_state_name(cmd->cmd.statemove->end_state)); #endif bitbang_end_state(cmd->cmd.statemove->end_state); bitbang_state_move(0); break; case JTAG_PATHMOVE: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states, - tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1])); + LOG_DEBUG("pathmove: %i states, end in %s", + cmd->cmd.pathmove->num_states, + tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1])); #endif bitbang_path_move(cmd->cmd.pathmove); break; case JTAG_SCAN: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("%s scan end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", tap_state_name(cmd->cmd.scan->end_state)); + LOG_DEBUG("%s scan end in %s", + (cmd->cmd.scan->ir_scan) ? "IR" : "DR", + tap_state_name(cmd->cmd.scan->end_state)); #endif bitbang_end_state(cmd->cmd.scan->end_state); scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer); diff --git a/src/jtag/drivers/bitbang.h b/src/jtag/drivers/bitbang.h index db5c4cb865..77f6954611 100644 --- a/src/jtag/drivers/bitbang.h +++ b/src/jtag/drivers/bitbang.h @@ -20,6 +20,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef BITBANG_H #define BITBANG_H diff --git a/src/jtag/drivers/bitq.c b/src/jtag/drivers/bitq.c index 87e0983d16..ac58190eef 100644 --- a/src/jtag/drivers/bitq.c +++ b/src/jtag/drivers/bitq.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -25,8 +26,7 @@ #include "bitq.h" #include - -struct bitq_interface* bitq_interface; /* low level bit queue interface */ +struct bitq_interface *bitq_interface; /* low level bit queue interface */ /* state of input queue */ struct bitq_state { @@ -44,29 +44,23 @@ static struct bitq_state bitq_in_state; static void bitq_in_proc(void) { /* loop through the queue */ - while (bitq_in_state.cmd) - { + while (bitq_in_state.cmd) { /* only JTAG_SCAN command may return data */ - if (bitq_in_state.cmd->type == JTAG_SCAN) - { + if (bitq_in_state.cmd->type == JTAG_SCAN) { /* loop through the fields */ - while (bitq_in_state.field_idx < bitq_in_state.cmd->cmd.scan->num_fields) - { + while (bitq_in_state.field_idx < bitq_in_state.cmd->cmd.scan->num_fields) { struct scan_field *field; field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx]; - if (field->in_value) - { + if (field->in_value) { /* field scanning */ - while (bitq_in_state.bit_pos < field->num_bits) - { + while (bitq_in_state.bit_pos < field->num_bits) { /* index of byte being scanned */ int in_idx = bitq_in_state.bit_pos / 8; /* mask of next bit to be scanned */ uint8_t in_mask = 1 << (bitq_in_state.bit_pos % 8); int tdo = bitq_interface->in(); - if (tdo < 0) - { + if (tdo < 0) { #ifdef _DEBUG_JTAG_IO_ LOG_DEBUG("bitq in EOF"); #endif @@ -89,7 +83,6 @@ static void bitq_in_proc(void) } } - static void bitq_io(int tms, int tdi, int tdo_req) { bitq_interface->out(tms, tdi, tdo_req); @@ -98,25 +91,21 @@ static void bitq_io(int tms, int tdi, int tdo_req) bitq_in_proc(); } - static void bitq_end_state(tap_state_t state) { - if (!tap_is_state_stable(state)) - { + if (!tap_is_state_stable(state)) { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } tap_set_end_state(state); } - static void bitq_state_move(tap_state_t new_state) { int i = 0; uint8_t tms_scan; - if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state)) - { + if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state)) { LOG_ERROR("TAP move from or to unstable state"); exit(-1); } @@ -124,8 +113,7 @@ static void bitq_state_move(tap_state_t new_state) tms_scan = tap_get_tms_path(tap_get_state(), new_state); int tms_count = tap_get_tms_path_len(tap_get_state(), new_state); - for (i = 0; i < tms_count; i++) - { + for (i = 0; i < tms_count; i++) { bitq_io(tms_scan & 1, 0, 0); tms_scan >>= 1; } @@ -133,19 +121,16 @@ static void bitq_state_move(tap_state_t new_state) tap_set_state(new_state); } - static void bitq_path_move(struct pathmove_command *cmd) { int i; - for (i = 0; i <= cmd->num_states; i++) - { + for (i = 0; i <= cmd->num_states; i++) { if (tap_state_transition(tap_get_state(), false) == cmd->path[i]) bitq_io(0, 0, 0); else if (tap_state_transition(tap_get_state(), true) == cmd->path[i]) bitq_io(1, 0, 0); - else - { + else { LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name( tap_get_state()), tap_state_name(cmd->path[i])); exit(-1); @@ -157,7 +142,6 @@ static void bitq_path_move(struct pathmove_command *cmd) tap_set_end_state(tap_get_state()); } - static void bitq_runtest(int num_cycles) { int i; @@ -175,13 +159,12 @@ static void bitq_runtest(int num_cycles) bitq_state_move(tap_get_end_state()); } - static void bitq_scan_field(struct scan_field *field, int do_pause) { int bit_cnt; int tdo_req; - const uint8_t* out_ptr; + const uint8_t *out_ptr; uint8_t out_mask; if (field->in_value) @@ -189,36 +172,29 @@ static void bitq_scan_field(struct scan_field *field, int do_pause) else tdo_req = 0; - if (field->out_value == NULL) - { + if (field->out_value == NULL) { /* just send zeros and request data from TDO */ for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--) bitq_io(0, 0, tdo_req); bitq_io(do_pause, 0, tdo_req); - } - else - { + } else { /* send data, and optionally request TDO */ out_mask = 0x01; out_ptr = field->out_value; - for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--) - { + for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--) { bitq_io(0, ((*out_ptr) & out_mask) != 0, tdo_req); - if (out_mask == 0x80) - { + if (out_mask == 0x80) { out_mask = 0x01; out_ptr++; - } - else + } else out_mask <<= 1; } bitq_io(do_pause, ((*out_ptr) & out_mask) != 0, tdo_req); } - if (do_pause) - { + if (do_pause) { bitq_io(0, 0, 0); if (tap_get_state() == TAP_IRSHIFT) tap_set_state(TAP_IRPAUSE); @@ -227,7 +203,6 @@ static void bitq_scan_field(struct scan_field *field, int do_pause) } } - static void bitq_scan(struct scan_command *cmd) { int i; @@ -243,28 +218,25 @@ static void bitq_scan(struct scan_command *cmd) bitq_scan_field(&cmd->fields[i], 1); } - int bitq_execute_queue(void) { - struct jtag_command* cmd = jtag_command_queue; /* currently processed command */ + struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ bitq_in_state.cmd = jtag_command_queue; bitq_in_state.field_idx = 0; bitq_in_state.bit_pos = 0; bitq_in_state.status = ERROR_OK; - while (cmd) - { - switch (cmd->type) - { + while (cmd) { + switch (cmd->type) { case JTAG_RESET: #ifdef _DEBUG_JTAG_IO_ LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); #endif - if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) - { + if ((cmd->cmd.reset->trst == 1) || + (cmd->cmd.reset->srst && + (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) tap_set_state(TAP_RESET); - } bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); if (bitq_interface->in_rdy()) bitq_in_proc(); @@ -328,13 +300,11 @@ int bitq_execute_queue(void) bitq_interface->flush(); bitq_in_proc(); - if (bitq_in_state.cmd) - { + if (bitq_in_state.cmd) { LOG_ERROR("missing data from bitq interface"); return ERROR_JTAG_QUEUE_FAILED; } - if (bitq_interface->in() >= 0) - { + if (bitq_interface->in() >= 0) { LOG_ERROR("extra data from bitq interface"); return ERROR_JTAG_QUEUE_FAILED; } @@ -342,7 +312,6 @@ int bitq_execute_queue(void) return bitq_in_state.status; } - void bitq_cleanup(void) { } diff --git a/src/jtag/drivers/bitq.h b/src/jtag/drivers/bitq.h index 75d208dae4..c65e931d90 100644 --- a/src/jtag/drivers/bitq.h +++ b/src/jtag/drivers/bitq.h @@ -17,13 +17,14 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef BITQ_H #define BITQ_H #include struct bitq_interface { - // function to enqueueing low level IO requests + /* function to enqueueing low level IO requests */ int (*out)(int tms, int tdi, int tdo_req); int (*flush)(void); diff --git a/src/jtag/drivers/buspirate.c b/src/jtag/drivers/buspirate.c index f3edc8a52b..86b9882356 100644 --- a/src/jtag/drivers/buspirate.c +++ b/src/jtag/drivers/buspirate.c @@ -44,7 +44,6 @@ static void buspirate_runtest(int num_cycles); static void buspirate_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command); - #define CMD_UNKNOWN 0x00 #define CMD_PORT_MODE 0x01 #define CMD_FEATURE 0x02 @@ -84,7 +83,6 @@ enum { SERIAL_FAST = 1 }; - static int buspirate_fd = -1; static int buspirate_pinmode = MODE_JTAG_OD; static int buspirate_baudrate = SERIAL_NORMAL; @@ -92,7 +90,6 @@ static int buspirate_vreg; static int buspirate_pullup; static char *buspirate_port; - /* TAP interface */ static void buspirate_tap_init(void); static int buspirate_tap_execute(void); @@ -279,9 +276,8 @@ COMMAND_HANDLER(buspirate_handle_adc_command) COMMAND_HANDLER(buspirate_handle_vreg_command) { - if (CMD_ARGC < 1) { + if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (atoi(CMD_ARGV[0]) == 1) buspirate_vreg = 1; @@ -296,9 +292,8 @@ COMMAND_HANDLER(buspirate_handle_vreg_command) COMMAND_HANDLER(buspirate_handle_pullup_command) { - if (CMD_ARGC < 1) { + if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (atoi(CMD_ARGV[0]) == 1) buspirate_pullup = 1; @@ -313,9 +308,8 @@ COMMAND_HANDLER(buspirate_handle_pullup_command) COMMAND_HANDLER(buspirate_handle_led_command) { - if (CMD_ARGC < 1) { + if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (atoi(CMD_ARGV[0]) == 1) { /* enable led */ @@ -335,9 +329,8 @@ COMMAND_HANDLER(buspirate_handle_led_command) COMMAND_HANDLER(buspirate_handle_mode_command) { - if (CMD_ARGC < 1) { + if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (CMD_ARGV[0][0] == 'n') buspirate_pinmode = MODE_JTAG; @@ -352,9 +345,8 @@ COMMAND_HANDLER(buspirate_handle_mode_command) COMMAND_HANDLER(buspirate_handle_speed_command) { - if (CMD_ARGC < 1) { + if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (CMD_ARGV[0][0] == 'n') buspirate_baudrate = SERIAL_NORMAL; @@ -369,9 +361,8 @@ COMMAND_HANDLER(buspirate_handle_speed_command) COMMAND_HANDLER(buspirate_handle_port_command) { - if (CMD_ARGC < 1) { + if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (buspirate_port == NULL) buspirate_port = strdup(CMD_ARGV[0]); @@ -987,8 +978,6 @@ static void buspirate_print_buffer(char *buf, int size) } } - if (line[0] != 0) { + if (line[0] != 0) LOG_DEBUG("%s", line); - } } - diff --git a/src/jtag/drivers/driver.c b/src/jtag/drivers/driver.c index 49f40246e9..87ed6cafde 100644 --- a/src/jtag/drivers/driver.c +++ b/src/jtag/drivers/driver.c @@ -27,6 +27,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -37,8 +38,7 @@ #include #include -struct jtag_callback_entry -{ +struct jtag_callback_entry { struct jtag_callback_entry *next; jtag_callback_t callback; @@ -48,8 +48,8 @@ struct jtag_callback_entry jtag_callback_data_t data3; }; -static struct jtag_callback_entry *jtag_callback_queue_head = NULL; -static struct jtag_callback_entry *jtag_callback_queue_tail = NULL; +static struct jtag_callback_entry *jtag_callback_queue_head; +static struct jtag_callback_entry *jtag_callback_queue_tail; static void jtag_callback_queue_reset(void) { @@ -62,60 +62,56 @@ static void jtag_callback_queue_reset(void) * * This allocates a new copy of out_value using cmd_queue_alloc. */ -static void cmd_queue_scan_field_clone(struct scan_field * dst, const struct scan_field * src) +static void cmd_queue_scan_field_clone(struct scan_field *dst, const struct scan_field *src) { dst->num_bits = src->num_bits; dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(DIV_ROUND_UP(src->num_bits, 8)), src->num_bits); dst->in_value = src->in_value; } - /** * see jtag_add_ir_scan() * */ -int interface_jtag_add_ir_scan(struct jtag_tap* active, const struct scan_field *in_fields, tap_state_t state) +int interface_jtag_add_ir_scan(struct jtag_tap *active, + const struct scan_field *in_fields, tap_state_t state) { size_t num_taps = jtag_tap_count_enabled(); - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); - struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command)); - struct scan_field * out_fields = cmd_queue_alloc(num_taps * sizeof(struct scan_field)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command)); + struct scan_field *out_fields = cmd_queue_alloc(num_taps * sizeof(struct scan_field)); jtag_queue_command(cmd); - cmd->type = JTAG_SCAN; - cmd->cmd.scan = scan; + cmd->type = JTAG_SCAN; + cmd->cmd.scan = scan; - scan->ir_scan = true; - scan->num_fields = num_taps; /* one field per device */ - scan->fields = out_fields; - scan->end_state = state; + scan->ir_scan = true; + scan->num_fields = num_taps; /* one field per device */ + scan->fields = out_fields; + scan->end_state = state; - - struct scan_field * field = out_fields; /* keep track where we insert data */ + struct scan_field *field = out_fields; /* keep track where we insert data */ /* loop over all enabled TAPs */ - for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) - { + for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) { /* search the input field list for fields for the current TAP */ - if (tap == active) - { + if (tap == active) { /* if TAP is listed in input fields, copy the value */ tap->bypass = 0; cmd_queue_scan_field_clone(field, in_fields); - } else - { + } else { /* if a TAP isn't listed in input fields, set it to BYPASS */ tap->bypass = 1; - field->num_bits = tap->ir_length; - field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length); - field->in_value = NULL; /* do not collect input for tap's in bypass */ + field->num_bits = tap->ir_length; + field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length); + field->in_value = NULL; /* do not collect input for tap's in bypass */ } /* update device information */ @@ -123,8 +119,8 @@ int interface_jtag_add_ir_scan(struct jtag_tap* active, const struct scan_field field++; } - - assert(field == out_fields + num_taps); /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */ + /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */ + assert(field == out_fields + num_taps); return ERROR_OK; } @@ -133,51 +129,47 @@ int interface_jtag_add_ir_scan(struct jtag_tap* active, const struct scan_field * see jtag_add_dr_scan() * */ -int interface_jtag_add_dr_scan(struct jtag_tap* active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state) +int interface_jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, + const struct scan_field *in_fields, tap_state_t state) { /* count devices in bypass */ size_t bypass_devices = 0; - for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) - { + for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) { if (tap->bypass) bypass_devices++; } - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); - struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command)); - struct scan_field * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command)); + struct scan_field *out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field)); jtag_queue_command(cmd); - cmd->type = JTAG_SCAN; - cmd->cmd.scan = scan; + cmd->type = JTAG_SCAN; + cmd->cmd.scan = scan; - scan->ir_scan = false; - scan->num_fields = in_num_fields + bypass_devices; - scan->fields = out_fields; - scan->end_state = state; + scan->ir_scan = false; + scan->num_fields = in_num_fields + bypass_devices; + scan->fields = out_fields; + scan->end_state = state; - - struct scan_field * field = out_fields; /* keep track where we insert data */ + struct scan_field *field = out_fields; /* keep track where we insert data */ /* loop over all enabled TAPs */ - for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) - { + for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) { /* if TAP is not bypassed insert matching input fields */ - if (!tap->bypass) - { + if (!tap->bypass) { assert(active == tap); #ifndef NDEBUG /* remember initial position for assert() */ struct scan_field *start_field = field; #endif /* NDEBUG */ - for (int j = 0; j < in_num_fields; j++) - { + for (int j = 0; j < in_num_fields; j++) { cmd_queue_scan_field_clone(field, in_fields + j); field++; @@ -187,11 +179,10 @@ int interface_jtag_add_dr_scan(struct jtag_tap* active, int in_num_fields, const } /* if a TAP is bypassed, generated a dummy bit*/ - else - { - field->num_bits = 1; - field->out_value = NULL; - field->in_value = NULL; + else { + field->num_bits = 1; + field->out_value = NULL; + field->in_value = NULL; field++; } @@ -202,8 +193,6 @@ int interface_jtag_add_dr_scan(struct jtag_tap* active, int in_num_fields, const return ERROR_OK; } - - /** * Generate a DR SCAN using the array of output values passed to the function * @@ -228,65 +217,60 @@ void interface_jtag_add_dr_out(struct jtag_tap *target_tap, size_t bypass_devices = 0; - for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) - { + for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) { if (tap->bypass) bypass_devices++; } - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); - struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command)); - struct scan_field * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command)); + struct scan_field *out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field)); jtag_queue_command(cmd); - cmd->type = JTAG_SCAN; - cmd->cmd.scan = scan; + cmd->type = JTAG_SCAN; + cmd->cmd.scan = scan; - scan->ir_scan = false; - scan->num_fields = in_num_fields + bypass_devices; - scan->fields = out_fields; - scan->end_state = end_state; + scan->ir_scan = false; + scan->num_fields = in_num_fields + bypass_devices; + scan->fields = out_fields; + scan->end_state = end_state; bool target_tap_match = false; - struct scan_field * field = out_fields; /* keep track where we insert data */ + struct scan_field *field = out_fields; /* keep track where we insert data */ /* loop over all enabled TAPs */ - for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) - { + for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) { /* if TAP is not bypassed insert matching input fields */ - if (!tap->bypass) - { + if (!tap->bypass) { assert(tap == target_tap); /* target_tap must match the one not bypassed TAP */ target_tap_match = true; - for (int j = 0; j < in_num_fields; j++) - { + for (int j = 0; j < in_num_fields; j++) { uint8_t out_value[4]; size_t scan_size = num_bits[j]; buf_set_u32(out_value, 0, scan_size, value[j]); - field->num_bits = scan_size; - field->out_value = buf_cpy(out_value, cmd_queue_alloc(DIV_ROUND_UP(scan_size, 8)), scan_size); - field->in_value = NULL; + field->num_bits = scan_size; + field->out_value = buf_cpy(out_value, cmd_queue_alloc(DIV_ROUND_UP(scan_size, 8)), scan_size); + field->in_value = NULL; field++; } } /* if a TAP is bypassed, generated a dummy bit*/ - else - { + else { - field->num_bits = 1; - field->out_value = NULL; - field->in_value = NULL; + field->num_bits = 1; + field->out_value = NULL; + field->in_value = NULL; field++; } @@ -298,23 +282,23 @@ void interface_jtag_add_dr_out(struct jtag_tap *target_tap, static int jtag_add_plain_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state, bool ir_scan) { - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); - struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command)); - struct scan_field * out_fields = cmd_queue_alloc(sizeof(struct scan_field)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command)); + struct scan_field *out_fields = cmd_queue_alloc(sizeof(struct scan_field)); jtag_queue_command(cmd); - cmd->type = JTAG_SCAN; - cmd->cmd.scan = scan; + cmd->type = JTAG_SCAN; + cmd->cmd.scan = scan; - scan->ir_scan = ir_scan; - scan->num_fields = 1; - scan->fields = out_fields; - scan->end_state = state; + scan->ir_scan = ir_scan; + scan->num_fields = 1; + scan->fields = out_fields; + scan->end_state = state; - out_fields->num_bits = num_bits; - out_fields->out_value = buf_cpy(out_bits, cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits); - out_fields->in_value = in_bits; + out_fields->num_bits = num_bits; + out_fields->out_value = buf_cpy(out_bits, cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits); + out_fields->in_value = in_bits; return ERROR_OK; } @@ -334,7 +318,7 @@ int interface_jtag_add_tlr(void) tap_state_t state = TAP_RESET; /* allocate memory for a new list member */ - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); jtag_queue_command(cmd); @@ -374,7 +358,7 @@ int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state int interface_jtag_add_pathmove(int num_states, const tap_state_t *path) { /* allocate memory for a new list member */ - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); jtag_queue_command(cmd); @@ -393,7 +377,7 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path) int interface_jtag_add_runtest(int num_cycles, tap_state_t state) { /* allocate memory for a new list member */ - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); jtag_queue_command(cmd); @@ -409,7 +393,7 @@ int interface_jtag_add_runtest(int num_cycles, tap_state_t state) int interface_jtag_add_clocks(int num_cycles) { /* allocate memory for a new list member */ - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); jtag_queue_command(cmd); @@ -424,7 +408,7 @@ int interface_jtag_add_clocks(int num_cycles) int interface_jtag_add_reset(int req_trst, int req_srst) { /* allocate memory for a new list member */ - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); jtag_queue_command(cmd); @@ -440,7 +424,7 @@ int interface_jtag_add_reset(int req_trst, int req_srst) int interface_jtag_add_sleep(uint32_t us) { /* allocate memory for a new list member */ - struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command)); + struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command)); jtag_queue_command(cmd); @@ -453,7 +437,9 @@ int interface_jtag_add_sleep(uint32_t us) } /* add callback to end of queue */ -void interface_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3) +void interface_jtag_add_callback4(jtag_callback_t callback, + jtag_callback_data_t data0, jtag_callback_data_t data1, + jtag_callback_data_t data2, jtag_callback_data_t data3) { struct jtag_callback_entry *entry = cmd_queue_alloc(sizeof(struct jtag_callback_entry)); @@ -464,12 +450,10 @@ void interface_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t entry->data2 = data2; entry->data3 = data3; - if (jtag_callback_queue_head == NULL) - { + if (jtag_callback_queue_head == NULL) { jtag_callback_queue_head = entry; jtag_callback_queue_tail = entry; - } else - { + } else { jtag_callback_queue_tail->next = entry; jtag_callback_queue_tail = entry; } @@ -477,17 +461,15 @@ void interface_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t int interface_jtag_execute_queue(void) { - static int reentry = 0; + static int reentry; - assert(reentry==0); + assert(reentry == 0); reentry++; int retval = default_interface_jtag_execute_queue(); - if (retval == ERROR_OK) - { + if (retval == ERROR_OK) { struct jtag_callback_entry *entry; - for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next) - { + for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next) { retval = entry->callback(entry->data0, entry->data1, entry->data2, entry->data3); if (retval != ERROR_OK) break; @@ -502,7 +484,8 @@ int interface_jtag_execute_queue(void) return retval; } -static int jtag_convert_to_callback4(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3) +static int jtag_convert_to_callback4(jtag_callback_data_t data0, + jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3) { ((jtag_callback1_t)data1)(data0); return ERROR_OK; @@ -513,10 +496,9 @@ void interface_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t jtag_add_callback4(jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0); } - /* A minidriver can use use an inline versions of this API level fn */ -void jtag_add_dr_out(struct jtag_tap* tap, - int num_fields, const int* num_bits, const uint32_t* value, +void jtag_add_dr_out(struct jtag_tap *tap, + int num_fields, const int *num_bits, const uint32_t *value, tap_state_t end_state) { assert(end_state != TAP_RESET); diff --git a/src/jtag/drivers/dummy.c b/src/jtag/drivers/dummy.c index 8b1e5ecb4c..be61b37d27 100644 --- a/src/jtag/drivers/dummy.c +++ b/src/jtag/drivers/dummy.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -25,17 +26,15 @@ #include "bitbang.h" #include "hello.h" - /* my private tap controller state, which tracks state for calling code */ static tap_state_t dummy_state = TAP_RESET; -static int dummy_clock; /* edge detector */ +static int dummy_clock; /* edge detector */ -static int clock_count; /* count clocks in any stable state, only stable states */ +static int clock_count; /* count clocks in any stable state, only stable states */ static uint32_t dummy_data; - static int dummy_read(void) { int data = 1 & dummy_data; @@ -43,21 +42,16 @@ static int dummy_read(void) return data; } - static void dummy_write(int tck, int tms, int tdi) { /* TAP standard: "state transitions occur on rising edge of clock" */ - if (tck != dummy_clock) - { - if (tck) - { + if (tck != dummy_clock) { + if (tck) { tap_state_t old_state = dummy_state; dummy_state = tap_state_transition(old_state, tms); - if (old_state != dummy_state) - { - if (clock_count) - { + if (old_state != dummy_state) { + if (clock_count) { LOG_DEBUG("dummy_tap: %d stable clocks", clock_count); clock_count = 0; } @@ -68,9 +62,7 @@ static void dummy_write(int tck, int tms, int tdi) if (dummy_state == TAP_DRCAPTURE) dummy_data = 0x01255043; #endif - } - else - { + } else { /* this is a stable state clock edge, no change of state here, * simply increment clock_count for subsequent logging */ @@ -102,30 +94,21 @@ static struct bitbang_interface dummy_bitbang = { .blink = &dummy_led, }; - static int dummy_khz(int khz, int *jtag_speed) { if (khz == 0) - { *jtag_speed = 0; - } else - { *jtag_speed = 64000/khz; - } return ERROR_OK; } static int dummy_speed_div(int speed, int *khz) { if (speed == 0) - { *khz = 0; - } else - { *khz = 64000/speed; - } return ERROR_OK; } diff --git a/src/jtag/drivers/ep93xx.c b/src/jtag/drivers/ep93xx.c index 7e3b4568be..978113076e 100644 --- a/src/jtag/drivers/ep93xx.c +++ b/src/jtag/drivers/ep93xx.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -34,7 +35,7 @@ #include -static uint8_t output_value = 0x0; +static uint8_t output_value; static int dev_mem_fd; static void *gpio_controller; static volatile uint8_t *gpio_data_register; @@ -52,8 +53,7 @@ static int ep93xx_quit(void); struct timespec ep93xx_zzzz; -struct jtag_interface ep93xx_interface = -{ +struct jtag_interface ep93xx_interface = { .name = "ep93xx", .supported = DEBUG_CAP_TMS_SEQ, @@ -64,8 +64,7 @@ struct jtag_interface ep93xx_interface = .quit = ep93xx_quit, }; -static struct bitbang_interface ep93xx_bitbang = -{ +static struct bitbang_interface ep93xx_bitbang = { .read = ep93xx_read, .write = ep93xx_write, .reset = ep93xx_reset, diff --git a/src/jtag/drivers/ft2232.c b/src/jtag/drivers/ft2232.c index 4b9bdef7dd..3a156bfea7 100644 --- a/src/jtag/drivers/ft2232.c +++ b/src/jtag/drivers/ft2232.c @@ -101,13 +101,12 @@ #include #include "ftd2xx_common.h" -enum ftdi_interface -{ - INTERFACE_ANY = 0, - INTERFACE_A = 1, - INTERFACE_B = 2, - INTERFACE_C = 3, - INTERFACE_D = 4 +enum ftdi_interface { + INTERFACE_ANY = 0, + INTERFACE_A = 1, + INTERFACE_B = 2, + INTERFACE_C = 3, + INTERFACE_D = 4 }; #elif BUILD_FT2232_LIBFTDI == 1 @@ -117,7 +116,7 @@ enum ftdi_interface /* max TCK for the high speed devices 30000 kHz */ #define FTDI_2232H_4232H_MAX_TCK 30000 /* max TCK for the full speed devices 6000 kHz */ -#define FTDI_2232C_MAX_TCK 6000 +#define FTDI_2232C_MAX_TCK 6000 /* this speed value tells that RTCK is requested */ #define RTCK_SPEED -1 @@ -150,13 +149,13 @@ enum ftdi_interface * * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure. */ -static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd); +static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd); -static char * ft2232_device_desc_A = NULL; -static char* ft2232_device_desc = NULL; -static char* ft2232_serial = NULL; -static uint8_t ft2232_latency = 2; -static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK; +static char *ft2232_device_desc_A; +static char *ft2232_device_desc; +static char *ft2232_serial; +static uint8_t ft2232_latency = 2; +static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK; #define MAX_USB_IDS 8 /* vid = pid = 0 marks the end of the list */ @@ -164,7 +163,7 @@ static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 }; static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 }; struct ft2232_layout { - char* name; + char *name; int (*init)(void); void (*reset)(int trst, int srst); void (*blink)(void); @@ -225,10 +224,9 @@ static void flossjtag_blink(void); /* common transport support options */ -//static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; +/* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */ -static const struct ft2232_layout ft2232_layouts[] = -{ +static const struct ft2232_layout ft2232_layouts[] = { { .name = "usbjtag", .init = usbjtag_init, .reset = ftx23_reset, @@ -321,8 +319,8 @@ static const struct ft2232_layout ft2232_layouts[] = }, { .name = "redbee-usb", .init = redbee_init, - .reset = redbee_reset, - .channel = INTERFACE_B, + .reset = redbee_reset, + .channel = INTERFACE_B, }, { .name = "lisa-l", .init = lisa_l_init, @@ -343,37 +341,37 @@ static const struct ft2232_layout ft2232_layouts[] = }; /* bitmask used to drive nTRST; usually a GPIOLx signal */ -static uint8_t nTRST; -static uint8_t nTRSTnOE; +static uint8_t nTRST; +static uint8_t nTRSTnOE; /* bitmask used to drive nSRST; usually a GPIOLx signal */ -static uint8_t nSRST; -static uint8_t nSRSTnOE; +static uint8_t nSRST; +static uint8_t nSRSTnOE; /** the layout being used with this debug session */ static const struct ft2232_layout *layout; /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */ -static uint8_t low_output = 0x0; +static uint8_t low_output; /* note that direction bit == 1 means that signal is an output */ /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */ -static uint8_t low_direction = 0x0; +static uint8_t low_direction; /** default value bitmask for CBUS GPIOH(0..4) */ -static uint8_t high_output = 0x0; +static uint8_t high_output; /** default direction bitmask for CBUS GPIOH(0..4) */ -static uint8_t high_direction = 0x0; +static uint8_t high_direction; #if BUILD_FT2232_FTD2XX == 1 -static FT_HANDLE ftdih = NULL; -static FT_DEVICE ftdi_device = 0; +static FT_HANDLE ftdih; +static FT_DEVICE ftdi_device; #elif BUILD_FT2232_LIBFTDI == 1 static struct ftdi_context ftdic; static enum ftdi_chip_type ftdi_device; #endif -static struct jtag_command* first_unsent; /* next command that has to be sent */ -static int require_send; +static struct jtag_command *first_unsent; /* next command that has to be sent */ +static int require_send; /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says: @@ -389,17 +387,17 @@ static int require_send; */ #if BUILD_FT2232_FTD2XX == 1 -#define FT2232_BUFFER_READ_QUEUE_SIZE (64*64) +#define FT2232_BUFFER_READ_QUEUE_SIZE (64*64) #else -#define FT2232_BUFFER_READ_QUEUE_SIZE (64*4) +#define FT2232_BUFFER_READ_QUEUE_SIZE (64*4) #endif #define FT2232_BUFFER_SIZE 131072 -static uint8_t* ft2232_buffer = NULL; -static int ft2232_buffer_size = 0; -static int ft2232_read_pointer = 0; -static int ft2232_expect_read = 0; +static uint8_t *ft2232_buffer; +static int ft2232_buffer_size; +static int ft2232_read_pointer; +static int ft2232_expect_read; /** * Function buffer_write @@ -443,17 +441,16 @@ static inline uint8_t buffer_read(void) */ static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit) { - uint8_t tms_byte; - int i; - int tms_ndx; /* bit index into tms_byte */ + uint8_t tms_byte; + int i; + int tms_ndx; /* bit index into tms_byte */ assert(tms_count > 0); DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d", - mpsse_cmd, tms_bits, tms_count); + mpsse_cmd, tms_bits, tms_count); - for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1) - { + for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) { bool bit = tms_bits & 1; if (bit) @@ -463,17 +460,16 @@ static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_b tap_set_state(tap_state_transition(tap_get_state(), bit)); /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero - also increment. + * also increment. */ ++tms_ndx; - if (tms_ndx == 7 || i == tms_count-1) - { + if (tms_ndx == 7 || i == tms_count-1) { buffer_write(mpsse_cmd); buffer_write(tms_ndx - 1); /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of - TMS/CS and is held static for the duration of TMS/CS clocking. + * TMS/CS and is held static for the duration of TMS/CS clocking. */ buffer_write(tms_byte | (tdi_bit << 7)); } @@ -500,11 +496,10 @@ static inline int get_tms_buffer_requirements(int bit_count) */ static void move_to_state(tap_state_t goal_state) { - tap_state_t start_state = tap_get_state(); + tap_state_t start_state = tap_get_state(); /* goal_state is 1/2 of a tuple/pair of states which allow convenient - lookup of the required TMS pattern to move to this state from the - start state. + * lookup of the required TMS pattern to move to this state from the start state. */ /* do the 2 lookups */ @@ -516,44 +511,37 @@ static void move_to_state(tap_state_t goal_state) clock_tms(0x4b, tms_bits, tms_count, 0); } -static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written) +static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written) { #if BUILD_FT2232_FTD2XX == 1 FT_STATUS status; DWORD dw_bytes_written = 0; - if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK) - { + status = FT_Write(ftdih, buf, size, &dw_bytes_written); + if (status != FT_OK) { *bytes_written = dw_bytes_written; LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; - } - else - { + } else *bytes_written = dw_bytes_written; - } + #elif BUILD_FT2232_LIBFTDI == 1 - int retval; - if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0) - { + int retval = ftdi_write_data(&ftdic, buf, size); + if (retval < 0) { *bytes_written = 0; LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; - } - else - { + } else *bytes_written = retval; - } + #endif if (*bytes_written != (uint32_t)size) - { return ERROR_JTAG_DEVICE_ERROR; - } return ERROR_OK; } -static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read) +static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read) { #if BUILD_FT2232_FTD2XX == 1 DWORD dw_bytes_read; @@ -561,11 +549,10 @@ static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read) int timeout = 5; *bytes_read = 0; - while ((*bytes_read < size) && timeout--) - { - if ((status = FT_Read(ftdih, buf + *bytes_read, size - - *bytes_read, &dw_bytes_read)) != FT_OK) - { + while ((*bytes_read < size) && timeout--) { + status = FT_Read(ftdih, buf + *bytes_read, size - + *bytes_read, &dw_bytes_read); + if (status != FT_OK) { *bytes_read = 0; LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; @@ -578,10 +565,9 @@ static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read) int timeout = LIBFTDI_READ_RETRY_COUNT; *bytes_read = 0; - while ((*bytes_read < size) && timeout--) - { - if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0) - { + while ((*bytes_read < size) && timeout--) { + retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read); + if (retval < 0) { *bytes_read = 0; LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; @@ -591,12 +577,11 @@ static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read) #endif - if (*bytes_read < size) - { + if (*bytes_read < size) { LOG_ERROR("couldn't read enough bytes from " - "FT2232 device (%i < %i)", - (unsigned)*bytes_read, - (unsigned)size); + "FT2232 device (%i < %i)", + (unsigned)*bytes_read, + (unsigned)size); return ERROR_JTAG_DEVICE_ERROR; } @@ -626,8 +611,8 @@ static int ft2232h_ft4232h_adaptive_clocking(bool enable) uint32_t bytes_written; int retval; - if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK) - { + retval = ft2232_write(&buf, sizeof(buf), &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't write command to %s adaptive clocking" , enable ? "enable" : "disable"); return retval; @@ -646,8 +631,7 @@ static int ft2232h_ft4232h_clk_divide_by_5(bool enable) uint32_t bytes_written; uint8_t buf = enable ? 0x8b : 0x8a; - if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) - { + if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) { LOG_ERROR("couldn't write command to %s clk divide by 5" , enable ? "enable" : "disable"); return ERROR_JTAG_INIT_FAILED; @@ -668,8 +652,7 @@ static int ft2232_speed(int speed) bool enable_adaptive_clocking = (RTCK_SPEED == speed); if (ft2232_device_is_highspeed()) retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking); - else if (enable_adaptive_clocking) - { + else if (enable_adaptive_clocking) { LOG_ERROR("ft2232 device %lu does not support RTCK" , (long unsigned int)ftdi_device); return ERROR_FAIL; @@ -683,8 +666,8 @@ static int ft2232_speed(int speed) buf[2] = (speed >> 8) & 0xff; /* valueH */ LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]); - if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK) - { + retval = ft2232_write(buf, sizeof(buf), &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't set FT2232 TCK speed"); return retval; } @@ -692,7 +675,7 @@ static int ft2232_speed(int speed) return ERROR_OK; } -static int ft2232_speed_div(int speed, int* khz) +static int ft2232_speed_div(int speed, int *khz) { /* Take a look in the FT2232 manual, * AN2232C-01 Command Processor for @@ -703,17 +686,13 @@ static int ft2232_speed_div(int speed, int* khz) return ERROR_OK; } -static int ft2232_khz(int khz, int* jtag_speed) +static int ft2232_khz(int khz, int *jtag_speed) { - if (khz == 0) - { - if (ft2232_device_is_highspeed()) - { + if (khz == 0) { + if (ft2232_device_is_highspeed()) { *jtag_speed = RTCK_SPEED; return ERROR_OK; - } - else - { + } else { LOG_DEBUG("RCLK not supported"); return ERROR_FAIL; } @@ -726,8 +705,8 @@ static int ft2232_khz(int khz, int* jtag_speed) * We will calc here with a multiplier * of 10 for better rounding later. */ - /* Calc speed, (ft2232_max_tck / khz) - 1 */ - /* Use 65000 for better rounding */ + /* Calc speed, (ft2232_max_tck / khz) - 1 + * Use 65000 for better rounding */ *jtag_speed = ((ft2232_max_tck*10) / khz) - 10; /* Add 0.9 for rounding */ @@ -738,15 +717,11 @@ static int ft2232_khz(int khz, int* jtag_speed) /* Check if speed is greater than 0 */ if (*jtag_speed < 0) - { *jtag_speed = 0; - } /* Check max value */ if (*jtag_speed > 0xFFFF) - { *jtag_speed = 0xFFFF; - } return ERROR_OK; } @@ -755,21 +730,19 @@ static void ft2232_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); - else - { + else { LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state)); exit(-1); } } -static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size) +static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size) { int num_bytes = (scan_size + 7) / 8; int bits_left = scan_size; int cur_byte = 0; - while (num_bytes-- > 1) - { + while (num_bytes-- > 1) { buffer[cur_byte++] = buffer_read(); bits_left -= 8; } @@ -778,10 +751,11 @@ static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size /* There is one more partial byte left from the clock data in/out instructions */ if (bits_left > 1) - { buffer[cur_byte] = buffer_read() >> 1; - } - /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */ + /* This shift depends on the length of the + *clock data to tms instruction, insterted + *at end of the scan, now fixed to a two + *step transition in ft2232_add_scan */ buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left); } @@ -789,13 +763,14 @@ static void ft2232_debug_dump_buffer(void) { int i; char line[256]; - char* line_p = line; - - for (i = 0; i < ft2232_buffer_size; i++) - { - line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]); - if (i % 16 == 15) - { + char *line_p = line; + + for (i = 0; i < ft2232_buffer_size; i++) { + line_p += snprintf(line_p, + sizeof(line) - (line_p - line), + "%2.2x ", + ft2232_buffer[i]); + if (i % 16 == 15) { LOG_DEBUG("%s", line); line_p = line; } @@ -805,19 +780,19 @@ static void ft2232_debug_dump_buffer(void) LOG_DEBUG("%s", line); } -static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last) +static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last) { - struct jtag_command* cmd; - uint8_t* buffer; + struct jtag_command *cmd; + uint8_t *buffer; int scan_size; - enum scan_type type; + enum scan_type type; int retval; uint32_t bytes_written = 0; uint32_t bytes_read = 0; #ifdef _DEBUG_USB_IO_ - struct timeval start, inter, inter2, end; - struct timeval d_inter, d_inter2, d_end; + struct timeval start, inter, inter2, end; + struct timeval d_inter, d_inter2, d_end; #endif #ifdef _DEBUG_USB_COMMS_ @@ -829,8 +804,8 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* gettimeofday(&start, NULL); #endif - if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK) - { + retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't write MPSSE commands to FT2232"); return retval; } @@ -839,8 +814,7 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* gettimeofday(&inter, NULL); #endif - if (ft2232_expect_read) - { + if (ft2232_expect_read) { /* FIXME this "timeout" is never changed ... */ int timeout = LIBFTDI_READ_RETRY_COUNT; ft2232_buffer_size = 0; @@ -849,8 +823,8 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* gettimeofday(&inter2, NULL); #endif - if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK) - { + retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read); + if (retval != ERROR_OK) { LOG_ERROR("couldn't read from FT2232"); return retval; } @@ -870,14 +844,13 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* ft2232_buffer_size = bytes_read; - if (ft2232_expect_read != ft2232_buffer_size) - { + if (ft2232_expect_read != ft2232_buffer_size) { LOG_ERROR("ft2232_expect_read (%i) != " - "ft2232_buffer_size (%i) " - "(%i retries)", - ft2232_expect_read, - ft2232_buffer_size, - LIBFTDI_READ_RETRY_COUNT - timeout); + "ft2232_buffer_size (%i) " + "(%i retries)", + ft2232_expect_read, + ft2232_buffer_size, + LIBFTDI_READ_RETRY_COUNT - timeout); ft2232_debug_dump_buffer(); exit(-1); @@ -885,8 +858,8 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* #ifdef _DEBUG_USB_COMMS_ LOG_DEBUG("read buffer (%i retries): %i bytes", - LIBFTDI_READ_RETRY_COUNT - timeout, - ft2232_buffer_size); + LIBFTDI_READ_RETRY_COUNT - timeout, + ft2232_buffer_size); ft2232_debug_dump_buffer(); #endif } @@ -900,25 +873,22 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* retval = ERROR_OK; cmd = first; - while (cmd != last) - { - switch (cmd->type) - { - case JTAG_SCAN: - type = jtag_scan_type(cmd->cmd.scan); - if (type != SCAN_OUT) - { - scan_size = jtag_scan_size(cmd->cmd.scan); - buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1); - ft2232_read_scan(type, buffer, scan_size); - if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK) - retval = ERROR_JTAG_QUEUE_FAILED; - free(buffer); - } - break; - - default: - break; + while (cmd != last) { + switch (cmd->type) { + case JTAG_SCAN: + type = jtag_scan_type(cmd->cmd.scan); + if (type != SCAN_OUT) { + scan_size = jtag_scan_size(cmd->cmd.scan); + buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1); + ft2232_read_scan(type, buffer, scan_size); + if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK) + retval = ERROR_JTAG_QUEUE_FAILED; + free(buffer); + } + break; + + default: + break; } cmd = cmd->next; @@ -938,7 +908,7 @@ static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* * ending with the last state at path[num_states-1] * @param num_states is the count of state steps to move through */ -static void ft2232_add_pathmove(tap_state_t* path, int num_states) +static void ft2232_add_pathmove(tap_state_t *path, int num_states) { int state_count = 0; @@ -947,11 +917,10 @@ static void ft2232_add_pathmove(tap_state_t* path, int num_states) DEBUG_JTAG_IO("-"); /* this loop verifies that the path is legal and logs each state in the path */ - while (num_states) - { - unsigned char tms_byte = 0; /* zero this on each MPSSE batch */ - int bit_count = 0; - int num_states_batch = num_states > 7 ? 7 : num_states; + while (num_states) { + unsigned char tms_byte = 0; /* zero this on each MPSSE batch */ + int bit_count = 0; + int num_states_batch = num_states > 7 ? 7 : num_states; /* command "Clock Data to TMS/CS Pin (no Read)" */ buffer_write(0x4b); @@ -961,17 +930,15 @@ static void ft2232_add_pathmove(tap_state_t* path, int num_states) while (num_states_batch--) { /* either TMS=0 or TMS=1 must work ... */ - if (tap_state_transition(tap_get_state(), false) - == path[state_count]) + if (tap_state_transition(tap_get_state(), false) == path[state_count]) buf_set_u32(&tms_byte, bit_count++, 1, 0x0); - else if (tap_state_transition(tap_get_state(), true) - == path[state_count]) + else if (tap_state_transition(tap_get_state(), true) == path[state_count]) buf_set_u32(&tms_byte, bit_count++, 1, 0x1); /* ... or else the caller goofed BADLY */ else { LOG_ERROR("BUG: %s -> %s isn't a valid " - "TAP state transition", + "TAP state transition", tap_state_name(tap_get_state()), tap_state_name(path[state_count])); exit(-1); @@ -987,70 +954,52 @@ static void ft2232_add_pathmove(tap_state_t* path, int num_states) tap_set_end_state(tap_get_state()); } -static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size) +static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size) { int num_bytes = (scan_size + 7) / 8; int bits_left = scan_size; int cur_byte = 0; int last_bit; - if (!ir_scan) - { + if (!ir_scan) { if (tap_get_state() != TAP_DRSHIFT) - { move_to_state(TAP_DRSHIFT); - } - } - else - { + } else { if (tap_get_state() != TAP_IRSHIFT) - { move_to_state(TAP_IRSHIFT); - } } /* add command for complete bytes */ - while (num_bytes > 1) - { + while (num_bytes > 1) { int thisrun_bytes; - if (type == SCAN_IO) - { + if (type == SCAN_IO) { /* Clock Data Bytes In and Out LSB First */ buffer_write(0x39); /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */ - } - else if (type == SCAN_OUT) - { + } else if (type == SCAN_OUT) { /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */ buffer_write(0x19); /* LOG_DEBUG("added TDI bytes (o)"); */ - } - else if (type == SCAN_IN) - { + } else if (type == SCAN_IN) { /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */ buffer_write(0x28); /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */ } thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1); - num_bytes -= thisrun_bytes; + num_bytes -= thisrun_bytes; buffer_write((uint8_t) (thisrun_bytes - 1)); buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8)); - if (type != SCAN_IN) - { + if (type != SCAN_IN) { /* add complete bytes */ - while (thisrun_bytes-- > 0) - { + while (thisrun_bytes-- > 0) { buffer_write(buffer[cur_byte++]); bits_left -= 8; } - } - else /* (type == SCAN_IN) */ - { + } else /* (type == SCAN_IN) */ bits_left -= 8 * (thisrun_bytes); - } } /* the most signifcant bit is scanned during TAP movement */ @@ -1060,22 +1009,16 @@ static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, last_bit = 0; /* process remaining bits but the last one */ - if (bits_left > 1) - { - if (type == SCAN_IO) - { + if (bits_left > 1) { + if (type == SCAN_IO) { /* Clock Data Bits In and Out LSB First */ buffer_write(0x3b); /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */ - } - else if (type == SCAN_OUT) - { + } else if (type == SCAN_OUT) { /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */ buffer_write(0x1b); /* LOG_DEBUG("added TDI bits (o)"); */ - } - else if (type == SCAN_IN) - { + } else if (type == SCAN_IN) { /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */ buffer_write(0x2a); /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */ @@ -1087,47 +1030,37 @@ static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, } if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT)) - || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) - { - if (type == SCAN_IO) - { + || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) { + if (type == SCAN_IO) { /* Clock Data Bits In and Out LSB First */ buffer_write(0x3b); /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */ - } - else if (type == SCAN_OUT) - { + } else if (type == SCAN_OUT) { /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */ buffer_write(0x1b); /* LOG_DEBUG("added TDI bits (o)"); */ - } - else if (type == SCAN_IN) - { + } else if (type == SCAN_IN) { /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */ buffer_write(0x2a); /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */ } buffer_write(0x0); buffer_write(last_bit); - } - else - { + } else { int tms_bits; int tms_count; - uint8_t mpsse_cmd; + uint8_t mpsse_cmd; /* move from Shift-IR/DR to end state */ - if (type != SCAN_OUT) - { - /* We always go to the PAUSE state in two step at the end of an IN or IO scan */ - /* This must be coordinated with the bit shifts in ft2232_read_scan */ + if (type != SCAN_OUT) { + /* We always go to the PAUSE state in two step at the end of an IN or IO + *scan + * This must be coordinated with the bit shifts in ft2232_read_scan */ tms_bits = 0x01; tms_count = 2; /* Clock Data to TMS/CS Pin with Read */ mpsse_cmd = 0x6b; - } - else - { + } else { tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state()); tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); /* Clock Data to TMS/CS Pin (no Read) */ @@ -1139,63 +1072,55 @@ static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, } if (tap_get_state() != tap_get_end_state()) - { move_to_state(tap_get_end_state()); - } } -static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size) +static int ft2232_large_scan(struct scan_command *cmd, + enum scan_type type, + uint8_t *buffer, + int scan_size) { int num_bytes = (scan_size + 7) / 8; int bits_left = scan_size; int cur_byte = 0; int last_bit; - uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8)); - uint8_t* receive_pointer = receive_buffer; + uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8)); + uint8_t *receive_pointer = receive_buffer; uint32_t bytes_written; uint32_t bytes_read; int retval; int thisrun_read = 0; - if (cmd->ir_scan) - { + if (cmd->ir_scan) { LOG_ERROR("BUG: large IR scans are not supported"); exit(-1); } if (tap_get_state() != TAP_DRSHIFT) - { move_to_state(TAP_DRSHIFT); - } - if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK) - { + retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't write MPSSE commands to FT2232"); exit(-1); } LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", - ft2232_buffer_size, (int)bytes_written); + ft2232_buffer_size, (int)bytes_written); ft2232_buffer_size = 0; /* add command for complete bytes */ - while (num_bytes > 1) - { + while (num_bytes > 1) { int thisrun_bytes; - if (type == SCAN_IO) - { + if (type == SCAN_IO) { /* Clock Data Bytes In and Out LSB First */ buffer_write(0x39); /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */ - } - else if (type == SCAN_OUT) - { + } else if (type == SCAN_OUT) { /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */ buffer_write(0x19); /* LOG_DEBUG("added TDI bytes (o)"); */ - } - else if (type == SCAN_IN) - { + } else if (type == SCAN_IN) { /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */ buffer_write(0x28); /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */ @@ -1207,41 +1132,35 @@ static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint buffer_write((uint8_t) (thisrun_bytes - 1)); buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8)); - if (type != SCAN_IN) - { + if (type != SCAN_IN) { /* add complete bytes */ - while (thisrun_bytes-- > 0) - { + while (thisrun_bytes-- > 0) { buffer_write(buffer[cur_byte]); cur_byte++; bits_left -= 8; } - } - else /* (type == SCAN_IN) */ - { + } else /* (type == SCAN_IN) */ bits_left -= 8 * (thisrun_bytes); - } - if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK) - { + retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't write MPSSE commands to FT2232"); exit(-1); } LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", - ft2232_buffer_size, - (int)bytes_written); + ft2232_buffer_size, + (int)bytes_written); ft2232_buffer_size = 0; - if (type != SCAN_OUT) - { - if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK) - { + if (type != SCAN_OUT) { + retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read); + if (retval != ERROR_OK) { LOG_ERROR("couldn't read from FT2232"); exit(-1); } LOG_DEBUG("thisrun_read: %i, bytes_read: %i", - thisrun_read, - (int)bytes_read); + thisrun_read, + (int)bytes_read); receive_pointer += bytes_read; } } @@ -1255,22 +1174,16 @@ static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint last_bit = 0; /* process remaining bits but the last one */ - if (bits_left > 1) - { - if (type == SCAN_IO) - { + if (bits_left > 1) { + if (type == SCAN_IO) { /* Clock Data Bits In and Out LSB First */ buffer_write(0x3b); /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */ - } - else if (type == SCAN_OUT) - { + } else if (type == SCAN_OUT) { /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */ buffer_write(0x1b); /* LOG_DEBUG("added TDI bits (o)"); */ - } - else if (type == SCAN_IN) - { + } else if (type == SCAN_IN) { /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */ buffer_write(0x2a); /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */ @@ -1283,44 +1196,33 @@ static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint thisrun_read += 2; } - if (tap_get_end_state() == TAP_DRSHIFT) - { - if (type == SCAN_IO) - { + if (tap_get_end_state() == TAP_DRSHIFT) { + if (type == SCAN_IO) { /* Clock Data Bits In and Out LSB First */ buffer_write(0x3b); /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */ - } - else if (type == SCAN_OUT) - { + } else if (type == SCAN_OUT) { /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */ buffer_write(0x1b); /* LOG_DEBUG("added TDI bits (o)"); */ - } - else if (type == SCAN_IN) - { + } else if (type == SCAN_IN) { /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */ buffer_write(0x2a); /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */ } buffer_write(0x0); buffer_write(last_bit); - } - else - { + } else { int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state()); int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - uint8_t mpsse_cmd; + uint8_t mpsse_cmd; /* move from Shift-IR/DR to end state */ - if (type != SCAN_OUT) - { + if (type != SCAN_OUT) { /* Clock Data to TMS/CS Pin with Read */ mpsse_cmd = 0x6b; /* LOG_DEBUG("added TMS scan (read)"); */ - } - else - { + } else { /* Clock Data to TMS/CS Pin (no Read) */ mpsse_cmd = 0x4b; /* LOG_DEBUG("added TMS scan (no read)"); */ @@ -1333,26 +1235,25 @@ static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint if (type != SCAN_OUT) thisrun_read += 1; - if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK) - { + retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't write MPSSE commands to FT2232"); exit(-1); } LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", - ft2232_buffer_size, - (int)bytes_written); + ft2232_buffer_size, + (int)bytes_written); ft2232_buffer_size = 0; - if (type != SCAN_OUT) - { - if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK) - { + if (type != SCAN_OUT) { + retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read); + if (retval != ERROR_OK) { LOG_ERROR("couldn't read from FT2232"); exit(-1); } LOG_DEBUG("thisrun_read: %i, bytes_read: %i", - thisrun_read, - (int)bytes_read); + thisrun_read, + (int)bytes_read); } return ERROR_OK; @@ -1364,19 +1265,17 @@ static int ft2232_predict_scan_out(int scan_size, enum scan_type type) int num_bytes = (scan_size - 1) / 8; if (tap_get_state() != TAP_DRSHIFT) - predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT)); + predicted_size += get_tms_buffer_requirements( + tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT)); - if (type == SCAN_IN) /* only from device to host */ - { + if (type == SCAN_IN) { /* only from device to host */ /* complete bytes */ predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3; /* remaining bits - 1 (up to 7) */ predicted_size += ((scan_size - 1) % 8) ? 2 : 0; - } - else /* host to device, or bidirectional */ - { - /* complete bytes */ + } else {/* host to device, or bidirectional + * complete bytes */ predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3; /* remaining bits -1 (up to 7) */ @@ -1390,10 +1289,10 @@ static int ft2232_predict_scan_in(int scan_size, enum scan_type type) { int predicted_size = 0; - if (type != SCAN_OUT) - { + if (type != SCAN_OUT) { /* complete bytes */ - predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0; + predicted_size += + (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0; /* remaining bits - 1 */ predicted_size += ((scan_size - 1) % 8) ? 1 : 0; @@ -1411,30 +1310,25 @@ static int ft2232_predict_scan_in(int scan_size, enum scan_type type) static void ftx23_reset(int trst, int srst) { enum reset_types jtag_reset_config = jtag_get_reset_config(); - if (trst == 1) - { + if (trst == 1) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) low_direction |= nTRSTnOE; /* switch to output pin (output is low) */ else low_output &= ~nTRST; /* switch output low */ - } - else if (trst == 0) - { + } else if (trst == 0) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */ + low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal + *and external pullup) */ else low_output |= nTRST; /* switch output high */ } - if (srst == 1) - { + if (srst == 1) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) low_output &= ~nSRST; /* switch output low */ else low_direction |= nSRSTnOE; /* switch to output pin (output is low) */ - } - else if (srst == 0) - { + } else if (srst == 0) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) low_output |= nSRST; /* switch output high */ else @@ -1450,30 +1344,24 @@ static void ftx23_reset(int trst, int srst) static void jtagkey_reset(int trst, int srst) { enum reset_types jtag_reset_config = jtag_get_reset_config(); - if (trst == 1) - { + if (trst == 1) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) high_output &= ~nTRSTnOE; else high_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) high_output |= nTRSTnOE; else high_output |= nTRST; } - if (srst == 1) - { + if (srst == 1) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) high_output &= ~nSRST; else high_output &= ~nSRSTnOE; - } - else if (srst == 0) - { + } else if (srst == 0) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) high_output |= nSRST; else @@ -1484,22 +1372,22 @@ static void jtagkey_reset(int trst, int srst) buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void olimex_jtag_reset(int trst, int srst) { enum reset_types jtag_reset_config = jtag_get_reset_config(); - if (trst == 1) - { + if (trst == 1) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) high_output &= ~nTRSTnOE; else high_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) high_output |= nTRSTnOE; else @@ -1507,76 +1395,66 @@ static void olimex_jtag_reset(int trst, int srst) } if (srst == 1) - { high_output |= nSRST; - } else if (srst == 0) - { high_output &= ~nSRST; - } /* command "set data bits high byte" */ buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void axm0432_jtag_reset(int trst, int srst) { - if (trst == 1) - { + if (trst == 1) { tap_set_state(TAP_RESET); high_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) high_output |= nTRST; - } if (srst == 1) - { high_output &= ~nSRST; - } else if (srst == 0) - { high_output |= nSRST; - } /* command "set data bits low byte" */ buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void flyswatter_reset(int trst, int srst) { if (trst == 1) - { low_output &= ~nTRST; - } else if (trst == 0) - { low_output |= nTRST; - } if (srst == 1) - { low_output |= nSRST; - } else if (srst == 0) - { low_output &= ~nSRST; - } /* command "set data bits low byte" */ buffer_write(0x80); buffer_write(low_output); buffer_write(low_direction); - LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction); + LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", + trst, + srst, + low_output, + low_direction); } static void flyswatter1_reset(int trst, int srst) @@ -1592,19 +1470,19 @@ static void flyswatter2_reset(int trst, int srst) static void minimodule_reset(int trst, int srst) { if (srst == 1) - { low_output &= ~nSRST; - } else if (srst == 0) - { low_output |= nSRST; - } /* command "set data bits low byte" */ buffer_write(0x80); buffer_write(low_output); buffer_write(low_direction); - LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction); + LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", + trst, + srst, + low_output, + low_direction); } static void turtle_reset(int trst, int srst) @@ -1612,68 +1490,54 @@ static void turtle_reset(int trst, int srst) trst = trst; if (srst == 1) - { low_output |= nSRST; - } else if (srst == 0) - { low_output &= ~nSRST; - } /* command "set data bits low byte" */ buffer_write(0x80); buffer_write(low_output); buffer_write(low_direction); - LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction); + LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", + srst, + low_output, + low_direction); } static void comstick_reset(int trst, int srst) { if (trst == 1) - { high_output &= ~nTRST; - } else if (trst == 0) - { high_output |= nTRST; - } if (srst == 1) - { high_output &= ~nSRST; - } else if (srst == 0) - { high_output |= nSRST; - } /* command "set data bits high byte" */ buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void stm32stick_reset(int trst, int srst) { if (trst == 1) - { high_output &= ~nTRST; - } else if (trst == 0) - { high_output |= nTRST; - } if (srst == 1) - { low_output &= ~nSRST; - } else if (srst == 0) - { low_output |= nSRST; - } /* command "set data bits low byte" */ buffer_write(0x80); @@ -1684,8 +1548,11 @@ static void stm32stick_reset(int trst, int srst) buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void sheevaplug_reset(int trst, int srst) @@ -1704,67 +1571,55 @@ static void sheevaplug_reset(int trst, int srst) buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void redbee_reset(int trst, int srst) { - if (trst == 1) - { + if (trst == 1) { tap_set_state(TAP_RESET); high_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) high_output |= nTRST; - } if (srst == 1) - { high_output &= ~nSRST; - } else if (srst == 0) - { high_output |= nSRST; - } /* command "set data bits low byte" */ buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, " - "high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + "high_direction: 0x%2.2x", trst, srst, high_output, + high_direction); } static void xds100v2_reset(int trst, int srst) { - if (trst == 1) - { + if (trst == 1) { tap_set_state(TAP_RESET); high_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) high_output |= nTRST; - } if (srst == 1) - { high_output |= nSRST; - } else if (srst == 0) - { high_output &= ~nSRST; - } /* command "set data bits low byte" */ buffer_write(0x82); buffer_write(high_output); buffer_write(high_direction); LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, " - "high_direction: 0x%2.2x", trst, srst, high_output, - high_direction); + "high_direction: 0x%2.2x", trst, srst, high_output, + high_direction); } static int ft2232_execute_runtest(struct jtag_command *cmd) @@ -1775,8 +1630,8 @@ static int ft2232_execute_runtest(struct jtag_command *cmd) retval = ERROR_OK; DEBUG_JTAG_IO("runtest %i cycles, end in %s", - cmd->cmd.runtest->num_cycles, - tap_state_name(cmd->cmd.runtest->end_state)); + cmd->cmd.runtest->num_cycles, + tap_state_name(cmd->cmd.runtest->end_state)); /* only send the maximum buffer size that FT2232C can handle */ predicted_size = 0; @@ -1787,21 +1642,18 @@ static int ft2232_execute_runtest(struct jtag_command *cmd) predicted_size += 3; if (tap_get_end_state() != TAP_IDLE) predicted_size += 3; - if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) - { + if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) { if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; require_send = 0; first_unsent = cmd; } - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { move_to_state(TAP_IDLE); require_send = 1; } i = cmd->cmd.runtest->num_cycles; - while (i > 0) - { + while (i > 0) { /* there are no state transitions in this code, so omit state tracking */ /* command "Clock Data to TMS/CS Pin (no Read)" */ @@ -1820,29 +1672,26 @@ static int ft2232_execute_runtest(struct jtag_command *cmd) ft2232_end_state(cmd->cmd.runtest->end_state); if (tap_get_state() != tap_get_end_state()) - { move_to_state(tap_get_end_state()); - } require_send = 1; DEBUG_JTAG_IO("runtest: %i, end in %s", - cmd->cmd.runtest->num_cycles, - tap_state_name(tap_get_end_state())); + cmd->cmd.runtest->num_cycles, + tap_state_name(tap_get_end_state())); return retval; } static int ft2232_execute_statemove(struct jtag_command *cmd) { - int predicted_size = 0; - int retval = ERROR_OK; + int predicted_size = 0; + int retval = ERROR_OK; DEBUG_JTAG_IO("statemove end in %s", - tap_state_name(cmd->cmd.statemove->end_state)); + tap_state_name(cmd->cmd.statemove->end_state)); /* only send the maximum buffer size that FT2232C can handle */ predicted_size = 3; - if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) - { + if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) { if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; require_send = 0; @@ -1858,9 +1707,8 @@ static int ft2232_execute_statemove(struct jtag_command *cmd) clock_tms(0x4b, 0xff, 5, 0); require_send = 1; - /* shortest-path move to desired end state */ - } else if (tap_get_state() != tap_get_end_state()) - { + /* shortest-path move to desired end state */ + } else if (tap_get_state() != tap_get_end_state()) { move_to_state(tap_get_end_state()); require_send = 1; } @@ -1874,10 +1722,10 @@ static int ft2232_execute_statemove(struct jtag_command *cmd) */ static int ft2232_execute_tms(struct jtag_command *cmd) { - int retval = ERROR_OK; - unsigned num_bits = cmd->cmd.tms->num_bits; - const uint8_t *bits = cmd->cmd.tms->bits; - unsigned count; + int retval = ERROR_OK; + unsigned num_bits = cmd->cmd.tms->num_bits; + const uint8_t *bits = cmd->cmd.tms->bits; + unsigned count; DEBUG_JTAG_IO("TMS: %d bits", num_bits); @@ -1936,20 +1784,19 @@ static int ft2232_execute_tms(struct jtag_command *cmd) static int ft2232_execute_pathmove(struct jtag_command *cmd) { - int predicted_size = 0; - int retval = ERROR_OK; + int predicted_size = 0; + int retval = ERROR_OK; - tap_state_t* path = cmd->cmd.pathmove->path; - int num_states = cmd->cmd.pathmove->num_states; + tap_state_t *path = cmd->cmd.pathmove->path; + int num_states = cmd->cmd.pathmove->num_states; DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states, - tap_state_name(tap_get_state()), - tap_state_name(path[num_states-1])); + tap_state_name(tap_get_state()), + tap_state_name(path[num_states-1])); /* only send the maximum buffer size that FT2232C can handle */ predicted_size = 3 * DIV_ROUND_UP(num_states, 7); - if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) - { + if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) { if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; @@ -1965,20 +1812,19 @@ static int ft2232_execute_pathmove(struct jtag_command *cmd) static int ft2232_execute_scan(struct jtag_command *cmd) { - uint8_t* buffer; + uint8_t *buffer; int scan_size; /* size of IR or DR scan */ int predicted_size = 0; int retval = ERROR_OK; - enum scan_type type = jtag_scan_type(cmd->cmd.scan); + enum scan_type type = jtag_scan_type(cmd->cmd.scan); DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type); scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer); predicted_size = ft2232_predict_scan_out(scan_size, type); - if ((predicted_size + 1) > FT2232_BUFFER_SIZE) - { + if ((predicted_size + 1) > FT2232_BUFFER_SIZE) { LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)"); /* unsent commands before this */ if (first_unsent != cmd) @@ -1993,12 +1839,11 @@ static int ft2232_execute_scan(struct jtag_command *cmd) if (buffer) free(buffer); return retval; - } - else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) - { - LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", - first_unsent, - cmd); + } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) { + LOG_DEBUG( + "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", + first_unsent, + cmd); if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; require_send = 0; @@ -2012,8 +1857,8 @@ static int ft2232_execute_scan(struct jtag_command *cmd) if (buffer) free(buffer); DEBUG_JTAG_IO("%s scan, %i bits, end in %s", - (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, - tap_state_name(tap_get_end_state())); + (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, + tap_state_name(tap_get_end_state())); return retval; } @@ -2025,28 +1870,26 @@ static int ft2232_execute_reset(struct jtag_command *cmd) retval = ERROR_OK; DEBUG_JTAG_IO("reset trst: %i srst %i", - cmd->cmd.reset->trst, cmd->cmd.reset->srst); + cmd->cmd.reset->trst, cmd->cmd.reset->srst); /* only send the maximum buffer size that FT2232C can handle */ predicted_size = 3; - if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) - { + if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) { if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; require_send = 0; first_unsent = cmd; } - if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) - { + if ((cmd->cmd.reset->trst == 1) || + (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) tap_set_state(TAP_RESET); - } layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); require_send = 1; DEBUG_JTAG_IO("trst: %i, srst: %i", - cmd->cmd.reset->trst, cmd->cmd.reset->srst); + cmd->cmd.reset->trst, cmd->cmd.reset->srst); return retval; } @@ -2058,12 +1901,12 @@ static int ft2232_execute_sleep(struct jtag_command *cmd) DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us); if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) - retval = ERROR_JTAG_QUEUE_FAILED; + retval = ERROR_JTAG_QUEUE_FAILED; first_unsent = cmd->next; jtag_sleep(cmd->cmd.sleep->us); DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s", - cmd->cmd.sleep->us, - tap_state_name(tap_get_state())); + cmd->cmd.sleep->us, + tap_state_name(tap_get_state())); return retval; } @@ -2078,8 +1921,8 @@ static int ft2232_execute_stableclocks(struct jtag_command *cmd) if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; DEBUG_JTAG_IO("clocks %i while in %s", - cmd->cmd.stableclocks->num_cycles, - tap_state_name(tap_get_state())); + cmd->cmd.stableclocks->num_cycles, + tap_state_name(tap_get_state())); return retval; } @@ -2087,29 +1930,42 @@ static int ft2232_execute_command(struct jtag_command *cmd) { int retval; - switch (cmd->type) - { - case JTAG_RESET: retval = ft2232_execute_reset(cmd); break; - case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break; - case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break; - case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break; - case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break; - case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break; - case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break; - case JTAG_TMS: - retval = ft2232_execute_tms(cmd); - break; - default: - LOG_ERROR("BUG: unknown JTAG command type encountered"); - retval = ERROR_JTAG_QUEUE_FAILED; - break; - } - return retval; -} - -static int ft2232_execute_queue(void) -{ - struct jtag_command* cmd = jtag_command_queue; /* currently processed command */ + switch (cmd->type) { + case JTAG_RESET: + retval = ft2232_execute_reset(cmd); + break; + case JTAG_RUNTEST: + retval = ft2232_execute_runtest(cmd); + break; + case JTAG_TLR_RESET: + retval = ft2232_execute_statemove(cmd); + break; + case JTAG_PATHMOVE: + retval = ft2232_execute_pathmove(cmd); + break; + case JTAG_SCAN: + retval = ft2232_execute_scan(cmd); + break; + case JTAG_SLEEP: + retval = ft2232_execute_sleep(cmd); + break; + case JTAG_STABLECLOCKS: + retval = ft2232_execute_stableclocks(cmd); + break; + case JTAG_TMS: + retval = ft2232_execute_tms(cmd); + break; + default: + LOG_ERROR("BUG: unknown JTAG command type encountered"); + retval = ERROR_JTAG_QUEUE_FAILED; + break; + } + return retval; +} + +static int ft2232_execute_queue(void) +{ + struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ int retval; first_unsent = cmd; /* next command that has to be sent */ @@ -2127,8 +1983,7 @@ static int ft2232_execute_queue(void) if (layout->blink) layout->blink(); - while (cmd) - { + while (cmd) { /* fill the write buffer with the desired command */ if (ft2232_execute_command(cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; @@ -2139,10 +1994,11 @@ static int ft2232_execute_queue(void) */ cmd = cmd->next; - if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE ) - { - if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1) ) - LOG_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read,(FT2232_BUFFER_READ_QUEUE_SIZE+1)); + if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) { + if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1)) + LOG_DEBUG("read buffer size looks too high %d/%d", + ft2232_expect_read, + (FT2232_BUFFER_READ_QUEUE_SIZE+1)); if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; first_unsent = cmd; @@ -2157,51 +2013,48 @@ static int ft2232_execute_queue(void) } #if BUILD_FT2232_FTD2XX == 1 -static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more) +static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more) { - FT_STATUS status; - DWORD deviceID; - char SerialNumber[16]; - char Description[64]; - DWORD openex_flags = 0; - char* openex_string = NULL; - uint8_t latency_timer; + FT_STATUS status; + DWORD deviceID; + char SerialNumber[16]; + char Description[64]; + DWORD openex_flags = 0; + char *openex_string = NULL; + uint8_t latency_timer; if (layout == NULL) { LOG_WARNING("No ft2232 layout specified'"); return ERROR_JTAG_INIT_FAILED; } - LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid); + LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", + layout->name, vid, pid); #if IS_WIN32 == 0 /* Add non-standard Vid/Pid to the linux driver */ - if ((status = FT_SetVIDPID(vid, pid)) != FT_OK) - { + status = FT_SetVIDPID(vid, pid); + if (status != FT_OK) LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid); - } + #endif - if (ft2232_device_desc && ft2232_serial) - { - LOG_WARNING("can't open by device description and serial number, giving precedence to serial"); + if (ft2232_device_desc && ft2232_serial) { + LOG_WARNING( + "can't open by device description and serial number, giving precedence to serial"); ft2232_device_desc = NULL; } - if (ft2232_device_desc) - { + if (ft2232_device_desc) { openex_string = ft2232_device_desc; openex_flags = FT_OPEN_BY_DESCRIPTION; - } - else if (ft2232_serial) - { + } else if (ft2232_serial) { openex_string = ft2232_serial; openex_flags = FT_OPEN_BY_SERIAL_NUMBER; - } - else - { + } else { LOG_ERROR("neither device description nor serial number specified"); - LOG_ERROR("please add \"ft2232_device_desc \" or \"ft2232_serial \" to your .cfg file"); + LOG_ERROR( + "please add \"ft2232_device_desc \" or \"ft2232_serial \" to your .cfg file"); return ERROR_JTAG_INIT_FAILED; } @@ -2221,29 +2074,26 @@ static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_mor /* drat, give the user a meaningfull message. * telling the use we tried *BOTH* methods. */ LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'", - ft2232_device_desc, - ft2232_device_desc_A); + ft2232_device_desc, + ft2232_device_desc_A); } } } - if (status != FT_OK) - { + if (status != FT_OK) { DWORD num_devices; - if (more) - { + if (more) { LOG_WARNING("unable to open ftdi device (trying more): %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); *try_more = 1; return ERROR_JTAG_INIT_FAILED; } LOG_ERROR("unable to open ftdi device: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY); - if (status == FT_OK) - { - char** desc_array = malloc(sizeof(char*) * (num_devices + 1)); + if (status == FT_OK) { + char **desc_array = malloc(sizeof(char *) * (num_devices + 1)); uint32_t i; for (i = 0; i < num_devices; i++) @@ -2253,8 +2103,7 @@ static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_mor status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags); - if (status == FT_OK) - { + if (status == FT_OK) { LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices); for (i = 0; i < num_devices; i++) LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]); @@ -2264,68 +2113,60 @@ static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_mor free(desc_array[i]); free(desc_array); - } - else - { + } else LOG_ERROR("ListDevices: NONE"); - } return ERROR_JTAG_INIT_FAILED; } - if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK) - { + status = FT_SetLatencyTimer(ftdih, ft2232_latency); + if (status != FT_OK) { LOG_ERROR("unable to set latency timer: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } - if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK) - { + status = FT_GetLatencyTimer(ftdih, &latency_timer); + if (status != FT_OK) { /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer * so ignore errors if using this driver version */ DWORD dw_version; status = FT_GetDriverVersion(ftdih, &dw_version); LOG_ERROR("unable to get latency timer: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); if ((status == FT_OK) && (dw_version == 0x10004)) { LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \ - "with FT_GetLatencyTimer, upgrade to a newer version"); - } - else { + "with FT_GetLatencyTimer, upgrade to a newer version"); + } else return ERROR_JTAG_INIT_FAILED; - } - } - else - { + } else LOG_DEBUG("current latency timer: %i", latency_timer); - } - if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK) - { + status = FT_SetTimeouts(ftdih, 5000, 5000); + if (status != FT_OK) { LOG_ERROR("unable to set timeouts: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } - if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK) - { + status = FT_SetBitMode(ftdih, 0x0b, 2); + if (status != FT_OK) { LOG_ERROR("unable to enable bit i/o mode: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } - if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK) - { + status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, + SerialNumber, Description, NULL); + if (status != FT_OK) { LOG_ERROR("unable to get FT_GetDeviceInfo: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; - } - else - { - static const char* type_str[] = - {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"}; + } else { + static const char *type_str[] = { + "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H" + }; unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1; unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types) ? ftdi_device : FT_DEVICE_UNKNOWN; @@ -2342,20 +2183,20 @@ static int ft2232_purge_ftd2xx(void) { FT_STATUS status; - if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK) - { + status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX); + if (status != FT_OK) { LOG_ERROR("error purging ftd2xx device: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } return ERROR_OK; } -#endif /* BUILD_FT2232_FTD2XX == 1 */ +#endif /* BUILD_FT2232_FTD2XX == 1 */ #if BUILD_FT2232_LIBFTDI == 1 -static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel) +static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel) { uint8_t latency_timer; @@ -2365,27 +2206,24 @@ static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_mo } LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)", - layout->name, vid, pid); + layout->name, vid, pid); if (ftdi_init(&ftdic) < 0) return ERROR_JTAG_INIT_FAILED; /* default to INTERFACE_A */ - if(channel == INTERFACE_ANY) { channel = INTERFACE_A; } - - if (ftdi_set_interface(&ftdic, channel) < 0) - { + if (channel == INTERFACE_ANY) + channel = INTERFACE_A; + if (ftdi_set_interface(&ftdic, channel) < 0) { LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str); return ERROR_JTAG_INIT_FAILED; } /* context, vendor id, product id */ - if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, - ft2232_serial) < 0) - { + if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) { if (more) LOG_WARNING("unable to open ftdi device (trying more): %s", - ftdic.error_str); + ftdic.error_str); else LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str); *try_more = 1; @@ -2393,33 +2231,28 @@ static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_mo } /* There is already a reset in ftdi_usb_open_desc, this should be redundant */ - if (ftdi_usb_reset(&ftdic) < 0) - { + if (ftdi_usb_reset(&ftdic) < 0) { LOG_ERROR("unable to reset ftdi device"); return ERROR_JTAG_INIT_FAILED; } - if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) - { + if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) { LOG_ERROR("unable to set latency timer"); return ERROR_JTAG_INIT_FAILED; } - if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) - { + if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) { LOG_ERROR("unable to get latency timer"); return ERROR_JTAG_INIT_FAILED; - } - else - { + } else LOG_DEBUG("current latency timer: %i", latency_timer); - } - ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */ + ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */ ftdi_device = ftdic.type; - static const char* type_str[] = - {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"}; + static const char *type_str[] = { + "AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown" + }; unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1; unsigned type_index = ((unsigned)ftdi_device < no_of_known_types) ? ftdi_device : no_of_known_types; @@ -2429,8 +2262,7 @@ static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_mo static int ft2232_purge_libftdi(void) { - if (ftdi_usb_purge_buffers(&ftdic) < 0) - { + if (ftdi_usb_purge_buffers(&ftdic) < 0) { LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str); return ERROR_JTAG_INIT_FAILED; } @@ -2438,11 +2270,11 @@ static int ft2232_purge_libftdi(void) return ERROR_OK; } -#endif /* BUILD_FT2232_LIBFTDI == 1 */ +#endif /* BUILD_FT2232_LIBFTDI == 1 */ -static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction ) +static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction) { - uint8_t buf[3]; + uint8_t buf[3]; uint32_t bytes_written; buf[0] = 0x80; /* command "set data bits low byte" */ @@ -2451,8 +2283,7 @@ static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction ) LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]); - if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) - { + if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) { LOG_ERROR("couldn't initialize data bits low byte"); return ERROR_JTAG_INIT_FAILED; } @@ -2460,9 +2291,9 @@ static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction ) return ERROR_OK; } -static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction ) +static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction) { - uint8_t buf[3]; + uint8_t buf[3]; uint32_t bytes_written; buf[0] = 0x82; /* command "set data bits high byte" */ @@ -2471,8 +2302,7 @@ static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction ) LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]); - if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) - { + if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) { LOG_ERROR("couldn't initialize data bits high byte"); return ERROR_JTAG_INIT_FAILED; } @@ -2482,26 +2312,20 @@ static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction ) static int ft2232_init(void) { - uint8_t buf[1]; + uint8_t buf[1]; int retval; uint32_t bytes_written; - if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7) - { + if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7) LOG_DEBUG("ft2232 interface using 7 step jtag state transitions"); - } else - { LOG_DEBUG("ft2232 interface using shortest path jtag state transitions"); - - } if (layout == NULL) { LOG_WARNING("No ft2232 layout specified'"); return ERROR_JTAG_INIT_FAILED; } - for (int i = 0; 1; i++) - { + for (int i = 0; 1; i++) { /* * "more indicates that there are more IDs to try, so we should * not print an error for an ID mismatch (but for anything @@ -2511,7 +2335,7 @@ static int ft2232_init(void) * ID mismatch (and nothing else) and that we should proceeed * with the next ID pair. */ - int more = ft2232_vid[i + 1] || ft2232_pid[i + 1]; + int more = ft2232_vid[i + 1] || ft2232_pid[i + 1]; int try_more = 0; #if BUILD_FT2232_FTD2XX == 1 @@ -2519,7 +2343,7 @@ static int ft2232_init(void) more, &try_more); #elif BUILD_FT2232_LIBFTDI == 1 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i], - more, &try_more, layout->channel); + more, &try_more, layout->channel); #endif if (retval >= 0) break; @@ -2533,13 +2357,14 @@ static int ft2232_init(void) if (layout->init() != ERROR_OK) return ERROR_JTAG_INIT_FAILED; - if (ft2232_device_is_highspeed()) - { + if (ft2232_device_is_highspeed()) { #ifndef BUILD_FT2232_HIGHSPEED #if BUILD_FT2232_FTD2XX == 1 - LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)"); + LOG_WARNING( + "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)"); #elif BUILD_FT2232_LIBFTDI == 1 - LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)"); + LOG_WARNING( + "High Speed device found - You need a newer libftdi version (0.16 or later)"); #endif #endif /* make sure the legacy mode is disabled */ @@ -2547,9 +2372,9 @@ static int ft2232_init(void) return ERROR_JTAG_INIT_FAILED; } - buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */ - if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) - { + buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */ + retval = ft2232_write(buf, 1, &bytes_written); + if (retval != ERROR_OK) { LOG_ERROR("couldn't write to FT2232 to disable loopback"); return ERROR_JTAG_INIT_FAILED; } @@ -2579,31 +2404,24 @@ static inline void ftx232_dbus_init(void) static int ftx232_dbus_write(void) { enum reset_types jtag_reset_config = jtag_get_reset_config(); - if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { - low_direction &= ~nTRSTnOE; /* nTRST input */ - low_output &= ~nTRST; /* nTRST = 0 */ - } - else - { - low_direction |= nTRSTnOE; /* nTRST output */ - low_output |= nTRST; /* nTRST = 1 */ + if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) { + low_direction &= ~nTRSTnOE; /* nTRST input */ + low_output &= ~nTRST; /* nTRST = 0 */ + } else { + low_direction |= nTRSTnOE; /* nTRST output */ + low_output |= nTRST; /* nTRST = 1 */ } - if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { - low_direction |= nSRSTnOE; /* nSRST output */ - low_output |= nSRST; /* nSRST = 1 */ - } - else - { - low_direction &= ~nSRSTnOE; /* nSRST input */ - low_output &= ~nSRST; /* nSRST = 0 */ + if (jtag_reset_config & RESET_SRST_PUSH_PULL) { + low_direction |= nSRSTnOE; /* nSRST output */ + low_output |= nSRST; /* nSRST = 1 */ + } else { + low_direction &= ~nSRSTnOE; /* nSRST input */ + low_output &= ~nSRST; /* nSRST = 0 */ } /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 DBUS"); return ERROR_JTAG_INIT_FAILED; } @@ -2674,8 +2492,8 @@ static int icdi_jtag_init(void) /* GPIOs on second channel/interface (UART) ... */ #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */ -#define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */ -#define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */ +#define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */ +#define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */ nTRST = 0x0; nTRSTnOE = 0x00; @@ -2706,21 +2524,17 @@ static int axm0432_jtag_init(void) low_direction = 0x2b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); return ERROR_JTAG_INIT_FAILED; } - if (strcmp(layout->name, "axm0432_jtag") == 0) - { + if (strcmp(layout->name, "axm0432_jtag") == 0) { nTRST = 0x08; - nTRSTnOE = 0x0; /* No output enable for TRST*/ + nTRSTnOE = 0x0; /* No output enable for TRST*/ nSRST = 0x04; - nSRSTnOE = 0x0; /* No output enable for SRST*/ - } - else - { + nSRSTnOE = 0x0; /* No output enable for SRST*/ + } else { LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout"); exit(-1); } @@ -2730,26 +2544,17 @@ static int axm0432_jtag_init(void) enum reset_types jtag_reset_config = jtag_get_reset_config(); if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag"); - } else - { high_output |= nTRST; - } if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag"); - } else - { high_output |= nSRST; - } /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -2763,42 +2568,32 @@ static int redbee_init(void) low_direction = 0x2b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout"); return ERROR_JTAG_INIT_FAILED; } nTRST = 0x08; - nTRSTnOE = 0x0; /* No output enable for TRST*/ + nTRSTnOE = 0x0; /* No output enable for TRST*/ nSRST = 0x04; - nSRSTnOE = 0x0; /* No output enable for SRST*/ + nSRSTnOE = 0x0; /* No output enable for SRST*/ high_output = 0x0; high_direction = 0x0c; enum reset_types jtag_reset_config = jtag_get_reset_config(); if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { LOG_ERROR("can't set nTRSTOE to push-pull on redbee"); - } else - { high_output |= nTRST; - } if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { LOG_ERROR("can't set nSRST to push-pull on redbee"); - } else - { high_output |= nSRST; - } /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -2812,29 +2607,23 @@ static int jtagkey_init(void) low_direction = 0x1b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); return ERROR_JTAG_INIT_FAILED; } - if (strcmp(layout->name, "jtagkey") == 0) - { + if (strcmp(layout->name, "jtagkey") == 0) { nTRST = 0x01; nTRSTnOE = 0x4; nSRST = 0x02; nSRSTnOE = 0x08; - } - else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) - || (strcmp(layout->name, "oocdlink") == 0)) - { + } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) + || (strcmp(layout->name, "oocdlink") == 0)) { nTRST = 0x02; nTRSTnOE = 0x1; nSRST = 0x08; nSRSTnOE = 0x04; - } - else - { + } else { LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout"); exit(-1); } @@ -2843,31 +2632,24 @@ static int jtagkey_init(void) high_direction = 0x0f; enum reset_types jtag_reset_config = jtag_get_reset_config(); - if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { + if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) { high_output |= nTRSTnOE; high_output &= ~nTRST; - } - else - { + } else { high_output &= ~nTRSTnOE; high_output |= nTRST; } - if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { + if (jtag_reset_config & RESET_SRST_PUSH_PULL) { high_output &= ~nSRSTnOE; high_output |= nSRST; - } - else - { + } else { high_output |= nSRSTnOE; high_output &= ~nSRST; } /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -2881,8 +2663,7 @@ static int olimex_jtag_init(void) low_direction = 0x1b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -2890,38 +2671,30 @@ static int olimex_jtag_init(void) nTRST = 0x01; nTRSTnOE = 0x4; nSRST = 0x02; - nSRSTnOE = 0x00; /* no output enable for nSRST */ + nSRSTnOE = 0x00;/* no output enable for nSRST */ high_output = 0x0; high_direction = 0x0f; enum reset_types jtag_reset_config = jtag_get_reset_config(); - if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { + if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) { high_output |= nTRSTnOE; high_output &= ~nTRST; - } - else - { + } else { high_output &= ~nTRSTnOE; high_output |= nTRST; } if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD"); - } else - { high_output &= ~nSRST; - } /* turn red LED on */ high_output |= 0x08; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -2943,16 +2716,15 @@ static int flyswatter_init(int rev) low_direction |= 1 << 7; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout"); return ERROR_JTAG_INIT_FAILED; } nTRST = 0x10; - nTRSTnOE = 0x0; /* not output enable for nTRST */ + nTRSTnOE = 0x0; /* not output enable for nTRST */ nSRST = 0x20; - nSRSTnOE = 0x00; /* no output enable for nSRST */ + nSRSTnOE = 0x00; /* no output enable for nSRST */ high_output = 0x00; @@ -2965,8 +2737,7 @@ static int flyswatter_init(int rev) high_output |= 0x08; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -2986,12 +2757,12 @@ static int flyswatter2_init(void) static int minimodule_init(void) { - low_output = 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18) - low_direction = 0xfb;//0xfb; + low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was + * 0x18) */ + low_direction = 0xfb; /* 0xfb; */ /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3003,11 +2774,10 @@ static int minimodule_init(void) high_direction = 0x05; /* turn red LED3 on, LED2 off */ - //high_output |= 0x08; + /* high_output |= 0x08; */ /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3021,8 +2791,7 @@ static int turtle_init(void) low_direction = 0x5b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3033,8 +2802,7 @@ static int turtle_init(void) high_direction = 0x0C; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3048,23 +2816,21 @@ static int comstick_init(void) low_direction = 0x0b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout"); return ERROR_JTAG_INIT_FAILED; } nTRST = 0x01; - nTRSTnOE = 0x00; /* no output enable for nTRST */ + nTRSTnOE = 0x00; /* no output enable for nTRST */ nSRST = 0x02; - nSRSTnOE = 0x00; /* no output enable for nSRST */ + nSRSTnOE = 0x00; /* no output enable for nSRST */ high_output = 0x03; high_direction = 0x03; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3078,23 +2844,21 @@ static int stm32stick_init(void) low_direction = 0x8b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout"); return ERROR_JTAG_INIT_FAILED; } nTRST = 0x01; - nTRSTnOE = 0x00; /* no output enable for nTRST */ + nTRSTnOE = 0x00; /* no output enable for nTRST */ nSRST = 0x80; - nSRSTnOE = 0x00; /* no output enable for nSRST */ + nSRSTnOE = 0x00; /* no output enable for nSRST */ high_output = 0x01; high_direction = 0x03; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3108,8 +2872,7 @@ static int sheevaplug_init(void) low_direction = 0x1b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3131,8 +2894,7 @@ static int sheevaplug_init(void) high_output &= ~nSRST; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3146,23 +2908,21 @@ static int cortino_jtag_init(void) low_direction = 0x1b; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout"); return ERROR_JTAG_INIT_FAILED; } nTRST = 0x01; - nTRSTnOE = 0x00; /* no output enable for nTRST */ + nTRSTnOE = 0x00; /* no output enable for nTRST */ nSRST = 0x02; - nSRSTnOE = 0x00; /* no output enable for nSRST */ + nSRSTnOE = 0x00; /* no output enable for nSRST */ high_output = 0x03; high_direction = 0x03; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3183,8 +2943,7 @@ static int lisa_l_init(void) high_direction = 0x18; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3205,8 +2964,7 @@ static int flossjtag_init(void) high_direction = 0x18; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3231,28 +2989,27 @@ static int flossjtag_init(void) * * CPLD logic: Disable loopback * * ACBUS6/SPARE2 = LOOPBACK = 0 */ -#define XDS100_nEMU_EN (1<<5) -#define XDS100_nEMU0 (1<<6) +#define XDS100_nEMU_EN (1<<5) +#define XDS100_nEMU0 (1<<6) -#define XDS100_PWR_RST (1<<3) -#define XDS100_nEMU1 (1<<4) -#define XDS100_LOOPBACK (1<<6) +#define XDS100_PWR_RST (1<<3) +#define XDS100_nEMU1 (1<<4) +#define XDS100_LOOPBACK (1<<6) static int xds100v2_init(void) { /* These are in the lower byte */ nTRST = 0x10; nTRSTnOE = 0x10; - /* These aren't actually used on 14 pin connectors */ - /* These are in the upper byte */ + /* These aren't actually used on 14 pin connectors + * These are in the upper byte */ nSRST = 0x01; nSRSTnOE = 0x01; low_output = 0x08 | nTRST | XDS100_nEMU_EN; low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0; - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3261,8 +3018,7 @@ static int xds100v2_init(void) high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3270,8 +3026,7 @@ static int xds100v2_init(void) high_output |= XDS100_PWR_RST; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -3320,13 +3075,9 @@ static void turtle_jtag_blink(void) * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3 */ if (high_output & 0x08) - { high_output = 0x04; - } else - { high_output = 0x08; - } buffer_write(0x82); buffer_write(high_output); @@ -3339,13 +3090,9 @@ static void lisa_l_blink(void) * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4 */ if (high_output & 0x10) - { high_output = 0x08; - } else - { high_output = 0x10; - } buffer_write(0x82); buffer_write(high_output); @@ -3358,13 +3105,9 @@ static void flossjtag_blink(void) * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4 */ if (high_output & 0x10) - { high_output = 0x08; - } else - { high_output = 0x10; - } buffer_write(0x82); buffer_write(high_output); @@ -3393,32 +3136,28 @@ COMMAND_HANDLER(ft2232_handle_device_desc_command) { char *cp; char buf[200]; - if (CMD_ARGC == 1) - { + if (CMD_ARGC == 1) { ft2232_device_desc = strdup(CMD_ARGV[0]); cp = strchr(ft2232_device_desc, 0); /* under Win32, the FTD2XX driver appends an "A" to the end * of the description, this examines the given desc * and creates the 'missing' _A or non_A variable. */ - if ((cp[-1] == 'A') && (cp[-2]==' ')) { + if ((cp[-1] == 'A') && (cp[-2] == ' ')) { /* it was, so make this the "A" version. */ ft2232_device_desc_A = ft2232_device_desc; /* and *CREATE* the non-A version. */ strcpy(buf, ft2232_device_desc); cp = strchr(buf, 0); cp[-2] = 0; - ft2232_device_desc = strdup(buf); + ft2232_device_desc = strdup(buf); } else { /* A not defined * so create it */ sprintf(buf, "%s A", ft2232_device_desc); ft2232_device_desc_A = strdup(buf); } - } - else - { + } else LOG_ERROR("expected exactly one argument to ft2232_device_desc "); - } return ERROR_OK; } @@ -3426,29 +3165,24 @@ COMMAND_HANDLER(ft2232_handle_device_desc_command) COMMAND_HANDLER(ft2232_handle_serial_command) { if (CMD_ARGC == 1) - { ft2232_serial = strdup(CMD_ARGV[0]); - } else - { return ERROR_COMMAND_SYNTAX_ERROR; - } return ERROR_OK; } COMMAND_HANDLER(ft2232_handle_layout_command) { - if (CMD_ARGC != 1) { + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - } if (layout) { LOG_ERROR("already specified ft2232_layout %s", - layout->name); + layout->name); return (strcmp(layout->name, CMD_ARGV[0]) != 0) - ? ERROR_FAIL - : ERROR_OK; + ? ERROR_FAIL + : ERROR_OK; } for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) { @@ -3464,14 +3198,12 @@ COMMAND_HANDLER(ft2232_handle_layout_command) COMMAND_HANDLER(ft2232_handle_vid_pid_command) { - if (CMD_ARGC > MAX_USB_IDS * 2) - { + if (CMD_ARGC > MAX_USB_IDS * 2) { LOG_WARNING("ignoring extra IDs in ft2232_vid_pid " - "(maximum is %d pairs)", MAX_USB_IDS); + "(maximum is %d pairs)", MAX_USB_IDS); CMD_ARGC = MAX_USB_IDS * 2; } - if (CMD_ARGC < 2 || (CMD_ARGC & 1)) - { + if (CMD_ARGC < 2 || (CMD_ARGC & 1)) { LOG_WARNING("incomplete ft2232_vid_pid configuration directive"); if (CMD_ARGC < 2) return ERROR_COMMAND_SYNTAX_ERROR; @@ -3480,8 +3212,7 @@ COMMAND_HANDLER(ft2232_handle_vid_pid_command) } unsigned i; - for (i = 0; i < CMD_ARGC; i += 2) - { + for (i = 0; i < CMD_ARGC; i += 2) { COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]); COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]); } @@ -3498,34 +3229,28 @@ COMMAND_HANDLER(ft2232_handle_vid_pid_command) COMMAND_HANDLER(ft2232_handle_latency_command) { if (CMD_ARGC == 1) - { ft2232_latency = atoi(CMD_ARGV[0]); - } else - { - return ERROR_COMMAND_SYNTAX_ERROR; - } + return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_OK; } -static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd) +static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd) { int retval = 0; /* 7 bits of either ones or zeros. */ - uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00); + uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00); - while (num_cycles > 0) - { + while (num_cycles > 0) { /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles * at most 7 bits per invocation. Here we invoke it potentially * several times. */ int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles; - if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) - { + if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) { if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; @@ -3577,26 +3302,26 @@ static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd) * ADBUS6 - VCC * ADBUS7 - GND */ -static int icebear_jtag_init(void) { - low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */ - low_output = 0x08; /* high: TMS; low: TCK TDI */ - nTRST = 0x10; - nSRST = 0x20; +static int icebear_jtag_init(void) +{ + low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */ + low_output = 0x08; /* high: TMS; low: TCK TDI */ + nTRST = 0x10; + nSRST = 0x20; enum reset_types jtag_reset_config = jtag_get_reset_config(); - if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) { - low_direction &= ~nTRST; /* nTRST high impedance */ - } + if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) + low_direction &= ~nTRST; /* nTRST high impedance */ else { - low_direction |= nTRST; - low_output |= nTRST; + low_direction |= nTRST; + low_output |= nTRST; } - low_direction |= nSRST; - low_output |= nSRST; + low_direction |= nSRST; + low_output |= nSRST; /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)"); return ERROR_JTAG_INIT_FAILED; } @@ -3605,7 +3330,7 @@ static int icebear_jtag_init(void) { high_direction = 0x00; /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)"); return ERROR_JTAG_INIT_FAILED; } @@ -3613,33 +3338,34 @@ static int icebear_jtag_init(void) { return ERROR_OK; } -static void icebear_jtag_reset(int trst, int srst) { - +static void icebear_jtag_reset(int trst, int srst) +{ if (trst == 1) { - low_direction |= nTRST; - low_output &= ~nTRST; - } - else if (trst == 0) { + low_direction |= nTRST; + low_output &= ~nTRST; + } else if (trst == 0) { enum reset_types jtag_reset_config = jtag_get_reset_config(); if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) - low_direction &= ~nTRST; + low_direction &= ~nTRST; else - low_output |= nTRST; + low_output |= nTRST; } - if (srst == 1) { + if (srst == 1) low_output &= ~nSRST; - } - else if (srst == 0) { + else if (srst == 0) low_output |= nSRST; - } /* command "set data bits low byte" */ buffer_write(0x80); buffer_write(low_output); buffer_write(low_direction); - LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction); + LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", + trst, + srst, + low_output, + low_direction); } /* --------------------------------------------------------------------- @@ -3720,37 +3446,35 @@ static int signalyzer_h_led_set(unsigned char channel, unsigned char led, #if BUILD_FT2232_FTD2XX == 1 FT_STATUS status; - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, - ((uint32_t)(channel << 8) | led))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + ((uint32_t)(channel << 8) | led)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - (SIGNALYZER_DATA_BUFFER_ADDR + 1), - ((uint32_t)(on_time << 8) | off_time))) != FT_OK) - { + status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1), + ((uint32_t)(on_time << 8) | off_time)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - (SIGNALYZER_DATA_BUFFER_ADDR + 2), - ((uint32_t)cycles))) != FT_OK) - { + status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2), + ((uint32_t)cycles)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_LED_SET)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_LED_SET); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } @@ -3758,37 +3482,35 @@ static int signalyzer_h_led_set(unsigned char channel, unsigned char led, #elif BUILD_FT2232_LIBFTDI == 1 int retval; - if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, - ((uint32_t)(channel << 8) | led))) < 0) - { + retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + ((uint32_t)(channel << 8) | led)); + if (retval < 0) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftdi_get_error_string(&ftdic)); + ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; } - if ((retval = signalyzer_h_ctrl_write( - (SIGNALYZER_DATA_BUFFER_ADDR + 1), - ((uint32_t)(on_time << 8) | off_time))) < 0) - { + retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1), + ((uint32_t)(on_time << 8) | off_time)); + if (retval < 0) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftdi_get_error_string(&ftdic)); + ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; } - if ((retval = signalyzer_h_ctrl_write( - (SIGNALYZER_DATA_BUFFER_ADDR + 2), - (uint32_t)cycles)) < 0) - { + retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2), + (uint32_t)cycles); + if (retval < 0) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftdi_get_error_string(&ftdic)); + ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; } - if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_LED_SET)) < 0) - { + retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_LED_SET); + if (retval < 0) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftdi_get_error_string(&ftdic)); + ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; } @@ -3809,7 +3531,7 @@ static int signalyzer_h_init(void) /* turn on center green led */ signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN, - 0xFFFF, 0x00, 0x00); + 0xFFFF, 0x00, 0x00); /* determine what channel config wants to open * TODO: change me... current implementation is made to work @@ -3817,129 +3539,118 @@ static int signalyzer_h_init(void) */ end_of_desc = strrchr(ft2232_device_desc, 0x00); - if (end_of_desc) - { + if (end_of_desc) { signalyzer_h_side = *(end_of_desc - 1); if (signalyzer_h_side == 'B') signalyzer_h_side = SIGNALYZER_CHAN_B; else signalyzer_h_side = SIGNALYZER_CHAN_A; - } - else - { + } else { LOG_ERROR("No Channel was specified"); return ERROR_FAIL; } signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN, - 1000, 1000, 0xFF); + 1000, 1000, 0xFF); #if BUILD_FT2232_FTD2XX == 1 /* read signalyzer versionining information */ - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_VERSION)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_VERSION); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - for (i = 0; i < 10; i++) - { - if ((status = signalyzer_h_ctrl_read( - (SIGNALYZER_DATA_BUFFER_ADDR + i), - &read_buf[i])) != FT_OK) - { + for (i = 0; i < 10; i++) { + status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), + &read_buf[i]); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_read returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } } LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }", - read_buf[0], read_buf[1], read_buf[2], read_buf[3], - read_buf[4], read_buf[5], read_buf[6]); + read_buf[0], read_buf[1], read_buf[2], read_buf[3], + read_buf[4], read_buf[5], read_buf[6]); /* set gpio register */ - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, - (uint32_t)(signalyzer_h_side << 8))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + (uint32_t)(signalyzer_h_side << 8)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, - 0x0404)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_GPIO_STATE); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } /* read adapter type information */ - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, - ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + ((uint32_t)(signalyzer_h_side << 8) | 0x01)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK) - { + status = signalyzer_h_ctrl_write( + (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK) - { + status = signalyzer_h_ctrl_write( + (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_I2C)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_I2C); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } usleep(100000); - if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, - &read_buf[0])) != FT_OK) - { + status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_read returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } if (read_buf[0] != 0x0498) signalyzer_h_adapter_type = 0x0000; - else - { - for (i = 0; i < 4; i++) - { - if ((status = signalyzer_h_ctrl_read( - (SIGNALYZER_DATA_BUFFER_ADDR + i), - &read_buf[i])) != FT_OK) - { + else { + for (i = 0; i < 4; i++) { + status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_read returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } } @@ -3958,8 +3669,7 @@ static int signalyzer_h_init(void) enum reset_types jtag_reset_config = jtag_get_reset_config(); /* ADAPTOR: EM_LT16_A */ - if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) - { + if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) { LOG_INFO("Signalyzer: EM-LT (16-channel level translator) " "detected. (HW: %2x).", (read_buf[1] >> 8)); @@ -3974,126 +3684,108 @@ static int signalyzer_h_init(void) high_output = 0x0; high_direction = 0x0; - if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { - low_direction &= ~nTRSTnOE; /* nTRST input */ - low_output &= ~nTRST; /* nTRST = 0 */ - } - else - { - low_direction |= nTRSTnOE; /* nTRST output */ - low_output |= nTRST; /* nTRST = 1 */ + if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) { + low_direction &= ~nTRSTnOE; /* nTRST input */ + low_output &= ~nTRST; /* nTRST = 0 */ + } else { + low_direction |= nTRSTnOE; /* nTRST output */ + low_output |= nTRST; /* nTRST = 1 */ } - if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { - low_direction |= nSRSTnOE; /* nSRST output */ - low_output |= nSRST; /* nSRST = 1 */ - } - else - { - low_direction &= ~nSRSTnOE; /* nSRST input */ - low_output &= ~nSRST; /* nSRST = 0 */ + if (jtag_reset_config & RESET_SRST_PUSH_PULL) { + low_direction |= nSRSTnOE; /* nSRST output */ + low_output |= nSRST; /* nSRST = 1 */ + } else { + low_direction &= ~nSRSTnOE; /* nSRST input */ + low_output &= ~nSRST; /* nSRST = 0 */ } #if BUILD_FT2232_FTD2XX == 1 /* enable power to the module */ - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR, - ((uint32_t)(signalyzer_h_side << 8) | 0x01))) - != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + ((uint32_t)(signalyzer_h_side << 8) | 0x01)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_POWERCONTROL_SET); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } /* set gpio mode register */ - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR, - (uint32_t)(signalyzer_h_side << 8))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + (uint32_t)(signalyzer_h_side << 8)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000)) - != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } /* set gpio register */ - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR, - (uint32_t)(signalyzer_h_side << 8))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + (uint32_t)(signalyzer_h_side << 8)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040)) - != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_GPIO_STATE); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } #endif } - /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */ else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) || - (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) || - (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) || - (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) - { + (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) || + (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) || + (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) { if (signalyzer_h_adapter_type - == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) + == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) " "detected. (HW: %2x).", (read_buf[1] >> 8)); else if (signalyzer_h_adapter_type - == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) + == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) LOG_INFO("Signalyzer: EM-ARM-JTAG_P " "(ARM JTAG with PSU) detected. (HW: %2x).", (read_buf[1] >> 8)); else if (signalyzer_h_adapter_type - == SIGNALYZER_MODULE_TYPE_EM_JTAG) + == SIGNALYZER_MODULE_TYPE_EM_JTAG) LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) " "detected. (HW: %2x).", (read_buf[1] >> 8)); else if (signalyzer_h_adapter_type - == SIGNALYZER_MODULE_TYPE_EM_JTAG_P) + == SIGNALYZER_MODULE_TYPE_EM_JTAG_P) LOG_INFO("Signalyzer: EM-JTAG-P " "(Generic JTAG with PSU) detected. (HW: %2x).", (read_buf[1] >> 8)); @@ -4109,113 +3801,91 @@ static int signalyzer_h_init(void) high_output = 0x0; high_direction = 0x1f; - if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { + if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) { high_output |= nTRSTnOE; high_output &= ~nTRST; - } - else - { + } else { high_output &= ~nTRSTnOE; high_output |= nTRST; } - if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { + if (jtag_reset_config & RESET_SRST_PUSH_PULL) { high_output &= ~nSRSTnOE; high_output |= nSRST; - } - else - { + } else { high_output |= nSRSTnOE; high_output &= ~nSRST; } #if BUILD_FT2232_FTD2XX == 1 /* enable power to the module */ - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR, - ((uint32_t)(signalyzer_h_side << 8) | 0x01))) - != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + ((uint32_t)(signalyzer_h_side << 8) | 0x01)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, + SIGNALYZER_COMMAND_POWERCONTROL_SET); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } /* set gpio mode register (IO_16 and IO_17 set as analog * inputs, other is gpio) */ - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR, - (uint32_t)(signalyzer_h_side << 8))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + (uint32_t)(signalyzer_h_side << 8)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060)) - != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } /* set gpio register (all inputs, for -P modules, * PSU will be turned off) */ - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR, - (uint32_t)(signalyzer_h_side << 8))) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR, + (uint32_t)(signalyzer_h_side << 8)); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000)) - != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } - if ((status = signalyzer_h_ctrl_write( - SIGNALYZER_COMMAND_ADDR, - SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK) - { + status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE); + if (status != FT_OK) { LOG_ERROR("signalyzer_h_ctrl_write returned: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; } #endif - } - - else if (signalyzer_h_adapter_type == 0x0000) - { + } else if (signalyzer_h_adapter_type == 0x0000) { LOG_INFO("Signalyzer: No external modules were detected."); nTRST = 0x10; @@ -4229,58 +3899,45 @@ static int signalyzer_h_init(void) high_output = 0x0; high_direction = 0x0; - if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) - { - low_direction &= ~nTRSTnOE; /* nTRST input */ - low_output &= ~nTRST; /* nTRST = 0 */ - } - else - { - low_direction |= nTRSTnOE; /* nTRST output */ - low_output |= nTRST; /* nTRST = 1 */ + if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) { + low_direction &= ~nTRSTnOE; /* nTRST input */ + low_output &= ~nTRST; /* nTRST = 0 */ + } else { + low_direction |= nTRSTnOE; /* nTRST output */ + low_output |= nTRST; /* nTRST = 1 */ } - if (jtag_reset_config & RESET_SRST_PUSH_PULL) - { - low_direction |= nSRSTnOE; /* nSRST output */ - low_output |= nSRST; /* nSRST = 1 */ - } - else - { - low_direction &= ~nSRSTnOE; /* nSRST input */ - low_output &= ~nSRST; /* nSRST = 0 */ + if (jtag_reset_config & RESET_SRST_PUSH_PULL) { + low_direction |= nSRSTnOE; /* nSRST output */ + low_output |= nSRST; /* nSRST = 1 */ + } else { + low_direction &= ~nSRSTnOE; /* nSRST input */ + low_output &= ~nSRST; /* nSRST = 0 */ } - } - else - { + } else { LOG_ERROR("Unknown module type is detected: %.4x", - signalyzer_h_adapter_type); + signalyzer_h_adapter_type); return ERROR_JTAG_DEVICE_ERROR; } /* initialize low byte of controller for jtag operation */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize Signalyzer-H layout"); return ERROR_JTAG_INIT_FAILED; } #if BUILD_FT2232_FTD2XX == 1 - if (ftdi_device == FT_DEVICE_2232H) - { + if (ftdi_device == FT_DEVICE_2232H) { /* initialize high byte of controller for jtag operation */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize Signalyzer-H layout"); return ERROR_JTAG_INIT_FAILED; } } #elif BUILD_FT2232_LIBFTDI == 1 - if (ftdi_device == TYPE_2232H) - { + if (ftdi_device == TYPE_2232H) { /* initialize high byte of controller for jtag operation */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize Signalyzer-H layout"); return ERROR_JTAG_INIT_FAILED; } @@ -4294,19 +3951,15 @@ static void signalyzer_h_reset(int trst, int srst) enum reset_types jtag_reset_config = jtag_get_reset_config(); /* ADAPTOR: EM_LT16_A */ - if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) - { - if (trst == 1) - { + if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) { + if (trst == 1) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) /* switch to output pin (output is low) */ low_direction |= nTRSTnOE; else /* switch output low */ low_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) /* switch to input pin (high-Z + internal * and external pullup) */ @@ -4316,17 +3969,14 @@ static void signalyzer_h_reset(int trst, int srst) low_output |= nTRST; } - if (srst == 1) - { + if (srst == 1) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) /* switch output low */ low_output &= ~nSRST; else /* switch to output pin (output is low) */ low_direction |= nSRSTnOE; - } - else if (srst == 0) - { + } else if (srst == 0) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) /* switch output high */ low_output |= nSRST; @@ -4340,39 +3990,32 @@ static void signalyzer_h_reset(int trst, int srst) buffer_write(low_output); buffer_write(low_direction); LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, " - "low_direction: 0x%2.2x", - trst, srst, low_output, low_direction); + "low_direction: 0x%2.2x", + trst, srst, low_output, low_direction); } /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */ else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) || - (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) || - (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) || - (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) - { - if (trst == 1) - { + (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) || + (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) || + (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) { + if (trst == 1) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) high_output &= ~nTRSTnOE; else high_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) high_output |= nTRSTnOE; else high_output |= nTRST; } - if (srst == 1) - { + if (srst == 1) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) high_output &= ~nSRST; else high_output &= ~nSRSTnOE; - } - else if (srst == 0) - { + } else if (srst == 0) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) high_output |= nSRST; else @@ -4384,22 +4027,17 @@ static void signalyzer_h_reset(int trst, int srst) buffer_write(high_output); buffer_write(high_direction); LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, " - "high_direction: 0x%2.2x", - trst, srst, high_output, high_direction); - } - else if (signalyzer_h_adapter_type == 0x0000) - { - if (trst == 1) - { + "high_direction: 0x%2.2x", + trst, srst, high_output, high_direction); + } else if (signalyzer_h_adapter_type == 0x0000) { + if (trst == 1) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) /* switch to output pin (output is low) */ low_direction |= nTRSTnOE; else /* switch output low */ low_output &= ~nTRST; - } - else if (trst == 0) - { + } else if (trst == 0) { if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) /* switch to input pin (high-Z + internal * and external pullup) */ @@ -4409,17 +4047,14 @@ static void signalyzer_h_reset(int trst, int srst) low_output |= nTRST; } - if (srst == 1) - { + if (srst == 1) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) /* switch output low */ low_output &= ~nSRST; else /* switch to output pin (output is low) */ low_direction |= nSRSTnOE; - } - else if (srst == 0) - { + } else if (srst == 0) { if (jtag_reset_config & RESET_SRST_PUSH_PULL) /* switch output high */ low_output |= nSRST; @@ -4433,8 +4068,8 @@ static void signalyzer_h_reset(int trst, int srst) buffer_write(low_output); buffer_write(low_direction); LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, " - "low_direction: 0x%2.2x", - trst, srst, low_output, low_direction); + "low_direction: 0x%2.2x", + trst, srst, low_output, low_direction); } } @@ -4450,14 +4085,13 @@ static void signalyzer_h_blink(void) *******************************************************************/ static int ktlink_init(void) { - uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5) + uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */ - low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en - low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in + low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */ + low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */ /* initialize low byte for jtag */ - if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -4467,8 +4101,8 @@ static int ktlink_init(void) nTRSTnOE = 0x04; nSRSTnOE = 0x08; - high_output = 0x80; // turn LED on - high_direction = 0xFF; // all outputs + high_output = 0x80; /* turn LED on */ + high_direction = 0xFF; /* all outputs */ enum reset_types jtag_reset_config = jtag_get_reset_config(); @@ -4489,8 +4123,7 @@ static int ktlink_init(void) } /* initialize high byte for jtag */ - if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) - { + if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) { LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout"); return ERROR_JTAG_INIT_FAILED; } @@ -4526,10 +4159,14 @@ static void ktlink_reset(int trst, int srst) high_output |= nSRSTnOE; } - buffer_write(0x82); // command "set data bits high byte" + buffer_write(0x82); /* command "set data bits high byte" */ buffer_write(high_output); buffer_write(high_direction); - LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction); + LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", + trst, + srst, + high_output, + high_direction); } static void ktlink_blink(void) @@ -4537,7 +4174,7 @@ static void ktlink_blink(void) /* LED connected to ACBUS7 */ high_output ^= 0x80; - buffer_write(0x82); // command "set data bits high byte" + buffer_write(0x82); /* command "set data bits high byte" */ buffer_write(high_output); buffer_write(high_direction); } diff --git a/src/jtag/drivers/ftd2xx_common.h b/src/jtag/drivers/ftd2xx_common.h index 46bdfa262c..da858630b2 100644 --- a/src/jtag/drivers/ftd2xx_common.h +++ b/src/jtag/drivers/ftd2xx_common.h @@ -27,8 +27,7 @@ static const char *ftd2xx_status_string(FT_STATUS status) { - switch (status) - { + switch (status) { case FT_OK: return "OK"; case FT_INVALID_HANDLE: return "invalid handle"; case FT_DEVICE_NOT_FOUND: return "device not found"; diff --git a/src/jtag/drivers/gw16012.c b/src/jtag/drivers/gw16012.c index 22a37ac84f..bf027f8fe9 100644 --- a/src/jtag/drivers/gw16012.c +++ b/src/jtag/drivers/gw16012.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -24,7 +25,6 @@ #include #include - #if 1 #define _DEBUG_GW16012_IO_ #endif @@ -35,14 +35,13 @@ #include #include -#define ioperm(startport,length,enable)\ - i386_set_ioperm((startport), (length), (enable)) +#define ioperm(startport, length, enable) \ + 386_set_ioperm((startport), (length), (enable)) #else #endif /* __FreeBSD__, __FreeBSD_kernel__ */ - #if PARPORT_USE_PPDEV == 1 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #include @@ -65,14 +64,13 @@ #include #endif - /* configuration */ uint16_t gw16012_port; /* interface variables */ -static uint8_t gw16012_msb = 0x0; -static uint8_t gw16012_control_value = 0x0; +static uint8_t gw16012_msb; +static uint8_t gw16012_control_value; #if PARPORT_USE_PPDEV == 1 static int device_handle; @@ -100,8 +98,7 @@ static void gw16012_data(uint8_t value) static void gw16012_control(uint8_t value) { - if (value != gw16012_control_value) - { + if (value != gw16012_control_value) { gw16012_control_value = value; #ifdef _DEBUG_GW16012_IO_ @@ -159,8 +156,7 @@ static void gw16012_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } @@ -174,8 +170,7 @@ static void gw16012_state_move(void) gw16012_control(0x0); /* single-bit mode */ - for (i = 0; i < tms_count; i++) - { + for (i = 0; i < tms_count; i++) { tms = (tms_scan >> i) & 1; gw16012_data(tms << 1); /* output next TMS bit */ } @@ -189,20 +184,15 @@ static void gw16012_path_move(struct pathmove_command *cmd) int state_count; state_count = 0; - while (num_states) - { + while (num_states) { gw16012_control(0x0); /* single-bit mode */ if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) - { gw16012_data(0x0); /* TCK cycle with TMS low */ - } else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count]) - { gw16012_data(0x2); /* TCK cycle with TMS high */ - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count])); + else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count])); exit(-1); } @@ -220,14 +210,12 @@ static void gw16012_runtest(int num_cycles) int i; /* only do a state_move when we're not already in IDLE */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { gw16012_end_state(TAP_IDLE); gw16012_state_move(); } - for (i = 0; i < num_cycles; i++) - { + for (i = 0; i < num_cycles; i++) { gw16012_control(0x0); /* single-bit mode */ gw16012_data(0x0); /* TMS cycle with TMS low */ } @@ -245,8 +233,8 @@ static void gw16012_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int uint8_t scan_out, scan_in; /* only if we're not already in the correct Shift state */ - if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) - { + if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || + (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) { if (ir_scan) gw16012_end_state(TAP_IRSHIFT); else @@ -256,8 +244,7 @@ static void gw16012_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int gw16012_end_state(saved_end_state); } - while (type == SCAN_OUT && ((bits_left - 1) > 7)) - { + while (type == SCAN_OUT && ((bits_left - 1) > 7)) { gw16012_control(0x2); /* seven-bit mode */ scan_out = buf_get_u32(buffer, bit_count, 7); gw16012_data(scan_out); @@ -266,29 +253,22 @@ static void gw16012_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int } gw16012_control(0x0); /* single-bit mode */ - while (bits_left-- > 0) - { + while (bits_left-- > 0) { uint8_t tms = 0; scan_out = buf_get_u32(buffer, bit_count, 1); - if (bits_left == 0) /* last bit */ - { + if (bits_left == 0) /* last bit */ { if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT)) || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) - { tms = 0; - } else - { tms = 2; - } } gw16012_data(scan_out | tms); - if (type != SCAN_OUT) - { + if (type != SCAN_OUT) { gw16012_input(&scan_in); buf_set_u32(buffer, bit_count, 1, ((scan_in & 0x08) >> 3)); } @@ -297,8 +277,7 @@ static void gw16012_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int } if (!((ir_scan && (tap_get_end_state() == TAP_IRSHIFT)) || - (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))) - { + (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))) { gw16012_data(0x0); if (ir_scan) tap_set_state(TAP_IRPAUSE); @@ -323,23 +302,20 @@ static int gw16012_execute_queue(void) */ retval = ERROR_OK; - while (cmd) - { - switch (cmd->type) - { + while (cmd) { + switch (cmd->type) { case JTAG_RESET: #ifdef _DEBUG_JTAG_IO_ LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); #endif if (cmd->cmd.reset->trst == 1) - { tap_set_state(TAP_RESET); - } gw16012_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); break; case JTAG_RUNTEST: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state); + LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, + cmd->cmd.runtest->end_state); #endif gw16012_end_state(cmd->cmd.runtest->end_state); gw16012_runtest(cmd->cmd.runtest->num_cycles); @@ -353,7 +329,8 @@ static int gw16012_execute_queue(void) break; case JTAG_PATHMOVE: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); + LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, + cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); #endif gw16012_path_move(cmd->cmd.pathmove); break; @@ -401,7 +378,8 @@ static int gw16012_get_giveio_access(void) if (version.dwPlatformId != VER_PLATFORM_WIN32_NT) return 0; - h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); if (h == INVALID_HANDLE_VALUE) { errno = ENODEV; return -1; @@ -423,24 +401,21 @@ static int gw16012_init_ioctls(void) { int temp = 0; temp = ioctl(device_handle, PPCLAIM); - if (temp < 0) - { + if (temp < 0) { LOG_ERROR("cannot claim device"); return ERROR_JTAG_INIT_FAILED; } temp = PARPORT_MODE_COMPAT; temp = ioctl(device_handle, PPSETMODE, &temp); - if (temp < 0) - { + if (temp < 0) { LOG_ERROR(" cannot set compatible mode to device"); return ERROR_JTAG_INIT_FAILED; } temp = IEEE1284_MODE_COMPAT; temp = ioctl(device_handle, PPNEGOT, &temp); - if (temp < 0) - { + if (temp < 0) { LOG_ERROR("cannot set compatible 1284 mode to device"); return ERROR_JTAG_INIT_FAILED; } @@ -455,15 +430,14 @@ static int gw16012_init_ioctls(void) return ERROR_OK; } -#endif // defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#endif /* defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */ static int gw16012_init_device(void) { const char *device_name = GW16012_PPDEV_NAME; char buffer[256]; - if (device_handle > 0) - { + if (device_handle > 0) { LOG_ERROR("device is already opened"); return ERROR_JTAG_INIT_FAILED; } @@ -472,8 +446,7 @@ static int gw16012_init_device(void) LOG_DEBUG("opening %s...", buffer); device_handle = open(buffer, O_WRONLY); - if (device_handle < 0) - { + if (device_handle < 0) { LOG_ERROR("cannot open device. check it exists and that user read and write rights are set"); return ERROR_JTAG_INIT_FAILED; } @@ -486,23 +459,21 @@ static int gw16012_init_device(void) return ERROR_OK; } -#else // PARPORT_USE_PPDEV +#else /* PARPORT_USE_PPDEV */ static int gw16012_init_device(void) { - if (gw16012_port == 0) - { + if (gw16012_port == 0) { gw16012_port = 0x378; LOG_WARNING("No gw16012 port specified, using default '0x378' (LPT1)"); } LOG_DEBUG("requesting privileges for parallel port 0x%lx...", (long unsigned)(gw16012_port)); #if PARPORT_USE_GIVEIO == 1 - if (gw16012_get_giveio_access() != 0) + if (gw16012_get_giveio_access() != 0) { #else /* PARPORT_USE_GIVEIO */ - if (ioperm(gw16012_port, 3, 1) != 0) + if (ioperm(gw16012_port, 3, 1) != 0) { #endif /* PARPORT_USE_GIVEIO */ - { LOG_ERROR("missing privileges for direct i/o"); return ERROR_JTAG_INIT_FAILED; } @@ -517,7 +488,7 @@ static int gw16012_init_device(void) return ERROR_OK; } -#endif // PARPORT_USE_PPDEV +#endif /* PARPORT_USE_PPDEV */ static int gw16012_init(void) { @@ -542,15 +513,11 @@ static int gw16012_quit(void) COMMAND_HANDLER(gw16012_handle_parport_port_command) { - if (CMD_ARGC == 1) - { + if (CMD_ARGC == 1) { /* only if the port wasn't overwritten by cmdline */ if (gw16012_port == 0) - { COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], gw16012_port); - } - else - { + else { LOG_ERROR("The parport port was already configured!"); return ERROR_FAIL; } diff --git a/src/jtag/drivers/jlink.c b/src/jtag/drivers/jlink.c index 43062fa31e..35c1a5c4b6 100644 --- a/src/jtag/drivers/jlink.c +++ b/src/jtag/drivers/jlink.c @@ -33,11 +33,10 @@ #include "libusb_common.h" /* See Segger's public documentation: - * Reference manual for J-Link USB Protocol - * Document RM08001-R6 Date: June 16, 2009 - * (Or newer, with some SWD information). - -http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf + * Reference manual for J-Link USB Protocol + * Document RM08001-R6 Date: June 16, 2009 + * (Or newer, with some SWD information). + * http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf */ /* @@ -46,6 +45,7 @@ http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf * * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address) */ + #define VID 0x1366, 0x1366, 0x1366, 0x1366 #define PID 0x0101, 0x0102, 0x0103, 0x0104 @@ -56,16 +56,16 @@ static unsigned int jlink_write_ep = JLINK_WRITE_ENDPOINT; static unsigned int jlink_read_ep = JLINK_READ_ENDPOINT; static unsigned int jlink_hw_jtag_version = 2; -#define JLINK_USB_TIMEOUT 1000 +#define JLINK_USB_TIMEOUT 1000 -// See Section 3.3.2 of the Segger JLink USB protocol manual +/* See Section 3.3.2 of the Segger JLink USB protocol manual */ /* 2048 is the max value we can use here */ #define JLINK_TAP_BUFFER_SIZE 2048 -//#define JLINK_TAP_BUFFER_SIZE 256 -//#define JLINK_TAP_BUFFER_SIZE 384 +/*#define JLINK_TAP_BUFFER_SIZE 256*/ +/*#define JLINK_TAP_BUFFER_SIZE 384*/ #define JLINK_IN_BUFFER_SIZE 2048 -#define JLINK_OUT_BUFFER_SIZE 2*2048 + 4 +#define JLINK_OUT_BUFFER_SIZE (2*2048 + 4) #define JLINK_EMU_RESULT_BUFFER_SIZE 64 /* Global USB buffers */ @@ -222,7 +222,7 @@ static inline void jlink_debug_buffer(uint8_t *buffer, int length) static enum tap_state jlink_last_state = TAP_RESET; -static struct jlink* jlink_handle; +static struct jlink *jlink_handle; /* pid could be specified at runtime */ static uint16_t vids[] = { VID, 0 }; @@ -320,17 +320,28 @@ static void jlink_execute_sleep(struct jtag_command *cmd) static void jlink_execute_command(struct jtag_command *cmd) { - switch (cmd->type) - { - case JTAG_RUNTEST: jlink_execute_runtest(cmd); break; - case JTAG_TLR_RESET: jlink_execute_statemove(cmd); break; - case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break; - case JTAG_SCAN: jlink_execute_scan(cmd); break; - case JTAG_RESET: jlink_execute_reset(cmd); break; - case JTAG_SLEEP: jlink_execute_sleep(cmd); break; - default: - LOG_ERROR("BUG: unknown JTAG command type encountered"); - exit(-1); + switch (cmd->type) { + case JTAG_RUNTEST: + jlink_execute_runtest(cmd); + break; + case JTAG_TLR_RESET: + jlink_execute_statemove(cmd); + break; + case JTAG_PATHMOVE: + jlink_execute_pathmove(cmd); + break; + case JTAG_SCAN: + jlink_execute_scan(cmd); + break; + case JTAG_RESET: + jlink_execute_reset(cmd); + break; + case JTAG_SLEEP: + jlink_execute_sleep(cmd); + break; + default: + LOG_ERROR("BUG: unknown JTAG command type encountered"); + exit(-1); } } @@ -338,8 +349,7 @@ static int jlink_execute_queue(void) { struct jtag_command *cmd = jtag_command_queue; - while (cmd != NULL) - { + while (cmd != NULL) { jlink_execute_command(cmd); cmd = cmd->next; } @@ -352,8 +362,7 @@ static int jlink_speed(int speed) { int result; - if (speed > JLINK_MAX_SPEED) - { + if (speed > JLINK_MAX_SPEED) { LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, JLINK_MAX_SPEED); speed = JLINK_MAX_SPEED; @@ -368,8 +377,7 @@ static int jlink_speed(int speed) usb_out_buffer[2] = (speed >> 8) & 0xff; result = jlink_usb_write(jlink_handle, 3); - if (result != 3) - { + if (result != 3) { LOG_ERROR("J-Link setting speed failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -377,7 +385,7 @@ static int jlink_speed(int speed) return ERROR_OK; } -static int jlink_speed_div(int speed, int* khz) +static int jlink_speed_div(int speed, int *khz) { *khz = speed; @@ -397,8 +405,7 @@ static int jlink_init(void) jlink_handle = jlink_usb_open(); - if (jlink_handle == 0) - { + if (jlink_handle == 0) { LOG_ERROR("Cannot find jlink Interface! Please check " "connection and permissions."); return ERROR_JTAG_INIT_FAILED; @@ -428,8 +435,7 @@ static int jlink_init(void) jlink_hw_jtag_version = 2; - if (jlink_get_version_info() == ERROR_OK) - { + if (jlink_get_version_info() == ERROR_OK) { /* attempt to get status */ jlink_get_status(); } @@ -442,9 +448,8 @@ static int jlink_init(void) /* v5/6 jlink seems to have an issue if the first tap move * is not divisible by 8, so we send a TLR on first power up */ - for (i = 0; i < 8; i++) { + for (i = 0; i < 8; i++) jlink_tap_append_step(1, 0); - } jlink_tap_execute(); return ERROR_OK; @@ -462,11 +467,8 @@ static int jlink_quit(void) static void jlink_end_state(tap_state_t state) { if (tap_is_state_stable(state)) - { tap_set_end_state(state); - } - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } @@ -480,8 +482,7 @@ static void jlink_state_move(void) uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state()); uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - for (i = 0; i < tms_scan_bits; i++) - { + for (i = 0; i < tms_scan_bits; i++) { tms = (tms_scan >> i) & 1; jlink_tap_append_step(tms, 0); } @@ -493,18 +494,12 @@ static void jlink_path_move(int num_states, tap_state_t *path) { int i; - for (i = 0; i < num_states; i++) - { + for (i = 0; i < num_states; i++) { if (path[i] == tap_state_transition(tap_get_state(), false)) - { jlink_tap_append_step(0, 0); - } else if (path[i] == tap_state_transition(tap_get_state(), true)) - { jlink_tap_append_step(1, 0); - } - else - { + else { LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i])); exit(-1); @@ -522,28 +517,23 @@ static void jlink_runtest(int num_cycles) tap_state_t saved_end_state = tap_get_end_state(); - jlink_tap_ensure_space(1,num_cycles + 16); + jlink_tap_ensure_space(1, num_cycles + 16); /* only do a state_move when we're not already in IDLE */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { jlink_end_state(TAP_IDLE); jlink_state_move(); -// num_cycles--; + /* num_cycles--; */ } /* execute num_cycles */ for (i = 0; i < num_cycles; i++) - { jlink_tap_append_step(0, 0); - } /* finish in end_state */ jlink_end_state(saved_end_state); if (tap_get_state() != tap_get_end_state()) - { jlink_state_move(); - } } static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, @@ -573,9 +563,7 @@ static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE); if (tap_get_state() != tap_get_end_state()) - { jlink_state_move(); - } } static void jlink_reset(int trst, int srst) @@ -584,23 +572,16 @@ static void jlink_reset(int trst, int srst) /* Signals are active low */ if (srst == 0) - { jlink_simple_command(EMU_CMD_HW_RESET1); - } + if (srst == 1) - { jlink_simple_command(EMU_CMD_HW_RESET0); - } if (trst == 1) - { jlink_simple_command(EMU_CMD_HW_TRST0); - } if (trst == 0) - { jlink_simple_command(EMU_CMD_HW_TRST1); - } } static void jlink_simple_command(uint8_t command) @@ -613,9 +594,7 @@ static void jlink_simple_command(uint8_t command) result = jlink_usb_write(jlink_handle, 1); if (result != 1) - { LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result); - } } static int jlink_get_status(void) @@ -625,8 +604,7 @@ static int jlink_get_status(void) jlink_simple_command(EMU_CMD_GET_STATE); result = jlink_usb_read(jlink_handle, 8); - if (result != 8) - { + if (result != 8) { LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -643,14 +621,13 @@ static int jlink_get_status(void) return ERROR_OK; } -#define jlink_dump_printf(context, expr ...) \ - do { \ - if (context) \ - command_print(context, expr); \ - else \ - LOG_INFO(expr); \ - } while(0); - +#define jlink_dump_printf(context, expr ...) \ + do { \ + if (context) \ + command_print(context, expr); \ + else \ + LOG_INFO(expr); \ + } while (0); static void jlink_caps_dump(struct command_context *ctx) { @@ -713,8 +690,7 @@ static void jlink_config_dump(struct command_context *ctx, struct jlink_config * jlink_config_usb_address_dump(ctx, cfg); jlink_config_kickstart_dump(ctx, cfg); - if (jlink_hw_type == JLINK_HW_TYPE_JLINK_PRO) - { + if (jlink_hw_type == JLINK_HW_TYPE_JLINK_PRO) { jlink_config_ip_dump(ctx, cfg); jlink_config_mac_address_dump(ctx, cfg); } @@ -728,8 +704,7 @@ static int jlink_get_config(struct jlink_config *cfg) jlink_simple_command(EMU_CMD_READ_CONFIG); result = jlink_usb_read(jlink_handle, size); - if (size != result) - { + if (size != result) { LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size, result); return ERROR_FAIL; } @@ -755,8 +730,7 @@ static int jlink_set_config(struct jlink_config *cfg) memcpy(usb_out_buffer, cfg, size); result = jlink_usb_write(jlink_handle, size); - if (result != size) - { + if (result != size) { LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result); return ERROR_FAIL; } @@ -774,22 +748,19 @@ static int jlink_get_version_info(void) jlink_simple_command(EMU_CMD_VERSION); result = jlink_usb_read(jlink_handle, 2); - if (2 != result) - { + if (2 != result) { LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } len = buf_get_u32(usb_in_buffer, 0, 16); - if (len > JLINK_IN_BUFFER_SIZE) - { + if (len > JLINK_IN_BUFFER_SIZE) { LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len); len = JLINK_IN_BUFFER_SIZE; } result = jlink_usb_read(jlink_handle, len); - if (result != len) - { + if (result != len) { LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -801,8 +772,7 @@ static int jlink_get_version_info(void) jlink_simple_command(EMU_CMD_GET_CAPS); result = jlink_usb_read(jlink_handle, 4); - if (4 != result) - { + if (4 != result) { LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -810,14 +780,12 @@ static int jlink_get_version_info(void) jlink_caps = buf_get_u32(usb_in_buffer, 0, 32); LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps); - if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION)) - { + if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION)) { /* query hardware version */ jlink_simple_command(EMU_CMD_GET_HW_VERSION); result = jlink_usb_read(jlink_handle, 4); - if (4 != result) - { + if (4 != result) { LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -836,14 +804,12 @@ static int jlink_get_version_info(void) LOG_INFO("J-Link hw type %s", jlink_hw_type_str[jlink_hw_type]); } - if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE)) - { + if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE)) { /* query hardware maximum memory block */ jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK); result = jlink_usb_read(jlink_handle, 4); - if (4 != result) - { + if (4 != result) { LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result); return ERROR_JTAG_DEVICE_ERROR; } @@ -852,8 +818,7 @@ static int jlink_get_version_info(void) LOG_INFO("J-Link max mem block %i", (int)jlink_max_size); } - if (jlink_caps & (1 << EMU_CAP_READ_CONFIG)) - { + if (jlink_caps & (1 << EMU_CAP_READ_CONFIG)) { if (jlink_get_config(&jlink_cfg) != ERROR_OK) return ERROR_JTAG_DEVICE_ERROR; @@ -865,8 +830,7 @@ static int jlink_get_version_info(void) COMMAND_HANDLER(jlink_pid_command) { - if (CMD_ARGC != 1) - { + if (CMD_ARGC != 1) { LOG_ERROR("Need exactly one argument to jlink_pid"); return ERROR_FAIL; } @@ -880,8 +844,7 @@ COMMAND_HANDLER(jlink_pid_command) COMMAND_HANDLER(jlink_handle_jlink_info_command) { - if (jlink_get_version_info() == ERROR_OK) - { + if (jlink_get_version_info() == ERROR_OK) { /* attempt to get status */ jlink_get_status(); } @@ -899,22 +862,23 @@ COMMAND_HANDLER(jlink_handle_jlink_caps_command) COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command) { switch (CMD_ARGC) { - case 0: - command_print(CMD_CTX, "J-Link hw jtag %i", jlink_hw_jtag_version); - break; - case 1: { - int request_version = atoi(CMD_ARGV[0]); - switch (request_version) { - case 2: case 3: - jlink_hw_jtag_version = request_version; + case 0: + command_print(CMD_CTX, "J-Link hw jtag %i", jlink_hw_jtag_version); break; + case 1: { + int request_version = atoi(CMD_ARGV[0]); + switch (request_version) { + case 2: + case 3: + jlink_hw_jtag_version = request_version; + break; + default: + return ERROR_COMMAND_SYNTAX_ERROR; + } + break; + } default: return ERROR_COMMAND_SYNTAX_ERROR; - } - break; - } - default: - return ERROR_COMMAND_SYNTAX_ERROR; } return ERROR_OK; @@ -924,8 +888,7 @@ COMMAND_HANDLER(jlink_handle_jlink_kickstart_command) { uint32_t kickstart; - if (CMD_ARGC < 1) - { + if (CMD_ARGC < 1) { jlink_config_kickstart_dump(CMD_CTX, &jlink_cfg); return ERROR_OK; } @@ -943,8 +906,7 @@ COMMAND_HANDLER(jlink_handle_jlink_mac_address_command) char *e; const char *str; - if (CMD_ARGC < 1) - { + if (CMD_ARGC < 1) { jlink_config_mac_address_dump(CMD_CTX, &jlink_cfg); return ERROR_OK; } @@ -952,26 +914,22 @@ COMMAND_HANDLER(jlink_handle_jlink_mac_address_command) str = CMD_ARGV[0]; if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || str[8] != ':' || - str[11] != ':' || str[14] != ':')) - { + str[11] != ':' || str[14] != ':')) { command_print(CMD_CTX, "ethaddr miss format ff:ff:ff:ff:ff:ff"); return ERROR_COMMAND_SYNTAX_ERROR; } - for (i = 5; i >= 0; i--) - { + for (i = 5; i >= 0; i--) { addr[i] = strtoul(str, &e, 16); str = e + 1; } - if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) - { + if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) { command_print(CMD_CTX, "invalid it's zero mac_address"); return ERROR_COMMAND_SYNTAX_ERROR; } - if (!(0x01 & addr[0])) - { + if (!(0x01 & addr[0])) { command_print(CMD_CTX, "invalid it's a multicat mac_address"); return ERROR_COMMAND_SYNTAX_ERROR; } @@ -1022,13 +980,12 @@ COMMAND_HANDLER(jlink_handle_jlink_ip_command) int ret; uint8_t subnet_bits = 24; - if (CMD_ARGC < 1) - { + if (CMD_ARGC < 1) { jlink_config_ip_dump(CMD_CTX, &jlink_cfg); return ERROR_OK; } - ret = string_to_ip(CMD_ARGV[0], (uint8_t*)&ip_address, &i); + ret = string_to_ip(CMD_ARGV[0], (uint8_t *)&ip_address, &i); if (ret != ERROR_OK) return ret; @@ -1036,18 +993,14 @@ COMMAND_HANDLER(jlink_handle_jlink_ip_command) /* check for this format A.B.C.D/E */ - if (i < len) - { + if (i < len) { if (CMD_ARGV[0][i] != '/') return ERROR_COMMAND_SYNTAX_ERROR; COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits); - } - else - { - if (CMD_ARGC > 1) - { - ret = string_to_ip(CMD_ARGV[1], (uint8_t*)&subnet_mask, &i); + } else { + if (CMD_ARGC > 1) { + ret = string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i); if (ret != ERROR_OK) return ret; } @@ -1055,10 +1008,10 @@ COMMAND_HANDLER(jlink_handle_jlink_ip_command) if (!subnet_mask) subnet_mask = (uint32_t)(subnet_bits < 32 ? - ((1ULL << subnet_bits) -1) : 0xffffffff); + ((1ULL << subnet_bits) - 1) : 0xffffffff); - cpy_ip(jlink_cfg.ip_address, (uint8_t*)&ip_address); - cpy_ip(jlink_cfg.subnet_mask, (uint8_t*)&subnet_mask); + cpy_ip(jlink_cfg.ip_address, (uint8_t *)&ip_address); + cpy_ip(jlink_cfg.subnet_mask, (uint8_t *)&subnet_mask); return ERROR_OK; } @@ -1071,8 +1024,7 @@ COMMAND_HANDLER(jlink_handle_jlink_reset_command) COMMAND_HANDLER(jlink_handle_jlink_save_command) { - if (!(jlink_caps & (1 << EMU_CAP_WRITE_CONFIG))) - { + if (!(jlink_caps & (1 << EMU_CAP_WRITE_CONFIG))) { command_print(CMD_CTX, "J-Link write emulator configuration not supported"); return ERROR_OK; } @@ -1085,16 +1037,14 @@ COMMAND_HANDLER(jlink_handle_jlink_usb_address_command) { uint32_t address; - if (CMD_ARGC < 1) - { + if (CMD_ARGC < 1) { jlink_config_usb_address_dump(CMD_CTX, &jlink_cfg); return ERROR_OK; } COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address); - if (address > 0x3 && address != 0xff) - { + if (address > 0x3 && address != 0xff) { command_print(CMD_CTX, "USB Address must be between 0x00 and 0x03 or 0xff"); return ERROR_COMMAND_SYNTAX_ERROR; } @@ -1108,17 +1058,15 @@ COMMAND_HANDLER(jlink_handle_jlink_config_command) struct jlink_config cfg; int ret = ERROR_OK; - if (CMD_ARGC == 0) - { - if (!(jlink_caps & (1 << EMU_CAP_READ_CONFIG))) - { + if (CMD_ARGC == 0) { + if (!(jlink_caps & (1 << EMU_CAP_READ_CONFIG))) { command_print(CMD_CTX, "J-Link read emulator configuration not supported"); goto exit; } ret = jlink_get_config(&cfg); - if ( ret != ERROR_OK) + if (ret != ERROR_OK) command_print(CMD_CTX, "J-Link read emulator configuration failled"); else jlink_config_dump(CMD_CTX, &jlink_cfg); @@ -1239,7 +1187,7 @@ struct jtag_interface jlink_interface = { /* J-Link tap functions */ -static unsigned tap_length = 0; +static unsigned tap_length; static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE]; static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE]; static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE]; @@ -1268,17 +1216,14 @@ static void jlink_tap_ensure_space(int scans, int bits) int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32; if (scans > available_scans || bits > available_bits) - { jlink_tap_execute(); - } } static void jlink_tap_append_step(int tms, int tdi) { int index_var = tap_length / 8; - if (index_var >= JLINK_TAP_BUFFER_SIZE) - { + if (index_var >= JLINK_TAP_BUFFER_SIZE) { LOG_ERROR("jlink_tap_append_step: overflow"); *(uint32_t *)0xFFFFFFFF = 0; exit(-1); @@ -1287,11 +1232,9 @@ static void jlink_tap_append_step(int tms, int tdi) int bit_index = tap_length % 8; uint8_t bit = 1 << bit_index; - // we do not pad TMS, so be sure to initialize all bits + /* we do not pad TMS, so be sure to initialize all bits */ if (0 == bit_index) - { tms_buffer[index_var] = tdi_buffer[index_var] = 0; - } if (tms) tms_buffer[index_var] |= bit; @@ -1318,8 +1261,7 @@ static void jlink_tap_append_scan(int length, uint8_t *buffer, pending_scan_result->command = command; pending_scan_result->buffer = buffer; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { int tms = (i < (length - 1)) ? 0 : 1; int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0; jlink_tap_append_step(tms, tdi); @@ -1342,11 +1284,9 @@ static int jlink_tap_execute(void) * message is a multiple of 64, creates problems with USB comms. * WARNING: This will interfere with tap state counting. */ while ((DIV_ROUND_UP(tap_length, 8) % 64) == 0) - { jlink_tap_append_step((tap_get_state() == TAP_RESET) ? 1 : 0, 0); - } - // number of full bytes (plus one if some would be left over) + /* number of full bytes (plus one if some would be left over) */ byte_length = DIV_ROUND_UP(tap_length, 8); bool use_jtag3 = jlink_hw_jtag_version >= 3; @@ -1361,8 +1301,7 @@ static int jlink_tap_execute(void) tap_length, jlink_last_state); result = jlink_usb_message(jlink_handle, 4 + 2 * byte_length, byte_length); - if (result != byte_length) - { + if (result != byte_length) { LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result, byte_length); jlink_tap_init(); @@ -1371,8 +1310,7 @@ static int jlink_tap_execute(void) memcpy(tdo_buffer, usb_in_buffer, byte_length); - for (i = 0; i < pending_scan_results_length; i++) - { + for (i = 0; i < pending_scan_results_length; i++) { struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i]; uint8_t *buffer = pending_scan_result->buffer; int length = pending_scan_result->length; @@ -1386,16 +1324,13 @@ static int jlink_tap_execute(void) jlink_debug_buffer(buffer, DIV_ROUND_UP(length, 8)); - if (jtag_read_buffer(buffer, command) != ERROR_OK) - { + if (jtag_read_buffer(buffer, command) != ERROR_OK) { jlink_tap_init(); return ERROR_JTAG_QUEUE_FAILED; } if (pending_scan_result->buffer != NULL) - { free(pending_scan_result->buffer); - } } jlink_tap_init(); @@ -1405,7 +1340,7 @@ static int jlink_tap_execute(void) /*****************************************************************************/ /* JLink USB low-level functions */ -static struct jlink* jlink_usb_open() +static struct jlink *jlink_usb_open() { struct jtag_libusb_device_handle *devh; if (jtag_libusb_open(vids, pids, &devh) != ERROR_OK) @@ -1432,13 +1367,11 @@ static struct jlink* jlink_usb_open() /* reopen jlink after usb_reset * on win32 this may take a second or two to re-enumerate */ int retval; - while ((retval = jtag_libusb_open(vids, pids, &devh)) != ERROR_OK) - { + while ((retval = jtag_libusb_open(vids, pids, &devh)) != ERROR_OK) { usleep(1000); timeout--; - if (!timeout) { + if (!timeout) break; - } } if (ERROR_OK != retval) return NULL; @@ -1478,16 +1411,14 @@ static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length) int result; result = jlink_usb_write(jlink, out_length); - if (result != out_length) - { + if (result != out_length) { LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result); return ERROR_JTAG_DEVICE_ERROR; } result = jlink_usb_read(jlink, in_length); - if ((result != in_length) && (result != (in_length + 1))) - { + if ((result != in_length) && (result != (in_length + 1))) { LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result); return ERROR_JTAG_DEVICE_ERROR; @@ -1497,18 +1428,15 @@ static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length) return result; int result2 = ERROR_OK; - if (result == in_length) - { + if (result == in_length) { /* Must read the result from the EMU too */ result2 = jlink_usb_read_emu_result(jlink); - if (1 != result2) - { + if (1 != result2) { LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, " "result=%d, in_length=%i", result2, in_length); /* Try again once, should only happen if (in_length%64 == 0) */ result2 = jlink_usb_read_emu_result(jlink); - if (1 != result2) - { + if (1 != result2) { LOG_ERROR("jlink_usb_read_emu_result failed " "(requested = 1, result=%d)", result2); return ERROR_JTAG_DEVICE_ERROR; @@ -1517,15 +1445,12 @@ static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length) /* Check the result itself */ result2 = usb_emu_result_buffer[0]; - } - else - { + } else { /* Save the result, then remove it from return value */ result2 = usb_in_buffer[result--]; } - if (result2) - { + if (result2) { LOG_ERROR("jlink_usb_message failed with result=%d)", result2); return ERROR_JTAG_DEVICE_ERROR; } @@ -1542,8 +1467,7 @@ static int usb_bulk_with_retries( { int tries = 3, count = 0; - while (tries && (count < size)) - { + while (tries && (count < size)) { int result = f(dev, ep, bytes + count, size - count, timeout); if (result > 0) count += result; @@ -1554,7 +1478,7 @@ static int usb_bulk_with_retries( } static int wrap_usb_bulk_write(jtag_libusb_device_handle *dev, int ep, - char *buff, int size, int timeout) + char *buff, int size, int timeout) { /* usb_bulk_write() takes const char *buff */ return jtag_libusb_bulk_write(dev, ep, buff, size, timeout); @@ -1579,8 +1503,7 @@ static int jlink_usb_write(struct jlink *jlink, int out_length) { int result; - if (out_length > JLINK_OUT_BUFFER_SIZE) - { + if (out_length > JLINK_OUT_BUFFER_SIZE) { LOG_ERROR("jlink_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE); return -1; @@ -1631,11 +1554,9 @@ static void jlink_debug_buffer(uint8_t *buffer, int length) int i; int j; - for (i = 0; i < length; i += BYTES_PER_LINE) - { + for (i = 0; i < length; i += BYTES_PER_LINE) { snprintf(line, 5, "%04x", i); - for (j = i; j < i + BYTES_PER_LINE && j < length; j++) - { + for (j = i; j < i + BYTES_PER_LINE && j < length; j++) { snprintf(s, 4, " %02x", buffer[j]); strcat(line, s); } diff --git a/src/jtag/drivers/libusb0_common.c b/src/jtag/drivers/libusb0_common.c index 8ed60df942..9f6cec756e 100644 --- a/src/jtag/drivers/libusb0_common.c +++ b/src/jtag/drivers/libusb0_common.c @@ -48,7 +48,7 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], struct usb_bus *busses = usb_get_busses(); for (struct usb_bus *bus = busses; bus; bus = bus->next) { for (struct usb_device *dev = bus->devices; - dev; dev = dev->next) { + dev; dev = dev->next) { if (!jtag_libusb_match(dev, vids, pids)) continue; @@ -68,19 +68,19 @@ void jtag_libusb_close(jtag_libusb_device_handle *dev) } int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes, - int size, int timeout) + int size, int timeout) { return usb_bulk_write(dev, ep, bytes, size, timeout); } int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes, - int size, int timeout) + int size, int timeout) { return usb_bulk_read(dev, ep, bytes, size, timeout); } int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh, - int configuration) + int configuration) { struct jtag_libusb_device *udev = jtag_libusb_get_device(devh); @@ -89,8 +89,8 @@ int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh, } int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev, - unsigned int *usb_read_ep, - unsigned int *usb_write_ep) + unsigned int *usb_read_ep, + unsigned int *usb_write_ep) { struct usb_interface *iface = udev->config->interface; struct usb_interface_descriptor *desc = iface->altsetting; diff --git a/src/jtag/drivers/libusb0_common.h b/src/jtag/drivers/libusb0_common.h index 985d084333..9ca65c5ff2 100644 --- a/src/jtag/drivers/libusb0_common.h +++ b/src/jtag/drivers/libusb0_common.h @@ -43,16 +43,16 @@ static inline int jtag_libusb_claim_interface(jtag_libusb_device_handle *devh, }; int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], - struct jtag_libusb_device_handle **out); + struct jtag_libusb_device_handle **out); void jtag_libusb_close(jtag_libusb_device_handle *dev); int jtag_libusb_bulk_write(struct jtag_libusb_device_handle *dev, int ep, - char *bytes, int size, int timeout); + char *bytes, int size, int timeout); int jtag_libusb_bulk_read(struct jtag_libusb_device_handle *dev, int ep, - char *bytes, int size, int timeout); + char *bytes, int size, int timeout); int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh, - int configuration); + int configuration); int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev, - unsigned int *usb_read_ep, - unsigned int *usb_write_ep); + unsigned int *usb_read_ep, + unsigned int *usb_write_ep); #endif /* JTAG_USB_COMMON_H */ diff --git a/src/jtag/drivers/libusb1_common.c b/src/jtag/drivers/libusb1_common.c index 1aae10d3bb..dcca8d1c7d 100644 --- a/src/jtag/drivers/libusb1_common.c +++ b/src/jtag/drivers/libusb1_common.c @@ -80,7 +80,7 @@ void jtag_libusb_close(jtag_libusb_device_handle *dev) } int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes, - int size, int timeout) + int size, int timeout) { int transferred = 0; @@ -90,7 +90,7 @@ int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes, } int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes, - int size, int timeout) + int size, int timeout) { int transferred = 0; @@ -100,7 +100,7 @@ int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes, } int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh, - int configuration) + int configuration) { struct jtag_libusb_device *udev = jtag_libusb_get_device(devh); int retCode = -99; @@ -116,8 +116,8 @@ int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh, } int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev, - unsigned int *usb_read_ep, - unsigned int *usb_write_ep) + unsigned int *usb_read_ep, + unsigned int *usb_write_ep) { const struct libusb_interface *inter; const struct libusb_interface_descriptor *interdesc; diff --git a/src/jtag/drivers/libusb1_common.h b/src/jtag/drivers/libusb1_common.h index 2cff3f09a9..2c570fa18e 100644 --- a/src/jtag/drivers/libusb1_common.h +++ b/src/jtag/drivers/libusb1_common.h @@ -37,22 +37,22 @@ #define jtag_libusb_get_device(devh) libusb_get_device(devh) static inline int jtag_libusb_claim_interface(jtag_libusb_device_handle *devh, - int iface) + int iface) { return libusb_claim_interface(devh, iface); }; int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], - struct jtag_libusb_device_handle **out); + struct jtag_libusb_device_handle **out); void jtag_libusb_close(jtag_libusb_device_handle *dev); int jtag_libusb_bulk_write(struct jtag_libusb_device_handle *dev, int ep, - char *bytes, int size, int timeout); + char *bytes, int size, int timeout); int jtag_libusb_bulk_read(struct jtag_libusb_device_handle *dev, int ep, - char *bytes, int size, int timeout); + char *bytes, int size, int timeout); int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh, - int configuration); + int configuration); int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev, - unsigned int *usb_read_ep, - unsigned int *usb_write_ep); + unsigned int *usb_read_ep, + unsigned int *usb_write_ep); #endif /* JTAG_USB_COMMON_H */ diff --git a/src/jtag/drivers/minidriver_imp.h b/src/jtag/drivers/minidriver_imp.h index 6683300091..ce618d4a83 100644 --- a/src/jtag/drivers/minidriver_imp.h +++ b/src/jtag/drivers/minidriver_imp.h @@ -29,23 +29,23 @@ static inline void interface_jtag_add_scan_check_alloc(struct scan_field *field) field->in_value = (uint8_t *)cmd_queue_alloc(num_bytes); } -void interface_jtag_add_dr_out(struct jtag_tap* tap, - int num_fields, const int* num_bits, const uint32_t* value, - tap_state_t end_state); +void interface_jtag_add_dr_out(struct jtag_tap *tap, + int num_fields, const int *num_bits, const uint32_t *value, + tap_state_t end_state); void interface_jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0); void interface_jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0, - jtag_callback_data_t data1, jtag_callback_data_t data2, - jtag_callback_data_t data3); + jtag_callback_data_t data1, jtag_callback_data_t data2, + jtag_callback_data_t data3); -void jtag_add_dr_out(struct jtag_tap* tap, - int num_fields, const int* num_bits, const uint32_t* value, - tap_state_t end_state); +void jtag_add_dr_out(struct jtag_tap *tap, + int num_fields, const int *num_bits, const uint32_t *value, + tap_state_t end_state); void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0, - jtag_callback_data_t data1, jtag_callback_data_t data2, - jtag_callback_data_t data3); + jtag_callback_data_t data1, jtag_callback_data_t data2, + jtag_callback_data_t data3); -#endif // MINIDRIVER_IMP_H +#endif /* MINIDRIVER_IMP_H */ diff --git a/src/jtag/drivers/parport.c b/src/jtag/drivers/parport.c index ec175fa938..3c07afb286 100644 --- a/src/jtag/drivers/parport.c +++ b/src/jtag/drivers/parport.c @@ -20,6 +20,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -31,8 +32,8 @@ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #include #include -#define ioperm(startport,length,enable)\ - i386_set_ioperm((startport), (length), (enable)) +#define ioperm(startport, length, enable)\ + i386_set_ioperm((startport), (length), (enable)) #endif /* __FreeBSD__ */ #if PARPORT_USE_PPDEV == 1 @@ -56,11 +57,10 @@ #include #endif - /* parallel port cable description */ struct cable { - char* name; + char *name; uint8_t TDO_MASK; /* status port bit containing current TDO value */ uint8_t TRST_MASK; /* data port bit for TRST */ uint8_t TMS_MASK; /* data port bit for TMS */ @@ -74,8 +74,7 @@ struct cable { uint8_t LED_MASK; /* data port bit for LED */ }; -static struct cable cables[] = -{ +static struct cable cables[] = { /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */ { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 }, { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 }, @@ -83,8 +82,8 @@ static struct cable cables[] = { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 }, { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 }, { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, - { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 }, - { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, + { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 }, + { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 }, { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 }, /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows: @@ -100,16 +99,16 @@ static struct cable cables[] = }; /* configuration */ -static char* parport_cable = NULL; +static char *parport_cable; static uint16_t parport_port; -static bool parport_exit = 0; +static bool parport_exit; static uint32_t parport_toggling_time_ns = 1000; static int wait_states; /* interface variables */ -static struct cable* cable; -static uint8_t dataport_value = 0x0; +static struct cable *cable; +static uint8_t dataport_value; #if PARPORT_USE_PPDEV == 1 static int device_handle; @@ -123,7 +122,7 @@ static int parport_read(void) int data = 0; #if PARPORT_USE_PPDEV == 1 - ioctl(device_handle, PPRSTATUS, & data); + ioctl(device_handle, PPRSTATUS, &data); #else data = inb(statusport); #endif @@ -134,7 +133,7 @@ static int parport_read(void) return 0; } -static __inline__ void parport_write_data(void) +static inline void parport_write_data(void) { uint8_t output; output = dataport_value ^ cable->OUTPUT_INVERT; @@ -208,7 +207,7 @@ static int parport_speed(int speed) return ERROR_OK; } -static int parport_khz(int khz, int* jtag_speed) +static int parport_khz(int khz, int *jtag_speed) { if (khz == 0) { LOG_DEBUG("RCLK not supported"); @@ -219,7 +218,7 @@ static int parport_khz(int khz, int* jtag_speed) return ERROR_OK; } -static int parport_speed_div(int speed, int* khz) +static int parport_speed_div(int speed, int *khz) { uint32_t denominator = (speed + 1) * parport_toggling_time_ns; @@ -269,24 +268,20 @@ static int parport_init(void) cur_cable = cables; - if ((parport_cable == NULL) || (parport_cable[0] == 0)) - { + if ((parport_cable == NULL) || (parport_cable[0] == 0)) { parport_cable = "wiggler"; LOG_WARNING("No parport cable specified, using default 'wiggler'"); } - while (cur_cable->name) - { - if (strcmp(cur_cable->name, parport_cable) == 0) - { + while (cur_cable->name) { + if (strcmp(cur_cable->name, parport_cable) == 0) { cable = cur_cable; break; } cur_cable++; } - if (!cable) - { + if (!cable) { LOG_ERROR("No matching cable found for %s", parport_cable); return ERROR_JTAG_INIT_FAILED; } @@ -294,8 +289,7 @@ static int parport_init(void) dataport_value = cable->PORT_INIT; #if PARPORT_USE_PPDEV == 1 - if (device_handle > 0) - { + if (device_handle > 0) { LOG_ERROR("device is already opened"); return ERROR_JTAG_INIT_FAILED; } @@ -312,8 +306,7 @@ static int parport_init(void) device_handle = open(buffer, O_WRONLY); #endif /* __FreeBSD__, __FreeBSD_kernel__ */ - if (device_handle < 0) - { + if (device_handle < 0) { int err = errno; LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err); return ERROR_JTAG_INIT_FAILED; @@ -324,32 +317,28 @@ static int parport_init(void) #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) int i = ioctl(device_handle, PPCLAIM); - if (i < 0) - { + if (i < 0) { LOG_ERROR("cannot claim device"); return ERROR_JTAG_INIT_FAILED; } i = PARPORT_MODE_COMPAT; - i= ioctl(device_handle, PPSETMODE, & i); - if (i < 0) - { + i = ioctl(device_handle, PPSETMODE, &i); + if (i < 0) { LOG_ERROR(" cannot set compatible mode to device"); return ERROR_JTAG_INIT_FAILED; } i = IEEE1284_MODE_COMPAT; - i = ioctl(device_handle, PPNEGOT, & i); - if (i < 0) - { + i = ioctl(device_handle, PPNEGOT, &i); + if (i < 0) { LOG_ERROR("cannot set compatible 1284 mode to device"); return ERROR_JTAG_INIT_FAILED; } #endif /* not __FreeBSD__, __FreeBSD_kernel__ */ #else /* not PARPORT_USE_PPDEV */ - if (parport_port == 0) - { + if (parport_port == 0) { parport_port = 0x378; LOG_WARNING("No parport port specified, using default '0x378' (LPT1)"); } @@ -359,11 +348,10 @@ static int parport_init(void) LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport); #if PARPORT_USE_GIVEIO == 1 - if (parport_get_giveio_access() != 0) + if (parport_get_giveio_access() != 0) { #else /* PARPORT_USE_GIVEIO */ - if (ioperm(dataport, 3, 1) != 0) + if (ioperm(dataport, 3, 1) != 0) { #endif /* PARPORT_USE_GIVEIO */ - { LOG_ERROR("missing privileges for direct i/o"); return ERROR_JTAG_INIT_FAILED; } @@ -391,14 +379,12 @@ static int parport_quit(void) { parport_led(0); - if (parport_exit) - { + if (parport_exit) { dataport_value = cable->PORT_EXIT; parport_write_data(); } - if (parport_cable) - { + if (parport_cable) { free(parport_cable); parport_cable = NULL; } @@ -408,15 +394,11 @@ static int parport_quit(void) COMMAND_HANDLER(parport_handle_parport_port_command) { - if (CMD_ARGC == 1) - { + if (CMD_ARGC == 1) { /* only if the port wasn't overwritten by cmdline */ if (parport_port == 0) - { COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], parport_port); - } - else - { + else { LOG_ERROR("The parport port was already configured!"); return ERROR_FAIL; } @@ -433,8 +415,7 @@ COMMAND_HANDLER(parport_handle_parport_cable_command) return ERROR_OK; /* only if the cable name wasn't overwritten by cmdline */ - if (parport_cable == 0) - { + if (parport_cable == 0) { /* REVISIT first verify that it's listed in cables[] ... */ parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char)); strcpy(parport_cable, CMD_ARGV[0]); @@ -448,9 +429,7 @@ COMMAND_HANDLER(parport_handle_parport_cable_command) COMMAND_HANDLER(parport_handle_write_on_exit_command) { if (CMD_ARGC != 1) - { return ERROR_COMMAND_SYNTAX_ERROR; - } COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit); diff --git a/src/jtag/drivers/presto.c b/src/jtag/drivers/presto.c index 0de48ffd88..28576a7959 100644 --- a/src/jtag/drivers/presto.c +++ b/src/jtag/drivers/presto.c @@ -35,7 +35,6 @@ #include #include "bitq.h" - /* PRESTO access library includes */ #if BUILD_PRESTO_FTD2XX == 1 #include @@ -72,16 +71,16 @@ struct presto { int buff_out_pos; uint8_t buff_in[BUFFER_SIZE]; - int buff_in_exp; /* expected in buffer length */ - int buff_in_len; /* length of data received */ + int buff_in_exp;/* expected in buffer length */ + int buff_in_len;/* length of data received */ int buff_in_pos; unsigned long total_out; unsigned long total_in; - int jtag_tms; /* last tms state */ - int jtag_tck; /* last tck state */ - int jtag_rst; /* last trst state */ + int jtag_tms; /* last tms state */ + int jtag_tck; /* last tck state */ + int jtag_rst; /* last trst state */ int jtag_tdi_data; int jtag_tdi_count; @@ -92,8 +91,7 @@ struct presto { static struct presto presto_state; static struct presto *presto = &presto_state; -static uint8_t presto_init_seq[] = -{ +static uint8_t presto_init_seq[] = { 0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0 }; @@ -101,38 +99,37 @@ static int presto_write(uint8_t *buf, uint32_t size) { #if BUILD_PRESTO_FTD2XX == 1 DWORD ftbytes; - if ((presto->status = FT_Write(presto->handle, buf, size, &ftbytes)) != FT_OK) - { + presto->status = FT_Write(presto->handle, buf, size, &ftbytes); + if (presto->status != FT_OK) { LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(presto->status)); return ERROR_JTAG_DEVICE_ERROR; } #elif BUILD_PRESTO_LIBFTDI == 1 uint32_t ftbytes; - if ((presto->retval = ftdi_write_data(&presto->ftdic, buf, size)) < 0) - { + presto->retval = ftdi_write_data(&presto->ftdic, buf, size); + if (presto->retval < 0) { LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic)); return ERROR_JTAG_DEVICE_ERROR; } ftbytes = presto->retval; #endif - if (ftbytes != size) - { + if (ftbytes != size) { LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)", - (unsigned)ftbytes, (unsigned)size); + (unsigned)ftbytes, (unsigned)size); return ERROR_JTAG_DEVICE_ERROR; } return ERROR_OK; } -static int presto_read(uint8_t* buf, uint32_t size) +static int presto_read(uint8_t *buf, uint32_t size) { #if BUILD_PRESTO_FTD2XX == 1 DWORD ftbytes; - if ((presto->status = FT_Read(presto->handle, buf, size, &ftbytes)) != FT_OK) - { + presto->status = FT_Read(presto->handle, buf, size, &ftbytes); + if (presto->status != FT_OK) { LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(presto->status)); return ERROR_JTAG_DEVICE_ERROR; } @@ -142,28 +139,28 @@ static int presto_read(uint8_t* buf, uint32_t size) struct timeval timeout, now; gettimeofday(&timeout, NULL); - timeval_add_time(&timeout, 1, 0); /* one second timeout */ + timeval_add_time(&timeout, 1, 0); /* one second timeout */ - while (ftbytes < size) - { - if ((presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes)) < 0) - { + while (ftbytes < size) { + presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes); + if (presto->retval < 0) { LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic)); return ERROR_JTAG_DEVICE_ERROR; } ftbytes += presto->retval; gettimeofday(&now, NULL); - if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) + if ((now.tv_sec > timeout.tv_sec) || + ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) break; } #endif - if (ftbytes != size) - { - /* this is just a warning, there might have been timeout when detecting PRESTO, which is not fatal */ + if (ftbytes != size) { + /* this is just a warning, there might have been timeout when detecting PRESTO, + *which is not fatal */ LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)", - (unsigned)ftbytes, (unsigned)size); + (unsigned)ftbytes, (unsigned)size); return ERROR_JTAG_DEVICE_ERROR; } @@ -186,39 +183,39 @@ static int presto_open_ftd2xx(char *req_serial) #if IS_WIN32 == 0 /* Add non-standard Vid/Pid to the linux driver */ - if ((presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID)) != FT_OK) - { + presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID); + if (presto->status != FT_OK) { LOG_ERROR("couldn't add PRESTO VID/PID"); exit(-1); } #endif - if ((presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY)) != FT_OK) - { + presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY); + if (presto->status != FT_OK) { LOG_ERROR("FT_ListDevices failed: %s", ftd2xx_status_string(presto->status)); return ERROR_JTAG_DEVICE_ERROR; } LOG_DEBUG("FTDI devices available: %" PRIu32, (uint32_t)numdevs); - for (i = 0; i < numdevs; i++) - { - if ((presto->status = FT_Open(i, &(presto->handle))) != FT_OK) - { - /* this is not fatal, the device may be legitimately open by other process, hence debug message only */ + for (i = 0; i < numdevs; i++) { + presto->status = FT_Open(i, &(presto->handle)); + if (presto->status != FT_OK) { + /* this is not fatal, the device may be legitimately open by other process, + *hence debug message only */ LOG_DEBUG("FT_Open failed: %s", ftd2xx_status_string(presto->status)); continue; } LOG_DEBUG("FTDI device %i open", (int)i); - if ((presto->status = FT_GetDeviceInfo(presto->handle, &device, &vidpid, - presto->serial, devname, NULL)) == FT_OK) - { - if (vidpid == PRESTO_VID_PID - && (req_serial == NULL || !strcmp(presto->serial, req_serial))) + presto->status = FT_GetDeviceInfo(presto->handle, &device, + &vidpid, presto->serial, devname, NULL); + if (presto->status == FT_OK) { + if (vidpid == PRESTO_VID_PID && (req_serial == NULL || + !strcmp(presto->serial, req_serial))) break; - } - else - LOG_DEBUG("FT_GetDeviceInfo failed: %s", ftd2xx_status_string(presto->status)); + } else + LOG_DEBUG("FT_GetDeviceInfo failed: %s", ftd2xx_status_string( + presto->status)); LOG_DEBUG("FTDI device %i does not match, closing", (int)i); FT_Close(presto->handle); @@ -226,74 +223,87 @@ static int presto_open_ftd2xx(char *req_serial) } if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE) - return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */ + return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */ - if ((presto->status = FT_SetLatencyTimer(presto->handle, 1)) != FT_OK) + presto->status = FT_SetLatencyTimer(presto->handle, 1); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - - if ((presto->status = FT_SetTimeouts(presto->handle, 100, 0)) != FT_OK) + presto->status = FT_SetTimeouts(presto->handle, 100, 0); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK) + presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto_data = 0xD0; - if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK) + presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason, - probably a bug in library threading */ + /* delay between first write/read turnaround (after purge?) necessary + * under Linux for unknown reason, + * probably a bug in library threading */ usleep(100000); - if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK) + presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - if (ftbytes != 1) - { + if (ftbytes != 1) { LOG_DEBUG("PRESTO reset"); - if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK) + presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - if ((presto->status = FT_SetBitMode(presto->handle, 0x80, 1)) != FT_OK) + presto->status = FT_SetBitMode(presto->handle, 0x80, 1); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - if ((presto->status = FT_SetBaudRate(presto->handle, 9600)) != FT_OK) + presto->status = FT_SetBaudRate(presto->handle, 9600); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto_data = 0; - for (i = 0; i < 4 * 62; i++) - if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK) + for (i = 0; i < 4 * 62; i++) { + presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - + } usleep(100000); - if ((presto->status = FT_SetBitMode(presto->handle, 0x00, 0)) != FT_OK) + presto->status = FT_SetBitMode(presto->handle, 0x00, 0); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK) + presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; presto_data = 0xD0; - if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK) + presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason, probably a bug in library threading */ usleep(100000); - if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK) + presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; - if (ftbytes != 1) - { + if (ftbytes != 1) { LOG_DEBUG("PRESTO not responding"); return ERROR_JTAG_DEVICE_ERROR; } } - if ((presto->status = FT_SetTimeouts(presto->handle, 0, 0)) != FT_OK) + presto->status = FT_SetTimeouts(presto->handle, 0, 0); + if (presto->status != FT_OK) return ERROR_JTAG_DEVICE_ERROR; + presto->status = FT_Write(presto->handle, &presto_init_seq, + sizeof(presto_init_seq), &ftbytes); - presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes); if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq)) return ERROR_JTAG_DEVICE_ERROR; @@ -308,46 +318,39 @@ static int presto_open_libftdi(char *req_serial) LOG_DEBUG("searching for PRESTO using libftdi"); /* initialize FTDI context structure */ - if (ftdi_init(&presto->ftdic) < 0) - { + if (ftdi_init(&presto->ftdic) < 0) { LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str); return ERROR_JTAG_DEVICE_ERROR; } /* context, vendor id, product id */ - if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) - { + if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) { LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str); return ERROR_JTAG_DEVICE_ERROR; } - if (ftdi_usb_reset(&presto->ftdic) < 0) - { + if (ftdi_usb_reset(&presto->ftdic) < 0) { LOG_ERROR("unable to reset PRESTO device"); return ERROR_JTAG_DEVICE_ERROR; } - if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) - { + if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) { LOG_ERROR("unable to set latency timer"); return ERROR_JTAG_DEVICE_ERROR; } - if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) - { + if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) { LOG_ERROR("unable to purge PRESTO buffers"); return ERROR_JTAG_DEVICE_ERROR; } presto_data = 0xD0; - if (presto_write(&presto_data, 1) != ERROR_OK) - { + if (presto_write(&presto_data, 1) != ERROR_OK) { LOG_ERROR("error writing to PRESTO"); return ERROR_JTAG_DEVICE_ERROR; } - if (presto_read(&presto_data, 1) != ERROR_OK) - { + if (presto_read(&presto_data, 1) != ERROR_OK) { LOG_DEBUG("no response from PRESTO, retrying"); if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) @@ -357,22 +360,20 @@ static int presto_open_libftdi(char *req_serial) if (presto_write(&presto_data, 1) != ERROR_OK) return ERROR_JTAG_DEVICE_ERROR; - if (presto_read(&presto_data, 1) != ERROR_OK) - { + if (presto_read(&presto_data, 1) != ERROR_OK) { LOG_ERROR("no response from PRESTO, giving up"); return ERROR_JTAG_DEVICE_ERROR; } } - if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK) - { + if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK) { LOG_ERROR("error writing PRESTO init sequence"); return ERROR_JTAG_DEVICE_ERROR; } return ERROR_OK; } -#endif /* BUILD_PRESTO_LIBFTDI == 1 */ +#endif /* BUILD_PRESTO_LIBFTDI == 1 */ static int presto_open(char *req_serial) { @@ -414,27 +415,35 @@ static int presto_close(void) if (presto->status != FT_OK) result = ERROR_JTAG_DEVICE_ERROR; - presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes); + presto->status = FT_Write(presto->handle, + &presto_init_seq, + sizeof(presto_init_seq), + &ftbytes); if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq)) result = ERROR_JTAG_DEVICE_ERROR; - if ((presto->status = FT_SetLatencyTimer(presto->handle, 16)) != FT_OK) + presto->status = FT_SetLatencyTimer(presto->handle, 16); + if (presto->status != FT_OK) result = ERROR_JTAG_DEVICE_ERROR; - if ((presto->status = FT_Close(presto->handle)) != FT_OK) + presto->status = FT_Close(presto->handle); + if (presto->status != FT_OK) result = ERROR_JTAG_DEVICE_ERROR; else presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE; #elif BUILD_PRESTO_LIBFTDI == 1 - if ((presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq))) != sizeof(presto_init_seq)) + presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq)); + if (presto->retval != sizeof(presto_init_seq)) result = ERROR_JTAG_DEVICE_ERROR; - if ((presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16)) < 0) + presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16); + if (presto->retval < 0) result = ERROR_JTAG_DEVICE_ERROR; - if ((presto->retval = ftdi_usb_close(&presto->ftdic)) < 0) + presto->retval = ftdi_usb_close(&presto->ftdic); + if (presto->retval < 0) result = ERROR_JTAG_DEVICE_ERROR; else ftdi_deinit(&presto->ftdic); @@ -449,17 +458,15 @@ static int presto_flush(void) return ERROR_OK; #if BUILD_PRESTO_FTD2XX == 1 - if (presto->status != FT_OK) + if (presto->status != FT_OK) { #elif BUILD_PRESTO_LIBFTDI == 1 - if (presto->retval < 0) + if (presto->retval < 0) { #endif - { LOG_DEBUG("error in previous communication, canceling I/O operation"); return ERROR_JTAG_DEVICE_ERROR; } - if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK) - { + if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK) { presto->buff_out_pos = 0; return ERROR_JTAG_DEVICE_ERROR; } @@ -473,8 +480,7 @@ static int presto_flush(void) presto->buff_in_pos = 0; presto->buff_in_len = 0; - if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK) - { + if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK) { presto->buff_in_exp = 0; return ERROR_JTAG_DEVICE_ERROR; } @@ -488,21 +494,21 @@ static int presto_flush(void) static int presto_sendbyte(int data) { - if (data == EOF) return presto_flush(); + if (data == EOF) + return presto_flush(); - if (presto->buff_out_pos < BUFFER_SIZE) - { + if (presto->buff_out_pos < BUFFER_SIZE) { presto->buff_out[presto->buff_out_pos++] = (uint8_t)data; - if (((data & 0xC0) == 0x40) || ((data & 0xD0)== 0xD0)) + if (((data & 0xC0) == 0x40) || ((data & 0xD0) == 0xD0)) presto->buff_in_exp++; - } - else + } else return ERROR_JTAG_DEVICE_ERROR; #if BUILD_PRESTO_FTD2XX == 1 if (presto->buff_out_pos >= BUFFER_SIZE) #elif BUILD_PRESTO_LIBFTDI == 1 - /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128 bytes only!) */ + /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128 + *bytes only!) */ if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp == 128) #endif return presto_flush(); @@ -536,8 +542,7 @@ static int presto_tdi_flush(void) if (presto->jtag_tdi_count == 0) return 0; - if (presto->jtag_tck == 0) - { + if (presto->jtag_tck == 0) { LOG_ERROR("BUG: unexpected TAP condition, TCK low"); return -1; } @@ -552,8 +557,7 @@ static int presto_tdi_flush(void) static int presto_tck_idle(void) { - if (presto->jtag_tck == 1) - { + if (presto->jtag_tck == 1) { presto_sendbyte(0xCA); presto->jtag_tck = 0; } @@ -569,11 +573,8 @@ static int presto_bitq_out(int tms, int tdi, int tdo_req) unsigned char cmd; if (presto->jtag_tck == 0) - { - presto_sendbyte(0xA4); /* LED idicator - JTAG active */ - } - else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms) - { + presto_sendbyte(0xA4); /* LED idicator - JTAG active */ + else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms) { presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count; if (++presto->jtag_tdi_count == 4) @@ -587,8 +588,7 @@ static int presto_bitq_out(int tms, int tdi, int tdo_req) cmd = tdi ? 0xCB : 0xCA; presto_sendbyte(cmd); - if (tms != presto->jtag_tms) - { + if (tms != presto->jtag_tms) { presto_sendbyte((tms ? 0xEC : 0xE8) | (presto->jtag_rst ? 0x02 : 0)); presto->jtag_tms = tms; } @@ -614,7 +614,7 @@ static int presto_bitq_flush(void) presto_tdi_flush(); presto_tck_idle(); - presto_sendbyte(0xA0); /* LED idicator - JTAG idle */ + presto_sendbyte(0xA0); /* LED idicator - JTAG idle */ return presto_flush(); } @@ -630,7 +630,8 @@ static int presto_bitq_in(void) { if (presto->buff_in_pos >= presto->buff_in_len) return -1; - if (presto->buff_in[presto->buff_in_pos++]&0x08) return 1; + if (presto->buff_in[presto->buff_in_pos++]&0x08) + return 1; return 0; } @@ -641,8 +642,7 @@ static int presto_bitq_sleep(unsigned long us) presto_tdi_flush(); presto_tck_idle(); - if (us > 100000) - { + if (us > 100000) { presto_bitq_flush(); jtag_sleep(us); return 0; @@ -671,40 +671,42 @@ static int presto_bitq_reset(int trst, int srst) } static struct bitq_interface presto_bitq = { - .out = &presto_bitq_out, - .flush = &presto_bitq_flush, - .sleep = &presto_bitq_sleep, - .reset = &presto_bitq_reset, - .in_rdy = &presto_bitq_in_rdy, - .in = &presto_bitq_in, - }; + .out = &presto_bitq_out, + .flush = &presto_bitq_flush, + .sleep = &presto_bitq_sleep, + .reset = &presto_bitq_reset, + .in_rdy = &presto_bitq_in_rdy, + .in = &presto_bitq_in, +}; /* -------------------------------------------------------------------------- */ static int presto_adapter_khz(int khz, int *jtag_speed) { - if (khz < 0) - { + if (khz < 0) { *jtag_speed = 0; return ERROR_COMMAND_SYNTAX_ERROR; } - if (khz >= 3000) *jtag_speed = 0; - else *jtag_speed = (1000 + khz-1)/khz; + if (khz >= 3000) + *jtag_speed = 0; + else + *jtag_speed = (1000 + khz-1)/khz; return 0; } static int presto_jtag_speed_div(int speed, int *khz) { - if ((speed < 0) || (speed > 1000)) - { + if ((speed < 0) || (speed > 1000)) { *khz = 0; return ERROR_COMMAND_SYNTAX_ERROR; } - if (speed == 0) *khz = 3000; - else *khz = 1000/speed; + if (speed == 0) + *khz = 3000; + else + *khz = 1000/speed; return 0; } @@ -714,9 +716,7 @@ static int presto_jtag_speed(int speed) int khz; if (presto_jtag_speed_div(speed, &khz)) - { return ERROR_COMMAND_SYNTAX_ERROR; - } presto->jtag_speed = speed; @@ -732,16 +732,12 @@ static char *presto_serial; COMMAND_HANDLER(presto_handle_serial_command) { - if (CMD_ARGC == 1) - { + if (CMD_ARGC == 1) { if (presto_serial) free(presto_serial); presto_serial = strdup(CMD_ARGV[0]); - } - else - { - return ERROR_COMMAND_SYNTAX_ERROR; - } + } else + return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_OK; } @@ -759,8 +755,7 @@ static const struct command_registration presto_command_handlers[] = { static int presto_jtag_init(void) { - if (presto_open(presto_serial) != ERROR_OK) - { + if (presto_open(presto_serial) != ERROR_OK) { presto_close(); if (presto_serial != NULL) LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial); @@ -780,8 +775,7 @@ static int presto_jtag_quit(void) presto_close(); LOG_INFO("PRESTO closed"); - if (presto_serial) - { + if (presto_serial) { free(presto_serial); presto_serial = NULL; } diff --git a/src/jtag/drivers/remote_bitbang.c b/src/jtag/drivers/remote_bitbang.c index d3ab1b11d3..eb98423cd7 100644 --- a/src/jtag/drivers/remote_bitbang.c +++ b/src/jtag/drivers/remote_bitbang.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -27,26 +28,29 @@ #include #include "bitbang.h" -// from unix man page and sys/un.h: +/* from unix man page and sys/un.h: */ #define UNIX_PATH_MAX 108 -// arbitrary limit on host name length: +/* arbitrary limit on host name length: */ #define REMOTE_BITBANG_HOST_MAX 255 #define REMOTE_BITBANG_RAISE_ERROR(expr ...) \ - LOG_ERROR(expr); LOG_ERROR("Terminating openocd."); exit(-1); + do { \ + LOG_ERROR(expr); \ + LOG_ERROR("Terminating openocd."); \ + exit(-1); \ + while (0) static char remote_bitbang_host[REMOTE_BITBANG_HOST_MAX] = "openocd"; -static uint16_t remote_bitbang_port = 0; +static uint16_t remote_bitbang_port; -FILE* remote_bitbang_in; -FILE* remote_bitbang_out; +FILE *remote_bitbang_in; +FILE *remote_bitbang_out; static void remote_bitbang_putc(int c) { - if (EOF == fputc(c, remote_bitbang_out)) { + if (EOF == fputc(c, remote_bitbang_out)) REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno)); - } } static int remote_bitbang_quit(void) @@ -60,9 +64,9 @@ static int remote_bitbang_quit(void) LOG_ERROR("fflush: %s", strerror(errno)); return ERROR_FAIL; } - - // We only need to close one of the FILE*s, because they both use the same - // underlying file descriptor. + + /* We only need to close one of the FILE*s, because they both use the same */ + /* underlying file descriptor. */ if (EOF == fclose(remote_bitbang_out)) { LOG_ERROR("fclose: %s", strerror(errno)); return ERROR_FAIL; @@ -72,7 +76,7 @@ static int remote_bitbang_quit(void) return ERROR_OK; } -// Get the next read response. +/* Get the next read response. */ static int remote_bitbang_rread(void) { if (EOF == fflush(remote_bitbang_out)) { @@ -82,12 +86,14 @@ static int remote_bitbang_rread(void) int c = fgetc(remote_bitbang_in); switch (c) { - case '0': return 0; - case '1': return 1; + case '0': + return 0; + case '1': + return 1; default: remote_bitbang_quit(); REMOTE_BITBANG_RAISE_ERROR( - "remote_bitbang: invalid read response: %c(%i)", c, c); + "remote_bitbang: invalid read response: %c(%i)", c, c); } } @@ -136,14 +142,22 @@ static int remote_bitbang_init_tcp(void) return ERROR_FAIL; } - struct hostent* hent = gethostbyname(remote_bitbang_host); + struct hostent *hent = gethostbyname(remote_bitbang_host); if (hent == NULL) { - char* errorstr = "???"; + char *errorstr = "???"; switch (h_errno) { - case HOST_NOT_FOUND: errorstr = "host not found"; break; - case NO_ADDRESS: errorstr = "no address"; break; - case NO_RECOVERY: errorstr = "no recovery"; break; - case TRY_AGAIN: errorstr = "try again"; break; + case HOST_NOT_FOUND: + errorstr = "host not found"; + break; + case NO_ADDRESS: + errorstr = "no address"; + break; + case NO_RECOVERY: + errorstr = "no recovery"; + break; + case TRY_AGAIN: + errorstr = "try again"; + break; } LOG_ERROR("gethostbyname: %s", errorstr); return ERROR_FAIL; @@ -152,8 +166,8 @@ static int remote_bitbang_init_tcp(void) struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(remote_bitbang_port); - addr.sin_addr = *(struct in_addr*)hent->h_addr; - if (connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0) { + addr.sin_addr = *(struct in_addr *)hent->h_addr; + if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { LOG_ERROR("connect: %s", strerror(errno)); return ERROR_FAIL; } @@ -188,7 +202,7 @@ static int remote_bitbang_init_unix(void) strncpy(addr.sun_path, remote_bitbang_host, UNIX_PATH_MAX); addr.sun_path[UNIX_PATH_MAX-1] = '\0'; - if (connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) { + if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) { LOG_ERROR("connect: %s", strerror(errno)); return ERROR_FAIL; } @@ -214,21 +228,20 @@ static int remote_bitbang_init(void) bitbang_interface = &remote_bitbang_bitbang; LOG_INFO("Initializing remote_bitbang driver"); - if (remote_bitbang_port == 0) { + if (remote_bitbang_port == 0) return remote_bitbang_init_unix(); - } return remote_bitbang_init_tcp(); } -static int remote_bitbang_khz(int khz, int* jtag_speed) +static int remote_bitbang_khz(int khz, int *jtag_speed) { *jtag_speed = 0; return ERROR_OK; } -static int remote_bitbang_speed_div(int speed, int* khz) +static int remote_bitbang_speed_div(int speed, int *khz) { - // I don't think this really matters any. + /* I don't think this really matters any. */ *khz = 1; return ERROR_OK; } diff --git a/src/jtag/drivers/rlink.c b/src/jtag/drivers/rlink.c index f524b5c9c3..c5c2701d1d 100644 --- a/src/jtag/drivers/rlink.c +++ b/src/jtag/drivers/rlink.c @@ -23,6 +23,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -36,92 +37,83 @@ #include "rlink_dtc_cmd.h" #include "usb_common.h" - -/* This feature is made useless by running the DTC all the time. When automatic, the LED is on whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */ +/* This feature is made useless by running the DTC all the time. When automatic, the LED is on + *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */ #undef AUTOMATIC_BUSY_LED /* This feature may require derating the speed due to reduced hold time. */ #undef USE_HARDWARE_SHIFTER_FOR_TMS +#define INTERFACE_NAME "RLink" -#define INTERFACE_NAME "RLink" - -#define USB_IDVENDOR (0x138e) -#define USB_IDPRODUCT (0x9000) - -#define USB_EP1OUT_ADDR (0x01) -#define USB_EP1OUT_SIZE (16) -#define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80) -#define USB_EP1IN_SIZE (USB_EP1OUT_SIZE) +#define USB_IDVENDOR (0x138e) +#define USB_IDPRODUCT (0x9000) -#define USB_EP2OUT_ADDR (0x02) -#define USB_EP2OUT_SIZE (64) -#define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80) -#define USB_EP2IN_SIZE (USB_EP2OUT_SIZE) -#define USB_EP2BANK_SIZE (512) +#define USB_EP1OUT_ADDR (0x01) +#define USB_EP1OUT_SIZE (16) +#define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80) +#define USB_EP1IN_SIZE (USB_EP1OUT_SIZE) -#define USB_TIMEOUT_MS (3 * 1000) +#define USB_EP2OUT_ADDR (0x02) +#define USB_EP2OUT_SIZE (64) +#define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80) +#define USB_EP2IN_SIZE (USB_EP2OUT_SIZE) +#define USB_EP2BANK_SIZE (512) -#define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff) +#define USB_TIMEOUT_MS (3 * 1000) +#define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff) -#define ST7_PD_NBUSY_LED ST7_PD0 -#define ST7_PD_NRUN_LED ST7_PD1 +#define ST7_PD_NBUSY_LED ST7_PD0 +#define ST7_PD_NRUN_LED ST7_PD1 /* low enables VPP at adapter header, high connects it to GND instead */ -#define ST7_PD_VPP_SEL ST7_PD6 +#define ST7_PD_VPP_SEL ST7_PD6 /* low: VPP = 12v, high: VPP <= 5v */ -#define ST7_PD_VPP_SHDN ST7_PD7 +#define ST7_PD_VPP_SHDN ST7_PD7 /* These pins are connected together */ -#define ST7_PE_ADAPTER_SENSE_IN ST7_PE3 -#define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4 +#define ST7_PE_ADAPTER_SENSE_IN ST7_PE3 +#define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4 /* Symbolic mapping between port pins and numbered IO lines */ -#define ST7_PA_IO1 ST7_PA1 -#define ST7_PA_IO2 ST7_PA2 -#define ST7_PA_IO4 ST7_PA4 -#define ST7_PA_IO8 ST7_PA6 -#define ST7_PA_IO10 ST7_PA7 -#define ST7_PB_IO5 ST7_PB5 -#define ST7_PC_IO9 ST7_PC1 -#define ST7_PC_IO3 ST7_PC2 -#define ST7_PC_IO7 ST7_PC3 -#define ST7_PE_IO6 ST7_PE5 +#define ST7_PA_IO1 ST7_PA1 +#define ST7_PA_IO2 ST7_PA2 +#define ST7_PA_IO4 ST7_PA4 +#define ST7_PA_IO8 ST7_PA6 +#define ST7_PA_IO10 ST7_PA7 +#define ST7_PB_IO5 ST7_PB5 +#define ST7_PC_IO9 ST7_PC1 +#define ST7_PC_IO3 ST7_PC2 +#define ST7_PC_IO7 ST7_PC3 +#define ST7_PE_IO6 ST7_PE5 /* Symbolic mapping between numbered IO lines and adapter signals */ -#define ST7_PA_RTCK ST7_PA_IO0 -#define ST7_PA_NTRST ST7_PA_IO1 -#define ST7_PC_TDI ST7_PC_IO3 -#define ST7_PA_DBGRQ ST7_PA_IO4 -#define ST7_PB_NSRST ST7_PB_IO5 -#define ST7_PE_TMS ST7_PE_IO6 -#define ST7_PC_TCK ST7_PC_IO7 -#define ST7_PC_TDO ST7_PC_IO9 -#define ST7_PA_DBGACK ST7_PA_IO10 +#define ST7_PA_RTCK ST7_PA_IO0 +#define ST7_PA_NTRST ST7_PA_IO1 +#define ST7_PC_TDI ST7_PC_IO3 +#define ST7_PA_DBGRQ ST7_PA_IO4 +#define ST7_PB_NSRST ST7_PB_IO5 +#define ST7_PE_TMS ST7_PE_IO6 +#define ST7_PC_TCK ST7_PC_IO7 +#define ST7_PC_TDO ST7_PC_IO9 +#define ST7_PA_DBGACK ST7_PA_IO10 static usb_dev_handle *pHDev; - /* * ep1 commands are up to USB_EP1OUT_SIZE bytes in length. * This function takes care of zeroing the unused bytes before sending the packet. * Any reply packet is not handled by this function. */ -static -int -ep1_generic_commandl( - usb_dev_handle *pHDev_param, - size_t length, - ... -) { - uint8_t usb_buffer[USB_EP1OUT_SIZE]; - uint8_t *usb_buffer_p; - va_list ap; - int usb_ret; - - if (length > sizeof(usb_buffer)) { +static int ep1_generic_commandl(usb_dev_handle *pHDev_param, size_t length, ...) +{ + uint8_t usb_buffer[USB_EP1OUT_SIZE]; + uint8_t *usb_buffer_p; + va_list ap; + int usb_ret; + + if (length > sizeof(usb_buffer)) length = sizeof(usb_buffer); - } usb_buffer_p = usb_buffer; @@ -132,53 +124,46 @@ ep1_generic_commandl( } memset( - usb_buffer_p, - 0, - sizeof(usb_buffer) - (usb_buffer_p - usb_buffer) - ); + usb_buffer_p, + 0, + sizeof(usb_buffer) - (usb_buffer_p - usb_buffer) + ); usb_ret = usb_bulk_write( pHDev_param, USB_EP1OUT_ADDR, (char *)usb_buffer, sizeof(usb_buffer), USB_TIMEOUT_MS - ); + ); - return(usb_ret); + return usb_ret; } - - #if 0 -static -ssize_t -ep1_memory_read( - usb_dev_handle *pHDev, - uint16_t addr, - size_t length, - uint8_t *buffer -) { - uint8_t usb_buffer[USB_EP1OUT_SIZE]; - int usb_ret; - size_t remain; - ssize_t count; +static ssize_t ep1_memory_read( + usb_dev_handle *pHDev, uint16_t addr, + size_t length, uint8_t *buffer) +{ + uint8_t usb_buffer[USB_EP1OUT_SIZE]; + int usb_ret; + size_t remain; + ssize_t count; usb_buffer[0] = EP1_CMD_MEMORY_READ; memset( - usb_buffer + 4, - 0, - sizeof(usb_buffer) - 4 - ); + usb_buffer + 4, + 0, + sizeof(usb_buffer) - 4 + ); remain = length; count = 0; while (remain) { - if (remain > sizeof(usb_buffer)) { + if (remain > sizeof(usb_buffer)) length = sizeof(usb_buffer); - } else { + else length = remain; - } usb_buffer[1] = addr >> 8; usb_buffer[2] = addr; @@ -188,21 +173,19 @@ ep1_memory_read( pHDev, USB_EP1OUT_ADDR, usb_buffer, sizeof(usb_buffer), USB_TIMEOUT_MS - ); + ); - if (usb_ret < sizeof(usb_buffer)) { + if (usb_ret < sizeof(usb_buffer)) break; - } usb_ret = usb_bulk_read( pHDev, USB_EP1IN_ADDR, buffer, length, USB_TIMEOUT_MS - ); + ); - if (usb_ret < length) { + if (usb_ret < length) break; - } addr += length; buffer += length; @@ -210,24 +193,17 @@ ep1_memory_read( remain -= length; } - return(count); + return count; } #endif - - -static -ssize_t -ep1_memory_write( - usb_dev_handle *pHDev_param, - uint16_t addr, - size_t length, - uint8_t const *buffer -) { - uint8_t usb_buffer[USB_EP1OUT_SIZE]; - int usb_ret; - size_t remain; - ssize_t count; +static ssize_t ep1_memory_write(usb_dev_handle *pHDev_param, uint16_t addr, + size_t length, uint8_t const *buffer) +{ + uint8_t usb_buffer[USB_EP1OUT_SIZE]; + int usb_ret; + size_t remain; + ssize_t count; usb_buffer[0] = EP1_CMD_MEMORY_WRITE; @@ -235,35 +211,33 @@ ep1_memory_write( count = 0; while (remain) { - if (remain > (sizeof(usb_buffer) - 4)) { + if (remain > (sizeof(usb_buffer) - 4)) length = (sizeof(usb_buffer) - 4); - } else { + else length = remain; - } usb_buffer[1] = addr >> 8; usb_buffer[2] = addr; usb_buffer[3] = length; memcpy( - usb_buffer + 4, - buffer, - length - ); + usb_buffer + 4, + buffer, + length + ); memset( - usb_buffer + 4 + length, - 0, - sizeof(usb_buffer) - 4 - length - ); + usb_buffer + 4 + length, + 0, + sizeof(usb_buffer) - 4 - length + ); usb_ret = usb_bulk_write( pHDev_param, USB_EP1OUT_ADDR, (char *)usb_buffer, sizeof(usb_buffer), USB_TIMEOUT_MS - ); + ); - if ((size_t)usb_ret < sizeof(usb_buffer)) { + if ((size_t)usb_ret < sizeof(usb_buffer)) break; - } addr += length; buffer += length; @@ -271,27 +245,21 @@ ep1_memory_write( remain -= length; } - return(count); + return count; } #if 0 -static -ssize_t -ep1_memory_writel( - usb_dev_handle *pHDev, - uint16_t addr, - size_t length, - ... -) { - uint8_t buffer[USB_EP1OUT_SIZE - 4]; - uint8_t *buffer_p; - va_list ap; - size_t remain; - - if (length > sizeof(buffer)) { +static ssize_t ep1_memory_writel(usb_dev_handle *pHDev, uint16_t addr, + size_t length, ...) +{ + uint8_t buffer[USB_EP1OUT_SIZE - 4]; + uint8_t *buffer_p; + va_list ap; + size_t remain; + + if (length > sizeof(buffer)) length = sizeof(buffer); - } remain = length; buffer_p = buffer; @@ -302,40 +270,34 @@ ep1_memory_writel( remain--; } - return(ep1_memory_write(pHDev, addr, length, buffer)); + return ep1_memory_write(pHDev, addr, length, buffer); } #endif +#define DTCLOAD_COMMENT (0) +#define DTCLOAD_ENTRY (1) +#define DTCLOAD_LOAD (2) +#define DTCLOAD_RUN (3) +#define DTCLOAD_LUT_START (4) +#define DTCLOAD_LUT (5) -#define DTCLOAD_COMMENT (0) -#define DTCLOAD_ENTRY (1) -#define DTCLOAD_LOAD (2) -#define DTCLOAD_RUN (3) -#define DTCLOAD_LUT_START (4) -#define DTCLOAD_LUT (5) - -#define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO +#define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO /* This gets set by the DTC loader */ static uint8_t dtc_entry_download; - /* The buffer is specially formatted to represent a valid image to load into the DTC. */ -static -int -dtc_load_from_buffer( - usb_dev_handle *pHDev_param, - const uint8_t *buffer, - size_t length -) { +static int dtc_load_from_buffer(usb_dev_handle *pHDev_param, const uint8_t *buffer, + size_t length) +{ struct header_s { - uint8_t type; - uint8_t length; + uint8_t type; + uint8_t length; }; - int usb_err; - struct header_s *header; - uint8_t lut_start = 0xc0; + int usb_err; + struct header_s *header; + uint8_t lut_start = 0xc0; dtc_entry_download = 0; @@ -343,8 +305,9 @@ dtc_load_from_buffer( usb_err = ep1_generic_commandl( pHDev_param, 1, EP1_CMD_DTC_STOP - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; while (length) { if (length < sizeof(*header)) { @@ -362,82 +325,83 @@ dtc_load_from_buffer( } switch (header->type) { - case DTCLOAD_COMMENT: - break; - - case DTCLOAD_ENTRY: - /* store entry addresses somewhere */ - if (!strncmp("download", (char *)buffer + 1, 8)) { - dtc_entry_download = buffer[0]; - } - break; - - case DTCLOAD_LOAD: - /* Send the DTC program to ST7 RAM. */ - usb_err = ep1_memory_write( - pHDev_param, - DTC_LOAD_BUFFER, - header->length + 1, buffer - ); - if (usb_err < 0) return(usb_err); - - /* Load it into the DTC. */ - usb_err = ep1_generic_commandl( - pHDev_param, 3, - EP1_CMD_DTC_LOAD, - (DTC_LOAD_BUFFER >> 8), - DTC_LOAD_BUFFER - ); - if (usb_err < 0) return(usb_err); - - break; - - case DTCLOAD_RUN: - usb_err = ep1_generic_commandl( - pHDev_param, 3, - EP1_CMD_DTC_CALL, - buffer[0], - EP1_CMD_DTC_WAIT - ); - if (usb_err < 0) return(usb_err); - - break; - - case DTCLOAD_LUT_START: - lut_start = buffer[0]; - break; - - case DTCLOAD_LUT: - usb_err = ep1_memory_write( - pHDev_param, - ST7_USB_BUF_EP0OUT + lut_start, - header->length + 1, buffer - ); - if (usb_err < 0) return(usb_err); - break; - - default: - LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type); - exit(1); - break; + case DTCLOAD_COMMENT: + break; + + case DTCLOAD_ENTRY: + /* store entry addresses somewhere */ + if (!strncmp("download", (char *)buffer + 1, 8)) + dtc_entry_download = buffer[0]; + break; + + case DTCLOAD_LOAD: + /* Send the DTC program to ST7 RAM. */ + usb_err = ep1_memory_write( + pHDev_param, + DTC_LOAD_BUFFER, + header->length + 1, buffer + ); + if (usb_err < 0) + return usb_err; + + /* Load it into the DTC. */ + usb_err = ep1_generic_commandl( + pHDev_param, 3, + EP1_CMD_DTC_LOAD, + (DTC_LOAD_BUFFER >> 8), + DTC_LOAD_BUFFER + ); + if (usb_err < 0) + return usb_err; + + break; + + case DTCLOAD_RUN: + usb_err = ep1_generic_commandl( + pHDev_param, 3, + EP1_CMD_DTC_CALL, + buffer[0], + EP1_CMD_DTC_WAIT + ); + if (usb_err < 0) + return usb_err; + + break; + + case DTCLOAD_LUT_START: + lut_start = buffer[0]; + break; + + case DTCLOAD_LUT: + usb_err = ep1_memory_write( + pHDev_param, + ST7_USB_BUF_EP0OUT + lut_start, + header->length + 1, buffer + ); + if (usb_err < 0) + return usb_err; + break; + + default: + LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type); + exit(1); + break; } buffer += (header->length + 1); length -= (header->length + 1); } - return(0); + return 0; } - /* * Start the DTC running in download mode (waiting for 512 byte command packets on ep2). */ -static -int -dtc_start_download(void) { - int usb_err; - uint8_t ep2txr; +static int dtc_start_download(void) +{ + int usb_err; + uint8_t ep2txr; /* set up for download mode and make sure EP2 is set up to transmit */ usb_err = ep1_generic_commandl( @@ -450,16 +414,18 @@ dtc_start_download(void) { ST7_EP2TXR >> 8, ST7_EP2TXR, 1 - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; /* read back ep2txr */ usb_err = usb_bulk_read( pHDev, USB_EP1IN_ADDR, (char *)&ep2txr, 1, USB_TIMEOUT_MS - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; usb_err = ep1_generic_commandl( pHDev, 13, @@ -477,32 +443,31 @@ dtc_start_download(void) { EP1_CMD_DTC_CALL, /* start running the DTC */ dtc_entry_download, EP1_CMD_DTC_GET_CACHED_STATUS - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; /* wait for completion */ usb_err = usb_bulk_read( pHDev, USB_EP1IN_ADDR, (char *)&ep2txr, 1, USB_TIMEOUT_MS - ); + ); - return(usb_err); + return usb_err; } - -static -int -dtc_run_download( - usb_dev_handle *pHDev_param, - uint8_t *command_buffer, - int command_buffer_size, - uint8_t *reply_buffer, - int reply_buffer_size -) { +static int dtc_run_download( + usb_dev_handle *pHDev_param, + uint8_t *command_buffer, + int command_buffer_size, + uint8_t *reply_buffer, + int reply_buffer_size + ) +{ char dtc_status; - int usb_err; - int i; + int usb_err; + int i; LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size); @@ -511,12 +476,13 @@ dtc_run_download( USB_EP2OUT_ADDR, (char *)command_buffer, USB_EP2BANK_SIZE, USB_TIMEOUT_MS - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; /* Wait for DTC to finish running command buffer */ - for (i = 10;;) { + for (i = 10;; ) { usb_err = ep1_generic_commandl( pHDev_param, 4, @@ -524,22 +490,25 @@ dtc_run_download( DTC_STATUS_POLL_BYTE >> 8, DTC_STATUS_POLL_BYTE, 1 - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; usb_err = usb_bulk_read( pHDev_param, USB_EP1IN_ADDR, &dtc_status, 1, USB_TIMEOUT_MS - ); - if (usb_err < 0) return(usb_err); + ); + if (usb_err < 0) + return usb_err; - if (dtc_status & 0x01) break; + if (dtc_status & 0x01) + break; if (!--i) { LOG_ERROR("too many retries waiting for DTC status"); - return(-ETIMEDOUT); + return -ETIMEDOUT; } } @@ -550,34 +519,35 @@ dtc_run_download( USB_EP2IN_ADDR, (char *)reply_buffer, reply_buffer_size, USB_TIMEOUT_MS - ); + ); if (usb_err < reply_buffer_size) { LOG_ERROR("Read of endpoint 2 returned %d, expected %d", - usb_err, reply_buffer_size - ); - return(usb_err); + usb_err, reply_buffer_size + ); + return usb_err; } } - return(usb_err); + return usb_err; } - /* - * The dtc reply queue is a singly linked list that describes what to do with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate these entries. + * The dtc reply queue is a singly linked list that describes what to do + * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate + * these entries. */ struct dtc_reply_queue_entry { - struct dtc_reply_queue_entry *next; - struct jtag_command *cmd; /* the command that resulted in this entry */ + struct dtc_reply_queue_entry *next; + struct jtag_command *cmd; /* the command that resulted in this entry */ struct { - uint8_t *buffer; /* the scan buffer */ - int size; /* size of the scan buffer in bits */ - int offset; /* how many bits were already done before this? */ - int length; /* how many bits are processed in this operation? */ - enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */ + uint8_t *buffer; /* the scan buffer */ + int size; /* size of the scan buffer in bits */ + int offset; /* how many bits were already done before this? */ + int length; /* how many bits are processed in this operation? */ + enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */ } scan; }; @@ -587,51 +557,39 @@ struct dtc_reply_queue_entry { * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue. */ -static -struct { - struct dtc_reply_queue_entry *rq_head; - struct dtc_reply_queue_entry *rq_tail; - uint32_t cmd_index; - uint32_t reply_index; - uint8_t cmd_buffer[USB_EP2BANK_SIZE]; +static struct { + struct dtc_reply_queue_entry *rq_head; + struct dtc_reply_queue_entry *rq_tail; + uint32_t cmd_index; + uint32_t reply_index; + uint8_t cmd_buffer[USB_EP2BANK_SIZE]; } dtc_queue; - /* - * The tap state queue is for accumulating TAP state changes wiithout needlessly flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to the dtc_queue. + * The tap state queue is for accumulating TAP state changes wiithout needlessly + * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to + * the dtc_queue. */ -static -struct { - uint32_t length; - uint32_t buffer; +static struct { + uint32_t length; + uint32_t buffer; } tap_state_queue; - - -static -int -dtc_queue_init(void) { +static int dtc_queue_init(void) +{ dtc_queue.rq_head = NULL; dtc_queue.rq_tail = NULL; dtc_queue.cmd_index = 0; dtc_queue.reply_index = 0; - return(0); + return 0; } - -static -inline -struct dtc_reply_queue_entry * -dtc_queue_enqueue_reply( - enum scan_type type, - uint8_t *buffer, - int size, - int offset, - int length, - struct jtag_command *cmd -) { - struct dtc_reply_queue_entry *rq_entry; +static inline struct dtc_reply_queue_entry *dtc_queue_enqueue_reply( + enum scan_type type, uint8_t *buffer, int size, int offset, + int length, struct jtag_command *cmd) +{ + struct dtc_reply_queue_entry *rq_entry; rq_entry = malloc(sizeof(struct dtc_reply_queue_entry)); if (rq_entry != NULL) { @@ -651,26 +609,25 @@ dtc_queue_enqueue_reply( dtc_queue.rq_tail = rq_entry; } - return(rq_entry); + return rq_entry; } - /* - * Running the queue means that any pending command buffer is run and any reply data dealt with. The command buffer is then cleared for subsequent processing. + * Running the queue means that any pending command buffer is run + * and any reply data dealt with. The command buffer is then cleared for subsequent processing. * The queue is automatically run by append when it is necessary to get space for the append. */ -static -int -dtc_queue_run(void) { - struct dtc_reply_queue_entry *rq_p, *rq_next; - int retval; - int usb_err; - int bit_cnt; - int x; - uint8_t *dtc_p, *tdo_p; - uint8_t dtc_mask, tdo_mask; - uint8_t reply_buffer[USB_EP2IN_SIZE]; +static int dtc_queue_run(void) +{ + struct dtc_reply_queue_entry *rq_p, *rq_next; + int retval; + int usb_err; + int bit_cnt; + int x; + uint8_t *dtc_p, *tdo_p; + uint8_t dtc_mask, tdo_mask; + uint8_t reply_buffer[USB_EP2IN_SIZE]; assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0)); assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE); @@ -678,14 +635,15 @@ dtc_queue_run(void) { retval = ERROR_OK; - if (dtc_queue.cmd_index < 1) return(retval); + if (dtc_queue.cmd_index < 1) + return retval; dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP; usb_err = dtc_run_download(pHDev, dtc_queue.cmd_buffer, dtc_queue.cmd_index, reply_buffer, dtc_queue.reply_index - ); + ); if (usb_err < 0) { LOG_ERROR("dtc_run_download: %s", usb_strerror()); exit(1); @@ -695,13 +653,17 @@ dtc_queue_run(void) { /* process the reply, which empties the reply queue and frees its entries */ dtc_p = reply_buffer; - /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It was that or craft a function to do the reversal, and that wouldn't work with bit-stuffing (supplying extra bits to use mostly byte operations), or any other scheme which would throw the byte alignment off. */ + /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the + *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It + *was that or craft a function to do the reversal, and that wouldn't work with + *bit-stuffing (supplying extra bits to use mostly byte operations), or any other + *scheme which would throw the byte alignment off. */ for ( - rq_p = dtc_queue.rq_head; - rq_p != NULL; - rq_p = rq_next - ) { + rq_p = dtc_queue.rq_head; + rq_p != NULL; + rq_p = rq_next + ) { tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8); tdo_mask = 1 << (rq_p->scan.offset % 8); @@ -713,15 +675,14 @@ dtc_queue_run(void) { dtc_mask = 1 << (8 - 1); for ( - ; - bit_cnt; - bit_cnt-- - ) { - if (*dtc_p & dtc_mask) { + ; + bit_cnt; + bit_cnt-- + ) { + if (*dtc_p & dtc_mask) *tdo_p |= tdo_mask; - } else { - *tdo_p &=~ tdo_mask; - } + else + *tdo_p &= ~tdo_mask; dtc_mask >>= 1; if (dtc_mask == 0) { @@ -739,27 +700,24 @@ dtc_queue_run(void) { /* extra bits or last bit */ x = *dtc_p++; - if (( - rq_p->scan.type == SCAN_IN - ) && ( + if ((rq_p->scan.type == SCAN_IN) && ( rq_p->scan.offset != rq_p->scan.size - 1 - )) { - /* extra bits were sent as a full byte with padding on the end */ + )) { + /* extra bits were sent as a full byte with padding on the + *end */ dtc_mask = 1 << (8 - 1); - } else { + } else dtc_mask = 1 << (bit_cnt - 1); - } for ( - ; - bit_cnt; - bit_cnt-- - ) { - if (x & dtc_mask) { + ; + bit_cnt; + bit_cnt-- + ) { + if (x & dtc_mask) *tdo_p |= tdo_mask; - } else { - *tdo_p &=~ tdo_mask; - } + else + *tdo_p &= ~tdo_mask; dtc_mask >>= 1; @@ -774,9 +732,9 @@ dtc_queue_run(void) { if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) { /* feed scan buffer back into openocd and free it */ - if (jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) { + if (jtag_read_buffer(rq_p->scan.buffer, + rq_p->cmd->cmd.scan) != ERROR_OK) retval = ERROR_JTAG_QUEUE_FAILED; - } free(rq_p->scan.buffer); } @@ -787,22 +745,17 @@ dtc_queue_run(void) { dtc_queue.rq_tail = NULL; } - /* reset state for new appends */ dtc_queue.cmd_index = 0; dtc_queue.reply_index = 0; - return(retval); + return retval; } /* runs the queue if it cannot take reserved_cmd bytes of command data * or reserved_reply bytes of reply data */ -static -int -dtc_queue_run_if_full( - int reserved_cmd, - int reserved_reply -) { +static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply) +{ /* reserve one additional byte for the STOP cmd appended during run */ if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE) return dtc_queue_run(); @@ -813,33 +766,30 @@ dtc_queue_run_if_full( return ERROR_OK; } -static -int -tap_state_queue_init(void) { +static int tap_state_queue_init(void) +{ tap_state_queue.length = 0; tap_state_queue.buffer = 0; - return(0); + return 0; } - -static -int -tap_state_queue_run(void) { - int i; - int bits; - uint8_t byte_param; - int retval; +static int tap_state_queue_run(void) +{ + int i; + int bits; + uint8_t byte_param; + int retval; retval = 0; - if (!tap_state_queue.length) return(retval); + if (!tap_state_queue.length) + return retval; bits = 1; byte_param = 0; - for (i = tap_state_queue.length; i--;) { + for (i = tap_state_queue.length; i--; ) { byte_param <<= 1; - if (tap_state_queue.buffer & 1) { + if (tap_state_queue.buffer & 1) byte_param |= 1; - } if ((bits >= 8) || !i) { byte_param <<= (8 - bits); @@ -849,74 +799,64 @@ tap_state_queue_run(void) { #ifdef USE_HARDWARE_SHIFTER_FOR_TMS if (bits == 8) { dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TMS_BYTES(1); + DTC_CMD_SHIFT_TMS_BYTES(1); } else { #endif - dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TMS_BITS(bits); + dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = + DTC_CMD_SHIFT_TMS_BITS(bits); #ifdef USE_HARDWARE_SHIFTER_FOR_TMS - } + } #endif dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - byte_param; + byte_param; byte_param = 0; bits = 1; - } else { + } else bits++; - } tap_state_queue.buffer >>= 1; } retval = tap_state_queue_init(); - return(retval); + return retval; } - -static -int -tap_state_queue_append( - uint8_t tms -) { - int retval; +static int tap_state_queue_append(uint8_t tms) +{ + int retval; if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) { retval = tap_state_queue_run(); - if (retval != 0) return(retval); + if (retval != 0) + return retval; } - if (tms) { + if (tms) tap_state_queue.buffer |= (1 << tap_state_queue.length); - } tap_state_queue.length++; - return(0); + return 0; } - -static -void rlink_end_state(tap_state_t state) +static void rlink_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } } - -static -void rlink_state_move(void) { +static void rlink_state_move(void) +{ int i = 0, tms = 0; uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state()); int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - for (i = 0; i < tms_count; i++) - { + for (i = 0; i < tms_count; i++) { tms = (tms_scan >> i) & 1; tap_state_queue_append(tms); } @@ -924,27 +864,22 @@ void rlink_state_move(void) { tap_set_state(tap_get_end_state()); } -static -void rlink_path_move(struct pathmove_command *cmd) +static void rlink_path_move(struct pathmove_command *cmd) { int num_states = cmd->num_states; int state_count; int tms = 0; state_count = 0; - while (num_states) - { + while (num_states) { if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) - { tms = 0; - } else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count]) - { tms = 1; - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count])); + else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(tap_get_state()), + tap_state_name(cmd->path[state_count])); exit(-1); } @@ -958,26 +893,21 @@ void rlink_path_move(struct pathmove_command *cmd) tap_set_end_state(tap_get_state()); } - -static -void rlink_runtest(int num_cycles) +static void rlink_runtest(int num_cycles) { int i; tap_state_t saved_end_state = tap_get_end_state(); /* only do a state_move when we're not already in RTI */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { rlink_end_state(TAP_IDLE); rlink_state_move(); } /* execute num_cycles */ for (i = 0; i < num_cycles; i++) - { tap_state_queue_append(0); - } /* finish in end_state */ rlink_end_state(saved_end_state); @@ -985,13 +915,11 @@ void rlink_runtest(int num_cycles) rlink_state_move(); } - /* (1) assert or (0) deassert reset lines */ -static -void rlink_reset(int trst, int srst) +static void rlink_reset(int trst, int srst) { - uint8_t bitmap; - int usb_err; + uint8_t bitmap; + int usb_err; /* Read port A for bit op */ usb_err = ep1_generic_commandl( @@ -1000,7 +928,7 @@ void rlink_reset(int trst, int srst) ST7_PADR >> 8, ST7_PADR, 1 - ); + ); if (usb_err < 0) { LOG_ERROR("%s", usb_strerror()); exit(1); @@ -1010,20 +938,20 @@ void rlink_reset(int trst, int srst) pHDev, USB_EP1IN_ADDR, (char *)&bitmap, 1, USB_TIMEOUT_MS - ); + ); if (usb_err < 1) { LOG_ERROR("%s", usb_strerror()); exit(1); } - if (trst) { + if (trst) bitmap &= ~ST7_PA_NTRST; - } else { + else bitmap |= ST7_PA_NTRST; - } - /* Write port A and read port B for bit op */ - /* port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0 and assert NSRST by setting DDR to 1. */ + /* Write port A and read port B for bit op + * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0 + *and assert NSRST by setting DDR to 1. */ usb_err = ep1_generic_commandl( pHDev, 9, EP1_CMD_MEMORY_WRITE, @@ -1035,7 +963,7 @@ void rlink_reset(int trst, int srst) ST7_PBDDR >> 8, ST7_PBDDR, 1 - ); + ); if (usb_err < 0) { LOG_ERROR("%s", usb_strerror()); exit(1); @@ -1045,17 +973,16 @@ void rlink_reset(int trst, int srst) pHDev, USB_EP1IN_ADDR, (char *)&bitmap, 1, USB_TIMEOUT_MS - ); + ); if (usb_err < 1) { LOG_ERROR("%s", usb_strerror()); exit(1); } - if (srst) { + if (srst) bitmap |= ST7_PB_NSRST; - } else { + else bitmap &= ~ST7_PB_NSRST; - } /* write port B and read dummy to ensure completion before returning */ usb_err = ep1_generic_commandl( @@ -1066,7 +993,7 @@ void rlink_reset(int trst, int srst) 1, bitmap, EP1_CMD_DTC_GET_CACHED_STATUS - ); + ); if (usb_err < 0) { LOG_ERROR("%s", usb_strerror()); exit(1); @@ -1076,33 +1003,27 @@ void rlink_reset(int trst, int srst) pHDev, USB_EP1IN_ADDR, (char *)&bitmap, 1, USB_TIMEOUT_MS - ); + ); if (usb_err < 1) { LOG_ERROR("%s", usb_strerror()); exit(1); } } - -static -int -rlink_scan( - struct jtag_command *cmd, - enum scan_type type, - uint8_t *buffer, - int scan_size -) { - bool ir_scan; - tap_state_t saved_end_state; - int byte_bits; - int extra_bits; - int chunk_bits; - int chunk_bytes; - int x; - - int tdi_bit_offset; - uint8_t tdi_mask, *tdi_p; - uint8_t dtc_mask; +static int rlink_scan(struct jtag_command *cmd, enum scan_type type, + uint8_t *buffer, int scan_size) +{ + bool ir_scan; + tap_state_t saved_end_state; + int byte_bits; + int extra_bits; + int chunk_bits; + int chunk_bytes; + int x; + + int tdi_bit_offset; + uint8_t tdi_mask, *tdi_p; + uint8_t dtc_mask; if (scan_size < 1) { LOG_ERROR("scan_size cannot be less than 1 bit"); @@ -1112,11 +1033,8 @@ rlink_scan( ir_scan = cmd->cmd.scan->ir_scan; /* Move to the proper state before starting to shift TDI/TDO. */ - if (!( - (!ir_scan && (tap_get_state() == TAP_DRSHIFT)) - || - (ir_scan && (tap_get_state() == TAP_IRSHIFT)) - )) { + if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || + (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) { saved_end_state = tap_get_end_state(); rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT); rlink_state_move(); @@ -1129,16 +1047,15 @@ rlink_scan( #if 0 printf("scan_size = %d, type = 0x%x\n", scan_size, type); { - int i; + int i; - /* clear unused bits in scan buffer for ease of debugging */ - /* (it makes diffing output easier) */ + /* clear unused bits in scan buffer for ease of debugging + * (it makes diffing output easier) */ buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1); printf("before scan:"); - for (i = 0; i < (scan_size + 7) / 8; i++) { + for (i = 0; i < (scan_size + 7) / 8; i++) printf(" %02x", buffer[i]); - } printf("\n"); } #endif @@ -1153,8 +1070,9 @@ rlink_scan( tdi_mask = 1; if (extra_bits && (type == SCAN_OUT)) { - /* Schedule any extra bits into the DTC command buffer, padding as needed */ - /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */ + /* Schedule any extra bits into the DTC command buffer, padding as needed + * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will + *fall off the end */ /* make sure there's room for two cmd bytes */ dtc_queue_run_if_full(2, 0); @@ -1163,9 +1081,8 @@ rlink_scan( dtc_mask = 1 << (extra_bits - 1); while (extra_bits--) { - if (*tdi_p & tdi_mask) { + if (*tdi_p & tdi_mask) x |= dtc_mask; - } dtc_mask >>= 1; @@ -1177,7 +1094,7 @@ rlink_scan( } dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TDI_BYTES(1); + DTC_CMD_SHIFT_TDI_BYTES(1); dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x; } @@ -1190,18 +1107,21 @@ rlink_scan( chunk_bits = byte_bits; /* we can only use up to 16 bytes at a time */ - if (chunk_bits > (16 * 8)) chunk_bits = (16 * 8); + if (chunk_bits > (16 * 8)) + chunk_bits = (16 * 8); if (type != SCAN_IN) { /* how much is there room for, considering stop and byte op? */ x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8; - if (chunk_bits > x) chunk_bits = x; + if (chunk_bits > x) + chunk_bits = x; } if (type != SCAN_OUT) { /* how much is there room for in the reply buffer? */ x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8; - if (chunk_bits > x) chunk_bits = x; + if (chunk_bits > x) + chunk_bits = x; } /* so the loop will end */ @@ -1212,7 +1132,7 @@ rlink_scan( type, buffer, scan_size, tdi_bit_offset, chunk_bits, cmd - ) == NULL) { + ) == NULL) { LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno)); exit(1); } @@ -1225,15 +1145,15 @@ rlink_scan( chunk_bytes = chunk_bits / 8; switch (type) { - case SCAN_IN: - x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes); - break; - case SCAN_OUT: - x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes); - break; - default: - x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes); - break; + case SCAN_IN: + x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes); + break; + case SCAN_OUT: + x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes); + break; + default: + x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes); + break; } dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x; @@ -1242,9 +1162,8 @@ rlink_scan( dtc_mask = 1 << (8 - 1); while (chunk_bits--) { - if (*tdi_p & tdi_mask) { + if (*tdi_p & tdi_mask) x |= dtc_mask; - } dtc_mask >>= 1; if (dtc_mask == 0) { @@ -1273,7 +1192,7 @@ rlink_scan( type, buffer, scan_size, tdi_bit_offset, extra_bits, cmd - ) == NULL) { + ) == NULL) { LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno)); exit(1); } @@ -1284,19 +1203,18 @@ rlink_scan( if (type == SCAN_IN) { dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TDO_BYTES(1); + DTC_CMD_SHIFT_TDO_BYTES(1); } else { dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TDIO_BITS(extra_bits); + DTC_CMD_SHIFT_TDIO_BITS(extra_bits); x = 0; dtc_mask = 1 << (8 - 1); while (extra_bits--) { - if (*tdi_p & tdi_mask) { + if (*tdi_p & tdi_mask) x |= dtc_mask; - } dtc_mask >>= 1; @@ -1319,14 +1237,14 @@ rlink_scan( if (type == SCAN_OUT) { dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0); + DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0); } else { if (dtc_queue_enqueue_reply( type, buffer, scan_size, tdi_bit_offset, 1, cmd - ) == NULL) { + ) == NULL) { LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno)); exit(1); } @@ -1334,22 +1252,21 @@ rlink_scan( dtc_queue.reply_index++; dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = - DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1); + DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1); } /* Move to pause state */ tap_state_queue_append(0); tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE); - if (tap_get_state() != tap_get_end_state()) rlink_state_move(); + if (tap_get_state() != tap_get_end_state()) + rlink_state_move(); - return(0); + return 0; } - -static -int rlink_execute_queue(void) +static int rlink_execute_queue(void) { - struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ + struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ int scan_size; enum scan_type type; uint8_t *buffer; @@ -1361,122 +1278,129 @@ int rlink_execute_queue(void) #ifndef AUTOMATIC_BUSY_LED /* turn LED on */ ep1_generic_commandl(pHDev, 2, - EP1_CMD_SET_PORTD_LEDS, - ~(ST7_PD_NBUSY_LED) - ); + EP1_CMD_SET_PORTD_LEDS, + ~(ST7_PD_NBUSY_LED) + ); #endif - while (cmd) - { - switch (cmd->type) - { - case JTAG_RUNTEST: - case JTAG_TLR_RESET: - case JTAG_PATHMOVE: - case JTAG_SCAN: - break; - - default: - /* some events, such as resets, need a queue flush to ensure consistency */ - tap_state_queue_run(); - dtc_queue_run(); - break; + while (cmd) { + switch (cmd->type) { + case JTAG_RUNTEST: + case JTAG_TLR_RESET: + case JTAG_PATHMOVE: + case JTAG_SCAN: + break; + + default: + /* some events, such as resets, need a queue flush to ensure + *consistency */ + tap_state_queue_run(); + dtc_queue_run(); + break; } - switch (cmd->type) - { - case JTAG_RESET: + switch (cmd->type) { + case JTAG_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); + LOG_DEBUG("reset trst: %i srst %i", + cmd->cmd.reset->trst, + cmd->cmd.reset->srst); #endif - if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) - { - tap_set_state(TAP_RESET); - } - rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); - break; - case JTAG_RUNTEST: + if ((cmd->cmd.reset->trst == 1) || + (cmd->cmd.reset->srst && + (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) + tap_set_state(TAP_RESET); + rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); + break; + case JTAG_RUNTEST: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state); + LOG_DEBUG("runtest %i cycles, end in %i", + cmd->cmd.runtest->num_cycles, + cmd->cmd.runtest->end_state); #endif - if (cmd->cmd.runtest->end_state != -1) - rlink_end_state(cmd->cmd.runtest->end_state); - rlink_runtest(cmd->cmd.runtest->num_cycles); - break; - case JTAG_TLR_RESET: + if (cmd->cmd.runtest->end_state != -1) + rlink_end_state(cmd->cmd.runtest->end_state); + rlink_runtest(cmd->cmd.runtest->num_cycles); + break; + case JTAG_TLR_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state); + LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state); #endif - if (cmd->cmd.statemove->end_state != -1) - rlink_end_state(cmd->cmd.statemove->end_state); - rlink_state_move(); - break; - case JTAG_PATHMOVE: + if (cmd->cmd.statemove->end_state != -1) + rlink_end_state(cmd->cmd.statemove->end_state); + rlink_state_move(); + break; + case JTAG_PATHMOVE: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); + LOG_DEBUG("pathmove: %i states, end in %i", + cmd->cmd.pathmove->num_states, + cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); #endif - rlink_path_move(cmd->cmd.pathmove); - break; - case JTAG_SCAN: + rlink_path_move(cmd->cmd.pathmove); + break; + case JTAG_SCAN: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state); + LOG_DEBUG("%s scan end in %i", + (cmd->cmd.scan->ir_scan) ? "IR" : "DR", + cmd->cmd.scan->end_state); #endif - if (cmd->cmd.scan->end_state != -1) - rlink_end_state(cmd->cmd.scan->end_state); - scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer); - type = jtag_scan_type(cmd->cmd.scan); - if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) { - retval = ERROR_FAIL; - } - break; - case JTAG_SLEEP: + if (cmd->cmd.scan->end_state != -1) + rlink_end_state(cmd->cmd.scan->end_state); + scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer); + type = jtag_scan_type(cmd->cmd.scan); + if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) + retval = ERROR_FAIL; + break; + case JTAG_SLEEP: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("sleep %i", cmd->cmd.sleep->us); + LOG_DEBUG("sleep %i", cmd->cmd.sleep->us); #endif - jtag_sleep(cmd->cmd.sleep->us); - break; - default: - LOG_ERROR("BUG: unknown JTAG command type encountered"); - exit(-1); + jtag_sleep(cmd->cmd.sleep->us); + break; + default: + LOG_ERROR("BUG: unknown JTAG command type encountered"); + exit(-1); } cmd = cmd->next; } - /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */ + /* Flush the DTC queue to make sure any pending reads have been done before exiting this + *function */ tap_state_queue_run(); tmp_retval = dtc_queue_run(); - if (tmp_retval != ERROR_OK) { + if (tmp_retval != ERROR_OK) retval = tmp_retval; - } #ifndef AUTOMATIC_BUSY_LED /* turn LED onff */ ep1_generic_commandl(pHDev, 2, - EP1_CMD_SET_PORTD_LEDS, - ~0 - ); + EP1_CMD_SET_PORTD_LEDS, + ~0 + ); #endif return retval; } +/* Using an unindexed table because it is infrequently accessed and it is short. The table must be + *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */ -/* Using an unindexed table because it is infrequently accessed and it is short. The table must be in order of ascending speed (and descending prescaler), as it is scanned in reverse. */ - -static -int rlink_speed(int speed) +static int rlink_speed(int speed) { - int i; + int i; if (speed == 0) { /* fastest speed */ speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler; } - for (i = rlink_speed_table_size; i--;) { + for (i = rlink_speed_table_size; i--; ) { if (rlink_speed_table[i].prescaler == speed) { - if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, rlink_speed_table[i].dtc_size) != 0) { - LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".", speed); + if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, + rlink_speed_table[i].dtc_size) != 0) { + LOG_ERROR( + "An error occurred while trying to load DTC code for speed \"%d\".", + speed); exit(1); } @@ -1490,56 +1414,46 @@ int rlink_speed(int speed) } LOG_ERROR("%d is not a supported speed", speed); - return(ERROR_FAIL); + return ERROR_FAIL; } +static int rlink_speed_div(int speed, int *khz) +{ + int i; -static -int rlink_speed_div( - int speed, - int *khz -) { - int i; - - for (i = rlink_speed_table_size; i--;) { + for (i = rlink_speed_table_size; i--; ) { if (rlink_speed_table[i].prescaler == speed) { *khz = rlink_speed_table[i].khz; - return(ERROR_OK); + return ERROR_OK; } } LOG_ERROR("%d is not a supported speed", speed); - return(ERROR_FAIL); + return ERROR_FAIL; } - -static -int rlink_khz( - int khz, - int *speed -) { - int i; +static int rlink_khz(int khz, int *speed) +{ + int i; if (khz == 0) { LOG_ERROR("RCLK not supported"); return ERROR_FAIL; } - for (i = rlink_speed_table_size; i--;) { + for (i = rlink_speed_table_size; i--; ) { if (rlink_speed_table[i].khz <= khz) { *speed = rlink_speed_table[i].prescaler; - return(ERROR_OK); + return ERROR_OK; } } LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz); *speed = rlink_speed_table[0].prescaler; - return(ERROR_OK); + return ERROR_OK; } - -static -int rlink_init(void) +static int rlink_init(void) { int i, j, retries; uint8_t reply_buffer[USB_EP1IN_SIZE]; @@ -1551,13 +1465,11 @@ int rlink_init(void) return ERROR_FAIL; struct usb_device *dev = usb_device(pHDev); - if (dev->descriptor.bNumConfigurations > 1) - { + if (dev->descriptor.bNumConfigurations > 1) { LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do..."); return ERROR_FAIL; } - if (dev->config->bNumInterfaces > 1) - { + if (dev->config->bNumInterfaces > 1) { LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do..."); return ERROR_FAIL; } @@ -1568,32 +1480,26 @@ int rlink_init(void) usb_set_configuration(pHDev, dev->config[0].bConfigurationValue); retries = 3; - do - { - i = usb_claim_interface(pHDev,0); - if (i) - { + do { + i = usb_claim_interface(pHDev, 0); + if (i) { LOG_ERROR("usb_claim_interface: %s", usb_strerror()); #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP j = usb_detach_kernel_driver_np(pHDev, 0); if (j) LOG_ERROR("detach kernel driver: %s", usb_strerror()); #endif - } - else - { + } else { LOG_DEBUG("interface claimed!"); break; } } while (--retries); - if (i) - { + if (i) { LOG_ERROR("Initialisation failed."); return ERROR_FAIL; } - if (usb_set_altinterface(pHDev,0) != 0) - { + if (usb_set_altinterface(pHDev, 0) != 0) { LOG_ERROR("Failed to set interface."); return ERROR_FAIL; } @@ -1611,133 +1517,136 @@ int rlink_init(void) j = ep1_generic_commandl( pHDev, 1, EP1_CMD_GET_FWREV - ); + ); if (j < USB_EP1OUT_SIZE) { LOG_ERROR("USB write error: %s", usb_strerror()); - return(ERROR_FAIL); + return ERROR_FAIL; } j = usb_bulk_read( pHDev, USB_EP1IN_ADDR, (char *)reply_buffer, sizeof(reply_buffer), 200 - ); - if (j != -ETIMEDOUT) break; + ); + if (j != -ETIMEDOUT) + break; } if (j < (int)sizeof(reply_buffer)) { LOG_ERROR("USB read error: %s", usb_strerror()); - return(ERROR_FAIL); + return ERROR_FAIL; } - LOG_DEBUG(INTERFACE_NAME" firmware version: %d.%d.%d", reply_buffer[0], reply_buffer[1], reply_buffer[2]); + LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d", + reply_buffer[0], + reply_buffer[1], + reply_buffer[2]); - if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) { - LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work."); - } + if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) + LOG_WARNING( + "The rlink device is not of the version that the developers have played with. It may or may not work."); /* Probe port E for adapter presence */ ep1_generic_commandl( - pHDev, 16, - EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */ - ST7_PEDR >> 8, - ST7_PEDR, - 3, - 0x00, /* DR */ - ST7_PE_ADAPTER_SENSE_OUT, /* DDR */ - ST7_PE_ADAPTER_SENSE_OUT, /* OR */ - EP1_CMD_MEMORY_READ, /* Read back */ - ST7_PEDR >> 8, - ST7_PEDR, - 1, - EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */ - ST7_PEDR >> 8, - ST7_PEDR, - 1, - ST7_PE_ADAPTER_SENSE_OUT - ); + pHDev, 16, + EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */ + ST7_PEDR >> 8, + ST7_PEDR, + 3, + 0x00, /* DR */ + ST7_PE_ADAPTER_SENSE_OUT, /* DDR */ + ST7_PE_ADAPTER_SENSE_OUT, /* OR */ + EP1_CMD_MEMORY_READ, /* Read back */ + ST7_PEDR >> 8, + ST7_PEDR, + 1, + EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */ + ST7_PEDR >> 8, + ST7_PEDR, + 1, + ST7_PE_ADAPTER_SENSE_OUT + ); usb_bulk_read( - pHDev, USB_EP1IN_ADDR, - (char *)reply_buffer, 1, - USB_TIMEOUT_MS - ); + pHDev, USB_EP1IN_ADDR, + (char *)reply_buffer, 1, + USB_TIMEOUT_MS + ); - if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) { + if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) LOG_WARNING("target detection problem"); - } ep1_generic_commandl( - pHDev, 11, - EP1_CMD_MEMORY_READ, /* Read back */ - ST7_PEDR >> 8, - ST7_PEDR, - 1, - EP1_CMD_MEMORY_WRITE, /* float port E */ - ST7_PEDR >> 8, - ST7_PEDR, - 3, - 0x00, /* DR */ - 0x00, /* DDR */ - 0x00 /* OR */ - ); + pHDev, 11, + EP1_CMD_MEMORY_READ, /* Read back */ + ST7_PEDR >> 8, + ST7_PEDR, + 1, + EP1_CMD_MEMORY_WRITE, /* float port E */ + ST7_PEDR >> 8, + ST7_PEDR, + 3, + 0x00, /* DR */ + 0x00, /* DDR */ + 0x00 /* OR */ + ); usb_bulk_read( - pHDev, USB_EP1IN_ADDR, - (char *)reply_buffer, 1, - USB_TIMEOUT_MS - ); + pHDev, USB_EP1IN_ADDR, + (char *)reply_buffer, 1, + USB_TIMEOUT_MS + ); - if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) { + if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) LOG_WARNING("target not plugged in"); - } /* float ports A and B */ ep1_generic_commandl( - pHDev, 11, - EP1_CMD_MEMORY_WRITE, - ST7_PADDR >> 8, - ST7_PADDR, - 2, - 0x00, - 0x00, - EP1_CMD_MEMORY_WRITE, - ST7_PBDDR >> 8, - ST7_PBDDR, - 1, - 0x00 - ); + pHDev, 11, + EP1_CMD_MEMORY_WRITE, + ST7_PADDR >> 8, + ST7_PADDR, + 2, + 0x00, + 0x00, + EP1_CMD_MEMORY_WRITE, + ST7_PBDDR >> 8, + ST7_PBDDR, + 1, + 0x00 + ); /* make sure DTC is stopped, set VPP control, set up ports A and B */ ep1_generic_commandl( - pHDev, 14, - EP1_CMD_DTC_STOP, - EP1_CMD_SET_PORTD_VPP, - ~(ST7_PD_VPP_SHDN), - EP1_CMD_MEMORY_WRITE, - ST7_PADR >> 8, - ST7_PADR, - 2, - ((~(0)) & (ST7_PA_NTRST)), - (ST7_PA_NTRST), - /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0 here and later assert NSRST by setting DDR bit to 1. */ - EP1_CMD_MEMORY_WRITE, - ST7_PBDR >> 8, - ST7_PBDR, - 1, - 0x00 - ); + pHDev, 14, + EP1_CMD_DTC_STOP, + EP1_CMD_SET_PORTD_VPP, + ~(ST7_PD_VPP_SHDN), + EP1_CMD_MEMORY_WRITE, + ST7_PADR >> 8, + ST7_PADR, + 2, + ((~(0)) & (ST7_PA_NTRST)), + (ST7_PA_NTRST), + /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0 + *here and later assert NSRST by setting DDR bit to 1. */ + EP1_CMD_MEMORY_WRITE, + ST7_PBDR >> 8, + ST7_PBDR, + 1, + 0x00 + ); /* set LED updating mode and make sure they're unlit */ ep1_generic_commandl( - pHDev, 3, + pHDev, 3, #ifdef AUTOMATIC_BUSY_LED - EP1_CMD_LEDUE_BUSY, + EP1_CMD_LEDUE_BUSY, #else - EP1_CMD_LEDUE_NONE, + EP1_CMD_LEDUE_NONE, #endif - EP1_CMD_SET_PORTD_LEDS, - ~0 - ); + EP1_CMD_SET_PORTD_LEDS, + ~0 + ); tap_state_queue_init(); dtc_queue_init(); @@ -1746,36 +1655,31 @@ int rlink_init(void) return ERROR_OK; } - -static -int rlink_quit(void) +static int rlink_quit(void) { /* stop DTC and make sure LEDs are off */ ep1_generic_commandl( - pHDev, 6, - EP1_CMD_DTC_STOP, - EP1_CMD_LEDUE_NONE, - EP1_CMD_SET_PORTD_LEDS, - ~0, - EP1_CMD_SET_PORTD_VPP, - ~0 - ); - - usb_release_interface(pHDev,0); - usb_close(pHDev); + pHDev, 6, + EP1_CMD_DTC_STOP, + EP1_CMD_LEDUE_NONE, + EP1_CMD_SET_PORTD_LEDS, + ~0, + EP1_CMD_SET_PORTD_VPP, + ~0 + ); + usb_release_interface(pHDev, 0); + usb_close(pHDev); return ERROR_OK; } - -struct jtag_interface rlink_interface = -{ - .name = "rlink", - .init = rlink_init, - .quit = rlink_quit, - .speed = rlink_speed, - .speed_div = rlink_speed_div, - .khz = rlink_khz, - .execute_queue = rlink_execute_queue, +struct jtag_interface rlink_interface = { + .name = "rlink", + .init = rlink_init, + .quit = rlink_quit, + .speed = rlink_speed, + .speed_div = rlink_speed_div, + .khz = rlink_khz, + .execute_queue = rlink_execute_queue, }; diff --git a/src/jtag/drivers/rlink_dtc_cmd.h b/src/jtag/drivers/rlink_dtc_cmd.h index 989750982b..4fa8a66c3c 100644 --- a/src/jtag/drivers/rlink_dtc_cmd.h +++ b/src/jtag/drivers/rlink_dtc_cmd.h @@ -18,7 +18,8 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -/* A command position with the high nybble of 0x0 is reserved for an error condition. If executed, it stops the DTC and raises the ERROR flag */ +/* A command position with the high nybble of 0x0 is reserved for an error condition. + * If executed, it stops the DTC and raises the ERROR flag */ #define DTC_CMD_SHIFT_TMS_BYTES(bytes) ((0x1 << 4) | ((bytes) - 1)) /* Shift 1-16 bytes out TMS. TDI is 0. */ @@ -46,10 +47,15 @@ ) | (\ (tdo) ? (1 << 3) : 0 \ )) -/* Single bit shift. */ -/* tms and tdi are the levels shifted out on TMS and TDI, respectively. */ -/* tdo indicates whether a byte will be returned in the reply buffer with its least significant bit set to reflect TDO */ -/* Care should be taken when tdo is zero, as the underlying code actually does put that byte in the reply buffer. Setting tdo to zero just moves the pointer back. The result is that if this command is executed when the reply buffer is already full, a byte will be written erroneously to memory not belonging to the reply buffer. This could be worked around at the expense of DTC code space and speed. */ +/* Single bit shift. + * tms and tdi are the levels shifted out on TMS and TDI, respectively. + * tdo indicates whether a byte will be returned in the reply buffer with its + * least significant bit set to reflect TDO + * Care should be taken when tdo is zero, as the underlying code actually does put + * that byte in the reply buffer. Setting tdo to zero just moves the pointer back. + * The result is that if this command is executed when the reply buffer is already full, + * a byte will be written erroneously to memory not belonging to the reply buffer. + * This could be worked around at the expense of DTC code space and speed. */ #define DTC_CMD_SHIFT_TMS_BITS(bits) ((0x9 << 4) | ((bits) - 1)) /* Shift 1-8 bits out TMS. */ @@ -60,7 +66,8 @@ /* Bits to be shifted out are left justified in the following byte. */ /* Bits shifted in are right justified in the byte placed in the reply buffer. */ - #define DTC_CMD_STOP (0xf << 4) /* Stop processing the command buffer and wait for the next one. */ -/* A shared status byte is updated with bit 0 set when this has happened, and it is cleared when a new command buffer becomes ready. The host can poll that byte to see when it is safe to read a reply. */ +/* A shared status byte is updated with bit 0 set when this has happened, + * and it is cleared when a new command buffer becomes ready. + * The host can poll that byte to see when it is safe to read a reply. */ diff --git a/src/jtag/drivers/rlink_speed_table.c b/src/jtag/drivers/rlink_speed_table.c index fa40fe8c6a..b84357731a 100644 --- a/src/jtag/drivers/rlink_speed_table.c +++ b/src/jtag/drivers/rlink_speed_table.c @@ -95,7 +95,7 @@ const struct rlink_speed_table rlink_speed_table[] = {{ dtc_8, sizeof(dtc_8), (ST7_FOSC * 2) / (1000 * 8), 8 }, { dtc_2, sizeof(dtc_2), (ST7_FOSC * 2) / (1000 * 2), 2 -}}; +} }; const size_t rlink_speed_table_size = ARRAY_SIZE(rlink_speed_table); diff --git a/src/jtag/drivers/stlink_usb.h b/src/jtag/drivers/stlink_usb.h index f564a0c518..4386ff268a 100644 --- a/src/jtag/drivers/stlink_usb.h +++ b/src/jtag/drivers/stlink_usb.h @@ -1,7 +1,29 @@ +/*************************************************************************** + * Copyright (C) 2011 by Mathias Kuester * + * Mathias Kuester * + * * + * This code is based on https://github.com/texane/stlink * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + #ifndef _STLINK_USB_H_ #define _STLINK_USB_H_ /** */ void *stlink_open_usb(struct stlink_interface_param_s *param); -#endif +#endif /* _STLINK_USB_H_ */ diff --git a/src/jtag/drivers/ulink.c b/src/jtag/drivers/ulink.c index 5c58da355c..4de1a77035 100644 --- a/src/jtag/drivers/ulink.c +++ b/src/jtag/drivers/ulink.c @@ -73,35 +73,32 @@ #define SPLIT_SCAN_THRESHOLD 10 /** ULINK hardware type */ -enum ulink_type -{ - /** Original ULINK adapter, based on Cypress EZ-USB (AN2131): - * Full JTAG support, no SWD support. */ - ULINK_1, +enum ulink_type { + /** Original ULINK adapter, based on Cypress EZ-USB (AN2131): + * Full JTAG support, no SWD support. */ + ULINK_1, - /** Newer ULINK adapter, based on NXP LPC2148. Currently unsupported. */ - ULINK_2, + /** Newer ULINK adapter, based on NXP LPC2148. Currently unsupported. */ + ULINK_2, - /** Newer ULINK adapter, based on EZ-USB FX2 + FPGA. Currently unsupported. */ - ULINK_PRO, + /** Newer ULINK adapter, based on EZ-USB FX2 + FPGA. Currently unsupported. */ + ULINK_PRO, - /** Newer ULINK adapter, possibly based on ULINK 2. Currently unsupported. */ - ULINK_ME + /** Newer ULINK adapter, possibly based on ULINK 2. Currently unsupported. */ + ULINK_ME }; -enum ulink_payload_direction -{ - PAYLOAD_DIRECTION_OUT, - PAYLOAD_DIRECTION_IN +enum ulink_payload_direction { + PAYLOAD_DIRECTION_OUT, + PAYLOAD_DIRECTION_IN }; -enum ulink_delay_type -{ - DELAY_CLOCK_TCK, - DELAY_CLOCK_TMS, - DELAY_SCAN_IN, - DELAY_SCAN_OUT, - DELAY_SCAN_IO +enum ulink_delay_type { + DELAY_CLOCK_TCK, + DELAY_CLOCK_TMS, + DELAY_SCAN_IN, + DELAY_SCAN_OUT, + DELAY_SCAN_IO }; /** @@ -127,42 +124,41 @@ enum ulink_delay_type * The last command sets #needs_postprocessing to true. */ struct ulink_cmd { - uint8_t id; ///< ULINK command ID + uint8_t id; /* /< ULINK command ID */ - uint8_t *payload_out; ///< OUT direction payload data - uint8_t payload_out_size; ///< OUT direction payload size for this command + uint8_t *payload_out; /* /< OUT direction payload data */ + uint8_t payload_out_size; /* /< OUT direction payload size for this command */ - uint8_t *payload_in_start; ///< Pointer to first element of IN payload array - uint8_t *payload_in; ///< Pointer where IN payload shall be stored - uint8_t payload_in_size; ///< IN direction payload size for this command + uint8_t *payload_in_start; /* /< Pointer to first element of IN payload array */ + uint8_t *payload_in; /* /< Pointer where IN payload shall be stored */ + uint8_t payload_in_size;/* /< IN direction payload size for this command */ - /** Indicates if this command needs post-processing */ - bool needs_postprocessing; + /** Indicates if this command needs post-processing */ + bool needs_postprocessing; - /** Indicates if ulink_clear_queue() should free payload_in_start */ - bool free_payload_in_start; + /** Indicates if ulink_clear_queue() should free payload_in_start */ + bool free_payload_in_start; - /** Pointer to corresponding OpenOCD command for post-processing */ - struct jtag_command *cmd_origin; + /** Pointer to corresponding OpenOCD command for post-processing */ + struct jtag_command *cmd_origin; - struct ulink_cmd *next; ///< Pointer to next command (linked list) + struct ulink_cmd *next; /* /< Pointer to next command (linked list) */ }; /** Describes one driver instance */ -struct ulink -{ - struct usb_dev_handle *usb_handle; - enum ulink_type type; - - int delay_scan_in; ///< Delay value for SCAN_IN commands - int delay_scan_out; ///< Delay value for SCAN_OUT commands - int delay_scan_io; ///< Delay value for SCAN_IO commands - int delay_clock_tck; ///< Delay value for CLOCK_TMS commands - int delay_clock_tms; ///< Delay value for CLOCK_TCK commands - - int commands_in_queue; ///< Number of commands in queue - struct ulink_cmd *queue_start; ///< Pointer to first command in queue - struct ulink_cmd *queue_end; ///< Pointer to last command in queue +struct ulink { + struct usb_dev_handle *usb_handle; + enum ulink_type type; + + int delay_scan_in; /* /< Delay value for SCAN_IN commands */ + int delay_scan_out; /* /< Delay value for SCAN_OUT commands */ + int delay_scan_io; /* /< Delay value for SCAN_IO commands */ + int delay_clock_tck; /* /< Delay value for CLOCK_TMS commands */ + int delay_clock_tms; /* /< Delay value for CLOCK_TCK commands */ + + int commands_in_queue; /* /< Number of commands in queue */ + struct ulink_cmd *queue_start; /* /< Pointer to first command in queue */ + struct ulink_cmd *queue_end; /* /< Pointer to last command in queue */ }; /**************************** Function Prototypes *****************************/ @@ -174,44 +170,56 @@ int ulink_usb_close(struct ulink **device); /* ULINK MCU (Cypress EZ-USB) specific functions */ int ulink_cpu_reset(struct ulink *device, char reset_bit); int ulink_load_firmware_and_renumerate(struct ulink **device, char *filename, - uint32_t delay); + uint32_t delay); int ulink_load_firmware(struct ulink *device, char *filename); int ulink_write_firmware_section(struct ulink *device, - struct image *firmware_image, int section_index); + struct image *firmware_image, int section_index); /* Generic helper functions */ void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals); /* OpenULINK command generation helper functions */ int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size, - enum ulink_payload_direction direction); + enum ulink_payload_direction direction); /* OpenULINK command queue helper functions */ int ulink_get_queue_size(struct ulink *device, - enum ulink_payload_direction direction); + enum ulink_payload_direction direction); void ulink_clear_queue(struct ulink *device); int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd); int ulink_execute_queued_commands(struct ulink *device, int timeout); #ifdef _DEBUG_JTAG_IO_ -const char * ulink_cmd_id_string(uint8_t id); +const char *ulink_cmd_id_string(uint8_t id); void ulink_print_command(struct ulink_cmd *ulink_cmd); void ulink_print_queue(struct ulink *device); #endif -int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type, - int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo, - uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end, - uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess); +int ulink_append_scan_cmd(struct ulink *device, + enum scan_type scan_type, + int scan_size_bits, + uint8_t *tdi, + uint8_t *tdo_start, + uint8_t *tdo, + uint8_t tms_count_start, + uint8_t tms_sequence_start, + uint8_t tms_count_end, + uint8_t tms_sequence_end, + struct jtag_command *origin, + bool postprocess); int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count, - uint8_t sequence); + uint8_t sequence); int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count); int ulink_append_get_signals_cmd(struct ulink *device); int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low, - uint8_t high); + uint8_t high); int ulink_append_sleep_cmd(struct ulink *device, uint32_t us); -int ulink_append_configure_tck_cmd(struct ulink *device, int delay_scan_in, - int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms); +int ulink_append_configure_tck_cmd(struct ulink *device, + int delay_scan_in, + int delay_scan_out, + int delay_scan_io, + int delay_tck, + int delay_tms); int ulink_append_led_cmd(struct ulink *device, uint8_t led_state); int ulink_append_test_cmd(struct ulink *device); @@ -257,29 +265,27 @@ struct ulink *ulink_handle; */ int ulink_usb_open(struct ulink **device) { - int ret; - struct usb_dev_handle *usb_handle; + int ret; + struct usb_dev_handle *usb_handle; - /* Currently, only original ULINK is supported */ - uint16_t vids[] = { ULINK_VID, 0 }; - uint16_t pids[] = { ULINK_PID, 0 }; + /* Currently, only original ULINK is supported */ + uint16_t vids[] = { ULINK_VID, 0 }; + uint16_t pids[] = { ULINK_PID, 0 }; - ret = jtag_usb_open(vids, pids, &usb_handle); + ret = jtag_usb_open(vids, pids, &usb_handle); - if (ret != ERROR_OK) { - return ret; - } + if (ret != ERROR_OK) + return ret; - ret = usb_claim_interface(usb_handle, 0); + ret = usb_claim_interface(usb_handle, 0); - if (ret != 0) { - return ret; - } + if (ret != 0) + return ret; - (*device)->usb_handle = usb_handle; - (*device)->type = ULINK_1; + (*device)->usb_handle = usb_handle; + (*device)->type = ULINK_1; - return ERROR_OK; + return ERROR_OK; } /** @@ -291,17 +297,15 @@ int ulink_usb_open(struct ulink **device) */ int ulink_usb_close(struct ulink **device) { - if (usb_release_interface((*device)->usb_handle, 0) != 0) { - return ERROR_FAIL; - } + if (usb_release_interface((*device)->usb_handle, 0) != 0) + return ERROR_FAIL; - if (usb_close((*device)->usb_handle) != 0) { - return ERROR_FAIL; - } + if (usb_close((*device)->usb_handle) != 0) + return ERROR_FAIL; - (*device)->usb_handle = NULL; + (*device)->usb_handle = NULL; - return ERROR_OK; + return ERROR_OK; } /******************* ULINK CPU (EZ-USB) specific functions ********************/ @@ -317,18 +321,17 @@ int ulink_usb_close(struct ulink **device) */ int ulink_cpu_reset(struct ulink *device, char reset_bit) { - int ret; - - ret = usb_control_msg(device->usb_handle, - (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE), - REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, USB_TIMEOUT); - - /* usb_control_msg() returns the number of bytes transferred during the - * DATA stage of the control transfer - must be exactly 1 in this case! */ - if (ret != 1) { - return ERROR_FAIL; - } - return ERROR_OK; + int ret; + + ret = usb_control_msg(device->usb_handle, + (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE), + REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, USB_TIMEOUT); + + /* usb_control_msg() returns the number of bytes transferred during the + * DATA stage of the control transfer - must be exactly 1 in this case! */ + if (ret != 1) + return ERROR_FAIL; + return ERROR_OK; } /** @@ -344,32 +347,29 @@ int ulink_cpu_reset(struct ulink *device, char reset_bit) * @return on failure: ERROR_FAIL */ int ulink_load_firmware_and_renumerate(struct ulink **device, - char *filename, uint32_t delay) + char *filename, uint32_t delay) { - int ret; + int ret; - /* Basic process: After downloading the firmware, the ULINK will disconnect - * itself and re-connect after a short amount of time so we have to close - * the handle and re-enumerate USB devices */ + /* Basic process: After downloading the firmware, the ULINK will disconnect + * itself and re-connect after a short amount of time so we have to close + * the handle and re-enumerate USB devices */ - ret = ulink_load_firmware(*device, filename); - if (ret != ERROR_OK) { - return ret; - } + ret = ulink_load_firmware(*device, filename); + if (ret != ERROR_OK) + return ret; - ret = ulink_usb_close(device); - if (ret != ERROR_OK) { - return ret; - } + ret = ulink_usb_close(device); + if (ret != ERROR_OK) + return ret; - usleep(delay); + usleep(delay); - ret = ulink_usb_open(device); - if (ret != ERROR_OK) { - return ret; - } + ret = ulink_usb_open(device); + if (ret != ERROR_OK) + return ret; - return ERROR_OK; + return ERROR_OK; } /** @@ -384,41 +384,40 @@ int ulink_load_firmware_and_renumerate(struct ulink **device, */ int ulink_load_firmware(struct ulink *device, char *filename) { - struct image ulink_firmware_image; - int ret, i; - - ret = ulink_cpu_reset(device, CPU_RESET); - if (ret != ERROR_OK) { - LOG_ERROR("Could not halt ULINK CPU"); - return ret; - } - - ulink_firmware_image.base_address = 0; - ulink_firmware_image.base_address_set = 0; - - ret = image_open(&ulink_firmware_image, filename, "ihex"); - if (ret != ERROR_OK) { - LOG_ERROR("Could not load firmware image"); - return ret; - } - - /* Download all sections in the image to ULINK */ - for (i = 0; i < ulink_firmware_image.num_sections; i++) { - ret = ulink_write_firmware_section(device, &ulink_firmware_image, i); - if (ret != ERROR_OK) { - return ret; - } - } - - image_close(&ulink_firmware_image); - - ret = ulink_cpu_reset(device, CPU_START); - if (ret != ERROR_OK) { - LOG_ERROR("Could not restart ULINK CPU"); - return ret; - } - - return ERROR_OK; + struct image ulink_firmware_image; + int ret, i; + + ret = ulink_cpu_reset(device, CPU_RESET); + if (ret != ERROR_OK) { + LOG_ERROR("Could not halt ULINK CPU"); + return ret; + } + + ulink_firmware_image.base_address = 0; + ulink_firmware_image.base_address_set = 0; + + ret = image_open(&ulink_firmware_image, filename, "ihex"); + if (ret != ERROR_OK) { + LOG_ERROR("Could not load firmware image"); + return ret; + } + + /* Download all sections in the image to ULINK */ + for (i = 0; i < ulink_firmware_image.num_sections; i++) { + ret = ulink_write_firmware_section(device, &ulink_firmware_image, i); + if (ret != ERROR_OK) + return ret; + } + + image_close(&ulink_firmware_image); + + ret = ulink_cpu_reset(device, CPU_START); + if (ret != ERROR_OK) { + LOG_ERROR("Could not restart ULINK CPU"); + return ret; + } + + return ERROR_OK; } /** @@ -433,61 +432,58 @@ int ulink_load_firmware(struct ulink *device, char *filename) * @return on failure: ERROR_FAIL */ int ulink_write_firmware_section(struct ulink *device, - struct image *firmware_image, int section_index) + struct image *firmware_image, int section_index) { - uint16_t addr, size, bytes_remaining, chunk_size; - uint8_t data[SECTION_BUFFERSIZE]; - uint8_t *data_ptr = data; - size_t size_read; - int ret; - - size = (uint16_t)firmware_image->sections[section_index].size; - addr = (uint16_t)firmware_image->sections[section_index].base_address; - - LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr, - size); - - if (data == NULL) { - return ERROR_FAIL; - } - - /* Copy section contents to local buffer */ - ret = image_read_section(firmware_image, section_index, 0, size, data, - &size_read); - - if ((ret != ERROR_OK) || (size_read != size)) { - /* Propagating the return code would return '0' (misleadingly indicating - * successful execution of the function) if only the size check fails. */ - return ERROR_FAIL; - } - - bytes_remaining = size; - - /* Send section data in chunks of up to 64 bytes to ULINK */ - while (bytes_remaining > 0) { - if (bytes_remaining > 64) { - chunk_size = 64; - } - else { - chunk_size = bytes_remaining; - } - - ret = usb_control_msg(device->usb_handle, - (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE), - REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (char *)data_ptr, - chunk_size, USB_TIMEOUT); - - if (ret != (int)chunk_size) { - /* Abort if libusb sent less data than requested */ - return ERROR_FAIL; - } - - bytes_remaining -= chunk_size; - addr += chunk_size; - data_ptr += chunk_size; - } - - return ERROR_OK; + uint16_t addr, size, bytes_remaining, chunk_size; + uint8_t data[SECTION_BUFFERSIZE]; + uint8_t *data_ptr = data; + size_t size_read; + int ret; + + size = (uint16_t)firmware_image->sections[section_index].size; + addr = (uint16_t)firmware_image->sections[section_index].base_address; + + LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr, + size); + + if (data == NULL) + return ERROR_FAIL; + + /* Copy section contents to local buffer */ + ret = image_read_section(firmware_image, section_index, 0, size, data, + &size_read); + + if ((ret != ERROR_OK) || (size_read != size)) { + /* Propagating the return code would return '0' (misleadingly indicating + * successful execution of the function) if only the size check fails. */ + return ERROR_FAIL; + } + + bytes_remaining = size; + + /* Send section data in chunks of up to 64 bytes to ULINK */ + while (bytes_remaining > 0) { + if (bytes_remaining > 64) + chunk_size = 64; + else + chunk_size = bytes_remaining; + + ret = usb_control_msg(device->usb_handle, + (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE), + REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (char *)data_ptr, + chunk_size, USB_TIMEOUT); + + if (ret != (int)chunk_size) { + /* Abort if libusb sent less data than requested */ + return ERROR_FAIL; + } + + bytes_remaining -= chunk_size; + addr += chunk_size; + data_ptr += chunk_size; + } + + return ERROR_OK; } /************************** Generic helper functions **************************/ @@ -500,14 +496,14 @@ int ulink_write_firmware_section(struct ulink *device, */ void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals) { - LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i," - " SRST: %i", - (output_signals & SIGNAL_TDI ? 1 : 0), - (input_signals & SIGNAL_TDO ? 1 : 0), - (output_signals & SIGNAL_TMS ? 1 : 0), - (output_signals & SIGNAL_TCK ? 1 : 0), - (output_signals & SIGNAL_TRST ? 0 : 1), // TRST and RESET are inverted - (output_signals & SIGNAL_RESET ? 0 : 1)); // by hardware + LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i," + " SRST: %i", + (output_signals & SIGNAL_TDI ? 1 : 0), + (input_signals & SIGNAL_TDO ? 1 : 0), + (output_signals & SIGNAL_TMS ? 1 : 0), + (output_signals & SIGNAL_TCK ? 1 : 0), + (output_signals & SIGNAL_TRST ? 0 : 1),/* TRST and RESET are inverted */ + (output_signals & SIGNAL_RESET ? 0 : 1)); /* by hardware */ } /**************** OpenULINK command generation helper functions ***************/ @@ -522,47 +518,45 @@ void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals) * @return on failure: ERROR_FAIL */ int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size, - enum ulink_payload_direction direction) + enum ulink_payload_direction direction) { - uint8_t *payload; - - payload = calloc(size, sizeof(uint8_t)); - - if (payload == NULL) { - LOG_ERROR("Could not allocate OpenULINK command payload: out of memory"); - return ERROR_FAIL; - } - - switch (direction) { - case PAYLOAD_DIRECTION_OUT: - if (ulink_cmd->payload_out != NULL) { - LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command"); - return ERROR_FAIL; - } - else { - ulink_cmd->payload_out = payload; - ulink_cmd->payload_out_size = size; - } - break; - case PAYLOAD_DIRECTION_IN: - if (ulink_cmd->payload_in_start != NULL) { - LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command"); - return ERROR_FAIL; - } - else { - ulink_cmd->payload_in_start = payload; - ulink_cmd->payload_in = payload; - ulink_cmd->payload_in_size = size; - - /* By default, free payload_in_start in ulink_clear_queue(). Commands - * that do not want this behavior (e. g. split scans) must turn it off - * separately! */ - ulink_cmd->free_payload_in_start = true; - } - break; - } - - return ERROR_OK; + uint8_t *payload; + + payload = calloc(size, sizeof(uint8_t)); + + if (payload == NULL) { + LOG_ERROR("Could not allocate OpenULINK command payload: out of memory"); + return ERROR_FAIL; + } + + switch (direction) { + case PAYLOAD_DIRECTION_OUT: + if (ulink_cmd->payload_out != NULL) { + LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command"); + return ERROR_FAIL; + } else { + ulink_cmd->payload_out = payload; + ulink_cmd->payload_out_size = size; + } + break; + case PAYLOAD_DIRECTION_IN: + if (ulink_cmd->payload_in_start != NULL) { + LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command"); + return ERROR_FAIL; + } else { + ulink_cmd->payload_in_start = payload; + ulink_cmd->payload_in = payload; + ulink_cmd->payload_in_size = size; + + /* By default, free payload_in_start in ulink_clear_queue(). Commands + * that do not want this behavior (e. g. split scans) must turn it off + * separately! */ + ulink_cmd->free_payload_in_start = true; + } + break; + } + + return ERROR_OK; } /****************** OpenULINK command queue helper functions ******************/ @@ -576,25 +570,25 @@ int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size, * direction. */ int ulink_get_queue_size(struct ulink *device, - enum ulink_payload_direction direction) + enum ulink_payload_direction direction) { - struct ulink_cmd *current = device->queue_start; - int sum = 0; - - while (current != NULL) { - switch (direction) { - case PAYLOAD_DIRECTION_OUT: - sum += current->payload_out_size + 1; // + 1 byte for Command ID - break; - case PAYLOAD_DIRECTION_IN: - sum += current->payload_in_size; - break; - } - - current = current->next; - } - - return sum; + struct ulink_cmd *current = device->queue_start; + int sum = 0; + + while (current != NULL) { + switch (direction) { + case PAYLOAD_DIRECTION_OUT: + sum += current->payload_out_size + 1; /* + 1 byte for Command ID */ + break; + case PAYLOAD_DIRECTION_IN: + sum += current->payload_in_size; + break; + } + + current = current->next; + } + + return sum; } /** @@ -606,35 +600,35 @@ int ulink_get_queue_size(struct ulink *device, */ void ulink_clear_queue(struct ulink *device) { - struct ulink_cmd *current = device->queue_start; - struct ulink_cmd *next = NULL; - - while (current != NULL) { - /* Save pointer to next element */ - next = current->next; - - /* Free payloads: OUT payload can be freed immediately */ - free(current->payload_out); - current->payload_out = NULL; - - /* IN payload MUST be freed ONLY if no other commands use the - * payload_in_start buffer */ - if (current->free_payload_in_start == true) { - free(current->payload_in_start); - current->payload_in_start = NULL; - current->payload_in = NULL; - } - - /* Free queue element */ - free(current); - - /* Proceed with next element */ - current = next; - } - - device->commands_in_queue = 0; - device->queue_start = NULL; - device->queue_end = NULL; + struct ulink_cmd *current = device->queue_start; + struct ulink_cmd *next = NULL; + + while (current != NULL) { + /* Save pointer to next element */ + next = current->next; + + /* Free payloads: OUT payload can be freed immediately */ + free(current->payload_out); + current->payload_out = NULL; + + /* IN payload MUST be freed ONLY if no other commands use the + * payload_in_start buffer */ + if (current->free_payload_in_start == true) { + free(current->payload_in_start); + current->payload_in_start = NULL; + current->payload_in = NULL; + } + + /* Free queue element */ + free(current); + + /* Proceed with next element */ + current = next; + } + + device->commands_in_queue = 0; + device->queue_start = NULL; + device->queue_end = NULL; } /** @@ -648,48 +642,45 @@ void ulink_clear_queue(struct ulink *device) */ int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd) { - int newsize_out, newsize_in; - int ret; - - newsize_out = ulink_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1 - + ulink_cmd->payload_out_size; - - newsize_in = ulink_get_queue_size(device, PAYLOAD_DIRECTION_IN) - + ulink_cmd->payload_in_size; - - /* Check if the current command can be appended to the queue */ - if ((newsize_out > 64) || (newsize_in > 64)) { - /* New command does not fit. Execute all commands in queue before starting - * new queue with the current command as first entry. */ - ret = ulink_execute_queued_commands(device, USB_TIMEOUT); - if (ret != ERROR_OK) { - return ret; - } - - ret = ulink_post_process_queue(device); - if (ret != ERROR_OK) { - return ret; - } - - ulink_clear_queue(device); - } - - if (device->queue_start == NULL) { - /* Queue was empty */ - device->commands_in_queue = 1; - - device->queue_start = ulink_cmd; - device->queue_end = ulink_cmd; - } - else { - /* There are already commands in the queue */ - device->commands_in_queue++; - - device->queue_end->next = ulink_cmd; - device->queue_end = ulink_cmd; - } - - return ERROR_OK; + int newsize_out, newsize_in; + int ret; + + newsize_out = ulink_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1 + + ulink_cmd->payload_out_size; + + newsize_in = ulink_get_queue_size(device, PAYLOAD_DIRECTION_IN) + + ulink_cmd->payload_in_size; + + /* Check if the current command can be appended to the queue */ + if ((newsize_out > 64) || (newsize_in > 64)) { + /* New command does not fit. Execute all commands in queue before starting + * new queue with the current command as first entry. */ + ret = ulink_execute_queued_commands(device, USB_TIMEOUT); + if (ret != ERROR_OK) + return ret; + + ret = ulink_post_process_queue(device); + if (ret != ERROR_OK) + return ret; + + ulink_clear_queue(device); + } + + if (device->queue_start == NULL) { + /* Queue was empty */ + device->commands_in_queue = 1; + + device->queue_start = ulink_cmd; + device->queue_end = ulink_cmd; + } else { + /* There are already commands in the queue */ + device->commands_in_queue++; + + device->queue_end->next = ulink_cmd; + device->queue_end = ulink_cmd; + } + + return ERROR_OK; } /** @@ -701,64 +692,59 @@ int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd) */ int ulink_execute_queued_commands(struct ulink *device, int timeout) { - struct ulink_cmd *current; - int ret, i, index_out, index_in, count_out, count_in; - uint8_t buffer[64]; + struct ulink_cmd *current; + int ret, i, index_out, index_in, count_out, count_in; + uint8_t buffer[64]; #ifdef _DEBUG_JTAG_IO_ - ulink_print_queue(device); + ulink_print_queue(device); #endif - index_out = 0; - count_out = 0; - count_in = 0; - - for (current = device->queue_start; current; current = current->next) { - /* Add command to packet */ - buffer[index_out] = current->id; - index_out++; - count_out++; - - for (i = 0; i < current->payload_out_size; i++) { - buffer[index_out + i] = current->payload_out[i]; - } - index_out += current->payload_out_size; - count_in += current->payload_in_size; - count_out += current->payload_out_size; - } - - /* Send packet to ULINK */ - ret = usb_bulk_write(device->usb_handle, (2 | USB_ENDPOINT_OUT), - (char *)buffer, count_out, timeout); - if (ret < 0) { - return ERROR_FAIL; - } - if (ret != count_out) { - return ERROR_FAIL; - } - - /* Wait for response if commands contain IN payload data */ - if (count_in > 0) { - ret = usb_bulk_read(device->usb_handle, (2 | USB_ENDPOINT_IN), - (char *)buffer, 64, timeout); - if (ret < 0) { - return ERROR_FAIL; - } - if (ret != count_in) { - return ERROR_FAIL; - } - - /* Write back IN payload data */ - index_in = 0; - for (current = device->queue_start; current; current = current->next) { - for (i = 0; i < current->payload_in_size; i++) { - current->payload_in[i] = buffer[index_in]; - index_in++; - } - } - } - - return ERROR_OK; + index_out = 0; + count_out = 0; + count_in = 0; + + for (current = device->queue_start; current; current = current->next) { + /* Add command to packet */ + buffer[index_out] = current->id; + index_out++; + count_out++; + + for (i = 0; i < current->payload_out_size; i++) + buffer[index_out + i] = current->payload_out[i]; + index_out += current->payload_out_size; + count_in += current->payload_in_size; + count_out += current->payload_out_size; + } + + /* Send packet to ULINK */ + ret = usb_bulk_write(device->usb_handle, (2 | USB_ENDPOINT_OUT), + (char *)buffer, count_out, timeout); + if (ret < 0) + return ERROR_FAIL; + if (ret != count_out) + return ERROR_FAIL; + + /* Wait for response if commands contain IN payload data */ + if (count_in > 0) { + ret = usb_bulk_read(device->usb_handle, (2 | USB_ENDPOINT_IN), + (char *)buffer, 64, timeout); + if (ret < 0) + return ERROR_FAIL; + if (ret != count_in) + return ERROR_FAIL; + + /* Write back IN payload data */ + index_in = 0; + for (current = device->queue_start; current; current = current->next) { + for (i = 0; i < current->payload_in_size; i++) { + current->payload_in[i] = buffer[index_in]; + index_in++; + } + } + } + + return ERROR_OK; } #ifdef _DEBUG_JTAG_IO_ @@ -769,64 +755,64 @@ int ulink_execute_queued_commands(struct ulink *device, int timeout) * @param id the OpenULINK command ID. * @return the corresponding human-readable string. */ -const char * ulink_cmd_id_string(uint8_t id) +const char *ulink_cmd_id_string(uint8_t id) { - switch (id) { - case CMD_SCAN_IN: - return "CMD_SCAN_IN"; - break; - case CMD_SLOW_SCAN_IN: - return "CMD_SLOW_SCAN_IN"; - break; - case CMD_SCAN_OUT: - return "CMD_SCAN_OUT"; - break; - case CMD_SLOW_SCAN_OUT: - return "CMD_SLOW_SCAN_OUT"; - break; - case CMD_SCAN_IO: - return "CMD_SCAN_IO"; - break; - case CMD_SLOW_SCAN_IO: - return "CMD_SLOW_SCAN_IO"; - break; - case CMD_CLOCK_TMS: - return "CMD_CLOCK_TMS"; - break; - case CMD_SLOW_CLOCK_TMS: - return "CMD_SLOW_CLOCK_TMS"; - break; - case CMD_CLOCK_TCK: - return "CMD_CLOCK_TCK"; - break; - case CMD_SLOW_CLOCK_TCK: - return "CMD_SLOW_CLOCK_TCK"; - break; - case CMD_SLEEP_US: - return "CMD_SLEEP_US"; - break; - case CMD_SLEEP_MS: - return "CMD_SLEEP_MS"; - break; - case CMD_GET_SIGNALS: - return "CMD_GET_SIGNALS"; - break; - case CMD_SET_SIGNALS: - return "CMD_SET_SIGNALS"; - break; - case CMD_CONFIGURE_TCK_FREQ: - return "CMD_CONFIGURE_TCK_FREQ"; - break; - case CMD_SET_LEDS: - return "CMD_SET_LEDS"; - break; - case CMD_TEST: - return "CMD_TEST"; - break; - default: - return "CMD_UNKNOWN"; - break; - } + switch (id) { + case CMD_SCAN_IN: + return "CMD_SCAN_IN"; + break; + case CMD_SLOW_SCAN_IN: + return "CMD_SLOW_SCAN_IN"; + break; + case CMD_SCAN_OUT: + return "CMD_SCAN_OUT"; + break; + case CMD_SLOW_SCAN_OUT: + return "CMD_SLOW_SCAN_OUT"; + break; + case CMD_SCAN_IO: + return "CMD_SCAN_IO"; + break; + case CMD_SLOW_SCAN_IO: + return "CMD_SLOW_SCAN_IO"; + break; + case CMD_CLOCK_TMS: + return "CMD_CLOCK_TMS"; + break; + case CMD_SLOW_CLOCK_TMS: + return "CMD_SLOW_CLOCK_TMS"; + break; + case CMD_CLOCK_TCK: + return "CMD_CLOCK_TCK"; + break; + case CMD_SLOW_CLOCK_TCK: + return "CMD_SLOW_CLOCK_TCK"; + break; + case CMD_SLEEP_US: + return "CMD_SLEEP_US"; + break; + case CMD_SLEEP_MS: + return "CMD_SLEEP_MS"; + break; + case CMD_GET_SIGNALS: + return "CMD_GET_SIGNALS"; + break; + case CMD_SET_SIGNALS: + return "CMD_SET_SIGNALS"; + break; + case CMD_CONFIGURE_TCK_FREQ: + return "CMD_CONFIGURE_TCK_FREQ"; + break; + case CMD_SET_LEDS: + return "CMD_SET_LEDS"; + break; + case CMD_TEST: + return "CMD_TEST"; + break; + default: + return "CMD_UNKNOWN"; + break; + } } /** @@ -836,16 +822,15 @@ const char * ulink_cmd_id_string(uint8_t id) */ void ulink_print_command(struct ulink_cmd *ulink_cmd) { - int i; + int i; - printf(" %-22s | OUT size = %i, bytes = 0x", - ulink_cmd_id_string(ulink_cmd->id), ulink_cmd->payload_out_size); + printf(" %-22s | OUT size = %i, bytes = 0x", + ulink_cmd_id_string(ulink_cmd->id), ulink_cmd->payload_out_size); - for (i = 0; i < ulink_cmd->payload_out_size; i++) { - printf("%02X ", ulink_cmd->payload_out[i]); - } - printf("\n | IN size = %i\n", - ulink_cmd->payload_in_size); + for (i = 0; i < ulink_cmd->payload_out_size; i++) + printf("%02X ", ulink_cmd->payload_out[i]); + printf("\n | IN size = %i\n", + ulink_cmd->payload_in_size); } /** @@ -855,16 +840,15 @@ void ulink_print_command(struct ulink_cmd *ulink_cmd) */ void ulink_print_queue(struct ulink *device) { - struct ulink_cmd *current; + struct ulink_cmd *current; - printf("OpenULINK command queue:\n"); + printf("OpenULINK command queue:\n"); - for (current = device->queue_start; current; current = current->next) { - ulink_print_command(current); - } + for (current = device->queue_start; current; current = current->next) + ulink_print_command(current); } -#endif /* _DEBUG_JTAG_IO_ */ +#endif /* _DEBUG_JTAG_IO_ */ /** * Perform JTAG scan @@ -897,101 +881,91 @@ void ulink_print_queue(struct ulink *device) * @return on failure: ERROR_FAIL */ int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type, - int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo, - uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end, - uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess) + int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo, + uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end, + uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret, i, scan_size_bytes; - uint8_t bits_last_byte; - - if (cmd == NULL) { - return ERROR_FAIL; - } - - /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID, - * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */ - if (scan_size_bits > (58 * 8)) { - LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too" - " large payload"); - return ERROR_FAIL; - } - - scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8); - - bits_last_byte = scan_size_bits % 8; - if (bits_last_byte == 0) { - bits_last_byte = 8; - } - - /* Allocate out_payload depending on scan type */ - switch (scan_type) { - case SCAN_IN: - if (device->delay_scan_in < 0) { - cmd->id = CMD_SCAN_IN; - } - else { - cmd->id = CMD_SLOW_SCAN_IN; - } - ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT); - break; - case SCAN_OUT: - if (device->delay_scan_out < 0) { - cmd->id = CMD_SCAN_OUT; - } - else { - cmd->id = CMD_SLOW_SCAN_OUT; - } - ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT); - break; - case SCAN_IO: - if (device->delay_scan_io < 0) { - cmd->id = CMD_SCAN_IO; - } - else { - cmd->id = CMD_SLOW_SCAN_IO; - } - ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT); - break; - default: - LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type"); - ret = ERROR_FAIL; - break; - } - - if (ret != ERROR_OK) { - return ret; - } - - /* Build payload_out that is common to all scan types */ - cmd->payload_out[0] = scan_size_bytes & 0xFF; - cmd->payload_out[1] = bits_last_byte & 0xFF; - cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F); - cmd->payload_out[3] = tms_sequence_start; - cmd->payload_out[4] = tms_sequence_end; - - /* Setup payload_out for types with OUT transfer */ - if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) { - for (i = 0; i < scan_size_bytes; i++) { - cmd->payload_out[i + 5] = tdi[i]; - } - } - - /* Setup payload_in pointers for types with IN transfer */ - if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) { - cmd->payload_in_start = tdo_start; - cmd->payload_in = tdo; - cmd->payload_in_size = scan_size_bytes; - } - - cmd->needs_postprocessing = postprocess; - cmd->cmd_origin = origin; - - /* For scan commands, we free payload_in_start only when the command is - * the last in a series of split commands or a stand-alone command */ - cmd->free_payload_in_start = postprocess; - - return ulink_append_queue(device, cmd); + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret, i, scan_size_bytes; + uint8_t bits_last_byte; + + if (cmd == NULL) + return ERROR_FAIL; + + /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID, + * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */ + if (scan_size_bits > (58 * 8)) { + LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too" + " large payload"); + return ERROR_FAIL; + } + + scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8); + + bits_last_byte = scan_size_bits % 8; + if (bits_last_byte == 0) + bits_last_byte = 8; + + /* Allocate out_payload depending on scan type */ + switch (scan_type) { + case SCAN_IN: + if (device->delay_scan_in < 0) + cmd->id = CMD_SCAN_IN; + else + cmd->id = CMD_SLOW_SCAN_IN; + ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT); + break; + case SCAN_OUT: + if (device->delay_scan_out < 0) + cmd->id = CMD_SCAN_OUT; + else + cmd->id = CMD_SLOW_SCAN_OUT; + ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT); + break; + case SCAN_IO: + if (device->delay_scan_io < 0) + cmd->id = CMD_SCAN_IO; + else + cmd->id = CMD_SLOW_SCAN_IO; + ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT); + break; + default: + LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type"); + ret = ERROR_FAIL; + break; + } + + if (ret != ERROR_OK) + return ret; + + /* Build payload_out that is common to all scan types */ + cmd->payload_out[0] = scan_size_bytes & 0xFF; + cmd->payload_out[1] = bits_last_byte & 0xFF; + cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F); + cmd->payload_out[3] = tms_sequence_start; + cmd->payload_out[4] = tms_sequence_end; + + /* Setup payload_out for types with OUT transfer */ + if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) { + for (i = 0; i < scan_size_bytes; i++) + cmd->payload_out[i + 5] = tdi[i]; + } + + /* Setup payload_in pointers for types with IN transfer */ + if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) { + cmd->payload_in_start = tdo_start; + cmd->payload_in = tdo; + cmd->payload_in_size = scan_size_bytes; + } + + cmd->needs_postprocessing = postprocess; + cmd->cmd_origin = origin; + + /* For scan commands, we free payload_in_start only when the command is + * the last in a series of split commands or a stand-alone command */ + cmd->free_payload_in_start = postprocess; + + return ulink_append_queue(device, cmd); } /** @@ -1005,32 +979,28 @@ int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type, * @return on failure: ERROR_FAIL */ int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count, - uint8_t sequence) + uint8_t sequence) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; - - if (cmd == NULL) { - return ERROR_FAIL; - } - - if (device->delay_clock_tms < 0) { - cmd->id = CMD_CLOCK_TMS; - } - else { - cmd->id = CMD_SLOW_CLOCK_TMS; - } - - /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */ - ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } - - cmd->payload_out[0] = count; - cmd->payload_out[1] = sequence; - - return ulink_append_queue(device, cmd); + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; + + if (cmd == NULL) + return ERROR_FAIL; + + if (device->delay_clock_tms < 0) + cmd->id = CMD_CLOCK_TMS; + else + cmd->id = CMD_SLOW_CLOCK_TMS; + + /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */ + ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); + if (ret != ERROR_OK) + return ret; + + cmd->payload_out[0] = count; + cmd->payload_out[1] = sequence; + + return ulink_append_queue(device, cmd); } /** @@ -1045,30 +1015,26 @@ int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count, */ int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; - - if (cmd == NULL) { - return ERROR_FAIL; - } - - if (device->delay_clock_tck < 0) { - cmd->id = CMD_CLOCK_TCK; - } - else { - cmd->id = CMD_SLOW_CLOCK_TCK; - } - - /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */ - ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } - - cmd->payload_out[0] = count & 0xff; - cmd->payload_out[1] = (count >> 8) & 0xff; - - return ulink_append_queue(device, cmd); + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; + + if (cmd == NULL) + return ERROR_FAIL; + + if (device->delay_clock_tck < 0) + cmd->id = CMD_CLOCK_TCK; + else + cmd->id = CMD_SLOW_CLOCK_TCK; + + /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */ + ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); + if (ret != ERROR_OK) + return ret; + + cmd->payload_out[0] = count & 0xff; + cmd->payload_out[1] = (count >> 8) & 0xff; + + return ulink_append_queue(device, cmd); } /** @@ -1080,24 +1046,22 @@ int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count) */ int ulink_append_get_signals_cmd(struct ulink *device) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; - if (cmd == NULL) { - return ERROR_FAIL; - } + if (cmd == NULL) + return ERROR_FAIL; - cmd->id = CMD_GET_SIGNALS; - cmd->needs_postprocessing = true; + cmd->id = CMD_GET_SIGNALS; + cmd->needs_postprocessing = true; - /* CMD_GET_SIGNALS has two IN payload bytes */ - ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN); + /* CMD_GET_SIGNALS has two IN payload bytes */ + ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN); - if (ret != ERROR_OK) { - return ret; - } + if (ret != ERROR_OK) + return ret; - return ulink_append_queue(device, cmd); + return ulink_append_queue(device, cmd); } /** @@ -1118,28 +1082,26 @@ int ulink_append_get_signals_cmd(struct ulink *device) * @return on failure: ERROR_FAIL */ int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low, - uint8_t high) + uint8_t high) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; - if (cmd == NULL) { - return ERROR_FAIL; - } + if (cmd == NULL) + return ERROR_FAIL; - cmd->id = CMD_SET_SIGNALS; + cmd->id = CMD_SET_SIGNALS; - /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */ - ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); + /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */ + ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } + if (ret != ERROR_OK) + return ret; - cmd->payload_out[0] = low; - cmd->payload_out[1] = high; + cmd->payload_out[0] = low; + cmd->payload_out[1] = high; - return ulink_append_queue(device, cmd); + return ulink_append_queue(device, cmd); } /** @@ -1152,26 +1114,24 @@ int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low, */ int ulink_append_sleep_cmd(struct ulink *device, uint32_t us) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; - if (cmd == NULL) { - return ERROR_FAIL; - } + if (cmd == NULL) + return ERROR_FAIL; - cmd->id = CMD_SLEEP_US; + cmd->id = CMD_SLEEP_US; - /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */ - ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); + /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */ + ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } + if (ret != ERROR_OK) + return ret; - cmd->payload_out[0] = us & 0x00ff; - cmd->payload_out[1] = (us >> 8) & 0x00ff; + cmd->payload_out[0] = us & 0x00ff; + cmd->payload_out[1] = (us >> 8) & 0x00ff; - return ulink_append_queue(device, cmd); + return ulink_append_queue(device, cmd); } /** @@ -1187,60 +1147,48 @@ int ulink_append_sleep_cmd(struct ulink *device, uint32_t us) * @return on failure: ERROR_FAIL */ int ulink_append_configure_tck_cmd(struct ulink *device, int delay_scan_in, - int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms) + int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; - - if (cmd == NULL) { - return ERROR_FAIL; - } - - cmd->id = CMD_CONFIGURE_TCK_FREQ; - - /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero - * IN payload bytes */ - ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } - - if (delay_scan_in < 0) { - cmd->payload_out[0] = 0; - } - else { - cmd->payload_out[0] = (uint8_t)delay_scan_in; - } - - if (delay_scan_out < 0) { - cmd->payload_out[1] = 0; - } - else { - cmd->payload_out[1] = (uint8_t)delay_scan_out; - } - - if (delay_scan_io < 0) { - cmd->payload_out[2] = 0; - } - else { - cmd->payload_out[2] = (uint8_t)delay_scan_io; - } - - if (delay_tck < 0) { - cmd->payload_out[3] = 0; - } - else { - cmd->payload_out[3] = (uint8_t)delay_tck; - } - - if (delay_tms < 0) { - cmd->payload_out[4] = 0; - } - else { - cmd->payload_out[4] = (uint8_t)delay_tms; - } - - return ulink_append_queue(device, cmd); + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; + + if (cmd == NULL) + return ERROR_FAIL; + + cmd->id = CMD_CONFIGURE_TCK_FREQ; + + /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero + * IN payload bytes */ + ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT); + if (ret != ERROR_OK) + return ret; + + if (delay_scan_in < 0) + cmd->payload_out[0] = 0; + else + cmd->payload_out[0] = (uint8_t)delay_scan_in; + + if (delay_scan_out < 0) + cmd->payload_out[1] = 0; + else + cmd->payload_out[1] = (uint8_t)delay_scan_out; + + if (delay_scan_io < 0) + cmd->payload_out[2] = 0; + else + cmd->payload_out[2] = (uint8_t)delay_scan_io; + + if (delay_tck < 0) + cmd->payload_out[3] = 0; + else + cmd->payload_out[3] = (uint8_t)delay_tck; + + if (delay_tms < 0) + cmd->payload_out[4] = 0; + else + cmd->payload_out[4] = (uint8_t)delay_tms; + + return ulink_append_queue(device, cmd); } /** @@ -1260,24 +1208,22 @@ int ulink_append_configure_tck_cmd(struct ulink *device, int delay_scan_in, */ int ulink_append_led_cmd(struct ulink *device, uint8_t led_state) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; - if (cmd == NULL) { - return ERROR_FAIL; - } + if (cmd == NULL) + return ERROR_FAIL; - cmd->id = CMD_SET_LEDS; + cmd->id = CMD_SET_LEDS; - /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */ - ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } + /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */ + ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT); + if (ret != ERROR_OK) + return ret; - cmd->payload_out[0] = led_state; + cmd->payload_out[0] = led_state; - return ulink_append_queue(device, cmd); + return ulink_append_queue(device, cmd); } /** @@ -1290,24 +1236,22 @@ int ulink_append_led_cmd(struct ulink *device, uint8_t led_state) */ int ulink_append_test_cmd(struct ulink *device) { - struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); - int ret; + struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd)); + int ret; - if (cmd == NULL) { - return ERROR_FAIL; - } + if (cmd == NULL) + return ERROR_FAIL; - cmd->id = CMD_TEST; + cmd->id = CMD_TEST; - /* CMD_TEST has one OUT payload byte and zero IN payload bytes */ - ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT); - if (ret != ERROR_OK) { - return ret; - } + /* CMD_TEST has one OUT payload byte and zero IN payload bytes */ + ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT); + if (ret != ERROR_OK) + return ret; - cmd->payload_out[0] = 0xAA; + cmd->payload_out[0] = 0xAA; - return ulink_append_queue(device, cmd); + return ulink_append_queue(device, cmd); } /****************** OpenULINK TCK frequency helper functions ******************/ @@ -1346,52 +1290,50 @@ int ulink_append_test_cmd(struct ulink *device) */ int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay) { - float t, x, x_ceil; - - /* Calculate period of requested TCK frequency */ - t = 1.0 / (float)(f); - - switch (type) { - case DELAY_CLOCK_TCK: - x = (t - (float)(6E-6)) / (float)(4E-6); - break; - case DELAY_CLOCK_TMS: - x = (t - (float)(8.5E-6)) / (float)(4E-6); - break; - case DELAY_SCAN_IN: - x = (t - (float)(8.8308E-6)) / (float)(4E-6); - break; - case DELAY_SCAN_OUT: - x = (t - (float)(1.0527E-5)) / (float)(4E-6); - break; - case DELAY_SCAN_IO: - x = (t - (float)(1.3132E-5)) / (float)(4E-6); - break; - default: - return ERROR_FAIL; - break; - } - - /* Check if the delay value is negative. This happens when a frequency is - * requested that is too high for the delay loop implementation. In this - * case, set delay value to zero. */ - if (x < 0) { - x = 0; - } - - /* We need to convert the exact delay value to an integer. Therefore, we - * round the exact value UP to ensure that the resulting frequency is NOT - * higher than the requested frequency. */ - x_ceil = ceilf(x); - - /* Check if the value is within limits */ - if (x_ceil > 255) { - return ERROR_FAIL; - } - - *delay = (int)x_ceil; - - return ERROR_OK; + float t, x, x_ceil; + + /* Calculate period of requested TCK frequency */ + t = 1.0 / (float)(f); + + switch (type) { + case DELAY_CLOCK_TCK: + x = (t - (float)(6E-6)) / (float)(4E-6); + break; + case DELAY_CLOCK_TMS: + x = (t - (float)(8.5E-6)) / (float)(4E-6); + break; + case DELAY_SCAN_IN: + x = (t - (float)(8.8308E-6)) / (float)(4E-6); + break; + case DELAY_SCAN_OUT: + x = (t - (float)(1.0527E-5)) / (float)(4E-6); + break; + case DELAY_SCAN_IO: + x = (t - (float)(1.3132E-5)) / (float)(4E-6); + break; + default: + return ERROR_FAIL; + break; + } + + /* Check if the delay value is negative. This happens when a frequency is + * requested that is too high for the delay loop implementation. In this + * case, set delay value to zero. */ + if (x < 0) + x = 0; + + /* We need to convert the exact delay value to an integer. Therefore, we + * round the exact value UP to ensure that the resulting frequency is NOT + * higher than the requested frequency. */ + x_ceil = ceilf(x); + + /* Check if the value is within limits */ + if (x_ceil > 255) + return ERROR_FAIL; + + *delay = (int)x_ceil; + + return ERROR_OK; } /** @@ -1410,63 +1352,52 @@ int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay) */ int ulink_calculate_frequency(enum ulink_delay_type type, int delay, long *f) { - float t, f_float, f_rounded; - - if (delay > 255) { - return ERROR_FAIL; - } - - switch (type) { - case DELAY_CLOCK_TCK: - if (delay < 0) { - t = (float)(2.666E-6); - } - else { - t = (float)(4E-6) * (float)(delay) + (float)(6E-6); - } - break; - case DELAY_CLOCK_TMS: - if (delay < 0) { - t = (float)(5.666E-6); - } - else { - t = (float)(4E-6) * (float)(delay) + (float)(8.5E-6); - } - break; - case DELAY_SCAN_IN: - if (delay < 0) { - t = (float)(5.5E-6); - } - else { - t = (float)(4E-6) * (float)(delay) + (float)(8.8308E-6); - } - break; - case DELAY_SCAN_OUT: - if (delay < 0) { - t = (float)(7.0E-6); - } - else { - t = (float)(4E-6) * (float)(delay) + (float)(1.0527E-5); - } - break; - case DELAY_SCAN_IO: - if (delay < 0) { - t = (float)(9.926E-6); - } - else { - t = (float)(4E-6) * (float)(delay) + (float)(1.3132E-5); - } - break; - default: - return ERROR_FAIL; - break; - } - - f_float = 1.0 / t; - f_rounded = roundf(f_float); - *f = (long)f_rounded; - - return ERROR_OK; + float t, f_float, f_rounded; + + if (delay > 255) + return ERROR_FAIL; + + switch (type) { + case DELAY_CLOCK_TCK: + if (delay < 0) + t = (float)(2.666E-6); + else + t = (float)(4E-6) * (float)(delay) + (float)(6E-6); + break; + case DELAY_CLOCK_TMS: + if (delay < 0) + t = (float)(5.666E-6); + else + t = (float)(4E-6) * (float)(delay) + (float)(8.5E-6); + break; + case DELAY_SCAN_IN: + if (delay < 0) + t = (float)(5.5E-6); + else + t = (float)(4E-6) * (float)(delay) + (float)(8.8308E-6); + break; + case DELAY_SCAN_OUT: + if (delay < 0) + t = (float)(7.0E-6); + else + t = (float)(4E-6) * (float)(delay) + (float)(1.0527E-5); + break; + case DELAY_SCAN_IO: + if (delay < 0) + t = (float)(9.926E-6); + else + t = (float)(4E-6) * (float)(delay) + (float)(1.3132E-5); + break; + default: + return ERROR_FAIL; + break; + } + + f_float = 1.0 / t; + f_rounded = roundf(f_float); + *f = (long)f_rounded; + + return ERROR_OK; } /******************* Interface between OpenULINK and OpenOCD ******************/ @@ -1479,13 +1410,12 @@ int ulink_calculate_frequency(enum ulink_delay_type type, int delay, long *f) */ static void ulink_set_end_state(tap_state_t endstate) { - if (tap_is_state_stable(endstate)) { - tap_set_end_state(endstate); - } - else { - LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate)); - exit( EXIT_FAILURE); - } + if (tap_is_state_stable(endstate)) + tap_set_end_state(endstate); + else { + LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate)); + exit(EXIT_FAILURE); + } } /** @@ -1497,24 +1427,23 @@ static void ulink_set_end_state(tap_state_t endstate) */ int ulink_queue_statemove(struct ulink *device) { - uint8_t tms_sequence, tms_count; - int ret; + uint8_t tms_sequence, tms_count; + int ret; - if (tap_get_state() == tap_get_end_state()) { - /* Do nothing if we are already there */ - return ERROR_OK; - } + if (tap_get_state() == tap_get_end_state()) { + /* Do nothing if we are already there */ + return ERROR_OK; + } - tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); - tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); + tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); + tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - ret = ulink_append_clock_tms_cmd(device, tms_count, tms_sequence); + ret = ulink_append_clock_tms_cmd(device, tms_count, tms_sequence); - if (ret == ERROR_OK) { - tap_set_state(tap_get_end_state()); - } + if (ret == ERROR_OK) + tap_set_state(tap_get_end_state()); - return ret; + return ret; } /** @@ -1527,152 +1456,172 @@ int ulink_queue_statemove(struct ulink *device) */ int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd) { - uint32_t scan_size_bits, scan_size_bytes, bits_last_scan; - uint32_t scans_max_payload, bytecount; - uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL; - uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL; - - uint8_t first_tms_count, first_tms_sequence; - uint8_t last_tms_count, last_tms_sequence; - - uint8_t tms_count_pause, tms_sequence_pause; - uint8_t tms_count_resume, tms_sequence_resume; - - uint8_t tms_count_start, tms_sequence_start; - uint8_t tms_count_end, tms_sequence_end; - - enum scan_type type; - int ret; - - /* Determine scan size */ - scan_size_bits = jtag_scan_size(cmd->cmd.scan); - scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8); - - /* Determine scan type (IN/OUT/IO) */ - type = jtag_scan_type(cmd->cmd.scan); - - /* Determine number of scan commands with maximum payload */ - scans_max_payload = scan_size_bytes / 58; - - /* Determine size of last shift command */ - bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8); - - /* Allocate TDO buffer if required */ - if ((type == SCAN_IN) || (type == SCAN_IO)) { - tdo_buffer_start = calloc(sizeof(uint8_t), scan_size_bytes); - - if (tdo_buffer_start == NULL) { - return ERROR_FAIL; - } - - tdo_buffer = tdo_buffer_start; - } - - /* Fill TDI buffer if required */ - if ((type == SCAN_OUT) || (type == SCAN_IO)) { - jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start); - tdi_buffer = tdi_buffer_start; - } - - /* Get TAP state transitions */ - if (cmd->cmd.scan->ir_scan) { - ulink_set_end_state(TAP_IRSHIFT); - first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); - - tap_set_state(TAP_IRSHIFT); - tap_set_end_state(cmd->cmd.scan->end_state); - last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); - - /* TAP state transitions for split scans */ - tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE); - tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE); - tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT); - tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT); - } - else { - ulink_set_end_state(TAP_DRSHIFT); - first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); - - tap_set_state(TAP_DRSHIFT); - tap_set_end_state(cmd->cmd.scan->end_state); - last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); - - /* TAP state transitions for split scans */ - tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE); - tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE); - tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT); - tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT); - } - - /* Generate scan commands */ - bytecount = scan_size_bytes; - while (bytecount > 0) { - if (bytecount == scan_size_bytes) { - /* This is the first scan */ - tms_count_start = first_tms_count; - tms_sequence_start = first_tms_sequence; - } - else { - /* Resume from previous scan */ - tms_count_start = tms_count_resume; - tms_sequence_start = tms_sequence_resume; - } - - if (bytecount > 58) { /* Full scan, at least one scan will follow */ - tms_count_end = tms_count_pause; - tms_sequence_end = tms_sequence_pause; - - ret = ulink_append_scan_cmd(device, type, 58 * 8, tdi_buffer, - tdo_buffer_start, tdo_buffer, tms_count_start, tms_sequence_start, - tms_count_end, tms_sequence_end, cmd, false); - - bytecount -= 58; - - /* Update TDI and TDO buffer pointers */ - if (tdi_buffer_start != NULL) { - tdi_buffer += 58; - } - if (tdo_buffer_start != NULL) { - tdo_buffer += 58; - } - } - else if (bytecount == 58) { /* Full scan, no further scans */ - tms_count_end = last_tms_count; - tms_sequence_end = last_tms_sequence; - - ret = ulink_append_scan_cmd(device, type, 58 * 8, tdi_buffer, - tdo_buffer_start, tdo_buffer, tms_count_start, tms_sequence_start, - tms_count_end, tms_sequence_end, cmd, true); - - bytecount = 0; - } - else { /* Scan with less than maximum payload, no further scans */ - tms_count_end = last_tms_count; - tms_sequence_end = last_tms_sequence; - - ret = ulink_append_scan_cmd(device, type, bits_last_scan, tdi_buffer, - tdo_buffer_start, tdo_buffer, tms_count_start, tms_sequence_start, - tms_count_end, tms_sequence_end, cmd, true); - - bytecount = 0; - } - - if (ret != ERROR_OK) { - free(tdi_buffer_start); - return ret; - } - } - - free(tdi_buffer_start); - - /* Set current state to the end state requested by the command */ - tap_set_state(cmd->cmd.scan->end_state); - - return ERROR_OK; + uint32_t scan_size_bits, scan_size_bytes, bits_last_scan; + uint32_t scans_max_payload, bytecount; + uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL; + uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL; + + uint8_t first_tms_count, first_tms_sequence; + uint8_t last_tms_count, last_tms_sequence; + + uint8_t tms_count_pause, tms_sequence_pause; + uint8_t tms_count_resume, tms_sequence_resume; + + uint8_t tms_count_start, tms_sequence_start; + uint8_t tms_count_end, tms_sequence_end; + + enum scan_type type; + int ret; + + /* Determine scan size */ + scan_size_bits = jtag_scan_size(cmd->cmd.scan); + scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8); + + /* Determine scan type (IN/OUT/IO) */ + type = jtag_scan_type(cmd->cmd.scan); + + /* Determine number of scan commands with maximum payload */ + scans_max_payload = scan_size_bytes / 58; + + /* Determine size of last shift command */ + bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8); + + /* Allocate TDO buffer if required */ + if ((type == SCAN_IN) || (type == SCAN_IO)) { + tdo_buffer_start = calloc(sizeof(uint8_t), scan_size_bytes); + + if (tdo_buffer_start == NULL) + return ERROR_FAIL; + + tdo_buffer = tdo_buffer_start; + } + + /* Fill TDI buffer if required */ + if ((type == SCAN_OUT) || (type == SCAN_IO)) { + jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start); + tdi_buffer = tdi_buffer_start; + } + + /* Get TAP state transitions */ + if (cmd->cmd.scan->ir_scan) { + ulink_set_end_state(TAP_IRSHIFT); + first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); + first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); + + tap_set_state(TAP_IRSHIFT); + tap_set_end_state(cmd->cmd.scan->end_state); + last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); + last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); + + /* TAP state transitions for split scans */ + tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE); + tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE); + tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT); + tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT); + } else { + ulink_set_end_state(TAP_DRSHIFT); + first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); + first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); + + tap_set_state(TAP_DRSHIFT); + tap_set_end_state(cmd->cmd.scan->end_state); + last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); + last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state()); + + /* TAP state transitions for split scans */ + tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE); + tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE); + tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT); + tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT); + } + + /* Generate scan commands */ + bytecount = scan_size_bytes; + while (bytecount > 0) { + if (bytecount == scan_size_bytes) { + /* This is the first scan */ + tms_count_start = first_tms_count; + tms_sequence_start = first_tms_sequence; + } else { + /* Resume from previous scan */ + tms_count_start = tms_count_resume; + tms_sequence_start = tms_sequence_resume; + } + + if (bytecount > 58) { /* Full scan, at least one scan will follow */ + tms_count_end = tms_count_pause; + tms_sequence_end = tms_sequence_pause; + + ret = ulink_append_scan_cmd(device, + type, + 58 * 8, + tdi_buffer, + tdo_buffer_start, + tdo_buffer, + tms_count_start, + tms_sequence_start, + tms_count_end, + tms_sequence_end, + cmd, + false); + + bytecount -= 58; + + /* Update TDI and TDO buffer pointers */ + if (tdi_buffer_start != NULL) + tdi_buffer += 58; + if (tdo_buffer_start != NULL) + tdo_buffer += 58; + } else if (bytecount == 58) { /* Full scan, no further scans */ + tms_count_end = last_tms_count; + tms_sequence_end = last_tms_sequence; + + ret = ulink_append_scan_cmd(device, + type, + 58 * 8, + tdi_buffer, + tdo_buffer_start, + tdo_buffer, + tms_count_start, + tms_sequence_start, + tms_count_end, + tms_sequence_end, + cmd, + true); + + bytecount = 0; + } else {/* Scan with less than maximum payload, no further scans */ + tms_count_end = last_tms_count; + tms_sequence_end = last_tms_sequence; + + ret = ulink_append_scan_cmd(device, + type, + bits_last_scan, + tdi_buffer, + tdo_buffer_start, + tdo_buffer, + tms_count_start, + tms_sequence_start, + tms_count_end, + tms_sequence_end, + cmd, + true); + + bytecount = 0; + } + + if (ret != ERROR_OK) { + free(tdi_buffer_start); + return ret; + } + } + + free(tdi_buffer_start); + + /* Set current state to the end state requested by the command */ + tap_set_state(cmd->cmd.scan->end_state); + + return ERROR_OK; } /** @@ -1685,15 +1634,14 @@ int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd) */ int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd) { - int ret; + int ret; - ret = ulink_append_clock_tms_cmd(device, 5, 0xff); + ret = ulink_append_clock_tms_cmd(device, 5, 0xff); - if (ret == ERROR_OK) { - tap_set_state(TAP_RESET); - } + if (ret == ERROR_OK) + tap_set_state(TAP_RESET); - return ret; + return ret; } /** @@ -1709,27 +1657,26 @@ int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd) */ int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd) { - int ret; - - /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */ - if (tap_get_state() != TAP_IDLE) { - ulink_set_end_state(TAP_IDLE); - ulink_queue_statemove(device); - } - - /* Generate the clock cycles */ - ret = ulink_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles); - if (ret != ERROR_OK) { - return ret; - } - - /* Move to end state specified in command */ - if (cmd->cmd.runtest->end_state != tap_get_state()) { - tap_set_end_state(cmd->cmd.runtest->end_state); - ulink_queue_statemove(device); - } - - return ERROR_OK; + int ret; + + /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */ + if (tap_get_state() != TAP_IDLE) { + ulink_set_end_state(TAP_IDLE); + ulink_queue_statemove(device); + } + + /* Generate the clock cycles */ + ret = ulink_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles); + if (ret != ERROR_OK) + return ret; + + /* Move to end state specified in command */ + if (cmd->cmd.runtest->end_state != tap_get_state()) { + tap_set_end_state(cmd->cmd.runtest->end_state); + ulink_queue_statemove(device); + } + + return ERROR_OK; } /** @@ -1741,24 +1688,20 @@ int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd) */ int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd) { - uint8_t low = 0, high = 0; - - if (cmd->cmd.reset->trst) { - tap_set_state(TAP_RESET); - high |= SIGNAL_TRST; - } - else { - low |= SIGNAL_TRST; - } - - if (cmd->cmd.reset->srst) { - high |= SIGNAL_RESET; - } - else { - low |= SIGNAL_RESET; - } - - return ulink_append_set_signals_cmd(device, low, high); + uint8_t low = 0, high = 0; + + if (cmd->cmd.reset->trst) { + tap_set_state(TAP_RESET); + high |= SIGNAL_TRST; + } else + low |= SIGNAL_TRST; + + if (cmd->cmd.reset->srst) + high |= SIGNAL_RESET; + else + low |= SIGNAL_RESET; + + return ulink_append_set_signals_cmd(device, low, high); } /** @@ -1771,58 +1714,53 @@ int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd) */ int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd) { - int ret, i, num_states, batch_size, state_count; - tap_state_t *path; - uint8_t tms_sequence; - - num_states = cmd->cmd.pathmove->num_states; - path = cmd->cmd.pathmove->path; - state_count = 0; - - while (num_states > 0) { - tms_sequence = 0; - - /* Determine batch size */ - if (num_states >= 8) { - batch_size = 8; - } - else { - batch_size = num_states; - } - - for (i = 0; i < batch_size; i++) { - if (tap_state_transition(tap_get_state(), false) == path[state_count]) { - /* Append '0' transition: clear bit 'i' in tms_sequence */ - buf_set_u32(&tms_sequence, i, 1, 0x0); - } - else if (tap_state_transition(tap_get_state(), true) - == path[state_count]) { - /* Append '1' transition: set bit 'i' in tms_sequence */ - buf_set_u32(&tms_sequence, i, 1, 0x1); - } - else { - /* Invalid state transition */ - LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition", - tap_state_name(tap_get_state()), - tap_state_name(path[state_count])); - return ERROR_FAIL; - } - - tap_set_state(path[state_count]); - state_count++; - num_states--; - } - - /* Append CLOCK_TMS command to OpenULINK command queue */ - LOG_INFO( - "pathmove batch: count = %i, sequence = 0x%x", batch_size, tms_sequence); - ret = ulink_append_clock_tms_cmd(ulink_handle, batch_size, tms_sequence); - if (ret != ERROR_OK) { - return ret; - } - } - - return ERROR_OK; + int ret, i, num_states, batch_size, state_count; + tap_state_t *path; + uint8_t tms_sequence; + + num_states = cmd->cmd.pathmove->num_states; + path = cmd->cmd.pathmove->path; + state_count = 0; + + while (num_states > 0) { + tms_sequence = 0; + + /* Determine batch size */ + if (num_states >= 8) + batch_size = 8; + else + batch_size = num_states; + + for (i = 0; i < batch_size; i++) { + if (tap_state_transition(tap_get_state(), false) == path[state_count]) { + /* Append '0' transition: clear bit 'i' in tms_sequence */ + buf_set_u32(&tms_sequence, i, 1, 0x0); + } else if (tap_state_transition(tap_get_state(), true) + == path[state_count]) { + /* Append '1' transition: set bit 'i' in tms_sequence */ + buf_set_u32(&tms_sequence, i, 1, 0x1); + } else { + /* Invalid state transition */ + LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition", + tap_state_name(tap_get_state()), + tap_state_name(path[state_count])); + return ERROR_FAIL; + } + + tap_set_state(path[state_count]); + state_count++; + num_states--; + } + + /* Append CLOCK_TMS command to OpenULINK command queue */ + LOG_INFO( + "pathmove batch: count = %i, sequence = 0x%x", batch_size, tms_sequence); + ret = ulink_append_clock_tms_cmd(ulink_handle, batch_size, tms_sequence); + if (ret != ERROR_OK) + return ret; + } + + return ERROR_OK; } /** @@ -1835,9 +1773,9 @@ int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd) */ int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd) { - /* IMPORTANT! Due to the time offset in command execution introduced by - * command queueing, this needs to be implemented in the ULINK device */ - return ulink_append_sleep_cmd(device, cmd->cmd.sleep->us); + /* IMPORTANT! Due to the time offset in command execution introduced by + * command queueing, this needs to be implemented in the ULINK device */ + return ulink_append_sleep_cmd(device, cmd->cmd.sleep->us); } /** @@ -1848,45 +1786,40 @@ int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd) */ int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd) { - int ret; - unsigned num_cycles; - - if (!tap_is_state_stable(tap_get_state())) { - LOG_ERROR("JTAG_STABLECLOCKS: state not stable"); - return ERROR_FAIL; - } - - num_cycles = cmd->cmd.stableclocks->num_cycles; - - /* TMS stays either high (Test Logic Reset state) or low (all other states) */ - if (tap_get_state() == TAP_RESET) { - ret = ulink_append_set_signals_cmd(device, 0, SIGNAL_TMS); - } - else { - ret = ulink_append_set_signals_cmd(device, SIGNAL_TMS, 0); - } - - if (ret != ERROR_OK) { - return ret; - } - - while (num_cycles > 0) { - if (num_cycles > 0xFFFF) { - /* OpenULINK CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */ - ret = ulink_append_clock_tck_cmd(device, 0xFFFF); - num_cycles -= 0xFFFF; - } - else { - ret = ulink_append_clock_tck_cmd(device, num_cycles); - num_cycles = 0; - } - - if (ret != ERROR_OK) { - return ret; - } - } - - return ERROR_OK; + int ret; + unsigned num_cycles; + + if (!tap_is_state_stable(tap_get_state())) { + LOG_ERROR("JTAG_STABLECLOCKS: state not stable"); + return ERROR_FAIL; + } + + num_cycles = cmd->cmd.stableclocks->num_cycles; + + /* TMS stays either high (Test Logic Reset state) or low (all other states) */ + if (tap_get_state() == TAP_RESET) + ret = ulink_append_set_signals_cmd(device, 0, SIGNAL_TMS); + else + ret = ulink_append_set_signals_cmd(device, SIGNAL_TMS, 0); + + if (ret != ERROR_OK) + return ret; + + while (num_cycles > 0) { + if (num_cycles > 0xFFFF) { + /* OpenULINK CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */ + ret = ulink_append_clock_tck_cmd(device, 0xFFFF); + num_cycles -= 0xFFFF; + } else { + ret = ulink_append_clock_tck_cmd(device, num_cycles); + num_cycles = 0; + } + + if (ret != ERROR_OK) + return ret; + } + + return ERROR_OK; } /** @@ -1898,26 +1831,26 @@ int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd) */ int ulink_post_process_scan(struct ulink_cmd *ulink_cmd) { - struct jtag_command *cmd = ulink_cmd->cmd_origin; - int ret; - - switch (jtag_scan_type(cmd->cmd.scan)) { - case SCAN_IN: - case SCAN_IO: - ret = jtag_read_buffer(ulink_cmd->payload_in_start, cmd->cmd.scan); - break; - case SCAN_OUT: - /* Nothing to do for OUT scans */ - ret = ERROR_OK; - break; - default: - LOG_ERROR("BUG: ulink_post_process_scan() encountered an unknown" - " JTAG scan type"); - ret = ERROR_FAIL; - break; - } - - return ret; + struct jtag_command *cmd = ulink_cmd->cmd_origin; + int ret; + + switch (jtag_scan_type(cmd->cmd.scan)) { + case SCAN_IN: + case SCAN_IO: + ret = jtag_read_buffer(ulink_cmd->payload_in_start, cmd->cmd.scan); + break; + case SCAN_OUT: + /* Nothing to do for OUT scans */ + ret = ERROR_OK; + break; + default: + LOG_ERROR("BUG: ulink_post_process_scan() encountered an unknown" + " JTAG scan type"); + ret = ERROR_FAIL; + break; + } + + return ret; } /** @@ -1929,47 +1862,46 @@ int ulink_post_process_scan(struct ulink_cmd *ulink_cmd) */ int ulink_post_process_queue(struct ulink *device) { - struct ulink_cmd *current; - struct jtag_command *openocd_cmd; - int ret; - - current = device->queue_start; - - while (current != NULL) { - openocd_cmd = current->cmd_origin; - - /* Check if a corresponding OpenOCD command is stored for this - * OpenULINK command */ - if ((current->needs_postprocessing == true) && (openocd_cmd != NULL)) { - switch (openocd_cmd->type) { - case JTAG_SCAN: - ret = ulink_post_process_scan(current); - break; - case JTAG_TLR_RESET: - case JTAG_RUNTEST: - case JTAG_RESET: - case JTAG_PATHMOVE: - case JTAG_SLEEP: - case JTAG_STABLECLOCKS: - /* Nothing to do for these commands */ - ret = ERROR_OK; - break; - default: - ret = ERROR_FAIL; - LOG_ERROR("BUG: ulink_post_process_queue() encountered unknown JTAG " - "command type"); - break; - } - - if (ret != ERROR_OK) { - return ret; - } - } - - current = current->next; - } - - return ERROR_OK; + struct ulink_cmd *current; + struct jtag_command *openocd_cmd; + int ret; + + current = device->queue_start; + + while (current != NULL) { + openocd_cmd = current->cmd_origin; + + /* Check if a corresponding OpenOCD command is stored for this + * OpenULINK command */ + if ((current->needs_postprocessing == true) && (openocd_cmd != NULL)) { + switch (openocd_cmd->type) { + case JTAG_SCAN: + ret = ulink_post_process_scan(current); + break; + case JTAG_TLR_RESET: + case JTAG_RUNTEST: + case JTAG_RESET: + case JTAG_PATHMOVE: + case JTAG_SLEEP: + case JTAG_STABLECLOCKS: + /* Nothing to do for these commands */ + ret = ERROR_OK; + break; + default: + ret = ERROR_FAIL; + LOG_ERROR("BUG: ulink_post_process_queue() encountered unknown JTAG " + "command type"); + break; + } + + if (ret != ERROR_OK) + return ret; + } + + current = current->next; + } + + return ERROR_OK; } /**************************** JTAG driver functions ***************************/ @@ -1988,60 +1920,57 @@ int ulink_post_process_queue(struct ulink *device) */ static int ulink_execute_queue(void) { - struct jtag_command *cmd = jtag_command_queue; - int ret; - - while (cmd) { - switch (cmd->type) { - case JTAG_SCAN: - ret = ulink_queue_scan(ulink_handle, cmd); - break; - case JTAG_TLR_RESET: - ret = ulink_queue_tlr_reset(ulink_handle, cmd); - break; - case JTAG_RUNTEST: - ret = ulink_queue_runtest(ulink_handle, cmd); - break; - case JTAG_RESET: - ret = ulink_queue_reset(ulink_handle, cmd); - break; - case JTAG_PATHMOVE: - ret = ulink_queue_pathmove(ulink_handle, cmd); - break; - case JTAG_SLEEP: - ret = ulink_queue_sleep(ulink_handle, cmd); - break; - case JTAG_STABLECLOCKS: - ret = ulink_queue_stableclocks(ulink_handle, cmd); - break; - default: - ret = ERROR_FAIL; - LOG_ERROR("BUG: encountered unknown JTAG command type"); - break; - } - - if (ret != ERROR_OK) { - return ret; - } - - cmd = cmd->next; - } - - if (ulink_handle->commands_in_queue > 0) { - ret = ulink_execute_queued_commands(ulink_handle, USB_TIMEOUT); - if (ret != ERROR_OK) { - return ret; - } - - ret = ulink_post_process_queue(ulink_handle); - if (ret != ERROR_OK) { - return ret; - } - - ulink_clear_queue(ulink_handle); - } - - return ERROR_OK; + struct jtag_command *cmd = jtag_command_queue; + int ret; + + while (cmd) { + switch (cmd->type) { + case JTAG_SCAN: + ret = ulink_queue_scan(ulink_handle, cmd); + break; + case JTAG_TLR_RESET: + ret = ulink_queue_tlr_reset(ulink_handle, cmd); + break; + case JTAG_RUNTEST: + ret = ulink_queue_runtest(ulink_handle, cmd); + break; + case JTAG_RESET: + ret = ulink_queue_reset(ulink_handle, cmd); + break; + case JTAG_PATHMOVE: + ret = ulink_queue_pathmove(ulink_handle, cmd); + break; + case JTAG_SLEEP: + ret = ulink_queue_sleep(ulink_handle, cmd); + break; + case JTAG_STABLECLOCKS: + ret = ulink_queue_stableclocks(ulink_handle, cmd); + break; + default: + ret = ERROR_FAIL; + LOG_ERROR("BUG: encountered unknown JTAG command type"); + break; + } + + if (ret != ERROR_OK) + return ret; + + cmd = cmd->next; + } + + if (ulink_handle->commands_in_queue > 0) { + ret = ulink_execute_queued_commands(ulink_handle, USB_TIMEOUT); + if (ret != ERROR_OK) + return ret; + + ret = ulink_post_process_queue(ulink_handle); + if (ret != ERROR_OK) + return ret; + + ulink_clear_queue(ulink_handle); + } + + return ERROR_OK; } /** @@ -2054,103 +1983,95 @@ static int ulink_execute_queue(void) */ static int ulink_khz(int khz, int *jtag_speed) { - int ret; - - if (khz == 0) { - LOG_ERROR("RCLK not supported"); - return ERROR_FAIL; - } - - /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency - * setting can be done independently from all other commands. */ - if (khz >= 375) { - ulink_handle->delay_clock_tck = -1; - } - else { - ret = ulink_calculate_delay(DELAY_CLOCK_TCK, khz * 1000, - &ulink_handle->delay_clock_tck); - if (ret != ERROR_OK) { - return ret; - } - } - - /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the - * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS - * commands, all SCAN commands MUST also use the variable frequency - * implementation! */ - if (khz >= 176) { - ulink_handle->delay_clock_tms = -1; - ulink_handle->delay_scan_in = -1; - ulink_handle->delay_scan_out = -1; - ulink_handle->delay_scan_io = -1; - } - else { - ret = ulink_calculate_delay(DELAY_CLOCK_TMS, khz * 1000, - &ulink_handle->delay_clock_tms); - if (ret != ERROR_OK) { - return ret; - } - - ret = ulink_calculate_delay(DELAY_SCAN_IN, khz * 1000, - &ulink_handle->delay_scan_in); - if (ret != ERROR_OK) { - return ret; - } - - ret = ulink_calculate_delay(DELAY_SCAN_OUT, khz * 1000, - &ulink_handle->delay_scan_out); - if (ret != ERROR_OK) { - return ret; - } - - ret = ulink_calculate_delay(DELAY_SCAN_IO, khz * 1000, - &ulink_handle->delay_scan_io); - if (ret != ERROR_OK) { - return ret; - } - } + int ret; + + if (khz == 0) { + LOG_ERROR("RCLK not supported"); + return ERROR_FAIL; + } + + /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency + * setting can be done independently from all other commands. */ + if (khz >= 375) + ulink_handle->delay_clock_tck = -1; + else { + ret = ulink_calculate_delay(DELAY_CLOCK_TCK, khz * 1000, + &ulink_handle->delay_clock_tck); + if (ret != ERROR_OK) + return ret; + } + + /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the + * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS + * commands, all SCAN commands MUST also use the variable frequency + * implementation! */ + if (khz >= 176) { + ulink_handle->delay_clock_tms = -1; + ulink_handle->delay_scan_in = -1; + ulink_handle->delay_scan_out = -1; + ulink_handle->delay_scan_io = -1; + } else { + ret = ulink_calculate_delay(DELAY_CLOCK_TMS, khz * 1000, + &ulink_handle->delay_clock_tms); + if (ret != ERROR_OK) + return ret; + + ret = ulink_calculate_delay(DELAY_SCAN_IN, khz * 1000, + &ulink_handle->delay_scan_in); + if (ret != ERROR_OK) + return ret; + + ret = ulink_calculate_delay(DELAY_SCAN_OUT, khz * 1000, + &ulink_handle->delay_scan_out); + if (ret != ERROR_OK) + return ret; + + ret = ulink_calculate_delay(DELAY_SCAN_IO, khz * 1000, + &ulink_handle->delay_scan_io); + if (ret != ERROR_OK) + return ret; + } #ifdef _DEBUG_JTAG_IO_ - long f_tck, f_tms, f_scan_in, f_scan_out, f_scan_io; - - ulink_calculate_frequency(DELAY_CLOCK_TCK, ulink_handle->delay_clock_tck, - &f_tck); - ulink_calculate_frequency(DELAY_CLOCK_TMS, ulink_handle->delay_clock_tms, - &f_tms); - ulink_calculate_frequency(DELAY_SCAN_IN, ulink_handle->delay_scan_in, - &f_scan_in); - ulink_calculate_frequency(DELAY_SCAN_OUT, ulink_handle->delay_scan_out, - &f_scan_out); - ulink_calculate_frequency(DELAY_SCAN_IO, ulink_handle->delay_scan_io, - &f_scan_io); - - DEBUG_JTAG_IO("ULINK TCK setup: delay_tck = %i (%li Hz),", - ulink_handle->delay_clock_tck, f_tck); - DEBUG_JTAG_IO(" delay_tms = %i (%li Hz),", - ulink_handle->delay_clock_tms, f_tms); - DEBUG_JTAG_IO(" delay_scan_in = %i (%li Hz),", - ulink_handle->delay_scan_in, f_scan_in); - DEBUG_JTAG_IO(" delay_scan_out = %i (%li Hz),", - ulink_handle->delay_scan_out, f_scan_out); - DEBUG_JTAG_IO(" delay_scan_io = %i (%li Hz),", - ulink_handle->delay_scan_io, f_scan_io); + long f_tck, f_tms, f_scan_in, f_scan_out, f_scan_io; + + ulink_calculate_frequency(DELAY_CLOCK_TCK, ulink_handle->delay_clock_tck, + &f_tck); + ulink_calculate_frequency(DELAY_CLOCK_TMS, ulink_handle->delay_clock_tms, + &f_tms); + ulink_calculate_frequency(DELAY_SCAN_IN, ulink_handle->delay_scan_in, + &f_scan_in); + ulink_calculate_frequency(DELAY_SCAN_OUT, ulink_handle->delay_scan_out, + &f_scan_out); + ulink_calculate_frequency(DELAY_SCAN_IO, ulink_handle->delay_scan_io, + &f_scan_io); + + DEBUG_JTAG_IO("ULINK TCK setup: delay_tck = %i (%li Hz),", + ulink_handle->delay_clock_tck, f_tck); + DEBUG_JTAG_IO(" delay_tms = %i (%li Hz),", + ulink_handle->delay_clock_tms, f_tms); + DEBUG_JTAG_IO(" delay_scan_in = %i (%li Hz),", + ulink_handle->delay_scan_in, f_scan_in); + DEBUG_JTAG_IO(" delay_scan_out = %i (%li Hz),", + ulink_handle->delay_scan_out, f_scan_out); + DEBUG_JTAG_IO(" delay_scan_io = %i (%li Hz),", + ulink_handle->delay_scan_io, f_scan_io); #endif - /* Configure the ULINK device with the new delay values */ - ret = ulink_append_configure_tck_cmd(ulink_handle, - ulink_handle->delay_scan_in, - ulink_handle->delay_scan_out, - ulink_handle->delay_scan_io, - ulink_handle->delay_clock_tck, - ulink_handle->delay_clock_tms); + /* Configure the ULINK device with the new delay values */ + ret = ulink_append_configure_tck_cmd(ulink_handle, + ulink_handle->delay_scan_in, + ulink_handle->delay_scan_out, + ulink_handle->delay_scan_io, + ulink_handle->delay_clock_tck, + ulink_handle->delay_clock_tms); - if (ret != ERROR_OK) { - return ret; - } + if (ret != ERROR_OK) + return ret; - *jtag_speed = khz; + *jtag_speed = khz; - return ERROR_OK; + return ERROR_OK; } /** @@ -2167,9 +2088,9 @@ static int ulink_khz(int khz, int *jtag_speed) */ static int ulink_speed(int speed) { - int dummy; + int dummy; - return ulink_khz(speed, &dummy); + return ulink_khz(speed, &dummy); } /** @@ -2187,9 +2108,9 @@ static int ulink_speed(int speed) */ static int ulink_speed_div(int speed, int *khz) { - *khz = speed; + *khz = speed; - return ERROR_OK; + return ERROR_OK; } /** @@ -2201,100 +2122,94 @@ static int ulink_speed_div(int speed, int *khz) */ static int ulink_init(void) { - int ret; - char str_manufacturer[20]; - bool download_firmware = false; - uint8_t *dummy; - uint8_t input_signals, output_signals; - - ulink_handle = calloc(1, sizeof(struct ulink)); - if (ulink_handle == NULL) { - return ERROR_FAIL; - } - - usb_init(); - - ret = ulink_usb_open(&ulink_handle); - if (ret != ERROR_OK) { - LOG_ERROR("Could not open ULINK device"); - return ret; - } - - /* Get String Descriptor to determine if firmware needs to be loaded */ - ret = usb_get_string_simple(ulink_handle->usb_handle, 1, str_manufacturer, 20); - if (ret < 0) { - /* Could not get descriptor -> Unconfigured or original Keil firmware */ - download_firmware = true; - } - else { - /* We got a String Descriptor, check if it is the correct one */ - if (strncmp(str_manufacturer, "OpenULINK", 9) != 0) { - download_firmware = true; - } - } - - if (download_firmware == true) { - LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling" - " ULINK device."); - ret = ulink_load_firmware_and_renumerate(&ulink_handle, - ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY); - if (ret != ERROR_OK) { - LOG_ERROR("Could not download firmware and re-numerate ULINK"); - return ret; - } - } - else { - LOG_INFO("ULINK device is already running OpenULINK firmware"); - } - - /* Initialize OpenULINK command queue */ - ulink_clear_queue(ulink_handle); - - /* Issue one test command with short timeout */ - ret = ulink_append_test_cmd(ulink_handle); - if (ret != ERROR_OK) { - return ret; - } - - ret = ulink_execute_queued_commands(ulink_handle, 200); - if (ret != ERROR_OK) { - /* Sending test command failed. The ULINK device may be forever waiting for - * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was - * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */ - dummy = calloc(64, sizeof(uint8_t)); - - ret = usb_bulk_read(ulink_handle->usb_handle, (2 | USB_ENDPOINT_IN), - (char *)dummy, 64, 200); - - free(dummy); - - if (ret < 0) { - /* Bulk IN transfer failed -> unrecoverable error condition */ - LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from " - "the USB port and re-connect, then re-run OpenOCD"); - return ERROR_FAIL; - } + int ret; + char str_manufacturer[20]; + bool download_firmware = false; + uint8_t *dummy; + uint8_t input_signals, output_signals; + + ulink_handle = calloc(1, sizeof(struct ulink)); + if (ulink_handle == NULL) + return ERROR_FAIL; + + usb_init(); + + ret = ulink_usb_open(&ulink_handle); + if (ret != ERROR_OK) { + LOG_ERROR("Could not open ULINK device"); + return ret; + } + + /* Get String Descriptor to determine if firmware needs to be loaded */ + ret = usb_get_string_simple(ulink_handle->usb_handle, 1, str_manufacturer, 20); + if (ret < 0) { + /* Could not get descriptor -> Unconfigured or original Keil firmware */ + download_firmware = true; + } else { + /* We got a String Descriptor, check if it is the correct one */ + if (strncmp(str_manufacturer, "OpenULINK", 9) != 0) + download_firmware = true; + } + + if (download_firmware == true) { + LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling" + " ULINK device."); + ret = ulink_load_firmware_and_renumerate(&ulink_handle, + ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY); + if (ret != ERROR_OK) { + LOG_ERROR("Could not download firmware and re-numerate ULINK"); + return ret; + } + } else + LOG_INFO("ULINK device is already running OpenULINK firmware"); + + /* Initialize OpenULINK command queue */ + ulink_clear_queue(ulink_handle); + + /* Issue one test command with short timeout */ + ret = ulink_append_test_cmd(ulink_handle); + if (ret != ERROR_OK) + return ret; + + ret = ulink_execute_queued_commands(ulink_handle, 200); + if (ret != ERROR_OK) { + /* Sending test command failed. The ULINK device may be forever waiting for + * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was + * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */ + dummy = calloc(64, sizeof(uint8_t)); + + ret = usb_bulk_read(ulink_handle->usb_handle, (2 | USB_ENDPOINT_IN), + (char *)dummy, 64, 200); + + free(dummy); + + if (ret < 0) { + /* Bulk IN transfer failed -> unrecoverable error condition */ + LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from " + "the USB port and re-connect, then re-run OpenOCD"); + return ERROR_FAIL; + } #ifdef _DEBUG_USB_COMMS_ - else { - /* Successfully received Bulk IN packet -> continue */ - LOG_INFO("Recovered from lost Bulk IN packet"); - } + else { + /* Successfully received Bulk IN packet -> continue */ + LOG_INFO("Recovered from lost Bulk IN packet"); + } #endif - } - ulink_clear_queue(ulink_handle); + } + ulink_clear_queue(ulink_handle); - ulink_append_get_signals_cmd(ulink_handle); - ulink_execute_queued_commands(ulink_handle, 200); + ulink_append_get_signals_cmd(ulink_handle); + ulink_execute_queued_commands(ulink_handle, 200); - /* Post-process the single CMD_GET_SIGNALS command */ - input_signals = ulink_handle->queue_start->payload_in[0]; - output_signals = ulink_handle->queue_start->payload_in[1]; + /* Post-process the single CMD_GET_SIGNALS command */ + input_signals = ulink_handle->queue_start->payload_in[0]; + output_signals = ulink_handle->queue_start->payload_in[1]; - ulink_print_signal_states(input_signals, output_signals); + ulink_print_signal_states(input_signals, output_signals); - ulink_clear_queue(ulink_handle); + ulink_clear_queue(ulink_handle); - return ERROR_OK; + return ERROR_OK; } /** @@ -2305,12 +2220,12 @@ static int ulink_init(void) */ static int ulink_quit(void) { - int ret; + int ret; - ret = ulink_usb_close(&ulink_handle); - free(ulink_handle); + ret = ulink_usb_close(&ulink_handle); + free(ulink_handle); - return ret; + return ret; } /** @@ -2318,45 +2233,45 @@ static int ulink_quit(void) */ COMMAND_HANDLER(ulink_download_firmware_handler) { - int ret; + int ret; if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV[0]); + LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV[0]); - /* Download firmware image in CMD_ARGV[0] */ - ret = ulink_load_firmware_and_renumerate(&ulink_handle, (char *)CMD_ARGV[0], - ULINK_RENUMERATION_DELAY); + /* Download firmware image in CMD_ARGV[0] */ + ret = ulink_load_firmware_and_renumerate(&ulink_handle, (char *)CMD_ARGV[0], + ULINK_RENUMERATION_DELAY); - return ret; + return ret; } /*************************** Command Registration **************************/ static const struct command_registration ulink_command_handlers[] = { - { - .name = "ulink_download_firmware", - .handler = &ulink_download_firmware_handler, - .mode = COMMAND_EXEC, - .help = "download firmware image to ULINK device", - .usage = "path/to/ulink_firmware.hex", - }, - COMMAND_REGISTRATION_DONE, + { + .name = "ulink_download_firmware", + .handler = &ulink_download_firmware_handler, + .mode = COMMAND_EXEC, + .help = "download firmware image to ULINK device", + .usage = "path/to/ulink_firmware.hex", + }, + COMMAND_REGISTRATION_DONE, }; struct jtag_interface ulink_interface = { - .name = "ulink", + .name = "ulink", - .commands = ulink_command_handlers, - .transports = jtag_only, + .commands = ulink_command_handlers, + .transports = jtag_only, - .execute_queue = ulink_execute_queue, - .khz = ulink_khz, - .speed = ulink_speed, - .speed_div = ulink_speed_div, + .execute_queue = ulink_execute_queue, + .khz = ulink_khz, + .speed = ulink_speed, + .speed_div = ulink_speed_div, - .init = ulink_init, - .quit = ulink_quit + .init = ulink_init, + .quit = ulink_quit }; diff --git a/src/jtag/drivers/usb_blaster.c b/src/jtag/drivers/usb_blaster.c index c5c356ce57..088ae34f80 100644 --- a/src/jtag/drivers/usb_blaster.c +++ b/src/jtag/drivers/usb_blaster.c @@ -89,7 +89,7 @@ #if (BUILD_USB_BLASTER_FTD2XX == 1 && BUILD_USB_BLASTER_LIBFTDI == 1) #error "BUILD_USB_BLASTER_FTD2XX && BUILD_USB_BLASTER_LIBFTDI " - "are mutually exclusive" +"are mutually exclusive" #elif (BUILD_USB_BLASTER_FTD2XX != 1 && BUILD_USB_BLASTER_LIBFTDI != 1) #error "BUILD_USB_BLASTER_FTD2XX || BUILD_USB_BLASTER_LIBFTDI must be chosen" #endif @@ -106,15 +106,15 @@ #include static char *usb_blaster_device_desc; -static uint16_t usb_blaster_vid = 0x09fb; /* Altera */ -static uint16_t usb_blaster_pid = 0x6001; /* USB-Blaster */ +static uint16_t usb_blaster_vid = 0x09fb; /* Altera */ +static uint16_t usb_blaster_pid = 0x6001; /* USB-Blaster */ /* last output byte in simple bit banging (legacy) mode */ static uint8_t out_value; /* global output buffer for bit banging */ -#define BUF_LEN 64 //Size of EP1 +#define BUF_LEN 64 /* Size of EP1 */ static uint8_t out_buffer[BUF_LEN]; -static uint16_t out_count = 0; +static uint16_t out_count; #if BUILD_USB_BLASTER_FTD2XX == 1 static FT_HANDLE ftdih; @@ -133,8 +133,7 @@ static int usb_blaster_buf_write( LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf[0], size); #endif status = FT_Write(ftdih, buf, size, &dw_bytes_written); - if (status != FT_OK) - { + if (status != FT_OK) { *bytes_written = dw_bytes_written; LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; @@ -147,8 +146,7 @@ static int usb_blaster_buf_write( LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf[0], size); #endif retval = ftdi_write_data(&ftdic, buf, size); - if (retval < 0) - { + if (retval < 0) { *bytes_written = 0; LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; @@ -158,16 +156,14 @@ static int usb_blaster_buf_write( #endif } -static int -usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read) +static int usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read) { #if BUILD_USB_BLASTER_FTD2XX == 1 DWORD dw_bytes_read; FT_STATUS status; status = FT_Read(ftdih, buf, size, &dw_bytes_read); - if (status != FT_OK) - { + if (status != FT_OK) { *bytes_read = dw_bytes_read; LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status)); return ERROR_JTAG_DEVICE_ERROR; @@ -183,15 +179,13 @@ usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read) int timeout = 100; *bytes_read = 0; - while ((*bytes_read < size) && timeout--) - { + while ((*bytes_read < size) && timeout--) { retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read); - if (retval < 0) - { + if (retval < 0) { *bytes_read = 0; LOG_ERROR("ftdi_read_data: %s", - ftdi_get_error_string(&ftdic)); + ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; } *bytes_read += retval; @@ -249,13 +243,13 @@ usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read) #define READ_TDO (1 << 0) -static void usb_blaster_write_databuffer(uint8_t* buf, uint16_t len) +static void usb_blaster_write_databuffer(uint8_t *buf, uint16_t len) { uint32_t bytes_written; usb_blaster_buf_write(buf, len, &bytes_written); out_count = 0; #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("---- WROTE %d",bytes_written); + LOG_DEBUG("---- WROTE %d", bytes_written); #endif } @@ -263,7 +257,7 @@ static void usb_blaster_addtowritebuffer(uint8_t value, bool forcewrite) { out_buffer[out_count] = value; out_count += 1; - if(out_count == BUF_LEN || forcewrite) + if (out_count == BUF_LEN || forcewrite) usb_blaster_write_databuffer(out_buffer, out_count); } @@ -273,7 +267,7 @@ static int usb_blaster_read_data(void) uint8_t buf[1]; uint32_t bytes_read; - if(out_count > 0) + if (out_count > 0) usb_blaster_write_databuffer(out_buffer, out_count); out_value |= READ; @@ -313,12 +307,12 @@ static int usb_blaster_speed(int speed) /* TODO: libftdi's ftdi_set_baudrate chokes on high rates, use lowlevel * usb function instead! And additionally allow user to throttle. */ - if (ftdi_set_baudrate(&ftdic, 3000000 / 4) < 0) - { + if (ftdi_set_baudrate(&ftdic, 3000000 / 4) < 0) { LOG_ERROR("Can't set baud rate to max: %s", ftdi_get_error_string(&ftdic)); return ERROR_JTAG_DEVICE_ERROR; - }; + } + ; #endif return ERROR_OK; @@ -327,13 +321,13 @@ static int usb_blaster_speed(int speed) static void usb_blaster_reset(int trst, int srst) { LOG_DEBUG("TODO: usb_blaster_reset(%d,%d) isn't implemented!", - trst, srst); + trst, srst); } static void usb_blaster_blink(int state) { out_value = 0x00; - if(state) + if (state) out_value |= LED; usb_blaster_addtowritebuffer(out_value, true); @@ -362,18 +356,16 @@ static int usb_blaster_init(void) #if BUILD_USB_BLASTER_FTD2XX == 1 /* Open by device description */ - if (usb_blaster_device_desc == NULL) - { + if (usb_blaster_device_desc == NULL) { LOG_WARNING("no usb_blaster device description specified, " - "using default 'USB-Blaster'"); + "using default 'USB-Blaster'"); usb_blaster_device_desc = "USB-Blaster"; } #if IS_WIN32 == 0 /* Add non-standard Vid/Pid to the linux driver */ status = FT_SetVIDPID(usb_blaster_vid, usb_blaster_pid); - if (status != FT_OK) - { + if (status != FT_OK) { LOG_WARNING("couldn't add %4.4x:%4.4x", usb_blaster_vid, usb_blaster_pid); } @@ -381,18 +373,16 @@ static int usb_blaster_init(void) status = FT_OpenEx(usb_blaster_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih); - if (status != FT_OK) - { + if (status != FT_OK) { DWORD num_devices; LOG_ERROR("unable to open ftdi device: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY); - if (status == FT_OK) - { + if (status == FT_OK) { char **desc_array = malloc(sizeof(char *) - * (num_devices + 1)); + * (num_devices + 1)); unsigned int i; for (i = 0; i < num_devices; i++) @@ -400,10 +390,9 @@ static int usb_blaster_init(void) desc_array[num_devices] = NULL; status = FT_ListDevices(desc_array, &num_devices, - FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); + FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); - if (status == FT_OK) - { + if (status == FT_OK) { LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices); for (i = 0; i < num_devices; i++) LOG_ERROR("%i: %s", i, desc_array[i]); @@ -412,36 +401,30 @@ static int usb_blaster_init(void) for (i = 0; i < num_devices; i++) free(desc_array[i]); free(desc_array); - } - else - { + } else printf("ListDevices: NONE\n"); - } return ERROR_JTAG_INIT_FAILED; } status = FT_SetLatencyTimer(ftdih, 2); - if (status != FT_OK) - { + if (status != FT_OK) { LOG_ERROR("unable to set latency timer: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } status = FT_GetLatencyTimer(ftdih, &latency_timer); - if (status != FT_OK) - { + if (status != FT_OK) { LOG_ERROR("unable to get latency timer: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } LOG_DEBUG("current latency timer: %i", latency_timer); status = FT_SetBitMode(ftdih, 0x00, 0); - if (status != FT_OK) - { + if (status != FT_OK) { LOG_ERROR("unable to disable bit i/o mode: %s", - ftd2xx_status_string(status)); + ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } #elif BUILD_USB_BLASTER_LIBFTDI == 1 @@ -449,26 +432,22 @@ static int usb_blaster_init(void) return ERROR_JTAG_INIT_FAILED; /* context, vendor id, product id */ - if (ftdi_usb_open(&ftdic, usb_blaster_vid, usb_blaster_pid) < 0) - { + if (ftdi_usb_open(&ftdic, usb_blaster_vid, usb_blaster_pid) < 0) { LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str); return ERROR_JTAG_INIT_FAILED; } - if (ftdi_usb_reset(&ftdic) < 0) - { + if (ftdi_usb_reset(&ftdic) < 0) { LOG_ERROR("unable to reset ftdi device"); return ERROR_JTAG_INIT_FAILED; } - if (ftdi_set_latency_timer(&ftdic, 2) < 0) - { + if (ftdi_set_latency_timer(&ftdic, 2) < 0) { LOG_ERROR("unable to set latency timer"); return ERROR_JTAG_INIT_FAILED; } - if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) - { + if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) { LOG_ERROR("unable to get latency timer"); return ERROR_JTAG_INIT_FAILED; } @@ -481,14 +460,13 @@ static int usb_blaster_init(void) #if 0 #if BUILD_USB_BLASTER_FTD2XX == 1 - if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK) - { + status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX); + if (status != FT_OK) { LOG_ERROR("error purging ftd2xx device: %i", status); return ERROR_JTAG_INIT_FAILED; } #elif BUILD_USB_BLASTER_LIBFTDI == 1 - if (ftdi_usb_purge_buffers(&ftdic) < 0) - { + if (ftdi_usb_purge_buffers(&ftdic) < 0) { LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str); return ERROR_JTAG_INIT_FAILED; } @@ -500,7 +478,7 @@ static int usb_blaster_init(void) static int usb_blaster_quit(void) { - if(out_count > 0) + if (out_count > 0) usb_blaster_write_databuffer(out_buffer, out_count); #if BUILD_USB_BLASTER_FTD2XX == 1 @@ -521,25 +499,22 @@ COMMAND_HANDLER(usb_blaster_handle_device_desc_command) usb_blaster_device_desc = strdup(CMD_ARGV[0]); else LOG_ERROR("require exactly one argument to " - "usb_blaster_device_desc "); + "usb_blaster_device_desc "); return ERROR_OK; } COMMAND_HANDLER(usb_blaster_handle_vid_pid_command) { - if (CMD_ARGC > 2) - { + if (CMD_ARGC > 2) { LOG_WARNING("ignoring extra IDs in usb_blaster_vid_pid " - "(maximum is 1 pair)"); + "(maximum is 1 pair)"); CMD_ARGC = 2; } - if (CMD_ARGC == 2) - { + if (CMD_ARGC == 2) { COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], usb_blaster_vid); COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], usb_blaster_pid); - } - else + } else LOG_WARNING("incomplete usb_blaster_vid_pid configuration"); return ERROR_OK; @@ -547,9 +522,8 @@ COMMAND_HANDLER(usb_blaster_handle_vid_pid_command) COMMAND_HANDLER(usb_blaster_handle_pin_command) { - if (CMD_ARGC == 2) - { - const char * const pin_name = CMD_ARGV[0]; + if (CMD_ARGC == 2) { + const char *const pin_name = CMD_ARGV[0]; uint8_t mask; unsigned int state; @@ -557,34 +531,26 @@ COMMAND_HANDLER(usb_blaster_handle_pin_command) mask = NCE; else if (!strcmp(pin_name, "pin8")) mask = NCS; - else - { + else { LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"", - CMD_NAME); + CMD_NAME); return ERROR_COMMAND_SYNTAX_ERROR; } COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], state); - if (state == 0) - { + if (state == 0) { out_value &= ~mask; usb_blaster_addtowritebuffer(out_value, true); - } - else if (state == 1) - { + } else if (state == 1) { out_value |= mask; usb_blaster_addtowritebuffer(out_value, true); - } - else - { + } else { LOG_ERROR("%s: pin state must be 0 or 1", CMD_NAME); return ERROR_COMMAND_SYNTAX_ERROR; } return ERROR_OK; - } - else - { + } else { LOG_ERROR("%s takes exactly two arguments", CMD_NAME); return ERROR_COMMAND_SYNTAX_ERROR; } diff --git a/src/jtag/drivers/usb_common.c b/src/jtag/drivers/usb_common.c index 463f1af942..f6fe030195 100644 --- a/src/jtag/drivers/usb_common.c +++ b/src/jtag/drivers/usb_common.c @@ -26,13 +26,10 @@ static bool jtag_usb_match(struct usb_device *dev, const uint16_t vids[], const uint16_t pids[]) { - for (unsigned i = 0; vids[i] && pids[i]; i++) - { + for (unsigned i = 0; vids[i] && pids[i]; i++) { if (dev->descriptor.idVendor == vids[i] && dev->descriptor.idProduct == pids[i]) - { return true; - } } return false; } @@ -44,10 +41,8 @@ int jtag_usb_open(const uint16_t vids[], const uint16_t pids[], usb_find_devices(); struct usb_bus *busses = usb_get_busses(); - for (struct usb_bus *bus = busses; bus; bus = bus->next) - { - for (struct usb_device *dev = bus->devices; dev; dev = dev->next) - { + for (struct usb_bus *bus = busses; bus; bus = bus->next) { + for (struct usb_device *dev = bus->devices; dev; dev = dev->next) { if (!jtag_usb_match(dev, vids, pids)) continue; diff --git a/src/jtag/drivers/usb_common.h b/src/jtag/drivers/usb_common.h index 32d3548d76..468acf9567 100644 --- a/src/jtag/drivers/usb_common.h +++ b/src/jtag/drivers/usb_common.h @@ -27,4 +27,4 @@ int jtag_usb_open(const uint16_t vids[], const uint16_t pids[], struct usb_dev_handle **out); -#endif // JTAG_USB_COMMON_H +#endif /* JTAG_USB_COMMON_H */ diff --git a/src/jtag/drivers/usbprog.c b/src/jtag/drivers/usbprog.c index 7d10d0f407..e90d56931e 100644 --- a/src/jtag/drivers/usbprog.c +++ b/src/jtag/drivers/usbprog.c @@ -38,15 +38,14 @@ #include #include "usb_common.h" - #define VID 0x1781 #define PID 0x0c63 /* Pins at usbprog */ -#define TDO_BIT 0 -#define TDI_BIT 3 -#define TCK_BIT 2 -#define TMS_BIT 1 +#define TDO_BIT 0 +#define TDI_BIT 3 +#define TCK_BIT 2 +#define TMS_BIT 1 static void usbprog_end_state(tap_state_t state); static void usbprog_state_move(void); @@ -54,33 +53,32 @@ static void usbprog_path_move(struct pathmove_command *cmd); static void usbprog_runtest(int num_cycles); static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size); -#define UNKNOWN_COMMAND 0x00 -#define PORT_DIRECTION 0x01 -#define PORT_SET 0x02 -#define PORT_GET 0x03 -#define PORT_SETBIT 0x04 -#define PORT_GETBIT 0x05 -#define WRITE_TDI 0x06 -#define READ_TDO 0x07 -#define WRITE_AND_READ 0x08 -#define WRITE_TMS 0x09 -#define WRITE_TMS_CHAIN 0x0A - -struct usbprog_jtag -{ - struct usb_dev_handle* usb_handle; +#define UNKNOWN_COMMAND 0x00 +#define PORT_DIRECTION 0x01 +#define PORT_SET 0x02 +#define PORT_GET 0x03 +#define PORT_SETBIT 0x04 +#define PORT_GETBIT 0x05 +#define WRITE_TDI 0x06 +#define READ_TDO 0x07 +#define WRITE_AND_READ 0x08 +#define WRITE_TMS 0x09 +#define WRITE_TMS_CHAIN 0x0A + +struct usbprog_jtag { + struct usb_dev_handle *usb_handle; }; -static struct usbprog_jtag * usbprog_jtag_handle; +static struct usbprog_jtag *usbprog_jtag_handle; -static struct usbprog_jtag* usbprog_jtag_open(void); -//static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag); +static struct usbprog_jtag *usbprog_jtag_open(void); +/* static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag); */ static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag); static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen); -static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size); -static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size); -static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size); +static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffer, int size); +static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buffer, int size); +static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char *buffer, int size); static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan); static char tms_chain[64]; @@ -93,10 +91,10 @@ static void usbprog_write(int tck, int tms, int tdi); static void usbprog_reset(int trst, int srst); static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction); -static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value); -//static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag); -static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value); -//static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit); +static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned char value); +/* static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag); */ +static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value); +/* static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit); */ static int usbprog_speed(int speed) { @@ -105,68 +103,69 @@ static int usbprog_speed(int speed) static int usbprog_execute_queue(void) { - struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ + struct jtag_command *cmd = jtag_command_queue; /* currently processed command */ int scan_size; enum scan_type type; uint8_t *buffer; - while (cmd) - { - switch (cmd->type) - { - case JTAG_RESET: + while (cmd) { + switch (cmd->type) { + case JTAG_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst); + LOG_DEBUG("reset trst: %i srst %i", + cmd->cmd.reset->trst, + cmd->cmd.reset->srst); #endif - if (cmd->cmd.reset->trst == 1) - { - tap_set_state(TAP_RESET); - } - usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); - break; - case JTAG_RUNTEST: + if (cmd->cmd.reset->trst == 1) + tap_set_state(TAP_RESET); + usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst); + break; + case JTAG_RUNTEST: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state); + LOG_DEBUG("runtest %i cycles, end in %i", + cmd->cmd.runtest->num_cycles, + cmd->cmd.runtest->end_state); #endif - usbprog_end_state(cmd->cmd.runtest->end_state); - usbprog_runtest(cmd->cmd.runtest->num_cycles); - break; - case JTAG_TLR_RESET: + usbprog_end_state(cmd->cmd.runtest->end_state); + usbprog_runtest(cmd->cmd.runtest->num_cycles); + break; + case JTAG_TLR_RESET: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state); + LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state); #endif - usbprog_end_state(cmd->cmd.statemove->end_state); - usbprog_state_move(); - break; - case JTAG_PATHMOVE: + usbprog_end_state(cmd->cmd.statemove->end_state); + usbprog_state_move(); + break; + case JTAG_PATHMOVE: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, - cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); + LOG_DEBUG("pathmove: %i states, end in %i", + cmd->cmd.pathmove->num_states, + cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]); #endif - usbprog_path_move(cmd->cmd.pathmove); - break; - case JTAG_SCAN: + usbprog_path_move(cmd->cmd.pathmove); + break; + case JTAG_SCAN: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state); + LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state); #endif - usbprog_end_state(cmd->cmd.scan->end_state); - scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer); - type = jtag_scan_type(cmd->cmd.scan); - usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size); - if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK) - return ERROR_JTAG_QUEUE_FAILED; - if (buffer) - free(buffer); - break; - case JTAG_SLEEP: + usbprog_end_state(cmd->cmd.scan->end_state); + scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer); + type = jtag_scan_type(cmd->cmd.scan); + usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size); + if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK) + return ERROR_JTAG_QUEUE_FAILED; + if (buffer) + free(buffer); + break; + case JTAG_SLEEP: #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("sleep %i", cmd->cmd.sleep->us); + LOG_DEBUG("sleep %i", cmd->cmd.sleep->us); #endif - jtag_sleep(cmd->cmd.sleep->us); - break; - default: - LOG_ERROR("BUG: unknown JTAG command type encountered"); - exit(-1); + jtag_sleep(cmd->cmd.sleep->us); + break; + default: + LOG_ERROR("BUG: unknown JTAG command type encountered"); + exit(-1); } cmd = cmd->next; @@ -180,8 +179,7 @@ static int usbprog_init(void) usbprog_jtag_handle = usbprog_jtag_open(); tms_chain_index = 0; - if (usbprog_jtag_handle == 0) - { + if (usbprog_jtag_handle == 0) { LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions."); return ERROR_JTAG_INIT_FAILED; } @@ -205,8 +203,7 @@ static void usbprog_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); - else - { + else { LOG_ERROR("BUG: %i is not a valid end state", state); exit(-1); } @@ -231,23 +228,20 @@ static void usbprog_path_move(struct pathmove_command *cmd) usbprog_jtag_tms_send(usbprog_jtag_handle); state_count = 0; - while (num_states) - { - if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) - { + while (num_states) { + if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) { /* LOG_INFO("1"); */ usbprog_write(0, 0, 0); usbprog_write(1, 0, 0); - } - else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count]) - { + } else if (tap_state_transition(tap_get_state(), + true) == cmd->path[state_count]) { /* LOG_INFO("2"); */ usbprog_write(0, 1, 0); usbprog_write(1, 1, 0); - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count])); + } else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(tap_get_state()), + tap_state_name(cmd->path[state_count])); exit(-1); } @@ -264,32 +258,28 @@ static void usbprog_runtest(int num_cycles) int i; /* only do a state_move when we're not already in IDLE */ - if (tap_get_state() != TAP_IDLE) - { + if (tap_get_state() != TAP_IDLE) { usbprog_end_state(TAP_IDLE); usbprog_state_move(); } /* execute num_cycles */ - if (num_cycles > 0) - { + if (num_cycles > 0) { usbprog_jtag_tms_send(usbprog_jtag_handle); usbprog_write(0, 0, 0); - } - else - { + } else { usbprog_jtag_tms_send(usbprog_jtag_handle); /* LOG_INFO("NUM CYCLES %i",num_cycles); */ } - for (i = 0; i < num_cycles; i++) - { + for (i = 0; i < num_cycles; i++) { usbprog_write(1, 0, 0); usbprog_write(0, 0, 0); } #ifdef _DEBUG_JTAG_IO_ - LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state())); + LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name( + tap_get_state()), tap_state_name(tap_get_end_state())); #endif /* finish in end_state */ @@ -317,14 +307,20 @@ static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int usbprog_jtag_tms_send(usbprog_jtag_handle); - void (*f)(struct usbprog_jtag *usbprog_jtag, char * buffer_local, int size); + void (*f)(struct usbprog_jtag *usbprog_jtag, char *buffer_local, int size); switch (type) { - case SCAN_OUT: f = &usbprog_jtag_write_tdi; break; - case SCAN_IN: f = &usbprog_jtag_read_tdo; break; - case SCAN_IO: f = &usbprog_jtag_write_and_read; break; - default: - LOG_ERROR("unknown scan type: %i", type); - exit(-1); + case SCAN_OUT: + f = &usbprog_jtag_write_tdi; + break; + case SCAN_IN: + f = &usbprog_jtag_read_tdo; + break; + case SCAN_IO: + f = &usbprog_jtag_write_and_read; + break; + default: + LOG_ERROR("unknown scan type: %i", type); + exit(-1); } f(usbprog_jtag_handle, (char *)buffer, scan_size); @@ -351,7 +347,7 @@ static void usbprog_write(int tck, int tms, int tdi) if (tck) output_value |= (1 << TCK_BIT); - usbprog_jtag_write_slice(usbprog_jtag_handle,output_value); + usbprog_jtag_write_slice(usbprog_jtag_handle, output_value); } /* (1) assert or (0) deassert reset lines */ @@ -374,7 +370,7 @@ static void usbprog_reset(int trst, int srst) struct usb_bus *busses; -struct usbprog_jtag* usbprog_jtag_open(void) +struct usbprog_jtag *usbprog_jtag_open(void) { usb_set_debug(10); usb_init(); @@ -405,20 +401,18 @@ static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag) static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen) { - int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg,msglen, 100); - if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \ - (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9)) + int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg, msglen, 100); + if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \ + (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9)) return 1; - if (res == msglen) - { + if (res == msglen) { /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */ - res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100); + res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100); if (res > 0) return (unsigned char)msg[1]; else return -1; - } - else + } else return -1; return 0; } @@ -428,7 +422,7 @@ static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag) usbprog_jtag_set_direction(usbprog_jtag, 0xFE); } -static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size) +static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char *buffer, int size) { char tmp[64]; /* fastes packet size for usb controller */ int send_bits, bufindex = 0, fillindex = 0, i, loops; @@ -436,16 +430,12 @@ static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char char swap; /* 61 byte can be transfered (488 bit) */ - while (size > 0) - { - if (size > 488) - { + while (size > 0) { + if (size > 488) { send_bits = 488; size = size - 488; loops = 61; - } - else - { + } else { send_bits = size; loops = size / 8; loops++; @@ -455,34 +445,30 @@ static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char tmp[1] = (char)(send_bits >> 8); /* high */ tmp[2] = (char)(send_bits); /* low */ - for (i = 0; i < loops; i++) - { + for (i = 0; i < loops; i++) { tmp[3 + i] = buffer[bufindex]; bufindex++; } - if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64) - { + if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64) { /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */ usleep(1); int timeout = 0; - while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1) - { + while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1) { timeout++; if (timeout > 10) break; } - for (i = 0; i < loops; i++) - { - swap = tmp[3 + i]; + for (i = 0; i < loops; i++) { + swap = tmp[3 + i]; buffer[fillindex++] = swap; } } } } -static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size) +static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffer, int size) { char tmp[64]; /* fastes packet size for usb controller */ int send_bits, fillindex = 0, i, loops; @@ -490,16 +476,12 @@ static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buff char swap; /* 61 byte can be transfered (488 bit) */ - while (size > 0) - { - if (size > 488) - { + while (size > 0) { + if (size > 488) { send_bits = 488; size = size - 488; loops = 61; - } - else - { + } else { send_bits = size; loops = size / 8; loops++; @@ -514,37 +496,31 @@ static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buff /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */ int timeout = 0; usleep(1); - while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1) - { + while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1) { timeout++; if (timeout > 10) break; } - for (i = 0; i < loops; i++) - { + for (i = 0; i < loops; i++) { swap = tmp[3 + i]; buffer[fillindex++] = swap; } } } -static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size) +static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buffer, int size) { char tmp[64]; /* fastes packet size for usb controller */ int send_bits, bufindex = 0, i, loops; /* 61 byte can be transfered (488 bit) */ - while (size > 0) - { - if (size > 488) - { + while (size > 0) { + if (size > 488) { send_bits = 488; size = size - 488; loops = 61; - } - else - { + } else { send_bits = size; loops = size/8; /* if (loops == 0) */ @@ -555,8 +531,7 @@ static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buf tmp[1] = (char)(send_bits >> 8); /* high */ tmp[2] = (char)(send_bits); /* low */ - for (i = 0; i < loops; i++) - { + for (i = 0; i < loops; i++) { tmp[3 + i] = buffer[bufindex]; bufindex++; } @@ -577,7 +552,7 @@ static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsign usbprog_jtag_message(usbprog_jtag, tmp, 2); } -static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value) +static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned char value) { char tmp[2]; tmp[0] = PORT_SET; @@ -595,7 +570,7 @@ static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag) } #endif -static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value) +static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value) { char tmp[3]; tmp[0] = PORT_SETBIT; @@ -631,8 +606,7 @@ static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag) { int i; /* LOG_INFO("TMS SEND"); */ - if (tms_chain_index > 0) - { + if (tms_chain_index > 0) { char tmp[tms_chain_index + 2]; tmp[0] = WRITE_TMS_CHAIN; tmp[1] = (char)(tms_chain_index); diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtogpio.c b/src/jtag/drivers/versaloon/usbtoxxx/usbtogpio.c index 5be515f64e..169ca36433 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtogpio.c +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtogpio.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -39,62 +40,58 @@ RESULT usbtogpio_fini(uint8_t interface_index) } RESULT usbtogpio_config(uint8_t interface_index, uint32_t mask, - uint32_t dir_mask, uint32_t pull_en_mask, - uint32_t input_pull_mask) + uint32_t dir_mask, uint32_t pull_en_mask, + uint32_t input_pull_mask) { uint8_t conf[8]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + dir_mask &= mask; SET_LE_U16(&conf[0], mask); SET_LE_U16(&conf[2], dir_mask); SET_LE_U16(&conf[4], pull_en_mask); SET_LE_U16(&conf[6], input_pull_mask); - + return usbtoxxx_conf_command(USB_TO_GPIO, interface_index, conf, - sizeof(conf)); + sizeof(conf)); } RESULT usbtogpio_in(uint8_t interface_index, uint32_t mask, uint32_t *value) { uint8_t buf[2]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + SET_LE_U16(&buf[0], mask); - + return usbtoxxx_in_command(USB_TO_GPIO, interface_index, buf, 2, 2, - (uint8_t*)value, 0, 2, 0); + (uint8_t *)value, 0, 2, 0); } RESULT usbtogpio_out(uint8_t interface_index, uint32_t mask, uint32_t value) { uint8_t buf[4]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + SET_LE_U16(&buf[0], mask); SET_LE_U16(&buf[2], value); - + return usbtoxxx_out_command(USB_TO_GPIO, interface_index, buf, 4, 0); } - diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtojtagraw.c b/src/jtag/drivers/versaloon/usbtoxxx/usbtojtagraw.c index 11e8064d56..db0b5a13f8 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtojtagraw.c +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtojtagraw.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -41,44 +42,39 @@ RESULT usbtojtagraw_fini(uint8_t interface_index) RESULT usbtojtagraw_config(uint8_t interface_index, uint32_t kHz) { uint8_t cfg_buf[4]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + SET_LE_U32(&cfg_buf[0], kHz); - + return usbtoxxx_conf_command(USB_TO_JTAG_RAW, interface_index, cfg_buf, 4); } RESULT usbtojtagraw_execute(uint8_t interface_index, uint8_t *tdi, - uint8_t *tms, uint8_t *tdo, uint32_t bitlen) + uint8_t *tms, uint8_t *tdo, uint32_t bitlen) { uint16_t bytelen; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + if (bitlen > 8 * 0xFFFF) - { return ERROR_FAIL; - } bytelen = (uint16_t)((bitlen + 7) >> 3); - + SET_LE_U32(&versaloon_cmd_buf[0], bitlen); memcpy(versaloon_cmd_buf + 4, tdi, bytelen); memcpy(versaloon_cmd_buf + 4 + bytelen, tms, bytelen); - + return usbtoxxx_inout_command(USB_TO_JTAG_RAW, interface_index, - versaloon_cmd_buf, 4 + bytelen * 2, bytelen, tdo, 0, bytelen, 0); + versaloon_cmd_buf, 4 + bytelen * 2, bytelen, tdo, 0, bytelen, 0); } - diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtopwr.c b/src/jtag/drivers/versaloon/usbtoxxx/usbtopwr.c index 13f7f153b4..78f74a0a90 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtopwr.c +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtopwr.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -41,27 +42,24 @@ RESULT usbtopwr_fini(uint8_t interface_index) RESULT usbtopwr_config(uint8_t interface_index) { #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + return usbtoxxx_conf_command(USB_TO_POWER, interface_index, NULL, 0); } RESULT usbtopwr_output(uint8_t interface_index, uint16_t mV) { #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + return usbtoxxx_out_command(USB_TO_POWER, interface_index, (uint8_t *)&mV, - 2, 0); + 2, 0); } - diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtoswd.c b/src/jtag/drivers/versaloon/usbtoxxx/usbtoswd.c index 93333f088b..1b0733e924 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtoswd.c +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtoswd.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -32,14 +33,12 @@ RESULT usbtoswd_callback(void *p, uint8_t *src, uint8_t *processed) { struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p; - + processed = processed; - + if (pending->extra_data != NULL) - { *((uint8_t *)pending->extra_data) = src[0]; - } - + return ERROR_OK; } @@ -54,77 +53,73 @@ RESULT usbtoswd_fini(uint8_t interface_index) } RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry, - uint16_t dly) + uint16_t dly) { uint8_t cfg_buf[5]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + cfg_buf[0] = trn; SET_LE_U16(&cfg_buf[1], retry); SET_LE_U16(&cfg_buf[3], dly); - + return usbtoxxx_conf_command(USB_TO_SWD, interface_index, cfg_buf, 5); } RESULT usbtoswd_seqout(uint8_t interface_index, uint8_t *data, uint16_t bitlen) { uint16_t bytelen = (bitlen + 7) >> 3; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + SET_LE_U16(&versaloon_cmd_buf[0], bitlen); memcpy(versaloon_cmd_buf + 2, data, bytelen); - + return usbtoxxx_out_command(USB_TO_SWD, interface_index, - versaloon_cmd_buf, bytelen + 2, 0); + versaloon_cmd_buf, bytelen + 2, 0); } RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen) { uint16_t bytelen = (bitlen + 7) >> 3; uint8_t buff[2]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + SET_LE_U16(&buff[0], bitlen); - + return usbtoxxx_in_command(USB_TO_SWD, interface_index, buff, 2, bytelen, - data, 0, bytelen, 0); + data, 0, bytelen, 0); } RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request, - uint32_t *data, uint8_t *ack) + uint32_t *data, uint8_t *ack) { uint8_t parity; uint8_t buff[5]; - + #if PARAM_CHECK - if (interface_index > 7) - { + if (interface_index > 7) { LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index); return ERROR_FAIL; } #endif - + parity = (request >> 1) & 1; parity += (request >> 2) & 1; parity += (request >> 3) & 1; @@ -132,27 +127,19 @@ RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request, parity &= 1; buff[0] = (request | 0x81 | (parity << 5)) & ~0x40; if (data != NULL) - { SET_LE_U32(&buff[1], *data); - } else - { memset(buff + 1, 0, 4); - } - + versaloon_set_extra_data(ack); versaloon_set_callback(usbtoswd_callback); - if (request & 0x04) - { - // read + if (request & 0x04) { + /* read */ return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5, - (uint8_t *)data, 1, 4, 0); - } - else - { - // write + (uint8_t *)data, 1, 4, 0); + } else { + /* write */ return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5, - NULL, 0, 0, 0); + NULL, 0, 0, 0); } } - diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.c b/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.c index f92f38c9da..cca8014615 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.c +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -28,40 +29,39 @@ #include "usbtoxxx.h" #include "usbtoxxx_internal.h" -#define N_A "n/a" -const char* types_name[96] = -{ -"usbtousart", "usbtospi", "usbtoi2c", "usbtogpio", "usbtocan", "usbtopwm", - "usbtoadc", "usbtodac", -"usbtomicrowire", "usbtoswim", "usbtodusi", N_A, N_A, N_A, "usbtopower", "usbtodelay", -N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, -N_A, N_A, N_A, N_A, N_A, N_A, N_A, -"usbtojtagll", "usbtojtaghl", "usbtoissp", "usbtoc2", "usbtosbw", - "usbtolpcicp", "usbtoswd", "usbtojtagraw", -"usbtobdm", N_A, N_A, N_A, N_A, N_A, N_A, N_A, -N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, -"usbtomsp430jtag", N_A, N_A, N_A, N_A, N_A, N_A, N_A, -"usbtopower", "usbtodelay", "usbtopoll", N_A, N_A, N_A, N_A, N_A, -N_A, N_A, N_A, N_A, N_A, N_A, N_A, "usbtoall" +#define N_A "n/a" + +const char *types_name[96] = { + "usbtousart", "usbtospi", "usbtoi2c", "usbtogpio", "usbtocan", "usbtopwm", + "usbtoadc", "usbtodac", + "usbtomicrowire", "usbtoswim", "usbtodusi", N_A, N_A, N_A, "usbtopower", "usbtodelay", + N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, + N_A, N_A, N_A, N_A, N_A, N_A, N_A, + "usbtojtagll", "usbtojtaghl", "usbtoissp", "usbtoc2", "usbtosbw", + "usbtolpcicp", "usbtoswd", "usbtojtagraw", + "usbtobdm", N_A, N_A, N_A, N_A, N_A, N_A, N_A, + N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, + "usbtomsp430jtag", N_A, N_A, N_A, N_A, N_A, N_A, N_A, + "usbtopower", "usbtodelay", "usbtopoll", N_A, N_A, N_A, N_A, N_A, + N_A, N_A, N_A, N_A, N_A, N_A, N_A, "usbtoall" }; uint8_t usbtoxxx_abilities[USB_TO_XXX_ABILITIES_LEN]; #define usbtoxxx_get_type_name(type) \ - types_name[((type) - VERSALOON_USB_TO_XXX_CMD_START) \ - % (sizeof(types_name) / sizeof(types_name[0]))] + types_name[((type) - VERSALOON_USB_TO_XXX_CMD_START) \ + % (sizeof(types_name) / sizeof(types_name[0]))] -static uint8_t type_pre = 0; -static uint16_t usbtoxxx_buffer_index = 0; -static uint16_t usbtoxxx_current_cmd_index = 0; -static uint8_t *usbtoxxx_buffer = NULL; +static uint8_t type_pre; +static uint16_t usbtoxxx_buffer_index; +static uint16_t usbtoxxx_current_cmd_index; +static uint8_t *usbtoxxx_buffer; -uint16_t collect_index = 0; +uint16_t collect_index; uint8_t collect_cmd; -static uint8_t poll_nesting = 0; +static uint8_t poll_nesting; -struct usbtoxxx_context_t -{ +struct usbtoxxx_context_t { uint8_t type_pre; uint8_t *usbtoxxx_buffer; uint16_t usbtoxxx_current_cmd_index; @@ -90,93 +90,78 @@ static void usbtoxxx_pop_context(struct usbtoxxx_context_t *c) RESULT usbtoxxx_validate_current_command_type(void) { - if (type_pre > 0) - { - // not the first command - if (NULL == usbtoxxx_buffer) - { + if (type_pre > 0) { + /* not the first command */ + if (NULL == usbtoxxx_buffer) { LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(usbtoxxx_buffer)); return ERRCODE_INVALID_BUFFER; } - + usbtoxxx_buffer[0] = type_pre; SET_LE_U16(&usbtoxxx_buffer[1], usbtoxxx_current_cmd_index); - + usbtoxxx_buffer_index += usbtoxxx_current_cmd_index; - } - else - { - // first command + } else { + /* first command */ usbtoxxx_buffer_index = 3; } - - // prepare for next command + + /* prepare for next command */ usbtoxxx_current_cmd_index = 3; usbtoxxx_buffer = versaloon_buf + usbtoxxx_buffer_index; - + collect_index = 0; collect_cmd = 0; - + return ERROR_OK; } - - RESULT usbtoxxx_execute_command(void) { uint16_t i; uint16_t inlen; RESULT result = ERROR_OK; - - if (poll_nesting) - { + + if (poll_nesting) { LOG_BUG(ERRMSG_INVALID_USAGE, "USB_TO_POLL"); versaloon_free_want_pos(); return ERROR_FAIL; } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); versaloon_free_want_pos(); return ERRCODE_FAILURE_OPERATION; } - if (3 == usbtoxxx_buffer_index) - { + if (3 == usbtoxxx_buffer_index) { versaloon_free_want_pos(); return ERROR_OK; } - + versaloon_buf[0] = USB_TO_ALL; SET_LE_U16(&versaloon_buf[1], usbtoxxx_buffer_index); - - if (ERROR_OK != versaloon_send_command(usbtoxxx_buffer_index, &inlen)) - { + + if (ERROR_OK != versaloon_send_command(usbtoxxx_buffer_index, &inlen)) { versaloon_free_want_pos(); return ERROR_FAIL; } - - // process return data + + /* process return data */ usbtoxxx_buffer_index = 0; - for (i = 0; i < versaloon_pending_idx; i++) - { - // check result + for (i = 0; i < versaloon_pending_idx; i++) { + /* check result */ if ((0 == i) || !((versaloon_pending[i].collect) - && (versaloon_pending[i - 1].collect) - && (versaloon_pending[i].cmd - == versaloon_pending[i - 1].cmd))) - { + && (versaloon_pending[i - 1].collect) + && (versaloon_pending[i].cmd + == versaloon_pending[i - 1].cmd))) { if (USB_TO_XXX_CMD_NOT_SUPPORT - == versaloon_buf[usbtoxxx_buffer_index]) - { + == versaloon_buf[usbtoxxx_buffer_index]) { LOG_ERROR(ERRMSG_NOT_SUPPORT_BY, - usbtoxxx_get_type_name(versaloon_pending[i].type), - "current dongle"); + usbtoxxx_get_type_name(versaloon_pending[i].type), + "current dongle"); result = ERROR_FAIL; break; - } - else if (USB_TO_XXX_OK != versaloon_buf[usbtoxxx_buffer_index]) - { + } else if (USB_TO_XXX_OK != versaloon_buf[usbtoxxx_buffer_index]) { LOG_ERROR("%s command 0x%02x failed with 0x%02x", usbtoxxx_get_type_name(versaloon_pending[i].type), versaloon_pending[i].cmd, @@ -186,28 +171,25 @@ RESULT usbtoxxx_execute_command(void) } usbtoxxx_buffer_index++; } - - // get result data - if (versaloon_pending[i].pos != NULL) - { + + /* get result data */ + if (versaloon_pending[i].pos != NULL) { uint8_t processed = 0; - - if (versaloon_pending[i].callback != NULL) - { + + if (versaloon_pending[i].callback != NULL) { versaloon_pending[i].callback(&versaloon_pending[i], - versaloon_buf + usbtoxxx_buffer_index, &processed); + versaloon_buf + usbtoxxx_buffer_index, &processed); } - if (!processed) - { + if (!processed) { struct versaloon_want_pos_t *tmp; - + tmp = versaloon_pending[i].pos; - while (tmp != NULL) - { - if ((tmp->buff != NULL) && (tmp->size > 0)) - { - memcpy(tmp->buff, versaloon_buf + usbtoxxx_buffer_index - + tmp->offset, tmp->size); + while (tmp != NULL) { + if ((tmp->buff != NULL) && (tmp->size > 0)) { + memcpy(tmp->buff, + versaloon_buf + usbtoxxx_buffer_index + + tmp->offset, + tmp->size); } struct versaloon_want_pos_t *free_tmp; free_tmp = tmp; @@ -216,53 +198,44 @@ RESULT usbtoxxx_execute_command(void) } versaloon_pending[i].pos = NULL; } - } - else if ((versaloon_pending[i].want_data_size > 0) - && (versaloon_pending[i].data_buffer != NULL)) - { + } else if ((versaloon_pending[i].want_data_size > 0) + && (versaloon_pending[i].data_buffer != NULL)) { uint8_t processed = 0; - - if (versaloon_pending[i].callback != NULL) - { + + if (versaloon_pending[i].callback != NULL) { versaloon_pending[i].callback(&versaloon_pending[i], - versaloon_buf + usbtoxxx_buffer_index, &processed); + versaloon_buf + usbtoxxx_buffer_index, &processed); } - if (!processed) - { + if (!processed) { memcpy(versaloon_pending[i].data_buffer, - versaloon_buf + usbtoxxx_buffer_index - + versaloon_pending[i].want_data_pos, - versaloon_pending[i].want_data_size); + versaloon_buf + usbtoxxx_buffer_index + + versaloon_pending[i].want_data_pos, + versaloon_pending[i].want_data_size); } } usbtoxxx_buffer_index += versaloon_pending[i].actual_data_size; - if (usbtoxxx_buffer_index > inlen) - { + if (usbtoxxx_buffer_index > inlen) { LOG_BUG("%s command 0x%02x process error", - usbtoxxx_get_type_name(versaloon_pending[i].type), - versaloon_pending[i].cmd); + usbtoxxx_get_type_name(versaloon_pending[i].type), + versaloon_pending[i].cmd); result = ERROR_FAIL; break; } } - - // data is not the right size - if (inlen != usbtoxxx_buffer_index) - { + + /* data is not the right size */ + if (inlen != usbtoxxx_buffer_index) { LOG_ERROR(ERRMSG_INVALID_TARGET, "length of return data"); result = ERROR_FAIL; } - + if (versaloon_pending_idx > 0) - { versaloon_pending_idx = 0; - } - else - { - // no receive data, avoid collision + else { + /* no receive data, avoid collision */ sleep_ms(10); } - + type_pre = 0; collect_cmd = 0; collect_index = 0; @@ -273,12 +246,10 @@ RESULT usbtoxxx_execute_command(void) RESULT usbtoxxx_init(void) { versaloon_pending_idx = 0; - + if ((ERROR_OK != usbtoinfo_get_abilities(usbtoxxx_abilities)) || - (ERROR_OK != usbtoxxx_execute_command())) - { + (ERROR_OK != usbtoxxx_execute_command())) return ERROR_FAIL; - } LOG_INFO("USB_TO_XXX abilities: 0x%08X:0x%08X:0x%08X", GET_LE_U32(&usbtoxxx_abilities[0]), GET_LE_U32(&usbtoxxx_abilities[4]), @@ -296,61 +267,51 @@ RESULT usbtoxxx_fini(void) bool usbtoxxx_interface_supported(uint8_t cmd) { if ((cmd < VERSALOON_USB_TO_XXX_CMD_START) || - (cmd > VERSALOON_USB_TO_XXX_CMD_END)) - { + (cmd > VERSALOON_USB_TO_XXX_CMD_END)) return false; - } - + cmd -= VERSALOON_USB_TO_XXX_CMD_START; return (usbtoxxx_abilities[cmd / 8] & (1 << (cmd % 8))) > 0; } - - RESULT usbtoxxx_ensure_buffer_size(uint16_t cmdlen) { - // check free space, commit if not enough + /* check free space, commit if not enough */ if (((usbtoxxx_buffer_index + usbtoxxx_current_cmd_index + cmdlen) >= versaloon_buf_size) - || (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER)) - { + || (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER)) { struct usbtoxxx_context_t context_tmp; uint8_t poll_nesting_tmp = 0; - + memset(&context_tmp, 0, sizeof(context_tmp)); - if (poll_nesting) - { - if (0 == poll_context.type_pre) - { + if (poll_nesting) { + if (0 == poll_context.type_pre) { LOG_BUG("USB_TO_POLL toooooo long"); return ERROR_OK; } - + usbtoxxx_save_context(&context_tmp); usbtoxxx_pop_context(&poll_context); poll_nesting_tmp = poll_nesting; poll_nesting = 0; } - + if (usbtoxxx_execute_command() != ERROR_OK) - { return ERROR_FAIL; - } - - if (poll_nesting_tmp) - { + + if (poll_nesting_tmp) { uint16_t newlen, oldlen; - + newlen = context_tmp.versaloon_pending_idx - - poll_context.versaloon_pending_idx; + - poll_context.versaloon_pending_idx; memcpy(&versaloon_pending[0], - &versaloon_pending[poll_context.versaloon_pending_idx], - sizeof(versaloon_pending[0]) * newlen); + &versaloon_pending[poll_context.versaloon_pending_idx], + sizeof(versaloon_pending[0]) * newlen); context_tmp.versaloon_pending_idx = newlen; oldlen = poll_context.usbtoxxx_buffer_index - + poll_context.usbtoxxx_current_cmd_index; + + poll_context.usbtoxxx_current_cmd_index; newlen = context_tmp.usbtoxxx_buffer_index - + context_tmp.usbtoxxx_current_cmd_index; + + context_tmp.usbtoxxx_current_cmd_index; memcpy(versaloon_buf + 3, versaloon_buf + oldlen, newlen - oldlen); oldlen -= 3; context_tmp.usbtoxxx_buffer -= oldlen; @@ -363,250 +324,198 @@ RESULT usbtoxxx_ensure_buffer_size(uint16_t cmdlen) } RESULT usbtoxxx_add_command(uint8_t type, uint8_t cmd, uint8_t *cmdbuf, - uint16_t cmdlen, uint16_t retlen, uint8_t *wantbuf, - uint16_t wantpos, uint16_t wantlen, uint8_t collect) + uint16_t cmdlen, uint16_t retlen, uint8_t *wantbuf, + uint16_t wantpos, uint16_t wantlen, uint8_t collect) { uint16_t len_tmp; - - // 3 more bytes by usbtoxxx_validate_current_command_type - // 3 more bytes when ((0 == collect_index) || (collect_cmd != cmd)) + + /* 3 more bytes by usbtoxxx_validate_current_command_type */ + /* 3 more bytes when ((0 == collect_index) || (collect_cmd != cmd)) */ if (ERROR_OK != usbtoxxx_ensure_buffer_size(cmdlen + 6)) - { return ERROR_FAIL; - } - - if ((type_pre != type) || (NULL == usbtoxxx_buffer)) - { - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if ((type_pre != type) || (NULL == usbtoxxx_buffer)) { + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } type_pre = type; } - - if ((0 == collect_index) || (collect_cmd != cmd)) - { + + if ((0 == collect_index) || (collect_cmd != cmd)) { usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = cmd; - - if (collect) - { + + if (collect) { collect_index = usbtoxxx_current_cmd_index; collect_cmd = cmd; - } - else - { + } else { collect_index = 0; collect_cmd = 0; } SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], cmdlen); usbtoxxx_current_cmd_index += 2; - } - else - { + } else { len_tmp = GET_LE_U16(&usbtoxxx_buffer[collect_index]) + cmdlen; SET_LE_U16(&usbtoxxx_buffer[collect_index], len_tmp); } - - if (cmdbuf != NULL) - { + + if (cmdbuf != NULL) { memcpy(usbtoxxx_buffer + usbtoxxx_current_cmd_index, cmdbuf, cmdlen); usbtoxxx_current_cmd_index += cmdlen; } - + return versaloon_add_pending(type, cmd, retlen, wantpos, wantlen, - wantbuf, collect); + wantbuf, collect); } - - - - RESULT usbtoinfo_get_abilities(uint8_t abilities[USB_TO_XXX_ABILITIES_LEN]) { if (ERROR_OK != usbtoxxx_ensure_buffer_size(3)) - { return ERROR_FAIL; - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } type_pre = USB_TO_INFO; - + return versaloon_add_pending(USB_TO_INFO, 0, USB_TO_XXX_ABILITIES_LEN, 0, - USB_TO_XXX_ABILITIES_LEN, abilities, 0); + USB_TO_XXX_ABILITIES_LEN, abilities, 0); } - - - RESULT usbtopoll_start(uint16_t retry_cnt, uint16_t interval_us) { if (ERROR_OK != usbtoxxx_ensure_buffer_size(3 + 5)) - { return ERROR_FAIL; - } if (!poll_nesting) - { usbtoxxx_save_context(&poll_context); - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } poll_nesting++; type_pre = USB_TO_POLL; - + usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = USB_TO_POLL_START; SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], retry_cnt); usbtoxxx_current_cmd_index += 2; SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], interval_us); usbtoxxx_current_cmd_index += 2; - + return versaloon_add_pending(USB_TO_POLL, 0, 0, 0, 0, NULL, 0); } RESULT usbtopoll_end(void) { - if (!poll_nesting) - { + if (!poll_nesting) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "check poll nesting"); return ERRCODE_FAILURE_OPERATION; } if (ERROR_OK != usbtoxxx_ensure_buffer_size(3 + 1)) - { return ERROR_FAIL; - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } - + poll_nesting--; type_pre = USB_TO_POLL; - + usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = USB_TO_POLL_END; - + return versaloon_add_pending(USB_TO_POLL, 0, 0, 0, 0, NULL, 0); } RESULT usbtopoll_checkok(uint8_t equ, uint16_t offset, uint8_t size, - uint32_t mask, uint32_t value) + uint32_t mask, uint32_t value) { uint8_t i; - - if (size > 4) - { - LOG_BUG(ERRMSG_INVALID_PARAMETER, __FUNCTION__); + + if (size > 4) { + LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__); return ERRCODE_INVALID_PARAMETER; } - if (!poll_nesting) - { + if (!poll_nesting) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "check poll nesting"); return ERRCODE_FAILURE_OPERATION; } if (ERROR_OK != usbtoxxx_ensure_buffer_size(3 + 4 + 2 * size)) - { return ERROR_FAIL; - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } - + type_pre = USB_TO_POLL; - + usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = USB_TO_POLL_CHECKOK; SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], offset); usbtoxxx_current_cmd_index += 2; usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = size; usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = equ; - for (i =0; i < size; i++) - { + for (i = 0; i < size; i++) usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = (mask >> (8 * i)) & 0xFF; - } - for (i =0; i < size; i++) - { + for (i = 0; i < size; i++) usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = (value >> (8 * i)) & 0xFF; - } - + return ERROR_OK; } RESULT usbtopoll_checkfail(uint8_t equ, uint16_t offset, uint8_t size, - uint32_t mask, uint32_t value) + uint32_t mask, uint32_t value) { uint8_t i; - - if (size > 4) - { - LOG_BUG(ERRMSG_INVALID_PARAMETER, __FUNCTION__); + + if (size > 4) { + LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__); return ERRCODE_INVALID_PARAMETER; } - if (!poll_nesting) - { + if (!poll_nesting) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "check poll nesting"); return ERRCODE_FAILURE_OPERATION; } if (ERROR_OK != usbtoxxx_ensure_buffer_size(3 + 4 + 2 * size)) - { return ERROR_FAIL; - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } - + type_pre = USB_TO_POLL; - + usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = USB_TO_POLL_CHECKFAIL; SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], offset); usbtoxxx_current_cmd_index += 2; usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = size; usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = equ; - for (i =0; i < size; i++) - { + for (i = 0; i < size; i++) usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = (mask >> (8 * i)) & 0xFF; - } - for (i =0; i < size; i++) - { + for (i = 0; i < size; i++) usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = (value >> (8 * i)) & 0xFF; - } - + return ERROR_OK; } RESULT usbtopoll_verifybuff(uint16_t offset, uint16_t size, uint8_t *buff) { - if (!poll_nesting) - { + if (!poll_nesting) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "check poll nesting"); return ERRCODE_FAILURE_OPERATION; } if (ERROR_OK != usbtoxxx_ensure_buffer_size(3 + 5 + size)) - { return ERROR_FAIL; - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } - + type_pre = USB_TO_POLL; - + usbtoxxx_buffer[usbtoxxx_current_cmd_index++] = USB_TO_POLL_VERIFYBUFF; SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], offset); usbtoxxx_current_cmd_index += 2; @@ -614,30 +523,24 @@ RESULT usbtopoll_verifybuff(uint16_t offset, uint16_t size, uint8_t *buff) usbtoxxx_current_cmd_index += 2; memcpy(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], buff, size); usbtoxxx_current_cmd_index += size; - + return ERROR_OK; } - - - RESULT usbtodelay_delay(uint16_t dly) { if (ERROR_OK != usbtoxxx_ensure_buffer_size(3 + 2)) - { return ERROR_FAIL; - } - - if (ERROR_OK != usbtoxxx_validate_current_command_type()) - { + + if (ERROR_OK != usbtoxxx_validate_current_command_type()) { LOG_BUG(ERRMSG_FAILURE_OPERATION, "validate previous commands"); return ERRCODE_FAILURE_OPERATION; } type_pre = USB_TO_DELAY; - + SET_LE_U16(&usbtoxxx_buffer[usbtoxxx_current_cmd_index], dly); usbtoxxx_current_cmd_index += 2; - + return versaloon_add_pending(USB_TO_DELAY, 0, 0, 0, 0, NULL, 0); } @@ -650,4 +553,3 @@ RESULT usbtodelay_delayus(uint16_t us) { return usbtodelay_delay(us & 0x7FFF); } - diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.h b/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.h index 092368ebae..daadc8e6be 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.h +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx.h @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef __USBTOXXX_H_INCLUDED__ #define __USBTOXXX_H_INCLUDED__ @@ -23,62 +24,54 @@ RESULT usbtoxxx_init(void); RESULT usbtoxxx_fini(void); RESULT usbtoxxx_execute_command(void); -#define USB_TO_XXX_ABILITIES_LEN 12 +#define USB_TO_XXX_ABILITIES_LEN 12 extern uint8_t usbtoxxx_abilities[USB_TO_XXX_ABILITIES_LEN]; bool usbtoxxx_interface_supported(uint8_t cmd); -// USB_TO_INFO +/* USB_TO_INFO */ RESULT usbtoinfo_get_abilities(uint8_t abilities[USB_TO_XXX_ABILITIES_LEN]); -// USB_TO_DELAY +/* USB_TO_DELAY */ RESULT usbtodelay_delay(uint16_t dly); RESULT usbtodelay_delayms(uint16_t ms); RESULT usbtodelay_delayus(uint16_t us); - - -// USB_TO_USART +/* USB_TO_USART */ RESULT usbtousart_init(uint8_t interface_index); RESULT usbtousart_fini(uint8_t interface_index); RESULT usbtousart_config(uint8_t interface_index, uint32_t baudrate, - uint8_t datalength, uint8_t mode); + uint8_t datalength, uint8_t mode); RESULT usbtousart_send(uint8_t interface_index, uint8_t *buf, uint16_t len); RESULT usbtousart_receive(uint8_t interface_index, uint8_t *buf, uint16_t len); RESULT usbtousart_status(uint8_t interface_index, - struct usart_status_t *status); + struct usart_status_t *status); -// USB_TO_SPI +/* USB_TO_SPI */ RESULT usbtospi_init(uint8_t interface_index); RESULT usbtospi_fini(uint8_t interface_index); RESULT usbtospi_config(uint8_t interface_index, uint32_t kHz, uint8_t mode); RESULT usbtospi_io(uint8_t interface_index, uint8_t *out, uint8_t *in, - uint16_t bytelen); + uint16_t bytelen); - - -// USB_TO_GPIO +/* USB_TO_GPIO */ RESULT usbtogpio_init(uint8_t interface_index); RESULT usbtogpio_fini(uint8_t interface_index); RESULT usbtogpio_config(uint8_t interface_index, uint32_t mask, - uint32_t dir_mask, uint32_t pull_en_mask, - uint32_t input_pull_mask); + uint32_t dir_mask, uint32_t pull_en_mask, + uint32_t input_pull_mask); RESULT usbtogpio_in(uint8_t interface_index, uint32_t mask, uint32_t *value); RESULT usbtogpio_out(uint8_t interface_index, uint32_t mask, uint32_t value); - - -// USB_TO_ISSP +/* USB_TO_ISSP */ RESULT usbtoissp_init(uint8_t interface_index); RESULT usbtoissp_fini(uint8_t interface_index); RESULT usbtoissp_enter_program_mode(uint8_t interface_index, uint8_t mode); RESULT usbtoissp_leave_program_mode(uint8_t interface_index, uint8_t mode); RESULT usbtoissp_wait_and_poll(uint8_t interface_index); RESULT usbtoissp_vector(uint8_t interface_index, uint8_t operate, uint8_t addr, - uint8_t data, uint8_t *buf); - - + uint8_t data, uint8_t *buf); -// USB_TO_LPCICP +/* USB_TO_LPCICP */ RESULT usbtolpcicp_init(uint8_t interface_index); RESULT usbtolpcicp_fini(uint8_t interface_index); RESULT usbtolpcicp_config(uint8_t interface_index); @@ -86,51 +79,42 @@ RESULT usbtolpcicp_enter_program_mode(uint8_t interface_index); RESULT usbtolpcicp_in(uint8_t interface_index, uint8_t *buff, uint16_t len); RESULT usbtolpcicp_out(uint8_t interface_index, uint8_t *buff, uint16_t len); RESULT usbtolpcicp_poll_ready(uint8_t interface_index, uint8_t data, - uint8_t *ret, uint8_t setmask, uint8_t clearmask, uint16_t pollcnt); - - + uint8_t *ret, uint8_t setmask, uint8_t clearmask, uint16_t pollcnt); -// USB_TO_JTAG_LL +/* USB_TO_JTAG_LL */ RESULT usbtojtagll_init(uint8_t interface_index); RESULT usbtojtagll_fini(uint8_t interface_index); RESULT usbtojtagll_config(uint8_t interface_index, uint32_t kHz); RESULT usbtojtagll_tms(uint8_t interface_index, uint8_t *tms, uint8_t bytelen); RESULT usbtojtagll_tms_clocks(uint8_t interface_index, uint32_t bytelen, - uint8_t tms); -RESULT usbtojtagll_scan(uint8_t interface_index, uint8_t* data, - uint16_t bitlen, uint8_t tms_before_valid, - uint8_t tms_before, uint8_t tms_after0, - uint8_t tms_after1); + uint8_t tms); +RESULT usbtojtagll_scan(uint8_t interface_index, uint8_t *data, + uint16_t bitlen, uint8_t tms_before_valid, + uint8_t tms_before, uint8_t tms_after0, + uint8_t tms_after1); - - -// USB_TO_JTAG_HL +/* USB_TO_JTAG_HL */ RESULT usbtojtaghl_init(uint8_t interface_index); RESULT usbtojtaghl_fini(uint8_t interface_index); RESULT usbtojtaghl_config(uint8_t interface_index, uint32_t kHz, uint8_t ub, - uint8_t ua, uint16_t bb, uint16_t ba); + uint8_t ua, uint16_t bb, uint16_t ba); RESULT usbtojtaghl_ir(uint8_t interface_index, uint8_t *ir, uint16_t bitlen, - uint8_t idle, uint8_t want_ret); + uint8_t idle, uint8_t want_ret); RESULT usbtojtaghl_dr(uint8_t interface_index, uint8_t *dr, uint16_t bitlen, - uint8_t idle, uint8_t want_ret); + uint8_t idle, uint8_t want_ret); RESULT usbtojtaghl_tms(uint8_t interface_index, uint8_t *tms, uint16_t bitlen); RESULT usbtojtaghl_runtest(uint8_t interface_index, uint32_t cycles); RESULT usbtojtaghl_register_callback(uint8_t index, jtag_callback_t send_callback, - jtag_callback_t receive_callback); - + jtag_callback_t receive_callback); - -// USB_TO_JTAG_RAW +/* USB_TO_JTAG_RAW */ RESULT usbtojtagraw_init(uint8_t interface_index); RESULT usbtojtagraw_fini(uint8_t interface_index); RESULT usbtojtagraw_config(uint8_t interface_index, uint32_t kHz); RESULT usbtojtagraw_execute(uint8_t interface_index, uint8_t *tdi, - uint8_t *tms, uint8_t *tdo, uint32_t bitlen); - + uint8_t *tms, uint8_t *tdo, uint32_t bitlen); - - -// USB_TO_C2 +/* USB_TO_C2 */ RESULT usbtoc2_init(uint8_t interface_index); RESULT usbtoc2_fini(uint8_t interface_index); RESULT usbtoc2_writeaddr(uint8_t interface_index, uint8_t addr); @@ -138,141 +122,117 @@ RESULT usbtoc2_readaddr(uint8_t interface_index, uint8_t *data); RESULT usbtoc2_writedata(uint8_t interface_index, uint8_t *buf, uint8_t len); RESULT usbtoc2_readdata(uint8_t interface_index, uint8_t *buf, uint8_t len); - - -// USB_TO_I2C +/* USB_TO_I2C */ RESULT usbtoi2c_init(uint8_t interface_index); RESULT usbtoi2c_fini(uint8_t interface_index); RESULT usbtoi2c_config(uint8_t interface_index, uint16_t kHz, - uint16_t byte_interval, uint16_t max_dly); + uint16_t byte_interval, uint16_t max_dly); RESULT usbtoi2c_read(uint8_t interface_index, uint16_t chip_addr, - uint8_t *data, uint16_t data_len, uint8_t stop, - bool nacklast); + uint8_t *data, uint16_t data_len, uint8_t stop, + bool nacklast); RESULT usbtoi2c_write(uint8_t interface_index, uint16_t chip_addr, - uint8_t *data, uint16_t data_len, uint8_t stop); - + uint8_t *data, uint16_t data_len, uint8_t stop); - -// USB_TO_MSP430_JTAG +/* USB_TO_MSP430_JTAG */ RESULT usbtomsp430jtag_init(uint8_t interface_index); RESULT usbtomsp430jtag_fini(uint8_t interface_index); RESULT usbtomsp430jtag_config(uint8_t interface_index, uint8_t has_test); RESULT usbtomsp430jtag_ir(uint8_t interface_index, uint8_t *ir, - uint8_t want_ret); + uint8_t want_ret); RESULT usbtomsp430jtag_dr(uint8_t interface_index, uint32_t *dr, - uint8_t bitlen, uint8_t want_ret); + uint8_t bitlen, uint8_t want_ret); RESULT usbtomsp430jtag_tclk(uint8_t interface_index, uint8_t value); RESULT usbtomsp430jtag_tclk_strobe(uint8_t interface_index, uint16_t cnt); RESULT usbtomsp430jtag_reset(uint8_t interface_index); RESULT usbtomsp430jtag_poll(uint8_t interface_index, uint32_t dr, - uint32_t mask, uint32_t value, uint8_t len, - uint16_t poll_cnt, uint8_t toggle_tclk); - - + uint32_t mask, uint32_t value, uint8_t len, + uint16_t poll_cnt, uint8_t toggle_tclk); -// USB_TO_MSP430_SBW +/* USB_TO_MSP430_SBW */ RESULT usbtomsp430sbw_init(uint8_t interface_index); RESULT usbtomsp430sbw_fini(uint8_t interface_index); RESULT usbtomsp430sbw_config(uint8_t interface_index, uint8_t has_test); RESULT usbtomsp430sbw_ir(uint8_t interface_index, uint8_t *ir, - uint8_t want_ret); + uint8_t want_ret); RESULT usbtomsp430sbw_dr(uint8_t interface_index, uint32_t *dr, - uint8_t bitlen, uint8_t want_ret); + uint8_t bitlen, uint8_t want_ret); RESULT usbtomsp430sbw_tclk(uint8_t interface_index, uint8_t value); RESULT usbtomsp430sbw_tclk_strobe(uint8_t interface_index, uint16_t cnt); RESULT usbtomsp430sbw_reset(uint8_t interface_index); RESULT usbtomsp430sbw_poll(uint8_t interface_index, uint32_t dr, uint32_t mask, - uint32_t value, uint8_t len, uint16_t poll_cnt, - uint8_t toggle_tclk); + uint32_t value, uint8_t len, uint16_t poll_cnt, + uint8_t toggle_tclk); - - -// USB_TO_POWER +/* USB_TO_POWER */ RESULT usbtopwr_init(uint8_t interface_index); RESULT usbtopwr_fini(uint8_t interface_index); RESULT usbtopwr_config(uint8_t interface_index); RESULT usbtopwr_output(uint8_t interface_index, uint16_t mV); - - -// USB_TO_POLL +/* USB_TO_POLL */ RESULT usbtopoll_start(uint16_t retry_cnt, uint16_t interval_us); RESULT usbtopoll_end(void); RESULT usbtopoll_checkok(uint8_t equ, uint16_t offset, uint8_t size, - uint32_t mask, uint32_t value); + uint32_t mask, uint32_t value); RESULT usbtopoll_checkfail(uint8_t equ, uint16_t offset, uint8_t size, - uint32_t mask, uint32_t value); + uint32_t mask, uint32_t value); RESULT usbtopoll_verifybuff(uint16_t offset, uint16_t size, uint8_t *buff); - - -// USB_TO_SWD +/* USB_TO_SWD */ RESULT usbtoswd_init(uint8_t interface_index); RESULT usbtoswd_fini(uint8_t interface_index); RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry, - uint16_t dly); + uint16_t dly); RESULT usbtoswd_seqout(uint8_t interface_index, uint8_t *data, uint16_t bitlen); RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen); RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request, - uint32_t *data, uint8_t *ack); - - + uint32_t *data, uint8_t *ack); -// USB_TO_SWIM +/* USB_TO_SWIM */ RESULT usbtoswim_init(uint8_t interface_index); RESULT usbtoswim_fini(uint8_t interface_index); RESULT usbtoswim_config(uint8_t interface_index, uint8_t mHz, uint8_t cnt0, - uint8_t cnt1); + uint8_t cnt1); RESULT usbtoswim_srst(uint8_t interface_index); RESULT usbtoswim_wotf(uint8_t interface_index, uint8_t *data, - uint16_t bytelen, uint32_t addr); + uint16_t bytelen, uint32_t addr); RESULT usbtoswim_rotf(uint8_t interface_index, uint8_t *data, - uint16_t bytelen, uint32_t addr); + uint16_t bytelen, uint32_t addr); RESULT usbtoswim_sync(uint8_t interface_index, uint8_t mHz); RESULT usbtoswim_enable(uint8_t interface_index); - - - -// USB_TO_BDM +/* USB_TO_BDM */ RESULT usbtobdm_init(uint8_t interface_index); RESULT usbtobdm_fini(uint8_t interface_index); RESULT usbtobdm_sync(uint8_t interface_index, uint16_t *khz); RESULT usbtobdm_transact(uint8_t interface_index, uint8_t *out, - uint8_t outlen, uint8_t *in, uint8_t inlen, uint8_t delay, uint8_t ack); - - + uint8_t outlen, uint8_t *in, uint8_t inlen, uint8_t delay, uint8_t ack); -// USB_TO_DUSI +/* USB_TO_DUSI */ RESULT usbtodusi_init(uint8_t interface_index); RESULT usbtodusi_fini(uint8_t interface_index); RESULT usbtodusi_config(uint8_t interface_index, uint32_t kHz, uint8_t mode); RESULT usbtodusi_io(uint8_t interface_index, uint8_t *mo, uint8_t *mi, - uint8_t *so, uint8_t *si, uint32_t bitlen); + uint8_t *so, uint8_t *si, uint32_t bitlen); - - -// USB_TO_MICROWIRE +/* USB_TO_MICROWIRE */ RESULT usbtomicrowire_init(uint8_t interface_index); RESULT usbtomicrowire_fini(uint8_t interface_index); RESULT usbtomicrowire_config(uint8_t interface_index, uint16_t kHz, - uint8_t sel_polarity); + uint8_t sel_polarity); RESULT usbtomicrowire_transport(uint8_t interface_index, - uint32_t opcode, uint8_t opcode_bitlen, - uint32_t addr, uint8_t addr_bitlen, - uint32_t data, uint8_t data_bitlen, - uint8_t *reply, uint8_t reply_bitlen); + uint32_t opcode, uint8_t opcode_bitlen, + uint32_t addr, uint8_t addr_bitlen, + uint32_t data, uint8_t data_bitlen, + uint8_t *reply, uint8_t reply_bitlen); RESULT usbtomicrowire_poll(uint8_t interface_index, uint16_t interval_us, - uint16_t retry_cnt); - + uint16_t retry_cnt); - -// USB_TO_PWM +/* USB_TO_PWM */ RESULT usbtopwm_init(uint8_t interface_index); RESULT usbtopwm_fini(uint8_t interface_index); RESULT usbtopwm_config(uint8_t interface_index, uint16_t kHz, uint8_t mode); RESULT usbtopwm_out(uint8_t interface_index, uint16_t count, uint16_t *rate); RESULT usbtopwm_in(uint8_t interface_index, uint16_t count, uint16_t *rate); -#endif /* __USBTOXXX_H_INCLUDED__ */ - +#endif /* __USBTOXXX_H_INCLUDED__ */ diff --git a/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx_internal.h b/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx_internal.h index 837d6949e1..de2ca8b919 100644 --- a/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx_internal.h +++ b/src/jtag/drivers/versaloon/usbtoxxx/usbtoxxx_internal.h @@ -16,154 +16,148 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef __USBTOXXX_INTERNAL_H_INCLUDED__ #define __USBTOXXX_INTERNAL_H_INCLUDED__ -// USB_TO_XXX USB Commands -// Page0 -#define USB_TO_USART (VERSALOON_USB_TO_XXX_CMD_START + 0x00) -#define USB_TO_SPI (VERSALOON_USB_TO_XXX_CMD_START + 0x01) -#define USB_TO_I2C (VERSALOON_USB_TO_XXX_CMD_START + 0x02) -#define USB_TO_GPIO (VERSALOON_USB_TO_XXX_CMD_START + 0x03) -#define USB_TO_CAN (VERSALOON_USB_TO_XXX_CMD_START + 0x04) -#define USB_TO_PWM (VERSALOON_USB_TO_XXX_CMD_START + 0x05) -#define USB_TO_ADC (VERSALOON_USB_TO_XXX_CMD_START + 0x06) -#define USB_TO_DAC (VERSALOON_USB_TO_XXX_CMD_START + 0x07) -#define USB_TO_MICROWIRE (VERSALOON_USB_TO_XXX_CMD_START + 0x08) -#define USB_TO_SWIM (VERSALOON_USB_TO_XXX_CMD_START + 0x09) -#define USB_TO_DUSI (VERSALOON_USB_TO_XXX_CMD_START + 0x0A) -// Page1 -#define USB_TO_JTAG_LL (VERSALOON_USB_TO_XXX_CMD_START + 0x20) -#define USB_TO_JTAG_HL (VERSALOON_USB_TO_XXX_CMD_START + 0x21) -#define USB_TO_ISSP (VERSALOON_USB_TO_XXX_CMD_START + 0x22) -#define USB_TO_C2 (VERSALOON_USB_TO_XXX_CMD_START + 0x23) -#define USB_TO_SBW (VERSALOON_USB_TO_XXX_CMD_START + 0x24) -#define USB_TO_LPCICP (VERSALOON_USB_TO_XXX_CMD_START + 0x25) -#define USB_TO_SWD (VERSALOON_USB_TO_XXX_CMD_START + 0x26) -#define USB_TO_JTAG_RAW (VERSALOON_USB_TO_XXX_CMD_START + 0x27) -#define USB_TO_BDM (VERSALOON_USB_TO_XXX_CMD_START + 0x28) -#define USB_TO_MSP430_JTAG (VERSALOON_USB_TO_XXX_CMD_START + 0x38) -// Page2 -#define USB_TO_POWER (VERSALOON_USB_TO_XXX_CMD_START + 0x40) -#define USB_TO_DELAY (VERSALOON_USB_TO_XXX_CMD_START + 0x41) -#define USB_TO_POLL (VERSALOON_USB_TO_XXX_CMD_START + 0x42) -#define USB_TO_INFO (VERSALOON_USB_TO_XXX_CMD_START + 0x5E) -#define USB_TO_ALL (VERSALOON_USB_TO_XXX_CMD_START + 0x5F) - - - -// USB_TO_XXX Masks -#define USB_TO_XXX_CMDMASK 0xF8 -#define USB_TO_XXX_CMDSHIFT 3 -#define USB_TO_XXX_IDXMASK 0x07 -// USB_TO_XXX Sub Commands -// Common Sub Commands -#define USB_TO_XXX_INIT (0x00 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_FINI (0x01 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_CONFIG (0x02 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_GETHWINFO (0x03 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_STATUS (0X04 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_IN_OUT (0x05 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_IN (0x06 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_OUT (0x07 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_POLL (0x08 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_SPECIAL (0x09 << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_RESET (0x0A << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_SYNC (0x0B << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_ENABLE (0x0C << USB_TO_XXX_CMDSHIFT) -#define USB_TO_XXX_DISABLE (0x0D << USB_TO_XXX_CMDSHIFT) -// USB_TO_POLL -#define USB_TO_POLL_START 0x00 -#define USB_TO_POLL_END 0x01 -#define USB_TO_POLL_CHECKOK 0x02 -#define USB_TO_POLL_CHECKFAIL 0x03 -#define USB_TO_POLL_VERIFYBUFF 0x04 - - - -// USB_TO_XXX Replys -#define USB_TO_XXX_OK 0x00 -#define USB_TO_XXX_FAILED 0x01 -#define USB_TO_XXX_TIME_OUT 0x02 -#define USB_TO_XXX_INVALID_INDEX 0x03 -#define USB_TO_XXX_INVALID_PARA 0x04 -#define USB_TO_XXX_INVALID_CMD 0x05 -#define USB_TO_XXX_CMD_NOT_SUPPORT 0x06 - - - -// USB_TO_XXX +/* USB_TO_XXX USB Commands */ +/* Page0 */ +#define USB_TO_USART (VERSALOON_USB_TO_XXX_CMD_START + 0x00) +#define USB_TO_SPI (VERSALOON_USB_TO_XXX_CMD_START + 0x01) +#define USB_TO_I2C (VERSALOON_USB_TO_XXX_CMD_START + 0x02) +#define USB_TO_GPIO (VERSALOON_USB_TO_XXX_CMD_START + 0x03) +#define USB_TO_CAN (VERSALOON_USB_TO_XXX_CMD_START + 0x04) +#define USB_TO_PWM (VERSALOON_USB_TO_XXX_CMD_START + 0x05) +#define USB_TO_ADC (VERSALOON_USB_TO_XXX_CMD_START + 0x06) +#define USB_TO_DAC (VERSALOON_USB_TO_XXX_CMD_START + 0x07) +#define USB_TO_MICROWIRE (VERSALOON_USB_TO_XXX_CMD_START + 0x08) +#define USB_TO_SWIM (VERSALOON_USB_TO_XXX_CMD_START + 0x09) +#define USB_TO_DUSI (VERSALOON_USB_TO_XXX_CMD_START + 0x0A) +/* Page1 */ +#define USB_TO_JTAG_LL (VERSALOON_USB_TO_XXX_CMD_START + 0x20) +#define USB_TO_JTAG_HL (VERSALOON_USB_TO_XXX_CMD_START + 0x21) +#define USB_TO_ISSP (VERSALOON_USB_TO_XXX_CMD_START + 0x22) +#define USB_TO_C2 (VERSALOON_USB_TO_XXX_CMD_START + 0x23) +#define USB_TO_SBW (VERSALOON_USB_TO_XXX_CMD_START + 0x24) +#define USB_TO_LPCICP (VERSALOON_USB_TO_XXX_CMD_START + 0x25) +#define USB_TO_SWD (VERSALOON_USB_TO_XXX_CMD_START + 0x26) +#define USB_TO_JTAG_RAW (VERSALOON_USB_TO_XXX_CMD_START + 0x27) +#define USB_TO_BDM (VERSALOON_USB_TO_XXX_CMD_START + 0x28) +#define USB_TO_MSP430_JTAG (VERSALOON_USB_TO_XXX_CMD_START + 0x38) +/* Page2 */ +#define USB_TO_POWER (VERSALOON_USB_TO_XXX_CMD_START + 0x40) +#define USB_TO_DELAY (VERSALOON_USB_TO_XXX_CMD_START + 0x41) +#define USB_TO_POLL (VERSALOON_USB_TO_XXX_CMD_START + 0x42) +#define USB_TO_INFO (VERSALOON_USB_TO_XXX_CMD_START + 0x5E) +#define USB_TO_ALL (VERSALOON_USB_TO_XXX_CMD_START + 0x5F) + +/* USB_TO_XXX Masks */ +#define USB_TO_XXX_CMDMASK 0xF8 +#define USB_TO_XXX_CMDSHIFT 3 +#define USB_TO_XXX_IDXMASK 0x07 +/* USB_TO_XXX Sub Commands */ +/* Common Sub Commands */ +#define USB_TO_XXX_INIT (0x00 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_FINI (0x01 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_CONFIG (0x02 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_GETHWINFO (0x03 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_STATUS (0X04 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_IN_OUT (0x05 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_IN (0x06 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_OUT (0x07 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_POLL (0x08 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_SPECIAL (0x09 << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_RESET (0x0A << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_SYNC (0x0B << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_ENABLE (0x0C << USB_TO_XXX_CMDSHIFT) +#define USB_TO_XXX_DISABLE (0x0D << USB_TO_XXX_CMDSHIFT) +/* USB_TO_POLL */ +#define USB_TO_POLL_START 0x00 +#define USB_TO_POLL_END 0x01 +#define USB_TO_POLL_CHECKOK 0x02 +#define USB_TO_POLL_CHECKFAIL 0x03 +#define USB_TO_POLL_VERIFYBUFF 0x04 + +/* USB_TO_XXX Replys */ +#define USB_TO_XXX_OK 0x00 +#define USB_TO_XXX_FAILED 0x01 +#define USB_TO_XXX_TIME_OUT 0x02 +#define USB_TO_XXX_INVALID_INDEX 0x03 +#define USB_TO_XXX_INVALID_PARA 0x04 +#define USB_TO_XXX_INVALID_CMD 0x05 +#define USB_TO_XXX_CMD_NOT_SUPPORT 0x06 + +/* USB_TO_XXX */ RESULT usbtoxxx_add_pending(uint8_t type, uint8_t cmd, uint16_t - actual_szie, uint16_t want_pos, - uint16_t want_size, uint8_t *buffer); + actual_szie, uint16_t want_pos, + uint16_t want_size, uint8_t *buffer); RESULT usbtoxxx_add_command(uint8_t type, uint8_t cmd, uint8_t *cmdbuf, - uint16_t cmdlen, uint16_t retlen, - uint8_t *wantbuf, uint16_t wantpos, - uint16_t wantlen, uint8_t collect); + uint16_t cmdlen, uint16_t retlen, + uint8_t *wantbuf, uint16_t wantpos, + uint16_t wantlen, uint8_t collect); #define usbtoxxx_init_command(type, port) \ - usbtoxxx_add_command((type), (USB_TO_XXX_INIT | (port)), \ - NULL, 0, 0, NULL, 0, 0, 0) + usbtoxxx_add_command((type), (USB_TO_XXX_INIT | (port)), \ + NULL, 0, 0, NULL, 0, 0, 0) #define usbtoxxx_fini_command(type, port) \ - usbtoxxx_add_command((type), (USB_TO_XXX_FINI | (port)), \ - NULL, 0, 0, NULL, 0, 0, 0) + usbtoxxx_add_command((type), (USB_TO_XXX_FINI | (port)), \ + NULL, 0, 0, NULL, 0, 0, 0) #define usbtoxxx_conf_command(type, port, cmdbuf, cmdlen) \ - usbtoxxx_add_command((type), (USB_TO_XXX_CONFIG | (port)), \ - (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) + usbtoxxx_add_command((type), (USB_TO_XXX_CONFIG | (port)), \ + (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) #define usbtoxxx_inout_command(type, port, cmdbuf, cmdlen, retlen, wantbuf, \ - wantpos, wantlen, c) \ - usbtoxxx_add_command((type), (USB_TO_XXX_IN_OUT | (port)), \ - (cmdbuf), (cmdlen), (retlen), (wantbuf), \ - (wantpos), (wantlen), (c)) + wantpos, wantlen, c) \ + usbtoxxx_add_command((type), (USB_TO_XXX_IN_OUT | (port)), \ + (cmdbuf), (cmdlen), (retlen), (wantbuf), \ + (wantpos), (wantlen), (c)) #define usbtoxxx_in_command(type, port, cmdbuf, cmdlen, retlen, wantbuf, \ - wantpos, wantlen, c) \ - usbtoxxx_add_command((type), (USB_TO_XXX_IN | (port)), (cmdbuf), \ - (cmdlen), (retlen), (wantbuf), (wantpos), \ - (wantlen), (c)) + wantpos, wantlen, c) \ + usbtoxxx_add_command((type), (USB_TO_XXX_IN | (port)), (cmdbuf), \ + (cmdlen), (retlen), (wantbuf), (wantpos), \ + (wantlen), (c)) #define usbtoxxx_out_command(type, port, cmdbuf, cmdlen, c) \ - usbtoxxx_add_command((type), (USB_TO_XXX_OUT | (port)), (cmdbuf), \ - (cmdlen), 0, NULL, 0, 0, (c)) + usbtoxxx_add_command((type), (USB_TO_XXX_OUT | (port)), (cmdbuf), \ + (cmdlen), 0, NULL, 0, 0, (c)) #define usbtoxxx_poll_command(type, port, cmdbuf, cmdlen, retbuf, retlen) \ - usbtoxxx_add_command((type), (USB_TO_XXX_POLL | (port)), (cmdbuf),\ - (cmdlen), (retlen), (retbuf), 0, (retlen), 0) -#define usbtoxxx_status_command(type, port, retlen, wantbuf, wantpos, wantlen, c)\ - usbtoxxx_add_command((type), (USB_TO_XXX_STATUS | (port)), \ - NULL, 0, (retlen), (wantbuf), (wantpos), \ - (wantlen), (c)) + usbtoxxx_add_command((type), (USB_TO_XXX_POLL | (port)), (cmdbuf), \ + (cmdlen), (retlen), (retbuf), 0, (retlen), 0) +#define usbtoxxx_status_command(type, port, retlen, wantbuf, wantpos, wantlen, c) \ + usbtoxxx_add_command((type), (USB_TO_XXX_STATUS | (port)), \ + NULL, 0, (retlen), (wantbuf), (wantpos), \ + (wantlen), (c)) #define usbtoxxx_special_command(type, port, cmdbuf, cmdlen, retlen, wantbuf, \ - wantpos, wantlen, c) \ - usbtoxxx_add_command((type), (USB_TO_XXX_SPECIAL | (port)), \ - (cmdbuf), (cmdlen), retlen, wantbuf, \ - wantpos, wantlen, (c)) + wantpos, wantlen, c) \ + usbtoxxx_add_command((type), (USB_TO_XXX_SPECIAL | (port)), \ + (cmdbuf), (cmdlen), retlen, wantbuf, \ + wantpos, wantlen, (c)) #define usbtoxxx_reset_command(type, port, cmdbuf, cmdlen) \ - usbtoxxx_add_command((type), (USB_TO_XXX_RESET | (port)), \ - (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) + usbtoxxx_add_command((type), (USB_TO_XXX_RESET | (port)), \ + (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) #define usbtoxxx_sync_command(type, port, cmdbuf, cmdlen, retlen, wantbuf) \ - usbtoxxx_add_command((type), (USB_TO_XXX_SYNC | (port)), \ - (cmdbuf), (cmdlen), (retlen), (wantbuf), 0, \ - (retlen), 0) + usbtoxxx_add_command((type), (USB_TO_XXX_SYNC | (port)), \ + (cmdbuf), (cmdlen), (retlen), (wantbuf), 0, \ + (retlen), 0) #define usbtoxxx_enable_command(type, port, cmdbuf, cmdlen) \ - usbtoxxx_add_command((type), (USB_TO_XXX_ENABLE | (port)), \ - (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) + usbtoxxx_add_command((type), (USB_TO_XXX_ENABLE | (port)), \ + (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) #define usbtoxxx_disable_command(type, port, cmdbuf, cmdlen) \ - usbtoxxx_add_command((type), (USB_TO_XXX_DISABLE | (port)), \ - (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) + usbtoxxx_add_command((type), (USB_TO_XXX_DISABLE | (port)), \ + (cmdbuf), (cmdlen), 0, NULL, 0, 0, 0) -// USB_TO_SPI +/* USB_TO_SPI */ #define USB_TO_SPI_BAUDRATE_MSK 0x1F #define USB_TO_SPI_CPOL_MSK 0x20 #define USB_TO_SPI_CPHA_MSK 0x40 #define USB_TO_SPI_MSB_FIRST 0x80 -// USB_TO_DUSI +/* USB_TO_DUSI */ #define USB_TO_DUSI_BAUDRATE_MSK 0x1F #define USB_TO_DUSI_CPOL_MSK 0x20 #define USB_TO_DUSI_CPHA_MSK 0x40 #define USB_TO_DUSI_MSB_FIRST 0x80 -// USB_TO_GPIO +/* USB_TO_GPIO */ #define USB_TO_GPIO_DIR_MSK 0x01 -#endif /* __USBTOXXX_INTERNAL_H_INCLUDED__ */ - +#endif /* __USBTOXXX_INTERNAL_H_INCLUDED__ */ diff --git a/src/jtag/drivers/versaloon/versaloon.c b/src/jtag/drivers/versaloon/versaloon.c index dbb8e4fb03..e33a260f00 100644 --- a/src/jtag/drivers/versaloon/versaloon.c +++ b/src/jtag/drivers/versaloon/versaloon.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -28,14 +29,14 @@ #include "versaloon_internal.h" #include "usbtoxxx/usbtoxxx.h" -uint8_t *versaloon_buf = NULL; -uint8_t *versaloon_cmd_buf = NULL; +uint8_t *versaloon_buf; +uint8_t *versaloon_cmd_buf; uint16_t versaloon_buf_size; struct versaloon_pending_t versaloon_pending[VERSALOON_MAX_PENDING_NUMBER]; -uint16_t versaloon_pending_idx = 0; +uint16_t versaloon_pending_idx; -usb_dev_handle *versaloon_usb_device_handle = NULL; +usb_dev_handle *versaloon_usb_device_handle; static uint32_t versaloon_usb_to = VERSALOON_TIMEOUT; RESULT versaloon_init(void); @@ -44,58 +45,59 @@ RESULT versaloon_get_target_voltage(uint16_t *voltage); RESULT versaloon_set_target_voltage(uint16_t voltage); RESULT versaloon_delay_ms(uint16_t ms); RESULT versaloon_delay_us(uint16_t us); -struct versaloon_interface_t versaloon_interface = -{ - .init = versaloon_init, - .fini = versaloon_fini, - {// adaptors - {// target_voltage - .get = versaloon_get_target_voltage, - .set = versaloon_set_target_voltage, + +struct versaloon_interface_t versaloon_interface = { + .init = versaloon_init, + .fini = versaloon_fini, + { /* adaptors */ + { /* target_voltage */ + .get = versaloon_get_target_voltage, + .set = versaloon_set_target_voltage, }, - {// gpio - .init = usbtogpio_init, - .fini = usbtogpio_fini, - .config = usbtogpio_config, - .out = usbtogpio_out, - .in = usbtogpio_in, + { /* gpio */ + .init = usbtogpio_init, + .fini = usbtogpio_fini, + .config = usbtogpio_config, + .out = usbtogpio_out, + .in = usbtogpio_in, }, - {// delay - .delayms = versaloon_delay_ms, - .delayus = versaloon_delay_us, + { /* delay */ + .delayms = versaloon_delay_ms, + .delayus = versaloon_delay_us, }, - {// swd - .init = usbtoswd_init, - .fini = usbtoswd_fini, - .config = usbtoswd_config, - .seqout = usbtoswd_seqout, - .seqin = usbtoswd_seqin, - .transact = usbtoswd_transact, + { /* swd */ + .init = usbtoswd_init, + .fini = usbtoswd_fini, + .config = usbtoswd_config, + .seqout = usbtoswd_seqout, + .seqin = usbtoswd_seqin, + .transact = usbtoswd_transact, }, - {// jtag_raw - .init = usbtojtagraw_init, - .fini = usbtojtagraw_fini, - .config = usbtojtagraw_config, - .execute = usbtojtagraw_execute, + { /* jtag_raw */ + .init = usbtojtagraw_init, + .fini = usbtojtagraw_fini, + .config = usbtojtagraw_config, + .execute = usbtojtagraw_execute, }, - .peripheral_commit = usbtoxxx_execute_command, + .peripheral_commit = usbtoxxx_execute_command, }, - {// usb_setting - .vid = VERSALOON_VID, - .pid = VERSALOON_PID, - .ep_out = VERSALOON_OUTP, - .ep_in = VERSALOON_INP, - .interface = VERSALOON_IFACE, - .serialstring = NULL, - .buf_size = 256, + { /* usb_setting */ + .vid = VERSALOON_VID, + .pid = VERSALOON_PID, + .ep_out = VERSALOON_OUTP, + .ep_in = VERSALOON_INP, + .interface = VERSALOON_IFACE, + .serialstring = NULL, + .buf_size = 256, } }; -// programmer_cmd -static uint32_t versaloon_pending_id = 0; -static versaloon_callback_t versaloon_callback = NULL; -static void *versaloon_extra_data = NULL; -static struct versaloon_want_pos_t *versaloon_want_pos = NULL; +/* programmer_cmd */ +static uint32_t versaloon_pending_id; +static versaloon_callback_t versaloon_callback; +static void *versaloon_extra_data; +static struct versaloon_want_pos_t *versaloon_want_pos; + void versaloon_set_pending_id(uint32_t id) { versaloon_pending_id = id; @@ -104,7 +106,7 @@ void versaloon_set_callback(versaloon_callback_t callback) { versaloon_callback = callback; } -void versaloon_set_extra_data(void * p) +void versaloon_set_extra_data(void *p) { versaloon_extra_data = p; } @@ -115,19 +117,16 @@ void versaloon_free_want_pos(void) struct versaloon_want_pos_t *tmp, *free_tmp; tmp = versaloon_want_pos; - while (tmp != NULL) - { + while (tmp != NULL) { free_tmp = tmp; tmp = tmp->next; free(free_tmp); } versaloon_want_pos = NULL; - for (i = 0; i < dimof(versaloon_pending); i++) - { + for (i = 0; i < dimof(versaloon_pending); i++) { tmp = versaloon_pending[i].pos; - while (tmp != NULL) - { + while (tmp != NULL) { free_tmp = tmp; tmp = tmp->next; free(free_tmp); @@ -141,8 +140,7 @@ RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff) struct versaloon_want_pos_t *new_pos = NULL; new_pos = (struct versaloon_want_pos_t *)malloc(sizeof(*new_pos)); - if (NULL == new_pos) - { + if (NULL == new_pos) { LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY); return ERRCODE_NOT_ENOUGH_MEMORY; } @@ -152,17 +150,12 @@ RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff) new_pos->next = NULL; if (NULL == versaloon_want_pos) - { versaloon_want_pos = new_pos; - } - else - { + else { struct versaloon_want_pos_t *tmp = versaloon_want_pos; while (tmp->next != NULL) - { tmp = tmp->next; - } tmp->next = new_pos; } @@ -173,10 +166,9 @@ RESULT versaloon_add_pending(uint8_t type, uint8_t cmd, uint16_t actual_szie, uint16_t want_pos, uint16_t want_size, uint8_t *buffer, uint8_t collect) { #if PARAM_CHECK - if (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER) - { + if (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER) { LOG_BUG(ERRMSG_INVALID_INDEX, versaloon_pending_idx, - "versaloon pending data"); + "versaloon pending data"); return ERROR_FAIL; } #endif @@ -206,83 +198,68 @@ RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen) int ret; #if PARAM_CHECK - if (NULL == versaloon_buf) - { + if (NULL == versaloon_buf) { LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(versaloon_buf)); return ERRCODE_INVALID_BUFFER; } - if ((0 == out_len) || (out_len > versaloon_interface.usb_setting.buf_size)) - { - LOG_BUG(ERRMSG_INVALID_PARAMETER, __FUNCTION__); + if ((0 == out_len) || (out_len > versaloon_interface.usb_setting.buf_size)) { + LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__); return ERRCODE_INVALID_PARAMETER; } #endif ret = usb_bulk_write(versaloon_usb_device_handle, - versaloon_interface.usb_setting.ep_out, (char *)versaloon_buf, - out_len, versaloon_usb_to); - if (ret != out_len) - { + versaloon_interface.usb_setting.ep_out, (char *)versaloon_buf, + out_len, versaloon_usb_to); + if (ret != out_len) { LOG_ERROR(ERRMSG_FAILURE_OPERATION_ERRSTRING, "send usb data", usb_strerror()); return ERRCODE_FAILURE_OPERATION; } - if (inlen != NULL) - { + if (inlen != NULL) { ret = usb_bulk_read(versaloon_usb_device_handle, - versaloon_interface.usb_setting.ep_in, (char *)versaloon_buf, - versaloon_interface.usb_setting.buf_size, versaloon_usb_to); - if (ret > 0) - { + versaloon_interface.usb_setting.ep_in, (char *)versaloon_buf, + versaloon_interface.usb_setting.buf_size, versaloon_usb_to); + if (ret > 0) { *inlen = (uint16_t)ret; return ERROR_OK; - } - else - { + } else { LOG_ERROR(ERRMSG_FAILURE_OPERATION_ERRSTRING, "receive usb data", - usb_strerror()); + usb_strerror()); return ERROR_FAIL; } - } - else - { + } else return ERROR_OK; - } } -#define VERSALOON_RETRY_CNT 10 +#define VERSALOON_RETRY_CNT 10 RESULT versaloon_init(void) { uint16_t ret = 0; uint8_t retry; uint32_t timeout_tmp; - // malloc temporary buffer + /* malloc temporary buffer */ versaloon_buf = (uint8_t *)malloc(versaloon_interface.usb_setting.buf_size); - if (NULL == versaloon_buf) - { + if (NULL == versaloon_buf) { LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY); return ERRCODE_NOT_ENOUGH_MEMORY; } - // connect to versaloon + /* connect to versaloon */ timeout_tmp = versaloon_usb_to; - // not output error message when connectting - // 100ms delay when connect + /* not output error message when connectting */ + /* 100ms delay when connect */ versaloon_usb_to = 100; - for (retry = 0; retry < VERSALOON_RETRY_CNT; retry++) - { + for (retry = 0; retry < VERSALOON_RETRY_CNT; retry++) { versaloon_buf[0] = VERSALOON_GET_INFO; if ((ERROR_OK == versaloon_send_command(1, &ret)) && (ret >= 3)) - { break; - } } versaloon_usb_to = timeout_tmp; - if (VERSALOON_RETRY_CNT == retry) - { + if (VERSALOON_RETRY_CNT == retry) { versaloon_fini(); LOG_ERROR(ERRMSG_FAILURE_OPERATION, "communicate with versaloon"); return ERRCODE_FAILURE_OPERATION; @@ -293,28 +270,25 @@ RESULT versaloon_init(void) versaloon_interface.usb_setting.buf_size = versaloon_buf_size; LOG_INFO("%s", versaloon_buf + 2); - // free temporary buffer + /* free temporary buffer */ free(versaloon_buf); versaloon_buf = NULL; versaloon_buf = (uint8_t *)malloc(versaloon_interface.usb_setting.buf_size); - if (NULL == versaloon_buf) - { + if (NULL == versaloon_buf) { versaloon_fini(); LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY); return ERRCODE_NOT_ENOUGH_MEMORY; } versaloon_cmd_buf = (uint8_t *)malloc(versaloon_interface.usb_setting.buf_size - 3); - if (NULL == versaloon_cmd_buf) - { + if (NULL == versaloon_cmd_buf) { versaloon_fini(); LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY); return ERRCODE_NOT_ENOUGH_MEMORY; } - if (ERROR_OK != usbtoxxx_init()) - { + if (ERROR_OK != usbtoxxx_init()) { LOG_ERROR(ERRMSG_FAILURE_OPERATION, "initialize usbtoxxx"); return ERROR_FAIL; } @@ -323,20 +297,17 @@ RESULT versaloon_init(void) RESULT versaloon_fini(void) { - if (versaloon_usb_device_handle != NULL) - { + if (versaloon_usb_device_handle != NULL) { usbtoxxx_fini(); versaloon_free_want_pos(); versaloon_usb_device_handle = NULL; - if (versaloon_buf != NULL) - { + if (versaloon_buf != NULL) { free(versaloon_buf); versaloon_buf = NULL; } - if (versaloon_cmd_buf != NULL) - { + if (versaloon_cmd_buf != NULL) { free(versaloon_cmd_buf); versaloon_cmd_buf = NULL; } @@ -360,27 +331,22 @@ RESULT versaloon_get_target_voltage(uint16_t *voltage) uint16_t inlen; #if PARAM_CHECK - if (NULL == versaloon_buf) - { + if (NULL == versaloon_buf) { LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(versaloon_buf)); return ERRCODE_INVALID_BUFFER; } - if (NULL == voltage) - { - LOG_BUG(ERRMSG_INVALID_PARAMETER, __FUNCTION__); + if (NULL == voltage) { + LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__); return ERRCODE_INVALID_PARAMETER; } #endif versaloon_buf[0] = VERSALOON_GET_TVCC; - if ((ERROR_OK != versaloon_send_command(1, &inlen)) || (inlen != 2)) - { + if ((ERROR_OK != versaloon_send_command(1, &inlen)) || (inlen != 2)) { LOG_ERROR(ERRMSG_FAILURE_OPERATION, "communicate with versaloon"); return ERRCODE_FAILURE_OPERATION; - } - else - { + } else { *voltage = versaloon_buf[0] + (versaloon_buf[1] << 8); return ERROR_OK; } @@ -395,4 +361,3 @@ RESULT versaloon_delay_us(uint16_t us) { return usbtodelay_delay(us & 0x7FFF); } - diff --git a/src/jtag/drivers/versaloon/versaloon.h b/src/jtag/drivers/versaloon/versaloon.h index 18ccbea424..9681225236 100644 --- a/src/jtag/drivers/versaloon/versaloon.h +++ b/src/jtag/drivers/versaloon/versaloon.h @@ -16,11 +16,11 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef __VERSALOON_H_INCLUDED__ #define __VERSALOON_H_INCLUDED__ -struct usart_status_t -{ +struct usart_status_t { uint32_t tx_buff_avail; uint32_t tx_buff_size; uint32_t rx_buff_avail; @@ -29,7 +29,7 @@ struct usart_status_t #include "usbtoxxx/usbtoxxx.h" -// GPIO pins +/* GPIO pins */ #define GPIO_SRST (1 << 0) #define GPIO_TRST (1 << 1) #define GPIO_USR1 (1 << 2) @@ -39,61 +39,55 @@ struct usart_status_t #define GPIO_TDI (1 << 6) #define GPIO_RTCK (1 << 7) #define GPIO_TMS (1 << 8) -struct interface_gpio_t -{ - RESULT (*init)(uint8_t interface_index); - RESULT (*fini)(uint8_t interface_index); - RESULT (*config)(uint8_t interface_index, uint32_t pin_mask, uint32_t io, - uint32_t pull_en_mask, uint32_t input_pull_mask); - RESULT (*out)(uint8_t interface_index, uint32_t pin_mask, uint32_t value); - RESULT (*in)(uint8_t interface_index, uint32_t pin_mask, uint32_t *value); + +struct interface_gpio_t { + RESULT(*init)(uint8_t interface_index); + RESULT(*fini)(uint8_t interface_index); + RESULT(*config)(uint8_t interface_index, uint32_t pin_mask, uint32_t io, + uint32_t pull_en_mask, uint32_t input_pull_mask); + RESULT(*out)(uint8_t interface_index, uint32_t pin_mask, uint32_t value); + RESULT(*in)(uint8_t interface_index, uint32_t pin_mask, uint32_t *value); }; -struct interface_delay_t -{ - RESULT (*delayms)(uint16_t ms); - RESULT (*delayus)(uint16_t us); +struct interface_delay_t { + RESULT(*delayms)(uint16_t ms); + RESULT(*delayus)(uint16_t us); }; -struct interface_swd_t -{ - RESULT (*init)(uint8_t interface_index); - RESULT (*fini)(uint8_t interface_index); - RESULT (*config)(uint8_t interface_index, uint8_t trn, uint16_t retry, +struct interface_swd_t { + RESULT(*init)(uint8_t interface_index); + RESULT(*fini)(uint8_t interface_index); + RESULT(*config)(uint8_t interface_index, uint8_t trn, uint16_t retry, uint16_t dly); - RESULT (*seqout)(uint8_t interface_index, uint8_t *data, uint16_t bitlen); - RESULT (*seqin)(uint8_t interface_index, uint8_t *data, uint16_t bitlen); - RESULT (*transact)(uint8_t interface_index, uint8_t request, + RESULT(*seqout)(uint8_t interface_index, uint8_t *data, uint16_t bitlen); + RESULT(*seqin)(uint8_t interface_index, uint8_t *data, uint16_t bitlen); + RESULT(*transact)(uint8_t interface_index, uint8_t request, uint32_t *data, uint8_t *ack); }; -struct interface_jtag_raw_t -{ - RESULT (*init)(uint8_t interface_index); - RESULT (*fini)(uint8_t interface_index); - RESULT (*config)(uint8_t interface_index, uint32_t kHz); - RESULT (*execute)(uint8_t interface_index, uint8_t* tdi, uint8_t* tms, +struct interface_jtag_raw_t { + RESULT(*init)(uint8_t interface_index); + RESULT(*fini)(uint8_t interface_index); + RESULT(*config)(uint8_t interface_index, uint32_t kHz); + RESULT(*execute)(uint8_t interface_index, uint8_t *tdi, uint8_t *tms, uint8_t *tdo, uint32_t bitlen); }; -struct interface_target_voltage_t -{ - RESULT (*get)(uint16_t *voltage); - RESULT (*set)(uint16_t voltage); +struct interface_target_voltage_t { + RESULT(*get)(uint16_t *voltage); + RESULT(*set)(uint16_t voltage); }; -struct versaloon_adaptors_t -{ +struct versaloon_adaptors_t { struct interface_target_voltage_t target_voltage; struct interface_gpio_t gpio; struct interface_delay_t delay; struct interface_swd_t swd; struct interface_jtag_raw_t jtag_raw; - RESULT (*peripheral_commit)(void); + RESULT(*peripheral_commit)(void); }; -struct versaloon_usb_setting_t -{ +struct versaloon_usb_setting_t { uint16_t vid; uint16_t pid; uint8_t ep_out; @@ -104,10 +98,9 @@ struct versaloon_usb_setting_t uint16_t buf_size; }; -struct versaloon_interface_t -{ - RESULT (*init)(void); - RESULT (*fini)(void); +struct versaloon_interface_t { + RESULT(*init)(void); + RESULT(*fini)(void); struct versaloon_adaptors_t adaptors; struct versaloon_usb_setting_t usb_setting; }; diff --git a/src/jtag/drivers/versaloon/versaloon_include.h b/src/jtag/drivers/versaloon/versaloon_include.h index eacb2e6495..9401bc6ac4 100644 --- a/src/jtag/drivers/versaloon/versaloon_include.h +++ b/src/jtag/drivers/versaloon/versaloon_include.h @@ -16,8 +16,9 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -// This file is used to include different header and macros -// according to different platform + +/* This file is used to include different header and macros */ +/* according to different platform */ #include #include #include "usb_common.h" @@ -31,7 +32,7 @@ #define RESULT int #define LOG_BUG LOG_ERROR -// Common error messages +/* Common error messages */ #define ERRMSG_NOT_ENOUGH_MEMORY "Lack of memory." #define ERRCODE_NOT_ENOUGH_MEMORY ERROR_FAIL @@ -52,38 +53,38 @@ #define ERRMSG_FAILURE_OPERATION_MESSAGE "Fail to %s, %s" #define ERRCODE_FAILURE_OPERATION ERROR_FAIL -#define GET_U16_MSBFIRST(p) ( ((*((uint8_t *)(p) + 0)) << 8) | \ - ((*((uint8_t *)(p) + 1)) << 0)) -#define GET_U32_MSBFIRST(p) ( ((*((uint8_t *)(p) + 0)) << 24) | \ - ((*((uint8_t *)(p) + 1)) << 16) | \ - ((*((uint8_t *)(p) + 2)) << 8) | \ - ((*((uint8_t *)(p) + 3)) << 0)) -#define GET_U16_LSBFIRST(p) ( ((*((uint8_t *)(p) + 0)) << 0) | \ - ((*((uint8_t *)(p) + 1)) << 8)) -#define GET_U32_LSBFIRST(p) ( ((*((uint8_t *)(p) + 0)) << 0) | \ - ((*((uint8_t *)(p) + 1)) << 8) | \ - ((*((uint8_t *)(p) + 2)) << 16) | \ - ((*((uint8_t *)(p) + 3)) << 24)) +#define GET_U16_MSBFIRST(p) (((*((uint8_t *)(p) + 0)) << 8) | \ + ((*((uint8_t *)(p) + 1)) << 0)) +#define GET_U32_MSBFIRST(p) (((*((uint8_t *)(p) + 0)) << 24) | \ + ((*((uint8_t *)(p) + 1)) << 16) | \ + ((*((uint8_t *)(p) + 2)) << 8) | \ + ((*((uint8_t *)(p) + 3)) << 0)) +#define GET_U16_LSBFIRST(p) (((*((uint8_t *)(p) + 0)) << 0) | \ + ((*((uint8_t *)(p) + 1)) << 8)) +#define GET_U32_LSBFIRST(p) (((*((uint8_t *)(p) + 0)) << 0) | \ + ((*((uint8_t *)(p) + 1)) << 8) | \ + ((*((uint8_t *)(p) + 2)) << 16) | \ + ((*((uint8_t *)(p) + 3)) << 24)) #define SET_U16_MSBFIRST(p, v) \ - do{\ + do {\ *((uint8_t *)(p) + 0) = (((uint16_t)(v)) >> 8) & 0xFF;\ *((uint8_t *)(p) + 1) = (((uint16_t)(v)) >> 0) & 0xFF;\ } while (0) #define SET_U32_MSBFIRST(p, v) \ - do{\ + do {\ *((uint8_t *)(p) + 0) = (((uint32_t)(v)) >> 24) & 0xFF;\ *((uint8_t *)(p) + 1) = (((uint32_t)(v)) >> 16) & 0xFF;\ *((uint8_t *)(p) + 2) = (((uint32_t)(v)) >> 8) & 0xFF;\ *((uint8_t *)(p) + 3) = (((uint32_t)(v)) >> 0) & 0xFF;\ } while (0) #define SET_U16_LSBFIRST(p, v) \ - do{\ + do {\ *((uint8_t *)(p) + 0) = (((uint16_t)(v)) >> 0) & 0xFF;\ *((uint8_t *)(p) + 1) = (((uint16_t)(v)) >> 8) & 0xFF;\ } while (0) #define SET_U32_LSBFIRST(p, v) \ - do{\ + do {\ *((uint8_t *)(p) + 0) = (((uint32_t)(v)) >> 0) & 0xFF;\ *((uint8_t *)(p) + 1) = (((uint32_t)(v)) >> 8) & 0xFF;\ *((uint8_t *)(p) + 2) = (((uint32_t)(v)) >> 16) & 0xFF;\ diff --git a/src/jtag/drivers/versaloon/versaloon_internal.h b/src/jtag/drivers/versaloon/versaloon_internal.h index c7c73b60ce..cba4dacd7e 100644 --- a/src/jtag/drivers/versaloon/versaloon_internal.h +++ b/src/jtag/drivers/versaloon/versaloon_internal.h @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef __VERSALOON_INTERNAL_H_INCLUDED__ #define __VERSALOON_INTERNAL_H_INCLUDED__ @@ -37,8 +38,8 @@ #define VERSALOON_TIMEOUT 5000 #define VERSALOON_TIMEOUT_LONG 60000 -// USB Commands -// Common Commands +/* USB Commands */ +/* Common Commands */ #define VERSALOON_COMMON_CMD_START 0x00 #define VERSALOON_COMMON_CMD_END 0x0F @@ -52,40 +53,34 @@ #define VERSALOON_FW_UPDATE 0x0F #define VERSALOON_FW_UPDATE_KEY 0xAA -// MCU Command +/* MCU Command */ #define VERSALOON_MCU_CMD_START 0x10 #define VERSALOON_MCU_CMD_END 0x1F -// USB_TO_XXX Command +/* USB_TO_XXX Command */ #define VERSALOON_USB_TO_XXX_CMD_START 0x20 #define VERSALOON_USB_TO_XXX_CMD_END 0x7F -// VSLLink Command +/* VSLLink Command */ #define VERSALOON_VSLLINK_CMD_START 0x80 #define VERSALOON_VSLLINK_CMD_END 0xFF - - -// Mass-product +/* Mass-product */ #define MP_OK 0x00 #define MP_FAIL 0x01 #define MP_ISSP 0x11 - - -// pending struct +/* pending struct */ #define VERSALOON_MAX_PENDING_NUMBER 4096 -typedef RESULT (*versaloon_callback_t)(void *, uint8_t *, uint8_t *); -struct versaloon_want_pos_t -{ +typedef RESULT(*versaloon_callback_t)(void *, uint8_t *, uint8_t *); +struct versaloon_want_pos_t { uint16_t offset; uint16_t size; uint8_t *buff; struct versaloon_want_pos_t *next; }; -struct versaloon_pending_t -{ +struct versaloon_pending_t { uint8_t type; uint8_t cmd; uint16_t want_data_pos; @@ -99,14 +94,14 @@ struct versaloon_pending_t versaloon_callback_t callback; }; extern struct versaloon_pending_t \ - versaloon_pending[VERSALOON_MAX_PENDING_NUMBER]; + versaloon_pending[VERSALOON_MAX_PENDING_NUMBER]; extern uint16_t versaloon_pending_idx; void versaloon_set_pending_id(uint32_t id); void versaloon_set_callback(versaloon_callback_t callback); -void versaloon_set_extra_data(void * p); +void versaloon_set_extra_data(void *p); RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff); RESULT versaloon_add_pending(uint8_t type, uint8_t cmd, uint16_t actual_szie, - uint16_t want_pos, uint16_t want_size, uint8_t *buffer, uint8_t collect); + uint16_t want_pos, uint16_t want_size, uint8_t *buffer, uint8_t collect); void versaloon_free_want_pos(void); RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen); @@ -115,4 +110,3 @@ extern uint8_t *versaloon_cmd_buf; extern uint16_t versaloon_buf_size; #endif /* __VERSALOON_INTERNAL_H_INCLUDED__ */ - diff --git a/src/jtag/drivers/vsllink.c b/src/jtag/drivers/vsllink.c index fe7bdb2414..1c0c3e1eee 100644 --- a/src/jtag/drivers/vsllink.c +++ b/src/jtag/drivers/vsllink.c @@ -38,18 +38,18 @@ static int vsllink_tms_offset; struct pending_scan_result { int src_offset; int dest_offset; - int length; /* Number of bits to read */ - struct scan_command *command; /* Corresponding scan command */ + int length; /* Number of bits to read */ + struct scan_command *command; /* Corresponding scan command */ uint8_t *ack; uint8_t *buffer; - bool last; /* indicate the last scan pending */ + bool last; /* indicate the last scan pending */ }; #define MAX_PENDING_SCAN_RESULTS 256 static int pending_scan_results_length; static struct pending_scan_result - pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS]; + pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS]; /* Queue command functions */ static void vsllink_end_state(tap_state_t state); @@ -59,7 +59,7 @@ static void vsllink_tms(int num_bits, const uint8_t *bits); static void vsllink_runtest(int num_cycles); static void vsllink_stableclocks(int num_cycles, int tms); static void vsllink_scan(bool ir_scan, enum scan_type type, - uint8_t *buffer, int scan_size, struct scan_command *command); + uint8_t *buffer, int scan_size, struct scan_command *command); static void vsllink_reset(int trst, int srst); /* VSLLink tap buffer functions */ @@ -105,9 +105,8 @@ static int vsllink_execute_queue(void) switch (cmd->type) { case JTAG_RUNTEST: DEBUG_JTAG_IO("runtest %i cycles, end in %s", - cmd->cmd.runtest->num_cycles, - tap_state_name(cmd->cmd.runtest - ->end_state)); + cmd->cmd.runtest->num_cycles, + tap_state_name(cmd->cmd.runtest->end_state)); vsllink_end_state(cmd->cmd.runtest->end_state); vsllink_runtest(cmd->cmd.runtest->num_cycles); @@ -115,24 +114,18 @@ static int vsllink_execute_queue(void) case JTAG_TLR_RESET: DEBUG_JTAG_IO("statemove end in %s", - tap_state_name(cmd->cmd.statemove - ->end_state)); + tap_state_name(cmd->cmd.statemove->end_state)); - vsllink_end_state(cmd->cmd.statemove - ->end_state); + vsllink_end_state(cmd->cmd.statemove->end_state); vsllink_state_move(); break; case JTAG_PATHMOVE: DEBUG_JTAG_IO("pathmove: %i states, end in %s", - cmd->cmd.pathmove->num_states, - tap_state_name(cmd->cmd.pathmove - ->path[cmd->cmd.pathmove - ->num_states - 1])); - - vsllink_path_move( - cmd->cmd.pathmove->num_states, - cmd->cmd.pathmove->path); + cmd->cmd.pathmove->num_states, + tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1])); + + vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path); break; case JTAG_SCAN: @@ -141,27 +134,25 @@ static int vsllink_execute_queue(void) vsllink_end_state(cmd->cmd.scan->end_state); scan_size = jtag_build_buffer( - cmd->cmd.scan, &buffer); + cmd->cmd.scan, &buffer); if (cmd->cmd.scan->ir_scan) DEBUG_JTAG_IO( - "JTAG Scan write IR(%d bits), " - "end in %s:", - scan_size, - tap_state_name(cmd->cmd.scan - ->end_state)); + "JTAG Scan write IR(%d bits), " + "end in %s:", + scan_size, + tap_state_name(cmd->cmd.scan->end_state)); else DEBUG_JTAG_IO( - "JTAG Scan write DR(%d bits), " - "end in %s:", - scan_size, - tap_state_name(cmd->cmd.scan - ->end_state)); + "JTAG Scan write DR(%d bits), " + "end in %s:", + scan_size, + tap_state_name(cmd->cmd.scan->end_state)); #ifdef _DEBUG_JTAG_IO_ vsllink_debug_buffer(buffer, - DIV_ROUND_UP(scan_size, 8)); + DIV_ROUND_UP(scan_size, 8)); #endif type = jtag_scan_type(cmd->cmd.scan); @@ -193,7 +184,7 @@ static int vsllink_execute_queue(void) case JTAG_STABLECLOCKS: DEBUG_JTAG_IO("add %d clocks", - cmd->cmd.stableclocks->num_cycles); + cmd->cmd.stableclocks->num_cycles); switch (tap_get_state()) { case TAP_RESET: @@ -210,29 +201,28 @@ static int vsllink_execute_queue(void) /* else, tms should be '0' */ scan_size = 0; break; - /* above stable states are OK */ + /* above stable states are OK */ default: - LOG_ERROR("jtag_add_clocks() " - "in non-stable state \"%s\"", - tap_state_name(tap_get_state()) - ); - exit(-1); + LOG_ERROR("jtag_add_clocks() " + "in non-stable state \"%s\"", + tap_state_name(tap_get_state()) + ); + exit(-1); } - vsllink_stableclocks(cmd->cmd.stableclocks - ->num_cycles, scan_size); + vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size); break; - case JTAG_TMS: - DEBUG_JTAG_IO("add %d jtag tms", - cmd->cmd.tms->num_bits); + case JTAG_TMS: + DEBUG_JTAG_IO("add %d jtag tms", + cmd->cmd.tms->num_bits); - vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits); - break; + vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits); + break; - default: - LOG_ERROR("BUG: unknown JTAG command type " - "encountered: %d", cmd->type); - exit(-1); + default: + LOG_ERROR("BUG: unknown JTAG command type " + "encountered: %d", cmd->type); + exit(-1); } cmd = cmd->next; } @@ -262,18 +252,15 @@ static int vsllink_speed_div(int jtag_speed, int *khz) static void vsllink_free_buffer(void) { - if (tdi_buffer != NULL) - { + if (tdi_buffer != NULL) { free(tdi_buffer); tdi_buffer = NULL; } - if (tdo_buffer != NULL) - { + if (tdo_buffer != NULL) { free(tdo_buffer); tdo_buffer = NULL; } - if (tms_buffer != NULL) - { + if (tms_buffer != NULL) { free(tms_buffer); tms_buffer = NULL; } @@ -298,33 +285,29 @@ static int vsllink_init(void) { vsllink_handle = vsllink_usb_open(); if (vsllink_handle == 0) { - LOG_ERROR("Can't find USB JTAG Interface!"\ - "Please check connection and permissions."); + LOG_ERROR("Can't find USB JTAG Interface!" \ + "Please check connection and permissions."); return ERROR_JTAG_INIT_FAILED; } LOG_DEBUG("vsllink found on %04X:%04X", - versaloon_interface.usb_setting.vid, - versaloon_interface.usb_setting.pid); + versaloon_interface.usb_setting.vid, + versaloon_interface.usb_setting.pid); versaloon_usb_device_handle = vsllink_handle->usb_handle; if (ERROR_OK != versaloon_interface.init()) - { return ERROR_FAIL; - } - if (versaloon_interface.usb_setting.buf_size < 32) - { + if (versaloon_interface.usb_setting.buf_size < 32) { versaloon_interface.fini(); return ERROR_FAIL; } - // malloc buffer size for tap + /* malloc buffer size for tap */ tap_buffer_size = versaloon_interface.usb_setting.buf_size - 32; vsllink_free_buffer(); tdi_buffer = (uint8_t *)malloc(tap_buffer_size); tdo_buffer = (uint8_t *)malloc(tap_buffer_size); tms_buffer = (uint8_t *)malloc(tap_buffer_size); - if ((NULL == tdi_buffer) || (NULL == tdo_buffer) || (NULL == tms_buffer)) - { + if ((NULL == tdi_buffer) || (NULL == tdo_buffer) || (NULL == tms_buffer)) { vsllink_quit(); return ERROR_FAIL; } @@ -333,19 +316,17 @@ static int vsllink_init(void) versaloon_interface.adaptors.jtag_raw.config(0, jtag_get_speed_khz()); versaloon_interface.adaptors.gpio.init(0); versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST, - GPIO_TRST, GPIO_SRST, GPIO_SRST); + GPIO_TRST, GPIO_SRST, GPIO_SRST); if (ERROR_OK != versaloon_interface.adaptors.peripheral_commit()) - { return ERROR_FAIL; - } vsllink_reset(0, 0); vsllink_tap_init(); return ERROR_OK; } -/***************************************************************************/ -/* Queue command implementations */ +/************************************************************************** + * Queue command implementations */ static void vsllink_end_state(tap_state_t state) { @@ -362,9 +343,9 @@ static void vsllink_state_move(void) { int i; uint8_t tms_scan = tap_get_tms_path(tap_get_state(), - tap_get_end_state()); + tap_get_end_state()); uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), - tap_get_end_state()); + tap_get_end_state()); for (i = 0; i < tms_scan_bits; i++) vsllink_tap_append_step((tms_scan >> i) & 1, 0); @@ -381,8 +362,8 @@ static void vsllink_path_move(int num_states, tap_state_t *path) vsllink_tap_append_step(1, 0); else { LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", - tap_state_name(tap_get_state()), - tap_state_name(path[i])); + tap_state_name(tap_get_state()), + tap_state_name(path[i])); exit(-1); } @@ -394,9 +375,8 @@ static void vsllink_path_move(int num_states, tap_state_t *path) static void vsllink_tms(int num_bits, const uint8_t *bits) { - for (int i = 0; i < num_bits; i++) { + for (int i = 0; i < num_bits; i++) vsllink_tap_append_step((bits[i / 8] >> (i % 8)) & 1, 0); - } } static void vsllink_stableclocks(int num_cycles, int tms) @@ -419,15 +399,15 @@ static void vsllink_runtest(int num_cycles) vsllink_stableclocks(num_cycles, 0); - // post-process - // set end_state + /* post-process */ + /* set end_state */ vsllink_end_state(saved_end_state); if (tap_get_end_state() != tap_get_end_state()) vsllink_state_move(); } static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, - int scan_size, struct scan_command *command) + int scan_size, struct scan_command *command) { tap_state_t saved_end_state; @@ -471,9 +451,8 @@ static void vsllink_reset(int trst, int srst) COMMAND_HANDLER(vsllink_handle_usb_vid_command) { - if (CMD_ARGC != 1) { + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - } COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], versaloon_interface.usb_setting.vid); @@ -482,9 +461,8 @@ COMMAND_HANDLER(vsllink_handle_usb_vid_command) COMMAND_HANDLER(vsllink_handle_usb_pid_command) { - if (CMD_ARGC != 1) { + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - } COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], versaloon_interface.usb_setting.pid); return ERROR_OK; @@ -492,9 +470,8 @@ COMMAND_HANDLER(vsllink_handle_usb_pid_command) COMMAND_HANDLER(vsllink_handle_usb_bulkin_command) { - if (CMD_ARGC != 1) { + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - } COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], versaloon_interface.usb_setting.ep_in); @@ -506,9 +483,8 @@ COMMAND_HANDLER(vsllink_handle_usb_bulkin_command) COMMAND_HANDLER(vsllink_handle_usb_bulkout_command) { - if (CMD_ARGC != 1) { + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - } COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], versaloon_interface.usb_setting.ep_out); @@ -520,17 +496,16 @@ COMMAND_HANDLER(vsllink_handle_usb_bulkout_command) COMMAND_HANDLER(vsllink_handle_usb_interface_command) { - if (CMD_ARGC != 1) { + if (CMD_ARGC != 1) return ERROR_COMMAND_SYNTAX_ERROR; - } COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], versaloon_interface.usb_setting.interface); return ERROR_OK; } -/***************************************************************************/ -/* VSLLink tap functions */ +/************************************************************************** + * VSLLink tap functions */ static void vsllink_tap_init(void) { @@ -542,7 +517,7 @@ static void vsllink_tap_init(void) static void vsllink_tap_ensure_pending(int scans) { int available_scans = - MAX_PENDING_SCAN_RESULTS - pending_scan_results_length; + MAX_PENDING_SCAN_RESULTS - pending_scan_results_length; if (scans > available_scans) vsllink_tap_execute(); @@ -572,7 +547,7 @@ static void vsllink_tap_append_step(int tms, int tdi) } static void vsllink_tap_append_scan(int length, uint8_t *buffer, - struct scan_command *command) + struct scan_command *command) { struct pending_scan_result *pending_scan_result; int len_tmp, len_all, i; @@ -581,8 +556,8 @@ static void vsllink_tap_append_scan(int length, uint8_t *buffer, while (len_all < length) { vsllink_tap_ensure_pending(1); pending_scan_result = - &pending_scan_results_buffer[ - pending_scan_results_length]; + &pending_scan_results_buffer[ + pending_scan_results_length]; if ((length - len_all) > (tap_buffer_size * 8 - tap_length)) { /* Use all memory available @@ -602,9 +577,9 @@ static void vsllink_tap_append_scan(int length, uint8_t *buffer, for (i = 0; i < len_tmp; i++) { vsllink_tap_append_step(((len_all + i) < length-1 - ? 0 : 1), - (buffer[(len_all + i)/8] - >> ((len_all + i)%8)) & 1); + ? 0 : 1), + (buffer[(len_all + i)/8] + >> ((len_all + i)%8)) & 1); } len_all += len_tmp; @@ -619,7 +594,7 @@ static int vsllink_jtag_execute(void) if (tap_length <= 0) return ERROR_OK; - versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer, + versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer, tdo_buffer, tap_length); result = versaloon_interface.adaptors.peripheral_commit(); @@ -644,12 +619,12 @@ static int vsllink_jtag_execute(void) "JTAG scan read(%d bits, from src %d bits to dest %d bits):", length, src_first, dest_first); vsllink_debug_buffer(buffer + dest_first / 8, - DIV_ROUND_UP(length, 7)); + DIV_ROUND_UP(length, 7)); #endif if (last) { if (jtag_read_buffer(buffer, command) - != ERROR_OK) { + != ERROR_OK) { vsllink_tap_init(); return ERROR_JTAG_QUEUE_FAILED; } @@ -673,22 +648,20 @@ static int vsllink_tap_execute(void) return vsllink_jtag_execute(); } -/*****************************************************************************/ -/* VSLLink USB low-level functions */ +/**************************************************************************** + * VSLLink USB low-level functions */ static uint8_t usb_check_string(usb_dev_handle *usb, uint8_t stringidx, - char * string, char * buff, uint16_t buf_size) + char *string, char *buff, uint16_t buf_size) { int len; uint8_t alloced = 0; uint8_t ret = 1; - if (NULL == buff) - { + if (NULL == buff) { buf_size = 256; - buff = (char*)malloc(buf_size); - if (NULL == buff) - { + buff = (char *)malloc(buf_size); + if (NULL == buff) { ret = 0; goto free_and_return; } @@ -697,31 +670,28 @@ static uint8_t usb_check_string(usb_dev_handle *usb, uint8_t stringidx, strcpy(buff, ""); len = usb_get_string_simple(usb, stringidx, (char *)buff, buf_size); - if ((len < 0) || (len != ((int)strlen((const char *)buff)))) - { + if ((len < 0) || (len != ((int)strlen((const char *)buff)))) { ret = 0; goto free_and_return; } buff[len] = '\0'; - if ((string != NULL) && strcmp((const char *)buff, string)) - { + if ((string != NULL) && strcmp((const char *)buff, string)) { ret = 0; goto free_and_return; } free_and_return: - if (alloced && (buff != NULL)) - { + if (alloced && (buff != NULL)) { free(buff); buff = NULL; } return ret; } -static usb_dev_handle* find_usb_device(uint16_t VID, uint16_t PID, - uint8_t interface, int8_t serialindex, char *serialstring, - int8_t productindex, char *productstring) +static usb_dev_handle *find_usb_device(uint16_t VID, uint16_t PID, + uint8_t interface, int8_t serialindex, char *serialstring, + int8_t productindex, char *productstring) { usb_dev_handle *dev_handle = NULL; struct usb_bus *busses; @@ -733,47 +703,39 @@ static usb_dev_handle* find_usb_device(uint16_t VID, uint16_t PID, usb_find_devices(); busses = usb_get_busses(); - for (bus = busses; bus; bus = bus->next) - { - for (dev = bus->devices; dev; dev = dev->next) - { + for (bus = busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { if ((dev->descriptor.idVendor == VID) - && (dev->descriptor.idProduct == PID)) - { + && (dev->descriptor.idProduct == PID)) { dev_handle = usb_open(dev); - if (NULL == dev_handle) - { + if (NULL == dev_handle) { LOG_ERROR("failed to open %04X:%04X, %s", VID, PID, - usb_strerror()); + usb_strerror()); continue; } - // check description string + /* check description string */ if (((productstring != NULL) && (productindex >= 0) && !usb_check_string(dev_handle, productindex, - productstring, NULL, 0)) - || ((serialstring != NULL) && (serialindex >= 0) - && !usb_check_string(dev_handle, serialindex, - serialstring, NULL, 0))) - { + productstring, NULL, 0)) + || ((serialstring != NULL) && (serialindex >= 0) + && !usb_check_string(dev_handle, serialindex, + serialstring, NULL, 0))) { usb_close(dev_handle); dev_handle = NULL; continue; } - if (usb_claim_interface(dev_handle, interface) != 0) - { + if (usb_claim_interface(dev_handle, interface) != 0) { LOG_ERROR(ERRMSG_FAILURE_OPERATION_MESSAGE, - "claim interface", usb_strerror()); + "claim interface", usb_strerror()); usb_close(dev_handle); dev_handle = NULL; continue; } if (dev_handle != NULL) - { return dev_handle; - } } } } @@ -788,9 +750,9 @@ static struct vsllink *vsllink_usb_open(void) struct usb_dev_handle *dev; dev = find_usb_device(versaloon_interface.usb_setting.vid, - versaloon_interface.usb_setting.pid, - versaloon_interface.usb_setting.interface, - 0, NULL, 2, "Versaloon"); + versaloon_interface.usb_setting.pid, + versaloon_interface.usb_setting.interface, + 0, NULL, 2, "Versaloon"); if (NULL == dev) return NULL; @@ -807,7 +769,7 @@ static void vsllink_usb_close(struct vsllink *vsllink) versaloon_interface.usb_setting.interface); if (ret != 0) { LOG_ERROR("fail to release interface %d, %d returned", - versaloon_interface.usb_setting.interface, ret); + versaloon_interface.usb_setting.interface, ret); exit(-1); } @@ -839,7 +801,7 @@ static void vsllink_debug_buffer(uint8_t *buffer, int length) LOG_DEBUG("%s", line); } } -#endif /* _DEBUG_JTAG_IO_ */ +#endif /* _DEBUG_JTAG_IO_ */ static const struct command_registration vsllink_command_handlers[] = { { diff --git a/src/jtag/minidriver/minidriver_imp.h b/src/jtag/minidriver/minidriver_imp.h index 98d87fe7eb..9f1bf26de3 100644 --- a/src/jtag/minidriver/minidriver_imp.h +++ b/src/jtag/minidriver/minidriver_imp.h @@ -18,13 +18,14 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef MINIDRIVER_IMP_H #define MINIDRIVER_IMP_H #include -static inline void jtag_add_dr_out(struct jtag_tap* tap, - int num_fields, const int* num_bits, const uint32_t* value, +static inline void jtag_add_dr_out(struct jtag_tap *tap, + int num_fields, const int *num_bits, const uint32_t *value, tap_state_t end_state) { cmd_queue_cur_state = end_state; @@ -36,8 +37,7 @@ static inline void jtag_add_dr_out(struct jtag_tap* tap, #define jtag_add_callback(callback, in) interface_jtag_add_callback(callback, in) -#define jtag_add_callback4(callback, in, data1, data2, data3) interface_jtag_add_callback4(callback, in, data1, data2, data3) - - +#define jtag_add_callback4(callback, in, data1, data2, data3) \ + interface_jtag_add_callback4(callback, in, data1, data2, data3) -#endif // MINIDRIVER_IMP_H +#endif /* MINIDRIVER_IMP_H */ diff --git a/src/jtag/minidummy/jtag_minidriver.h b/src/jtag/minidummy/jtag_minidriver.h index d50a80040a..e51b6a11ab 100644 --- a/src/jtag/minidummy/jtag_minidriver.h +++ b/src/jtag/minidummy/jtag_minidriver.h @@ -17,10 +17,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ - - - -static __inline__ void interface_jtag_add_dr_out_core(struct jtag_tap *targettap, +static inline void interface_jtag_add_dr_out_core(struct jtag_tap *targettap, int num_fields, const int *num_bits, const uint32_t *value, @@ -29,7 +26,7 @@ static __inline__ void interface_jtag_add_dr_out_core(struct jtag_tap *targettap /* synchronously do the operation here */ } -static __inline__ void interface_jtag_add_dr_out(struct jtag_tap *targettap, +static inline void interface_jtag_add_dr_out(struct jtag_tap *targettap, int num_fields, const int *num_bits, const uint32_t *value, @@ -40,4 +37,5 @@ static __inline__ void interface_jtag_add_dr_out(struct jtag_tap *targettap, #define interface_jtag_add_callback(callback, in) callback(in) -#define interface_jtag_add_callback4(callback, in, data1, data2, data3) jtag_set_error(callback(in, data1, data2, data3)) +#define interface_jtag_add_callback4(callback, in, data1, data2, data3) \ + jtag_set_error(callback(in, data1, data2, data3)) diff --git a/src/jtag/minidummy/minidummy.c b/src/jtag/minidummy/minidummy.c index 4dbcff6d35..12a658c191 100644 --- a/src/jtag/minidummy/minidummy.c +++ b/src/jtag/minidummy/minidummy.c @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -25,8 +26,7 @@ #include #include -struct jtag_interface minidummy_interface = -{ +struct jtag_interface minidummy_interface = { .name = "minidummy", .execute_queue = NULL, .speed = NULL, @@ -46,29 +46,32 @@ int interface_jtag_execute_queue(void) return ERROR_OK; } -int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field *fields, tap_state_t state) +int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field *fields, + tap_state_t state) { /* synchronously do the operation here */ return ERROR_OK; - } -int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state) +int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, + uint8_t *in_bits, tap_state_t state) { /* synchronously do the operation here */ return ERROR_OK; } -int interface_jtag_add_dr_scan(struct jtag_tap *active, int num_fields, const struct scan_field *fields, tap_state_t state) +int interface_jtag_add_dr_scan(struct jtag_tap *active, int num_fields, + const struct scan_field *fields, tap_state_t state) { /* synchronously do the operation here */ return ERROR_OK; } -int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state) +int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, + uint8_t *in_bits, tap_state_t state) { /* synchronously do the operation here */ @@ -118,19 +121,14 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path) tap_state_t cur_state = cmd_queue_cur_state; - while (num_states) - { + while (num_states) { if (tap_state_transition(cur_state, false) == path[state_count]) - { tms = 0; - } else if (tap_state_transition(cur_state, true) == path[state_count]) - { tms = 1; - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[state_count])); + else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(cur_state), tap_state_name(path[state_count])); exit(-1); } @@ -154,18 +152,20 @@ int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state return ERROR_OK; } -void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, const uint8_t *buffer, int little, int count) +void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, const uint8_t *buffer, + int little, int count) { int i; - for (i = 0; i < count; i++) - { + for (i = 0; i < count; i++) { embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little)); buffer += 4; } } -int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opcode, uint32_t * data, size_t count) +int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap *tap, uint32_t opcode, + uint32_t *data, size_t count) { - int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap * tap, uint32_t opcode, uint32_t * data, size_t count); + int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap *tap, \ + uint32_t opcode, uint32_t *data, size_t count); return arm11_run_instr_data_to_core_noack_inner_default(tap, opcode, data, count); } diff --git a/src/jtag/stlink/stlink_interface.c b/src/jtag/stlink/stlink_interface.c index 6a856945fd..9c0215f02d 100644 --- a/src/jtag/stlink/stlink_interface.c +++ b/src/jtag/stlink/stlink_interface.c @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif diff --git a/src/jtag/stlink/stlink_interface.h b/src/jtag/stlink/stlink_interface.h index b1bffc77bd..4b6949e42f 100644 --- a/src/jtag/stlink/stlink_interface.h +++ b/src/jtag/stlink/stlink_interface.h @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef _STLINK_INTERFACE_ #define _STLINK_INTERFACE_ diff --git a/src/jtag/stlink/stlink_layout.h b/src/jtag/stlink/stlink_layout.h index caae8284e2..c0c89f5707 100644 --- a/src/jtag/stlink/stlink_layout.h +++ b/src/jtag/stlink/stlink_layout.h @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef _STLINK_LAYOUT_H_ #define _STLINK_LAYOUT_H_ diff --git a/src/jtag/stlink/stlink_tcl.h b/src/jtag/stlink/stlink_tcl.h index 395d546d38..6cdb5b5b37 100644 --- a/src/jtag/stlink/stlink_tcl.h +++ b/src/jtag/stlink/stlink_tcl.h @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef _STLINK_TCL_ #define _STLINK_TCL_ diff --git a/src/jtag/stlink/stlink_transport.h b/src/jtag/stlink/stlink_transport.h index e5197dae3a..615cc89f48 100644 --- a/src/jtag/stlink/stlink_transport.h +++ b/src/jtag/stlink/stlink_transport.h @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef _STLINK_TRANSPORT_ #define _STLINK_TRANSPORT_ diff --git a/src/jtag/zy1000/jtag_minidriver.h b/src/jtag/zy1000/jtag_minidriver.h index 543fd9bbae..15d921838a 100644 --- a/src/jtag/zy1000/jtag_minidriver.h +++ b/src/jtag/zy1000/jtag_minidriver.h @@ -24,13 +24,13 @@ #if BUILD_ZY1000_MASTER #if BUILD_ECOSBOARD -#include // low level i/o -#include // low level i/o +#include /* low level i/o */ +#include /* low level i/o */ #define ZY1000_PEEK(a, b) HAL_READ_UINT32(a, b) #define ZY1000_POKE(a, b) HAL_WRITE_UINT32(a, b) #else -#define ZY1000_PEEK(a, b) do {b = *( ( volatile uint32_t *)(a) );} while (0) -#define ZY1000_POKE(a, b) do {*( ( volatile uint32_t *)(a) ) = b;} while (0) +#define ZY1000_PEEK(a, b) do {b = *((volatile uint32_t *)(a)); } while (0) +#define ZY1000_POKE(a, b) do {*((volatile uint32_t *)(a)) = b; } while (0) extern volatile void *zy1000_jtag_master; #define ZY1000_JTAG_BASE ((unsigned long)zy1000_jtag_master) #endif @@ -41,29 +41,26 @@ extern volatile void *zy1000_jtag_master; #define ZY1000_JTAG_BASE 0 extern void zy1000_tcpout(uint32_t address, uint32_t data); extern uint32_t zy1000_tcpin(uint32_t address); -#define ZY1000_PEEK(a, b) b=zy1000_tcpin(a) +#define ZY1000_PEEK(a, b) b = zy1000_tcpin(a) #define ZY1000_POKE(a, b) zy1000_tcpout(a, b) #endif - - #if BUILD_ZY1000_MASTER -// FIFO empty? -static __inline__ void waitIdle(void) +/* FIFO empty? */ +static inline void waitIdle(void) { uint32_t empty; - do - { + do { ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, empty); } while ((empty & 0x100) == 0); } -static __inline__ void zy1000_flush_readqueue(void) +static inline void zy1000_flush_readqueue(void) { /* Not used w/hardware fifo */ } -static __inline__ void zy1000_flush_callbackqueue(void) +static inline void zy1000_flush_callbackqueue(void) { /* Not used w/hardware fifo */ } @@ -71,16 +68,20 @@ static __inline__ void zy1000_flush_callbackqueue(void) extern void waitIdle(void); void zy1000_flush_readqueue(void); void zy1000_flush_callbackqueue(void); -void zy1000_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3); +void zy1000_jtag_add_callback4(jtag_callback_t callback, + jtag_callback_data_t data0, + jtag_callback_data_t data1, + jtag_callback_data_t data2, + jtag_callback_data_t data3); void zy1000_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0); #endif -static __inline__ void waitQueue(void) +static inline void waitQueue(void) { -// waitIdle(); +/* waitIdle(); */ } -static __inline__ void sampleShiftRegister(void) +static inline void sampleShiftRegister(void) { #if 0 uint32_t dummy; @@ -89,17 +90,16 @@ static __inline__ void sampleShiftRegister(void) #endif } -static __inline__ void setCurrentState(enum tap_state state) +static inline void setCurrentState(enum tap_state state) { uint32_t a; a = state; int repeat = 0; - if (state == TAP_RESET) - { - // The FPGA nor we know the current state of the CPU TAP - // controller. This will move it to TAP for sure. - // - // 5 should be enough here, 7 is what OpenOCD uses + if (state == TAP_RESET) { + /* The FPGA nor we know the current state of the CPU TAP */ + /* controller. This will move it to TAP for sure. */ + /* */ + /* 5 should be enough here, 7 is what OpenOCD uses */ repeat = 7; } waitQueue(); @@ -112,9 +112,12 @@ static __inline__ void setCurrentState(enum tap_state state) * Enter state and cause repeat transitions *out* of that state. So if the endState != state, then * the transition from state to endState counts as a transition out of state. */ -static __inline__ void shiftValueInner(const enum tap_state state, const enum tap_state endState, int repeat, uint32_t value) +static inline void shiftValueInner(const enum tap_state state, + const enum tap_state endState, + int repeat, + uint32_t value) { - uint32_t a,b; + uint32_t a, b; a = state; b = endState; waitQueue(); @@ -122,44 +125,42 @@ static __inline__ void shiftValueInner(const enum tap_state state, const enum ta ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value); #if 1 #if TEST_MANUAL() - if ((state == TAP_DRSHIFT) && (endState != TAP_DRSHIFT)) - { + if ((state == TAP_DRSHIFT) && (endState != TAP_DRSHIFT)) { int i; setCurrentState(state); - for (i = 0; i < repeat; i++) - { + for (i = 0; i < repeat; i++) { int tms; tms = 0; if ((i == repeat-1) && (state != endState)) - { tms = 1; - } - /* shift out value */ + /* shift out value */ waitIdle(); ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, (((value >> i)&1) << 1) | tms); } waitIdle(); ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0); waitIdle(); - //ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRSHIFT); // set this state and things break => expected - ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRPAUSE); // set this and things will work => expected. Not setting this is not sufficient to make things break. + /* ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRSHIFT); // set this state and things + * break => expected */ + ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRPAUSE); /* set this and things will + * work => expected. Not + * setting this is not + * sufficient to make things + * break. */ setCurrentState(endState); } else - { ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (repeat << 8) | (a << 4) | b); - } + #else /* fast version */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (repeat << 8) | (a << 4) | b); #endif #else /* maximum debug version */ - if ((repeat > 0) && ((state == TAP_DRSHIFT)||(state == TAP_SI))) - { + if ((repeat > 0) && ((state == TAP_DRSHIFT) || (state == TAP_SI))) { int i; /* sample shift register for every bit. */ - for (i = 0; i < repeat-1; i++) - { + for (i = 0; i < repeat-1; i++) { sampleShiftRegister(); ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value >> i); ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (1 << 8) | (a << 4) | a); @@ -167,8 +168,7 @@ static __inline__ void shiftValueInner(const enum tap_state state, const enum ta sampleShiftRegister(); ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value >> (repeat-1)); ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (1 << 8) | (a << 4) | b); - } else - { + } else { sampleShiftRegister(); ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (repeat << 8) | (a << 4) | b); } @@ -176,69 +176,62 @@ static __inline__ void shiftValueInner(const enum tap_state state, const enum ta #endif } - - -static __inline__ void interface_jtag_add_dr_out_core(struct jtag_tap *target_tap, - int num_fields, - const int *num_bits, - const uint32_t *value, - enum tap_state end_state) +static inline void interface_jtag_add_dr_out_core(struct jtag_tap *target_tap, + int num_fields, + const int *num_bits, + const uint32_t *value, + enum tap_state end_state) { enum tap_state pause_state = TAP_DRSHIFT; struct jtag_tap *tap, *nextTap; - for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap) - { + for (tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = nextTap) { nextTap = jtag_tap_next_enabled(tap); if (nextTap == NULL) - { pause_state = end_state; - } - if (tap == target_tap) - { + if (tap == target_tap) { int j; for (j = 0; j < (num_fields-1); j++) - { shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, num_bits[j], value[j]); - } shiftValueInner(TAP_DRSHIFT, pause_state, num_bits[j], value[j]); - } else - { + } else { /* program the scan field to 1 bit length, and ignore it's value */ shiftValueInner(TAP_DRSHIFT, pause_state, 1, 0); } } } -static __inline__ void interface_jtag_add_dr_out(struct jtag_tap *target_tap, - int num_fields, - const int *num_bits, - const uint32_t *value, - enum tap_state end_state) +static inline void interface_jtag_add_dr_out(struct jtag_tap *target_tap, + int num_fields, + const int *num_bits, + const uint32_t *value, + enum tap_state end_state) { int singletap = (jtag_tap_next_enabled(jtag_tap_next_enabled(NULL)) == NULL); - if ((singletap) && (num_fields == 3)) - { + if ((singletap) && (num_fields == 3)) { /* used by embeddedice_write_reg_inner() */ shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, num_bits[0], value[0]); shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, num_bits[1], value[1]); shiftValueInner(TAP_DRSHIFT, end_state, num_bits[2], value[2]); - } else if ((singletap) && (num_fields == 2)) - { + } else if ((singletap) && (num_fields == 2)) { /* used by arm7 code */ shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, num_bits[0], value[0]); shiftValueInner(TAP_DRSHIFT, end_state, num_bits[1], value[1]); } else - { interface_jtag_add_dr_out_core(target_tap, num_fields, num_bits, value, end_state); - } } #if BUILD_ZY1000_MASTER #define interface_jtag_add_callback(callback, in) callback(in) -#define interface_jtag_add_callback4(callback, in, data1, data2, data3) jtag_set_error(callback(in, data1, data2, data3)) +#define interface_jtag_add_callback4(callback, in, data1, data2, \ + data3) jtag_set_error(callback(in, data1, data2, data3)) #else #define interface_jtag_add_callback(callback, in) zy1000_jtag_add_callback(callback, in) -#define interface_jtag_add_callback4(callback, in, data1, data2, data3) zy1000_jtag_add_callback4(callback, in, data1, data2, data3) +#define interface_jtag_add_callback4(callback, in, data1, data2, data3) zy1000_jtag_add_callback4( \ + callback, \ + in, \ + data1, \ + data2, \ + data3) #endif diff --git a/src/jtag/zy1000/zy1000.c b/src/jtag/zy1000/zy1000.c index 22193abd2c..e61a618390 100644 --- a/src/jtag/zy1000/zy1000.c +++ b/src/jtag/zy1000/zy1000.c @@ -58,7 +58,7 @@ #if BUILD_ECOSBOARD #include "zy1000_version.h" -#include // low level i/o +#include /* low level i/o */ #include #ifdef CYGPKG_HAL_NIOS2 @@ -80,18 +80,14 @@ #define ZYLIN_KHZ 60000 #endif - /* The software needs to check if it's in RCLK mode or not */ -static bool zy1000_rclk = false; +static bool zy1000_rclk; static int zy1000_khz(int khz, int *jtag_speed) { if (khz == 0) - { *jtag_speed = 0; - } - else - { + else { int speed; /* Round speed up to nearest divisor. * @@ -110,11 +106,10 @@ static int zy1000_khz(int khz, int *jtag_speed) * 64000 / 6 = 10666 * */ - speed = (ZYLIN_KHZ + (khz -1)) / khz; - speed = (speed + 1 ) / 2; + speed = (ZYLIN_KHZ + (khz - 1)) / khz; + speed = (speed + 1) / 2; speed *= 2; - if (speed > 8190) - { + if (speed > 8190) { /* maximum dividend */ speed = 8190; } @@ -126,13 +121,9 @@ static int zy1000_khz(int khz, int *jtag_speed) static int zy1000_speed_div(int speed, int *khz) { if (speed == 0) - { *khz = 0; - } else - { *khz = ZYLIN_KHZ / speed; - } return ERROR_OK; } @@ -140,7 +131,7 @@ static int zy1000_speed_div(int speed, int *khz) static bool readPowerDropout(void) { uint32_t state; - // sample and clear power dropout + /* sample and clear power dropout */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x80); ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state); bool powerDropout; @@ -152,7 +143,7 @@ static bool readPowerDropout(void) static bool readSRST(void) { uint32_t state; - // sample and clear SRST sensing + /* sample and clear SRST sensing */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000040); ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state); bool srstAsserted; @@ -180,20 +171,15 @@ static void waitSRST(bool asserted) long total = 0; const char *mode = asserted ? "assert" : "deassert"; - for (;;) - { + for (;; ) { bool srstAsserted = readSRST(); - if ( (asserted && srstAsserted) || (!asserted && !srstAsserted) ) - { + if ((asserted && srstAsserted) || (!asserted && !srstAsserted)) { if (total > 1) - { LOG_USER("SRST took %dms to %s", (int)total, mode); - } break; } - if (first) - { + if (first) { first = false; start = timeval_ms(); } @@ -202,15 +188,13 @@ static void waitSRST(bool asserted) keep_alive(); - if (total > 5000) - { + if (total > 5000) { LOG_ERROR("SRST took too long to %s: %dms", mode, (int)total); break; } } } - void zy1000_reset(int trst, int srst) { LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst); @@ -222,11 +206,8 @@ void zy1000_reset(int trst, int srst) waitIdle(); if (!srst) - { ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000001); - } - else - { + else { /* Danger!!! if clk != 0 when in * idle in TAP_IDLE, reset halt on str912 will fail. */ @@ -236,31 +217,24 @@ void zy1000_reset(int trst, int srst) } if (!trst) - { ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000002); - } - else - { + else { /* assert reset */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000002); } - if (trst||(srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) - { + if (trst || (srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) { /* we're now in the RESET state until trst is deasserted */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_RESET); - } else - { + } else { /* We'll get RCLK failure when we assert TRST, so clear any false positives here */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400); } /* wait for srst to float back up */ - if ((!srst && ((jtag_get_reset_config() & RESET_TRST_PULLS_SRST) == 0))|| - (!srst && !trst && (jtag_get_reset_config() & RESET_TRST_PULLS_SRST))) - { + if ((!srst && ((jtag_get_reset_config() & RESET_TRST_PULLS_SRST) == 0)) || + (!srst && !trst && (jtag_get_reset_config() & RESET_TRST_PULLS_SRST))) waitSRST(false); - } } int zy1000_speed(int speed) @@ -270,19 +244,18 @@ int zy1000_speed(int speed) zy1000_rclk = false; - if (speed == 0) - { + if (speed == 0) { /*0 means RCLK*/ ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x100); zy1000_rclk = true; LOG_DEBUG("jtag_speed using RCLK"); - } - else - { - if (speed > 8190 || speed < 2) - { - LOG_USER("valid ZY1000 jtag_speed=[8190,2]. With divisor is %dkHz / even values between 8190-2, i.e. min %dHz, max %dMHz", - ZYLIN_KHZ, (ZYLIN_KHZ * 1000) / 8190, ZYLIN_KHZ / (2 * 1000)); + } else { + if (speed > 8190 || speed < 2) { + LOG_USER( + "valid ZY1000 jtag_speed=[8190,2]. With divisor is %dkHz / even values between 8190-2, i.e. min %dHz, max %dMHz", + ZYLIN_KHZ, + (ZYLIN_KHZ * 1000) / 8190, + ZYLIN_KHZ / (2 * 1000)); return ERROR_COMMAND_SYNTAX_ERROR; } @@ -298,34 +271,29 @@ int zy1000_speed(int speed) static bool savePower; - static void setPower(bool power) { savePower = power; if (power) - { ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x8); - } else - { + else ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x8); - } } COMMAND_HANDLER(handle_power_command) { - switch (CMD_ARGC) - { - case 1: { - bool enable; - COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable); - setPower(enable); - // fall through - } - case 0: - LOG_INFO("Target power %s", savePower ? "on" : "off"); - break; - default: - return ERROR_COMMAND_SYNTAX_ERROR; + switch (CMD_ARGC) { + case 1: { + bool enable; + COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable); + setPower(enable); + /* fall through */ + } + case 0: + LOG_INFO("Target power %s", savePower ? "on" : "off"); + break; + default: + return ERROR_COMMAND_SYNTAX_ERROR; } return ERROR_OK; @@ -333,7 +301,7 @@ COMMAND_HANDLER(handle_power_command) #if !BUILD_ZY1000_MASTER static char *tcp_server = "notspecified"; -static int jim_zy1000_server(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +static int jim_zy1000_server(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { if (argc != 2) return JIM_ERR; @@ -346,48 +314,36 @@ static int jim_zy1000_server(Jim_Interp *interp, int argc, Jim_Obj *const *argv) #if BUILD_ECOSBOARD /* Give TELNET a way to find out what version this is */ -static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { if ((argc < 1) || (argc > 3)) return JIM_ERR; const char *version_str = NULL; if (argc == 1) - { version_str = ZYLIN_OPENOCD_VERSION; - } else - { + else { const char *str = Jim_GetString(argv[1], NULL); const char *str2 = NULL; if (argc > 2) str2 = Jim_GetString(argv[2], NULL); if (strcmp("openocd", str) == 0) - { version_str = ZYLIN_OPENOCD; - } else if (strcmp("zy1000", str) == 0) - { version_str = ZYLIN_VERSION; - } else if (strcmp("date", str) == 0) - { version_str = ZYLIN_DATE; - } else if (strcmp("time", str) == 0) - { version_str = ZYLIN_TIME; - } - else if (strcmp("pcb", str) == 0) - { + else if (strcmp("pcb", str) == 0) { #ifdef CYGPKG_HAL_NIOS2 - version_str="c"; + version_str = "c"; #else - version_str="b"; + version_str = "b"; #endif } #ifdef CYGPKG_HAL_NIOS2 - else if (strcmp("fpga", str) == 0) - { + else if (strcmp("fpga", str) == 0) { /* return a list of 32 bit integers to describe the expected * and actual FPGA @@ -396,22 +352,24 @@ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv uint32_t id, timestamp; HAL_READ_UINT32(SYSID_BASE, id); HAL_READ_UINT32(SYSID_BASE+4, timestamp); - sprintf(fpga_id, "0x%08x 0x%08x 0x%08x 0x%08x", id, timestamp, SYSID_ID, SYSID_TIMESTAMP); + sprintf(fpga_id, + "0x%08x 0x%08x 0x%08x 0x%08x", + id, + timestamp, + SYSID_ID, + SYSID_TIMESTAMP); version_str = fpga_id; - if ((argc>2) && (strcmp("time", str2) == 0)) - { - time_t last_mod = timestamp; - char * t = ctime (&last_mod) ; - t[strlen(t)-1] = 0; - version_str = t; + if ((argc > 2) && (strcmp("time", str2) == 0)) { + time_t last_mod = timestamp; + char *t = ctime(&last_mod); + t[strlen(t)-1] = 0; + version_str = t; } } #endif else - { return JIM_ERR; - } } Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1)); @@ -423,33 +381,31 @@ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv #ifdef CYGPKG_HAL_NIOS2 -struct info_forward -{ +struct info_forward { void *data; struct cyg_upgrade_info *upgraded_file; }; -static void report_info(void *data, const char * format, va_list args) +static void report_info(void *data, const char *format, va_list args) { char *s = alloc_vprintf(format, args); LOG_USER_N("%s", s); free(s); } -struct cyg_upgrade_info firmware_info = -{ - (uint8_t *)0x84000000, - "/ram/firmware.phi", - "Firmware", - 0x0300000, - 0x1f00000 - - 0x0300000, - "ZylinNiosFirmware\n", - report_info, +struct cyg_upgrade_info firmware_info = { + (uint8_t *)0x84000000, + "/ram/firmware.phi", + "Firmware", + 0x0300000, + 0x1f00000 - + 0x0300000, + "ZylinNiosFirmware\n", + report_info, }; -// File written to /ram/firmware.phi before arriving at this fn -static int jim_zy1000_writefirmware(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +/* File written to /ram/firmware.phi before arriving at this fn */ +static int jim_zy1000_writefirmware(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { if (argc != 1) return JIM_ERR; @@ -461,13 +417,11 @@ static int jim_zy1000_writefirmware(Jim_Interp *interp, int argc, Jim_Obj *const } #endif -static int -zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp, - int argc, - Jim_Obj * const *argv) +static int zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp, + int argc, + Jim_Obj * const *argv) { - if (argc != 1) - { + if (argc != 1) { Jim_WrongNumArgs(interp, 1, argv, "powerstatus"); return JIM_ERR; } @@ -479,16 +433,12 @@ zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp, return JIM_OK; } - - int zy1000_quit(void) { return ERROR_OK; } - - int interface_jtag_execute_queue(void) { uint32_t empty; @@ -503,8 +453,7 @@ int interface_jtag_execute_queue(void) /* and handle any callbacks... */ zy1000_flush_callbackqueue(); - if (zy1000_rclk) - { + if (zy1000_rclk) { /* Only check for errors when using RCLK to speed up * jtag over TCP/IP */ @@ -512,57 +461,50 @@ int interface_jtag_execute_queue(void) /* clear JTAG error register */ ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400); - if ((empty&0x400) != 0) - { + if ((empty&0x400) != 0) { LOG_WARNING("RCLK timeout"); /* the error is informative only as we don't want to break the firmware if there * is a false positive. */ - // return ERROR_FAIL; + /* return ERROR_FAIL; */ } } return ERROR_OK; } - - - static void writeShiftValue(uint8_t *data, int bits); -// here we shuffle N bits out/in -static __inline void scanBits(const uint8_t *out_value, uint8_t *in_value, int num_bits, bool pause_now, tap_state_t shiftState, tap_state_t end_state) +/* here we shuffle N bits out/in */ +static inline void scanBits(const uint8_t *out_value, + uint8_t *in_value, + int num_bits, + bool pause_now, + tap_state_t shiftState, + tap_state_t end_state) { tap_state_t pause_state = shiftState; - for (int j = 0; j < num_bits; j += 32) - { + for (int j = 0; j < num_bits; j += 32) { int k = num_bits - j; - if (k > 32) - { + if (k > 32) { k = 32; /* we have more to shift out */ - } else if (pause_now) - { + } else if (pause_now) { /* this was the last to shift out this time */ pause_state = end_state; } - // we have (num_bits + 7)/8 bytes of bits to toggle out. - // bits are pushed out LSB to MSB + /* we have (num_bits + 7)/8 bytes of bits to toggle out. */ + /* bits are pushed out LSB to MSB */ uint32_t value; value = 0; - if (out_value != NULL) - { + if (out_value != NULL) { for (int l = 0; l < k; l += 8) - { - value|=out_value[(j + l)/8]<= 32 is not defined by the C standard * and will in fact shift by &0x1f bits on nios */ } @@ -570,50 +512,47 @@ static __inline void scanBits(const uint8_t *out_value, uint8_t *in_value, int n shiftValueInner(shiftState, pause_state, k, value); if (in_value != NULL) - { writeShiftValue(in_value + (j/8), k); - } } } -static __inline void scanFields(int num_fields, const struct scan_field *fields, tap_state_t shiftState, tap_state_t end_state) +static inline void scanFields(int num_fields, + const struct scan_field *fields, + tap_state_t shiftState, + tap_state_t end_state) { - for (int i = 0; i < num_fields; i++) - { + for (int i = 0; i < num_fields; i++) { scanBits(fields[i].out_value, - fields[i].in_value, - fields[i].num_bits, - (i == num_fields-1), - shiftState, - end_state); + fields[i].in_value, + fields[i].num_bits, + (i == num_fields-1), + shiftState, + end_state); } } -int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field *fields, tap_state_t state) +int interface_jtag_add_ir_scan(struct jtag_tap *active, + const struct scan_field *fields, + tap_state_t state) { int scan_size = 0; struct jtag_tap *tap, *nextTap; tap_state_t pause_state = TAP_IRSHIFT; - for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap) - { + for (tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = nextTap) { nextTap = jtag_tap_next_enabled(tap); - if (nextTap==NULL) - { + if (nextTap == NULL) pause_state = state; - } scan_size = tap->ir_length; /* search the list */ - if (tap == active) - { + if (tap == active) { scanFields(1, fields, TAP_IRSHIFT, pause_state); /* update device information */ buf_cpy(fields[0].out_value, tap->cur_instr, scan_size); tap->bypass = 0; - } else - { + } else { /* if a device isn't listed, set it to BYPASS */ assert(scan_size <= 32); shiftValueInner(TAP_IRSHIFT, pause_state, scan_size, 0xffffffff); @@ -630,36 +569,33 @@ int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field return ERROR_OK; } - - - - -int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state) +int interface_jtag_add_plain_ir_scan(int num_bits, + const uint8_t *out_bits, + uint8_t *in_bits, + tap_state_t state) { scanBits(out_bits, in_bits, num_bits, true, TAP_IRSHIFT, state); return ERROR_OK; } -int interface_jtag_add_dr_scan(struct jtag_tap *active, int num_fields, const struct scan_field *fields, tap_state_t state) +int interface_jtag_add_dr_scan(struct jtag_tap *active, + int num_fields, + const struct scan_field *fields, + tap_state_t state) { struct jtag_tap *tap, *nextTap; tap_state_t pause_state = TAP_DRSHIFT; - for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap) - { + for (tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = nextTap) { nextTap = jtag_tap_next_enabled(tap); - if (nextTap==NULL) - { + if (nextTap == NULL) pause_state = state; - } /* Find a range of fields to write to this tap */ - if (tap == active) - { + if (tap == active) { assert(!tap->bypass); scanFields(num_fields, fields, TAP_DRSHIFT, pause_state); - } else - { + } else { /* Shift out a 0 for disabled tap's */ assert(tap->bypass); shiftValueInner(TAP_DRSHIFT, pause_state, 1, 0); @@ -668,7 +604,10 @@ int interface_jtag_add_dr_scan(struct jtag_tap *active, int num_fields, const st return ERROR_OK; } -int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state) +int interface_jtag_add_plain_dr_scan(int num_bits, + const uint8_t *out_bits, + uint8_t *in_bits, + tap_state_t state) { scanBits(out_bits, in_bits, num_bits, true, TAP_DRSHIFT, state); return ERROR_OK; @@ -680,7 +619,6 @@ int interface_jtag_add_tlr() return ERROR_OK; } - int interface_jtag_add_reset(int req_trst, int req_srst) { zy1000_reset(req_trst, req_srst); @@ -694,14 +632,11 @@ static int zy1000_jtag_add_clocks(int num_cycles, tap_state_t state, tap_state_t /* execute num_cycles, 32 at the time. */ int i; - for (i = 0; i < num_cycles; i += 32) - { + for (i = 0; i < num_cycles; i += 32) { int num; num = 32; if (num_cycles-i < num) - { num = num_cycles-i; - } shiftValueInner(clockstate, clockstate, num, 0); } @@ -715,8 +650,7 @@ static int zy1000_jtag_add_clocks(int num_cycles, tap_state_t state, tap_state_t uint8_t tms_scan = tap_get_tms_path(t, state); int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state()); - for (i = 0; i < tms_count; i++) - { + for (i = 0; i < tms_count; i++) { tms = (tms_scan >> i) & 1; waitIdle(); ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms); @@ -743,18 +677,13 @@ int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state /*wait for the fifo to be empty*/ waitIdle(); - for (unsigned i = 0; i < num_bits; i++) - { + for (unsigned i = 0; i < num_bits; i++) { int tms; if (((seq[i/8] >> (i % 8)) & 1) == 0) - { tms = 0; - } else - { tms = 1; - } waitIdle(); ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, tms); @@ -762,11 +691,10 @@ int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state waitIdle(); if (state != TAP_INVALID) - { ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, state); - } else - { - /* this would be normal if we are switching to SWD mode */ + else { + /* this would be normal if + * we are switching to SWD mode */ } return ERROR_OK; } @@ -784,19 +712,14 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path) memset(seq, 0, sizeof(seq)); assert(num_states < (int)((sizeof(seq) * 8))); - while (num_states) - { + while (num_states) { if (tap_state_transition(cur_state, false) == path[state_count]) - { tms = 0; - } else if (tap_state_transition(cur_state, true) == path[state_count]) - { tms = 1; - } - else - { - LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[state_count])); + else { + LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", + tap_state_name(cur_state), tap_state_name(path[state_count])); exit(-1); } @@ -818,48 +741,42 @@ static void jtag_pre_post_bits(struct jtag_tap *tap, int *pre, int *post) bool found = false; struct jtag_tap *cur_tap, *nextTap; - for (cur_tap = jtag_tap_next_enabled(NULL); cur_tap!= NULL; cur_tap = nextTap) - { + for (cur_tap = jtag_tap_next_enabled(NULL); cur_tap != NULL; cur_tap = nextTap) { nextTap = jtag_tap_next_enabled(cur_tap); if (cur_tap == tap) - { found = true; - } else - { + else { if (found) - { post_bits++; - } else - { + else pre_bits++; - } } } *pre = pre_bits; *post = post_bits; } -/* - static const int embeddedice_num_bits[] = {32, 6}; +#if 0 +static const int embeddedice_num_bits[] = {32, 6}; uint32_t values[2]; values[0] = value; values[1] = (1 << 5) | reg_addr; - jtag_add_dr_out(tap, - 2, - embeddedice_num_bits, - values, - TAP_IDLE); -*/ + jtag_add_dr_out(tap, 2, embeddedice_num_bits, values, TAP_IDLE); +#endif -void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, const uint8_t *buffer, int little, int count) +void embeddedice_write_dcc(struct jtag_tap *tap, + int reg_addr, + const uint8_t *buffer, + int little, + int count) { #if 0 int i; - for (i = 0; i < count; i++) - { - embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little)); + for (i = 0; i < count; i++) { + embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, + little)); buffer += 4; } #else @@ -867,48 +784,47 @@ void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, const uint8_t *bu int post_bits; jtag_pre_post_bits(tap, &pre_bits, &post_bits); - if ((pre_bits > 32) || (post_bits + 6 > 32)) - { + if ((pre_bits > 32) || (post_bits + 6 > 32)) { int i; - for (i = 0; i < count; i++) - { - embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little)); + for (i = 0; i < count; i++) { + embeddedice_write_reg_inner(tap, reg_addr, + fast_target_buffer_get_u32(buffer, little)); buffer += 4; } - } else - { + } else { int i; - for (i = 0; i < count; i++) - { + for (i = 0; i < count; i++) { /* Fewer pokes means we get to use the FIFO more efficiently */ shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, pre_bits, 0); - shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, little)); + shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, + fast_target_buffer_get_u32(buffer, little)); /* Danger! here we need to exit into the TAP_IDLE state to make * DCC pick up this value. */ - shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits, (reg_addr | (1 << 5))); + shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits, + (reg_addr | (1 << 5))); buffer += 4; } } #endif } - - -int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opcode, const uint32_t * data, size_t count) +int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap *tap, + uint32_t opcode, + const uint32_t *data, + size_t count) { /* bypass bits before and after */ int pre_bits; int post_bits; jtag_pre_post_bits(tap, &pre_bits, &post_bits); - post_bits+=2; + post_bits += 2; - if ((pre_bits > 32) || (post_bits > 32)) - { - int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap *, uint32_t, const uint32_t *, size_t); + if ((pre_bits > 32) || (post_bits > 32)) { + int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap *, + uint32_t, const uint32_t *, size_t); return arm11_run_instr_data_to_core_noack_inner_default(tap, opcode, data, count); - } else - { + } else { static const int bits[] = {32, 2}; uint32_t values[] = {0, 0}; @@ -916,8 +832,7 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc * with unaligned uint32_t * pointers... */ const uint8_t *t = (const uint8_t *)data; - while (--count > 0) - { + while (--count > 0) { #if 1 /* Danger! This code doesn't update cmd_queue_cur_state, so * invoking jtag_add_pathmove() before jtag_add_dr_out() after @@ -936,7 +851,8 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc shiftValueInner(TAP_DRSHIFT, TAP_DRPAUSE, post_bits, 0); /* copy & paste from arm11_dbgtap.c */ - //TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT + /* TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, + * TAP_DRCAPTURE, TAP_DRSHIFT */ /* KLUDGE! we have to flush the fifo or the Nios CPU locks up. * This is probably a bug in the Avalon bus(cross clocking bridge?) * or in the jtag registers module. @@ -954,9 +870,9 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRSHIFT); waitIdle(); #else - static const tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] = - { - TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT + static const tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] = { + TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, + TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT }; values[0] = *t++; @@ -993,7 +909,6 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc } } - static const struct command_registration zy1000_commands[] = { { .name = "power", @@ -1040,7 +955,6 @@ static const struct command_registration zy1000_commands[] = { COMMAND_REGISTRATION_DONE }; - #if !BUILD_ZY1000_MASTER static int tcp_ip = -1; @@ -1062,16 +976,12 @@ static bool flush_writes(void) static bool writeLong(uint32_t l) { int i; - for (i = 0; i < 4; i++) - { + for (i = 0; i < 4; i++) { uint8_t c = (l >> (i*8))&0xff; out_buffer[out_pos++] = c; - if (out_pos >= sizeof(out_buffer)) - { + if (out_pos >= sizeof(out_buffer)) { if (!flush_writes()) - { return false; - } } } return true; @@ -1081,29 +991,22 @@ static bool readLong(uint32_t *out_data) { uint32_t data = 0; int i; - for (i = 0; i < 4; i++) - { + for (i = 0; i < 4; i++) { uint8_t c; - if (in_pos == in_write) - { + if (in_pos == in_write) { /* If we have some data that we can send, send them before * we wait for more data */ - if (out_pos > 0) - { + if (out_pos > 0) { if (!flush_writes()) - { return false; - } } /* read more */ int t; t = read(tcp_ip, in_buffer, sizeof(in_buffer)); if (t < 1) - { return false; - } in_write = (size_t) t; in_pos = 0; } @@ -1115,16 +1018,15 @@ static bool readLong(uint32_t *out_data) return true; } -enum ZY1000_CMD -{ +enum ZY1000_CMD { ZY1000_CMD_POKE = 0x0, ZY1000_CMD_PEEK = 0x8, ZY1000_CMD_SLEEP = 0x1, ZY1000_CMD_WAITIDLE = 2 }; -#include /* for socket(), connect(), send(), and recv() */ -#include /* for sockaddr_in and inet_addr() */ +#include /* for socket(), connect(), send(), and recv() */ +#include /* for sockaddr_in and inet_addr() */ /* We initialize this late since we need to know the server address * first. @@ -1134,45 +1036,41 @@ static void tcpip_open(void) if (tcp_ip >= 0) return; - struct sockaddr_in echoServAddr; /* Echo server address */ + struct sockaddr_in echoServAddr;/* Echo server address */ /* Create a reliable, stream socket using TCP */ - if ((tcp_ip = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) - { + tcp_ip = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (tcp_ip < 0) { fprintf(stderr, "Failed to connect to zy1000 server\n"); exit(-1); } /* Construct the server address structure */ - memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ - echoServAddr.sin_family = AF_INET; /* Internet address family */ - echoServAddr.sin_addr.s_addr = inet_addr(tcp_server); /* Server IP address */ - echoServAddr.sin_port = htons(7777); /* Server port */ + memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ + echoServAddr.sin_family = AF_INET; /* Internet address family */ + echoServAddr.sin_addr.s_addr = inet_addr(tcp_server); /* Server IP address */ + echoServAddr.sin_port = htons(7777); /* Server port */ /* Establish the connection to the echo server */ - if (connect(tcp_ip, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) - { + if (connect(tcp_ip, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) { fprintf(stderr, "Failed to connect to zy1000 server\n"); exit(-1); } int flag = 1; setsockopt(tcp_ip, /* socket affected */ - IPPROTO_TCP, /* set option at TCP level */ - TCP_NODELAY, /* name of option */ - (char *)&flag, /* the cast is historical cruft */ - sizeof(int)); /* length of option value */ + IPPROTO_TCP, /* set option at TCP level */ + TCP_NODELAY, /* name of option */ + (char *)&flag, /* the cast is historical cruft */ + sizeof(int)); /* length of option value */ } - /* send a poke */ void zy1000_tcpout(uint32_t address, uint32_t data) { tcpip_open(); - if (!writeLong((ZY1000_CMD_POKE << 24) | address)|| - !writeLong(data)) - { + if (!writeLong((ZY1000_CMD_POKE << 24) | address) || !writeLong(data)) { fprintf(stderr, "Could not write to zy1000 server\n"); exit(-1); } @@ -1185,8 +1083,7 @@ void zy1000_tcpout(uint32_t address, uint32_t data) void waitIdle(void) { tcpip_open(); - if (!writeLong((ZY1000_CMD_WAITIDLE << 24))) - { + if (!writeLong((ZY1000_CMD_WAITIDLE << 24))) { fprintf(stderr, "Could not write to zy1000 server\n"); exit(-1); } @@ -1199,9 +1096,7 @@ uint32_t zy1000_tcpin(uint32_t address) zy1000_flush_readqueue(); uint32_t data; - if (!writeLong((ZY1000_CMD_PEEK << 24) | address)|| - !readLong(&data)) - { + if (!writeLong((ZY1000_CMD_PEEK << 24) | address) || !readLong(&data)) { fprintf(stderr, "Could not read from zy1000 server\n"); exit(-1); } @@ -1211,9 +1106,7 @@ uint32_t zy1000_tcpin(uint32_t address) int interface_jtag_add_sleep(uint32_t us) { tcpip_open(); - if (!writeLong((ZY1000_CMD_SLEEP << 24))|| - !writeLong(us)) - { + if (!writeLong((ZY1000_CMD_SLEEP << 24)) || !writeLong(us)) { fprintf(stderr, "Could not read from zy1000 server\n"); exit(-1); } @@ -1222,32 +1115,28 @@ int interface_jtag_add_sleep(uint32_t us) /* queue a readback */ #define readqueue_size 16384 -static struct -{ +static struct { uint8_t *dest; int bits; } readqueue[readqueue_size]; -static int readqueue_pos = 0; +static int readqueue_pos; /* flush the readqueue, this means reading any data that * we're expecting and store them into the final position */ void zy1000_flush_readqueue(void) { - if (readqueue_pos == 0) - { + if (readqueue_pos == 0) { /* simply debugging by allowing easy breakpoints when there * is something to do. */ return; } int i; tcpip_open(); - for (i = 0; i < readqueue_pos; i++) - { + for (i = 0; i < readqueue_pos; i++) { uint32_t value; - if (!readLong(&value)) - { + if (!readLong(&value)) { fprintf(stderr, "Could not read from zy1000 server\n"); exit(-1); } @@ -1255,25 +1144,22 @@ void zy1000_flush_readqueue(void) uint8_t *in_value = readqueue[i].dest; int k = readqueue[i].bits; - // we're shifting in data to MSB, shift data to be aligned for returning the value + /* we're shifting in data to MSB, shift data to be aligned for returning the value */ value >>= 32-k; for (int l = 0; l < k; l += 8) - { - in_value[l/8]=(value >> l)&0xff; - } + in_value[l/8] = (value >> l)&0xff; } readqueue_pos = 0; } /* By queuing the callback's we avoid flushing the -read queue until jtag_execute_queue(). This can -reduce latency dramatically for cases where -callbacks are used extensively. + * read queue until jtag_execute_queue(). This can + * reduce latency dramatically for cases where + * callbacks are used extensively. */ #define callbackqueue_size 128 -static struct callbackentry -{ +static struct callbackentry { jtag_callback_t callback; jtag_callback_data_t data0; jtag_callback_data_t data1; @@ -1281,14 +1167,16 @@ static struct callbackentry jtag_callback_data_t data3; } callbackqueue[callbackqueue_size]; -static int callbackqueue_pos = 0; +static int callbackqueue_pos; -void zy1000_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3) +void zy1000_jtag_add_callback4(jtag_callback_t callback, + jtag_callback_data_t data0, + jtag_callback_data_t data1, + jtag_callback_data_t data2, + jtag_callback_data_t data3) { if (callbackqueue_pos >= callbackqueue_size) - { zy1000_flush_callbackqueue(); - } callbackqueue[callbackqueue_pos].callback = callback; callbackqueue[callbackqueue_pos].data0 = data0; @@ -1307,7 +1195,10 @@ void zy1000_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t da zy1000_flush_callbackqueue(); } -static int zy1000_jtag_convert_to_callback4(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3) +static int zy1000_jtag_convert_to_callback4(jtag_callback_data_t data0, + jtag_callback_data_t data1, + jtag_callback_data_t data2, + jtag_callback_data_t data3) { ((jtag_callback1_t)data1)(data0); return ERROR_OK; @@ -1315,20 +1206,24 @@ static int zy1000_jtag_convert_to_callback4(jtag_callback_data_t data0, jtag_cal void zy1000_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0) { - zy1000_jtag_add_callback4(zy1000_jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0); + zy1000_jtag_add_callback4(zy1000_jtag_convert_to_callback4, + data0, + (jtag_callback_data_t)callback, + 0, + 0); } void zy1000_flush_callbackqueue(void) { /* we have to flush the read queue so we have access to - the data the callbacks will use + the data the callbacks will use */ zy1000_flush_readqueue(); int i; - for (i = 0; i < callbackqueue_pos; i++) - { + for (i = 0; i < callbackqueue_pos; i++) { struct callbackentry *entry = &callbackqueue[i]; - jtag_set_error(entry->callback(entry->data0, entry->data1, entry->data2, entry->data3)); + jtag_set_error(entry->callback(entry->data0, entry->data1, entry->data2, + entry->data3)); } callbackqueue_pos = 0; } @@ -1337,16 +1232,13 @@ static void writeShiftValue(uint8_t *data, int bits) { waitIdle(); - if (!writeLong((ZY1000_CMD_PEEK << 24) | (ZY1000_JTAG_BASE + 0xc))) - { + if (!writeLong((ZY1000_CMD_PEEK << 24) | (ZY1000_JTAG_BASE + 0xc))) { fprintf(stderr, "Could not read from zy1000 server\n"); exit(-1); } if (readqueue_pos >= readqueue_size) - { zy1000_flush_readqueue(); - } readqueue[readqueue_pos].dest = data; readqueue[readqueue_pos].bits = bits; @@ -1365,14 +1257,12 @@ static void writeShiftValue(uint8_t *data, int bits) ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value); VERBOSE(LOG_INFO("getShiftValue %08x", value)); - // data in, LSB to MSB - // we're shifting in data to MSB, shift data to be aligned for returning the value + /* data in, LSB to MSB */ + /* we're shifting in data to MSB, shift data to be aligned for returning the value */ value >>= 32 - bits; for (int l = 0; l < bits; l += 8) - { - data[l/8]=(value >> l)&0xff; - } + data[l/8] = (value >> l)&0xff; } #endif @@ -1391,15 +1281,14 @@ static void watchdog_server(cyg_addrword_t data) { int so_reuseaddr_option = 1; - int fd; - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) - { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd == -1) { LOG_ERROR("error creating socket: %s", strerror(errno)); exit(-1); } - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option, - sizeof(int)); + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &so_reuseaddr_option, + sizeof(int)); struct sockaddr_in sin; unsigned int address_size; @@ -1409,51 +1298,44 @@ static void watchdog_server(cyg_addrword_t data) sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(8888); - if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) - { + if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) { LOG_ERROR("couldn't bind to socket: %s", strerror(errno)); exit(-1); } - if (listen(fd, 1) == -1) - { + if (listen(fd, 1) == -1) { LOG_ERROR("couldn't listen on socket: %s", strerror(errno)); exit(-1); } - for (;;) - { + for (;; ) { int watchdog_ip = accept(fd, (struct sockaddr *) &sin, &address_size); /* Start watchdog, must be reset every 10 seconds. */ HAL_WRITE_UINT32(WATCHDOG_BASE + 4, 4); - if (watchdog_ip < 0) - { + if (watchdog_ip < 0) { LOG_ERROR("couldn't open watchdog socket: %s", strerror(errno)); exit(-1); } int flag = 1; setsockopt(watchdog_ip, /* socket affected */ - IPPROTO_TCP, /* set option at TCP level */ - TCP_NODELAY, /* name of option */ - (char *)&flag, /* the cast is historical cruft */ - sizeof(int)); /* length of option value */ + IPPROTO_TCP, /* set option at TCP level */ + TCP_NODELAY, /* name of option */ + (char *)&flag, /* the cast is historical cruft */ + sizeof(int)); /* length of option value */ char buf; - for (;;) - { - if (read(watchdog_ip, &buf, 1) == 1) - { + for (;; ) { + if (read(watchdog_ip, &buf, 1) == 1) { /* Reset timer */ HAL_WRITE_UINT32(WATCHDOG_BASE + 8, 0x1234); /* Echo so we can telnet in and see that resetting works */ write(watchdog_ip, &buf, 1); - } else - { + } else { /* Stop tickling the watchdog, the CPU will reset in < 10 seconds * now. */ @@ -1487,43 +1369,43 @@ int zy1000_init(void) #if BUILD_ECOSBOARD LOG_USER("%s", ZYLIN_OPENOCD_VERSION); #elif BUILD_ZY1000_MASTER - int fd; - if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) - { - LOG_ERROR("No access to /dev/mem"); - return ERROR_FAIL; - } + int fd = open("/dev/mem", O_RDWR | O_SYNC); + if (fd == -1) { + LOG_ERROR("No access to /dev/mem"); + return ERROR_FAIL; + } #ifndef REGISTERS_BASE #define REGISTERS_BASE 0x9002000 #define REGISTERS_SPAN 128 #endif - - zy1000_jtag_master = mmap(0, REGISTERS_SPAN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, REGISTERS_BASE); - - if(zy1000_jtag_master == (void *) -1) - { - close(fd); - LOG_ERROR("No access to /dev/mem"); - return ERROR_FAIL; - } -#endif - + zy1000_jtag_master = mmap(0, + REGISTERS_SPAN, + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + REGISTERS_BASE); + + if (zy1000_jtag_master == (void *) -1) { + close(fd); + LOG_ERROR("No access to /dev/mem"); + return ERROR_FAIL; + } +#endif - ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); // Turn on LED1 & LED2 - - setPower(true); // on by default + ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); /* Turn on LED1 & LED2 */ + setPower(true); /* on by default */ - /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */ + /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */ zy1000_reset(0, 0); #if BUILD_ZY1000_MASTER #if BUILD_ECOSBOARD #ifdef WATCHDOG_BASE cyg_thread_create(1, watchdog_server, (cyg_addrword_t) 0, "watchdog tcip/ip server", - (void *) watchdog_stack, sizeof(watchdog_stack), - &watchdog_thread_handle, &watchdog_thread_object); + (void *) watchdog_stack, sizeof(watchdog_stack), + &watchdog_thread_handle, &watchdog_thread_object); cyg_thread_resume(watchdog_thread_handle); #endif #endif @@ -1532,10 +1414,7 @@ int zy1000_init(void) return ERROR_OK; } - - -struct jtag_interface zy1000_interface = -{ +struct jtag_interface zy1000_interface = { .name = "ZY1000", .supported = DEBUG_CAP_TMS_SEQ, .execute_queue = NULL, -- 2.30.2