bullshit commit, sync for travel (to zn00H!) :)
[my-code/arm.git] / betty / betty.c
index 2cd287f..3a3e7a5 100644 (file)
@@ -16,6 +16,8 @@ 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;
 
 /*
  * functions
@@ -36,9 +38,38 @@ void display_string_page(u8 x,u8 p,u8 *s,u8 f,u8 o,u8 sp) {
        }
 }
 
-void input_event(void) {
+void audio_mute_toggle(void) {
 
-       /* button test! */
+       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:
@@ -103,11 +134,28 @@ void input_event(void) {
                                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;
                }
        }
+
+       /* 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;
+               }
+       }
+       interrupt_ack();
+       interrupt_ext_ir_set(INTERRUPT_EXT3);
 }
 
 /*
@@ -117,7 +165,9 @@ void input_event(void) {
 int main() {
 
        /* variables */
-       t_button button;
+
+       /* init */
+       sample_count=0;
 
        /* system init */
        pll_init();
@@ -139,7 +189,7 @@ int main() {
        /* button init */
        button.mode=BUTTON_INT;
        button_init(&button);
-       button_set_retries(&button,0);
+       button_set_retries(&button,30);
 
        /* flash init */
        flash_init();
@@ -150,10 +200,32 @@ int main() {
 
        /* interrupt init */
        interrupt_init();
+
+       // ext interrupt
        interrupt_ext_ir_config(INTERRUPT_EXT3,INTERRUPT_EXT_NOWAKE,
                                INTERRUPT_EXT_MODE_EDGE,
                                INTERRUPT_EXT_POLAR_LOW);
-       interrupt_enable(INTERRUPT_EINT3,INTERRUPT_MODE_VIRQ,0,input_event);
+       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;
+       
 
        /*
         * start it ...
@@ -167,6 +239,8 @@ int main() {
 
        
        while(1) {
+               pause(0xffffff);
+               uart0_send_string((char *)announce);
        }
 
        return 0;