display backlight fun ..., display init (not working!), spi1 stuff for cc1100
[my-code/arm.git] / betty / betty.c
1 /*
2  * betty.c - alternative firmware for the betty tv ;)
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 /*
9  * includes
10  */
11
12 #include "lpc2xxx.h"
13
14 /*
15  * defines
16  */
17
18 /* bank 0/2 and boootloader addr/size */
19 #define BANK0                   0x80000000
20 #define BANK1                   0x81000000
21 #define BANK2                   0x82000000
22 #define BANK_SIZE               0x00100000
23 #define BOOTLOADER              0x7fffe000
24 #define BL_SIZE                 0x00002000
25
26 /* flash cmd addresses - flash[0-18] <--> arm[1-19]*/
27 #define B0F555  (*((volatile unsigned short *)(BANK0|0xaaa)))   // 0x555
28 #define B0F2AA  (*((volatile unsigned short *)(BANK0|0x554)))   // 0x2aa
29 #define B0F     (*((volatile unsigned short *)(BANK0)))
30 #define B2F555  (*((volatile unsigned short *)(BANK2|0xaaa)))   // 0x555
31 #define B2F2AA  (*((volatile unsigned short *)(BANK2|0x554)))   // 0x2aa
32 #define B2F     (*((volatile unsigned short *)(BANK2)))
33
34 /* lcd command and data addresses */
35 #define LCD_CMD         (*((volatile unsigned char *)(BANK1)))
36 #define LCD_DATA        (*((volatile unsigned char *)(BANK1+1)))
37
38 /*
39  * type definitions
40  */
41
42 typedef unsigned char u8;
43 typedef unsigned short u16;
44 typedef unsigned int u32;
45
46  /*
47   * functions
48   */
49
50 void mmap_init(u8 memtype) {
51
52         MEMMAP=memtype;
53 }
54
55 void pll_init(void) {
56
57         /* configuration */
58         PLLCFG=0x42;    // multiplier = 3 (for cclk), dividor = 4 (for f_cco)
59         PLLCON=0x03;    // enable and set as clk source for the lpc
60         /* feed sequence */
61         PLLFEED=0xaa;
62         PLLFEED=0x55;
63         /* wait for lock */
64         while(!(PLLSTAT&(1<<10)))
65                 continue;
66 }
67
68 void ext_mem_bank_init(void) {
69
70         BCFG0=0x10000420;       // flash 1
71         BCFG1=0x00000c42;       // lcd
72         BCFG2=0x10000420;       // flash 2
73 }
74
75
76 void pin_select_init() {
77
78         /*
79          * a[19:2] -> address lines
80          */
81
82         PINSEL2=0x0d6041d4;
83 }
84
85 void uart0_init(void) {
86
87         PINSEL0=0x05;                   // pin select -> tx, rx
88         UART0_FCR=0x07;                 // enable fifo
89         UART0_LCR=0x83;                 // set dlab + word length
90         UART0_DLL=0x04;                 // br: 38400 @ 10/4 mhz
91         UART0_DLM=0x00;
92         UART0_LCR=0x03;                 // unset dlab
93 }
94
95 void uart0_send_string(char *txbuf) {
96
97         int i;
98
99         i=0;
100
101         while(txbuf[i]) {
102                 UART0_THR=txbuf[i++];
103                 /* flush if tx buffer maximum reached */
104                 if(!(i%16))
105                         while(!(UART0_LSR&(1<<6)))
106                                 continue;
107         }
108         
109         /* flush if \n and \r do not fit in the tx buffer */
110         if(i>14)
111                 while(!(UART0_LSR&(1<<6)))
112                         continue;
113
114         UART0_THR='\n';
115         UART0_THR='\r';
116
117         /* flush uart0 anyways */
118         while(!(UART0_LSR&(1<<6)))
119                 continue;
120 }
121
122 void uart0_send_buf16(u16 *buf,int len) {
123
124         int i;
125
126         i=0;
127
128         for(i=0;i<len/2;i++) {
129                 if(!(i%8))
130                         while(!(UART0_LSR&(1<<6)))
131                                 continue;
132                 UART0_THR=buf[i]&0xff;
133                 UART0_THR=(buf[i]>>8)&0xff;
134         }
135 }
136
137 void uart0_send_buf32(u32 *buf,int len) {
138
139         int i;
140
141         i=0;
142
143         for(i=0;i<len/4;i++) {
144                 if(!(i%4))
145                         while(!(UART0_LSR&(1<<6)))
146                                 continue;
147                 UART0_THR=buf[i]&0xff;
148                 UART0_THR=(buf[i]>>8)&0xff;
149                 UART0_THR=(buf[i]>>16)&0xff;
150                 UART0_THR=(buf[i]>>24)&0xff;
151         }
152 }
153
154 void uart0_send_byte(u8 send) {
155
156         while(!(UART0_LSR&(1<<5)))
157                 continue;
158
159         UART0_THR=send;
160 }
161
162 u8 uart0_get_byte(void) {
163
164         u8 rx;
165
166         while(!(UART0_LSR&(1<<0)))
167                 continue;
168
169         rx=UART0_RBR;
170
171         return rx;
172 }
173
174 void pause(int cnt) {
175
176         while(cnt--)
177                 asm volatile ("nop");
178 }
179         
180 void init_lcd(u8 s) {
181
182         LCD_CMD=0xe1;   // ?
183         LCD_CMD=0xe2;
184         pause(48000);
185         LCD_CMD=0xab;
186         LCD_CMD=0x27;
187         LCD_CMD=0x81;
188         LCD_CMD=0x3f;
189         LCD_CMD=0x65;
190         LCD_CMD=0x60;
191         LCD_CMD=0x1c;
192         LCD_CMD=0x61;
193         LCD_CMD=0x0a;
194         LCD_CMD=0x62;
195         LCD_CMD=0x75;
196         LCD_CMD=0x63;
197         LCD_CMD=0x81;
198         LCD_CMD=0x90;
199         LCD_CMD=0x88;
200         LCD_CMD=0x00;
201         LCD_CMD=0x89;
202         LCD_CMD=0x00;
203         LCD_CMD=0x8a;
204         LCD_CMD=0x33;
205         LCD_CMD=0x8b;
206         LCD_CMD=0x33;
207         LCD_CMD=0x8c;
208         LCD_CMD=0x66;
209         LCD_CMD=0x8d;
210         LCD_CMD=0x66;
211         LCD_CMD=0x8e;
212         LCD_CMD=0x99;
213         LCD_CMD=0x8f;
214         LCD_CMD=0x99;
215         if(s) {
216                 LCD_CMD=0xa1;
217                 LCD_CMD=0xc0;
218         }
219         else {
220                 LCD_CMD=0xa0;
221                 LCD_CMD=0xc8;
222         }
223         LCD_CMD=0x2e;
224         pause(48000);
225         LCD_CMD=0x2f;
226         LCD_CMD=0xa4;
227         LCD_CMD=0xa6;
228 }
229
230 /*
231  * spi0 stuff (+ cc1100)
232  */
233
234 #define SLAVE   0
235 #define MASTER  1
236
237 void spi1_init(u8 width,u8 type,u8 div) {
238
239         if((width<8)|(width>16))
240                 width=8;
241         if(width==16)
242                 width=0;
243
244         div&=0xfe;
245         if(div<8)
246                 div=8;
247
248         S1SPCR=(1<<2)|(width<<8)|(type<<5);
249         S1SPCCR=div;
250 }
251
252 #define cc1100_init     spi1_init(8,MASTER,8)
253
254 void spi1_send(u16 data) {
255
256         S1SPDR=data;
257
258         while(!(S1SPSR&(1<<7)))
259                 continue;
260 }
261
262 void bl_init(void) {
263
264         IODIR0|=(1<<4);
265 }
266
267 void bl_toggle(void) {
268
269         if(IOPIN0&(1<<4))
270                 IOCLR0=(1<<4);
271         else
272                 IOSET0=(1<<4);
273 }
274
275 void bl_on(void) {
276
277         IOCLR0=(1<<4);
278 }
279
280 void bl_off(void) {
281
282         IOSET0=(1<<4);
283 }
284
285 /*
286  * main function
287  */
288
289 int main() {
290
291         char buf[]="betty - live from the flash at 0x80000000! ;)\r\n";
292
293         pll_init();
294         uart0_init();
295         ext_mem_bank_init();
296         pin_select_init();
297         init_lcd(0);
298         bl_init();
299
300         pause(0xffffff);
301
302         while(1) {
303                 uart0_send_string(buf);
304                 bl_toggle();
305                 pause(0x9ffff);
306         }
307
308         return 0;
309 }
310