1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Tomasz Malesinski
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
27 /* #include "config.h" */
30 #include "ifp_usb_serial.h"
44 #define ISP1582_BASE (0x24100000)
45 #define ISP1582_ADDRESS (*(volatile unsigned char *)ISP1582_BASE)
46 #define ISP1582_MODE (*(volatile unsigned short *)(ISP1582_BASE + 0xc))
47 #define ISP1582_INTCONF (*(volatile unsigned char *)(ISP1582_BASE + 0x10))
48 #define ISP1582_OTG (*(volatile unsigned char *)(ISP1582_BASE + 0x12))
49 #define ISP1582_INTEN (*(volatile unsigned long *)(ISP1582_BASE + 0x14))
51 #define ISP1582_EPINDEX (*(volatile unsigned char *)(ISP1582_BASE + 0x2c))
52 #define ISP1582_CTRLFUN (*(volatile unsigned char *)(ISP1582_BASE + 0x28))
53 #define ISP1582_DATA (*(volatile unsigned short *)(ISP1582_BASE + 0x20))
54 #define ISP1582_BUFLEN (*(volatile unsigned short *)(ISP1582_BASE + 0x1c))
55 #define ISP1582_BUFSTAT (*(volatile unsigned char *)(ISP1582_BASE + 0x1e))
56 #define ISP1582_MAXPKSZ (*(volatile unsigned short *)(ISP1582_BASE + 0x04))
57 #define ISP1582_EPTYPE (*(volatile unsigned short *)(ISP1582_BASE + 0x08))
59 #define ISP1582_INT (*(volatile unsigned long *)(ISP1582_BASE + 0x18))
60 #define ISP1582_CHIPID (*(volatile unsigned long *)(ISP1582_BASE + 0x70))
61 #define ISP1582_FRAMENO (*(volatile unsigned short *)(ISP1582_BASE + 0x74))
62 #define ISP1582_UNLOCK (*(volatile unsigned short *)(ISP1582_BASE + 0x7c))
64 #define ISP1582_UNLOCK_CODE 0xaa37
71 #define STATE_DEFAULT 0
72 #define STATE_ADDRESS 1
73 #define STATE_CONFIGURED 2
79 unsigned char *out_buf
;
82 void (*out_done
)(int, unsigned char *, int);
83 unsigned char out_in_progress
;
85 unsigned char *in_buf
;
89 void (*in_done
)(int, unsigned char *, int);
92 unsigned char halt
[2];
93 unsigned char enabled
[2];
94 short max_pkt_size
[2];
97 static char usb_connect_state
;
99 static struct usb_endpoint endpoints
[N_ENDPOINTS
];
101 static unsigned char setup_pkt_buf
[8];
102 static unsigned char setup_out_buf
[8];
103 static unsigned char usb_state
;
104 static unsigned char usb_remote_wakeup
;
107 static unsigned char int_count
[32];
109 static int log_pos_x
= 0;
110 static int log_pos_y
= 3;
113 static void nop_f(void)
118 static void log_char(char c
)
125 lcd_puts(log_pos_x
, log_pos_y
, s
);
140 #define SERIAL_BUF_SIZE 1024
144 unsigned char buf
[SERIAL_BUF_SIZE
];
148 static struct serial_fifo serial_in_fifo
;
149 static struct serial_fifo serial_out_fifo
;
150 static unsigned char serial_in_pkt
[64];
152 static unsigned char device_descriptor
[18] = {
154 0x01, /* descriptor type */
155 0x10, 0x01, /* USB version (1.1) */
156 0xff, 0xff, /* class and subclass */
158 0x40, /* max packet size 0 */
159 0x02, 0x41, /* vendor (iRiver) */
160 0x07, 0xee, /* product (0xee07) */
161 0x01, 0x00, /* device version */
162 0x01, /* manufacturer string */
163 0x02, /* product string */
164 0x00, /* serial number string */
165 0x01 /* number of configurations */
168 static unsigned char cfg_descriptor
[32] = {
170 0x02, /* descriptor type */
171 0x20, 0x00, /* total length */
172 0x01, /* number of interfaces */
173 0x01, /* configuration value */
174 0x00, /* configuration string */
175 0x80, /* attributes (none) */
176 0x32, /* max power (100 mA) */
177 /* interface descriptor */
179 0x04, /* descriptor type */
180 0x00, /* interface number */
181 0x00, /* alternate setting */
182 0x02, /* number of endpoints */
183 0xff, /* interface class */
184 0xff, /* interface subclass */
185 0xff, /* interface protocol */
186 0x00, /* interface string */
189 0x05, /* descriptor type */
190 0x81, /* endpoint 1 IN */
191 0x02, /* attributes (bulk) */
192 0x40, 0x00, /* max packet size */
196 0x05, /* descriptor type */
197 0x01, /* endpoint 1 IN */
198 0x02, /* attributes (bulk) */
199 0x40, 0x00, /* max packet size */
203 static unsigned char lang_descriptor
[4] = {
205 0x03, /* descriptor type */
206 0x09, 0x04 /* English (US) */
209 #define N_STRING_DESCRIPTORS 2
211 static unsigned char string_descriptor_vendor
[] = {
213 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
214 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
215 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
217 static unsigned char string_descriptor_product
[] = {
219 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
220 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
222 static unsigned char *string_descriptor
[N_STRING_DESCRIPTORS
] = {
223 string_descriptor_vendor
,
224 string_descriptor_product
227 static inline int ep_index(int n
, int dir
)
229 return (n
<< 1) | dir
;
232 static inline int epidx_dir(int idx
)
237 static inline int epidx_n(int idx
)
242 int usb_connected(void)
244 return GPIO7_READ
& 1;
247 static inline void usb_select_endpoint(int idx
)
249 ISP1582_EPINDEX
= idx
;
252 static inline void usb_select_setup_endpoint(void)
254 ISP1582_EPINDEX
= 0x20;
257 void usb_setup_endpoint(int idx
, int max_pkt_size
, int type
)
259 struct usb_endpoint
*ep
;
261 usb_select_endpoint(idx
);
262 ISP1582_MAXPKSZ
= max_pkt_size
;
263 /* |= is in the original firmware */
264 ISP1582_EPTYPE
|= 0x1c | type
;
266 ISP1582_CTRLFUN
|= 0x10;
267 ISP1582_INTEN
|= (1 << (10 + idx
));
269 ep
= &(endpoints
[epidx_n(idx
)]);
270 ep
->halt
[epidx_dir(idx
)] = 0;
271 ep
->enabled
[epidx_dir(idx
)] = 1;
272 ep
->out_in_progress
= 0;
275 ep
->max_pkt_size
[epidx_dir(idx
)] = max_pkt_size
;
278 void usb_disable_endpoint(int idx
)
280 usb_select_endpoint(idx
);
282 ISP1582_INTEN
&= ~(1 << (10 + idx
));
283 endpoints
[epidx_n(idx
)].enabled
[epidx_dir(idx
)] = 1;
286 void usb_reconnect(void)
289 ISP1582_MODE
&= ~1; /* SOFTCT off */
290 for (i
= 0; i
< 10000; i
++)
292 ISP1582_MODE
|= 1; /* SOFTCT on */
295 void usb_cleanup(void)
297 ISP1582_MODE
&= ~1; /* SOFTCT off */
300 void usb_setup(int reset
)
304 for (i
= 0; i
< N_ENDPOINTS
; i
++)
305 endpoints
[i
].enabled
[0] = endpoints
[i
].enabled
[1] = 0;
307 ISP1582_UNLOCK
= ISP1582_UNLOCK_CODE
;
309 ISP1582_MODE
= 0x88; /* CLKAON | GLINTENA */
310 ISP1582_INTCONF
= 0x57;
311 ISP1582_INTEN
= 0xd39;
313 ISP1582_ADDRESS
= reset
? 0x80: 0;
315 usb_setup_endpoint(ep_index(0, DIR_RX
), 64, 0);
316 usb_setup_endpoint(ep_index(0, DIR_TX
), 64, 0);
318 ISP1582_MODE
|= 1; /* SOFTCT on */
320 usb_state
= STATE_DEFAULT
;
321 usb_remote_wakeup
= 0;
324 static int usb_get_packet(unsigned char *buf
, int max_len
)
327 len
= ISP1582_BUFLEN
;
329 if (max_len
< 0 || max_len
> len
)
335 unsigned short d
= ISP1582_DATA
;
340 buf
[i
] = (d
>> 8) & 0xff;
346 static void usb_receive(int n
)
350 if (endpoints
[n
].halt
[DIR_RX
]
351 || !endpoints
[n
].enabled
[DIR_RX
]
352 || endpoints
[n
].in_min_len
< 0
353 || !endpoints
[n
].in_ack
)
356 endpoints
[n
].in_ack
= 0;
358 usb_select_endpoint(ep_index(n
, DIR_RX
));
360 len
= usb_get_packet(endpoints
[n
].in_buf
+ endpoints
[n
].in_ptr
,
361 endpoints
[n
].in_max_len
- endpoints
[n
].in_ptr
);
362 endpoints
[n
].in_ptr
+= len
;
363 if (endpoints
[n
].in_ptr
>= endpoints
[n
].in_min_len
) {
364 endpoints
[n
].in_min_len
= -1;
365 if (endpoints
[n
].in_done
)
366 (*(endpoints
[n
].in_done
))(n
, endpoints
[n
].in_buf
,
367 endpoints
[n
].in_ptr
);
371 static int usb_out_buffer_full(int ep
)
373 usb_select_endpoint(ep_index(ep
, DIR_TX
));
374 if (ISP1582_EPTYPE
& 4)
375 return (ISP1582_BUFSTAT
& 3) == 3;
377 return (ISP1582_BUFSTAT
& 3) != 0;
380 static void usb_send(int n
)
382 int max_pkt_size
, len
;
387 if (endpoints
[n
].halt
[DIR_TX
])
389 if (!endpoints
[n
].out_in_progress
)
393 if (endpoints
[n
].halt
[DIR_TX
]
394 || !endpoints
[n
].enabled
[DIR_TX
]
395 || !endpoints
[n
].out_in_progress
)
398 if (endpoints
[n
].out_ptr
< 0)
400 endpoints
[n
].out_in_progress
= 0;
401 if (endpoints
[n
].out_done
)
402 (*(endpoints
[n
].out_done
))(n
, endpoints
[n
].out_buf
,
403 endpoints
[n
].out_len
);
407 if (usb_out_buffer_full(n
))
413 usb_select_endpoint(ep_index(n
, DIR_TX
));
414 max_pkt_size
= endpoints
[n
].max_pkt_size
[DIR_TX
];
415 len
= endpoints
[n
].out_len
- endpoints
[n
].out_ptr
;
416 if (len
> max_pkt_size
)
419 log_char('0' + (len
% 10));
420 ISP1582_BUFLEN
= len
;
421 p
= endpoints
[n
].out_buf
+ endpoints
[n
].out_ptr
;
423 while (len
- i
>= 2) {
424 ISP1582_DATA
= p
[i
] | (p
[i
+ 1] << 8);
430 endpoints
[n
].out_ptr
+= len
;
433 if (endpoints[n].out_ptr == endpoints[n].out_len
434 && len < max_pkt_size)
436 if (endpoints
[n
].out_ptr
== endpoints
[n
].out_len
)
437 endpoints
[n
].out_ptr
= -1;
440 static void usb_stall_endpoint(int idx
)
442 usb_select_endpoint(idx
);
443 ISP1582_CTRLFUN
|= 1;
444 endpoints
[epidx_n(idx
)].halt
[epidx_dir(idx
)] = 1;
447 static void usb_unstall_endpoint(int idx
)
449 usb_select_endpoint(idx
);
450 ISP1582_CTRLFUN
&= ~1;
451 ISP1582_EPTYPE
&= ~8;
453 ISP1582_CTRLFUN
|= 0x10;
454 if (epidx_dir(idx
) == DIR_TX
)
455 endpoints
[epidx_n(idx
)].out_in_progress
= 0;
458 endpoints
[epidx_n(idx
)].in_min_len
= -1;
459 endpoints
[epidx_n(idx
)].in_ack
= 0;
461 endpoints
[epidx_n(idx
)].halt
[epidx_dir(idx
)] = 0;
464 static void usb_status_ack(int dir
)
466 log_char(dir
? '@' : '#');
467 usb_select_endpoint(ep_index(0, dir
));
468 ISP1582_CTRLFUN
|= 2;
471 static void usb_set_address(int adr
)
473 ISP1582_ADDRESS
= adr
| 0x80;
476 static void usb_data_stage_enable(int dir
)
478 usb_select_endpoint(ep_index(0, dir
));
479 ISP1582_CTRLFUN
|= 4;
482 static void usb_request_error(void)
484 usb_stall_endpoint(ep_index(0, DIR_TX
));
485 usb_stall_endpoint(ep_index(0, DIR_RX
));
488 static void usb_receive_block(unsigned char *buf
, int min_len
,
490 void (*in_done
)(int, unsigned char *, int),
493 endpoints
[ep
].in_done
= in_done
;
494 endpoints
[ep
].in_buf
= buf
;
495 endpoints
[ep
].in_max_len
= max_len
;
496 endpoints
[ep
].in_min_len
= min_len
;
497 endpoints
[ep
].in_ptr
= 0;
501 static void usb_send_block(unsigned char *buf
, int len
,
502 void (*done
)(int, unsigned char *, int),
505 endpoints
[ep
].out_done
= done
;
506 endpoints
[ep
].out_buf
= buf
;
507 endpoints
[ep
].out_len
= len
;
508 endpoints
[ep
].out_ptr
= 0;
509 endpoints
[ep
].out_in_progress
= 1;
513 static void out_send_status(int n
, unsigned char *buf
, int len
)
518 usb_status_ack(DIR_RX
);
521 static void usb_send_block_and_status(unsigned char *buf
, int len
, int ep
)
523 usb_send_block(buf
, len
, out_send_status
, ep
);
526 static void usb_setup_set_address(int adr
)
528 usb_set_address(adr
);
529 usb_state
= adr
? STATE_ADDRESS
: STATE_DEFAULT
;
530 usb_status_ack(DIR_TX
);
533 static void usb_send_descriptor(unsigned char *device_descriptor
,
534 int descriptor_len
, int buffer_len
)
536 int len
= descriptor_len
< buffer_len
? descriptor_len
: buffer_len
;
537 usb_send_block_and_status(device_descriptor
, len
, 0);
540 static void usb_setup_get_descriptor(int type
, int index
, int lang
, int len
)
543 usb_data_stage_enable(DIR_TX
);
548 usb_send_descriptor(device_descriptor
,
549 sizeof(device_descriptor
), len
);
555 usb_send_descriptor(cfg_descriptor
,
556 sizeof(cfg_descriptor
), len
);
562 usb_send_descriptor(lang_descriptor
,
563 sizeof(lang_descriptor
), len
);
564 else if (index
<= N_STRING_DESCRIPTORS
)
565 usb_send_descriptor(string_descriptor
[index
- 1],
566 string_descriptor
[index
- 1][0],
576 static void usb_setup_get_configuration(void)
578 setup_out_buf
[0] = (usb_state
== STATE_CONFIGURED
) ? 1 : 0;
579 usb_data_stage_enable(DIR_TX
);
580 usb_send_block_and_status(setup_out_buf
, 1, 0);
583 static void usb_setup_interface(void)
585 usb_setup_endpoint(ep_index(1, DIR_RX
), 64, TYPE_BULK
);
586 usb_setup_endpoint(ep_index(1, DIR_TX
), 64, TYPE_BULK
);
589 static void usb_setup_set_configuration(int value
)
594 usb_disable_endpoint(ep_index(1, DIR_RX
));
595 usb_disable_endpoint(ep_index(1, DIR_TX
));
596 usb_state
= STATE_ADDRESS
;
597 usb_status_ack(DIR_TX
);
600 usb_setup_interface();
601 usb_state
= STATE_CONFIGURED
;
602 usb_status_ack(DIR_TX
);
609 static void usb_send_status(void)
611 usb_data_stage_enable(DIR_TX
);
612 usb_send_block_and_status(setup_out_buf
, 2, 0);
615 static void usb_setup_get_device_status(void)
617 setup_out_buf
[0] = (usb_remote_wakeup
!= 0) ? 2 : 0;
618 setup_out_buf
[1] = 0;
622 static void usb_setup_clear_device_feature(int feature
)
625 usb_remote_wakeup
= 0;
626 usb_status_ack(DIR_TX
);
631 static void usb_setup_set_device_feature(int feature
)
634 usb_remote_wakeup
= 1;
635 usb_status_ack(DIR_TX
);
640 static void usb_setup_clear_endpoint_feature(int endpoint
, int feature
)
642 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
644 else if ((endpoint
& 0xf) == 1)
646 usb_unstall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
647 usb_status_ack(DIR_TX
);
653 static void usb_setup_set_endpoint_feature(int endpoint
, int feature
)
655 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
657 else if ((endpoint
& 0xf) == 1)
659 usb_stall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
660 usb_status_ack(DIR_TX
);
666 static void usb_setup_get_interface_status(int interface
)
668 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
672 setup_out_buf
[0] = setup_out_buf
[1] = 0;
677 static void usb_setup_get_endpoint_status(int endpoint
)
679 if ((usb_state
== STATE_CONFIGURED
&& (endpoint
& 0xf) <= 1)
680 || (usb_state
== STATE_ADDRESS
&& (endpoint
& 0xf) == 0))
682 setup_out_buf
[0] = endpoints
[endpoint
& 0xf].halt
[endpoint
>> 7];
683 setup_out_buf
[1] = 0;
690 static void usb_setup_get_interface(int interface
)
692 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
696 setup_out_buf
[0] = 0;
697 usb_data_stage_enable(DIR_TX
);
698 usb_send_block_and_status(setup_out_buf
, 1, 0);
702 static void usb_setup_set_interface(int interface
, int setting
)
704 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0 || setting
!= 0)
708 usb_setup_interface();
709 usb_status_ack(DIR_TX
);
713 static void usb_handle_setup_pkt(unsigned char *pkt
)
715 switch ((pkt
[0] << 8) | pkt
[1])
719 usb_setup_set_address(pkt
[2]);
723 usb_setup_get_descriptor(pkt
[3], pkt
[2], (pkt
[5] << 8) | pkt
[4],
724 (pkt
[7] << 8) | pkt
[6]);
727 usb_setup_get_configuration();
730 usb_setup_set_configuration(pkt
[2]);
733 usb_setup_get_device_status();
736 usb_setup_get_interface_status(pkt
[4]);
739 usb_setup_get_endpoint_status(pkt
[4]);
742 usb_setup_clear_device_feature(pkt
[2]);
745 usb_setup_clear_endpoint_feature(pkt
[4], pkt
[2]);
748 usb_setup_set_device_feature(pkt
[2]);
751 usb_setup_set_endpoint_feature(pkt
[4], pkt
[2]);
754 usb_setup_get_interface(pkt
[4]);
757 usb_setup_set_interface(pkt
[4], pkt
[2]);
760 /* set interface feature */
762 /* clear interface feature */
772 static void usb_handle_setup_rx(void)
779 usb_select_setup_endpoint();
780 len
= usb_get_packet(setup_pkt_buf
, 8);
783 usb_handle_setup_pkt(setup_pkt_buf
);
787 snprintf(s, 10, "l%02x", len);
790 for (i
= 0; i
< 8; i
++)
791 snprintf(s
+ i
* 2, 3, "%02x", setup_pkt_buf
[i
]);
797 static void usb_handle_data_int(int ep
, int dir
)
803 endpoints
[ep
].in_ack
= 1;
808 static void usb_handle_int(int i
)
813 snprintf(s, sizeof(s), "%02d", i);
829 usb_handle_data_int((i
- 10) / 2, i
% 2);
840 usb_handle_setup_rx();
847 void usb_handle_interrupts(void)
860 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
865 ints
= ISP1582_INT
& ISP1582_INTEN
;
869 while (!(ints
& (1 << i
)))
871 ISP1582_INT
= 1 << i
;
875 for (i
= 0; i
< 8; i
++)
876 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
]);
878 for (i
= 0; i
< 8; i
++)
879 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
+ 8]);
885 lcd_puts(0, 3, usb_connected() ? "C" : "N");
891 static inline int fifo_mod(int n
)
893 return (n
>= SERIAL_BUF_SIZE
) ? n
- SERIAL_BUF_SIZE
: n
;
896 static void fifo_init(struct serial_fifo
*fifo
)
898 fifo
->head
= fifo
->tail
= 0;
901 static int fifo_empty(struct serial_fifo
*fifo
)
903 return fifo
->head
== fifo
->tail
;
906 static int fifo_full(struct serial_fifo
*fifo
)
908 return fifo_mod(fifo
->head
+ 1) == fifo
->tail
;
911 static void fifo_remove(struct serial_fifo
*fifo
, int n
)
913 fifo
->tail
= fifo_mod(fifo
->tail
+ n
);
918 static void fifo_add(struct serial_fifo *fifo, int n)
920 fifo->head = fifo_mod(fifo->head + n);
923 static void fifo_free_block(struct serial_fifo *fifo,
924 unsigned char **ptr, int *len)
926 *ptr = fifo->buf + fifo->head;
927 if (fifo->head >= fifo->tail)
929 int l = SERIAL_BUF_SIZE - fifo->head;
935 *len = fifo->tail - fifo->head - 1;
939 static int fifo_free_space(struct serial_fifo
*fifo
)
941 if (fifo
->head
>= fifo
->tail
)
942 return SERIAL_BUF_SIZE
- (fifo
->head
- fifo
->tail
) - 1;
944 return fifo
->tail
- fifo
->head
- 1;
947 static int fifo_get_byte(struct serial_fifo
*fifo
)
949 int r
= fifo
->buf
[fifo
->tail
];
950 fifo
->tail
= fifo_mod(fifo
->tail
+ 1);
954 static void fifo_put_byte(struct serial_fifo
*fifo
, int b
)
956 fifo
->buf
[fifo
->head
] = b
;
957 fifo
->head
= fifo_mod(fifo
->head
+ 1);
960 static void fifo_full_block(struct serial_fifo
*fifo
,
961 unsigned char **ptr
, int *len
)
963 *ptr
= fifo
->buf
+ fifo
->tail
;
964 if (fifo
->head
>= fifo
->tail
)
965 *len
= fifo
->head
- fifo
->tail
;
967 *len
= SERIAL_BUF_SIZE
- fifo
->tail
;
970 static void serial_fill_in_fifo(int ep
, unsigned char *buf
, int len
);
971 static void serial_free_out_fifo(int ep
, unsigned char *buf
, int len
);
973 static void serial_restart_input(int ep
)
975 if (fifo_free_space(&serial_in_fifo
) >= 64)
976 usb_receive_block(serial_in_pkt
, 1, 64, serial_fill_in_fifo
, ep
);
979 static void serial_fill_in_fifo(int ep
, unsigned char *buf
, int len
)
982 for (i
= 0; i
< len
; i
++)
983 fifo_put_byte(&serial_in_fifo
, buf
[i
]);
984 serial_restart_input(ep
);
987 static void serial_restart_output(int ep
)
989 unsigned char *block
;
991 fifo_full_block(&serial_out_fifo
, &block
, &blen
);
996 snprintf(s
, sizeof(s
), "o%03lx/%03x", block
- serial_out_fifo
.buf
,
1001 usb_send_block(block
, blen
, serial_free_out_fifo
, ep
);
1005 static void serial_free_out_fifo(int ep
, unsigned char *buf
, int len
)
1008 fifo_remove(&serial_out_fifo
, len
);
1009 serial_restart_output(ep
);
1012 void usb_serial_handle(void)
1023 b
= button_get(false);
1024 if (b
== BUTTON_PLAY
)
1026 else if (b
& BUTTON_REL
)
1032 if (!usb_connect_state
)
1034 if (usb_connected())
1038 (*(volatile unsigned long *)0x80005004) = 2;
1039 (*(volatile unsigned long *)0x80005008) = 0;
1040 for (i
= 0; i
< 100000; i
++)
1043 usb_connect_state
= 1;
1048 if (!usb_connected())
1050 usb_connect_state
= 0;
1055 usb_handle_interrupts();
1057 if (usb_state
== STATE_CONFIGURED
)
1059 if (endpoints
[1].in_min_len
< 0)
1060 serial_restart_input(1);
1061 if (!endpoints
[1].out_in_progress
)
1062 serial_restart_output(1);
1071 static int usb_serial_in_empty(void)
1073 return fifo_empty(&serial_in_fifo);
1077 int usb_serial_get_byte(void)
1079 while (fifo_empty(&serial_in_fifo
))
1080 usb_serial_handle();
1081 return fifo_get_byte(&serial_in_fifo
);
1084 int usb_serial_try_get_byte(void)
1087 if (fifo_empty(&serial_in_fifo
))
1090 r
= fifo_get_byte(&serial_in_fifo
);
1091 usb_serial_handle();
1097 static int usb_serial_out_full(void)
1099 return fifo_full(&serial_out_fifo);
1103 void usb_serial_put_byte(int b
)
1105 while (fifo_full(&serial_out_fifo
))
1106 usb_serial_handle();
1107 fifo_put_byte(&serial_out_fifo
, b
);
1108 usb_serial_handle();
1111 int usb_serial_try_put_byte(int b
)
1114 if (!fifo_full(&serial_out_fifo
))
1116 fifo_put_byte(&serial_out_fifo
, b
);
1119 usb_serial_handle();
1123 void usb_serial_init(void)
1125 fifo_init(&serial_in_fifo
);
1126 fifo_init(&serial_out_fifo
);
1127 usb_connect_state
= 0;