Cap the level chooser to NUM_LEVELS (100) to avoid accessing level 101, which contain...
[kugel-rb/myfork.git] / bootloader / samsung_yps3.c
blob511b6208a44fdd976f438ec6a94daba16a9d62b0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Bertrik Sikken
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
27 #include "config.h"
29 #include "inttypes.h"
30 #include "cpu.h"
31 #include "system.h"
32 #include "lcd.h"
33 #include "kernel.h"
34 #include "thread.h"
35 #include "backlight.h"
36 #include "backlight-target.h"
37 #include "button.h"
38 #include "panic.h"
39 #include "power.h"
40 #include "common.h"
41 #include "usb.h"
42 #include "bitmaps/rockboxlogo.h"
44 #include "adc.h"
45 #include "adc-target.h"
47 #include "timer.h"
49 #include "i2c-s5l8700.h"
50 #include "dma-target.h"
51 #include "pcm.h"
52 #include "audiohw.h"
53 #include "rtc.h"
54 #include "tuner.h"
55 #include "si4700.h"
56 #include "fmradio_i2c.h"
58 char version[] = APPSVERSION;
59 #define LONG_DELAY 200000
60 #define SHORT_DELAY 50000
61 #define PAUSE_DELAY 50000
63 static inline void delay(unsigned int duration)
65 volatile unsigned int i;
66 for(i=0;i<duration;i++);
69 // forward declaration
70 static int rds_decode(int line, struct si4700_dbg_info *nfo);
72 void main(void)
74 char mystring[64];
75 int line, col;
76 unsigned char read_data[16];
77 int i;
78 struct si4700_dbg_info si4700_info;
79 // unsigned int data;
80 int brightness = 0;
81 unsigned int button;
83 // enable all peripherals
84 PWRCON = 0;
86 // disable all interrupts
87 INTMSK = 0;
89 // start with all GPIOs as input
90 PCON0 = 0;
91 PCON1 = 0;
92 PCON2 = 0;
93 PCON3 = 0;
94 PCON4 = 0;
95 PCON5 = 0;
96 PCON6 = 0;
97 PCON7 = 0;
99 system_init();
100 kernel_init();
102 asm volatile("msr cpsr_c, #0x13\n\t"); // enable interrupts
104 lcd_init();
106 lcd_clear_display();
107 lcd_bitmap(rockboxlogo, 0, 160, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
108 lcd_update();
110 power_init();
112 i2c_init();
113 fmradio_i2c_init();
114 adc_init();
115 _backlight_init();
117 // FM power
118 si4700_init();
119 tuner_power(true);
120 si4700_set(RADIO_SLEEP, 0);
121 si4700_set(RADIO_MUTE, 0);
122 si4700_set(RADIO_FREQUENCY, 100700000);
124 lcd_puts_scroll(0,0,"+++ this is a very very long line to test scrolling. ---");
126 button_init_device();
128 while (true)
130 line = 1;
132 #if 1 /* enable to see GPIOs */
133 snprintf(mystring, 64, "%02X %02X %02X %02X %02X %02X %02X %02X", PDAT0, PDAT1, PDAT2, PDAT3, PDAT4, PDAT5, PDAT6, PDAT7);
134 lcd_puts(0, line++, mystring);
135 #endif
137 #if 1 /* enable this to see info about the RTC */
138 rtc_read_datetime(read_data);
139 snprintf(mystring, 64, "RTC:");
140 for (i = 0; i < 7; i++) {
141 snprintf(mystring + 2 * i + 4, 64, "%02X", read_data[i]);
143 lcd_puts(0, line++, mystring);
144 #endif
146 #if 0 /* enable this so see info about the codec */
147 memset(read_data, 0, sizeof(read_data));
148 for (i = 0; i < 1; i++) {
149 i2c_read(0x34, i, 2, read_data);
150 data = read_data[0] << 8 | read_data[1];
151 snprintf(mystring + 4 * i, 64, "%04X", data);
153 lcd_puts(0, line++, mystring);
154 #endif
156 #if 1 /* enable this to see radio debug info */
157 si4700_dbg_info(&si4700_info);
158 col = snprintf(mystring, 64, "FM: ");
159 for (i = 0; i < 16; i++) {
160 col += snprintf(mystring + col, 64, "%04X ", si4700_info.regs[i]);
161 if (((i + 1) % 4) == 0) {
162 lcd_puts(0, line++, mystring);
163 col = 4;
166 line = rds_decode(line, &si4700_info);
167 #endif
169 #if 1 /* enable this to see ADC info */
170 snprintf(mystring, 64, "%04X %04X %04X %04X", adc_read(0), adc_read(1), adc_read(2), adc_read(3));
171 lcd_puts(0, line++, mystring);
172 snprintf(mystring, 64, "ADC:USB %4d mV BAT %4d mV",
173 (adc_read(0) * 6000) >> 10, (adc_read(2) * 4650) >> 10);
174 lcd_puts(0, line++, mystring);
175 #endif
177 #if 1 /* enable this so see USB info */
178 snprintf(mystring, 64, "CLK %08X CLK2 %08X", CLKCON, CLKCON2);
179 lcd_puts(0, line++, mystring);
181 snprintf(mystring, 64, "%04X %04X %04X %04X", PHYCTRL, PHYPWR, URSTCON, UCLKCON);
182 lcd_puts(0, line++, mystring);
184 snprintf(mystring, 64, "SCR %04X SSR %04X EIR %04X", SCR, SSR, EIR);
185 lcd_puts(0, line++, mystring);
187 snprintf(mystring, 64, "FAR %04X FNR %04X EP0 %04X", FAR, FNR, EP0SR);
188 lcd_puts(0, line++, mystring);
189 #endif
191 #if 1 /* button lights controlled by keypad */
192 button = button_read_device();
193 if (button & (BUTTON_UP | BUTTON_DOWN | BUTTON_LEFT | BUTTON_RIGHT)) {
194 PDAT3 |= (1 << 3);
196 else {
197 PDAT3 &= ~(1 << 3);
199 if (button & (BUTTON_BACK | BUTTON_MENU)) {
200 PDAT3 |= (1 << 2);
202 else {
203 PDAT3 &= ~(1 << 2);
205 if (button & (BUTTON_SELECT)) {
206 PDAT4 |= (1 << 2);
208 else {
209 PDAT4 &= ~(1 << 2);
211 #endif
213 #if 1 /* backlight brightness controlled by up/down keys */
214 if (button_read_device() & BUTTON_UP) {
215 if (brightness < MAX_BRIGHTNESS_SETTING) {
216 brightness++;
217 _backlight_set_brightness(brightness);
220 else if (button_read_device() & BUTTON_DOWN) {
221 if (brightness > MIN_BRIGHTNESS_SETTING) {
222 brightness--;
223 _backlight_set_brightness(brightness);
226 snprintf(mystring, 64, "bright %3d", brightness);
227 lcd_puts(0, line++, mystring);
228 #endif
231 #if 1 /* button info */
232 snprintf(mystring, 64, "BUTTONS %08X, %s", button_read_device(),
233 headphones_inserted() ? "HP" : "hp");
234 lcd_puts(0, line++, mystring);
235 #endif
237 lcd_update();
242 static int rds_decode(int line, struct si4700_dbg_info *nfo)
244 unsigned short rdsdata[4];
245 unsigned int pi, group, tp, pty, segment, abflag;
246 static unsigned int af1 = 0, af2 = 0;
247 static unsigned int day = 0, hour = 0, minute = 0;
248 static unsigned int abflag_prev = -1;
249 static char mystring[64];
251 /* big RDS arrays */
252 static char ps[9];
253 static char rt[65];
255 rdsdata[0] = nfo->regs[12];
256 rdsdata[1] = nfo->regs[13];
257 rdsdata[2] = nfo->regs[14];
258 rdsdata[3] = nfo->regs[15];
260 pi = rdsdata[0];
261 group = (rdsdata[1] >> 11) & 0x1F;
262 tp = (rdsdata[1] >> 10) & 1;
263 pty = (rdsdata[1] >> 5) & 0x1F;
265 switch (group) {
267 case 0: /* group 0A: basic info */
268 af1 = (rdsdata[2] >> 8) & 0xFF;
269 af2 = (rdsdata[2] >> 0) & 0xFF;
270 /* fall through */
271 case 1: /* group 0B: basic info */
272 segment = rdsdata[1] & 3;
273 ps[segment * 2 + 0] = (rdsdata[3] >> 8) & 0xFF;
274 ps[segment * 2 + 1] = (rdsdata[3] >> 0) & 0xFF;
275 break;
277 case 2: /* group 1A: programme item */
278 case 3: /* group 1B: programme item */
279 day = (rdsdata[3] >> 11) & 0x1F;
280 hour = (rdsdata[3] >> 6) & 0x1F;
281 minute = (rdsdata[3] >> 0) & 0x3F;
282 break;
284 case 4: /* group 2A: radio text */
285 segment = rdsdata[1] & 0xF;
286 abflag = (rdsdata[1] >> 4) & 1;
287 if (abflag != abflag_prev) {
288 memset(rt, '.', 64);
289 abflag_prev = abflag;
291 rt[segment * 4 + 0] = (rdsdata[2] >> 8) & 0xFF;
292 rt[segment * 4 + 1] = (rdsdata[2] >> 0) & 0xFF;
293 rt[segment * 4 + 2] = (rdsdata[3] >> 8) & 0xFF;
294 rt[segment * 4 + 3] = (rdsdata[3] >> 0) & 0xFF;
295 break;
297 case 5: /* group 2B: radio text */
298 segment = rdsdata[1] & 0xF;
299 abflag = (rdsdata[1] >> 4) & 1;
300 if (abflag != abflag_prev) {
301 memset(rt, '.', 64);
302 abflag_prev = abflag;
304 rt[segment * 2 + 0] = (rdsdata[3] >> 8) & 0xFF;
305 rt[segment * 2 + 1] = (rdsdata[3] >> 0) & 0xFF;
306 break;
308 default:
309 break;
312 snprintf(mystring, 64, "PI:%04X,TP:%d,PTY:%2d,AF:%02X/%02X", pi, tp, pty, af1, af2);
313 lcd_puts(0, line++, mystring);
314 snprintf(mystring, 64, "PS:%s,ITEM:%02d-%02d:%02d", ps, day, hour, minute);
315 lcd_puts(0, line++, mystring);
316 snprintf(mystring, 64, "RT:%s", rt);
317 lcd_puts(0, line++, mystring);
319 return line;