Do core interrupt masking in a less general fashion and save some instructions to...
[kugel-rb.git] / flash / bootbox / main.c
blob1f459b7bcb9f6805900b974425bf44db520fbb30
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Jörg Hohensohn aka [IDC]Dragon
12 * This is "Bootbox", a minimalistic loader, rescue firmware for just
13 * booting into a full features one. Aside from that it does charging
14 * and USB mode, to enable copying the desired firmware.
16 * All files in this archive are subject to the GNU General Public License.
17 * See the file COPYING in the source tree root for full license agreement.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "cpu.h"
28 #include "system.h"
29 #include "lcd.h"
30 #include "kernel.h"
31 #include "thread.h"
32 #include "ata.h"
33 #include "disk.h"
34 #include "font.h"
35 #include "adc.h"
36 #include "button.h"
37 #include "panic.h"
38 #include "power.h"
39 #include "file.h"
40 #include "buffer.h"
41 #include "rolo.h"
42 #include "usb.h"
43 #include "powermgmt.h"
45 void usb_screen(void)
47 lcd_clear_display();
48 lcd_puts(0, 0, "USB mode");
49 lcd_update();
51 usb_acknowledge(SYS_USB_CONNECTED_ACK);
52 while(usb_wait_for_disconnect_w_tmo(&button_queue, HZ)) {
56 int show_logo(void)
58 lcd_clear_display();
59 lcd_puts(0, 0, "Rockbox");
60 lcd_puts(0, 1, "Rescue boot");
61 lcd_update();
63 return 0;
66 #if CONFIG_CHARGING
68 bool backlight_get_on_when_charging(void)
70 return false;
73 void charging_screen(void)
75 unsigned int button;
76 const char* msg;
78 ide_power_enable(false); /* power down the disk, else would be spinning */
80 lcd_clear_display();
84 #if CONFIG_CHARGING == CHARGING_CONTROL
85 if (charge_state == CHARGING)
86 msg = "charging";
87 else if (charge_state == TOPOFF)
88 msg = "topoff charge";
89 else if (charge_state == TRICKLE)
90 msg = "trickle charge";
91 else
92 msg = "not charging";
94 #else
95 msg = "charging";
96 #endif
97 lcd_puts(0, 0, msg);
99 char buf[32];
100 int battv = battery_voltage();
101 snprintf(buf, sizeof(buf), "%d.%02dV %d%%",
102 battv / 1000, (battv % 1000) / 10, battery_level());
103 lcd_puts(0, 1, buf);
105 lcd_update();
107 button = button_get_w_tmo(HZ/2);
108 #ifdef BUTTON_ON
109 if (button == (BUTTON_ON | BUTTON_REL))
110 #else
111 if (button == (BUTTON_RIGHT | BUTTON_REL))
112 #endif
113 break; /* start */
114 else
116 if (usb_detect())
117 break;
118 else if (!charger_inserted())
119 power_off(); /* charger removed: power down */
121 } while (1);
123 #endif /* CONFIG_CHARGING */
125 /* prompt user to plug USB and fix a problem */
126 void prompt_usb(const char* msg1, const char* msg2)
128 int button;
129 lcd_clear_display();
130 lcd_puts(0, 0, msg1);
131 lcd_puts(0, 1, msg2);
132 #ifdef HAVE_LCD_BITMAP
133 lcd_puts(0, 2, "Insert USB cable");
134 lcd_puts(0, 3, "and fix it.");
135 #endif
136 lcd_update();
139 button = button_get(true);
140 if (button == SYS_POWEROFF)
142 power_off();
144 } while (button != SYS_USB_CONNECTED);
145 usb_screen();
146 system_reboot();
149 void main(void)
151 int rc;
153 power_init();
154 system_init();
155 kernel_init();
156 buffer_init();
157 lcd_init();
158 show_logo();
159 enable_irq();
160 adc_init();
161 usb_init();
162 button_init();
163 powermgmt_init();
165 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
166 if (charger_inserted()
167 #ifdef ATA_POWER_PLAYERSTYLE
168 && !ide_powered() /* relies on probing result from bootloader */
169 #endif
172 charging_screen(); /* display a "charging" screen */
173 show_logo(); /* again, to provide better visual feedback */
175 #endif
177 rc = ata_init();
178 if(rc)
180 #ifdef HAVE_LCD_BITMAP
181 char str[32];
182 lcd_clear_display();
183 snprintf(str, 31, "ATA error: %d", rc);
184 lcd_puts(0, 1, str);
185 lcd_update();
186 while(!(button_get(true) & BUTTON_REL));
187 #endif
188 panicf("ata: %d", rc);
191 //disk_init();
192 usb_start_monitoring();
193 while (usb_detect())
194 { /* enter USB mode early, before trying to mount */
195 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
197 usb_screen();
201 rc = disk_mount_all();
202 if (rc<=0)
204 prompt_usb("No partition", "found.");
207 { // rolo the firmware
208 static const char filename[] = "/" BOOTFILE;
209 rolo_load((char*)filename); /* won't return if started */
211 prompt_usb("No firmware", filename);
217 /* These functions are present in the firmware library, but we reimplement
218 them here because the originals do a lot more than we want */
220 void screen_dump(void)
224 int dbg_ports(void)
226 return 0;
229 void audio_stop(void)
233 int audio_status(void)
235 return 0;
238 void audio_stop_recording(void)
242 void mp3_shutdown(void)
246 void i2c_init(void)
250 void backlight_on(void)