Merge pull request #843 from mariusgreuel/pr-ftdi-syncbb

Fix receive buffer implementation in ftdi_syncbb programmer
This commit is contained in:
Jörg Wunsch 2022-01-27 23:15:10 +01:00 committed by GitHub
commit dd8c3ff830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 7 deletions

View File

@ -147,6 +147,7 @@ static struct {
static struct { static struct {
int discard; // # of bytes to discard during read int discard; // # of bytes to discard during read
int pending; // # of bytes that have been written since last read int pending; // # of bytes that have been written since last read
int len; // # of bytes in receive buffer
int wr; // write pointer int wr; // write pointer
int rd; // read pointer int rd; // read pointer
uint8_t buf[FT245R_BUFSIZE]; // receive ring buffer uint8_t buf[FT245R_BUFSIZE]; // receive ring buffer
@ -159,16 +160,19 @@ static int ft245r_tpi_rx(PROGRAMMER * pgm, uint8_t *bytep);
// Discard all data from the receive buffer. // Discard all data from the receive buffer.
static void ft245r_rx_buf_purge(PROGRAMMER * pgm) { static void ft245r_rx_buf_purge(PROGRAMMER * pgm) {
rx.len = 0;
rx.rd = rx.wr = 0; rx.rd = rx.wr = 0;
} }
static void ft245r_rx_buf_put(PROGRAMMER * pgm, uint8_t byte) { static void ft245r_rx_buf_put(PROGRAMMER * pgm, uint8_t byte) {
rx.len++;
rx.buf[rx.wr++] = byte; rx.buf[rx.wr++] = byte;
if (rx.wr >= sizeof(rx.buf)) if (rx.wr >= sizeof(rx.buf))
rx.wr = 0; rx.wr = 0;
} }
static uint8_t ft245r_rx_buf_get(PROGRAMMER * pgm) { static uint8_t ft245r_rx_buf_get(PROGRAMMER * pgm) {
rx.len--;
uint8_t byte = rx.buf[rx.rd++]; uint8_t byte = rx.buf[rx.rd++];
if (rx.rd >= sizeof(rx.buf)) if (rx.rd >= sizeof(rx.buf))
rx.rd = 0; rx.rd = 0;
@ -193,6 +197,20 @@ static int ft245r_fill(PROGRAMMER * pgm) {
return nread; return nread;
} }
static int ft245r_rx_buf_fill_and_get(PROGRAMMER* pgm)
{
while (rx.len == 0)
{
int result = ft245r_fill(pgm);
if (result < 0)
{
return result;
}
}
return ft245r_rx_buf_get(pgm);
}
/* Flush pending TX data to the FTDI send FIFO. */ /* Flush pending TX data to the FTDI send FIFO. */
static int ft245r_flush(PROGRAMMER * pgm) { static int ft245r_flush(PROGRAMMER * pgm) {
int rv, len = tx.len, avail; int rv, len = tx.len, avail;
@ -269,14 +287,32 @@ static int ft245r_recv(PROGRAMMER * pgm, unsigned char * buf, size_t len) {
__func__, rx.discard, len); __func__, rx.discard, len);
#endif #endif
while (rx.discard > 0) { while (rx.discard > 0) {
ft245r_rx_buf_get(pgm); int result = ft245r_rx_buf_fill_and_get(pgm);
if (result < 0)
{
return result;
}
--rx.discard; --rx.discard;
} }
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i)
buf[i] = ft245r_rx_buf_get(pgm); {
int result = ft245r_rx_buf_fill_and_get(pgm);
if (result < 0)
{
return result;
}
buf[i] = (uint8_t)result;
for (j = 1; j < baud_multiplier; ++j) for (j = 1; j < baud_multiplier; ++j)
ft245r_rx_buf_get(pgm); {
result = ft245r_rx_buf_fill_and_get(pgm);
if (result < 0)
{
return result;
}
}
} }
return 0; return 0;
} }