Battery blinks if >BATTERY_LEVEL_DANGEROUS
[kugel-rb.git] / firmware / usb.c
blobe78f8aead1af1d381d20c86042e5855addb39953
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
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 ****************************************************************************/
19 #include "config.h"
20 #include "sh7034.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "system.h"
24 #include "debug.h"
25 #include "ata.h"
26 #include "fat.h"
27 #include "disk.h"
28 #include "panic.h"
29 #include "lcd.h"
30 #include "adc.h"
31 #include "usb.h"
32 #include "button.h"
33 #include "sprintf.h"
35 extern void dbg_ports(void); /* NASTY! defined in apps/ */
37 #define USB_REALLY_BRAVE
40 #ifndef SIMULATOR
42 /* Messages from usb_tick */
43 #define USB_INSERTED 1
44 #define USB_EXTRACTED 2
46 /* Thread states */
47 #define EXTRACTING 1
48 #define EXTRACTED 2
49 #define INSERTED 3
50 #define INSERTING 4
52 /* The ADC tick reads one channel per tick, and we want to check 3 successive
53 readings on the USB voltage channel. This doesn't apply to the Player, but
54 debouncing the USB detection port won't hurt us either. */
55 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
56 static int countdown;
58 static int usb_state;
60 static char usb_stack[DEFAULT_STACK_SIZE];
61 static char usb_thread_name[] = "usb";
62 static struct event_queue usb_queue;
63 static bool last_usb_status;
64 static bool usb_monitor_enabled;
65 static bool usb_inverted;
67 static void usb_enable(bool on)
69 if(usb_inverted)
70 on = !on;
72 if(on)
73 PADR &= ~0x400; /* enable USB */
74 else
75 PADR |= 0x400;
76 PAIOR |= 0x400;
79 static void usb_slave_mode(bool on)
81 int rc;
82 struct partinfo* pinfo;
84 if(on)
86 DEBUGF("Entering USB slave mode\n");
87 ata_soft_reset();
88 ata_init();
89 ata_enable(false);
90 usb_enable(true);
92 else
94 DEBUGF("Leaving USB slave mode\n");
96 /* Let the ISDx00 settle */
97 sleep(HZ*1);
99 usb_enable(false);
101 rc = ata_init();
102 if(rc)
104 char str[32];
105 lcd_clear_display();
106 snprintf(str, 31, "ATA error: %d", rc);
107 lcd_puts(0, 0, str);
108 lcd_puts(0, 1, "Press ON to debug");
109 lcd_update();
110 while(button_get(true) != BUTTON_ON) {};
111 dbg_ports();
112 panicf("ata: %d",rc);
115 pinfo = disk_init();
116 if (!pinfo)
117 panicf("disk: NULL");
119 rc = fat_mount(pinfo[0].start);
120 if(rc)
121 panicf("mount: %d",rc);
125 static void usb_thread(void)
127 int num_acks_to_expect = -1;
128 bool waiting_for_ack;
129 struct event ev;
131 waiting_for_ack = false;
133 while(1)
135 queue_wait(&usb_queue, &ev);
136 switch(ev.id)
138 case USB_INSERTED:
139 /* Tell all threads that they have to back off the ATA.
140 We subtract one for our own thread. */
141 num_acks_to_expect =
142 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
143 waiting_for_ack = true;
144 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
145 num_acks_to_expect);
146 break;
148 case SYS_USB_CONNECTED_ACK:
149 if(waiting_for_ack)
151 num_acks_to_expect--;
152 if(num_acks_to_expect == 0)
154 DEBUGF("All threads have acknowledged the connect.\n");
155 #ifdef USB_REALLY_BRAVE
156 usb_slave_mode(true);
157 usb_state = USB_INSERTED;
158 usb_display_info();
159 #else
160 system_reboot();
161 #endif
163 else
165 DEBUGF("usb: got ack, %d to go...\n",
166 num_acks_to_expect);
169 break;
171 case USB_EXTRACTED:
172 if(usb_state == USB_INSERTED)
174 /* Only disable the USB mode if we really have enabled it
175 some threads might not have acknowledged the
176 insertion */
177 usb_slave_mode(false);
180 usb_state = USB_EXTRACTED;
182 /* Tell all threads that we are back in business */
183 num_acks_to_expect =
184 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
185 waiting_for_ack = true;
186 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
187 num_acks_to_expect);
188 break;
190 case SYS_USB_DISCONNECTED_ACK:
191 if(waiting_for_ack)
193 num_acks_to_expect--;
194 if(num_acks_to_expect == 0)
196 DEBUGF("All threads have acknowledged. "
197 "We're in business.\n");
199 else
201 DEBUGF("usb: got ack, %d to go...\n",
202 num_acks_to_expect);
205 break;
210 static void usb_tick(void)
212 bool current_status;
214 if(usb_monitor_enabled)
216 #ifdef ARCHOS_RECORDER
217 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
218 #else
219 current_status = (PADR & 0x8000)?false:true;
220 #endif
222 /* Only report when the status has changed */
223 if(current_status != last_usb_status)
225 last_usb_status = current_status;
226 countdown = NUM_POLL_READINGS;
228 else
230 /* Count down until it gets negative */
231 if(countdown >= 0)
232 countdown--;
234 /* Report to the thread if we have had 3 identical status
235 readings in a row */
236 if(countdown == 0)
238 if(current_status)
239 queue_post(&usb_queue, USB_INSERTED, NULL);
240 else
241 queue_post(&usb_queue, USB_EXTRACTED, NULL);
247 void usb_acknowledge(int id)
249 queue_post(&usb_queue, id, NULL);
252 void usb_init(void)
254 usb_state = USB_EXTRACTED;
255 usb_monitor_enabled = false;
256 countdown = -1;
258 /* This is lame. Really lame. We determine the polarity of the USB
259 enable pin by checking how it is set by the Archos firmware. */
260 if(PADR & 0x400)
261 usb_inverted = false;
262 else
263 usb_inverted = true;
265 usb_enable(false);
267 /* We assume that the USB cable is extracted */
268 last_usb_status = false;
270 queue_init(&usb_queue);
271 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
273 tick_add_task(usb_tick);
276 void usb_wait_for_disconnect(struct event_queue *q)
278 struct event ev;
280 /* Don't return until we get SYS_USB_DISCONNECTED */
281 while(1)
283 queue_wait(q, &ev);
284 if(ev.id == SYS_USB_DISCONNECTED)
286 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
287 return;
292 void usb_start_monitoring(void)
294 usb_monitor_enabled = true;
297 void usb_display_info(void)
299 lcd_stop_scroll();
300 lcd_clear_display();
302 #ifdef HAVE_LCD_BITMAP
303 lcd_puts(4, 3, "[USB Mode]");
304 lcd_update();
305 #else
306 lcd_puts(0, 0, "[USB Mode]");
307 #endif
310 #else
312 /* Dummy simulator functions */
313 void usb_acknowledge(int id)
315 id = id;
318 void usb_init(void)
322 void usb_start_monitoring(void)
326 void usb_display_info(void)
330 #endif