flashing works, sample betty fw announcing itself :)
[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 BANK2                   0x82000000
21 #define BANK_SIZE               0x00100000
22 #define BOOTLOADER              0x7fffe000
23 #define BL_SIZE                 0x00002000
24
25 /* flash cmd addresses - flash[0-18] <--> arm[1-19]*/
26 #define B0F555  (*((volatile unsigned short *)(BANK0|0xaaa)))   // 0x555
27 #define B0F2AA  (*((volatile unsigned short *)(BANK0|0x554)))   // 0x2aa
28 #define B0F     (*((volatile unsigned short *)(BANK0)))
29 #define B2F555  (*((volatile unsigned short *)(BANK2|0xaaa)))   // 0x555
30 #define B2F2AA  (*((volatile unsigned short *)(BANK2|0x554)))   // 0x2aa
31 #define B2F     (*((volatile unsigned short *)(BANK2)))
32
33 /*
34  * type definitions
35  */
36
37 typedef unsigned char u8;
38 typedef unsigned short u16;
39 typedef unsigned int u32;
40
41  /*
42   * functions
43   */
44
45 void mmap_init(u8 memtype) {
46
47         MEMMAP=memtype;
48 }
49
50 void pll_init(void) {
51
52         /* configuration */
53         PLLCFG=0x42;    // multiplier = 3 (for cclk), dividor = 4 (for f_cco)
54         PLLCON=0x03;    // enable and set as clk source for the lpc
55         /* feed sequence */
56         PLLFEED=0xaa;
57         PLLFEED=0x55;
58         /* wait for lock */
59         while(!(PLLSTAT&(1<<10)))
60                 continue;
61 }
62
63 void ext_mem_bank_init(void) {
64
65         BCFG0=0x10000420;       // flash 1
66         BCFG1=0x00000c42;       // lcd
67         BCFG2=0x10000420;       // flash 2
68 }
69
70
71 void pin_select_init() {
72
73         /*
74          * a[19:2] -> address lines
75          */
76
77         PINSEL2=0x0d6041d4;
78 }
79
80 void uart0_init(void) {
81
82         PINSEL0=0x05;                   // pin select -> tx, rx
83         UART0_FCR=0x07;                 // enable fifo
84         UART0_LCR=0x83;                 // set dlab + word length
85         UART0_DLL=0x04;                 // br: 38400 @ 10/4 mhz
86         UART0_DLM=0x00;
87         UART0_LCR=0x03;                 // unset dlab
88 }
89
90 void uart0_send_string(char *txbuf) {
91
92         int i;
93
94         i=0;
95
96         while(txbuf[i]) {
97                 UART0_THR=txbuf[i++];
98                 /* flush if tx buffer maximum reached */
99                 if(!(i%16))
100                         while(!(UART0_LSR&(1<<6)))
101                                 continue;
102         }
103         
104         /* flush if \n and \r do not fit in the tx buffer */
105         if(i>14)
106                 while(!(UART0_LSR&(1<<6)))
107                         continue;
108
109         UART0_THR='\n';
110         UART0_THR='\r';
111
112         /* flush uart0 anyways */
113         while(!(UART0_LSR&(1<<6)))
114                 continue;
115 }
116
117 void uart0_send_buf16(u16 *buf,int len) {
118
119         int i;
120
121         i=0;
122
123         for(i=0;i<len/2;i++) {
124                 if(!(i%8))
125                         while(!(UART0_LSR&(1<<6)))
126                                 continue;
127                 UART0_THR=buf[i]&0xff;
128                 UART0_THR=(buf[i]>>8)&0xff;
129         }
130 }
131
132 void uart0_send_buf32(u32 *buf,int len) {
133
134         int i;
135
136         i=0;
137
138         for(i=0;i<len/4;i++) {
139                 if(!(i%4))
140                         while(!(UART0_LSR&(1<<6)))
141                                 continue;
142                 UART0_THR=buf[i]&0xff;
143                 UART0_THR=(buf[i]>>8)&0xff;
144                 UART0_THR=(buf[i]>>16)&0xff;
145                 UART0_THR=(buf[i]>>24)&0xff;
146         }
147 }
148
149 void uart0_send_byte(u8 send) {
150
151         while(!(UART0_LSR&(1<<5)))
152                 continue;
153
154         UART0_THR=send;
155 }
156
157 u8 uart0_get_byte(void) {
158
159         u8 rx;
160
161         while(!(UART0_LSR&(1<<0)))
162                 continue;
163
164         rx=UART0_RBR;
165
166         return rx;
167 }
168
169 /*
170  * main function
171  */
172
173 int main() {
174
175         char buf[]="betty - live from the flash at 0x80000000! ;)\r\n";
176         u8 byte;
177
178         pll_init();
179         uart0_init();
180         ext_mem_bank_init();
181         pin_select_init();
182
183         while(1) {
184                 byte=uart0_get_byte();
185                 uart0_send_string(buf);
186                 uart0_send_byte(byte);
187         }
188
189         return 0;
190 }
191