strat db
[aversive.git] / projects / microb2010 / ballboard / main.c
index 46a3b52..725a89d 100755 (executable)
@@ -1,7 +1,7 @@
-/*  
+/*
  *  Copyright Droids Corporation
  *  Olivier Matz <zer0@droids-corp.org>
  *  Copyright Droids Corporation
  *  Olivier Matz <zer0@droids-corp.org>
- * 
+ *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
 #include "actuator.h"
 #include "cs.h"
 #include "i2c_protocol.h"
 #include "actuator.h"
 #include "cs.h"
 #include "i2c_protocol.h"
-#include "beacon.h"
-#include "scanner.h"
+#include "state.h"
 
 /* 0 means "programmed"
  * ---- with 16 Mhz quartz
  * CKSEL 3-0 : 0111
 
 /* 0 means "programmed"
  * ---- with 16 Mhz quartz
  * CKSEL 3-0 : 0111
- * SUT 1-0 : 10 
+ * SUT 1-0 : 10
  * CKDIV8 : 1
  * ---- bootloader
  * BOOTZ 1-0 : 01 (4K bootloader)
  * CKDIV8 : 1
  * ---- bootloader
  * BOOTZ 1-0 : 01 (4K bootloader)
@@ -108,22 +107,27 @@ void bootloader(void)
        __asm__ __volatile__ ("ldi r31,0xf8\n");
        __asm__ __volatile__ ("ldi r30,0x00\n");
        __asm__ __volatile__ ("eijmp\n");
        __asm__ __volatile__ ("ldi r31,0xf8\n");
        __asm__ __volatile__ ("ldi r30,0x00\n");
        __asm__ __volatile__ ("eijmp\n");
-       
+
        /* never returns */
 }
 
 void do_led_blink(__attribute__((unused)) void *dummy)
 {
        /* never returns */
 }
 
 void do_led_blink(__attribute__((unused)) void *dummy)
 {
-#if 1 /* simple blink */
-       static uint8_t a=0;
-
-       if(a)
-               LED1_ON();
-       else
-               LED1_OFF();
-       
-       a = !a;
-#endif
+       static uint8_t a = 0;
+
+       if (ballboard.flags & DO_ERRBLOCKING) {
+               if (a & 1)
+                       LED1_ON();
+               else
+                       LED1_OFF();
+       }
+       else {
+               if (a & 4)
+                       LED1_ON();
+               else
+                       LED1_OFF();
+       }
+       a++;
 }
 
 static void main_timer_interrupt(void)
 }
 
 static void main_timer_interrupt(void)
@@ -151,8 +155,9 @@ int main(void)
        LED1_OFF();
        memset(&gen, 0, sizeof(gen));
        memset(&ballboard, 0, sizeof(ballboard));
        LED1_OFF();
        memset(&gen, 0, sizeof(gen));
        memset(&ballboard, 0, sizeof(ballboard));
-       ballboard.flags = DO_ENCODERS | DO_CS | DO_POWER; // DO_BD
-       
+       ballboard.flags = DO_ENCODERS | DO_CS | DO_POWER |
+               DO_ERRBLOCKING | DO_BD;
+
        /* UART */
        uart_init();
 #if CMDLINE_UART == 3
        /* UART */
        uart_init();
 #if CMDLINE_UART == 3
@@ -165,7 +170,7 @@ int main(void)
 #  error not supported
 #endif
 
 #  error not supported
 #endif
 
-       //eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_BALLBOARD);
+       eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_BALLBOARD);
        /* check eeprom to avoid to run the bad program */
        if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
            EEPROM_MAGIC_BALLBOARD) {
        /* check eeprom to avoid to run the bad program */
        if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
            EEPROM_MAGIC_BALLBOARD) {
@@ -194,13 +199,12 @@ int main(void)
        timer0_register_OV_intr(main_timer_interrupt);
 
        /* PWM */
        timer0_register_OV_intr(main_timer_interrupt);
 
        /* PWM */
-       PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10, 
+       PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
                                 TIMER1_PRESCALER_DIV_1);
                                 TIMER1_PRESCALER_DIV_1);
-       PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10, 
+       PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
                                 TIMER4_PRESCALER_DIV_1);
                                 TIMER4_PRESCALER_DIV_1);
-       
-       PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED | 
-                     PWM_NG_MODE_SIGN_INVERTED,
+
+       PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
                      &PORTD, 4);
        PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED,
                      &PORTD, 5);
                      &PORTD, 4);
        PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED,
                      &PORTD, 5);
@@ -211,11 +215,11 @@ int main(void)
 
 
        /* servos */
 
 
        /* servos */
-       PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10, 
+       PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
                                 TIMER1_PRESCALER_DIV_256);
        PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
                      NULL, 0);
                                 TIMER1_PRESCALER_DIV_256);
        PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
                      NULL, 0);
-       PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10, 
+       PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
                                 TIMER1_PRESCALER_DIV_256);
        PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
                      NULL, 0);
                                 TIMER1_PRESCALER_DIV_256);
        PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
                      NULL, 0);
@@ -223,12 +227,12 @@ int main(void)
                      NULL, 0);
        PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
                      NULL, 0);
                      NULL, 0);
        PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
                      NULL, 0);
-       
+
        /* SCHEDULER */
        scheduler_init();
 
        /* SCHEDULER */
        scheduler_init();
 
-       scheduler_add_periodical_event_priority(do_led_blink, NULL, 
-                                               100000L / SCHEDULER_UNIT, 
+       scheduler_add_periodical_event_priority(do_led_blink, NULL,
+                                               100000L / SCHEDULER_UNIT,
                                                LED_PRIO);
        /* all cs management */
        microb_cs_init();
                                                LED_PRIO);
        /* all cs management */
        microb_cs_init();
@@ -242,10 +246,15 @@ int main(void)
        /* ax12 */
        ax12_user_init();
 
        /* ax12 */
        ax12_user_init();
 
+       gen.logs[0] = E_USER_ST_MACH;
+       gen.log_level = 5;
+
        sei();
 
        printf_P(PSTR("\r\n"));
        printf_P(PSTR("Dass das Gluck deinen Haus setzt.\r\n"));
        sei();
 
        printf_P(PSTR("\r\n"));
        printf_P(PSTR("Dass das Gluck deinen Haus setzt.\r\n"));
+
+       state_machine();
        cmdline_interact();
 
        return 0;
        cmdline_interact();
 
        return 0;