-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcc1111_vcom.c
470 lines (420 loc) · 11.3 KB
/
cc1111_vcom.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* CC Bootloader - USB CDC class (serial) driver
*
* Adapted from AltOS code by Fergus Noble (c) 2011
* AltOS code Copyright © 2009 Keith Packard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "cc1111.h"
#include "cc1111_vcom.h"
static __xdata u16 usb_in_bytes;
static __xdata u16 usb_in_bytes_last;
static __xdata u16 usb_out_bytes;
volatile static __xdata u8 usb_iif;
static __xdata u8 usb_running;
static void vcom_set_interrupts()
{
// IN interrupts on the control an IN endpoints
USBIIE = (1 << USB_CONTROL_EP) | (1 << USB_IN_EP);
// OUT interrupts on the OUT endpoint
USBOIE = (1 << USB_OUT_EP);
// Only care about reset
USBCIE = USBCIE_RSTIE;
}
struct usb_setup {
u8 dir_type_recip;
u8 request;
u16 value;
u16 index;
u16 length;
} __xdata usb_setup;
__xdata u8 usb_ep0_state;
u8 * __xdata usb_ep0_in_data;
__xdata u8 usb_ep0_in_len;
__xdata u8 usb_ep0_in_buf[2];
__xdata u8 usb_ep0_out_len;
__xdata u8 *__xdata usb_ep0_out_data;
__xdata u8 usb_configuration;
// Send an IN data packet
static void vcom_ep0_flush()
{
__xdata u8 this_len;
__xdata u8 cs0;
// If the IN packet hasn't been picked up, just return
USBINDEX = 0;
cs0 = USBCS0;
if (cs0 & USBCS0_INPKT_RDY)
return;
this_len = usb_ep0_in_len;
if (this_len > USB_CONTROL_SIZE)
this_len = USB_CONTROL_SIZE;
cs0 = USBCS0_INPKT_RDY;
if (this_len != USB_CONTROL_SIZE) {
cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
usb_ep0_state = USB_EP0_IDLE;
}
usb_ep0_in_len -= this_len;
while (this_len--)
USBFIFO[0] = *usb_ep0_in_data++;
USBINDEX = 0;
USBCS0 = cs0;
}
__xdata static struct usb_line_coding usb_line_codings = {115200, 0, 0, 8};
// Walk through the list of descriptors and find a match
static void vcom_get_descriptor(u16 value)
{
const u8 *__xdata descriptor;
__xdata u8 type = value >> 8;
__xdata u8 index = value;
descriptor = usb_descriptors;
while (descriptor[0] != 0) {
if (descriptor[1] == type && index-- == 0) {
if (type == USB_DESC_CONFIGURATION)
usb_ep0_in_len = descriptor[2];
else
usb_ep0_in_len = descriptor[0];
usb_ep0_in_data = descriptor;
break;
}
descriptor += descriptor[0];
}
}
// Read data from the ep0 OUT fifo
static void vcom_ep0_fill()
{
__xdata u8 len;
USBINDEX = 0;
len = USBCNT0;
if (len > usb_ep0_out_len)
len = usb_ep0_out_len;
usb_ep0_out_len -= len;
while (len--)
*usb_ep0_out_data++ = USBFIFO[0];
}
void vcom_ep0_queue_byte (u8 a)
{
usb_ep0_in_buf[usb_ep0_in_len++] = a;
}
void vcom_set_address (u8 address)
{
usb_running = 1;
USBADDR = address | 0x80;
while (USBADDR & 0x80) {}
}
static void vcom_set_configuration()
{
// Set the IN max packet size, double buffered
USBINDEX = USB_IN_EP;
USBMAXI = USB_IN_SIZE >> 3;
USBCSIH |= USBCSIH_IN_DBL_BUF;
// Set the OUT max packet size, double buffered
USBINDEX = USB_OUT_EP;
USBMAXO = USB_OUT_SIZE >> 3;
USBCSOH = USBCSOH_OUT_DBL_BUF;
}
static void vcom_ep0_setup()
{
// Pull the setup packet out of the fifo
usb_ep0_out_data = (__xdata u8 *) &usb_setup;
usb_ep0_out_len = 8;
vcom_ep0_fill();
if (usb_ep0_out_len != 0)
return;
// Figure out how to ACK the setup packet
if (usb_setup.dir_type_recip & USB_DIR_IN) {
if (usb_setup.length)
usb_ep0_state = USB_EP0_DATA_IN;
else
usb_ep0_state = USB_EP0_IDLE;
} else {
if (usb_setup.length)
usb_ep0_state = USB_EP0_DATA_OUT;
else
usb_ep0_state = USB_EP0_IDLE;
}
USBINDEX = 0;
if (usb_ep0_state == USB_EP0_IDLE)
USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
else
USBCS0 = USBCS0_CLR_OUTPKT_RDY;
usb_ep0_in_data = usb_ep0_in_buf;
usb_ep0_in_len = 0;
switch(usb_setup.dir_type_recip & USB_SETUP_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch(usb_setup.dir_type_recip & USB_SETUP_RECIP_MASK) {
case USB_RECIP_DEVICE:
switch(usb_setup.request) {
case USB_REQ_GET_STATUS:
vcom_ep0_queue_byte(0);
vcom_ep0_queue_byte(0);
break;
case USB_REQ_SET_ADDRESS:
vcom_set_address(usb_setup.value);
break;
case USB_REQ_GET_DESCRIPTOR:
vcom_get_descriptor(usb_setup.value);
break;
case USB_REQ_GET_CONFIGURATION:
vcom_ep0_queue_byte(usb_configuration);
break;
case USB_REQ_SET_CONFIGURATION:
usb_configuration = usb_setup.value;
vcom_set_configuration();
break;
}
break;
case USB_RECIP_INTERFACE:
#pragma disable_warning 110
switch(usb_setup.request) {
case USB_REQ_GET_STATUS:
vcom_ep0_queue_byte(0);
vcom_ep0_queue_byte(0);
break;
case USB_REQ_GET_INTERFACE:
vcom_ep0_queue_byte(0);
break;
case USB_REQ_SET_INTERFACE:
break;
}
break;
case USB_RECIP_ENDPOINT:
switch(usb_setup.request) {
case USB_REQ_GET_STATUS:
vcom_ep0_queue_byte(0);
vcom_ep0_queue_byte(0);
break;
}
break;
}
break;
case USB_TYPE_CLASS:
switch (usb_setup.request) {
case SET_LINE_CODING:
usb_ep0_out_len = 7;
usb_ep0_out_data = (__xdata u8 *) &usb_line_codings;
break;
case GET_LINE_CODING:
usb_ep0_in_len = 7;
usb_ep0_in_data = (u8 *) &usb_line_codings;
break;
case SET_CONTROL_LINE_STATE:
break;
}
break;
}
if (usb_ep0_state != USB_EP0_DATA_OUT) {
if (usb_setup.length < usb_ep0_in_len)
usb_ep0_in_len = usb_setup.length;
vcom_ep0_flush();
}
}
// End point 0 receives all of the control messages.
// This function must be called periodically to process ep0 messages.
static void vcom_ep0()
{
__xdata u8 cs0;
// If the ep0 flag has been set by the USB interrupt then do some processing
if (usb_iif & 1)
{
usb_iif &= ~1; // clear flag
USBINDEX = 0;
cs0 = USBCS0;
if (cs0 & USBCS0_SETUP_END) {
usb_ep0_state = USB_EP0_IDLE;
USBCS0 = USBCS0_CLR_SETUP_END;
}
if (cs0 & USBCS0_SENT_STALL) {
usb_ep0_state = USB_EP0_IDLE;
USBCS0 &= ~USBCS0_SENT_STALL;
}
if (usb_ep0_state == USB_EP0_DATA_IN &&
(cs0 & USBCS0_INPKT_RDY) == 0)
{
vcom_ep0_flush();
}
if (cs0 & USBCS0_OUTPKT_RDY) {
switch (usb_ep0_state) {
case USB_EP0_IDLE:
vcom_ep0_setup();
break;
case USB_EP0_DATA_OUT:
vcom_ep0_fill();
if (usb_ep0_out_len == 0)
usb_ep0_state = USB_EP0_IDLE;
USBINDEX = 0;
if (usb_ep0_state == USB_EP0_IDLE)
USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
else
USBCS0 = USBCS0_CLR_OUTPKT_RDY;
break;
}
}
}
}
// This interrupt is shared with port 2,
// so when we hook that up, fix this
void vcom_isr() __interrupt 6
{
USBIF = 0;
usb_iif |= USBIIF;
vcom_ep0();
if (USBCIF & USBCIF_RSTIF)
vcom_set_interrupts();
}
// Wait for a free IN buffer
static void vcom_in_wait()
{
while (1) {
USBINDEX = USB_IN_EP;
if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
break;
while (!(usb_iif & (1 << USB_IN_EP))) {}
}
}
// Send the current IN packet
static void vcom_in_send()
{
USBINDEX = USB_IN_EP;
USBCSIL |= USBCSIL_INPKT_RDY;
usb_in_bytes_last = usb_in_bytes;
usb_in_bytes = 0;
}
void vcom_flush()
{
if (!usb_running)
return;
// If there are pending bytes, or if the last packet was full,
// send another IN packet
if (usb_in_bytes || (usb_in_bytes_last == USB_IN_SIZE)) {
vcom_in_wait();
vcom_in_send();
}
}
void vcom_putchar(char c) __reentrant
{
if (!usb_running)
return;
vcom_in_wait();
// Queue a byte, sending the packet when full
USBFIFO[USB_IN_EP << 1] = c;
if (++usb_in_bytes == USB_IN_SIZE)
vcom_in_send();
}
char vcom_pollchar()
{
char c;
if (usb_out_bytes == 0) {
USBINDEX = USB_OUT_EP;
if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
return USB_READ_AGAIN;
usb_out_bytes = (USBCNTH << 8) | USBCNTL;
if (usb_out_bytes == 0) {
USBINDEX = USB_OUT_EP;
USBCSOL &= ~USBCSOL_OUTPKT_RDY;
return USB_READ_AGAIN;
}
}
--usb_out_bytes;
c = USBFIFO[USB_OUT_EP << 1];
if (usb_out_bytes == 0) {
USBINDEX = USB_OUT_EP;
USBCSOL &= ~USBCSOL_OUTPKT_RDY;
}
return c;
}
char vcom_getchar()
{
char c;
while ((c = vcom_pollchar()) == USB_READ_AGAIN)
{
while (!(USBOIF & (1 << USB_OUT_EP))) {}
}
return c;
}
void vcom_enable()
{
// Turn on the USB controller
SLEEP |= SLEEP_USB_EN;
vcom_set_configuration();
vcom_set_interrupts();
// enable USB interrupts
IEN2 |= IEN2_USBIE;
// Clear any pending interrupts
USBCIF = 0;
USBOIF = 0;
USBIIF = 0;
USBIF = 0;
}
void vcom_disable()
{
// Disable USB interrupts
USBIIE = 0;
USBOIE = 0;
USBCIE = 0;
IEN2 &= ~IEN2_USBIE;
// Clear any pending interrupts
USBCIF = 0;
USBOIF = 0;
USBIIF = 0;
// Turn off the USB controller
SLEEP &= ~SLEEP_USB_EN;
}
void initUSB()
{
// Init ep0
usb_ep0_state = USB_EP0_IDLE;
usb_iif = 0;
vcom_enable();
vcom_up();
}
void usbProcessEvents()
{
return; /* dummy function */
}
void vcom_readline(char* buff) {
char c;
while ((c = vcom_getchar()) != '\n') {
*buff++ = c;
}
*buff = 0;
}
void vcom_putstr(char* buff) {
while (*buff) {
vcom_putchar(*buff++);
}
vcom_flush();
}
void vcom_up() {
// Bring up the USB link
P1DIR |= 0x02;
P1_1 = 1;
}
void vcom_down() {
// Bring down the USB link
P1_1 = 0;
P1DIR &= ~0x02;
}
void txdata(u8 app, u8 cmd, u16 len, __xdata u8* dataptr)
{
u16 test = 0;
/*removes warning */
test = app = cmd = len;
/* function from usb thing, only need data ptr */
while (*dataptr)
{
vcom_putchar(*dataptr++);
}
vcom_flush();
}