blob: 55897de5f535beb00ec575fa83b8eacf3641b2f7 [file] [log] [blame]
Björn Stenberg82182852002-05-16 12:53:40 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Björn Stenberg
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
Linus Nielsen Feltzing688b4dd2004-10-15 02:16:31 +000019#include "config.h"
20
Björn Stenberge324db62002-05-17 19:50:37 +000021#include "ata.h"
22#include "disk.h"
23#include "fat.h"
24#include "lcd.h"
Linus Nielsen Feltzing9b3a3be2002-06-24 13:51:40 +000025#include "rtc.h"
Björn Stenberge324db62002-05-17 19:50:37 +000026#include "debug.h"
27#include "led.h"
28#include "kernel.h"
29#include "button.h"
Björn Stenberg82182852002-05-16 12:53:40 +000030#include "tree.h"
Björn Stenberg55c7da22002-05-21 14:28:10 +000031#include "panic.h"
32#include "menu.h"
Linus Nielsen Feltzing0f607752002-05-29 09:14:38 +000033#include "system.h"
Linus Nielsen Feltzing6c77cc32002-06-30 13:24:29 +000034#include "usb.h"
Heikki Hannikainen6eb42542002-08-06 10:52:51 +000035#include "powermgmt.h"
Linus Nielsen Feltzing7d3c31b2002-06-30 20:25:37 +000036#include "adc.h"
Linus Nielsen Feltzing6d2e4922002-07-02 21:40:12 +000037#include "i2c.h"
Björn Stenberg18239b82002-08-12 12:44:18 +000038#ifndef DEBUG
39#include "serial.h"
40#endif
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +000041#include "audio.h"
Jörg Hohensohnf9933652004-01-05 20:42:51 +000042#include "mp3_playback.h"
Björn Stenberg3ed28742002-05-24 15:51:39 +000043#include "main_menu.h"
Felix Arendse096c862002-06-12 15:31:35 +000044#include "thread.h"
Linus Nielsen Feltzing31e20972002-06-20 08:59:54 +000045#include "settings.h"
Linus Nielsen Feltzing9b3a3be2002-06-24 13:51:40 +000046#include "backlight.h"
Linus Nielsen Feltzing97f94b42002-07-22 22:44:03 +000047#include "status.h"
Björn Stenberg6b25f792002-07-15 11:02:12 +000048#include "debug_menu.h"
Daniel Stenberg7b719112002-05-29 10:05:27 +000049#include "version.h"
Linus Nielsen Feltzing6c77cc32002-06-30 13:24:29 +000050#include "sprintf.h"
Daniel Stenberg93b231c2002-09-12 13:33:59 +000051#include "font.h"
Daniel Stenberg5b662ff2002-09-25 11:42:10 +000052#include "language.h"
Björn Stenbergdea31222002-10-08 15:42:40 +000053#include "wps-display.h"
Björn Stenberg7bb746b2003-04-24 17:31:36 +000054#include "playlist.h"
Linus Nielsen Feltzing20d031f2003-05-09 16:01:21 +000055#include "buffer.h"
Jörg Hohensohnc47f8c02003-07-03 22:13:39 +000056#include "rolo.h"
Jörg Hohensohn5dd17b12003-07-17 20:29:51 +000057#include "screens.h"
58#include "power.h"
Jörg Hohensohn4f36ea82004-03-14 21:33:53 +000059#include "talk.h"
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +000060#include "plugin.h"
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +000061#include "misc.h"
Michiel Van Der Kolk10b20692005-07-01 18:01:23 +000062#include "database.h"
Michiel Van Der Kolk451dd482005-03-28 00:00:24 +000063
Henrik Backec1a951f2005-03-28 13:00:22 +000064#if (CONFIG_HWCODEC == MASNONE)
Miika Pekkarinen20b38972005-07-13 12:48:22 +000065#include "pcmbuf.h"
Daniel Stenberg771ed792005-07-14 21:32:56 +000066#else
67#define pcmbuf_init()
Henrik Backec1a951f2005-03-28 13:00:22 +000068#endif
Linus Nielsen Feltzing52d5b302005-07-09 08:05:04 +000069#if defined(IRIVER_H100_SERIES) && !defined(SIMULATOR)
Andye5d08722005-06-19 03:05:53 +000070#include "pcm_record.h"
71#endif
Michiel Van Der Kolk451dd482005-03-28 00:00:24 +000072
Jörg Hohensohn4a5df8e2004-10-15 22:10:55 +000073#ifdef CONFIG_TUNER
Jörg Hohensohn8f8fbac2004-10-15 21:41:46 +000074#include "radio.h"
Jörg Hohensohn4a5df8e2004-10-15 22:10:55 +000075#endif
Jörg Hohensohnda848572004-12-28 22:16:07 +000076#ifdef HAVE_MMC
77#include "ata_mmc.h"
78#endif
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +000079
Christian Gmeiner7c1879b2005-04-15 12:08:49 +000080#ifdef HAVE_REMOTE_LCD
81#include "lcd-remote.h"
82#endif
83
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +000084/*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
Björn Stenberg18239b82002-08-12 12:44:18 +000085
Jens Arnoldabe77a12004-08-01 21:50:57 +000086const char appsversion[]=APPSVERSION;
Daniel Stenberg7b719112002-05-29 10:05:27 +000087
Felix Arendse096c862002-06-12 15:31:35 +000088void init(void);
Björn Stenbergad9829a2002-06-10 12:42:23 +000089
Björn Stenberg55c7da22002-05-21 14:28:10 +000090void app_main(void)
91{
Björn Stenbergad9829a2002-06-10 12:42:23 +000092 init();
Björn Stenberg55c7da22002-05-21 14:28:10 +000093 browse_root();
94}
95
Björn Stenbergad9829a2002-06-10 12:42:23 +000096#ifdef SIMULATOR
97
Felix Arendse096c862002-06-12 15:31:35 +000098void init(void)
Björn Stenbergad9829a2002-06-10 12:42:23 +000099{
100 init_threads();
Linus Nielsen Feltzing20d031f2003-05-09 16:01:21 +0000101 buffer_init();
Björn Stenbergad9829a2002-06-10 12:42:23 +0000102 lcd_init();
Jens Arnold3a5bd7a2005-07-07 21:03:58 +0000103#ifdef HAVE_REMOTE_LCD
104 lcd_remote_init();
105#endif
Kjell Ericsonfd9f8642002-10-28 19:52:22 +0000106 font_init();
Björn Stenbergad9829a2002-06-10 12:42:23 +0000107 show_logo();
Jens Arnold0f040292005-01-19 21:43:15 +0000108 lang_init();
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000109 audio_init();
Björn Stenberg6b25f792002-07-15 11:02:12 +0000110 settings_reset();
Jens Arnoldc12e87d2004-09-28 18:09:10 +0000111 settings_calc_config_sector();
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000112 settings_load(SETTINGS_ALL);
113 settings_apply();
Björn Stenbergad9829a2002-06-10 12:42:23 +0000114 sleep(HZ/2);
Daniel Stenbergb85f1032003-05-14 06:38:17 +0000115 tree_init();
Linus Nielsen Feltzingfe6cf2d2003-05-17 22:25:52 +0000116 playlist_init();
Jörg Hohensohnf9933652004-01-05 20:42:51 +0000117 mp3_init( global_settings.volume,
118 global_settings.bass,
119 global_settings.treble,
120 global_settings.balance,
121 global_settings.loudness,
Jörg Hohensohnf9933652004-01-05 20:42:51 +0000122 global_settings.avc,
Linus Nielsen Feltzing7bade1a2004-07-06 12:17:14 +0000123 global_settings.channel_config,
Jens Arnold76b257f2005-01-12 00:24:15 +0000124 global_settings.stereo_width,
Linus Nielsen Feltzing7bade1a2004-07-06 12:17:14 +0000125 global_settings.mdb_strength,
126 global_settings.mdb_harmonics,
127 global_settings.mdb_center,
128 global_settings.mdb_shape,
129 global_settings.mdb_enable,
130 global_settings.superbass);
Jens Arnold3c348df2005-01-26 22:48:25 +0000131 button_clear_queue(); /* Empty the keyboard buffer */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000132#if CONFIG_HWCODEC == MASNONE
133 talk_init();
134#endif
Björn Stenbergad9829a2002-06-10 12:42:23 +0000135}
136
137#else
Björn Stenberg88f7fb02002-05-24 15:40:14 +0000138
Daniel Stenberg6682c032002-06-12 18:00:32 +0000139void init(void)
Björn Stenberge324db62002-05-17 19:50:37 +0000140{
Jörg Hohensohn7ee03d02004-12-29 22:33:32 +0000141 int rc;
Jens Arnold27abc2a2005-05-17 22:10:51 +0000142 bool mounted = false;
Daniel Stenberg8ef88a12005-02-03 08:36:43 +0000143#if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
Jörg Hohensohn5dd17b12003-07-17 20:29:51 +0000144 /* if nobody initialized ATA before, I consider this a cold start */
145 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
Daniel Stenberg8ef88a12005-02-03 08:36:43 +0000146#endif
Linus Nielsen Feltzing0f607752002-05-29 09:14:38 +0000147 system_init();
Linus Nielsen Feltzing8c00b4c2002-06-10 14:44:15 +0000148 kernel_init();
Linus Nielsen Feltzing31e20972002-06-20 08:59:54 +0000149
Linus Nielsen Feltzinge7b993f2005-03-18 11:38:15 +0000150#ifdef HAVE_ADJUSTABLE_CPU_FREQ
151 set_cpu_frequency(CPUFREQ_NORMAL);
152#endif
153
Linus Nielsen Feltzing20d031f2003-05-09 16:01:21 +0000154 buffer_init();
155
Björn Stenberg6b25f792002-07-15 11:02:12 +0000156 settings_reset();
Linus Nielsen Feltzing0f607752002-05-29 09:14:38 +0000157
Björn Stenberg4afa7092002-05-30 12:17:18 +0000158 lcd_init();
Jens Arnold3a5bd7a2005-07-07 21:03:58 +0000159#ifdef HAVE_REMOTE_LCD
Christian Gmeiner7c1879b2005-04-15 12:08:49 +0000160 lcd_remote_init();
161#endif
Björn Stenbergbed3d3f2002-09-20 08:07:51 +0000162 font_init();
Daniel Stenberg75e09a82002-06-11 08:24:33 +0000163 show_logo();
Jens Arnold0f040292005-01-19 21:43:15 +0000164 lang_init();
Daniel Stenberg75e09a82002-06-11 08:24:33 +0000165
Björn Stenbergc4d8d972003-02-14 09:44:34 +0000166 set_irq_level(0);
Björn Stenbergf43b27a2002-05-27 09:53:28 +0000167#ifdef DEBUG
Björn Stenberge324db62002-05-17 19:50:37 +0000168 debug_init();
Björn Stenberg18239b82002-08-12 12:44:18 +0000169#else
Jens Arnold2d875f82004-09-20 22:15:35 +0000170#ifndef HAVE_MMC /* FIXME: This is also necessary for debug builds
171 * (do debug builds on the Ondio make sense?) */
Björn Stenberg18239b82002-08-12 12:44:18 +0000172 serial_setup();
Björn Stenbergf43b27a2002-05-27 09:53:28 +0000173#endif
Jens Arnold2d875f82004-09-20 22:15:35 +0000174#endif
Björn Stenberge324db62002-05-17 19:50:37 +0000175
Linus Nielsen Feltzing6d2e4922002-07-02 21:40:12 +0000176 i2c_init();
Linus Nielsen Feltzing76759e02002-08-06 08:14:08 +0000177
178#ifdef HAVE_RTC
179 rtc_init();
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000180 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
Linus Nielsen Feltzing76759e02002-08-06 08:14:08 +0000181#endif
182
Linus Nielsen Feltzing7d3c31b2002-06-30 20:25:37 +0000183 adc_init();
184
Linus Nielsen Feltzing6c77cc32002-06-30 13:24:29 +0000185 usb_init();
186
Linus Nielsen Feltzing9b3a3be2002-06-24 13:51:40 +0000187 backlight_init();
188
Björn Stenberge324db62002-05-17 19:50:37 +0000189 button_init();
Linus Nielsen Feltzing4b545592002-07-02 17:59:10 +0000190
Linus Nielsen Feltzing53ee3442003-08-25 08:57:49 +0000191 powermgmt_init();
Jörg Hohensohn020352c2003-07-18 21:58:28 +0000192
Jörg Hohensohnef8d5082004-10-19 08:20:38 +0000193#ifdef CONFIG_TUNER
194 radio_init();
195#endif
196
Daniel Stenberg8ef88a12005-02-03 08:36:43 +0000197#if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
Jens Arnold7197edd2004-12-16 22:31:26 +0000198 if (coldstart && charger_inserted()
199 && !global_settings.car_adapter_mode
200#ifdef ATA_POWER_PLAYERSTYLE
201 && !ide_powered() /* relies on probing result from bootloader */
202#endif
203 )
Jörg Hohensohn5dd17b12003-07-17 20:29:51 +0000204 {
205 rc = charging_screen(); /* display a "charging" screen */
Jens Arnold7c7dd432005-07-11 19:14:26 +0000206 if (rc == 1) /* charger removed */
Jörg Hohensohn5dd17b12003-07-17 20:29:51 +0000207 power_off();
208 /* "On" pressed or USB connected: proceed */
Jens Arnold67b17e12004-11-21 09:29:51 +0000209 show_logo(); /* again, to provide better visual feedback */
Jörg Hohensohn5dd17b12003-07-17 20:29:51 +0000210 }
Daniel Stenberg8e43ccb2003-12-12 13:34:27 +0000211#endif
Jörg Hohensohnc47f8c02003-07-03 22:13:39 +0000212
Linus Nielsen Feltzing4b545592002-07-02 17:59:10 +0000213 rc = ata_init();
Linus Nielsen Feltzing71ef31a2002-07-02 18:01:31 +0000214 if(rc)
Linus Nielsen Feltzing4b545592002-07-02 17:59:10 +0000215 {
Björn Stenbergb47e64e2002-08-08 20:44:25 +0000216#ifdef HAVE_LCD_BITMAP
Linus Nielsen Feltzing4b545592002-07-02 17:59:10 +0000217 char str[32];
218 lcd_clear_display();
219 snprintf(str, 31, "ATA error: %d", rc);
220 lcd_puts(0, 1, str);
221 lcd_puts(0, 3, "Press ON to debug");
222 lcd_update();
Björn Stenberg6c33c512004-09-19 21:58:37 +0000223 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzing4b545592002-07-02 17:59:10 +0000224 dbg_ports();
225#endif
226 panicf("ata: %d", rc);
227 }
Jens Arnold2d875f82004-09-20 22:15:35 +0000228
Linus Nielsen Feltzinge85acf12004-05-21 21:49:04 +0000229 usb_start_monitoring();
Jens Arnold27abc2a2005-05-17 22:10:51 +0000230 while (usb_detect())
231 { /* enter USB mode early, before trying to mount */
232 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
233#ifdef HAVE_MMC
234 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
Linus Nielsen Feltzing15124aa2004-07-05 22:13:20 +0000235#endif
Jens Arnold27abc2a2005-05-17 22:10:51 +0000236 {
237 usb_screen();
238 mounted = true; /* mounting done @ end of USB mode */
239 }
Jens Arnold97a80492005-06-23 02:18:29 +0000240#ifdef HAVE_USB_POWER
241 if (usb_powered()) /* avoid deadlock */
242 break;
243#endif
Jens Arnold27abc2a2005-05-17 22:10:51 +0000244 }
245
246 if (!mounted)
247 {
248 rc = disk_mount_all();
249 if (rc<=0)
250 {
251 lcd_clear_display();
252 lcd_puts(0, 0, "No partition");
253 lcd_puts(0, 1, "found.");
254#ifdef HAVE_LCD_BITMAP
255 lcd_puts(0, 2, "Insert USB cable");
256 lcd_puts(0, 3, "and fix it.");
257 lcd_update();
258#endif
259 while(button_get(true) != SYS_USB_CONNECTED) {};
260 usb_screen();
261 system_reboot();
262 }
Linus Nielsen Feltzing15124aa2004-07-05 22:13:20 +0000263 }
Björn Stenbergde943a52002-07-15 15:06:45 +0000264
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000265 /* On software codec platforms we have to init audio before
266 calling audio_set_buffer_margin(). */
267#if (CONFIG_HWCODEC == MASNONE)
268 audio_init();
269#endif
Jens Arnoldc12e87d2004-09-28 18:09:10 +0000270 settings_calc_config_sector();
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000271 settings_load(SETTINGS_ALL);
272 settings_apply();
Jens Arnoldc12e87d2004-09-28 18:09:10 +0000273
Linus Nielsen Feltzing20d031f2003-05-09 16:01:21 +0000274 status_init();
Jens Arnold36056002004-09-29 21:41:10 +0000275 playlist_init();
Linus Nielsen Feltzing20d031f2003-05-09 16:01:21 +0000276 tree_init();
Michiel Van Der Kolkd6ab7232005-07-04 22:10:42 +0000277
Linus Nielsen Feltzing67bea322003-05-17 20:38:38 +0000278 /* No buffer allocation (see buffer.c) may take place after the call to
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000279 audio_init() since the mpeg thread takes the rest of the buffer space */
Jörg Hohensohnf9933652004-01-05 20:42:51 +0000280 mp3_init( global_settings.volume,
281 global_settings.bass,
282 global_settings.treble,
283 global_settings.balance,
284 global_settings.loudness,
Jörg Hohensohnf9933652004-01-05 20:42:51 +0000285 global_settings.avc,
Linus Nielsen Feltzing7bade1a2004-07-06 12:17:14 +0000286 global_settings.channel_config,
Jens Arnold76b257f2005-01-12 00:24:15 +0000287 global_settings.stereo_width,
Linus Nielsen Feltzing7bade1a2004-07-06 12:17:14 +0000288 global_settings.mdb_strength,
289 global_settings.mdb_harmonics,
290 global_settings.mdb_center,
291 global_settings.mdb_shape,
292 global_settings.mdb_enable,
293 global_settings.superbass);
Jens Arnold65144352005-06-20 10:04:16 +0000294#if (CONFIG_HWCODEC == MASNONE)
Jens Arnold65144352005-06-20 10:04:16 +0000295 sound_settings_apply();
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000296#else
297 audio_init();
Jens Arnold65144352005-06-20 10:04:16 +0000298#endif
Linus Nielsen Feltzing52d5b302005-07-09 08:05:04 +0000299#if defined(IRIVER_H100_SERIES) && !defined(SIMULATOR)
Jens Arnold65144352005-06-20 10:04:16 +0000300 pcm_init_recording();
301#endif
Jörg Hohensohn4f36ea82004-03-14 21:33:53 +0000302 talk_init();
Michiel Van Der Kolkd6ab7232005-07-04 22:10:42 +0000303 /* runtime database has to be initialized after audio_init() */
304 rundb_init();
Jens Arnold3a5bd7a2005-07-07 21:03:58 +0000305
Linus Nielsen Feltzing67bea322003-05-17 20:38:38 +0000306
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000307#ifdef AUTOROCK
Jens Arnold5690f782005-06-04 23:15:52 +0000308 {
Jörg Hohensohnc47f8c02003-07-03 22:13:39 +0000309 int fd;
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000310 static const char filename[] = PLUGIN_DIR "/autostart.rock";
311
Jörg Hohensohnc47f8c02003-07-03 22:13:39 +0000312 fd = open(filename, O_RDONLY);
Jörg Hohensohn1b4e3092005-06-22 06:11:51 +0000313 if(fd >= 0) /* no complaint if it doesn't exist */
Jörg Hohensohnc47f8c02003-07-03 22:13:39 +0000314 {
315 close(fd);
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000316 plugin_load((char*)filename, NULL); /* start if it does */
Jörg Hohensohnc47f8c02003-07-03 22:13:39 +0000317 }
318 }
Jörg Hohensohn6ff6d212004-06-19 15:50:02 +0000319#endif /* #ifdef AUTOROCK */
Michiel Van Der Kolk451dd482005-03-28 00:00:24 +0000320
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000321#ifdef HAVE_CHARGING
322 car_adapter_mode_init();
323#endif
Björn Stenberge324db62002-05-17 19:50:37 +0000324}
325
Björn Stenberg82182852002-05-16 12:53:40 +0000326int main(void)
327{
Björn Stenberg55c7da22002-05-21 14:28:10 +0000328 app_main();
Björn Stenberge324db62002-05-17 19:50:37 +0000329
330 while(1) {
Jens Arnold5690f782005-06-04 23:15:52 +0000331#if CONFIG_LED == LED_REAL
Björn Stenberge324db62002-05-17 19:50:37 +0000332 led(true); sleep(HZ/10);
333 led(false); sleep(HZ/10);
Jens Arnold5690f782005-06-04 23:15:52 +0000334#endif
Björn Stenberge324db62002-05-17 19:50:37 +0000335 }
Björn Stenberg82182852002-05-16 12:53:40 +0000336 return 0;
337}
Björn Stenberg55c7da22002-05-21 14:28:10 +0000338#endif
Daniel Stenberg8ef88a12005-02-03 08:36:43 +0000339