bullshit commit, sync for travel (to zn00H!) :)
[my-code/arm.git] / betty / betty.c
index 81b4687..3a3e7a5 100644 (file)
  *
  */
 
-/*
- * includes
- */
-
-#include "lpc2xxx.h"
+/* includes */
+#include "betty.h"
 
 /*
- * defines
+ * global variables
  */
 
-/* bank 0/2 and boootloader addr/size */
-#define BANK0                  0x80000000
-#define BANK1                  0x81000000
-#define BANK2                  0x82000000
-#define BANK_SIZE              0x00100000
-#define BOOTLOADER             0x7fffe000
-#define BL_SIZE                        0x00002000
-
-/* flash cmd addresses - flash[0-18] <--> arm[1-19]*/
-#define B0F555 (*((volatile unsigned short *)(BANK0|0xaaa)))   // 0x555
-#define B0F2AA (*((volatile unsigned short *)(BANK0|0x554)))   // 0x2aa
-#define B0F    (*((volatile unsigned short *)(BANK0)))
-#define B2F555 (*((volatile unsigned short *)(BANK2|0xaaa)))   // 0x555
-#define B2F2AA (*((volatile unsigned short *)(BANK2|0x554)))   // 0x2aa
-#define B2F    (*((volatile unsigned short *)(BANK2)))
-
-/* lcd command and data addresses */
-#define LCD_CMD                (*((volatile unsigned char *)(BANK1)))
-#define LCD_DATA       (*((volatile unsigned char *)(BANK1+1)))
+const char announce[]="betty - live from flash at 0x80000000! ;)\r\n";
+const char d1_txt[]="betty";
+const char d2_txt[]="- alphablend -";
+u8 contrast;
+t_button button;
+int sample_count;
 
 /*
- * type definitions
+ * functions
  */
 
-typedef unsigned char u8;
-typedef unsigned short u16;
-typedef unsigned int u32;
-
- /*
-  * functions
-  */
-
-void mmap_init(u8 memtype) {
-
-       MEMMAP=memtype;
-}
-
-void pll_init(void) {
-
-       /* configuration */
-       PLLCFG=0x42;    // multiplier = 3 (for cclk), dividor = 4 (for f_cco)
-       PLLCON=0x03;    // enable and set as clk source for the lpc
-       /* feed sequence */
-       PLLFEED=0xaa;
-       PLLFEED=0x55;
-       /* wait for lock */
-       while(!(PLLSTAT&(1<<10)))
-               continue;
-}
-
-void ext_mem_bank_init(void) {
-
-       BCFG0=0x10000420;       // flash 1
-       BCFG1=0x00000c42;       // lcd
-       BCFG2=0x10000420;       // flash 2
-}
-
-
-void pin_select_init() {
-
-       /*
-        * a[19:2] -> address lines
-        */
-
-       PINSEL2=0x0d6041d4;
-}
-
-void uart0_init(void) {
-
-       PINSEL0=0x05;                   // pin select -> tx, rx
-       UART0_FCR=0x07;                 // enable fifo
-       UART0_LCR=0x83;                 // set dlab + word length
-       UART0_DLL=0x04;                 // br: 38400 @ 10/4 mhz
-       UART0_DLM=0x00;
-       UART0_LCR=0x03;                 // unset dlab
-}
+#define cc1100_init    spi1_init(8,SPI_MASTER,8)
 
