cp210x: handle 0 return from usb_bulk_read().

This fixes a problem with the Olimex MSP430-JTAG-TINY v1, reported by
Hardy Griech on mspgcc-users.
This commit is contained in:
Daniel Beer 2013-05-07 08:08:33 +12:00
parent cb4091f121
commit 947f72e5ea
1 changed files with 21 additions and 13 deletions

View File

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <usb.h> #include <usb.h>
#include <time.h>
#include "cp210x.h" #include "cp210x.h"
#include "util.h" #include "util.h"
@ -67,7 +68,7 @@ struct cp210x_transport {
#define CP210X_WRITE_DTR 0x0100 #define CP210X_WRITE_DTR 0x0100
#define CP210X_WRITE_RTS 0x0200 #define CP210X_WRITE_RTS 0x0200
#define TIMEOUT 30000 #define TIMEOUT_S 30
static int configure_port(struct cp210x_transport *tr, int baud_rate) static int configure_port(struct cp210x_transport *tr, int baud_rate)
{ {
@ -201,7 +202,7 @@ static int usbtr_send(transport_t tr_base, const uint8_t *data, int len)
#endif #endif
while (len) { while (len) {
sent = usb_bulk_write(tr->handle, V1_OUT_EP, sent = usb_bulk_write(tr->handle, V1_OUT_EP,
(char *)data, len, TIMEOUT); (char *)data, len, TIMEOUT_S * 1000);
if (sent <= 0) { if (sent <= 0) {
pr_error(__FILE__": can't send data"); pr_error(__FILE__": can't send data");
return -1; return -1;
@ -218,29 +219,36 @@ static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
{ {
struct cp210x_transport *tr = (struct cp210x_transport *)tr_base; struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
int rlen; int rlen;
time_t deadline = time(NULL) + TIMEOUT_S;
#ifdef DEBUG_CP210X #ifdef DEBUG_CP210X
printc(__FILE__": %s : read max %d\n", __FUNCTION__, max_len); printc(__FILE__": %s : read max %d\n", __FUNCTION__, max_len);
#endif #endif
while (time(NULL) < deadline) {
rlen = usb_bulk_read(tr->handle, V1_IN_EP, (char *)databuf, rlen = usb_bulk_read(tr->handle, V1_IN_EP, (char *)databuf,
max_len, TIMEOUT); max_len, TIMEOUT_S * 1000);
#ifdef DEBUG_CP210X #ifdef DEBUG_CP210X
printc(__FILE__": %s : read %d\n", __FUNCTION__, rlen); printc(__FILE__": %s : read %d\n", __FUNCTION__, rlen);
#endif #endif
if (rlen <= 0) { if (rlen < 0) {
pr_error(__FILE__": can't receive data"); pr_error(__FILE__": can't receive data");
return -1; return -1;
} }
if (rlen > 0) {
#ifdef DEBUG_CP210X #ifdef DEBUG_CP210X
debug_hexdump(__FILE__": USB transfer in", databuf, rlen); debug_hexdump(__FILE__": USB transfer in", databuf, rlen);
#endif #endif
return rlen; return rlen;
} }
}
pr_error(__FILE__": read operation timed out");
return -1;
}
static void usbtr_destroy(transport_t tr_base) static void usbtr_destroy(transport_t tr_base)
{ {