diff --git a/src/DAP.c b/src/DAP.c index 6eb16d1..dac63d6 100644 --- a/src/DAP.c +++ b/src/DAP.c @@ -80,7 +80,7 @@ static uint8_t DAP_Info(uint8_t id, uint8_t *info) { uint8_t length = 0; - DEBUGF("DAP_Info: %02X\n", id); + DPRINTF("DAP_Info: %02X\n", id); switch (id) { @@ -167,15 +167,15 @@ static uint32_t DAP_LED(uint8_t *request, uint8_t *response) switch (*request) { case DAP_LED_DEBUGGER_CONNECTED: - DEBUGF("DAP_LED: CONNECTED %02X\n", (*(request+1) & 1)); + DPRINTF("DAP_LED: CONNECTED %02X\n", (*(request+1) & 1)); LED_CONNECTED_OUT((*(request+1) & 1)); break; case DAP_LED_TARGET_RUNNING: - DEBUGF("DAP_LED: RUNNING %02X\n", (*(request+1) & 1)); + DPRINTF("DAP_LED: RUNNING %02X\n", (*(request+1) & 1)); LED_RUNNING_OUT((*(request+1) & 1)); break; default: - DEBUG("DAP_LED: ERROR\n"); + DPRINT("DAP_LED: ERROR\n"); *response = DAP_ERROR; return (1); } @@ -205,20 +205,20 @@ static uint32_t DAP_Connect(uint8_t *request, uint8_t *response) { #if (DAP_SWD != 0) case DAP_PORT_SWD: - DEBUG("DAP_CONNECT: SWD\n"); + DPRINT("DAP_CONNECT: SWD\n"); DAP_Data.debug_port = DAP_PORT_SWD; PORT_SWD_SETUP(); break; #endif #if (DAP_JTAG != 0) case DAP_PORT_JTAG: - DEBUG("DAP_CONNECT: JTAG\n"); + DPRINT("DAP_CONNECT: JTAG\n"); DAP_Data.debug_port = DAP_PORT_JTAG; PORT_JTAG_SETUP(); break; #endif default: - DEBUG("DAP_CONNECT: DISABLED\n"); + DPRINT("DAP_CONNECT: DISABLED\n"); *response = DAP_PORT_DISABLED; return (1); } @@ -234,7 +234,7 @@ static uint32_t DAP_Connect(uint8_t *request, uint8_t *response) // return: number of bytes in response static uint32_t DAP_Disconnect(uint8_t *response) { - DEBUG("DAP_DISCONNECT: DISABLED\n"); + DPRINT("DAP_DISCONNECT: DISABLED\n"); DAP_Data.debug_port = DAP_PORT_DISABLED; PORT_OFF(); @@ -251,7 +251,7 @@ static uint32_t DAP_ResetTarget(uint8_t *response) { *(response + 1) = RESET_TARGET(); *(response + 0) = DAP_OK; - DEBUGF("DAP_RESET: %02X\n", *(response + 1)); + DPRINTF("DAP_RESET: %02X\n", *(response + 1)); return (2); } @@ -263,7 +263,9 @@ static uint32_t DAP_ResetTarget(uint8_t *response) #if ((DAP_SWD != 0) || (DAP_JTAG != 0)) static uint32_t DAP_SWJ_Pins(uint8_t *request, uint8_t *response) { - uint8_t value; + (void) request; + (void) response; + /*uint8_t value; uint8_t select; uint32_t wait; @@ -344,15 +346,16 @@ static uint32_t DAP_SWJ_Pins(uint8_t *request, uint8_t *response) value = (PIN_SWCLK_TCK_IN() << DAP_SWJ_SWCLK_TCK) | (PIN_SWDIO_TMS_IN() << DAP_SWJ_SWDIO_TMS) | #if (DAP_JTAG != 0) - (PIN_TDI_IN() << DAP_SWJ_TDI) | - (PIN_TDO_IN() << DAP_SWJ_TDO) | + (PIN_TDI_IN_NO_BUFF() << DAP_SWJ_TDI) | + (PIN_TDI_IN_NO_BUFF() << DAP_SWJ_TDO) | #endif (PIN_nTRST_IN() << DAP_SWJ_nTRST) | (PIN_nRESET_IN() << DAP_SWJ_nRESET); DEBUGF(" %02X\n", (uint8_t)value); *response = (uint8_t)value; - return (1); + return (1);*/ + return 0; } #endif @@ -372,7 +375,7 @@ static uint32_t DAP_SWJ_Clock(uint8_t *request, uint8_t *response) (*(request+2) << 16) | (*(request+3) << 24); - DEBUGF("DAP_SWJ_Clock: %u\n", clock); + DPRINTF("DAP_SWJ_Clock: %u\n", clock); if (clock == 0) { @@ -403,7 +406,7 @@ static uint32_t DAP_SWJ_Clock(uint8_t *request, uint8_t *response) DAP_Data.clock_delay = delay; int success = ftdi_set_baudrate (&__ftdic, clock/2); if( success < 0){ - DEBUGF("Could not set baudrate: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not set baudrate: %s\n",ftdi_get_error_string(&__ftdic)); } } @@ -426,7 +429,7 @@ static uint32_t DAP_SWJ_Sequence(uint8_t *request, uint8_t *response) count = *request++; if (count == 0) count = 256; - DEBUGF("DAP_SWJ_Sequence: %u\n", count); + DPRINTF("DAP_SWJ_Sequence: %u\n", count); SWJ_Sequence(count, request); @@ -449,7 +452,7 @@ static uint32_t DAP_SWD_Configure(uint8_t *request, uint8_t *response) DAP_Data.swd_conf.turnaround = (value & 0x03) + 1; DAP_Data.swd_conf.data_phase = (value & 0x04) ? 1 : 0; - DEBUGF("DAP_SWD_Configure: %d %d\n", + DPRINTF("DAP_SWD_Configure: %d %d\n", DAP_Data.swd_conf.turnaround, DAP_Data.swd_conf.data_phase ); @@ -469,11 +472,11 @@ static uint32_t DAP_SWD_Abort(uint8_t *request, uint8_t *response) { uint32_t data; - DEBUG("DAP_SWD_Abort: "); + DPRINT("DAP_SWD_Abort: "); if (DAP_Data.debug_port != DAP_PORT_SWD) { - DEBUG("ERROR\n"); + DPRINT("ERROR\n"); *response = DAP_ERROR; return (1); } @@ -484,7 +487,7 @@ static uint32_t DAP_SWD_Abort(uint8_t *request, uint8_t *response) (*(request+3) << 16) | (*(request+4) << 24); - DEBUGF("%04X\n", data); + DPRINTF("%04X\n", data); // Write Abort register SWD_Transfer(DP_ABORT, &data); *response = DAP_OK; @@ -505,7 +508,7 @@ static uint32_t DAP_JTAG_Sequence(uint8_t *request, uint8_t *response) uint32_t response_count; uint32_t count; - DEBUG("DAP_JTAG_Sequence: \n"); + DPRINT("DAP_JTAG_Sequence: \n"); *response++ = DAP_OK; response_count = 1; @@ -543,7 +546,7 @@ static uint32_t DAP_JTAG_Configure(uint8_t *request, uint8_t *response) uint32_t bits; uint32_t n; - DEBUG("DAP_JTAG_Configure: \n"); + DPRINT("DAP_JTAG_Configure: \n"); count = *request++; DAP_Data.jtag_dev.count = count; @@ -577,11 +580,11 @@ static uint32_t DAP_JTAG_IDCode(uint8_t *request, uint8_t *response) { uint32_t data; - DEBUG("DAP_JTAG_IDCode: "); + DPRINT("DAP_JTAG_IDCode: "); if (DAP_Data.debug_port != DAP_PORT_JTAG) { -err: DEBUG("ERROR\n"); +err: DPRINT("ERROR\n"); *response = DAP_ERROR; return (1); } @@ -604,7 +607,7 @@ err: DEBUG("ERROR\n"); *(response+3) = (uint8_t)(data >> 16); *(response+4) = (uint8_t)(data >> 24); - DEBUGF("%04X\n", data); + DPRINTF("%04X\n", data); return (1+4); } @@ -620,10 +623,10 @@ static uint32_t DAP_JTAG_Abort(uint8_t *request, uint8_t *response) { uint32_t data; - DEBUG("DAP_JTAG_Abort: "); + DPRINT("DAP_JTAG_Abort: "); if (DAP_Data.debug_port != DAP_PORT_JTAG) { -err: DEBUG("ERROR\n"); +err: DPRINT("ERROR\n"); *response = DAP_ERROR; return (1); } @@ -645,7 +648,7 @@ err: DEBUG("ERROR\n"); JTAG_WriteAbort(data); *response = DAP_OK; - DEBUGF("%04X\n", data); + DPRINTF("%04X\n", data); return (1); } @@ -661,7 +664,7 @@ static uint32_t DAP_TransferConfigure(uint8_t *request, uint8_t *response) DAP_Data.transfer.idle_cycles = *(request+0); DAP_Data.transfer.retry_count = *(request+1) | (*(request+2) << 8); DAP_Data.transfer.match_retry = *(request+3) | (*(request+4) << 8); - DEBUGF("DAP_TransferConfigure: %d %d %d\n", + DPRINTF("DAP_TransferConfigure: %d %d %d\n", DAP_Data.transfer.idle_cycles, DAP_Data.transfer.retry_count, DAP_Data.transfer.match_retry @@ -703,7 +706,7 @@ static uint32_t DAP_SWD_Transfer(uint8_t *request, uint8_t *response) request++; // Ignore DAP index request_count = *request++; - DEBUGF("DAP_SWD_Transfer: %d\n", request_count); + DPRINTF("DAP_SWD_Transfer: %d\n", request_count); while (request_count--) { @@ -921,7 +924,7 @@ static uint32_t DAP_JTAG_Transfer(uint8_t *request, uint8_t *response) uint32_t data; uint32_t ir; - DEBUG("DAP_JTAG_Transfer:\n"); + DPRINT("DAP_JTAG_Transfer:\n"); response_count = 0; response_value = 0; @@ -1118,7 +1121,7 @@ static uint32_t DAP_SWD_TransferBlock(uint8_t *request, uint8_t *response) uint32_t retry; uint32_t data; - DEBUG("DAP_SWD_TransferBlock:\n"); + DPRINT("DAP_SWD_TransferBlock:\n"); response_count = 0; response_value = 0; @@ -1223,7 +1226,7 @@ static uint32_t DAP_JTAG_TransferBlock(uint8_t *request, uint8_t *response) uint32_t data; uint32_t ir; - DEBUG("DAP_JTAG_TransferBlock:\n"); + DPRINT("DAP_JTAG_TransferBlock:\n"); response_count = 0; response_value = 0; @@ -1320,7 +1323,7 @@ end: #if 0 __weak uint32_t DAP_ProcessVendorCommand(uint8_t *request, uint8_t *response) { - DEBUGF("DAP_ProcessVendorCommand:\n"); + DPRINTF("DAP_ProcessVendorCommand:\n"); *response = ID_DAP_Invalid; return (1); } @@ -1483,7 +1486,7 @@ uint32_t DAP_ProcessCommand(uint8_t *request, uint8_t *response) // Setup DAP -void DAP_Setup(void) +int DAP_Setup(void) { // Default settings (only non-zero values) //DAP_Data.debug_port = 0; @@ -1501,5 +1504,5 @@ void DAP_Setup(void) //DAP_Data.jtag_dev.count = 0; #endif - DAP_SETUP(); // Device specific setup + return DAP_SETUP(); // Device specific setup } diff --git a/src/DAP.h b/src/DAP.h index 744a135..d85193a 100644 --- a/src/DAP.h +++ b/src/DAP.h @@ -200,7 +200,7 @@ extern uint8_t SWD_Transfer (uint8_t request, uint32_t *data); extern uint32_t DAP_ProcessVendorCommand (uint8_t *request, uint8_t *response); extern uint32_t DAP_ProcessCommand (uint8_t *request, uint8_t *response); -extern void DAP_Setup (void); +extern int DAP_Setup (void); // Configurable delay for clock generation #define DELAY_SLOW_CYCLES 1 // Number of cycles for one iteration diff --git a/src/DAP_config.h b/src/DAP_config.h index a917aa0..cadbf25 100644 --- a/src/DAP_config.h +++ b/src/DAP_config.h @@ -62,7 +62,7 @@ Provides definitions about: /// Indicate that JTAG communication mode is available at the Debug Port. /// This information is returned by the command \ref DAP_Info as part of Capabilities. -#define DAP_JTAG 1 ///< JTAG Mode: 1 = available, 0 = not available. +#define DAP_JTAG 0 ///< JTAG Mode: 1 = available, 0 = not available. /// Configure maximum number of JTAG devices on the scan chain connected to the Debug Access Port. /// This setting impacts the RAM requirements of the Debug Unit. Valid range is 1 .. 255. @@ -139,19 +139,19 @@ of the same I/O port. The following SWDIO I/O Pin functions are provided: // Configure DAP I/O pins ------------------------------ -#define TMS_SWDIO RTS +#define TMS_SWDIO CTS #define TCK_SWCLK TXD #define TDO_SWO RXD -#define TDI DCD -#define nRESET DTR +#define TDI DTR +#define nRESET DCD #define nTRST RI -#define LED_CONN CTS +#define LED_CONN RTS #define LED_RUN DSR static __inline const char* pin_to_str(uint8_t pin){ switch(pin){ case TMS_SWDIO: - return "TMS_SWDIO(RTS)"; + return "TMS_SWDIO(CTS)"; case TCK_SWCLK: return "TCK_SWCLK(TXD)"; case TDO_SWO: @@ -159,11 +159,11 @@ static __inline const char* pin_to_str(uint8_t pin){ case TDI: return "TDI(DCD)"; case nRESET: - return "nRESET(DTR)"; + return "nRESET(DTR)"; case nTRST: return "nTRST(RI)"; case LED_CONN: - return "LED_CONN(CTS)"; + return "LED_CONN(RTS)"; case LED_RUN: return "LED_RUN(DSR)"; default: @@ -171,34 +171,36 @@ static __inline const char* pin_to_str(uint8_t pin){ } } -static __inline void FTDI_BITBANG_SETUP() { +static __inline int FTDI_BITBANG_SETUP() { int success; /* Enable bitbang mode */ success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_BITBANG ); if( success<0 ){ - DEBUGF("Could not change to async. bitbang: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not change to async. bitbang: %s\n",ftdi_get_error_string(&__ftdic)); + return -1; } success = ftdi_write_data(&__ftdic,&out_state,1); if( success < 0){ - DEBUGF("Could not write pins: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not write pins: %s\n",ftdi_get_error_string(&__ftdic)); + return -2; } - /*ftdi_read_data_set_chunksize(&__ftdic,1); - ftdi_write_data_set_chunksize(&__ftdic,1);*/ + ftdi_read_data_set_chunksize(&__ftdic,64); + ftdi_write_data_set_chunksize(&__ftdic,64); + return 0; } static __forceinline void FTDI_WRITE_OUT_READ_MODE( ) { int success = ftdi_write_data(&__ftdic,out_state_buff,out_state_buff_pos); if( success<0 ){ - DEBUGF("Could not write pins: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not write pins: %s\n",ftdi_get_error_string(&__ftdic)); exit(1); } } static __forceinline void FTDI_WRITE_OUT( ) { - int success; if( out_state_buff_pos==0 ){ - DEBUGF("buffer is empty %d \n",out_state_buff_pos); + DPRINTF("buffer is empty %d \n",out_state_buff_pos); return; } FTDI_WRITE_OUT_READ_MODE( ); @@ -211,7 +213,7 @@ static __forceinline void FTDI_SUBMIT( ) { out_state_buff[out_state_buff_pos] = out_state; out_state_buff_pos++; if( out_state_buff_pos>=BUFFER_SIZE ){ - DEBUG("FTDI bitbang-buffer overflow!\n"); + DPRINT("FTDI bitbang-buffer overflow!\n"); FTDI_WRITE_OUT( ); } } @@ -224,14 +226,12 @@ static __inline void FTDI_BEGIN_READ(){ int success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_SYNCBB ); if( success<0 ){ - DEBUGF("Could not change to sync. bitbang: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not change to sync. bitbang: %s\n",ftdi_get_error_string(&__ftdic)); } success = ftdi_usb_purge_buffers(&__ftdic); if( success<0 ){ - DEBUGF("Could not flush buffer: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not flush buffer: %s\n",ftdi_get_error_string(&__ftdic)); } - - //memset ( pin_read_buff, 0, BUFFER_SIZE*sizeof(int) ); is_in_read_mode = 1; } @@ -245,13 +245,13 @@ static __inline void FTDI_END_READ(){ while( remaining ){ success = ftdi_read_data(&__ftdic,ptr,remaining); if( success<0 ){ - DEBUGF("Error while read: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Error while read: %s\n",ftdi_get_error_string(&__ftdic)); exit(1); } ptr+=success; remaining-=success; } - DEBUGF("Read %d/%d bytes\n",(int)(ptr-in_buff),out_state_buff_pos); + DPRINTF("Read %d/%d bytes\n",(int)(ptr-in_buff),out_state_buff_pos); out_state_buff_pos = 0; for(i=0;i> 1) & 0x55) ; + c = (c & 0x33) + ((c >> 2) & 0x33); + c = (c & 0x0F) + ((c >> 4) & 0x0F); + return c; +} + +static __inline uint8_t bit_count32(uint32_t v){ + uint32_t c = 0; + c = (v & 0x55555555) + ((v >> 1) & 0x55555555) ; + c = (c & 0x33333333) + ((c >> 2) & 0x33333333) ; + c = (c & 0x0F0F0F0F) + ((c >> 4) & 0x0F0F0F0F) ; + c = (c & 0x00FF00FF) + ((c >> 8) & 0x00FF00FF) ; + c = (c & 0x0000FFFF) + ((c >>16) & 0x0000FFFF) ; + + return c; +} + //************************************************************************************************** /** \defgroup DAP_Config_Initialization_gr CMSIS-DAP Initialization @@ -309,27 +342,29 @@ Status LEDs. In detail the operation of Hardware I/O and LED pins are enabled an - for nTRST, nRESET a weak pull-up (if available) is enabled. - LED output pins are enabled and LEDs are turned off. */ -static __inline void DAP_SETUP (void) { +static __inline int DAP_SETUP (void) { /* Initialize context for subsequent function calls */ ftdi_init(&__ftdic); int success; /* Open FTDI device based on FT232R vendor & product IDs */ success = ftdi_usb_open(&__ftdic, VID, PID); if(success < 0) { - DEBUGF("Can't open device [%x:%x]: %s\n",VID,PID,ftdi_get_error_string(&__ftdic)); - exit(1); + DPRINTF("Can't open device [%x:%x]: %s\n",VID,PID,ftdi_get_error_string(&__ftdic)); + return -1; } dir_state = LED_CONN|LED_RUN|TCK_SWCLK|nRESET; out_state = 0xFF;// & ~(LED_CONN|LED_RUN); - FTDI_BITBANG_SETUP(); + if( FTDI_BITBANG_SETUP() < 0 ){ + return -2; + } success = ftdi_set_baudrate (&__ftdic, DAP_DEFAULT_SWJ_CLOCK/2); if( success < 0){ - DEBUGF("Could not set baudrate: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not set baudrate: %s\n",ftdi_get_error_string(&__ftdic)); + /* not critical */ } - /*uint8_t foo; - success = ftdi_read_data(&__ftdic,&foo,1);*/ + return 0; } ///@} @@ -359,21 +394,54 @@ static __inline void PORT_SWD_SETUP (void) { FTDI_BITBANG_SETUP(); } -static __forceinline uint32_t PIN_IN ( uint8_t pin ) { +static __forceinline uint32_t PIN_IN_NOBUFF ( uint8_t pin ) { uint8_t c; if( dir_state & pin ){ - DEBUGF("Pin %s is not an input pin!\n", pin_to_str(pin)); + DPRINTF("Pin %s is not an input pin!\n", pin_to_str(pin)); } int success = ftdi_read_pins(&__ftdic,&c); if(success < 0){ - DEBUGF("Could not read pin: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not read pin: %s\n",ftdi_get_error_string(&__ftdic)); } return (c & pin)!=0 ? 1 : 0; } +static __forceinline void PIN_IN(uint8_t pin, uint8_t *ptr,uint8_t bit) { + if( !is_in_read_mode ){ + DPRINT("Not in read mode!\n"); + return; + } + if( ptr!=NULL ){ + read_slot = ptr; + if( bit_count8(bit)==1 ){ + pin_read_mask=bit; + }else{ + pin_read_mask=1; + } + } + + /* correlate the read with the output buffer */ + pin_read_buff[pin_read_pos].pos = out_state_buff_pos; + pin_read_buff[pin_read_pos].write_bitmask = pin_read_mask; + pin_read_buff[pin_read_pos].read_bitmask = pin; + + pin_read_buff[pin_read_pos].ptr = read_slot; + pin_read_mask = pin_read_mask<<1; + if( !pin_read_mask ){ + pin_read_mask = 1; + read_slot += 1; + } + pin_read_pos++; + + if( pin_read_pos==BUFFER_SIZE ){ + DPRINT("read buffer overflow!\n"); + } +} + + static __forceinline void PIN_SET ( uint8_t pin, uint32_t bit ) { if( !(dir_state & pin) ){ - DEBUGF("Pin %s is not an output pin!\n", pin_to_str(pin)); + DPRINTF("Pin %s is not an output pin!\n", pin_to_str(pin)); return; } /*if(bit & ~1){ @@ -386,7 +454,7 @@ static __forceinline void PIN_SET ( uint8_t pin, uint32_t bit ) { } } -extern void late_close_socket(); +extern void deinit(); /** Disable JTAG/SWD I/O Pins. Disables the DAP Hardware I/O pins which configures: - TCK/SWCLK, TMS/SWDIO, TDI, TDO, nTRST, nRESET to High-Z mode. @@ -395,7 +463,7 @@ static __inline void PORT_OFF (void) { //TODO HighZ? /*ftdi_disable_bitbang(&__ftdic); ftdi_deinit(&__ftdic); TODO */ - late_close_socket(); + deinit(); } @@ -404,8 +472,8 @@ static __inline void PORT_OFF (void) { /** SWCLK/TCK I/O pin: Get Input. \return Current status of the SWCLK/TCK DAP hardware I/O pin. */ -static __forceinline uint32_t PIN_SWCLK_TCK_IN (void) { - return PIN_IN(TCK_SWCLK); +static __forceinline void PIN_SWCLK_TCK_IN (uint8_t *ptr,uint8_t bit){ + PIN_IN( TCK_SWCLK, ptr, bit ); } /** SWCLK/TCK I/O pin: Set Output to High. @@ -428,8 +496,8 @@ static __forceinline void PIN_SWCLK_TCK_CLR (void) { /** SWDIO/TMS I/O pin: Get Input. \return Current status of the SWDIO/TMS DAP hardware I/O pin. */ -static __forceinline uint32_t PIN_SWDIO_TMS_IN (void) { - return PIN_IN(TMS_SWDIO); +static __forceinline void PIN_SWDIO_TMS_IN (uint8_t *ptr,uint8_t bit){ + PIN_IN( TMS_SWDIO, ptr, bit ); } /** SWDIO/TMS I/O pin: Set Output to High. @@ -447,72 +515,11 @@ static __forceinline void PIN_SWDIO_TMS_CLR (void) { } -static __inline uint8_t bit_count8(uint8_t v){ - /* - * http://stackoverflow.com/questions/3815165/how-to-implement-bitcount-using-only-bitwise-operators - * 0x55=01010101, 0x33=00110011, 0x0F=00001111 - * v = [v7][v6][v5][v4][v3][v2][v1][v0] - * 1. - * c = 0[v6]0[v4]0[v2]0[v0] + 0[v7]0[v5]0[v3]0[v1] - * c = [v7+v6][v5+v4][v3+v2][v1+v0] - * 2. - * c = 00[v5+v4]00[v1+v0] + 00[v7+v6]00[v3+v2] - * c = [v7+v6+v5+v4][v3+v2+v1+v0] - * 3. - * c = 0000[v3+v2+v1+v0] + c=0000[v7+v6+v5+v4] - * c = [v7+v6+v5+v4+v3+v2+v1+v0] - */ - uint8_t c = 0; - c = (v & 0x55) + ((v >> 1) & 0x55) ; - c = (c & 0x33) + ((c >> 2) & 0x33); - c = (c & 0x0F) + ((c >> 4) & 0x0F); - return c; -} - -static __inline uint8_t bit_count32(uint32_t v){ - uint32_t c = 0; - c = (v & 0x55555555) + ((v >> 1) & 0x55555555) ; - c = (c & 0x33333333) + ((c >> 2) & 0x33333333) ; - c = (c & 0x0F0F0F0F) + ((c >> 4) & 0x0F0F0F0F) ; - c = (c & 0x00FF00FF) + ((c >> 8) & 0x00FF00FF) ; - c = (c & 0x0000FFFF) + ((c >>16) & 0x0000FFFF) ; - - return c; -} - /** SWDIO I/O pin: Get Input (used in SWD mode only). \return Current status of the SWDIO DAP hardware I/O pin. */ static __forceinline void PIN_SWDIO_IN (uint8_t *ptr,uint8_t bit){ - if( !is_in_read_mode ){ - DEBUG("Not in read mode!\n"); - return; - } - if( ptr!=NULL ){ - read_slot = ptr; - if( bit_count8(bit)==1 ){ - pin_read_mask=bit; - }else{ - pin_read_mask=1; - } - } - - /* correlate the read with the output buffer */ - pin_read_buff[pin_read_pos].pos = out_state_buff_pos; - pin_read_buff[pin_read_pos].write_bitmask = pin_read_mask; - pin_read_buff[pin_read_pos].read_bitmask = TMS_SWDIO; - - pin_read_buff[pin_read_pos].ptr = read_slot; - pin_read_mask = pin_read_mask<<1; - if( !pin_read_mask ){ - pin_read_mask = 1; - read_slot += 1; - } - pin_read_pos++; - - if( pin_read_pos==BUFFER_SIZE ){ - DEBUG("read buffer overflow!\n"); - } + PIN_IN( TMS_SWDIO, ptr, bit ); } /** SWDIO I/O pin: Set Output (used in SWD mode only). @@ -532,7 +539,7 @@ static __forceinline void PIN_SWDIO_OUT_ENABLE (void) { dir_state |= TMS_SWDIO; int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode ); if( success<0 ){ - DEBUGF("Could not change SWDIO to out: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not change SWDIO to out: %s\n",ftdi_get_error_string(&__ftdic)); } } @@ -547,7 +554,7 @@ static __forceinline void PIN_SWDIO_OUT_DISABLE (void) { int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode ); if( success<0 ){ - DEBUGF("Could not change SWDIO to in: %s\n",ftdi_get_error_string(&__ftdic)); + DPRINTF("Could not change SWDIO to in: %s\n",ftdi_get_error_string(&__ftdic)); } } @@ -557,8 +564,8 @@ static __forceinline void PIN_SWDIO_OUT_DISABLE (void) { /** TDI I/O pin: Get Input. \return Current status of the TDI DAP hardware I/O pin. */ -static __forceinline uint32_t PIN_TDI_IN (void) { - return PIN_IN(TDI); +static __forceinline void PIN_TDI_IN (uint8_t *ptr,uint8_t bit){ + PIN_IN( TMS_SWDIO, ptr, bit ); } /** TDI I/O pin: Set Output. @@ -574,8 +581,12 @@ static __forceinline void PIN_TDI_OUT (uint32_t bit) { /** TDO I/O pin: Get Input. \return Current status of the TDO DAP hardware I/O pin. */ -static __forceinline uint32_t PIN_TDO_IN (void) { - return PIN_IN(TDO_SWO); + +/** TDO I/O pin: Get Input. +\return Current status of the TDO DAP hardware I/O pin. +*/ +static __forceinline void PIN_TDO_IN (uint8_t *ptr,uint8_t bit){ + PIN_IN(TDO_SWO,ptr,bit); } @@ -584,8 +595,8 @@ static __forceinline uint32_t PIN_TDO_IN (void) { /** nTRST I/O pin: Get Input. \return Current status of the nTRST DAP hardware I/O pin. */ -static __forceinline uint32_t PIN_nTRST_IN (void) { - return PIN_IN(nTRST); +static __forceinline void PIN_nTRST_IN (uint8_t *ptr,uint8_t bit){ + PIN_IN( nTRST, ptr, bit ); } /** nTRST I/O pin: Set Output. @@ -602,8 +613,8 @@ static __forceinline void PIN_nTRST_OUT (uint32_t bit) { /** nRESET I/O pin: Get Input. \return Current status of the nRESET DAP hardware I/O pin. */ -static __forceinline uint32_t PIN_nRESET_IN (void) { - return PIN_IN(nRESET); +static __forceinline void PIN_nRESET_IN (uint8_t *ptr,uint8_t bit){ + PIN_IN( nRESET, ptr, bit ); } /** nRESET I/O pin: Set Output. diff --git a/src/Debug.h b/src/Debug.h index e133e2d..aa7f1db 100644 --- a/src/Debug.h +++ b/src/Debug.h @@ -10,12 +10,12 @@ #ifndef SRC_DEBUG_H_ #define SRC_DEBUG_H_ -#define DEBUG_OUT #ifdef DEBUG_OUT -# define DEBUGF(format, ...) fprintf (stderr, format, ##__VA_ARGS__) -# define DEBUG(str) fprintf (stderr, str) +# define DPRINTF(format, ...) fprintf (stderr, format, ##__VA_ARGS__) +# define DPRINT(str) fprintf (stderr, str) #else -# define DEBUGF(format, ...) +# define DPRINT(str) +# define DPRINTF(format, ...) #endif #endif //SRC_DEBUG_H_ diff --git a/src/JTAG_DP.c b/src/JTAG_DP.c index c23d384..35ff738 100644 --- a/src/JTAG_DP.c +++ b/src/JTAG_DP.c @@ -47,7 +47,7 @@ #define JTAG_CYCLE_TDO(tdo) \ PIN_TCK_CLR(); \ PIN_DELAY(); \ - tdo = PIN_TDO_IN(); \ + PIN_TDO_IN(tdo,1); \ PIN_TCK_SET(); \ PIN_DELAY() @@ -55,7 +55,7 @@ PIN_TDI_OUT(tdi); \ PIN_TCK_CLR(); \ PIN_DELAY(); \ - tdo = PIN_TDO_IN(); \ + PIN_TDO_IN(tdo,1); \ PIN_TCK_SET(); \ PIN_DELAY() @@ -71,9 +71,9 @@ // tdo: pointer to TDO captured data // return: none void JTAG_Sequence (uint32_t info, uint8_t *tdi, uint8_t *tdo) { - uint32_t i_val; - uint32_t o_val; - uint32_t bit; + uint32_t i_val=0; + uint32_t o_val=0; + uint8_t bit[64]; uint32_t n, k; n = info & JTAG_SEQUENCE_TCK; @@ -89,10 +89,10 @@ void JTAG_Sequence (uint32_t info, uint8_t *tdi, uint8_t *tdo) { i_val = *tdi++; o_val = 0; for (k = 8; k && n; k--, n--) { - JTAG_CYCLE_TDIO(i_val, bit); + //JTAG_CYCLE_TDIO(i_val, bit); i_val >>= 1; o_val >>= 1; - o_val |= bit << 7; + o_val |= 1 << 7; } o_val >>= k; if (info & JTAG_SEQUENCE_TDO) { diff --git a/src/SW_DP.c b/src/SW_DP.c index bbe643c..b354600 100644 --- a/src/SW_DP.c +++ b/src/SW_DP.c @@ -64,13 +64,13 @@ void SWJ_Sequence (uint32_t count, uint8_t *data) val = 0; n = 0; - DEBUG("DATA:"); + DPRINT("DATA:"); while (count--) { if (n == 0) { val = *data++; - DEBUGF(" %02X", val); + DPRINTF(" %02X", val); n = 8; } if (val & 1) @@ -85,7 +85,7 @@ void SWJ_Sequence (uint32_t count, uint8_t *data) val >>= 1; n--; } - DEBUG("\n"); + DPRINT("\n"); } #endif diff --git a/src/Timer.c b/src/Timer.c index df6484b..8d06801 100644 --- a/src/Timer.c +++ b/src/Timer.c @@ -5,6 +5,7 @@ * Author: powertomato */ #include "Timer.h" +#include "Debug.h" #include "device.h" /* for NULL*/ static int64_t get_time() { diff --git a/src/ftdi_vars.c b/src/ftdi_vars.c index a0b0cb9..1731dcd 100644 --- a/src/ftdi_vars.c +++ b/src/ftdi_vars.c @@ -9,8 +9,6 @@ #include "ftdi_vars.h" struct ftdi_context __ftdic; -uint16_t VID = 0x0403; -uint16_t PID = 0x6001; uint8_t out_state; uint8_t dir_state; diff --git a/src/main.c b/src/main.c index 681ec03..06a1144 100644 --- a/src/main.c +++ b/src/main.c @@ -26,26 +26,59 @@ #include -extern void process_data(void); +extern void process_data_socket(void); extern void init_socket(void); -// Main program -int main (void) { - - DAP_Setup(); // DAP Setup - +// Process a single DAP request +void process_data(uint8_t *req, uint8_t *resp) { + // Process DAP Command and prepare response + DAP_ProcessCommand(req, resp); + if( out_state_buff_pos != 0 ){ + FTDI_WRITE_OUT(); + } + +} + +uint16_t VID = 0; +uint16_t PID = 0; +int init(uint16_t vid, uint16_t pid){ + VID=vid; + PID=pid; + if( DAP_Setup() < 0){ + return -1; + } + //DAP_PACKET_COUNT LED_CONNECTED_OUT(1); // Turn on Debugger Connected LED LED_RUNNING_OUT(1); // Turn on Target Running LED Delayms(500); // Wait for 500ms LED_RUNNING_OUT(0); // Turn off Target Running LED LED_CONNECTED_OUT(0); // Turn off Debugger Connected LED - fflush(stdout); + + return 0; +} + +extern void late_close_socket(); +void deinit(){ +#ifndef SHARED_OBJECT_LIB + late_close_socket(); +#endif +} + +#ifndef SHARED_OBJECT_LIB +// Main program +int main (void) { + + if( init(0x0403,0x6001) < 0 ){ + exit(1); + } + init_socket(); while (1) { // Endless Loop //usbd_hid_process(); // Process USB HID Data // TODO thread sleep - process_data(); + process_data_socket(); } } +#endif diff --git a/src/socket_ipc.c b/src/socket_ipc.c index 39cce31..d70750a 100644 --- a/src/socket_ipc.c +++ b/src/socket_ipc.c @@ -5,6 +5,8 @@ * Author: powertomato */ +#ifndef SHARED_OBJECT_LIB + #include #include #include "DAP_config.h" @@ -19,16 +21,8 @@ #include #include -static volatile uint8_t request_full; // Request Buffer Usage Flag -static volatile uint32_t request_head; // ReqBuff Buffer In Index -static volatile uint32_t request_tail; // ReqBuff Buffer Out Index - -static volatile uint8_t response_full; // Response Buffer Usage Flag -static volatile uint32_t response_head; // Response Buffer In Index -static volatile uint32_t response_tail; // Response Buffer Out Index - -static uint8_t request_buff[DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // ReqBuff Buffer -static uint8_t response_buff[DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // Response Buffer +/*static uint8_t request_buff[DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // ReqBuff Buffer*/ +static uint8_t response_buff[DAP_PACKET_SIZE]; // Response Buffer #ifdef __CYGWIN__ /*include oddness...*/ typedef struct { @@ -49,16 +43,10 @@ sockaddr_in_t serv_addr; sockaddr_in_t cli_addr; void init_socket(void) { - request_full = 0; - request_head = 0; - request_tail = 0; - response_full = 0; - response_head = 0; - response_tail = 0; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { - DEBUGF("ERROR opening socket: %s\n", strerror(errno)); + DPRINTF("ERROR opening socket: %s\n", strerror(errno)); exit(1); } memset((char *) &serv_addr, 0, sizeof(serv_addr)); @@ -68,11 +56,11 @@ void init_socket(void) { serv_addr.sin_port = htons(portno); int yes = 1; if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0){ - DEBUGF("ERROR on setting socket option: %s\n", strerror(errno)); + DPRINTF("ERROR on setting socket option: %s\n", strerror(errno)); exit(1); } if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ - DEBUGF("ERROR on binding: %s\n", strerror(errno)); + DPRINTF("ERROR on binding: %s\n", strerror(errno)); exit(1); } listen(sockfd,5); @@ -93,13 +81,14 @@ void late_close_socket() { should_close = 1; } -// USB HID Callback: when system initializes +// Wait for a client to connect void wait_for_client(void) { printf("Wait client... "); + fflush(stdout); clilen = sizeof(cli_addr); csockfd = accept(sockfd, (struct sockaddr*)&cli_addr, (socklen_t*)&clilen); if (csockfd < 0){ - DEBUGF("ERROR on accept: %s\n", strerror(errno)); + DPRINTF("ERROR on accept: %s\n", strerror(errno)); exit(1); } @@ -110,56 +99,14 @@ void wait_for_client(void) { } /* TODO change nonblocking to a timed out read */ if( fcntl(csockfd, F_SETFL, flags | O_NONBLOCK) < 0 ){ - DEBUGF("ERROR seting non-blocking: %s\n", strerror(errno)); + DPRINTF("ERROR seting non-blocking: %s\n", strerror(errno)); } printf("connected\n"); } -// USB HID Callback: when data needs to be prepared for the host -int write_response() { - if ((response_tail != response_head) || response_full) { - int n = write(csockfd,response_buff[response_tail],DAP_PACKET_SIZE); - if (n != DAP_PACKET_SIZE){ - DEBUGF("ERROR writing to socket: %s\n", strerror(errno)); - close_socket(); - } - - // remove a response - response_tail = (response_tail+1) % DAP_PACKET_COUNT; - response_full = 0; - return (DAP_PACKET_SIZE); - } - return (0); -} - - -// USB HID Callback: when data is received from the host -void buffer_request(uint8_t *buff, int len) { - if (len == 0){ - return; - } - if (buff[0] == ID_DAP_TransferAbort) { - DAP_TransferAbort = 1; - return; - } - if (request_full) { - return; // Discard packet when buffer is full - } - // add a request - memcpy(request_buff[request_head], buff, len); - request_head++; - if (request_head == DAP_PACKET_COUNT) { - request_head = 0; - } - if (request_head == request_tail) { - request_full = 1; - } -} - - -// Process USB HID Data -void process_data(void) { +extern void process_data(uint8_t *req, uint8_t *resp); +void process_data_socket(void) { static uint8_t buff[DAP_PACKET_SIZE]; static int buff_pos = 0; @@ -167,15 +114,24 @@ void process_data(void) { if( csockfd == -1 ){ wait_for_client(); } - n = read(csockfd,buff+buff_pos,DAP_PACKET_SIZE); + n = read(csockfd,buff+buff_pos,DAP_PACKET_SIZE-buff_pos); if ( n==0 ) { }else if( n>0 ) { buff_pos += n; if( buff_pos==DAP_PACKET_SIZE ) { - buffer_request(buff,DAP_PACKET_SIZE); + process_data(buff,response_buff); /*DAP_PACKET_SIZE*/ buff_pos=0; + if( csockfd != -1 ) { + // write a response + n = write(csockfd,response_buff,DAP_PACKET_SIZE); + if (n != DAP_PACKET_SIZE){ + DPRINTF("ERROR writing to socket: %s\n", strerror(errno)); + close_socket(); + } + + } } }else if( n<0 ){ switch( errno ) { @@ -187,38 +143,17 @@ void process_data(void) { } /*no break*/ default: - DEBUGF("A problem occured: %d %d %s\n", n, errno,strerror(errno) ); - DEBUG("Closing connection!\n"); + DPRINTF("A problem occured: %d %d %s\n", n, errno,strerror(errno) ); + DPRINT("Closing connection!\n"); close_socket(csockfd); break; } } - // Process pending requests - if ((request_tail != request_head) || request_full) { - - // Process DAP Command and prepare response - DAP_ProcessCommand(request_buff[request_tail], response_buff[response_head]); - if( out_state_buff_pos != 0 ){ - FTDI_WRITE_OUT(); - } - - // remove a request - request_tail = (request_tail + 1) % DAP_PACKET_COUNT; - request_full = 0; - - if( csockfd != -1 ) { - // add a response - response_head = (response_head + 1) % DAP_PACKET_COUNT; - if (response_head == response_tail) { - response_full = 1; - } - write_response(); - } - } if(should_close) { close_socket(); should_close = 0; } } +#endif