1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "string.h"
#include "system.h"
#include "usb_core.h"
#include "usb_drv.h"
#include "kernel.h"
//#define LOGF_ENABLE
#include "logf.h"
#ifdef USB_SERIAL
#define BUFFER_SIZE 512 /* Max 16k because of controller limitations */
static unsigned char _send_buffer[BUFFER_SIZE] __attribute__((aligned(32)));
static unsigned char* send_buffer;
static unsigned char _receive_buffer[512] __attribute__((aligned(32)));
static unsigned char* receive_buffer;
static bool busy_sending = false;
static int buffer_start;
static int buffer_length;
static bool active = false;
static struct mutex sendlock;
/* called by usb_code_init() */
void usb_serial_init(void)
{
logf("serial: init");
send_buffer = (void*)UNCACHED_ADDR(&_send_buffer);
receive_buffer = (void*)UNCACHED_ADDR(&_receive_buffer);
busy_sending = false;
buffer_start = 0;
buffer_length = 0;
active = true;
mutex_init(&sendlock);
}
void usb_serial_exit(void)
{
active = false;
}
static void sendout(void)
{
if(buffer_start+buffer_length > BUFFER_SIZE)
{
/* Buffer wraps. Only send the first part */
usb_drv_send_nonblocking(EP_SERIAL, &send_buffer[buffer_start],(BUFFER_SIZE - buffer_start));
}
else
{
/* Send everything */
usb_drv_send_nonblocking(EP_SERIAL, &send_buffer[buffer_start],buffer_length);
}
busy_sending=true;
}
void usb_serial_send(unsigned char *data,int length)
{
if(!active)
return;
if(length<=0)
return;
mutex_lock(&sendlock);
if(buffer_start+buffer_length > BUFFER_SIZE)
{
/* current buffer wraps, so new data can't */
int available_space = BUFFER_SIZE - buffer_length;
length=MIN(length,available_space);
memcpy(&send_buffer[(buffer_start+buffer_length)%BUFFER_SIZE],data,length);
buffer_length+=length;
}
else
{
/* current buffer doesn't wrap, so new data might */
int available_end_space = (BUFFER_SIZE - (buffer_start + buffer_length));
int first_chunk = MIN(length,available_end_space);
memcpy(&send_buffer[buffer_start + buffer_length],data,first_chunk);
length-=first_chunk;
buffer_length+=first_chunk;
if(length>0)
{
/* wrap */
memcpy(&send_buffer[0],&data[first_chunk],MIN(length,buffer_start));
buffer_length+=MIN(length,buffer_start);
}
}
if(busy_sending)
{
/* Do nothing. The transfer completion handler will pick it up */
}
else
{
sendout();
}
mutex_unlock(&sendlock);
}
/* called by usb_core_transfer_complete() */
void usb_serial_transfer_complete(bool in, int status, int length)
{
switch (in) {
case false:
logf("serial: %s", receive_buffer);
/* Data received. TODO : Do something with it ? */
usb_drv_recv(EP_SERIAL, receive_buffer, sizeof _receive_buffer);
break;
case true:
mutex_lock(&sendlock);
/* Data sent out. Update circular buffer */
if(status == 0)
{
buffer_start = (buffer_start + length)%BUFFER_SIZE;
buffer_length -= length;
}
busy_sending = false;
if(buffer_length>0)
{
sendout();
}
mutex_unlock(&sendlock);
break;
}
}
/* called by usb_core_control_request() */
bool usb_serial_control_request(struct usb_ctrlrequest* req)
{
bool handled = false;
switch (req->bRequest) {
case USB_REQ_SET_CONFIGURATION:
logf("serial: set config");
/* prime rx endpoint */
usb_drv_recv(EP_SERIAL, receive_buffer, sizeof _receive_buffer);
handled = true;
/* we come here too after a bus reset, so reset some data */
mutex_lock(&sendlock);
busy_sending = false;
if(buffer_length>0)
{
sendout();
}
mutex_unlock(&sendlock);
break;
default:
logf("serial: unhandeld req %d", req->bRequest);
}
return handled;
}
#endif /*USB_SERIAL*/
|