1
0
Fork 0
forked from len0rd/rockbox

more fixes. There's still something wrong as it sometimes suddenly stops sending data. Disconnecting and reconnecting gets it going again

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20072 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Frank Gevaerts 2009-02-21 00:28:40 +00:00
parent b51785008b
commit 88eb592cf4

View file

@ -56,7 +56,7 @@ static struct usb_endpoint_descriptor __attribute__((aligned(2))) endpoint_descr
.bInterval = 0 .bInterval = 0
}; };
#define BUFFER_SIZE 512 #define BUFFER_SIZE 150
#if CONFIG_CPU == IMX31L #if CONFIG_CPU == IMX31L
static unsigned char send_buffer[BUFFER_SIZE] static unsigned char send_buffer[BUFFER_SIZE]
USBDEVBSS_ATTR __attribute__((aligned(32))); USBDEVBSS_ATTR __attribute__((aligned(32)));
@ -69,7 +69,8 @@ static unsigned char receive_buffer[32]
USB_DEVBSS_ATTR __attribute__((aligned(32))); USB_DEVBSS_ATTR __attribute__((aligned(32)));
#endif #endif
static bool busy_sending = false; static void sendout(void);
static int buffer_start; static int buffer_start;
/* The number of bytes to transfer that haven't been given to the USB stack yet */ /* The number of bytes to transfer that haven't been given to the USB stack yet */
static int buffer_length; static int buffer_length;
@ -80,8 +81,6 @@ static bool active = false;
static int ep_in, ep_out; static int ep_in, ep_out;
static int usb_interface; static int usb_interface;
static struct mutex sendlock SHAREDBSS_ATTR;
int usb_serial_request_endpoints(struct usb_class_driver *drv) int usb_serial_request_endpoints(struct usb_class_driver *drv)
{ {
ep_in = usb_core_request_endpoint(USB_DIR_IN, drv); ep_in = usb_core_request_endpoint(USB_DIR_IN, drv);
@ -144,26 +143,22 @@ void usb_serial_init_connection(void)
usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer); usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer);
/* we come here too after a bus reset, so reset some data */ /* we come here too after a bus reset, so reset some data */
mutex_lock(&sendlock); buffer_transitlength = 0;
busy_sending = false;
if(buffer_length>0) if(buffer_length>0)
{ {
sendout(); sendout();
} }
active=true; active=true;
mutex_unlock(&sendlock);
} }
/* called by usb_code_init() */ /* called by usb_code_init() */
void usb_serial_init(void) void usb_serial_init(void)
{ {
logf("serial: init"); logf("serial: init");
busy_sending = false;
buffer_start = 0; buffer_start = 0;
buffer_length = 0; buffer_length = 0;
buffer_transit_length = 0; buffer_transitlength = 0;
active = true; active = true;
mutex_init(&sendlock);
} }
void usb_serial_disconnect(void) void usb_serial_disconnect(void)
@ -173,7 +168,7 @@ void usb_serial_disconnect(void)
static void sendout(void) static void sendout(void)
{ {
if(buffer_start+buffer_length > BUFFER_SIZE) if(buffer_start+buffer_length >= BUFFER_SIZE)
{ {
/* Buffer wraps. Only send the first part */ /* Buffer wraps. Only send the first part */
buffer_transitlength=BUFFER_SIZE - buffer_start; buffer_transitlength=BUFFER_SIZE - buffer_start;
@ -183,10 +178,12 @@ static void sendout(void)
/* Send everything */ /* Send everything */
buffer_transitlength=buffer_length; buffer_transitlength=buffer_length;
} }
if(buffer_transitlength>0)
{
buffer_length-=buffer_transitlength;
usb_drv_send_nonblocking(ep_in, &send_buffer[buffer_start], usb_drv_send_nonblocking(ep_in, &send_buffer[buffer_start],
buffer_transitlength); buffer_transitlength);
buffer_length-=buffer_transitlength; }
busy_sending=true;
} }
void usb_serial_send(unsigned char *data,int length) void usb_serial_send(unsigned char *data,int length)
@ -195,9 +192,8 @@ void usb_serial_send(unsigned char *data,int length)
return; return;
if(length<=0) if(length<=0)
return; return;
mutex_lock(&sendlock);
int freestart=(buffer_start+buffer_length+buffer_transitlength)%BUFFER_SIZE; int freestart=(buffer_start+buffer_length+buffer_transitlength)%BUFFER_SIZE;
if(buffer_start+buffer_transit_length+buffer_length > BUFFER_SIZE) if(buffer_start+buffer_transitlength+buffer_length >= BUFFER_SIZE)
{ {
/* current buffer wraps, so new data can't */ /* current buffer wraps, so new data can't */
int available_space = BUFFER_SIZE - buffer_length - buffer_transitlength; int available_space = BUFFER_SIZE - buffer_length - buffer_transitlength;
@ -209,7 +205,7 @@ void usb_serial_send(unsigned char *data,int length)
else else
{ {
/* current buffer doesn't wrap, so new data might */ /* current buffer doesn't wrap, so new data might */
int available_end_space = (BUFFER_SIZE - (buffer_start+buffer_length+buffer_transit_length)); int available_end_space = (BUFFER_SIZE - (buffer_start+buffer_length+buffer_transitlength));
int first_chunk = MIN(length,available_end_space); int first_chunk = MIN(length,available_end_space);
memcpy(&send_buffer[freestart],data,first_chunk); memcpy(&send_buffer[freestart],data,first_chunk);
length-=first_chunk; length-=first_chunk;
@ -221,7 +217,7 @@ void usb_serial_send(unsigned char *data,int length)
buffer_length+=MIN(length,buffer_start); buffer_length+=MIN(length,buffer_start);
} }
} }
if(busy_sending) if(buffer_transitlength>0)
{ {
/* Do nothing. The transfer completion handler will pick it up */ /* Do nothing. The transfer completion handler will pick it up */
} }
@ -229,7 +225,6 @@ void usb_serial_send(unsigned char *data,int length)
{ {
sendout(); sendout();
} }
mutex_unlock(&sendlock);
} }
/* called by usb_core_transfer_complete() */ /* called by usb_core_transfer_complete() */
@ -246,20 +241,21 @@ void usb_serial_transfer_complete(int ep,int dir, int status, int length)
break; break;
case USB_DIR_IN: case USB_DIR_IN:
mutex_lock(&sendlock);
/* Data sent out. Update circular buffer */ /* Data sent out. Update circular buffer */
if(status == 0) if(status == 0)
{ {
buffer_start = (buffer_start + length)%BUFFER_SIZE; if(length!=buffer_transitlength)
buffer_transitlength -= length; {
/* do something? */
}
buffer_start = (buffer_start + buffer_transitlength)%BUFFER_SIZE;
buffer_transitlength = 0;
} }
busy_sending = false;
if(buffer_length>0) if(buffer_length>0)
{ {
sendout(); sendout();
} }
mutex_unlock(&sendlock);
break; break;
} }
} }