bullshit commit, sync for travel (to zn00H!) :)
[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 /* includes */
9 #include "betty.h"
10
11 /*
12  * global variables
13  */
14
15 const char announce[]="betty - live from flash at 0x80000000! ;)\r\n";
16 const char d1_txt[]="betty";
17 const char d2_txt[]="- alphablend -";
18 u8 contrast;
19 t_button button;
20 int sample_count;
21
22 /*
23  * functions
24  */
25
26 #define cc1100_init     spi1_init(8,SPI_MASTER,8)
27
28 void display_string_page(u8 x,u8 p,u8 *s,u8 f,u8 o,u8 sp) {
29
30         while(*(s)) {
31                 if(x==DISPLAY_DIMX) {
32                         p+=1;
33                         x=o;
34                 }
35                 display_font_page(x,p,(u8 *)default_font+(*(s)<<3),f);
36                 x+=sp;
37                 s++;
38         }
39 }
40
41 void audio_mute_toggle(void) {
42
43         if(IOPIN0&(1<<11))
44                 IOCLR0=(1<<11);
45         else
46                 IOSET0=(1<<11);
47 }
48
49 #define AUDIO_TONE_C    (7500000/(262*256))
50 #define AUDIO_TONE_D    (7500000/(294*256))
51 #define AUDIO_TONE_E    (7500000/(330*256))
52 #define AUDIO_TONE_F    (7500000/(349*256))
53 #define AUDIO_TONE_G    (7500000/(392*256))
54 //#define AUDIO_TONE_A  (7500000/(440*256))
55 #define AUDIO_TONE_A    120
56 #define AUDIO_TONE_H    (7500000/(494*256))
57
58 void __attribute__ ((interrupt("IRQ"))) set_sample(void) {
59
60         //uart0_send_byte('S');
61         pwm_update_match(PWM_CHAN2,PWM_SINGLE_EDGE,sinus[sample_count++],0);
62         if(sample_count==0xff)
63                 sample_count=0;
64         interrupt_ack();
65         interrupt_tc_ir_set(INTERRUPT_TC0,INTERRUPT_MATCH0);
66 }
67
68 void __attribute__ ((interrupt("IRQ"))) input_event(void) {
69
70         u8 noret,tries;
71
72         /* button scan */
73         if(button_get_event(&button)) {
74                 switch(button.key[0]) {
75                         case BUTTON_POWER:
76                                 display_logo((u8 *)default_logo);
77                                 break;
78                         case BUTTON_DOWN:
79                                 if(contrast>0x00)
80                                         contrast-=1;
81                                 DISPLAY_SET_CONTRAST(contrast);
82                                 break;
83                         case BUTTON_UP:
84                                 if(contrast<0x3f)
85                                         contrast+=1;
86                                 DISPLAY_SET_CONTRAST(contrast);
87                                 break;
88                         case BUTTON_A:
89                                 display_fill_screen(DISPLAY_FILL_W);
90                                 break;
91                         case BUTTON_B:
92                                 display_fill_screen(DISPLAY_FILL_LG);
93                                 break;
94                         case BUTTON_C:
95                                 display_fill_screen(DISPLAY_FILL_DG);
96                                 break;
97                         case BUTTON_D:
98                                 display_fill_screen(DISPLAY_FILL_B);
99                                 break;
100                         case BUTTON_1:
101                                 display_rectangle_page(4,2,60,5,
102                                                        DISPLAY_FILL_B,0xff);
103                                 break;
104                         case BUTTON_2:
105                                 display_rectangle_page(40,5,50,5,
106                                                        DISPLAY_FILL_LG,0x7f);
107                                 break;
108                         case BUTTON_3:
109                                 break;
110                         case BUTTON_4:
111                                 display_font_page(42,18,
112                                                   (u8 *)default_font+(0x34<<3),
113                                                   DISPLAY_FILL_B);
114                                 break;
115                         case BUTTON_5:
116                                 display_font_page(50,18,
117                                                   (u8 *)default_font+(0x35<<3),
118                                                   DISPLAY_FILL_DG);
119                                 break;
120                         case BUTTON_6:
121                                 display_font_page(58,18,
122                                                   (u8 *)default_font+(0x36<<3),
123                                                   DISPLAY_FILL_W);
124                                 break;
125                         case BUTTON_7:
126                                 break;
127                         case BUTTON_8:
128                                 break;
129                         case BUTTON_BETTY:
130                                 display_string_page(50,13,(u8 *)d1_txt,
131                                                     DISPLAY_FILL_B,0,8);
132                                 break;
133                         case BUTTON_EXIT:
134                                 display_string_page(10,8,(u8 *)d2_txt,
135                                                     DISPLAY_FILL_B,0,8);
136                                 break;
137                         case BUTTON_MUTE:
138                                 audio_mute_toggle();
139                                 break;
140                         default:
141                                 display_clear_screen();
142                                 break;
143                 }
144         }
145
146         /* resume the interrupt at button release */
147         noret=1;
148         while(noret) {
149                 tries=0xff;
150                 noret=1;
151                 while(tries--) {
152                         noret=IOPIN0&(1<<30)?0:1;
153                         if(noret)
154                                 break;
155                 }
156         }
157         interrupt_ack();
158         interrupt_ext_ir_set(INTERRUPT_EXT3);
159 }
160
161 /*
162  * main function
163  */
164
165 int main() {
166
167         /* variables */
168
169         /* init */
170         sample_count=0;
171
172         /* system init */
173         pll_init();
174         pin_init();
175         ext_mem_init();
176
177         /* memory mapping - dirty! */
178         if((void *)announce<(void *)FLASH_BANK0)
179                 mmap_init(MEMTYPE_RAM);
180         else
181                 mmap_init(MEMTYPE_EXT);
182         
183         /* uart init */
184         uart0_init();
185
186         /* display init */
187         display_bl_init();
188
189         /* button init */
190         button.mode=BUTTON_INT;
191         button_init(&button);
192         button_set_retries(&button,30);
193
194         /* flash init */
195         flash_init();
196
197         /* display init */
198         display_init();
199         contrast=0x38;
200
201         /* interrupt init */
202         interrupt_init();
203
204         // ext interrupt
205         interrupt_ext_ir_config(INTERRUPT_EXT3,INTERRUPT_EXT_NOWAKE,
206                                 INTERRUPT_EXT_MODE_EDGE,
207                                 INTERRUPT_EXT_POLAR_LOW);
208         interrupt_ext_ir_set(INTERRUPT_EXT3);
209         interrupt_enable(INTERRUPT_EINT3,INTERRUPT_MODE_VIRQ,
210                          0,(u32)&input_event);
211
212         // timer interrupt
213         interrupt_tc_config(INTERRUPT_TC0,INTERRUPT_TC_MODE_T,0,0);
214         interrupt_tc_match_config(INTERRUPT_TC0,INTERRUPT_M0,AUDIO_TONE_A,
215                                   INTERRUPT_TC_MATCH_IR|
216                                   INTERRUPT_TC_MATCH_RESET);
217         interrupt_tc_ir_set(INTERRUPT_TC0,INTERRUPT_MATCH0);
218         interrupt_enable(INTERRUPT_TIMER0,INTERRUPT_MODE_VIRQ,
219                          1,(u32)&set_sample);
220         interrupt_tc_enable(INTERRUPT_TC0);
221
222         /* pwm init */
223         pwm_reset;
224         pwm_set_rate_and_prescaler(0xff,0);
225         pwm_config(PWM_CHAN2,PWM_SINGLE_EDGE,0,0);
226         pwm_match_ctrl_config(PWM_CHAN2,PWM_MATCH_RESET);
227         pwm_enable;
228         
229
230         /*
231          * start it ...
232          */
233
234         /* toggle the display back light */
235         display_bl_toggle();
236
237         /* announce */
238         uart0_send_string((char *)announce);
239
240         
241         while(1) {
242                 pause(0xffffff);
243                 uart0_send_string((char *)announce);
244         }
245
246         return 0;
247 }