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"
43 #define ISP1582_BASE (0x24100000)
44 #define ISP1582_ADDRESS (*(volatile unsigned char *)ISP1582_BASE)
45 #define ISP1582_MODE (*(volatile unsigned short *)(ISP1582_BASE + 0xc))
46 #define ISP1582_INTCONF (*(volatile unsigned char *)(ISP1582_BASE + 0x10))
47 #define ISP1582_OTG (*(volatile unsigned char *)(ISP1582_BASE + 0x12))
48 #define ISP1582_INTEN (*(volatile unsigned long *)(ISP1582_BASE + 0x14))
50 #define ISP1582_EPINDEX (*(volatile unsigned char *)(ISP1582_BASE + 0x2c))
51 #define ISP1582_CTRLFUN (*(volatile unsigned char *)(ISP1582_BASE + 0x28))
52 #define ISP1582_DATA (*(volatile unsigned short *)(ISP1582_BASE + 0x20))
53 #define ISP1582_BUFLEN (*(volatile unsigned short *)(ISP1582_BASE + 0x1c))
54 #define ISP1582_BUFSTAT (*(volatile unsigned char *)(ISP1582_BASE + 0x1e))
55 #define ISP1582_MAXPKSZ (*(volatile unsigned short *)(ISP1582_BASE + 0x04))
56 #define ISP1582_EPTYPE (*(volatile unsigned short *)(ISP1582_BASE + 0x08))
58 #define ISP1582_INT (*(volatile unsigned long *)(ISP1582_BASE + 0x18))
59 #define ISP1582_CHIPID (*(volatile unsigned long *)(ISP1582_BASE + 0x70))
60 #define ISP1582_FRAMENO (*(volatile unsigned short *)(ISP1582_BASE + 0x74))
61 #define ISP1582_UNLOCK (*(volatile unsigned short *)(ISP1582_BASE + 0x7c))
63 #define ISP1582_UNLOCK_CODE 0xaa37
67 #define STATE_DEFAULT 0
68 #define STATE_ADDRESS 1
69 #define STATE_CONFIGURED 2
75 unsigned char *out_buf
;
78 void (*out_done
)(int, unsigned char *, int);
79 unsigned char out_in_progress
;
81 unsigned char *in_buf
;
85 void (*in_done
)(int, unsigned char *, int);
88 unsigned char halt
[2];
89 unsigned char enabled
[2];
90 short max_pkt_size
[2];
93 static char usb_connect_state
;
95 static struct usb_endpoint endpoints
[N_ENDPOINTS
];
97 static unsigned char setup_pkt_buf
[8];
98 static unsigned char setup_out_buf
[8];
99 static unsigned char usb_state
;
100 static unsigned char usb_remote_wakeup
;
103 static unsigned char int_count
[32];
105 static int log_pos_x
= 0;
106 static int log_pos_y
= 3;
109 static void nop_f(void)
114 static void log_char(char c
)
121 lcd_puts(log_pos_x
, log_pos_y
, s
);
136 #define SERIAL_BUF_SIZE 1024
140 unsigned char buf
[SERIAL_BUF_SIZE
];
144 static struct serial_fifo serial_in_fifo
;
145 static struct serial_fifo serial_out_fifo
;
146 static unsigned char serial_in_pkt
[64];
148 static unsigned char device_descriptor
[18] = {
150 0x01, /* descriptor type */
151 0x10, 0x01, /* USB version (1.1) */
152 0xff, 0xff, /* class and subclass */
154 0x40, /* max packet size 0 */
155 0x02, 0x41, /* vendor (iRiver) */
156 0x07, 0xee, /* product (0xee07) */
157 0x01, 0x00, /* device version */
158 0x01, /* manufacturer string */
159 0x02, /* product string */
160 0x00, /* serial number string */
161 0x01 /* number of configurations */
164 static unsigned char cfg_descriptor
[32] = {
166 0x02, /* descriptor type */
167 0x20, 0x00, /* total length */
168 0x01, /* number of interfaces */
169 0x01, /* configuration value */
170 0x00, /* configuration string */
171 0x80, /* attributes (none) */
172 0x32, /* max power (100 mA) */
173 /* interface descriptor */
175 0x04, /* descriptor type */
176 0x00, /* interface number */
177 0x00, /* alternate setting */
178 0x02, /* number of endpoints */
179 0xff, /* interface class */
180 0xff, /* interface subclass */
181 0xff, /* interface protocol */
182 0x00, /* interface string */
185 0x05, /* descriptor type */
186 0x81, /* endpoint 1 IN */
187 0x02, /* attributes (bulk) */
188 0x40, 0x00, /* max packet size */
192 0x05, /* descriptor type */
193 0x01, /* endpoint 1 IN */
194 0x02, /* attributes (bulk) */
195 0x40, 0x00, /* max packet size */
199 static unsigned char lang_descriptor
[4] = {
201 0x03, /* descriptor type */
202 0x09, 0x04 /* English (US) */
205 #define N_STRING_DESCRIPTORS 2
207 static unsigned char string_descriptor_vendor
[] = {
209 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
210 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
211 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
213 static unsigned char string_descriptor_product
[] = {
215 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
216 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
218 static unsigned char *string_descriptor
[N_STRING_DESCRIPTORS
] = {
219 string_descriptor_vendor
,
220 string_descriptor_product
223 static inline int ep_index(int n
, int dir
)
225 return (n
<< 1) | dir
;
228 static inline int epidx_dir(int idx
)
233 static inline int epidx_n(int idx
)
238 int usb_connected(void)
240 return GPIO7_READ
& 1;
243 static inline void usb_select_endpoint(int idx
)
245 ISP1582_EPINDEX
= idx
;
248 static inline void usb_select_setup_endpoint(void)
250 ISP1582_EPINDEX
= 0x20;
253 void usb_setup_endpoint(int idx
, int max_pkt_size
, int type
)
255 struct usb_endpoint
*ep
;
257 usb_select_endpoint(idx
);
258 ISP1582_MAXPKSZ
= max_pkt_size
;
259 /* |= is in the original firmware */
260 ISP1582_EPTYPE
|= 0x1c | type
;
262 ISP1582_CTRLFUN
|= 0x10;
263 ISP1582_INTEN
|= (1 << (10 + idx
));
265 ep
= &(endpoints
[epidx_n(idx
)]);
266 ep
->halt
[epidx_dir(idx
)] = 0;
267 ep
->enabled
[epidx_dir(idx
)] = 1;
268 ep
->out_in_progress
= 0;
271 ep
->max_pkt_size
[epidx_dir(idx
)] = max_pkt_size
;
274 void usb_disable_endpoint(int idx
)
276 usb_select_endpoint(idx
);
278 ISP1582_INTEN
&= ~(1 << (10 + idx
));
279 endpoints
[epidx_n(idx
)].enabled
[epidx_dir(idx
)] = 1;
282 void usb_reconnect(void)
285 ISP1582_MODE
&= ~1; /* SOFTCT off */
286 for (i
= 0; i
< 10000; i
++)
288 ISP1582_MODE
|= 1; /* SOFTCT on */
291 void usb_cleanup(void)
293 ISP1582_MODE
&= ~1; /* SOFTCT off */
296 void usb_setup(int reset
)
300 for (i
= 0; i
< N_ENDPOINTS
; i
++)
301 endpoints
[i
].enabled
[0] = endpoints
[i
].enabled
[1] = 0;
303 ISP1582_UNLOCK
= ISP1582_UNLOCK_CODE
;
305 ISP1582_MODE
= 0x88; /* CLKAON | GLINTENA */
306 ISP1582_INTCONF
= 0x57;
307 ISP1582_INTEN
= 0xd39;
309 ISP1582_ADDRESS
= reset
? 0x80: 0;
311 usb_setup_endpoint(ep_index(0, DIR_RX
), 64, 0);
312 usb_setup_endpoint(ep_index(0, DIR_TX
), 64, 0);
314 ISP1582_MODE
|= 1; /* SOFTCT on */
316 usb_state
= STATE_DEFAULT
;
317 usb_remote_wakeup
= 0;
320 static int usb_get_packet(unsigned char *buf
, int max_len
)
323 len
= ISP1582_BUFLEN
;
325 if (max_len
< 0 || max_len
> len
)
331 unsigned short d
= ISP1582_DATA
;
336 buf
[i
] = (d
>> 8) & 0xff;
342 static void usb_receive(int n
)
346 if (endpoints
[n
].halt
[DIR_RX
]
347 || !endpoints
[n
].enabled
[DIR_RX
]
348 || endpoints
[n
].in_min_len
< 0
349 || !endpoints
[n
].in_ack
)
352 endpoints
[n
].in_ack
= 0;
354 usb_select_endpoint(ep_index(n
, DIR_RX
));
356 len
= usb_get_packet(endpoints
[n
].in_buf
+ endpoints
[n
].in_ptr
,
357 endpoints
[n
].in_max_len
- endpoints
[n
].in_ptr
);
358 endpoints
[n
].in_ptr
+= len
;
359 if (endpoints
[n
].in_ptr
>= endpoints
[n
].in_min_len
) {
360 endpoints
[n
].in_min_len
= -1;
361 if (endpoints
[n
].in_done
)
362 (*(endpoints
[n
].in_done
))(n
, endpoints
[n
].in_buf
,
363 endpoints
[n
].in_ptr
);
367 static int usb_out_buffer_full(int ep
)
369 usb_select_endpoint(ep_index(ep
, DIR_TX
));
370 if (ISP1582_EPTYPE
& 4)
371 return (ISP1582_BUFSTAT
& 3) == 3;
373 return (ISP1582_BUFSTAT
& 3) != 0;
376 static void usb_send(int n
)
378 int max_pkt_size
, len
;
383 if (endpoints
[n
].halt
[DIR_TX
])
385 if (!endpoints
[n
].out_in_progress
)
389 if (endpoints
[n
].halt
[DIR_TX
]
390 || !endpoints
[n
].enabled
[DIR_TX
]
391 || !endpoints
[n
].out_in_progress
)
394 if (endpoints
[n
].out_ptr
< 0)
396 endpoints
[n
].out_in_progress
= 0;
397 if (endpoints
[n
].out_done
)
398 (*(endpoints
[n
].out_done
))(n
, endpoints
[n
].out_buf
,
399 endpoints
[n
].out_len
);
403 if (usb_out_buffer_full(n
))
409 usb_select_endpoint(ep_index(n
, DIR_TX
));
410 max_pkt_size
= endpoints
[n
].max_pkt_size
[DIR_TX
];
411 len
= endpoints
[n
].out_len
- endpoints
[n
].out_ptr
;
412 if (len
> max_pkt_size
)
415 log_char('0' + (len
% 10));
416 ISP1582_BUFLEN
= len
;
417 p
= endpoints
[n
].out_buf
+ endpoints
[n
].out_ptr
;
419 while (len
- i
>= 2) {
420 ISP1582_DATA
= p
[i
] | (p
[i
+ 1] << 8);
426 endpoints
[n
].out_ptr
+= len
;
429 if (endpoints[n].out_ptr == endpoints[n].out_len
430 && len < max_pkt_size)
432 if (endpoints
[n
].out_ptr
== endpoints
[n
].out_len
)
433 endpoints
[n
].out_ptr
= -1;
436 static void usb_stall_endpoint(int idx
)
438 usb_select_endpoint(idx
);
439 ISP1582_CTRLFUN
|= 1;
440 endpoints
[epidx_n(idx
)].halt
[epidx_dir(idx
)] = 1;
443 static void usb_unstall_endpoint(int idx
)
445 usb_select_endpoint(idx
);
446 ISP1582_CTRLFUN
&= ~1;
447 ISP1582_EPTYPE
&= ~8;
449 ISP1582_CTRLFUN
|= 0x10;
450 if (epidx_dir(idx
) == DIR_TX
)
451 endpoints
[epidx_n(idx
)].out_in_progress
= 0;
454 endpoints
[epidx_n(idx
)].in_min_len
= -1;
455 endpoints
[epidx_n(idx
)].in_ack
= 0;
457 endpoints
[epidx_n(idx
)].halt
[epidx_dir(idx
)] = 0;
460 static void usb_status_ack(int dir
)
462 log_char(dir
? '@' : '#');
463 usb_select_endpoint(ep_index(0, dir
));
464 ISP1582_CTRLFUN
|= 2;
467 static void usb_set_address(int adr
)
469 ISP1582_ADDRESS
= adr
| 0x80;
472 static void usb_data_stage_enable(int dir
)
474 usb_select_endpoint(ep_index(0, dir
));
475 ISP1582_CTRLFUN
|= 4;
478 static void usb_request_error(void)
480 usb_stall_endpoint(ep_index(0, DIR_TX
));
481 usb_stall_endpoint(ep_index(0, DIR_RX
));
484 static void usb_receive_block(unsigned char *buf
, int min_len
,
486 void (*in_done
)(int, unsigned char *, int),
489 endpoints
[ep
].in_done
= in_done
;
490 endpoints
[ep
].in_buf
= buf
;
491 endpoints
[ep
].in_max_len
= max_len
;
492 endpoints
[ep
].in_min_len
= min_len
;
493 endpoints
[ep
].in_ptr
= 0;
497 static void usb_send_block(unsigned char *buf
, int len
,
498 void (*done
)(int, unsigned char *, int),
501 endpoints
[ep
].out_done
= done
;
502 endpoints
[ep
].out_buf
= buf
;
503 endpoints
[ep
].out_len
= len
;
504 endpoints
[ep
].out_ptr
= 0;
505 endpoints
[ep
].out_in_progress
= 1;
509 static void out_send_status(int n
, unsigned char *buf
, int len
)
514 usb_status_ack(DIR_RX
);
517 static void usb_send_block_and_status(unsigned char *buf
, int len
, int ep
)
519 usb_send_block(buf
, len
, out_send_status
, ep
);
522 static void usb_setup_set_address(int adr
)
524 usb_set_address(adr
);
525 usb_state
= adr
? STATE_ADDRESS
: STATE_DEFAULT
;
526 usb_status_ack(DIR_TX
);
529 static void usb_send_descriptor(unsigned char *device_descriptor
,
530 int descriptor_len
, int buffer_len
)
532 int len
= descriptor_len
< buffer_len
? descriptor_len
: buffer_len
;
533 usb_send_block_and_status(device_descriptor
, len
, 0);
536 static void usb_setup_get_descriptor(int type
, int index
, int lang
, int len
)
539 usb_data_stage_enable(DIR_TX
);
544 usb_send_descriptor(device_descriptor
,
545 sizeof(device_descriptor
), len
);
551 usb_send_descriptor(cfg_descriptor
,
552 sizeof(cfg_descriptor
), len
);
558 usb_send_descriptor(lang_descriptor
,
559 sizeof(lang_descriptor
), len
);
560 else if (index
<= N_STRING_DESCRIPTORS
)
561 usb_send_descriptor(string_descriptor
[index
- 1],
562 string_descriptor
[index
- 1][0],
572 static void usb_setup_get_configuration(void)
574 setup_out_buf
[0] = (usb_state
== STATE_CONFIGURED
) ? 1 : 0;
575 usb_data_stage_enable(DIR_TX
);
576 usb_send_block_and_status(setup_out_buf
, 1, 0);
579 static void usb_setup_interface(void)
581 usb_setup_endpoint(ep_index(1, DIR_RX
), 64, TYPE_BULK
);
582 usb_setup_endpoint(ep_index(1, DIR_TX
), 64, TYPE_BULK
);
585 static void usb_setup_set_configuration(int value
)
590 usb_disable_endpoint(ep_index(1, DIR_RX
));
591 usb_disable_endpoint(ep_index(1, DIR_TX
));
592 usb_state
= STATE_ADDRESS
;
593 usb_status_ack(DIR_TX
);
596 usb_setup_interface();
597 usb_state
= STATE_CONFIGURED
;
598 usb_status_ack(DIR_TX
);
605 static void usb_send_status(void)
607 usb_data_stage_enable(DIR_TX
);
608 usb_send_block_and_status(setup_out_buf
, 2, 0);
611 static void usb_setup_get_device_status(void)
613 setup_out_buf
[0] = (usb_remote_wakeup
!= 0) ? 2 : 0;
614 setup_out_buf
[1] = 0;
618 static void usb_setup_clear_device_feature(int feature
)
621 usb_remote_wakeup
= 0;
622 usb_status_ack(DIR_TX
);
627 static void usb_setup_set_device_feature(int feature
)
630 usb_remote_wakeup
= 1;
631 usb_status_ack(DIR_TX
);
636 static void usb_setup_clear_endpoint_feature(int endpoint
, int feature
)
638 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
640 else if ((endpoint
& 0xf) == 1)
642 usb_unstall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
643 usb_status_ack(DIR_TX
);
649 static void usb_setup_set_endpoint_feature(int endpoint
, int feature
)
651 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
653 else if ((endpoint
& 0xf) == 1)
655 usb_stall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
656 usb_status_ack(DIR_TX
);
662 static void usb_setup_get_interface_status(int interface
)
664 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
668 setup_out_buf
[0] = setup_out_buf
[1] = 0;
673 static void usb_setup_get_endpoint_status(int endpoint
)
675 if ((usb_state
== STATE_CONFIGURED
&& (endpoint
& 0xf) <= 1)
676 || (usb_state
== STATE_ADDRESS
&& (endpoint
& 0xf) == 0))
678 setup_out_buf
[0] = endpoints
[endpoint
& 0xf].halt
[endpoint
>> 7];
679 setup_out_buf
[1] = 0;
686 static void usb_setup_get_interface(int interface
)
688 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
692 setup_out_buf
[0] = 0;
693 usb_data_stage_enable(DIR_TX
);
694 usb_send_block_and_status(setup_out_buf
, 1, 0);
698 static void usb_setup_set_interface(int interface
, int setting
)
700 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0 || setting
!= 0)
704 usb_setup_interface();
705 usb_status_ack(DIR_TX
);
709 static void usb_handle_setup_pkt(unsigned char *pkt
)
711 switch ((pkt
[0] << 8) | pkt
[1])
715 usb_setup_set_address(pkt
[2]);
719 usb_setup_get_descriptor(pkt
[3], pkt
[2], (pkt
[5] << 8) | pkt
[4],
720 (pkt
[7] << 8) | pkt
[6]);
723 usb_setup_get_configuration();
726 usb_setup_set_configuration(pkt
[2]);
729 usb_setup_get_device_status();
732 usb_setup_get_interface_status(pkt
[4]);
735 usb_setup_get_endpoint_status(pkt
[4]);
738 usb_setup_clear_device_feature(pkt
[2]);
741 usb_setup_clear_endpoint_feature(pkt
[4], pkt
[2]);
744 usb_setup_set_device_feature(pkt
[2]);
747 usb_setup_set_endpoint_feature(pkt
[4], pkt
[2]);
750 usb_setup_get_interface(pkt
[4]);
753 usb_setup_set_interface(pkt
[4], pkt
[2]);
756 /* set interface feature */
758 /* clear interface feature */
768 static void usb_handle_setup_rx(void)
775 usb_select_setup_endpoint();
776 len
= usb_get_packet(setup_pkt_buf
, 8);
779 usb_handle_setup_pkt(setup_pkt_buf
);
783 snprintf(s, 10, "l%02x", len);
786 for (i
= 0; i
< 8; i
++)
787 snprintf(s
+ i
* 2, 3, "%02x", setup_pkt_buf
[i
]);
793 static void usb_handle_data_int(int ep
, int dir
)
799 endpoints
[ep
].in_ack
= 1;
804 static void usb_handle_int(int i
)
809 snprintf(s, sizeof(s), "%02d", i);
825 usb_handle_data_int((i
- 10) / 2, i
% 2);
836 usb_handle_setup_rx();
843 void usb_handle_interrupts(void)
856 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
861 ints
= ISP1582_INT
& ISP1582_INTEN
;
865 while (!(ints
& (1 << i
)))
867 ISP1582_INT
= 1 << i
;
871 for (i
= 0; i
< 8; i
++)
872 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
]);
874 for (i
= 0; i
< 8; i
++)
875 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
+ 8]);
881 lcd_puts(0, 3, usb_connected() ? "C" : "N");
887 static inline int fifo_mod(int n
)
889 return (n
>= SERIAL_BUF_SIZE
) ? n
- SERIAL_BUF_SIZE
: n
;
892 static void fifo_init(struct serial_fifo
*fifo
)
894 fifo
->head
= fifo
->tail
= 0;
897 static int fifo_empty(struct serial_fifo
*fifo
)
899 return fifo
->head
== fifo
->tail
;
902 static int fifo_full(struct serial_fifo
*fifo
)
904 return fifo_mod(fifo
->head
+ 1) == fifo
->tail
;
907 static void fifo_remove(struct serial_fifo
*fifo
, int n
)
909 fifo
->tail
= fifo_mod(fifo
->tail
+ n
);
914 static void fifo_add(struct serial_fifo *fifo, int n)
916 fifo->head = fifo_mod(fifo->head + n);
919 static void fifo_free_block(struct serial_fifo *fifo,
920 unsigned char **ptr, int *len)
922 *ptr = fifo->buf + fifo->head;
923 if (fifo->head >= fifo->tail)
925 int l = SERIAL_BUF_SIZE - fifo->head;
931 *len = fifo->tail - fifo->head - 1;
935 static int fifo_free_space(struct serial_fifo
*fifo
)
937 if (fifo
->head
>= fifo
->tail
)
938 return SERIAL_BUF_SIZE
- (fifo
->head
- fifo
->tail
) - 1;
940 return fifo
->tail
- fifo
->head
- 1;
943 static int fifo_get_byte(struct serial_fifo
*fifo
)
945 int r
= fifo
->buf
[fifo
->tail
];
946 fifo
->tail
= fifo_mod(fifo
->tail
+ 1);
950 static void fifo_put_byte(struct serial_fifo
*fifo
, int b
)
952 fifo
->buf
[fifo
->head
] = b
;
953 fifo
->head
= fifo_mod(fifo
->head
+ 1);
956 static void fifo_full_block(struct serial_fifo
*fifo
,
957 unsigned char **ptr
, int *len
)
959 *ptr
= fifo
->buf
+ fifo
->tail
;
960 if (fifo
->head
>= fifo
->tail
)
961 *len
= fifo
->head
- fifo
->tail
;
963 *len
= SERIAL_BUF_SIZE
- fifo
->tail
;
966 static void serial_fill_in_fifo(int ep
, unsigned char *buf
, int len
);
967 static void serial_free_out_fifo(int ep
, unsigned char *buf
, int len
);
969 static void serial_restart_input(int ep
)
971 if (fifo_free_space(&serial_in_fifo
) >= 64)
972 usb_receive_block(serial_in_pkt
, 1, 64, serial_fill_in_fifo
, ep
);
975 static void serial_fill_in_fifo(int ep
, unsigned char *buf
, int len
)
978 for (i
= 0; i
< len
; i
++)
979 fifo_put_byte(&serial_in_fifo
, buf
[i
]);
980 serial_restart_input(ep
);
983 static void serial_restart_output(int ep
)
985 unsigned char *block
;
987 fifo_full_block(&serial_out_fifo
, &block
, &blen
);
991 lcd_putsf(0, 2, "o%03lx/%03x", block
- serial_out_fifo
.buf
, blen
);
994 usb_send_block(block
, blen
, serial_free_out_fifo
, ep
);
998 static void serial_free_out_fifo(int ep
, unsigned char *buf
, int len
)
1001 fifo_remove(&serial_out_fifo
, len
);
1002 serial_restart_output(ep
);
1005 void usb_serial_handle(void)
1016 b
= button_get(false);
1017 if (b
== BUTTON_PLAY
)
1019 else if (b
& BUTTON_REL
)
1025 if (!usb_connect_state
)
1027 if (usb_connected())
1031 (*(volatile unsigned long *)0x80005004) = 2;
1032 (*(volatile unsigned long *)0x80005008) = 0;
1033 for (i
= 0; i
< 100000; i
++)
1036 usb_connect_state
= 1;
1041 if (!usb_connected())
1043 usb_connect_state
= 0;
1048 usb_handle_interrupts();
1050 if (usb_state
== STATE_CONFIGURED
)
1052 if (endpoints
[1].in_min_len
< 0)
1053 serial_restart_input(1);
1054 if (!endpoints
[1].out_in_progress
)
1055 serial_restart_output(1);
1064 static int usb_serial_in_empty(void)
1066 return fifo_empty(&serial_in_fifo);
1070 int usb_serial_get_byte(void)
1072 while (fifo_empty(&serial_in_fifo
))
1073 usb_serial_handle();
1074 return fifo_get_byte(&serial_in_fifo
);
1077 int usb_serial_try_get_byte(void)
1080 if (fifo_empty(&serial_in_fifo
))
1083 r
= fifo_get_byte(&serial_in_fifo
);
1084 usb_serial_handle();
1090 static int usb_serial_out_full(void)
1092 return fifo_full(&serial_out_fifo);
1096 void usb_serial_put_byte(int b
)
1098 while (fifo_full(&serial_out_fifo
))
1099 usb_serial_handle();
1100 fifo_put_byte(&serial_out_fifo
, b
);
1101 usb_serial_handle();
1104 int usb_serial_try_put_byte(int b
)
1107 if (!fifo_full(&serial_out_fifo
))
1109 fifo_put_byte(&serial_out_fifo
, b
);
1112 usb_serial_handle();
1116 void usb_serial_init(void)
1118 fifo_init(&serial_in_fifo
);
1119 fifo_init(&serial_out_fifo
);
1120 usb_connect_state
= 0;