void UserInit(void) { InitLED(); InitTempSensor(); InitFAN(); InitWorkTick(); //InitI2CMaster(); InitResultRx(); DetectAsics(); }//end UserInit
void UserInit(void) { mInitAllLEDs(); mInitAllSwitches(); old_sw2 = sw2; old_sw3 = sw3; InitTempSensor(); mInitPOT(); ResetTempLog(); temp_mode = TEMP_REAL_TIME; #if defined(__18CXX) /* Init Timer0 for data logging interval (every 1 second) */ T0CON = 0b10010111; /* Timer0 is already enabled by default */ #elif defined(__C30__) || defined __XC16__ #endif }//end UserInit
void SensorsTask(void) { // Array storing the status of each physical sensor. // If a sensor fails to initialize, or fails 3 sensor reads in a row, // it will be disabled here until the next system reboot // 0 = tmp0 // 1 = tmp1 // 2 = tmp3 // 3 = humidity / air temp // 4 = pressure // 5 = accelerometer uint8_t enabledSensors[7] = {3, 3, 3, 3, 3, 3, 3}; uint8_t i; I2C_Status retVal = I2C_OK; INFO("(SENSORS_TASK) I2C Sensor failed to initialize\r\n"); for(i = 0; i < 3; i++) { // If the temperature sensor initialized, set its enabled value to 3 (so it has 3 chances to respond to a read request) // Else, disable the sensor if(InitTempSensor(i) != I2C_OK) { I2C_Reset(SLB_I2C); enabledSensors[i] = 0; WARN("(SENSORS_TASK) I2C Sensor failed to initialize\r\n"); } } if(InitHumiditySensor() != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[3] = 0; WARN("(SENSORS_TASK) Humidity sensor failed to initialize\r\n"); } if(InitPressureSensor() != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[4] = 0; WARN("(SENSORS_TASK) Pressure sensor failed to initialize\r\n"); } if(InitAccelerometer() != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[5] = 0; WARN("(SENSORS_TASK) Accelerometer failed to initialize\r\n"); } // Let other tasks in the system warmup before entering the sensor polling loop osDelay(2000); // TODO: re-initialize the sensors once a day to check for failures and for sensors that have come back online // TODO: report to the base station when a sensor fails while(1) { for(i = 0; i < 3; i++) { if(enabledSensors[i] > 0) { switch(i) { case 0: retVal = ReadTempSensor(0, &sensorData.temp0); break; case 1: retVal = ReadTempSensor(1, &sensorData.temp1); break; case 2: retVal = ReadTempSensor(2, &sensorData.temp2); break; default: retVal = ReadTempSensor(0, &sensorData.temp0); break; } // If the sensor read failed, indicate that the sensor has one less chance to respond correctly before being disabled if(retVal != I2C_OK) { I2C_Reset(SLB_I2C); enabledSensors[i]--; WARN("(SENSORS_TASK) Temp sensor read failed\r\n"); } // The sensor is still alive! Restore it to a full 3 chances to respond else if(enabledSensors[i] != 3) { enabledSensors[i] = 3; DEBUG("(SENSORS_TASK) Temp sensor connection restored\r\n"); } } } if(enabledSensors[3] > 0) { do { if(ReadHumiditySensor(&sensorData.humid) != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[3]--; WARN("(SENSORS_TASK) Humidity sensor read failed\r\n"); break; } else if(enabledSensors[3] != 3) { enabledSensors[3] = 3; DEBUG("(SENSORS_TASK) Humidity sensor connection restored\r\n"); } if(ReadAirTempSensor(&sensorData.tempAir) != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[3]--; WARN("(SENSORS_TASK) Air temp sensor read failed\r\n"); } else if(enabledSensors[3] != 3) { enabledSensors[3] = 3; DEBUG("(SENSORS_TASK) Air temp sensor connection restored\r\n"); } } while(0); } if(enabledSensors[4] > 0) { if(ReadPressureSensor(&sensorData.alt) != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[4]--; WARN("(SENSORS_TASK) Altimeter sensor read failed\r\n"); } } if(enabledSensors[5] > 0) { uint16_t x, y, z; if(ReadAccelerometer(&x, &y, &z) != I2C_OK) { I2C_Reset(ALB_I2C); enabledSensors[5]--; WARN("(SENSORS_TASK) Accelerometer sensor read failed\r\n"); } DEBUG("X: %d, Y: %d, Z: %d", x, y, z); } ReadSoilMoisture(&sensorData.moist0, &sensorData.moist1, &sensorData.moist2); // Send sensor Data to the base station SendSensorData(); osDelay(pollingRate); } }
void main(void) { // // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the F2837xS_SysCtrl.c file. // InitSysCtrl(); // // Step 2. Initialize GPIO: // This example function is found in the F2837xS_Gpio.c file and // illustrates how to set the GPIO to it's default state. // InitGpio(); // Skipped for this example // // Step 3. Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts // DINT; // // Initialize the PIE control registers to their default state. // The default state is all PIE interrupts disabled and flags // are cleared. // This function is found in the F2837xS_PieCtrl.c file. // InitPieCtrl(); // // Disable CPU interrupts and clear all CPU interrupt flags: // IER = 0x0000; IFR = 0x0000; // // Initialize the PIE vector table with pointers to the shell Interrupt // Service Routines (ISR). // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in F2837xS_DefaultIsr.c. // This function is found in F2837xS_PieVect.c. // InitPieVectTable(); // // Map ISR functions // EALLOW; PieVectTable.ADCA1_INT = &adca1_isr; //function for ADCA interrupt 1 EDIS; // // Configure the ADC and power it up // ConfigureADC(); // // Initialize the temperature sensor // Note: The argument needs to change if using a VREFHI voltage other than 3.0V // InitTempSensor(3.0); // // Configure the ePWM // ConfigureEPWM(); // // Setup the ADC for ePWM triggered conversions on temperature sensor // SetupADCEpwm(); // // Enable global Interrupts and higher priority real-time debug events: // IER |= M_INT1; //Enable group 1 interrupts EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM // // enable PIE interrupt // PieCtrlRegs.PIEIER1.bit.INTx1 = 1; // // sync ePWM // EALLOW; CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1; // // start ePWM // EPwm1Regs.ETSEL.bit.SOCAEN = 1; //enable SOCA EPwm1Regs.TBCTL.bit.CTRMODE = 0; //unfreeze, and enter up count mode // // take conversions indefinitely in loop // while(1); }