예제 #1
0
//----------------------------------------------------------
//      System initialization
//----------------------------------------------------------
static inline void initSystem(void)
{
    bool success;
    (void)success;

    // disable interrupts: disabled on msp430 by default, but other systems might need this
    DISABLE_INTS();

    // stop the watchdog: GCC disables it by default, but other compilers might not be so helpful
    watchdogStop();

    // TODO: init dynamic memory
    // platformMemInit();

    // basic, platform-specific initialization: timers, platform-specific drivers (?)
    initPlatform();

    // start energy accounting (as soon as timers are initialized)
    energyConsumerOn(ENERGY_CONSUMER_MCU);

#ifdef USE_PRINT
    // init printing to serial (makes sense only after clock has been calibrated)
    if (printInit != NULL) printInit();
#endif

    INIT_PRINTF("starting MansOS...\n");

#ifdef USE_LEDS
    INIT_PRINTF("init LED(s)...\n");
    ledsInit();
#endif
#ifdef USE_BEEPER
    beeperInit();
#endif
#ifdef RAMTEXT_START
    if ((MemoryAddress_t)&_end > RAMTEXT_START) {
        // Panic right aways on RAM overflow.
        // In case this happens, you might want to increase the address
        // specified by CONST_RAMTEXT_START in config file
        assertionFailed("Overflow between .data and .ramtext sections", __FILE__, __LINE__);
    }
#endif
#ifdef USE_ADC
    if (adcInit != NULL) {
        INIT_PRINTF("init ADC...\n");
        adcInit();
    }
#endif
#ifdef USE_RANDOM
    INIT_PRINTF("init RNG...\n");
    randomInit();
#endif
#if USE_ALARMS
    INIT_PRINTF("init alarms...\n");
    initAlarms();
#endif
#ifdef USE_RADIO
    INIT_PRINTF("init radio...\n");
    radioInit();
#endif
#ifdef USE_ADDRESSING
    INIT_PRINTF("init communication stack...\n");
    networkingInit();
#endif
#ifdef USE_EXT_FLASH
    INIT_PRINTF("init external flash...\n");
    extFlashInit();
#endif
#ifdef USE_SDCARD
    INIT_PRINTF("init SD card...\n");
    sdcardInit();
#endif
#ifdef USE_EEPROM
    INIT_PRINTF("init EEPROM...\n");
    eepromInit();
#endif
#ifdef USE_ISL29003
    INIT_PRINTF("init ISL light sensor...\n");
    success = islInit();
    if (!success) {
        INIT_PRINTF("ISL init failed!\n");
    }
#endif
#ifdef USE_ADS1115
    INIT_PRINTF("init ADS111x ADC converter chip...\n");
    adsInit();
#endif
#if USE_ADS8638
    INIT_PRINTF("init ADS8638 ADC converter chip...\n");
    ads8638Init();
#endif
#if USE_ADS8328
    INIT_PRINTF("init ADS8328 ADC converter chip...\n");
    ads8328Init();
#endif
#if USE_AD5258
    INIT_PRINTF("init AD5258 digital potentiometer...\n");
    ad5258Init();
#endif
#if USE_DAC7718
    INIT_PRINTF("init DAC7718 DAC converter chip...\n");
    dac7718Init();
#endif
#if USE_ISL1219
    INIT_PRINTF("init ISL1219 real-time clock chip...\n");
    isl1219Init();
#endif
#ifdef USE_HUMIDITY
    INIT_PRINTF("init humidity sensor...\n");
    humidityInit();
#endif
#ifdef USE_ACCEL
    INIT_PRINTF("init accelerometer...\n");
    accelInit();
#endif
#ifdef USE_TIMESYNC
    INIT_PRINTF("init base station time sync...\n");
    timesyncInit();
#endif
#ifdef USE_SMP
    INIT_PRINTF("init SSMP...\n");
    smpInit();
#endif
#ifdef USE_REPROGRAMMING
    INIT_PRINTF("init reprogramming...\n");
    bootParamsInit();
#endif
#ifdef USE_DCO_RECALIBRATION
    extern void dcoRecalibrationInit(void);
    INIT_PRINTF("init DCO recalibration...\n");
    dcoRecalibrationInit();
#endif
#ifdef USE_FS
    INIT_PRINTF("init file system...\n");
    fsInit();
#endif
#ifdef USE_FATFS
    INIT_PRINTF("init FAT file system...\n");
    fatFsInit();
    INIT_PRINTF("init POSIX-like file routines...\n");
    posixStdioInit();
#endif
#ifdef USE_WMP
    INIT_PRINTF("init WMP...\n");
    wmpInit();
#endif
#ifdef USE_SEAL_NET
    INIT_PRINTF("init SEAL networking...\n");
    sealNetInit();
#endif

    INIT_PRINTF("starting the application...\n");
}
//Initialize all sensors on the IMU board
void IMUinit() {
    //Initialize the I2C bus
    i2cInit();
    
    //Disable interrupts while everything is being set up
    CRITICAL_SECTION_START;
    
    //Initialize the gyro
    if(gyroInit(GYRO_RANGE_250DPS)) {
        theFlags.gyroEnabled = TRUE;
        
        //Set the pin as input
        cbi(DDRE, DDE4);
        
        //Disable the pullup on the relevant pin
        cbi(PORTE, PORTE4);
        
        //Attach the DRDY interrupt
        sbi(EICRB, ISC40); //Writing 1 to ISC40 and ISC41 sets interrupt on rising edge
        sbi(EICRB, ISC41);
        
        sbi(EIMSK, INT4); //Setting INT4 in EIMSK enables the interrupt (if they are globally enabled (but they are cause i2cinit did that))
        
        //Now wait for DRDY to go low and reenmable it to start everything off
        while(inb(PINE) & _BV(PINE4)) {
            ; //Wait for the port to go low
        }
        gyroEnableDrdy();
    } else {
        #ifdef IMU_DEBUG
        printf("Error intitializing the gyro!\r\n");
        #endif
        theFlags.gyroEnabled = FALSE;
    }
    
    //Intialize the accelerometer
    if(accelInit()) {
        theFlags.accelEnabled = TRUE;
        
        //Set the pin as input
        cbi(DDRD, DDD2);
        
        //Disable the pullup on the relevant pin
        cbi(PORTD, PORTD2);
        
        //Attach the DRDY interrupt
        sbi(EICRA, ISC20); //Writing 1 to ISC20 and ISC21 sets interrupt on rising edge
        sbi(EICRA, ISC21);
        
        sbi(EIMSK, INT2); //Setting INT2 in EIMSK enables the interrupt (if they are globally enabled (but they are cause i2cinit did that))
        
        //Now wait for DRDY to go low and reenable it to start everything off
        while(inb(PINB) & _BV(PINB2)) {
            ; //Wait for the port to go low
        }
        accelEnableDrdy();
    } else {
        #ifdef IMU_DEBUG
        printf("Error intitializing the accelerometer!\r\n");
        #endif
        theFlags.accelEnabled = FALSE;
    }
    
    //Initialize the magnetometer
    if(magInit()) {
        theFlags.magEnabled = TRUE;
        
        //Set the pin as input
        cbi(DDRD, DDD3);
        
        //Disable the pullup on the DRDY pin
        cbi(PORTD, PORTD3);
        
        //Attach the DRDY interrupt
        sbi(EICRA, ISC30); //Writing 1 to ISC30 and ISC31 sets interrupt on rising edge
        sbi(EICRA, ISC31);
        
        sbi(EIMSK, INT3); //Setting INT3 in EIMSK enables the interrupt (if they are globally enabled (but they are cause i2cinit did that))
        
    } else {
        #ifdef IMU_DEBUG
        printf("Error intitializing the magnetometer!\r\n");
        #endif
        theFlags.magEnabled = FALSE;
    }
    
    //Initialize the BMP180 pressure/temp sensor
    if(bmpInit()) {
        theFlags.bmpEnabled = TRUE;
        
        //Attach the interrupt handler for the timer ticks
        timerSetInterruptCallback( imuTimerTick );
        
        //Start a temperature measurrement to set everything off
        const u08 toSend[2] = {BMP180_REGISTER_CONTROL, BMP180_REGISTER_READTEMPCMD};
        i2cMasterSendNI(BMP180_ADDRESS, 2, (u08*)&toSend);
        
        myBmpState = TEMPERATURE_MEASURING;
        bmpLastStateChange = millis();
        
    } else {
        #ifdef IMU_DEBUG
        printf("Error intitializing the BMP180!\r\n");
        #endif
        theFlags.bmpEnabled = FALSE;
    }
    
    
    //Attach the custom stop handler after all sensors are initialized
    i2cSetStopHandler( customStopHandler );
    
    //Re-enable the interrupts
    CRITICAL_SECTION_END;
}