Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 by Dave Chapman |
| 11 | * |
| 12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing |
| 13 | * |
| 14 | * All files in this archive are subject to the GNU General Public License. |
| 15 | * See the file COPYING in the source tree root for full license agreement. |
| 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 <string.h> |
| 26 | #include "cpu.h" |
| 27 | #include "system.h" |
| 28 | #include "lcd.h" |
| 29 | #include "kernel.h" |
| 30 | #include "thread.h" |
| 31 | #include "ata.h" |
| 32 | #include "fat.h" |
| 33 | #include "disk.h" |
| 34 | #include "font.h" |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 35 | #include "button.h" |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 36 | #include "adc.h" |
| 37 | #include "adc-target.h" |
| 38 | #include "backlight-target.h" |
| 39 | #include "panic.h" |
| 40 | #include "power.h" |
| 41 | #include "file.h" |
| 42 | #include "common.h" |
| 43 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 44 | #if defined(COWON_D2) |
| 45 | #include "i2c.h" |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 46 | #define LOAD_ADDRESS 0x20000000 /* DRAM_START */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 49 | char version[] = APPSVERSION; |
| 50 | |
| 51 | extern int line; |
| 52 | |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 53 | #define MAX_LOAD_SIZE (8*1024*1024) /* Arbitrary, but plenty. */ |
| 54 | |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 55 | void* main(void) |
| 56 | { |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 57 | int button; |
| 58 | int power_count = 0; |
| 59 | int count = 0; |
| 60 | bool do_power_off = false; |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 61 | |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 62 | #if defined(COWON_D2) && defined(TCCBOOT) |
| 63 | int rc; |
| 64 | unsigned char* loadbuffer = (unsigned char*)LOAD_ADDRESS; |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 65 | #endif |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 66 | |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 67 | power_init(); |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 68 | system_init(); |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 69 | lcd_init(); |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 70 | |
| 71 | #if defined(COWON_D2) |
| 72 | kernel_init(); |
| 73 | #endif |
| 74 | |
| 75 | adc_init(); |
| 76 | button_init(); |
| 77 | backlight_init(); |
| 78 | |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 79 | font_init(); |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 80 | lcd_setfont(FONT_SYSFIXED); |
| 81 | |
Jens Arnold | ef12b3b | 2007-11-12 18:49:53 +0000 | [diff] [blame] | 82 | _backlight_on(); |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 83 | |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 84 | /* Only load the firmware if TCCBOOT is defined - this ensures SDRAM_START is |
| 85 | available for loading the firmware. Otherwise display the debug screen. */ |
| 86 | #if defined(COWON_D2) && defined(TCCBOOT) |
| 87 | printf("Rockbox boot loader"); |
| 88 | printf("Version %s", version); |
| 89 | |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 90 | printf("ATA"); |
| 91 | rc = ata_init(); |
| 92 | if(rc) |
| 93 | { |
| 94 | reset_screen(); |
| 95 | error(EATA, rc); |
| 96 | } |
| 97 | |
| 98 | printf("mount"); |
| 99 | rc = disk_mount_all(); |
| 100 | if (rc<=0) |
| 101 | { |
| 102 | error(EDISK,rc); |
| 103 | } |
| 104 | |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 105 | rc = load_firmware(loadbuffer, BOOTFILE, MAX_LOAD_SIZE); |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 106 | |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 107 | if (rc < 0) |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 108 | { |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 109 | error(EBOOTFILE,rc); |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 110 | } |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 111 | else if (rc == EOK) |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 112 | { |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 113 | int(*kernel_entry)(void); |
| 114 | |
| 115 | kernel_entry = (void*) loadbuffer; |
| 116 | rc = kernel_entry(); |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 117 | } |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 118 | #else |
Rob Purchase | 5991478 | 2008-01-25 21:37:59 +0000 | [diff] [blame] | 119 | |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 120 | while(!do_power_off) { |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 121 | line = 0; |
| 122 | printf("Hello World!"); |
| 123 | |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 124 | button = button_read_device(); |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 125 | |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 126 | /* Power-off if POWER button has been held for a long time |
| 127 | This loop is currently running at about 100 iterations/second |
| 128 | */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 129 | if (button & POWEROFF_BUTTON) { |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 130 | power_count++; |
| 131 | if (power_count > 200) |
| 132 | do_power_off = true; |
| 133 | } else { |
| 134 | power_count = 0; |
| 135 | } |
| 136 | |
| 137 | printf("Btn: 0x%08x",button); |
| 138 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 139 | #if defined(COWON_D2) |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 140 | int i; |
| 141 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 142 | printf("GPIOA: 0x%08x",GPIOA); |
| 143 | printf("GPIOB: 0x%08x",GPIOB); |
| 144 | printf("GPIOC: 0x%08x",GPIOC); |
| 145 | printf("GPIOD: 0x%08x",GPIOD); |
| 146 | printf("GPIOE: 0x%08x",GPIOE); |
| 147 | |
| 148 | for (i = 0; i<4; i++) |
| 149 | { |
| 150 | printf("ADC%d: 0x%04x",i,adc_read(i)); |
| 151 | } |
| 152 | |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 153 | /* TODO: Move this stuff out to a touchscreen driver and establish |
| 154 | how such a beast is going to work. Since it needs I2C read/write, |
| 155 | it can't easily go on an interrupt-based tick task. */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 156 | { |
| 157 | unsigned char buf[] = { 0x2f, (0xE<<1) | 1, /* ADC start for X+Y */ |
| 158 | 0, 0, 0 }; |
| 159 | int x,y; |
| 160 | i2c_write(0x10, buf, 2); |
| 161 | i2c_readmem(0x10, 0x2e, buf, 5); |
| 162 | x = (buf[2] << 2) | (buf[3] & 3); |
| 163 | y = (buf[4] << 2) | ((buf[3] & 0xC) >> 2); |
| 164 | printf("X: 0x%03x Y: 0x%03x",x,y); |
| 165 | |
Rob Purchase | addb522 | 2008-02-13 23:50:44 +0000 | [diff] [blame] | 166 | x = (x*LCD_WIDTH) / 1024; |
| 167 | y = (y*LCD_HEIGHT) / 1024; |
| 168 | lcd_hline(x-5, x+5, y); |
| 169 | lcd_vline(x, y-5, y+5); |
| 170 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 171 | buf[0] = 0x2f; |
| 172 | buf[1] = (0xF<<1) | 1; /* ADC start for P1+P2 */ |
| 173 | i2c_write(0x10, buf, 2); |
| 174 | i2c_readmem(0x10, 0x2e, buf, 5); |
| 175 | x = (buf[2] << 2) | (buf[3] & 3); |
| 176 | y = (buf[4] << 2) | ((buf[3] & 0xC) >> 2); |
| 177 | printf("P1: 0x%03x P2: 0x%03x",x,y); |
| 178 | } |
| 179 | #endif |
| 180 | |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 181 | count++; |
| 182 | printf("Count: %d",count); |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 185 | lcd_clear_display(); |
| 186 | line = 0; |
| 187 | printf("POWER-OFF"); |
| 188 | |
Christian Gmeiner | 868fcce | 2008-02-16 12:44:15 +0000 | [diff] [blame] | 189 | /* Power-off */ |
| 190 | power_off(); |
Dave Chapman | a4d48d0 | 2007-11-01 23:38:57 +0000 | [diff] [blame] | 191 | |
Rob Purchase | c883611 | 2008-03-12 20:57:19 +0000 | [diff] [blame] | 192 | printf("(NOT) POWERED OFF"); |
| 193 | #endif |
| 194 | |
Dave Chapman | 28f6ae4 | 2007-10-28 11:08:10 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* These functions are present in the firmware library, but we reimplement |
| 199 | them here because the originals do a lot more than we want */ |
| 200 | void usb_acknowledge(void) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | void usb_wait_for_disconnect(void) |
| 205 | { |
| 206 | } |