Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id:$ |
| 9 | * |
| 10 | * Copyright (C) 2010 Marcin Bukat |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include "inttypes.h" |
| 26 | #include "string.h" |
| 27 | #include "cpu.h" |
| 28 | #include "system.h" |
| 29 | #include "lcd.h" |
| 30 | #include "kernel.h" |
| 31 | #include "thread.h" |
| 32 | #include "storage.h" |
| 33 | #include "usb.h" |
| 34 | #include "disk.h" |
| 35 | #include "font.h" |
| 36 | #include "adc.h" |
| 37 | #include "backlight.h" |
| 38 | #include "backlight-target.h" |
| 39 | #include "button.h" |
| 40 | #include "panic.h" |
| 41 | #include "power.h" |
| 42 | #include "powermgmt.h" |
| 43 | #include "file.h" |
| 44 | |
| 45 | #include "common.h" |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 46 | #include "version.h" |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 47 | |
| 48 | #include <stdarg.h> |
| 49 | |
| 50 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
| 51 | #define MAX_LOADSIZE (10*1024*1024) |
| 52 | |
| 53 | #define DRAM_START 0x31000000 |
| 54 | |
| 55 | #define BOOTMENU_TIMEOUT (10*HZ) |
| 56 | #define BOOTMENU_OPTIONS 3 |
| 57 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 58 | #define EVENT_NONE 0x00 |
| 59 | #define EVENT_ON 0x01 |
| 60 | #define EVENT_AC 0x02 |
| 61 | #define EVENT_USB 0x04 |
| 62 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 63 | /* From common.c */ |
| 64 | extern int line; |
| 65 | static const char *bootmenu_options[] = { |
| 66 | "Boot rockbox", |
| 67 | "Boot MPIO firmware", |
| 68 | "Shutdown" |
| 69 | }; |
| 70 | |
| 71 | enum option_t { |
| 72 | rockbox, |
| 73 | mpio_firmware, |
| 74 | shutdown |
| 75 | }; |
| 76 | |
| 77 | int usb_screen(void) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
| 81 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 82 | static inline bool _charger_inserted(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 83 | { |
| 84 | return (GPIO1_READ & (1<<14)) ? false : true; |
| 85 | } |
| 86 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 87 | static inline bool _battery_full(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 88 | { |
| 89 | return (GPIO_READ & (1<<30)) ? true : false; |
| 90 | } |
| 91 | |
| 92 | /* Reset the cookie for the crt0 crash check */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 93 | static inline void __reset_cookie(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 94 | { |
| 95 | asm(" move.l #0,%d0"); |
| 96 | asm(" move.l %d0,0x10017ffc"); |
| 97 | } |
| 98 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 99 | static void start_rockbox(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 100 | { |
| 101 | adc_close(); |
| 102 | asm(" move.w #0x2700,%sr"); |
| 103 | __reset_cookie(); |
| 104 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
| 105 | asm(" movec.l %d0,%vbr"); |
| 106 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 107 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
| 108 | asm(" jmp (%a0)"); |
| 109 | } |
| 110 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 111 | static void start_mpio_firmware(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 112 | { |
| 113 | asm(" move.w #0x2700,%sr"); |
| 114 | __reset_cookie(); |
| 115 | asm(" movec.l %d0,%vbr"); |
| 116 | asm(" move.l 0,%sp"); |
| 117 | asm(" jmp 8"); |
| 118 | } |
| 119 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 120 | static void __shutdown(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 121 | { |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 122 | if (_charger_inserted()) |
| 123 | /* if AC power do nothing */ |
| 124 | return; |
| 125 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 126 | /* We need to gracefully spin down the disk to prevent clicks. */ |
| 127 | if (ide_powered()) |
| 128 | { |
| 129 | /* Make sure ATA has been initialized. */ |
| 130 | storage_init(); |
| 131 | |
| 132 | /* And put the disk into sleep immediately. */ |
| 133 | storage_sleepnow(); |
| 134 | } |
| 135 | |
| 136 | /* Backlight OFF */ |
| 137 | _backlight_off(); |
| 138 | __reset_cookie(); |
| 139 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 140 | power_off(); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* Print the battery voltage (and a warning message). */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 144 | static void check_battery(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 145 | { |
| 146 | |
| 147 | int battery_voltage, batt_int, batt_frac; |
| 148 | |
| 149 | battery_voltage = battery_adc_voltage(); |
| 150 | batt_int = battery_voltage / 1000; |
| 151 | batt_frac = (battery_voltage % 1000) / 10; |
| 152 | |
| 153 | printf("Battery: %d.%02dV", batt_int, batt_frac); |
| 154 | |
| 155 | if (battery_voltage <= 3500) |
| 156 | { |
| 157 | printf("WARNING! BATTERY LOW!!"); |
| 158 | sleep(HZ*2); |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 164 | static void lcd_putstring_centered(const char *string) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 165 | { |
| 166 | int w,h; |
| 167 | font_getstringsize(string, &w, &h, FONT_SYSFIXED); |
| 168 | lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, string); |
| 169 | } |
| 170 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 171 | static void rb_boot(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 172 | { |
| 173 | int rc; |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 174 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 175 | /* boost to speedup rb image loading */ |
| 176 | cpu_boost(true); |
| 177 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 178 | rc = storage_init(); |
| 179 | if(rc) |
| 180 | { |
| 181 | printf("ATA error: %d", rc); |
| 182 | sleep(HZ*5); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | disk_init(); |
| 187 | |
| 188 | rc = disk_mount_all(); |
| 189 | if (rc<=0) |
| 190 | { |
| 191 | printf("No partition found"); |
| 192 | sleep(HZ*5); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | printf("Loading firmware"); |
| 197 | |
| 198 | rc = load_firmware((unsigned char *)DRAM_START, |
| 199 | BOOTFILE, MAX_LOADSIZE); |
| 200 | |
| 201 | if (rc < EOK) |
| 202 | { |
| 203 | printf("Error!"); |
| 204 | printf("Can't load " BOOTFILE ": "); |
| 205 | printf("Result: %s", strerror(rc)); |
| 206 | sleep(HZ*5); |
| 207 | return; |
| 208 | } |
| 209 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 210 | cpu_boost(false); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 211 | start_rockbox(); |
| 212 | } |
| 213 | |
| 214 | static void bootmenu(void) |
| 215 | { |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 216 | enum option_t i; |
| 217 | enum option_t option = rockbox; |
| 218 | int button; |
| 219 | const char select[] = "->"; |
| 220 | long start_tick = current_tick; |
| 221 | |
| 222 | /* backbone of menu */ |
| 223 | /* run the loader */ |
| 224 | printf("Rockbox boot loader"); |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 225 | printf("Ver: " RBVERSION); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 226 | |
| 227 | check_battery(); |
| 228 | |
| 229 | printf(""); |
| 230 | printf("========================="); |
| 231 | |
| 232 | line += BOOTMENU_OPTIONS+2; /* skip lines */ |
| 233 | |
| 234 | printf("========================="); |
| 235 | printf(""); |
| 236 | printf(" [FF] [PREV] to move "); |
| 237 | printf(" [PLAY] to confirm "); |
| 238 | |
| 239 | /* content of menu and keys handling */ |
| 240 | while (TIME_BEFORE(current_tick,start_tick + BOOTMENU_TIMEOUT)) |
| 241 | { |
| 242 | /* Draw the menu. */ |
| 243 | line = 6; /* move below header */ |
| 244 | |
| 245 | for (i=0;i<BOOTMENU_OPTIONS;i++) |
| 246 | { |
| 247 | if (i != option) |
| 248 | printf(" %s",bootmenu_options[i]); |
| 249 | else |
| 250 | printf("%s %s",select,bootmenu_options[i]); |
| 251 | } |
| 252 | |
| 253 | line = 15; |
| 254 | |
| 255 | printf("Time left: %ds",(BOOTMENU_TIMEOUT - |
| 256 | (current_tick - start_tick))/HZ); |
| 257 | |
| 258 | lcd_update(); |
| 259 | |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 260 | button = BUTTON_NONE; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 261 | button = button_get_w_tmo(HZ); |
| 262 | |
| 263 | switch (button) |
| 264 | { |
| 265 | case BUTTON_PREV: |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 266 | case BUTTON_RC_PREV: |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 267 | if (option > rockbox) |
| 268 | option--; |
| 269 | else |
| 270 | option = shutdown; |
| 271 | break; |
| 272 | |
| 273 | case BUTTON_NEXT: |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 274 | case BUTTON_RC_NEXT: |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 275 | if (option < shutdown) |
| 276 | option++; |
| 277 | else |
| 278 | option = rockbox; |
| 279 | break; |
| 280 | |
| 281 | case BUTTON_PLAY: |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 282 | case BUTTON_RC_PLAY: |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 283 | case (BUTTON_PLAY|BUTTON_REC): |
| 284 | reset_screen(); |
| 285 | |
| 286 | switch (option) |
| 287 | { |
| 288 | case rockbox: |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 289 | rb_boot(); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 290 | break; |
| 291 | |
| 292 | case mpio_firmware: |
| 293 | start_mpio_firmware(); |
| 294 | break; |
| 295 | |
| 296 | default: |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 297 | return; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 298 | break; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | /* timeout */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void main(void) |
| 306 | { |
| 307 | /* messages */ |
| 308 | const char usb_connect_msg[] = "Bootloader USB mode"; |
| 309 | const char charging_msg[] = "Charging..."; |
| 310 | const char complete_msg[] = "Charging complete"; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 311 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 312 | /* helper variable for messages */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 313 | bool blink_toggle = false; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 314 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 315 | int button; |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 316 | unsigned int event = EVENT_NONE; |
| 317 | unsigned int last_event = EVENT_NONE; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 318 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 319 | /* this is default mode after power_init() */ |
| 320 | bool high_current_charging = true; |
| 321 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 322 | power_init(); |
| 323 | |
| 324 | system_init(); |
| 325 | kernel_init(); |
| 326 | |
| 327 | set_cpu_frequency(CPUFREQ_NORMAL); |
| 328 | coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS); |
| 329 | |
| 330 | enable_irq(); |
| 331 | lcd_init(); |
| 332 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 333 | /* only lowlevel functions no queue init */ |
| 334 | _backlight_init(); |
| 335 | _backlight_hw_on(); |
| 336 | |
| 337 | /* setup font system*/ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 338 | font_init(); |
| 339 | lcd_setfont(FONT_SYSFIXED); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 340 | |
| 341 | /* buttons reading */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 342 | adc_init(); |
| 343 | button_init(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 344 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 345 | usb_init(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 346 | cpu_idle_mode(true); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 347 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 348 | /* Handle wakeup event. Possibilities are: |
| 349 | * ON button (PLAY) |
| 350 | * USB insert |
| 351 | * AC charger plug |
| 352 | */ |
| 353 | |
| 354 | while(1) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 355 | { |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 356 | /* read buttons */ |
| 357 | event = EVENT_NONE; |
| 358 | button = button_get_w_tmo(HZ); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 359 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 360 | if ( (button & BUTTON_PLAY) || (button & BUTTON_RC_PLAY) ) |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 361 | event |= EVENT_ON; |
| 362 | |
| 363 | if ( usb_detect() == USB_INSERTED ) |
| 364 | event |= EVENT_USB; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 365 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 366 | if ( _charger_inserted() ) |
| 367 | event |= EVENT_AC; |
| 368 | |
| 369 | reset_screen(); |
| 370 | switch (event) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 371 | { |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 372 | case EVENT_ON: |
| 373 | case (EVENT_ON | EVENT_AC): |
| 374 | /* hold is handled in button driver */ |
| 375 | cpu_idle_mode(false); |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 376 | ide_power_enable(true); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 377 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 378 | if (button == (BUTTON_PLAY|BUTTON_REC)) |
| 379 | bootmenu(); |
| 380 | else |
| 381 | rb_boot(); |
| 382 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 383 | break; |
| 384 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 385 | case EVENT_AC: |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 386 | /* AC plug in */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 387 | if (!(last_event & EVENT_AC)) |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 388 | { |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 389 | /* reset charging circuit */ |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 390 | and_l(~(1<<23), &GPIO_ENABLE); |
| 391 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 392 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 393 | /* USB unplug */ |
| 394 | if (last_event & EVENT_USB) |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 395 | { |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 396 | usb_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 397 | sleep(HZ); |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 398 | ide_power_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 399 | sleep(HZ); |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 400 | } |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 401 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 402 | if(!_battery_full()) |
| 403 | { |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 404 | if (blink_toggle) |
| 405 | lcd_putstring_centered(charging_msg); |
| 406 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 407 | blink_toggle = !blink_toggle; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 408 | } |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 409 | else /* end of charge condition */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 410 | { |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 411 | /* put LTC1733 into shutdown mode */ |
| 412 | or_l((1<<23), &GPIO_ENABLE); |
| 413 | |
| 414 | if (high_current_charging) |
| 415 | { |
| 416 | /* switch to low current mode */ |
| 417 | and_l(~(1<<15), &GPIO_OUT); |
| 418 | |
| 419 | /* reset charging circuit */ |
| 420 | and_l(~(1<<23), &GPIO_ENABLE); |
| 421 | |
| 422 | high_current_charging = false; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | lcd_putstring_centered(complete_msg); |
| 427 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 428 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 429 | check_battery(); |
| 430 | break; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 431 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 432 | case EVENT_USB: |
| 433 | case (EVENT_USB | EVENT_AC): |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 434 | /* AC plug in while in USB mode */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 435 | if (!(last_event & EVENT_AC)) |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 436 | { |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 437 | /* reset charger circuit */ |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 438 | and_l(~(1<<23), &GPIO_ENABLE); |
| 439 | } |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 440 | |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 441 | /* USB plug in */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 442 | if (!(last_event & EVENT_USB)) |
| 443 | { |
| 444 | /* init USB */ |
| 445 | ide_power_enable(true); |
| 446 | sleep(HZ/20); |
| 447 | usb_enable(true); |
| 448 | } |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 449 | |
| 450 | /* display blinking USB indicator */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 451 | line = 0; |
| 452 | |
| 453 | if (blink_toggle) |
| 454 | lcd_putstring_centered(usb_connect_msg); |
| 455 | |
| 456 | check_battery(); |
| 457 | blink_toggle = !blink_toggle; |
| 458 | storage_spin(); |
| 459 | break; |
| 460 | |
| 461 | default: |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 462 | /* USB unplug */ |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 463 | if (last_event & EVENT_USB) |
| 464 | { |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 465 | /* disable USB */ |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 466 | usb_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 467 | sleep(HZ); |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 468 | ide_power_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 469 | sleep(HZ); |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 472 | /* spurious wakeup ?*/ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 473 | __shutdown(); |
| 474 | break; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 475 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 476 | lcd_update(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 477 | last_event = event; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* These functions are present in the firmware library, but we reimplement |
| 483 | them here because the originals do a lot more than we want */ |
| 484 | void screen_dump(void) |
| 485 | { |
| 486 | } |