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