Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct flash paged write for avrftdi.c #1074

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/avrftdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
size_t blocksize = pdata->rx_buffer_size/2; // we are reading 2 bytes per data byte

// determine a maximum size of data block
size_t max_size = MIN(pdata->ftdic->max_packet_size,pdata->tx_buffer_size);
size_t max_size = MIN(pdata->ftdic->max_packet_size, (unsigned int) pdata->tx_buffer_size);
// select block size so that resulting commands does not exceed max_size if possible
blocksize = MAX(1,(max_size-7)/((8*2*6)+(8*1*2)));
//avrdude_message(MSG_INFO, "blocksize %d \n",blocksize);
Expand All @@ -368,9 +368,8 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
// (8*1) inputs per data byte, 2 transmit bytes per input (GET_BITS_LOW/HIGH),
// 1x SEND_IMMEDIATE
int len = 0;
int i;

for(i = 0 ; i< transfer_size; i++) {
for(size_t i = 0 ; i < transfer_size; i++) {
len += set_data(pgm, send_buffer + len, buf[written+i], (mode & MPSSE_DO_READ) != 0);
}

Expand All @@ -387,14 +386,14 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
E(ftdi_write_data(pdata->ftdic, send_buffer, len) != len, pdata->ftdic);
if (mode & MPSSE_DO_READ) {
int n;
int k = 0;
size_t k = 0;
do {
n = ftdi_read_data(pdata->ftdic, &recv_buffer[k], 2*16*transfer_size - k);
E(n < 0, pdata->ftdic);
k += n;
} while (k < transfer_size);

for(i = 0 ; i< transfer_size; i++) {
for(size_t i = 0 ; i< transfer_size; i++) {
data[written + i] = extract_data(pgm, recv_buffer, i);
}
}
Expand Down Expand Up @@ -437,15 +436,15 @@ static int avrftdi_transmit_mpsse(avrftdi_t* pdata, unsigned char mode, const un
{
size_t transfer_size = (remaining > blocksize) ? blocksize : remaining;

E(ftdi_write_data(pdata->ftdic, (unsigned char*)&buf[written], transfer_size) != transfer_size, pdata->ftdic);
E((size_t) ftdi_write_data(pdata->ftdic, (unsigned char*)&buf[written], transfer_size) != transfer_size, pdata->ftdic);
#if 0
if(remaining < blocksize)
E(ftdi_write_data(pdata->ftdic, &si, sizeof(si)) != sizeof(si), pdata->ftdic);
#endif

if (mode & MPSSE_DO_READ) {
int n;
int k = 0;
size_t k = 0;
do {
n = ftdi_read_data(pdata->ftdic, &data[written + k], transfer_size - k);
E(n < 0, pdata->ftdic);
Expand Down Expand Up @@ -1015,7 +1014,7 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
return -1;
}

if(page_size != m->page_size) {
if(page_size != (unsigned int) m->page_size) {
log_warn("Parameter page_size is %d, ", page_size);
log_warn("but m->page_size is %d. Using the latter.\n", m->page_size);
}
Expand Down Expand Up @@ -1059,12 +1058,11 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
/* find a poll byte. We cannot poll a value of 0xff, so look
* for a value != 0xff
*/
for(poll_index = addr+len-1; poll_index > addr-1; poll_index--)
for(poll_index = addr+len-1; poll_index+1 > addr; poll_index--)
if(m->buf[poll_index] != 0xff)
break;

if((poll_index < addr + len) && m->buf[poll_index] != 0xff)
{
if(poll_index+1 > addr) {
buf_size = bufptr - buf;

if(verbose > TRACE)
Expand Down Expand Up @@ -1105,12 +1103,11 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size, unsigned int addr, unsigned int len)
{
OPCODE * readop;
int byte, word;

unsigned int buf_size = 4 * len + 4;
unsigned char* o_buf = alloca(buf_size);
unsigned char* i_buf = alloca(buf_size);
unsigned int index;


memset(o_buf, 0, buf_size);
memset(i_buf, 0, buf_size);
Expand All @@ -1129,7 +1126,7 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
return -1;

/* word addressing! */
for(word = addr/2, index = 0; word < (addr + len)/2; word++)
for(unsigned int word = addr/2, index = 0; word < (addr + len)/2; word++)
{
/* one byte is transferred via a 4-byte opcode.
* TODO: reduce magic numbers
Expand Down Expand Up @@ -1160,7 +1157,7 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
memset(&m->buf[addr], 0, page_size);

/* every (read) op is 4 bytes in size and yields one byte of memory data */
for(byte = 0; byte < page_size; byte++) {
for(unsigned int byte = 0; byte < page_size; byte++) {
if(byte & 1)
readop = m->op[AVR_OP_READ_HI];
else
Expand Down