forked from len0rd/rockbox
simplify and cleanup usb_serial.c (FS#10149 by Tomer Shalev)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20746 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
75a0a434d1
commit
4bff30a77f
1 changed files with 28 additions and 36 deletions
|
|
@ -158,19 +158,10 @@ void usb_serial_disconnect(void)
|
||||||
|
|
||||||
static void sendout(void)
|
static void sendout(void)
|
||||||
{
|
{
|
||||||
if(buffer_start+buffer_length >= BUFFER_SIZE)
|
buffer_transitlength = MIN(buffer_length,BUFFER_SIZE-buffer_start);
|
||||||
|
if(buffer_transitlength > 0)
|
||||||
{
|
{
|
||||||
/* Buffer wraps. Only send the first part */
|
buffer_length -= buffer_transitlength;
|
||||||
buffer_transitlength=BUFFER_SIZE - buffer_start;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Send everything */
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
@ -178,25 +169,30 @@ static void sendout(void)
|
||||||
|
|
||||||
void usb_serial_send(unsigned char *data,int length)
|
void usb_serial_send(unsigned char *data,int length)
|
||||||
{
|
{
|
||||||
if(!active)
|
int freestart, available_end_space, i;
|
||||||
|
|
||||||
|
if (!active||length<=0)
|
||||||
return;
|
return;
|
||||||
if(length<=0)
|
|
||||||
return;
|
i=buffer_start+buffer_length+buffer_transitlength;
|
||||||
int freestart=(buffer_start+buffer_length+buffer_transitlength)%BUFFER_SIZE;
|
freestart=i%BUFFER_SIZE;
|
||||||
if(buffer_start+buffer_transitlength+buffer_length >= BUFFER_SIZE)
|
available_end_space=BUFFER_SIZE-i;
|
||||||
|
|
||||||
|
if (0>=available_end_space)
|
||||||
{
|
{
|
||||||
/* current buffer wraps, so new data can't */
|
/* current buffer wraps, so new data can't wrap */
|
||||||
int available_space = BUFFER_SIZE - buffer_length - buffer_transitlength;
|
int available_space = BUFFER_SIZE -
|
||||||
length=MIN(length,available_space);
|
(buffer_length + buffer_transitlength);
|
||||||
memcpy(&send_buffer[freestart],
|
|
||||||
data,length);
|
length = MIN(length,available_space);
|
||||||
|
memcpy(&send_buffer[freestart],data,length);
|
||||||
buffer_length+=length;
|
buffer_length+=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_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;
|
||||||
buffer_length+=first_chunk;
|
buffer_length+=first_chunk;
|
||||||
|
|
@ -207,20 +203,18 @@ void usb_serial_send(unsigned char *data,int length)
|
||||||
buffer_length+=MIN(length,buffer_start);
|
buffer_length+=MIN(length,buffer_start);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(buffer_transitlength>0)
|
|
||||||
{
|
if (buffer_transitlength==0)
|
||||||
/* Do nothing. The transfer completion handler will pick it up */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sendout();
|
sendout();
|
||||||
}
|
/* else do nothing. The transfer completion handler will pick it up */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* called by usb_core_transfer_complete() */
|
/* called by usb_core_transfer_complete() */
|
||||||
void usb_serial_transfer_complete(int ep,int dir, int status, int length)
|
void usb_serial_transfer_complete(int ep,int dir, int status, int length)
|
||||||
{
|
{
|
||||||
(void)ep;
|
(void)ep;
|
||||||
|
(void)length;
|
||||||
|
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case USB_DIR_OUT:
|
case USB_DIR_OUT:
|
||||||
logf("serial: %s", receive_buffer);
|
logf("serial: %s", receive_buffer);
|
||||||
|
|
@ -234,11 +228,9 @@ void usb_serial_transfer_complete(int ep,int dir, int status, int length)
|
||||||
/* Data sent out. Update circular buffer */
|
/* Data sent out. Update circular buffer */
|
||||||
if(status == 0)
|
if(status == 0)
|
||||||
{
|
{
|
||||||
if(length!=buffer_transitlength)
|
/* TODO: Handle (length != buffer_transitlength) */
|
||||||
{
|
|
||||||
/* do something? */
|
buffer_start=(buffer_start+buffer_transitlength)%BUFFER_SIZE;
|
||||||
}
|
|
||||||
buffer_start = (buffer_start + buffer_transitlength)%BUFFER_SIZE;
|
|
||||||
buffer_transitlength = 0;
|
buffer_transitlength = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue