beep when GPS ready
[protos/xbee-avr.git] / main.c
diff --git a/main.c b/main.c
index c438575..aded312 100644 (file)
--- a/main.c
+++ b/main.c
@@ -27,6 +27,7 @@
 
 /* fuses:
  * avrdude -p atmega1284p -P usb -c avrispmkii -U lfuse:w:0xff:m -U hfuse:w:0x91:m -U efuse:w:0xff:m
+ * -> it failed but I answered y, then make reset and it was ok
  */
 
 #include <aversive.h>
 #include <parse.h>
 #include <rdline.h>
 #include <timer.h>
+#include <i2c.h>
 
+#include "../fpv-common/i2c_commands.h"
 #include "eeprom_config.h"
 #include "beep.h"
 #include "xbee_user.h"
+#include "i2c_protocol.h"
 #include "main.h"
 
 struct xbeeboard xbeeboard;
@@ -89,15 +93,21 @@ void bootloader(void)
 }
 
 /* return time in milliseconds on unsigned 16 bits */
-static uint16_t get_time_ms(void)
+uint16_t get_time_ms(void)
 {
-       return (uint16_t)global_ms;
+       uint16_t ms;
+       uint8_t flags;
+       IRQ_LOCK(flags);
+       ms = global_ms;
+       IRQ_UNLOCK(flags);
+       return ms;
 }
 
 static void main_timer_interrupt(void)
 {
        static uint16_t cycles;
-       static uint8_t cpt;
+       static uint8_t cpt = 0;
+       static uint8_t stack = 0;
 
        cpt++;
 
@@ -112,22 +122,30 @@ static void main_timer_interrupt(void)
        else
                BUZZER_OFF();
 
-       /* interrupt every 2048 cycles */
-       cycles += 2048;
+       if ((cpt & 0x03) != 0)
+               return;
+
+       /* the following code is only called one interrupt among 4: every 682us
+        * (at 12 Mhz) = 8192 cycles */
+       cycles += 8192;
        if (cycles >= 12000) {
                cycles -= 12000;
                global_ms ++;
        }
 
-       /* called every 682us (at 12 Mhz), but global_ms is not incremented at
-        * each call */
+       /* called  */
+       if (stack++ == 0)
+               LED2_ON();
        sei();
-       callout_manage(&xbeeboard.intr_cm);
+       if ((cpt & 0x3) == 0)
+               callout_manage(&xbeeboard.intr_cm);
+       cli();
+       if (--stack == 0)
+               LED2_OFF();
 }
 
 int main(void)
 {
-       //struct callout t1;
        FILE *xbee_file;
        int8_t err;
        struct xbee_dev dev;
@@ -143,9 +161,21 @@ int main(void)
        timer0_register_OV_intr(main_timer_interrupt);
 
        callout_mgr_init(&xbeeboard.intr_cm, get_time_ms);
-       callout_mgr_init(&xbeeboard.mainloop_cm, get_time_ms);
 
        cmdline_init();
+       /* LOGS */
+       error_register_emerg(mylog);
+       error_register_error(mylog);
+       error_register_warning(mylog);
+       error_register_notice(mylog);
+       error_register_debug(mylog);
+
+       /* I2C */
+       i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
+       i2c_protocol_init();
+       i2c_register_recv_event(i2c_recvevent);
+       i2c_register_send_event(i2c_sendevent);
+
        spi_servo_init();
        beep_init();
 
@@ -166,6 +196,11 @@ int main(void)
                fprintf(stderr, "cannot register default channel\n");
                return -1;
        }
+
+       xbeeapp_init();
+
+       rc_proto_init();
+
        sei();
 
        eeprom_load_config();