Implement as genuine a set_irq_level function for the sim as possible. The yield...
[Rockbox.git] / apps / main.c
blobd75bb726cfc2bcd653fad9e04de68b39283516b2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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"
21 #include "ata.h"
22 #include "ata_idle_notify.h"
23 #include "disk.h"
24 #include "fat.h"
25 #include "lcd.h"
26 #include "rtc.h"
27 #include "debug.h"
28 #include "led.h"
29 #include "kernel.h"
30 #include "button.h"
31 #include "tree.h"
32 #include "filetypes.h"
33 #include "panic.h"
34 #include "menu.h"
35 #include "system.h"
36 #include "usb.h"
37 #include "powermgmt.h"
38 #include "adc.h"
39 #include "i2c.h"
40 #ifndef DEBUG
41 #include "serial.h"
42 #endif
43 #include "audio.h"
44 #include "mp3_playback.h"
45 #include "thread.h"
46 #include "settings.h"
47 #include "backlight.h"
48 #include "status.h"
49 #include "debug_menu.h"
50 #include "version.h"
51 #include "sprintf.h"
52 #include "font.h"
53 #include "language.h"
54 #include "gwps.h"
55 #include "playlist.h"
56 #include "buffer.h"
57 #include "rolo.h"
58 #include "screens.h"
59 #include "power.h"
60 #include "talk.h"
61 #include "plugin.h"
62 #include "misc.h"
63 #include "dircache.h"
64 #ifdef HAVE_TAGCACHE
65 #include "tagcache.h"
66 #include "tagtree.h"
67 #endif
68 #include "lang.h"
69 #include "string.h"
70 #include "splash.h"
71 #include "eeprom_settings.h"
72 #include "scrobbler.h"
73 #include "icon.h"
75 #if (CONFIG_CODEC == SWCODEC)
76 #include "playback.h"
77 #include "pcmbuf.h"
78 #else
79 #define pcmbuf_init()
80 #endif
81 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
82 #include "pcm_record.h"
83 #endif
85 #ifdef BUTTON_REC
86 #define SETTINGS_RESET BUTTON_REC
87 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
88 #define SETTINGS_RESET BUTTON_A
89 #endif
91 #if CONFIG_TUNER
92 #include "radio.h"
93 #endif
94 #ifdef HAVE_MMC
95 #include "ata_mmc.h"
96 #endif
98 #ifdef HAVE_REMOTE_LCD
99 #include "lcd-remote.h"
100 #endif
102 #ifdef HAVE_USBSTACK
103 #include "usbstack.h"
104 #endif
106 #if CONFIG_USBOTG == USBOTG_ISP1362
107 #include "isp1362.h"
108 #endif
110 #if CONFIG_USBOTG == USBOTG_M5636
111 #include "m5636.h"
112 #endif
114 #include "cuesheet.h"
116 #ifdef SIMULATOR
117 #include "system-sdl.h"
118 #endif
120 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
122 const char appsversion[]=APPSVERSION;
124 static void init(void);
126 #ifdef SIMULATOR
127 void app_main(void)
128 #else
129 static void app_main(void)
130 #endif
132 init();
133 tree_gui_init();
134 root_menu();
137 static int init_dircache(bool preinit)
139 #ifdef HAVE_DIRCACHE
140 int result = 0;
141 bool clear = false;
143 if (preinit)
144 dircache_init();
146 if (!global_settings.dircache)
147 return 0;
149 # ifdef HAVE_EEPROM_SETTINGS
150 if (firmware_settings.initialized && firmware_settings.disk_clean
151 && preinit)
153 result = dircache_load();
155 if (result < 0)
157 firmware_settings.disk_clean = false;
158 if (global_status.dircache_size <= 0)
160 /* This will be in default language, settings are not
161 applied yet. Not really any easy way to fix that. */
162 gui_syncsplash(0, str(LANG_SCANNING_DISK));
163 clear = true;
166 dircache_build(global_status.dircache_size);
169 else
170 # endif
172 if (preinit)
173 return -1;
175 if (!dircache_is_enabled()
176 && !dircache_is_initializing())
178 if (global_status.dircache_size <= 0)
180 gui_syncsplash(0, str(LANG_SCANNING_DISK));
181 clear = true;
183 result = dircache_build(global_status.dircache_size);
186 if (result < 0)
188 /* Initialization of dircache failed. Manual action is
189 * necessary to enable dircache again.
191 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
192 global_settings.dircache = false;
197 if (clear)
199 backlight_on();
200 show_logo();
201 global_status.dircache_size = dircache_get_cache_size();
202 status_save();
205 return result;
206 #else
207 (void)preinit;
208 return 0;
209 #endif
212 #ifdef HAVE_TAGCACHE
213 static void init_tagcache(void)
215 bool clear = false;
217 tagcache_init();
219 while (!tagcache_is_initialized())
221 #ifdef HAVE_LCD_CHARCELLS
222 char buf[32];
223 #endif
224 int ret = tagcache_get_commit_step();
226 if (ret > 0)
228 #if CONFIG_CODEC == SWCODEC
229 /* hwcodec can't use voice here, as the database commit
230 * uses the audio buffer. */
231 static long talked_tick = 0;
232 if(global_settings.talk_menu
233 && (talked_tick == 0
234 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
236 talked_tick = current_tick;
237 talk_id(LANG_TAGCACHE_INIT, false);
238 talk_number(ret, true);
239 talk_id(VOICE_OF, true);
240 talk_number(tagcache_get_max_commit_step(), true);
242 #endif
243 #ifdef HAVE_LCD_BITMAP
244 gui_syncsplash(0, "%s [%d/%d]",
245 str(LANG_TAGCACHE_INIT), ret,
246 tagcache_get_max_commit_step());
247 #else
248 lcd_double_height(false);
249 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
250 tagcache_get_max_commit_step());
251 lcd_puts(0, 1, buf);
252 lcd_update();
253 #endif
254 clear = true;
256 sleep(HZ/4);
258 tagtree_init();
260 if (clear)
262 backlight_on();
263 show_logo();
266 #endif
268 #ifdef SIMULATOR
270 static void init(void)
272 init_threads();
273 buffer_init();
274 set_irq_level(0);
275 lcd_init();
276 #ifdef HAVE_REMOTE_LCD
277 lcd_remote_init();
278 #endif
279 font_init();
280 show_logo();
281 button_init();
282 backlight_init();
283 lang_init();
284 #ifdef DEBUG
285 debug_init();
286 #endif
287 /* Must be done before any code uses the multi-screen APi */
288 screen_access_init();
289 gui_syncstatusbar_init(&statusbars);
290 ata_init();
291 settings_reset();
292 settings_load(SETTINGS_ALL);
293 gui_sync_wps_init();
294 settings_apply();
295 init_dircache(true);
296 init_dircache(false);
297 #ifdef HAVE_TAGCACHE
298 init_tagcache();
299 #endif
300 sleep(HZ/2);
301 tree_mem_init();
302 filetype_init();
303 playlist_init();
305 #if CONFIG_CODEC != SWCODEC
306 mp3_init( global_settings.volume,
307 global_settings.bass,
308 global_settings.treble,
309 global_settings.balance,
310 global_settings.loudness,
311 global_settings.avc,
312 global_settings.channel_config,
313 global_settings.stereo_width,
314 global_settings.mdb_strength,
315 global_settings.mdb_harmonics,
316 global_settings.mdb_center,
317 global_settings.mdb_shape,
318 global_settings.mdb_enable,
319 global_settings.superbass);
321 /* audio_init must to know the size of voice buffer so init voice first */
322 talk_init();
323 #endif /* CONFIG_CODEC != SWCODEC */
325 scrobbler_init();
326 cuesheet_init();
328 audio_init();
329 button_clear_queue(); /* Empty the keyboard buffer */
332 #else
334 static void init(void)
336 int rc;
337 bool mounted = false;
338 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
339 /* if nobody initialized ATA before, I consider this a cold start */
340 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
341 #endif
343 system_init();
344 kernel_init();
346 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
347 set_cpu_frequency(CPUFREQ_NORMAL);
348 #ifdef CPU_COLDFIRE
349 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
350 #endif
351 cpu_boost(true);
352 #endif
354 buffer_init();
356 settings_reset();
358 power_init();
360 set_irq_level(0);
361 #ifdef CPU_ARM
362 set_fiq_status(FIQ_ENABLED);
363 #endif
364 lcd_init();
365 #ifdef HAVE_REMOTE_LCD
366 lcd_remote_init();
367 #endif
368 font_init();
370 show_logo();
371 lang_init();
373 #ifdef DEBUG
374 debug_init();
375 #else
376 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
377 serial_setup();
378 #endif
379 #endif
381 i2c_init();
383 #if CONFIG_RTC
384 rtc_init();
385 #endif
386 #ifdef HAVE_RTC_RAM
387 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
388 #endif
390 adc_init();
392 #ifdef HAVE_USBSTACK
393 usb_stack_init();
394 #endif
395 usb_init();
396 #if CONFIG_USBOTG == USBOTG_ISP1362
397 isp1362_init();
398 #elif CONFIG_USBOTG == USBOTG_M5636
399 m5636_init();
400 #endif
402 backlight_init();
404 button_init();
406 powermgmt_init();
408 #if CONFIG_TUNER
409 radio_init();
410 #endif
412 /* Must be done before any code uses the multi-screen APi */
413 screen_access_init();
414 gui_syncstatusbar_init(&statusbars);
416 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
417 if (coldstart && charger_inserted()
418 && !global_settings.car_adapter_mode
419 #ifdef ATA_POWER_PLAYERSTYLE
420 && !ide_powered() /* relies on probing result from bootloader */
421 #endif
424 rc = charging_screen(); /* display a "charging" screen */
425 if (rc == 1) /* charger removed */
426 power_off();
427 /* "On" pressed or USB connected: proceed */
428 show_logo(); /* again, to provide better visual feedback */
430 #endif
432 ata_idle_notify_init();
433 rc = ata_init();
434 if(rc)
436 #ifdef HAVE_LCD_BITMAP
437 char str[32];
438 lcd_clear_display();
439 snprintf(str, 31, "ATA error: %d", rc);
440 lcd_puts(0, 1, str);
441 lcd_puts(0, 3, "Press ON to debug");
442 lcd_update();
443 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
444 dbg_ports();
445 #endif
446 panicf("ata: %d", rc);
449 #ifdef HAVE_EEPROM_SETTINGS
450 eeprom_settings_init();
451 #endif
453 usb_start_monitoring();
454 while (usb_detect() == USB_INSERTED)
456 #ifdef HAVE_EEPROM_SETTINGS
457 firmware_settings.disk_clean = false;
458 #endif
459 /* enter USB mode early, before trying to mount */
460 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
461 #ifdef HAVE_MMC
462 if (!mmc_touched() ||
463 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
464 #endif
466 usb_screen();
467 mounted = true; /* mounting done @ end of USB mode */
469 #ifdef HAVE_USB_POWER
470 if (usb_powered()) /* avoid deadlock */
471 break;
472 #endif
475 if (!mounted)
477 rc = disk_mount_all();
478 if (rc<=0)
480 lcd_clear_display();
481 lcd_puts(0, 0, "No partition");
482 lcd_puts(0, 1, "found.");
483 #ifdef HAVE_LCD_BITMAP
484 lcd_puts(0, 2, "Insert USB cable");
485 lcd_puts(0, 3, "and fix it.");
486 #endif
487 lcd_update();
489 while(button_get(true) != SYS_USB_CONNECTED) {};
490 usb_screen();
491 system_reboot();
495 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
496 (CONFIG_KEYPAD == IRIVER_H10_PAD)
497 #ifdef SETTINGS_RESET
498 /* Reset settings if holding the reset button. (Rec on Archos,
499 A on Gigabeat) */
500 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
501 #else
502 /* Reset settings if the hold button is turned on */
503 if (button_hold())
504 #endif
506 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
507 settings_reset();
509 else
510 #endif
511 settings_load(SETTINGS_ALL);
513 if (init_dircache(true) < 0)
515 #ifdef HAVE_TAGCACHE
516 remove(TAGCACHE_STATEFILE);
517 #endif
520 gui_sync_wps_init();
521 settings_apply();
522 init_dircache(false);
523 #ifdef HAVE_TAGCACHE
524 init_tagcache();
525 #endif
527 #ifdef HAVE_EEPROM_SETTINGS
528 if (firmware_settings.initialized)
530 /* In case we crash. */
531 firmware_settings.disk_clean = false;
532 eeprom_settings_store();
534 #endif
535 status_init();
536 playlist_init();
537 tree_mem_init();
538 filetype_init();
539 scrobbler_init();
540 cuesheet_init();
542 #if CONFIG_CODEC != SWCODEC
543 /* No buffer allocation (see buffer.c) may take place after the call to
544 audio_init() since the mpeg thread takes the rest of the buffer space */
545 mp3_init( global_settings.volume,
546 global_settings.bass,
547 global_settings.treble,
548 global_settings.balance,
549 global_settings.loudness,
550 global_settings.avc,
551 global_settings.channel_config,
552 global_settings.stereo_width,
553 global_settings.mdb_strength,
554 global_settings.mdb_harmonics,
555 global_settings.mdb_center,
556 global_settings.mdb_shape,
557 global_settings.mdb_enable,
558 global_settings.superbass);
560 /* audio_init must to know the size of voice buffer so init voice first */
561 talk_init();
562 #endif /* CONFIG_CODEC != SWCODEC */
564 audio_init();
566 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
567 pcm_rec_init();
568 #endif
570 /* runtime database has to be initialized after audio_init() */
571 cpu_boost(false);
573 #ifdef AUTOROCK
575 int fd;
576 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
578 fd = open(filename, O_RDONLY);
579 if(fd >= 0) /* no complaint if it doesn't exist */
581 close(fd);
582 plugin_load((char*)filename, NULL); /* start if it does */
585 #endif /* #ifdef AUTOROCK */
587 #if CONFIG_CHARGING
588 car_adapter_mode_init();
589 #endif
592 #ifdef CPU_PP
593 void cop_main(void)
595 /* This is the entry point for the coprocessor
596 Anyone not running an upgraded bootloader will never reach this point,
597 so it should not be assumed that the coprocessor be usable even on
598 platforms which support it.
600 A kernel thread is initially setup on the coprocessor and immediately
601 destroyed for purposes of continuity. The cop sits idle until at least
602 one thread exists on it. */
604 /* 3G doesn't have Rolo or dual core support yet */
605 #if NUM_CORES > 1
606 system_init();
607 kernel_init();
608 /* This should never be reached */
609 #endif
610 while(1) {
611 COP_CTL = PROC_SLEEP;
614 #endif /* CPU_PP */
616 int main(void)
618 app_main();
620 while(1) {
621 #if (CONFIG_LED == LED_REAL)
622 led(true); sleep(HZ/10);
623 led(false); sleep(HZ/10);
624 #endif
626 return 0;
628 #endif