-void uart0_send_string(char *txbuf) {
+void display_string_page(u8 x,u8 p,u8 *s,u8 f,u8 o,u8 sp) {
 
-       int i;
-
-       i=0;
-
-       while(txbuf[i]) {
-               UART0_THR=txbuf[i++];
-               /* flush if tx buffer maximum reached */
-               if(!(i%16))
-                       while(!(UART0_LSR&(1<<6)))
-                               continue;
-       }
-       
-       /* flush if \n and \r do not fit in the tx buffer */
-       if(i>14)
-               while(!(UART0_LSR&(1<<6)))
-                       continue;
-
-       UART0_THR='\n';
-       UART0_THR='\r';
-
-       /* flush uart0 anyways */
-       while(!(UART0_LSR&(1<<6)))
-               continue;
-}
-
-void uart0_send_buf16(u16 *buf,int len) {
-
-       int i;
-
-       i=0;
-
-       for(i=0;i<len/2;i++) {
-               if(!(i%8))
-                       while(!(UART0_LSR&(1<<6)))
-                               continue;
-               UART0_THR=buf[i]&0xff;
-               UART0_THR=(buf[i]>>8)&0xff;
+       while(*(s)) {
+               if(x==DISPLAY_DIMX) {
+                       p+=1;
+                       x=o;
+               }
+               display_font_page(x,p,(u8 *)default_font+(*(s)<<3),f);
+               x+=sp;
+               s++;
        }
 }
 
-void uart0_send_buf32(u32 *buf,int len) {
+void audio_mute_toggle(void) {
 
-       int i;
-
-       i=0;
-
-       for(i=0;i<len/4;i++) {
-               if(!(i%4))
-                       while(!(UART0_LSR&(1<<6)))
-                               continue;
-               UART0_THR=buf[i]&0xff;
-               UART0_THR=(buf[i]>>8)&0xff;
-               UART0_THR=(buf[i]>>16)&0xff;
-               UART0_THR=(buf[i]>>24)&0xff;
+       if(IOPIN0&(1<<11))
+               IOCLR0=(1<<11);
+       else
+               IOSET0=(1<<11);
+}
+
+#define AUDIO_TONE_C   (7500000/(262*256))
+#define AUDIO_TONE_D   (7500000/(294*256))
+#define AUDIO_TONE_E   (7500000/(330*256))
+#define AUDIO_TONE_F   (7500000/(349*256))
+#define AUDIO_TONE_G   (7500000/(392*256))
+//#define AUDIO_TONE_A (7500000/(440*256))
+#define AUDIO_TONE_A   120
+#define AUDIO_TONE_H   (7500000/(494*256))
+
+void __attribute__ ((interrupt("IRQ"))) set_sample(void) {
+
+       //uart0_send_byte('S');
+       pwm_update_match(PWM_CHAN2,PWM_SINGLE_EDGE,sinus[sample_count++],0);
+       if(sample_count==0xff)
+               sample_count=0;
+       interrupt_ack();
+       interrupt_tc_ir_set(INTERRUPT_TC0,INTERRUPT_MATCH0);
+}
+
+void __attribute__ ((interrupt("IRQ"))) input_event(void) {
+
+       u8 noret,tries;
+
+       /* button scan */
+       if(button_get_event(&button)) {
+               switch(button.key[0]) {
+                       case BUTTON_POWER:
+                               display_logo((u8 *)default_logo);
+                               break;
+                       case BUTTON_DOWN:
+                               if(contrast>0x00)
+                                       contrast-=1;
+                               DISPLAY_SET_CONTRAST(contrast);
+                               break;
+                       case BUTTON_UP:
+                               if(contrast<0x3f)
+                                       contrast+=1;
+                               DISPLAY_SET_CONTRAST(contrast);
+                               break;
+                       case BUTTON_A:
+                               display_fill_screen(DISPLAY_FILL_W);
+                               break;
+                       case BUTTON_B:
+                               display_fill_screen(DISPLAY_FILL_LG);
+                               break;
+                       case BUTTON_C:
+                               display_fill_screen(DISPLAY_FILL_DG);
+                               break;
+                       case BUTTON_D:
+                               display_fill_screen(DISPLAY_FILL_B);
+                               break;
+                       case BUTTON_1:
+                               display_rectangle_page(4,2,60,5,
+                                                      DISPLAY_FILL_B,0xff);
+                               break;
+                       case BUTTON_2:
+                               display_rectangle_page(40,5,50,5,
+                                                      DISPLAY_FILL_LG,0x7f);
+                               break;
+                       case BUTTON_3:
+                               break;
+                       case BUTTON_4:
+                               display_font_page(42,18,
+                                                 (u8 *)default_font+(0x34<<3),
+                                                 DISPLAY_FILL_B);
+                               break;
+                       case BUTTON_5:
+                               display_font_page(50,18,
+                                                 (u8 *)default_font+(0x35<<3),
+                                                 DISPLAY_FILL_DG);
+                               break;
+                       case BUTTON_6:
+                               display_font_page(58,18,
+                                                 (u8 *)default_font+(0x36<<3),
+                                                 DISPLAY_FILL_W);
+                               break;
+                       case BUTTON_7:
+                               break;
+                       case BUTTON_8:
+                               break;
+                       case BUTTON_BETTY:
+                               display_string_page(50,13,(u8 *)d1_txt,
+                                                   DISPLAY_FILL_B,0,8);
+                               break;
+                       case BUTTON_EXIT:
+                               display_string_page(10,8,(u8 *)d2_txt,
+                                                   DISPLAY_FILL_B,0,8);
+                               break;
+                       case BUTTON_MUTE:
+                               audio_mute_toggle();
+                               break;
+                       default:
+                               display_clear_screen();
+                               break;
+               }
        }
-}
-
-void uart0_send_byte(u8 send) {
-
-       while(!(UART0_LSR&(1<<5)))
-               continue;
-
-       UART0_THR=send;
-}
-
-u8 uart0_get_byte(void) {
-
-       u8 rx;
 
-       while(!(UART0_LSR&(1<<0)))
-               continue;
-
-       rx=UART0_RBR;
-
-       return rx;
-}
-
-void pause(int cnt) {
-
-       while(cnt--)
-               asm volatile ("nop");
-}
-       
-void init_lcd(u8 s) {
-
-       LCD_CMD=0xe1;   // ?
-       LCD_CMD=0xe2;
-       pause(48000);
-       LCD_CMD=0xab;
-       LCD_CMD=0x27;
-       LCD_CMD=0x81;
-       LCD_CMD=0x3f;
-       LCD_CMD=0x65;
-       LCD_CMD=0x60;
-       LCD_CMD=0x1c;
-       LCD_CMD=0x61;
-       LCD_CMD=0x0a;
-       LCD_CMD=0x62;
-       LCD_CMD=0x75;
-       LCD_CMD=0x63;
-       LCD_CMD=0x81;
-       LCD_CMD=0x90;
-       LCD_CMD=0x88;
-       LCD_CMD=0x00;
-       LCD_CMD=0x89;
-       LCD_CMD=0x00;
-       LCD_CMD=0x8a;
-       LCD_CMD=0x33;
-       LCD_CMD=0x8b;
-       LCD_CMD=0x33;
-       LCD_CMD=0x8c;
-       LCD_CMD=0x66;
-       LCD_CMD=0x8d;
-       LCD_CMD=0x66;
-       LCD_CMD=0x8e;
-       LCD_CMD=0x99;
-       LCD_CMD=0x8f;
-       LCD_CMD=0x99;
-       if(s) {
-               LCD_CMD=0xa1;
-               LCD_CMD=0xc0;
-       }
-       else {
-               LCD_CMD=0xa0;
-               LCD_CMD=0xc8;
+       /* resume the interrupt at button release */
+       noret=1;
+       while(noret) {
+               tries=0xff;
+               noret=1;
+               while(tries--) {
+                       noret=IOPIN0&(1<<30)?0:1;
+                       if(noret)
+                               break;
+               }
        }
-       LCD_CMD=0x2e;
-       pause(48000);
-       LCD_CMD=0x2f;
-       LCD_CMD=0xa4;
-       LCD_CMD=0xa6;
+       interrupt_ack();
+       interrupt_ext_ir_set(INTERRUPT_EXT3);
 }
 
 /*
- * spi0 stuff (+ cc1100)
+ * main function
  */
 
-#define SLAVE  0
-#define MASTER 1
-
-void spi1_init(u8 width,u8 type,u8 div) {
-
-       if((width<8)|(width>16))
-               width=8;
-       if(width==16)
-               width=0;
-
-       div&=0xfe;
-       if(div<8)
-               div=8;
-
-       S1SPCR=(1<<2)|(width<<8)|(type<<5);
-       S1SPCCR=div;
-}
-
-#define cc1100_init    spi1_init(8,MASTER,8)
-
-void spi1_send(u16 data) {
-
-       S1SPDR=data;
+int main() {
 
-       while(!(S1SPSR&(1<<7)))
-               continue;
-}
+       /* variables */
 
-void bl_init(void) {
+       /* init */
+       sample_count=0;
 
-       IODIR0|=(1<<4);
-}
-
-void bl_toggle(void) {
+       /* system init */
+       pll_init();
+       pin_init();
+       ext_mem_init();
 
-       if(IOPIN0&(1<<4))
-               IOCLR0=(1<<4);
+       /* memory mapping - dirty! */
+       if((void *)announce<(void *)FLASH_BANK0)
+               mmap_init(MEMTYPE_RAM);
        else
-               IOSET0=(1<<4);
-}
-
-void bl_on(void) {
-
-       IOCLR0=(1<<4);
-}
-
-void bl_off(void) {
-
-       IOSET0=(1<<4);
-}
-
-/*
- * main function
- */
+               mmap_init(MEMTYPE_EXT);
+       
+       /* uart init */
+       uart0_init();
 
-int main() {
+       /* display init */
+       display_bl_init();
+
+       /* button init */
+       button.mode=BUTTON_INT;
+       button_init(&button);
+       button_set_retries(&button,30);
+
+       /* flash init */
+       flash_init();
+
+       /* display init */
+       display_init();
+       contrast=0x38;
+
+       /* interrupt init */
+       interrupt_init();
+
+       // ext interrupt
+       interrupt_ext_ir_config(INTERRUPT_EXT3,INTERRUPT_EXT_NOWAKE,
+                               INTERRUPT_EXT_MODE_EDGE,
+                               INTERRUPT_EXT_POLAR_LOW);
+       interrupt_ext_ir_set(INTERRUPT_EXT3);
+       interrupt_enable(INTERRUPT_EINT3,INTERRUPT_MODE_VIRQ,
+                        0,(u32)&input_event);
+
+       // timer interrupt
+       interrupt_tc_config(INTERRUPT_TC0,INTERRUPT_TC_MODE_T,0,0);
+       interrupt_tc_match_config(INTERRUPT_TC0,INTERRUPT_M0,AUDIO_TONE_A,
+                                 INTERRUPT_TC_MATCH_IR|
+                                 INTERRUPT_TC_MATCH_RESET);
+       interrupt_tc_ir_set(INTERRUPT_TC0,INTERRUPT_MATCH0);
+       interrupt_enable(INTERRUPT_TIMER0,INTERRUPT_MODE_VIRQ,
+                        1,(u32)&set_sample);
+       interrupt_tc_enable(INTERRUPT_TC0);
+
+       /* pwm init */
+       pwm_reset;
+       pwm_set_rate_and_prescaler(0xff,0);
+       pwm_config(PWM_CHAN2,PWM_SINGLE_EDGE,0,0);
+       pwm_match_ctrl_config(PWM_CHAN2,PWM_MATCH_RESET);
+       pwm_enable;
+       
 
-       char buf[]="betty - live from the flash at 0x80000000! ;)\r\n";
+       /*
+        * start it ...
+        */
 
-       pll_init();
-       uart0_init();
-       ext_mem_bank_init();
-       pin_select_init();
-       init_lcd(0);
-       bl_init();
+       /* toggle the display back light */
+       display_bl_toggle();
 
-       pause(0xffffff);
+       /* announce */
+       uart0_send_string((char *)announce);
 
+       
        while(1) {
-               uart0_send_string(buf);
-               bl_toggle();
-               pause(0x9ffff);
+               pause(0xffffff);
+               uart0_send_string((char *)announce);
        }
 
        return 0;
 }
-