m:robe 100 manual work - it is now in a state comparable to the gigabeat one
[Rockbox.git] / firmware / ifp_usb_serial.c
blob635123baa95a745b34df0589ce067edda8bb7b60
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Tomasz Malesinski
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 #define LCD_DEBUG
22 #define BUTTONS
25 /* #include "config.h" */
26 #include <stdlib.h>
27 #include "pnx0101.h"
28 #include "ifp_usb_serial.h"
30 #ifdef BUTTONS
31 #include "kernel.h"
32 #include "button.h"
33 #include "system.h"
34 #endif
36 #ifdef LCD_DEBUG
37 #include "lcd.h"
38 #include "sprintf.h"
39 #endif
42 #define ISP1582_BASE (0x24100000)
43 #define ISP1582_ADDRESS (*(volatile unsigned char *)ISP1582_BASE)
44 #define ISP1582_MODE (*(volatile unsigned short *)(ISP1582_BASE + 0xc))
45 #define ISP1582_INTCONF (*(volatile unsigned char *)(ISP1582_BASE + 0x10))
46 #define ISP1582_OTG (*(volatile unsigned char *)(ISP1582_BASE + 0x12))
47 #define ISP1582_INTEN (*(volatile unsigned long *)(ISP1582_BASE + 0x14))
49 #define ISP1582_EPINDEX (*(volatile unsigned char *)(ISP1582_BASE + 0x2c))
50 #define ISP1582_CTRLFUN (*(volatile unsigned char *)(ISP1582_BASE + 0x28))
51 #define ISP1582_DATA (*(volatile unsigned short *)(ISP1582_BASE + 0x20))
52 #define ISP1582_BUFLEN (*(volatile unsigned short *)(ISP1582_BASE + 0x1c))
53 #define ISP1582_BUFSTAT (*(volatile unsigned char *)(ISP1582_BASE + 0x1e))
54 #define ISP1582_MAXPKSZ (*(volatile unsigned short *)(ISP1582_BASE + 0x04))
55 #define ISP1582_EPTYPE (*(volatile unsigned short *)(ISP1582_BASE + 0x08))
57 #define ISP1582_INT (*(volatile unsigned long *)(ISP1582_BASE + 0x18))
58 #define ISP1582_CHIPID (*(volatile unsigned long *)(ISP1582_BASE + 0x70))
59 #define ISP1582_FRAMENO (*(volatile unsigned short *)(ISP1582_BASE + 0x74))
60 #define ISP1582_UNLOCK (*(volatile unsigned short *)(ISP1582_BASE + 0x7c))
62 #define ISP1582_UNLOCK_CODE 0xaa37
64 #define DIR_RX 0
65 #define DIR_TX 1
67 #define TYPE_BULK 2
69 #define STATE_DEFAULT 0
70 #define STATE_ADDRESS 1
71 #define STATE_CONFIGURED 2
73 #define N_ENDPOINTS 2
75 struct usb_endpoint
77 unsigned char *out_buf;
78 short out_len;
79 short out_ptr;
80 void (*out_done)(int, unsigned char *, int);
81 unsigned char out_in_progress;
83 unsigned char *in_buf;
84 short in_min_len;
85 short in_max_len;
86 short in_ptr;
87 void (*in_done)(int, unsigned char *, int);
88 unsigned char in_ack;
90 unsigned char halt[2];
91 unsigned char enabled[2];
92 short max_pkt_size[2];
95 static char usb_connect_state;
97 static struct usb_endpoint endpoints[N_ENDPOINTS];
99 static unsigned char setup_pkt_buf[8];
100 static unsigned char setup_out_buf[8];
101 static unsigned char usb_state;
102 static unsigned char usb_remote_wakeup;
104 #ifdef LCD_DEBUG
105 static unsigned char int_count[32];
107 static int log_pos_x = 0;
108 static int log_pos_y = 3;
109 #endif
111 static void nop_f(void)
115 #ifdef LCD_DEBUG
116 static void log_char(char c)
118 char s[2];
120 s[0] = c;
121 s[1] = 0;
123 lcd_puts(log_pos_x, log_pos_y, s);
124 lcd_update();
125 log_pos_x++;
126 if (log_pos_x >= 16)
128 log_pos_x = 0;
129 log_pos_y++;
130 if (log_pos_y > 5)
131 log_pos_y = 3;
134 #else
135 #define log_char(c)
136 #endif
138 #define SERIAL_BUF_SIZE 1024
140 struct serial_fifo
142 unsigned char buf[SERIAL_BUF_SIZE];
143 short head, tail;
146 static struct serial_fifo serial_in_fifo;
147 static struct serial_fifo serial_out_fifo;
148 static unsigned char serial_in_pkt[64];
150 static unsigned char device_descriptor[18] = {
151 0x12, /* length */
152 0x01, /* descriptor type */
153 0x10, 0x01, /* USB version (1.1) */
154 0xff, 0xff, /* class and subclass */
155 0xff, /* protocol */
156 0x40, /* max packet size 0 */
157 0x02, 0x41, /* vendor (iRiver) */
158 0x07, 0xee, /* product (0xee07) */
159 0x01, 0x00, /* device version */
160 0x01, /* manufacturer string */
161 0x02, /* product string */
162 0x00, /* serial number string */
163 0x01 /* number of configurations */
166 static unsigned char cfg_descriptor[32] = {
167 0x09, /* length */
168 0x02, /* descriptor type */
169 0x20, 0x00, /* total length */
170 0x01, /* number of interfaces */
171 0x01, /* configuration value */
172 0x00, /* configuration string */
173 0x80, /* attributes (none) */
174 0x32, /* max power (100 mA) */
175 /* interface descriptor */
176 0x09, /* length */
177 0x04, /* descriptor type */
178 0x00, /* interface number */
179 0x00, /* alternate setting */
180 0x02, /* number of endpoints */
181 0xff, /* interface class */
182 0xff, /* interface subclass */
183 0xff, /* interface protocol */
184 0x00, /* interface string */
185 /* endpoint IN */
186 0x07, /* length */
187 0x05, /* descriptor type */
188 0x81, /* endpoint 1 IN */
189 0x02, /* attributes (bulk) */
190 0x40, 0x00, /* max packet size */
191 0x00, /* interval */
192 /* endpoint OUT */
193 0x07, /* length */
194 0x05, /* descriptor type */
195 0x01, /* endpoint 1 IN */
196 0x02, /* attributes (bulk) */
197 0x40, 0x00, /* max packet size */
198 0x00 /* interval */
201 static unsigned char lang_descriptor[4] = {
202 0x04, /* length */
203 0x03, /* descriptor type */
204 0x09, 0x04 /* English (US) */
207 #define N_STRING_DESCRIPTORS 2
209 static unsigned char string_descriptor_vendor[] = {
210 0x2e, 0x03,
211 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
212 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
213 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
215 static unsigned char string_descriptor_product[] = {
216 0x1c, 0x03,
217 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
218 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
220 static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = {
221 string_descriptor_vendor,
222 string_descriptor_product
225 static inline int ep_index(int n, int dir)
227 return (n << 1) | dir;
230 static inline int epidx_dir(int idx)
232 return idx & 1;
235 static inline int epidx_n(int idx)
237 return idx >> 1;
240 int usb_connected(void)
242 return GPIO7_READ & 1;
245 static inline void usb_select_endpoint(int idx)
247 ISP1582_EPINDEX = idx;
250 static inline void usb_select_setup_endpoint(void)
252 ISP1582_EPINDEX = 0x20;
255 void usb_setup_endpoint(int idx, int max_pkt_size, int type)
257 struct usb_endpoint *ep;
259 usb_select_endpoint(idx);
260 ISP1582_MAXPKSZ = max_pkt_size;
261 /* |= is in the original firmware */
262 ISP1582_EPTYPE |= 0x1c | type;
263 /* clear buffer */
264 ISP1582_CTRLFUN |= 0x10;
265 ISP1582_INTEN |= (1 << (10 + idx));
267 ep = &(endpoints[epidx_n(idx)]);
268 ep->halt[epidx_dir(idx)] = 0;
269 ep->enabled[epidx_dir(idx)] = 1;
270 ep->out_in_progress = 0;
271 ep->in_min_len = -1;
272 ep->in_ack = 0;
273 ep->max_pkt_size[epidx_dir(idx)] = max_pkt_size;
276 void usb_disable_endpoint(int idx)
278 usb_select_endpoint(idx);
279 ISP1582_EPTYPE &= 8;
280 ISP1582_INTEN &= ~(1 << (10 + idx));
281 endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 1;
284 void usb_reconnect(void)
286 int i;
287 ISP1582_MODE &= ~1; /* SOFTCT off */
288 for (i = 0; i < 10000; i++)
289 nop_f();
290 ISP1582_MODE |= 1; /* SOFTCT on */
293 void usb_cleanup(void)
295 ISP1582_MODE &= ~1; /* SOFTCT off */
298 void usb_setup(int reset)
300 int i;
302 for (i = 0; i < N_ENDPOINTS; i++)
303 endpoints[i].enabled[0] = endpoints[i].enabled[1] = 0;
305 ISP1582_UNLOCK = ISP1582_UNLOCK_CODE;
306 if (!reset)
307 ISP1582_MODE = 0x88; /* CLKAON | GLINTENA */
308 ISP1582_INTCONF = 0x57;
309 ISP1582_INTEN = 0xd39;
311 ISP1582_ADDRESS = reset ? 0x80: 0;
313 usb_setup_endpoint(ep_index(0, DIR_RX), 64, 0);
314 usb_setup_endpoint(ep_index(0, DIR_TX), 64, 0);
316 ISP1582_MODE |= 1; /* SOFTCT on */
318 usb_state = STATE_DEFAULT;
319 usb_remote_wakeup = 0;
322 static int usb_get_packet(unsigned char *buf, int max_len)
324 int len, i;
325 len = ISP1582_BUFLEN;
327 if (max_len < 0 || max_len > len)
328 max_len = len;
330 i = 0;
331 while (i < len)
333 unsigned short d = ISP1582_DATA;
334 if (i < max_len)
335 buf[i] = d & 0xff;
336 i++;
337 if (i < max_len)
338 buf[i] = (d >> 8) & 0xff;
339 i++;
341 return max_len;
344 static void usb_receive(int n)
346 int len;
348 if (endpoints[n].halt[DIR_RX]
349 || !endpoints[n].enabled[DIR_RX]
350 || endpoints[n].in_min_len < 0
351 || !endpoints[n].in_ack)
352 return;
354 endpoints[n].in_ack = 0;
356 usb_select_endpoint(ep_index(n, DIR_RX));
358 len = usb_get_packet(endpoints[n].in_buf + endpoints[n].in_ptr,
359 endpoints[n].in_max_len - endpoints[n].in_ptr);
360 endpoints[n].in_ptr += len;
361 if (endpoints[n].in_ptr >= endpoints[n].in_min_len) {
362 endpoints[n].in_min_len = -1;
363 if (endpoints[n].in_done)
364 (*(endpoints[n].in_done))(n, endpoints[n].in_buf,
365 endpoints[n].in_ptr);
369 static int usb_out_buffer_full(int ep)
371 usb_select_endpoint(ep_index(ep, DIR_TX));
372 if (ISP1582_EPTYPE & 4)
373 return (ISP1582_BUFSTAT & 3) == 3;
374 else
375 return (ISP1582_BUFSTAT & 3) != 0;
378 static void usb_send(int n)
380 int max_pkt_size, len;
381 int i;
382 unsigned char *p;
384 #ifdef LCD_DEBUG
385 if (endpoints[n].halt[DIR_TX])
386 log_char('H');
387 if (!endpoints[n].out_in_progress)
388 log_char('$');
389 #endif
391 if (endpoints[n].halt[DIR_TX]
392 || !endpoints[n].enabled[DIR_TX]
393 || !endpoints[n].out_in_progress)
394 return;
396 if (endpoints[n].out_ptr < 0)
398 endpoints[n].out_in_progress = 0;
399 if (endpoints[n].out_done)
400 (*(endpoints[n].out_done))(n, endpoints[n].out_buf,
401 endpoints[n].out_len);
402 return;
405 if (usb_out_buffer_full(n))
407 log_char('F');
408 return;
411 usb_select_endpoint(ep_index(n, DIR_TX));
412 max_pkt_size = endpoints[n].max_pkt_size[DIR_TX];
413 len = endpoints[n].out_len - endpoints[n].out_ptr;
414 if (len > max_pkt_size)
415 len = max_pkt_size;
417 log_char('0' + (len % 10));
418 ISP1582_BUFLEN = len;
419 p = endpoints[n].out_buf + endpoints[n].out_ptr;
420 i = 0;
421 while (len - i >= 2) {
422 ISP1582_DATA = p[i] | (p[i + 1] << 8);
423 i += 2;
425 if (i < len)
426 ISP1582_DATA = p[i];
428 endpoints[n].out_ptr += len;
431 if (endpoints[n].out_ptr == endpoints[n].out_len
432 && len < max_pkt_size)
434 if (endpoints[n].out_ptr == endpoints[n].out_len)
435 endpoints[n].out_ptr = -1;
438 static void usb_stall_endpoint(int idx)
440 usb_select_endpoint(idx);
441 ISP1582_CTRLFUN |= 1;
442 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1;
445 static void usb_unstall_endpoint(int idx)
447 usb_select_endpoint(idx);
448 ISP1582_CTRLFUN &= ~1;
449 ISP1582_EPTYPE &= ~8;
450 ISP1582_EPTYPE |= 8;
451 ISP1582_CTRLFUN |= 0x10;
452 if (epidx_dir(idx) == DIR_TX)
453 endpoints[epidx_n(idx)].out_in_progress = 0;
454 else
456 endpoints[epidx_n(idx)].in_min_len = -1;
457 endpoints[epidx_n(idx)].in_ack = 0;
459 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0;
462 static void usb_status_ack(int dir)
464 log_char(dir ? '@' : '#');
465 usb_select_endpoint(ep_index(0, dir));
466 ISP1582_CTRLFUN |= 2;
469 static void usb_set_address(int adr)
471 ISP1582_ADDRESS = adr | 0x80;
474 static void usb_data_stage_enable(int dir)
476 usb_select_endpoint(ep_index(0, dir));
477 ISP1582_CTRLFUN |= 4;
480 static void usb_request_error(void)
482 usb_stall_endpoint(ep_index(0, DIR_TX));
483 usb_stall_endpoint(ep_index(0, DIR_RX));
486 static void usb_receive_block(unsigned char *buf, int min_len,
487 int max_len,
488 void (*in_done)(int, unsigned char *, int),
489 int ep)
491 endpoints[ep].in_done = in_done;
492 endpoints[ep].in_buf = buf;
493 endpoints[ep].in_max_len = max_len;
494 endpoints[ep].in_min_len = min_len;
495 endpoints[ep].in_ptr = 0;
496 usb_receive(ep);
499 static void usb_send_block(unsigned char *buf, int len,
500 void (*done)(int, unsigned char *, int),
501 int ep)
503 endpoints[ep].out_done = done;
504 endpoints[ep].out_buf = buf;
505 endpoints[ep].out_len = len;
506 endpoints[ep].out_ptr = 0;
507 endpoints[ep].out_in_progress = 1;
508 usb_send(ep);
511 static void out_send_status(int n, unsigned char *buf, int len)
513 (void)n;
514 (void)buf;
515 (void)len;
516 usb_status_ack(DIR_RX);
519 static void usb_send_block_and_status(unsigned char *buf, int len, int ep)
521 usb_send_block(buf, len, out_send_status, ep);
524 static void usb_setup_set_address(int adr)
526 usb_set_address(adr);
527 usb_state = adr ? STATE_ADDRESS : STATE_DEFAULT;
528 usb_status_ack(DIR_TX);
531 static void usb_send_descriptor(unsigned char *device_descriptor,
532 int descriptor_len, int buffer_len)
534 int len = descriptor_len < buffer_len ? descriptor_len : buffer_len;
535 usb_send_block_and_status(device_descriptor, len, 0);
538 static void usb_setup_get_descriptor(int type, int index, int lang, int len)
540 (void)lang;
541 usb_data_stage_enable(DIR_TX);
542 switch (type)
544 case 1:
545 if (index == 0)
546 usb_send_descriptor(device_descriptor,
547 sizeof(device_descriptor), len);
548 else
549 usb_request_error();
550 break;
551 case 2:
552 if (index == 0)
553 usb_send_descriptor(cfg_descriptor,
554 sizeof(cfg_descriptor), len);
555 else
556 usb_request_error();
557 break;
558 case 3:
559 if (index == 0)
560 usb_send_descriptor(lang_descriptor,
561 sizeof(lang_descriptor), len);
562 else if (index <= N_STRING_DESCRIPTORS)
563 usb_send_descriptor(string_descriptor[index - 1],
564 string_descriptor[index - 1][0],
565 len);
566 else
567 usb_request_error();
568 break;
569 default:
570 usb_request_error();
574 static void usb_setup_get_configuration(void)
576 setup_out_buf[0] = (usb_state == STATE_CONFIGURED) ? 1 : 0;
577 usb_data_stage_enable(DIR_TX);
578 usb_send_block_and_status(setup_out_buf, 1, 0);
581 static void usb_setup_interface(void)
583 usb_setup_endpoint(ep_index(1, DIR_RX), 64, TYPE_BULK);
584 usb_setup_endpoint(ep_index(1, DIR_TX), 64, TYPE_BULK);
587 static void usb_setup_set_configuration(int value)
589 switch (value)
591 case 0:
592 usb_disable_endpoint(ep_index(1, DIR_RX));
593 usb_disable_endpoint(ep_index(1, DIR_TX));
594 usb_state = STATE_ADDRESS;
595 usb_status_ack(DIR_TX);
596 break;
597 case 1:
598 usb_setup_interface();
599 usb_state = STATE_CONFIGURED;
600 usb_status_ack(DIR_TX);
601 break;
602 default:
603 usb_request_error();
607 static void usb_send_status(void)
609 usb_data_stage_enable(DIR_TX);
610 usb_send_block_and_status(setup_out_buf, 2, 0);
613 static void usb_setup_get_device_status(void)
615 setup_out_buf[0] = (usb_remote_wakeup != 0) ? 2 : 0;
616 setup_out_buf[1] = 0;
617 usb_send_status();
620 static void usb_setup_clear_device_feature(int feature)
622 if (feature == 1) {
623 usb_remote_wakeup = 0;
624 usb_status_ack(DIR_TX);
625 } else
626 usb_request_error();
629 static void usb_setup_set_device_feature(int feature)
631 if (feature == 1) {
632 usb_remote_wakeup = 1;
633 usb_status_ack(DIR_TX);
634 } else
635 usb_request_error();
638 static void usb_setup_clear_endpoint_feature(int endpoint, int feature)
640 if (usb_state != STATE_CONFIGURED || feature != 0)
641 usb_request_error();
642 else if ((endpoint & 0xf) == 1)
644 usb_unstall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
645 usb_status_ack(DIR_TX);
647 else
648 usb_request_error();
651 static void usb_setup_set_endpoint_feature(int endpoint, int feature)
653 if (usb_state != STATE_CONFIGURED || feature != 0)
654 usb_request_error();
655 else if ((endpoint & 0xf) == 1)
657 usb_stall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
658 usb_status_ack(DIR_TX);
660 else
661 usb_request_error();
664 static void usb_setup_get_interface_status(int interface)
666 if (usb_state != STATE_CONFIGURED || interface != 0)
667 usb_request_error();
668 else
670 setup_out_buf[0] = setup_out_buf[1] = 0;
671 usb_send_status();
675 static void usb_setup_get_endpoint_status(int endpoint)
677 if ((usb_state == STATE_CONFIGURED && (endpoint & 0xf) <= 1)
678 || (usb_state == STATE_ADDRESS && (endpoint & 0xf) == 0))
680 setup_out_buf[0] = endpoints[endpoint & 0xf].halt[endpoint >> 7];
681 setup_out_buf[1] = 0;
682 usb_send_status();
684 else
685 usb_request_error();
688 static void usb_setup_get_interface(int interface)
690 if (usb_state != STATE_CONFIGURED || interface != 0)
691 usb_request_error();
692 else
694 setup_out_buf[0] = 0;
695 usb_data_stage_enable(DIR_TX);
696 usb_send_block_and_status(setup_out_buf, 1, 0);
700 static void usb_setup_set_interface(int interface, int setting)
702 if (usb_state != STATE_CONFIGURED || interface != 0 || setting != 0)
703 usb_request_error();
704 else
706 usb_setup_interface();
707 usb_status_ack(DIR_TX);
711 static void usb_handle_setup_pkt(unsigned char *pkt)
713 switch ((pkt[0] << 8) | pkt[1])
715 case 0x0005:
716 log_char('A');
717 usb_setup_set_address(pkt[2]);
718 break;
719 case 0x8006:
720 log_char('D');
721 usb_setup_get_descriptor(pkt[3], pkt[2], (pkt[5] << 8) | pkt[4],
722 (pkt[7] << 8) | pkt[6]);
723 break;
724 case 0x8008:
725 usb_setup_get_configuration();
726 break;
727 case 0x0009:
728 usb_setup_set_configuration(pkt[2]);
729 break;
730 case 0x8000:
731 usb_setup_get_device_status();
732 break;
733 case 0x8100:
734 usb_setup_get_interface_status(pkt[4]);
735 break;
736 case 0x8200:
737 usb_setup_get_endpoint_status(pkt[4]);
738 break;
739 case 0x0001:
740 usb_setup_clear_device_feature(pkt[2]);
741 break;
742 case 0x0201:
743 usb_setup_clear_endpoint_feature(pkt[4], pkt[2]);
744 break;
745 case 0x0003:
746 usb_setup_set_device_feature(pkt[2]);
747 break;
748 case 0x0203:
749 usb_setup_set_endpoint_feature(pkt[4], pkt[2]);
750 break;
751 case 0x810a:
752 usb_setup_get_interface(pkt[4]);
753 break;
754 case 0x010b:
755 usb_setup_set_interface(pkt[4], pkt[2]);
756 break;
757 case 0x0103:
758 /* set interface feature */
759 case 0x0101:
760 /* clear interface feature */
761 case 0x0007:
762 /* set descriptor */
763 case 0x820c:
764 /* synch frame */
765 default:
766 usb_request_error();
770 static void usb_handle_setup_rx(void)
772 int len;
773 #ifdef LCD_DEBUG
774 char s[20];
775 int i;
776 #endif
777 usb_select_setup_endpoint();
778 len = usb_get_packet(setup_pkt_buf, 8);
780 if (len == 8)
781 usb_handle_setup_pkt(setup_pkt_buf);
783 #ifdef LCD_DEBUG
785 snprintf(s, 10, "l%02x", len);
786 lcd_puts(0, 5, s);
788 for (i = 0; i < 8; i++)
789 snprintf(s + i * 2, 3, "%02x", setup_pkt_buf[i]);
790 lcd_puts(0, 0, s);
791 lcd_update();
792 #endif
795 static void usb_handle_data_int(int ep, int dir)
797 if (dir == DIR_TX)
798 usb_send(ep);
799 else
801 endpoints[ep].in_ack = 1;
802 usb_receive(ep);
806 static void usb_handle_int(int i)
808 #ifdef LCD_DEBUG
810 char s[10];
811 snprintf(s, sizeof(s), "%02d", i);
812 lcd_puts(0, 2, s);
813 lcd_update();
815 int_count[i]++;
816 if (i == 10)
817 log_char('o');
818 if (i == 11)
819 log_char('i');
820 if (i == 12)
821 log_char('O');
822 if (i == 13)
823 log_char('I');
824 #endif
826 if (i >= 10)
827 usb_handle_data_int((i - 10) / 2, i % 2);
828 else
830 switch (i)
832 case 0:
833 log_char('r');
834 usb_setup(1);
835 break;
836 case 8:
837 log_char('s');
838 usb_handle_setup_rx();
839 break;
845 void usb_handle_interrupts(void)
847 #ifdef LCD_DEBUG
848 char s[20];
849 #endif
851 while (1)
853 unsigned long ints;
854 int i;
856 #ifdef LCD_DEBUG
858 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
859 lcd_puts(0, 2, s);
861 #endif
863 ints = ISP1582_INT & ISP1582_INTEN;
864 if (!ints) break;
866 i = 0;
867 while (!(ints & (1 << i)))
868 i++;
869 ISP1582_INT = 1 << i;
870 usb_handle_int(i);
872 #ifdef LCD_DEBUG
873 for (i = 0; i < 8; i++)
874 snprintf(s + i * 2, 3, "%02x", int_count[i]);
875 lcd_puts(0, 6, s);
876 for (i = 0; i < 8; i++)
877 snprintf(s + i * 2, 3, "%02x", int_count[i + 8]);
878 lcd_puts(0, 7, s);
879 #endif
881 #ifdef LCD_DEBUG
883 lcd_puts(0, 3, usb_connected() ? "C" : "N");
884 lcd_update();
886 #endif
889 static inline int fifo_mod(int n)
891 return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n;
894 static void fifo_init(struct serial_fifo *fifo)
896 fifo->head = fifo->tail = 0;
899 static int fifo_empty(struct serial_fifo *fifo)
901 return fifo->head == fifo->tail;
904 static int fifo_full(struct serial_fifo *fifo)
906 return fifo_mod(fifo->head + 1) == fifo->tail;
909 static void fifo_remove(struct serial_fifo *fifo, int n)
911 fifo->tail = fifo_mod(fifo->tail + n);
915 Not used:
916 static void fifo_add(struct serial_fifo *fifo, int n)
918 fifo->head = fifo_mod(fifo->head + n);
921 static void fifo_free_block(struct serial_fifo *fifo,
922 unsigned char **ptr, int *len)
924 *ptr = fifo->buf + fifo->head;
925 if (fifo->head >= fifo->tail)
927 int l = SERIAL_BUF_SIZE - fifo->head;
928 if (fifo->tail == 0)
929 l--;
930 *len = l;
932 else
933 *len = fifo->tail - fifo->head - 1;
937 static int fifo_free_space(struct serial_fifo *fifo)
939 if (fifo->head >= fifo->tail)
940 return SERIAL_BUF_SIZE - (fifo->head - fifo->tail) - 1;
941 else
942 return fifo->tail - fifo->head - 1;
945 static int fifo_get_byte(struct serial_fifo *fifo)
947 int r = fifo->buf[fifo->tail];
948 fifo->tail = fifo_mod(fifo->tail + 1);
949 return r;
952 static void fifo_put_byte(struct serial_fifo *fifo, int b)
954 fifo->buf[fifo->head] = b;
955 fifo->head = fifo_mod(fifo->head + 1);
958 static void fifo_full_block(struct serial_fifo *fifo,
959 unsigned char **ptr, int *len)
961 *ptr = fifo->buf + fifo->tail;
962 if (fifo->head >= fifo->tail)
963 *len = fifo->head - fifo->tail;
964 else
965 *len = SERIAL_BUF_SIZE - fifo->tail;
968 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len);
969 static void serial_free_out_fifo(int ep, unsigned char *buf, int len);
971 static void serial_restart_input(int ep)
973 if (fifo_free_space(&serial_in_fifo) >= 64)
974 usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep);
977 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len)
979 int i;
980 for (i = 0; i < len; i++)
981 fifo_put_byte(&serial_in_fifo, buf[i]);
982 serial_restart_input(ep);
985 static void serial_restart_output(int ep)
987 unsigned char *block;
988 int blen;
989 fifo_full_block(&serial_out_fifo, &block, &blen);
990 if (blen)
992 #ifdef LCD_DEBUG
993 char s[20];
994 snprintf(s, sizeof(s), "o%03lx/%03x", block - serial_out_fifo.buf,
995 blen);
996 lcd_puts(0, 2, s);
997 lcd_update();
998 #endif
999 usb_send_block(block, blen, serial_free_out_fifo, ep);
1003 static void serial_free_out_fifo(int ep, unsigned char *buf, int len)
1005 (void)buf;
1006 fifo_remove(&serial_out_fifo, len);
1007 serial_restart_output(ep);
1010 void usb_serial_handle(void)
1012 #ifdef BUTTONS
1013 static int t = 0;
1015 t++;
1016 if (t >= 1000)
1018 int b;
1019 t = 0;
1020 yield();
1021 b = button_get(false);
1022 if (b == BUTTON_PLAY)
1023 system_reboot();
1024 else if (b & BUTTON_REL)
1025 usb_reconnect();
1027 #endif
1030 if (!usb_connect_state)
1032 if (usb_connected())
1034 int i;
1035 GPIO3_SET = 4;
1036 (*(volatile unsigned long *)0x80005004) = 2;
1037 (*(volatile unsigned long *)0x80005008) = 0;
1038 for (i = 0; i < 100000; i++)
1039 nop_f();
1040 usb_setup(0);
1041 usb_connect_state = 1;
1044 else
1046 if (!usb_connected())
1048 usb_connect_state = 0;
1049 usb_cleanup();
1051 else
1053 usb_handle_interrupts();
1055 if (usb_state == STATE_CONFIGURED)
1057 if (endpoints[1].in_min_len < 0)
1058 serial_restart_input(1);
1059 if (!endpoints[1].out_in_progress)
1060 serial_restart_output(1);
1068 Not used:
1069 static int usb_serial_in_empty(void)
1071 return fifo_empty(&serial_in_fifo);
1075 int usb_serial_get_byte(void)
1077 while (fifo_empty(&serial_in_fifo))
1078 usb_serial_handle();
1079 return fifo_get_byte(&serial_in_fifo);
1082 int usb_serial_try_get_byte(void)
1084 int r;
1085 if (fifo_empty(&serial_in_fifo))
1086 r = -1;
1087 else
1088 r = fifo_get_byte(&serial_in_fifo);
1089 usb_serial_handle();
1090 return r;
1094 Not used:
1095 static int usb_serial_out_full(void)
1097 return fifo_full(&serial_out_fifo);
1101 void usb_serial_put_byte(int b)
1103 while (fifo_full(&serial_out_fifo))
1104 usb_serial_handle();
1105 fifo_put_byte(&serial_out_fifo, b);
1106 usb_serial_handle();
1109 int usb_serial_try_put_byte(int b)
1111 int r = -1;
1112 if (!fifo_full(&serial_out_fifo))
1114 fifo_put_byte(&serial_out_fifo, b);
1115 r = 0;
1117 usb_serial_handle();
1118 return r;
1121 void usb_serial_init(void)
1123 fifo_init(&serial_in_fifo);
1124 fifo_init(&serial_out_fifo);
1125 usb_connect_state = 0;