avrftdi: Set guard time to fixed 2 bits.

avrftdi_private.h, avrftdi.c: remove pdata->guard_bits refernces, since
they are no longer needed
avrftdi_tpi.c: Use fixed guard time of 2 bits, this gives 16 bits per
frame. tpi_byte2frame() and tpi_frame2byte() functions are now
symmetrical and extraction of received data in avrftdi_read_byte is less
complicted. Write the new guard time to the TPI in
avrftdi_tpi_program_enable().

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1150 81a1dc3b-b13d-400b-aceb-764788c761c2
This commit is contained in:
hweisbach 2013-05-02 11:06:52 +00:00
parent d61242c19c
commit 8b4c54e37f
3 changed files with 28 additions and 46 deletions

View File

@ -1296,7 +1296,6 @@ avrftdi_setup(PROGRAMMER * pgm)
pdata->pin_value = 0; pdata->pin_value = 0;
pdata->pin_direction = 0; pdata->pin_direction = 0;
pdata->led_mask = 0; pdata->led_mask = 0;
pdata->guard_bits = 128 + 2;
pdata->set_pin = &set_pin; pdata->set_pin = &set_pin;
} }

View File

@ -50,8 +50,6 @@ typedef struct avrftdi_s {
int pin_limit; int pin_limit;
/* internal RX buffer of the device. needed for INOUT transfers */ /* internal RX buffer of the device. needed for INOUT transfers */
int rx_buffer_size; int rx_buffer_size;
/* number of guard bits for TPI. should be moved to struct PROGRAMMER */
int guard_bits;
/* function pointer to the set_pin function, so that we do not have to drag /* function pointer to the set_pin function, so that we do not have to drag
* it into global scope. it's a hack, but i think it's slightly better than * it into global scope. it's a hack, but i think it's slightly better than
* the alternative. * the alternative.

View File

@ -104,7 +104,7 @@ avrftdi_tpi_initialize(PROGRAMMER * pgm, AVRPART * p)
return ret; return ret;
} }
#define TPI_BIT_PAR 0x2000 #define TPI_PARITY_MASK 0x2000
static uint16_t static uint16_t
tpi_byte2frame(uint8_t byte) tpi_byte2frame(uint8_t byte)
@ -115,7 +115,7 @@ tpi_byte2frame(uint8_t byte)
frame |= ((byte << 5) & 0x1fe0); frame |= ((byte << 5) & 0x1fe0);
if(parity) if(parity)
frame |= TPI_BIT_PAR; frame |= TPI_PARITY_MASK;
return frame; return frame;
} }
@ -123,20 +123,15 @@ tpi_byte2frame(uint8_t byte)
static int static int
tpi_frame2byte(uint16_t frame, uint8_t * byte) tpi_frame2byte(uint16_t frame, uint8_t * byte)
{ {
/* drop partity + 2 stop bits */ /* drop idle and start bit(s) */
*byte = (frame >> 1) & 0xff; *byte = (frame >> 5) & 0xff;
int parity = __builtin_popcount(*byte) & 1; int parity = __builtin_popcount(*byte) & 1;
int parity_rcvd = (frame & 0x200) ? 1 : 0; int parity_rcvd = (frame & TPI_PARITY_MASK) ? 1 : 0;
log_info("Recevied frame with payload 0x%02x and %s parity\n",
*byte, parity ? "odd" : "even");
return parity != parity_rcvd; return parity != parity_rcvd;
} }
#define TPI_FRAME_SIZE 2
static int static int
avrftdi_tpi_break(PROGRAMMER * pgm) avrftdi_tpi_break(PROGRAMMER * pgm)
{ {
@ -172,28 +167,19 @@ avrftdi_tpi_write_byte(PROGRAMMER * pgm, unsigned char byte)
return 0; return 0;
} }
#define TPI_FRAME_SIZE 12
#define TPI_IDLE_BITS 2
static int static int
avrftdi_tpi_read_byte(PROGRAMMER * pgm, unsigned char * byte) avrftdi_tpi_read_byte(PROGRAMMER * pgm, unsigned char * byte)
{ {
uint16_t frame; uint16_t frame;
int guard_bits = to_pdata(pgm)->guard_bits; /* use 2 guard bits, 2 default idle bits + 12 frame bits = 16 bits total */
int bytes = ((guard_bits + 7) / 8) + 2; const int bytes = 3;
int i = 0, n = 0; int err, i = 0;
/* worst case size: unsigned char buffer[4];
* - 128 guard bits
* - 2 default idle bits
* - 12 frame bits
* = 142 bits
*/
unsigned char buffer[18];
if(bytes > sizeof(buffer))
log_warn("Requested more bytes (%d) than available buffer space (%d)\n", bytes, sizeof(buffer));
log_info("Guard bit size (incl. default idle bits) is %d\n", guard_bits);
log_info("Reading %d bytes for guard bits + frame\n", bytes);
/* set it high, so the PDI won't detect we're driving the line */ /* set it high, so the PDI won't detect we're driving the line */
to_pdata(pgm)->set_pin(pgm, PIN_AVR_MOSI, ON); to_pdata(pgm)->set_pin(pgm, PIN_AVR_MOSI, ON);
@ -212,22 +198,24 @@ avrftdi_tpi_read_byte(PROGRAMMER * pgm, unsigned char * byte)
i = 0; i = 0;
do { do {
n = ftdi_read_data(to_pdata(pgm)->ftdic, &buffer[i], bytes - i); int err = ftdi_read_data(to_pdata(pgm)->ftdic, &buffer[i], bytes - i);
E(n < 0, to_pdata(pgm)->ftdic); E(err < 0, to_pdata(pgm)->ftdic);
i += n; i += err;
} while(i < bytes); } while(i < bytes);
/* dismiss at least (guard_bits / 8) bytes */
i = guard_bits / 8;
frame = buffer[i] | (buffer[i+1] << 8);
/* now shift the rest of guard bits out */ log_trace("MPSSE: 0x%02x 0x%02x 0x%02x 0x%02x (Read frame)\n",
i = guard_bits - (i*8); buffer[0], buffer[1], buffer[2], buffer[3]);
frame >>= i;
log_info("Received frame 0x%04x (LSB first)\n", frame);
return tpi_frame2byte(frame, byte); frame = buffer[0] | (buffer[1] << 8);
err = tpi_frame2byte(frame, byte);
log_trace("Frame: 0x%04x, byte: 0x%02x\n", frame, *byte);
//avrftdi_debug_frame(frame);
return err;
} }
int int
@ -240,12 +228,9 @@ avrftdi_tpi_program_enable(PROGRAMMER * pgm, AVRPART * p)
log_info("TPI program enable\n"); log_info("TPI program enable\n");
//TODO determine guard time:
//-disable output possible -> guard time
//-else: minimum guard time
/* set guard time */ /* set guard time */
//avrftdi_tpi_write_byte(pgm, TPI_OP_SSTCS(TPIPCR)); avrftdi_tpi_write_byte(pgm, TPI_OP_SSTCS(TPIPCR));
//avrftdi_tpi_write_byte(pgm, TPIPCR_GT_2b); avrftdi_tpi_write_byte(pgm, TPIPCR_GT_2b);
/* send SKEY */ /* send SKEY */
avrftdi_tpi_write_byte(pgm, TPI_CMD_SKEY); avrftdi_tpi_write_byte(pgm, TPI_CMD_SKEY);