ScienceSensorHub using the MAX32630FTHR

Dependencies:   Adafruit_FeatherOLED BMI160_Fork OneWire SDFileSystem TSL2561 USBDevice VEML6070 max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "max32630fthr.h"
00003 #include "OneWire.h"
00004 #include "USBSerial.h"
00005 #include "bmi160.h"
00006 #include "TSL2561.h"
00007 #include "Adafruit_SSD1306.h"
00008 #include "VEML6070.h"
00009 #include "SDFileSystem.h"
00010 
00011 #define BUFF_LENGTH     9
00012 
00013 
00014 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00015 
00016 AnalogIn batMonitor(AIN_0);
00017 
00018 // Hardware serial port over DAPLink
00019 Serial daplink(P2_1, P2_0);
00020 
00021 // UART 2. Connected to ScienceSensorBus0
00022 RawSerial uart2(P3_1, P3_0);
00023 
00024 // UART 3. Connected to ScienceSensorBus1
00025 //Serial uart3(P5_4, P5_3);
00026 
00027 // Virtual serial port over USB
00028 USBSerial microUSB;
00029 
00030 
00031 using namespace OneWire;
00032 using namespace RomCommands;
00033 
00034 DigitalOut rLED(LED1, LED_ON);
00035 DigitalOut gLED(LED2, LED_OFF);
00036 DigitalOut bLED(LED3, LED_OFF);
00037 
00038 //Buttons A,B,C on FeatherWingOLED
00039 DigitalIn aButton(P5_3, PullUp);
00040 DigitalIn bButton(P3_3, PullUp);
00041 DigitalIn cButton(P3_2, PullUp);
00042 
00043 InterruptIn imuInt(P3_6);
00044 
00045 //Get 1-Wire Master (owm) instance
00046 //         (extWeakPup, extStrongPup)
00047 MCU_OWM owm(false, true);
00048 
00049 //Make sure owm is initialized
00050 OneWireMaster::CmdResult result = owm.OWInitMaster();
00051 
00052 //
00053 SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd");  // mosi, miso, sclk, cs
00054 
00055 //Setup I2C busses
00056 I2C i2cBus(P5_7, P6_0);     //Actually i2c2
00057 I2C i2cBus1(P3_4, P3_5);
00058 
00059 //Setup FeatherOLED
00060 Adafruit_SSD1306_I2c featherOLED(i2cBus1);
00061 
00062 //Setup PMIC
00063 MAX14690 pmic(i2cBus);
00064 
00065 // Variable Definitions
00066 uint8_t             screenMode = 0;
00067 uint8_t             screenMax = 4;
00068 event_callback_t    serialRXCb;
00069 uint16_t            co2Level = 420;
00070 char             rx_buf[BUFF_LENGTH + 1];
00071 volatile int rx_in=0;
00072 volatile int rx_out=0;
00073 
00074 // Circular buffers for serial TX and RX data - used by interrupt routines
00075 const int buffer_size = 255;
00076 // might need to increase buffer size for high baud rates
00077 char tx_buffer[buffer_size+1];
00078 char rx_buffer[buffer_size+1];
00079 
00080 void doubleTapDetected(void)
00081 {
00082     rLED = !rLED;
00083     screenMode = (screenMode + 1) % screenMax;
00084 }
00085 
00086 // Interupt Routine to read in data from serial port
00087 void serialRX(void)
00088 {
00089     bLED = LED_ON;
00090 // Loop just in case more than one character is in UART's receive FIFO buffer
00091 // Stop if buffer full
00092     while ((uart2.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00093         rx_buffer[rx_in] = uart2.getc();
00094 // Uncomment to Echo to USB serial to watch data flow
00095 //        monitor_device.putc(rx_buffer[rx_in]);
00096         rx_in = (rx_in + 1) % buffer_size;
00097     }
00098     bLED = LED_OFF;
00099     return;
00100 }
00101 
00102 
00103 
00104 // main() runs in its own thread in the OS
00105 // (note the calls to Thread::wait below for delays)
00106 int main()
00107 {
00108 
00109     uart2.attach(&serialRX, Serial::RxIrq);
00110     imuInt.mode(PullDown);
00111 
00112     pmic.monCfg = MAX14690::MON_BAT;
00113     char tVar, pmicStatusA, pmicStatusB;
00114     pmic.readReg(MAX14690::REG_BOOT_CFG, &tVar);
00115     pmic.readReg(MAX14690::REG_STATUS_A, &pmicStatusA);
00116     pmic.readReg(MAX14690::REG_STATUS_B, &pmicStatusB);
00117     gLED = LED_ON;
00118     rLED = LED_ON;
00119     char co2cmd[9]= {0xff,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
00120     char co2rtrn[9];
00121 
00122 
00123     i2cBus.frequency(100000);
00124     i2cBus1.frequency(400000);
00125 
00126     uint32_t failures = 0;
00127 
00128     DigitalIn uSDdetect(P2_2, PullUp);
00129     static const uint32_t N = 14400;
00130     uint32_t samples = 0;
00131     float accYaxisBuff[N];
00132     float accZaxisBuff[N];
00133     float gyroXaxisBuff[N];
00134     int32_t pulseWidthBuff[N];
00135 
00136     //Sensor data vars
00137     BMI160::SensorData accData;
00138     BMI160::SensorData gyroData;
00139     BMI160::SensorTime sensorTime;
00140 
00141     //Configure Color Sensor Instance. (Attached to I2C2)
00142 //    TCS3472_I2C rgbSensor(i2cBus);
00143 
00144     //get Lux Sensor instance and configure it.
00145     TSL2561 luxSensor(i2cBus, TSL2561_ADDRESS_GND);
00146 
00147     //Get UV Sensor Instance & Configure It.
00148     VEML6070 uvSensor(i2cBus);
00149     uvSensor.begin(VEML6070_4_T);
00150 
00151     //Get IMU instance and configure it
00152     BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
00153 
00154     //Power up sensors in normal mode
00155     if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) {
00156         printf("Failed to set gyroscope power mode\n");
00157         failures++;
00158     }
00159 
00160     wait(0.1);
00161 
00162     if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) {
00163         printf("Failed to set accelerometer power mode\n");
00164         failures++;
00165     }
00166     wait(0.1);
00167 
00168     BMI160::AccConfig accConfig;
00169     BMI160::AccConfig accConfigRead;
00170     accConfig.range = BMI160::SENS_4G;
00171     accConfig.us = BMI160::ACC_US_OFF;
00172     accConfig.bwp = BMI160::ACC_BWP_2;
00173     accConfig.odr = BMI160::ACC_ODR_11;
00174 
00175     imu.writeRegister(BMI160::CMD, 0xB1); //Reset the interrupt engine
00176     imu.writeRegister(BMI160::INT_EN_0, 0x30);  // Set DoubleTap Detection Interrupt
00177     imu.writeRegister(BMI160::INT_OUT_CTRL, 0b00001010);  // Interrupt Output Control
00178     imu.writeRegister(BMI160::INT_LATCH, 0b00000100);  // Interrupt Latch.
00179     imu.writeRegister(BMI160::INT_MAP_0, 0b00110000);   // Map DoubleTap to Interrupt 1
00180     imu.writeRegister(BMI160::INT_TAP_0, 0b00000111);
00181     imu.writeRegister(BMI160::INT_TAP_1, 0b00001010);
00182     imuInt.fall(&doubleTapDetected);
00183 
00184     Thread::wait(50);
00185 
00186     featherOLED.clearDisplay();
00187     featherOLED.setTextCursor(0,0);
00188     featherOLED.printf("%ux%u OLED Display\r\n", featherOLED.width(), featherOLED.height());
00189     featherOLED.printf("HelloWorld \r");
00190     featherOLED.display();
00191 
00192     FILE *fp = fopen("/sd/myfile.txt", "w");
00193     fprintf(fp, "Begin Logging!\n");
00194 //    fclose(fp);
00195     rLED = LED_OFF;
00196 
00197     while (true) {
00198         gLED = !gLED;
00199         pmic.readReg(MAX14690::REG_STATUS_A, &pmicStatusA);
00200         pmic.readReg(MAX14690::REG_STATUS_B, &pmicStatusB);
00201         featherOLED.clearDisplay();
00202         featherOLED.setTextCursor(0,0);
00203         switch (screenMode) {
00204             case 0x00 :  //Main Screen
00205                 featherOLED.printf("MAX32630FTHR OLED\n");
00206                 featherOLED.printf("ScienceSensorHub\r\n");
00207                 featherOLED.printf("by Scott Roberts\r\n");
00208                 break;
00209             case 0x01 :  //Light Values
00210                 featherOLED.printf("Plant Sensors\r\n");
00211                 featherOLED.printf("Lux: %+5.2f \r\n", luxSensor.lux());
00212                 featherOLED.printf("UV: %+3u\r\n", uvSensor.readUV());
00213                 featherOLED.printf("CO2: %3u ",co2Level);
00214 
00215                 break;
00216             case 0x02 :  //IMU Values
00217                 featherOLED.printf("IMU Values\r\n");
00218                 imu.getSensorXYZ(accData, accConfig.range);
00219                 featherOLED.printf("X: %f\r\n",accData.xAxis.scaled);
00220                 featherOLED.printf("Y: %f\r\n",accData.yAxis.scaled);
00221                 featherOLED.printf("Z: %f\r\n",accData.zAxis.scaled);
00222                     break;
00223                 case 0x03 :  //Diagnostics
00224                         featherOLED.printf("Diagnostics\r\n");
00225                         featherOLED.printf("B: %5.3f \r\n",batMonitor.read()*5.0f);
00226                         if((pmicStatusB & 0x08) != 0) featherOLED.printf("USBok/");
00227                         if((pmicStatusB & 0x01) ==1) featherOLED.printf("ChgTmo/");
00228                         switch(pmicStatusA & 0x07) {
00229                             case 0x00 :
00230                                 featherOLED.printf("Coff/");
00231                                     break;
00232                                 case 0x01 :
00233                                     featherOLED.printf("LTemp/");
00234                                     break;
00235                                 case 0x02 :
00236                                     featherOLED.printf("PreC/");
00237                                     break;
00238                                 case 0x03 :
00239                                     featherOLED.printf("FCCC/");
00240                                     break;
00241                                 case 0x04 :
00242                                     featherOLED.printf("FCCV/");
00243                                     break;
00244                                 case 0x05 :
00245                                     featherOLED.printf("MCinPr/");
00246                                     break;
00247                                 case 0x06 :
00248                                     featherOLED.printf("MTdone/");
00249                                     break;
00250                                 case 0x07 :
00251                                     featherOLED.printf("Cfault/");
00252                                     break;
00253                             }
00254                 break;
00255             case 0x04 : //Oops
00256                     featherOLED.printf("Misc\r\n");
00257                     break;
00258 
00259                 }
00260 
00261 
00262         featherOLED.display();
00263 
00264         Thread::wait(750);
00265     }
00266 }
00267