예제 #1
0
void main(void) {
    PHY_init_t phy_params;
    LINK_init_t link_params;       

    WDTCONbits.SWDTEN = 0; 
    
    LOG_init();                
    D_G printf("Main started\n");    
    
    phy_params.bitrate = DATA_RATE_66;
    phy_params.band = BAND_863;
    phy_params.channel = 28;
    phy_params.power = TX_POWER_13_DB;
    phy_params.cca_noise_treshold = 30;
    PHY_init(phy_params);
    D_G printf("PHY inicialized\n");
    
    link_params.tx_max_retries = 0;
    link_params.rx_data_commit_timeout = 64; 
    LINK_init(link_params);

    ds_prepare();
    for (uint8_t i = 0; i < 12; i++) {
        LED0 = ~LED0;
        delay_ms(50);
    }
    
    fitp_init(); 
    
    /*
    GLOBAL_STORAGE.edid[0] = 0xED; //E
    GLOBAL_STORAGE.edid[1] = 0x00; //d
    GLOBAL_STORAGE.edid[2] = 0x00; //i
    GLOBAL_STORAGE.edid[3] = 0x01; //d
    
    GLOBAL_STORAGE.nid[0]=0x4e;  //N
    GLOBAL_STORAGE.nid[1]=0x69; //i
    GLOBAL_STORAGE.nid[2]=0x64; //d
    GLOBAL_STORAGE.nid[3]=0x3c;  //:
    */
             
    GLOBAL_STORAGE.sleepy_device = true;
    
    euid_load(); // load euid from eeprom
    refresh_load_eeprom();
    
    accel_int = false;
    while (1) {       
        
        if (accel_int) {
            HW_ReInit();      
            if (sendValues()) { 
                D_G printf("Send values success\n");               
                LED1 = 1;
                delay_ms(1000);
                LED1 = 0;            
            }
            else if (fitp_join()){
                D_G printf("Join success\n"); 
                LED1 = 1;
                delay_ms(1000);
                LED1 = 0;
            }
            else {  // cannot send data and even join fails
                D_G printf("Send value and join failed\n");
                LED0 = 1;
                delay_ms(1000);
                LED0 = 0;
            }
            accel_int = false;                                    
        }           
       
        if (fitp_joined()){            
            ds_prepare();
            HW_DeInit();
            StartTimer(2);            
            goSleep();
            if (accel_int) {
                continue;
            }
            //LED1 = 1;
            HW_ReInit();
            sendValues();             
        }   
        
        HW_DeInit();
        if(GLOBAL_STORAGE.refresh_time < 3) {
        	GLOBAL_STORAGE.refresh_time = 3;  // if too short time         
        	save_refresh_eeprom(GLOBAL_STORAGE.refresh_time); //save refresh time on eeprom
        }
        StartTimer(GLOBAL_STORAGE.refresh_time - 2);  // -2 because ds_prepare takes up to 2seconds 
        //LED1 = 0;
        goSleep();
    }
}
예제 #2
0
int main(void) {
    wdt_enable(WDTO_4S);
    wdt_reset();
    /* configure PPM output port */
    PPM_DDR |= (1<<PPM_BIT);
    PPM_PORT &= ~(1<<PPM_BIT);

    /* configure LED output port */
    LED_DDR |= (1<<LED_BIT);
    LED_PORT |= (1<<LED_BIT);

    /* configure VOL(tage) warning port */
    VOL_DDR &= ~(1<<VOL_BIT);
    VOL_PORT |= (1<<VOL_BIT); // enable pullup

#ifdef ENABLE_SERIAL
    serial_init();
#endif
#ifdef ENABLE_TWI
    twi_init();
#endif
#ifdef USE_NUNCHUK
    nunchuk_init();
    wdt_reset();
#endif

    /* configure switches */
    sw_init();

    /* configure ADC */
    adc_init();

#if defined(USE_TWI_ADC)
    twi_adc_init();
    wdt_reset();
#endif
#if defined(USE_MAG)
    mag_init();
    wdt_reset();
#endif
#if defined(USE_ACC)
    acc_init();
#endif
#if defined(USE_LCD)
    /* initialize LCD twice (due to timing issues?) */
    lcd_init();
    wdt_reset();
    _delay_ms(100);
    wdt_reset();
    lcd_init();
    wdt_reset();
    lcd_splash();
#endif

    /* configure watchfog timer to reset after 60ms */
    wdt_enable(WDTO_60MS);

    /* configure timer */

    /* enable CTC waveform generation (TOP == OCR1A) */
    TCCR1B |= (1<<WGM12);
    /* set compare value for the stop pulse to 300µs */
    OCR1B = STOP_US;
    /* set pulse width to max for now */
    OCR1A = ~0;
    /* set Timer 1 to clk/8, giving us ticks of 1 µs */
    TCCR1B |= (1<<CS11);

    /* Timer 2 generates overflows at 1kHz */
#if defined(TCCR2) /* e.g. ATMega8 */
#define TIMER2_COMP_IRQ TIMER2_COMP_vect
    TCCR2 = (1<<WGM21 | 1<<CS22);
    OCR2 = 0x7D;
    /* enable compare and overflow interrupts */
    TIMSK = (1<<OCIE2 | 1<<OCIE1B | 1<<OCIE1A);
#elif defined(TCCR2A) /* e.g. ATMega{8,16,32}8 */
#define TIMER2_COMP_IRQ TIMER2_COMPA_vect
    TCCR2A = (1<<WGM21);
    TCCR2B = (1<<CS22);
    OCR2A = 0x7D;
    /* enable compare and overflow interrupts */
    TIMSK1 = (1<<OCIE1B | 1<<OCIE1A);
    TIMSK2 = (1<<OCIE2A);
#else
#error "Unable to determine timer 2 configuration registers"
#endif

    /* initialize channel data */
    start_ppm_frame();
    set_ppm(1);
    start_ppm_pulse();

    /* enable interrupts */
    sei();

    serial_write_str("Welcome!\n");
    while (1) {
        /* reset watchdog */
        wdt_reset();

        /* keep sampling adc data */
        adc_query();

        /* query switches */
        sw_query();

        /* prepare Datenschlag data frames */
        ds_prepare();

#ifdef USE_TWI_ADC
        /* query TWI/I²C ADC */
        twi_adc_query();
#endif
#if defined(USE_MAG)
#ifdef MAG_CENTER_CALIBRATION_TRIGGER_INPUT
        if (get_input_scaled( (MAG_CENTER_CALIBRATION_TRIGGER_INPUT), 1, -1) == (MAG_CENTER_CALIBRATION_TRIGGER_VALUE)) {
            mag_set_calibration(millis + 10000L);
        }
#endif
        if (mag_is_calibrating()) {
            mag_calibrate(0);
        } else {
            mag_calibrate(1);
            mag_query();
            mag_dump();
        }
#endif
#if defined(USE_ACC)
        acc_query();
        acc_dump();
#endif

#ifdef USE_NUNCHUK
        nunchuk_query();
#endif

        check_voltage();

        /* switch LED */
        if (!low_voltage || (millis/250 % 2)) {
            LED_PORT |= (1<<LED_BIT);
        } else {
            LED_PORT &= ~(1<<LED_BIT);
        }

#ifdef USE_LCD
        static enum {
            LCD_MODE_STATUS,
#ifdef LCD_MENU
            LCD_MODE_MENU,
#endif
            LCD_MODE_CNT
        } lcd_mode;

        uint8_t lcd_mode_changed = 0;
#ifdef LCD_MODE_SWITCH_INPUT
        static int8_t old_sw_state = 0;
        int8_t sw_state = get_input_scaled(LCD_MODE_SWITCH_INPUT, -1, 1);
        if (old_sw_state != sw_state) {
            lcd_mode += LCD_MODE_CNT;
            lcd_mode += sw_state;
            lcd_mode %= LCD_MODE_CNT;
            old_sw_state = sw_state;
            lcd_mode_changed = 1;
        }
#endif
        switch (lcd_mode) {
        case LCD_MODE_STATUS:
            lcd_status_update(lcd_mode_changed);
            break;
#ifdef LCD_MENU
        case LCD_MODE_MENU:
            lcd_menu_update(lcd_mode_changed);
            break;
#endif
        default:
            break;
        }
#endif
    }
    return 0;
}