diff --git a/src/DAP_config.h b/src/DAP_config.h index 342089b..f21e224 100644 --- a/src/DAP_config.h +++ b/src/DAP_config.h @@ -38,6 +38,7 @@ Provides definitions about: #include "ftdi_vars.h" #include "device.h" // Debug Unit Cortex-M Processor Header File #include "Debug.h" +#include "Timer.h" #include #include @@ -138,29 +139,29 @@ of the same I/O port. The following SWDIO I/O Pin functions are provided: // Configure DAP I/O pins ------------------------------ -#define TMS_SWDIO DCD -#define TCK_SWCLK RI +#define TMS_SWDIO RI +#define TCK_SWCLK TXD #define TDO_SWO RXD -#define TDI RTS +#define TDI DCD #define nRESET DTR -#define nTRST TXD +#define nTRST RTS #define LED_CONN CTS #define LED_RUN DSR static __inline const char* pin_to_str(uint8_t pin){ switch(pin){ case TMS_SWDIO: - return "TMS_SWDIO(DCD)"; + return "TMS_SWDIO(RI)"; case TCK_SWCLK: - return "TCK_SWCLK(RI)"; + return "TCK_SWCLK(TXD)"; case TDO_SWO: return "TDO_SWO(RXD)"; case TDI: - return "TDI(RTS)"; + return "TDI(DCD)"; case nRESET: return "nRESET(DTR)"; case nTRST: - return "nTRST(TXD)"; + return "nTRST(RTS)"; case LED_CONN: return "LED_CONN(CTS)"; case LED_RUN: @@ -173,7 +174,7 @@ static __inline const char* pin_to_str(uint8_t pin){ static __inline void FTDI_BITBANG_SETUP() { int success; /* Enable bitbang mode */ - success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_BITBANG ); + success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_SYNCBB ); if( success<0 ){ DEBUG("Could not change to async. bitbang: %s\n",ftdi_get_error_string(&__ftdic)); } @@ -185,52 +186,38 @@ static __inline void FTDI_BITBANG_SETUP() { ftdi_write_data_set_chunksize(&__ftdic,1);*/ } -/** Setup of the Debug Unit I/O pins and LEDs (called when Debug Unit is initialized). -This function performs the initialization of the CMSIS-DAP Hardware I/O Pins and the -Status LEDs. In detail the operation of Hardware I/O and LED pins are enabled and set: - - I/O clock system enabled. - - all I/O pins: input buffer enabled, output pins are set to HighZ mode. - - 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) { - /* 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) { - DEBUG("Can't open device [%x:%x]: %s\n",VID,PID,ftdi_get_error_string(&__ftdic)); - exit(1); - } - - dir_state = LED_CONN|LED_RUN; - out_state = 0xFF & ~(LED_CONN|LED_RUN); - FTDI_BITBANG_SETUP(); - success = ftdi_set_baudrate (&__ftdic, DAP_DEFAULT_SWJ_CLOCK/2); - if( success < 0){ - DEBUG("Could not set baudrate: %s\n",ftdi_get_error_string(&__ftdic)); +static __forceinline void FTDI_WRITE_OUT_READ_MODE( ) { + int success = ftdi_write_data(&__ftdic,out_state_buff,out_state_buff_pos); + if( success<0 ){ + DEBUG("Could not write pins: %s\n",ftdi_get_error_string(&__ftdic)); + exit(1); } - uint8_t foo; - success = ftdi_read_data(&__ftdic,&foo,1); } - static __forceinline void FTDI_WRITE_OUT( ) { int success; if( out_state_buff_pos==0 ){ DEBUG("buffer is empty %d \n",out_state_buff_pos); return; } - success = ftdi_write_data(&__ftdic,out_state_buff,out_state_buff_pos); - if( success<0 ){ - DEBUG("Could not write pins: %s\n",ftdi_get_error_string(&__ftdic)); + FTDI_WRITE_OUT_READ_MODE( ); + + /* every generates the same amout of reads */ + + success = 0; + while(success == 0) { + success = ftdi_read_data(&__ftdic,in_buff,out_state_buff_pos); + } + if( success != out_state_buff_pos ){ + DEBUG("Could not read pins: %s\n",ftdi_get_error_string(&__ftdic)); exit(1); } - out_state_buff_pos=0; + + out_state_buff_pos = 0; } + static __forceinline void FTDI_SUBMIT( ) { out_state_buff[out_state_buff_pos] = out_state; out_state_buff_pos++; @@ -240,39 +227,13 @@ static __forceinline void FTDI_SUBMIT( ) { } } -/** Setup JTAG I/O pins: TCK, TMS, TDI, TDO, nTRST, and nRESET. -Configures the DAP Hardware I/O pins for JTAG mode: - - TCK, TMS, TDI, nTRST, nRESET to output mode and set to high level. - - TDO to input mode. -*/ -static __inline void PORT_JTAG_SETUP (void) { - /* Initialize context for subsequent function calls */ - dir_state = LED_CONN|LED_RUN|TCK_SWCLK|TMS_SWDIO|TDI|nRESET;; - out_state = TCK_SWCLK | TMS_SWDIO | TDI | nTRST | nRESET; - FTDI_BITBANG_SETUP(); -} - - -/** Setup SWD I/O pins: SWCLK, SWDIO, and nRESET. -Configures the DAP Hardware I/O pins for Serial Wire Debug (SWD) mode: - - SWCLK, SWDIO, nRESET to output mode and set to default high level. - - TDI, TMS, nTRST to HighZ mode (pins are unused in SWD mode). -*/ -static __inline void PORT_SWD_SETUP (void) { - /* Initialize context for subsequent function calls */ - dir_state = LED_CONN|LED_RUN|TCK_SWCLK|TMS_SWDIO|nRESET; - out_state = TCK_SWCLK | TMS_SWDIO | nRESET; - FTDI_BITBANG_SETUP(); -} - static __inline void FTDI_BEGIN_READ(){ /* submit signal change buffer */ FTDI_SUBMIT(); FTDI_WRITE_OUT(); /* flush read buffer */ - int success; - success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_SYNCBB ); + int success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_SYNCBB ); if( success<0 ){ DEBUG("Could not change to sync. bitbang: %s\n",ftdi_get_error_string(&__ftdic)); } @@ -280,6 +241,7 @@ static __inline void FTDI_BEGIN_READ(){ if( success<0 ){ DEBUG("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; } @@ -287,9 +249,11 @@ static __inline void FTDI_BEGIN_READ(){ static __inline void FTDI_END_READ(){ int success, remaining; uint32_t read_size; + read_size = remaining = out_state_buff_pos; - FTDI_SUBMIT(); - FTDI_WRITE_OUT(); + FTDI_WRITE_OUT_READ_MODE(); + out_state_buff_pos = 0; + success = 0; uint8_t *ptr = in_buff; while( remaining ){ @@ -303,30 +267,128 @@ static __inline void FTDI_END_READ(){ printf("try %d\n", success); } printf("Read %d/%d bytes\n",(int)(ptr-in_buff),read_size); - if( read_size!=success ){ - DEBUG("Not sufficient bytes read: %s\n",ftdi_get_error_string(&__ftdic)); - }else{ - DEBUG("Read %d bytes\n",read_size); - int i; - for(i=0;i>7); } + printf("\n"); - success = ftdi_set_bitmode(&__ftdic, dir_state, BITMODE_BITBANG ); + for(i=0;i> 3; \ + SW_WRITE_BIT(bit); /* A3 Bit */ \ parity += bit; \ SW_WRITE_BIT(parity); /* Parity Bit */ \ SW_WRITE_BIT(0); /* Stop Bit */ \ SW_WRITE_BIT(1); /* Park Bit */ \ \ /* Turnaround */ \ + FTDI_BEGIN_READ(); \ PIN_SWDIO_OUT_DISABLE(); \ for (n = DAP_Data.swd_conf.turnaround; n; n--) { \ SW_CLOCK_CYCLE(); \ diff --git a/src/ftdi_vars.c b/src/ftdi_vars.c index 0b85c3e..a0b0cb9 100644 --- a/src/ftdi_vars.c +++ b/src/ftdi_vars.c @@ -21,7 +21,7 @@ int out_state_buff_pos = 0; uint8_t in_buff[BUFFER_SIZE]; uint8_t *read_slot; -red_pos_t pin_read_buff[BUFFER_SIZE]; +read_pos_t pin_read_buff[BUFFER_SIZE]; int pin_read_pos = 0; uint8_t pin_read_mask = 1; diff --git a/src/ftdi_vars.h b/src/ftdi_vars.h index b256d0b..9653396 100644 --- a/src/ftdi_vars.h +++ b/src/ftdi_vars.h @@ -23,9 +23,10 @@ typedef struct { uint8_t* ptr; - uint8_t bitmask; + uint8_t write_bitmask; + uint8_t read_bitmask; int pos; -} red_pos_t; +} read_pos_t; extern struct ftdi_context __ftdic; @@ -40,7 +41,7 @@ extern int out_state_buff_pos; extern uint8_t in_buff[BUFFER_SIZE]; extern uint8_t *read_slot; -extern red_pos_t pin_read_buff[BUFFER_SIZE]; +extern read_pos_t pin_read_buff[BUFFER_SIZE]; extern int pin_read_pos; extern uint8_t pin_read_mask;