Make sure to create the logger first. Fixes a segfault due to a race with info downlo...
[Rockbox.git] / bootloader / telechips.c
blobe1b80f9be6762e37512e65dec5c6d6b9b06c3c31
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Dave Chapman
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
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"
35 #include "button.h"
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"
44 #if defined(COWON_D2)
45 #include "i2c.h"
46 #endif
48 char version[] = APPSVERSION;
50 extern int line;
52 void* main(void)
54 int button;
55 int power_count = 0;
56 int count = 0;
57 bool do_power_off = false;
59 #if defined(COWON_D2)
60 int i,rc,fd,len;
61 int* buf = (int*)0x21000000; /* Unused DRAM */
62 #endif
64 power_init();
65 lcd_init();
66 system_init();
68 #if defined(COWON_D2)
69 kernel_init();
70 #endif
72 adc_init();
73 button_init();
74 backlight_init();
76 font_init();
78 lcd_setfont(FONT_SYSFIXED);
80 #if defined(COWON_D2)
81 lcd_enable(true);
82 #endif
84 _backlight_on();
86 #if defined(COWON_D2)
87 printf("ATA");
88 rc = ata_init();
89 if(rc)
91 reset_screen();
92 error(EATA, rc);
95 printf("mount");
96 rc = disk_mount_all();
97 if (rc<=0)
99 error(EDISK,rc);
102 #if 0
103 printf("opening test file...");
105 fd = open("/test.bin", O_RDONLY);
106 if (fd < 0) panicf("could not open test file");
108 len = filesize(fd);
109 printf("Length: %x", len);
111 lseek(fd, 0, SEEK_SET);
112 read(fd, buf, len);
113 close(fd);
115 printf("testing contents...");
117 i = 0;
118 while (buf[i] == i && i<(len/4)) { i++; }
120 if (i < len/4)
122 printf("mismatch at %x [0x%x]", i, buf[i]);
124 else
126 printf("passed!");
128 while (!button_read_device()) {};
129 while (button_read_device()) {};
130 #endif
131 #endif
133 while(!do_power_off) {
134 line = 0;
135 printf("Hello World!");
137 button = button_read_device();
139 /* Power-off if POWER button has been held for a long time
140 This loop is currently running at about 100 iterations/second
142 if (button & POWEROFF_BUTTON) {
143 power_count++;
144 if (power_count > 200)
145 do_power_off = true;
146 } else {
147 power_count = 0;
150 printf("Btn: 0x%08x",button);
152 #if defined(COWON_D2)
153 printf("GPIOA: 0x%08x",GPIOA);
154 printf("GPIOB: 0x%08x",GPIOB);
155 printf("GPIOC: 0x%08x",GPIOC);
156 printf("GPIOD: 0x%08x",GPIOD);
157 printf("GPIOE: 0x%08x",GPIOE);
159 for (i = 0; i<4; i++)
161 printf("ADC%d: 0x%04x",i,adc_read(i));
164 /* TODO: Move this stuff out to a touchscreen driver and establish
165 how such a beast is going to work. Since it needs I2C read/write,
166 it can't easily go on an interrupt-based tick task. */
168 unsigned char buf[] = { 0x2f, (0xE<<1) | 1, /* ADC start for X+Y */
169 0, 0, 0 };
170 int x,y;
171 i2c_write(0x10, buf, 2);
172 i2c_readmem(0x10, 0x2e, buf, 5);
173 x = (buf[2] << 2) | (buf[3] & 3);
174 y = (buf[4] << 2) | ((buf[3] & 0xC) >> 2);
175 printf("X: 0x%03x Y: 0x%03x",x,y);
177 x = (x*LCD_WIDTH) / 1024;
178 y = (y*LCD_HEIGHT) / 1024;
179 lcd_hline(x-5, x+5, y);
180 lcd_vline(x, y-5, y+5);
182 buf[0] = 0x2f;
183 buf[1] = (0xF<<1) | 1; /* ADC start for P1+P2 */
184 i2c_write(0x10, buf, 2);
185 i2c_readmem(0x10, 0x2e, buf, 5);
186 x = (buf[2] << 2) | (buf[3] & 3);
187 y = (buf[4] << 2) | ((buf[3] & 0xC) >> 2);
188 printf("P1: 0x%03x P2: 0x%03x",x,y);
190 #endif
192 count++;
193 printf("Count: %d",count);
196 lcd_clear_display();
197 line = 0;
198 printf("POWER-OFF");
200 /* Power-off */
201 power_off();
203 return 0;
206 /* These functions are present in the firmware library, but we reimplement
207 them here because the originals do a lot more than we want */
208 void usb_acknowledge(void)
212 void usb_wait_for_disconnect(void)