mdot UDK & STMicro MEMS Shield Sensor packet example

Dependencies:   libmDot-mbed5 DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B X_NUCLEO_IKS01A1 Senet_Packet

Fork of MTDOT-UDKDemo_Senet by canuck lehead

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers board_evb.cpp Source File

board_evb.cpp

00001 /***
00002  *       _____                         _   
00003  *      / ____|                       | |  
00004  *     | (___     ___   _ __     ___  | |_ 
00005  *      \___ \   / _ \ | '_ \   / _ \ | __|
00006  *      ____) | |  __/ | | | | |  __/ | |_ 
00007  *     |_____/   \___| |_| |_|  \___|  \__|
00008  *         (C) 2016 Senet, Inc                                
00009  *                                         
00010  */
00011 
00012 #ifdef MTDOT_EVB
00013 #include "board_evb.h"
00014 #include "MMA845x.h"
00015 #include "MPL3115A2.h"
00016 #include "ISL29011.h"
00017 #include "NCP5623B.h"
00018 #include "DOGS102.h"
00019 #include "font_6x8.h"
00020 #include "MultiTech_Logo.h"
00021 
00022 Serial debugUART(PA_9, PA_10); // mDot debug UART
00023 
00024 static InterruptIn mDot08(PA_12);  //  GPIO/USB       PB S1 on EVB
00025 static InterruptIn mDot09(PA_11);  //  GPIO/USB       PB S2 on EVB
00026 static InterruptIn mDot12(PA_0);    //  GPIO/UART_CTS  PRESSURE_INT2 on EVB
00027 static DigitalOut  mDot13(PC_13,1); //  GPIO           LCD_C/D
00028 static InterruptIn mDot15(PC_1);    //  GPIO           LIGHT_PROX_INT on EVB
00029 static InterruptIn mDot16(PA_1);    //  GPIO/UART_RTS  ACCEL_INT2 on EVB
00030 static DigitalOut mDot17(PA_4,1);   //  GPIO/SPI_NCS   LCD_CS on EVB
00031 static AnalogIn mDot20(PB_1);         //  GPIO          Current Sense Analog in on EVB
00032 static I2C mDoti2c(PC_9,PA_8);         // mDot External I2C mDot6 and mDot7
00033 static SPI mDotspi(PA_7,PA_6,PA_5);   // mDot external SPI mDot11, mDot4, and mDot18
00034 static MMA845x_DATA   accel_data   = 0;
00035 static MPL3115A2_DATA baro_data    = 0;
00036 static MMA845x*       evbAccel     = 0;
00037 static MPL3115A2*     evbBaro      = 0;
00038 static ISL29011*      evbAmbLight  = 0;
00039 static NCP5623B*      evbBackLight = 0;
00040 static DOGS102*       evbLCD       = 0;
00041 static Thread         thread_1;
00042 static Thread         thread_2;
00043 static char           txtstr[17];
00044 // flags for pushbutton debounce code
00045 static bool pb1_low     = false;
00046 static bool pb2_low     = false;
00047 static bool evbLedState = false;
00048 
00049 CBoardEVB::CBoardEVB()
00050 {
00051     boardPtr = this;
00052 }
00053 
00054 
00055 EBoardStatus CBoardEVB::init()
00056 {
00057     CBoard::init();
00058 
00059     // Setting up LED1 as activity LED
00060     mDotPtr->setActivityLedPin(PB_0);
00061     mDotPtr->setActivityLedEnable(true);
00062 
00063     // threads for de-bouncing pushbutton switches
00064     thread_1.start(callback(this, &CBoardEVB::pb1_debounce));
00065     thread_2.start(callback(this, &CBoardEVB::pb2_debounce));
00066 
00067     evbAccel     = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer
00068     evbBaro      = new MPL3115A2(mDoti2c); // setup Barometric sensor
00069     evbAmbLight  = new ISL29011(mDoti2c); // Setup Ambient Light Sensor
00070     evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip
00071     evbLCD       = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD
00072 
00073     /*
00074      *  Setup SW1 as program stop function
00075      */
00076     mDot08.disable_irq();
00077     mDot08.fall(callback(this, &CBoardEVB::pb1ISR));
00078 
00079     /*
00080      *  need to call this function after rise or fall because rise/fall sets
00081      *  mode to PullNone
00082      */
00083     mDot08.mode(PullUp);
00084 
00085     mDot08.enable_irq();
00086 
00087     /*
00088      *  Setup SW2 as packet time change
00089      */
00090     mDot09.disable_irq();
00091     mDot09.fall(callback(this, &CBoardEVB::pb2ISR));
00092 
00093     /*
00094      *  need to call this function after rise or fall because rise/fall sets
00095      *  mode to PullNone
00096      */
00097     mDot09.mode(PullUp);
00098 
00099     mDot09.enable_irq();
00100 
00101     /*
00102     * Setting other InterruptIn pins with Pull Ups
00103     */
00104     mDot12.mode(PullUp);
00105     mDot15.mode(PullUp);
00106     mDot16.mode(PullUp);
00107 
00108     printf("font table address %p\n\r",&font_6x8);
00109     printf("bitmap address %p\n\r",&MultiTech_Logo);
00110 
00111     // Setup and display logo on LCD
00112     evbLCD->startUpdate();
00113 
00114     evbLCD->writeBitmap(0,0,MultiTech_Logo);
00115 
00116     sprintf(txtstr,"MTDOT");
00117     evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
00118     sprintf(txtstr,"Evaluation");
00119     evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
00120     sprintf(txtstr,"Board");
00121     evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
00122 
00123     evbLCD->endUpdate();
00124 
00125     return Board_Ok;
00126 }
00127 
00128 EBoardStatus CBoardEVB::start()
00129 {
00130     osDelay(200);
00131     evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
00132 
00133     // sets LED2 to 50% max current
00134     evbBackLight->setLEDCurrent(16);
00135 
00136     printf("Start of Test\n\r");
00137 
00138     osDelay (500); // allows other threads to process
00139     printf("shutdown LED:\n\r");
00140     evbBackLight->shutdown();
00141 
00142     osDelay (500); // allows other threads to process
00143     printf("Turn on LED2\n\r");
00144     evbBackLight->setLEDCurrent(16);
00145 
00146     char data = evbAccel->getWhoAmI();
00147     printf("Accelerometer who_am_i value = %x \n\r", data);
00148 
00149     uint8_t result = evbAccel->getStatus();
00150     printf("status byte = %x \n\r", result);
00151 
00152     printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE");
00153 
00154     result = evbBaro->getStatus();
00155     printf("status byte = %x \n\r", result);
00156 
00157     /*
00158     *  Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz
00159     *  normal oversample mode, High pass filter off
00160     */
00161     evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF,
00162             MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF );
00163 
00164     /*
00165      * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition.
00166      * and a sample taken every second when in active mode
00167      */
00168     evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, MPL3115A2::AT_1);
00169 
00170     /*
00171      * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution,
00172      * and 16000 lux range
00173      */
00174     evbAmbLight->setMode(ISL29011::ALS_CONT);
00175     evbAmbLight->setResolution(ISL29011::ADC_16BIT);
00176     evbAmbLight->setRange(ISL29011::RNG_16000);
00177 
00178     /*
00179      * Set the accelerometer for active mode
00180      */
00181     evbAccel->activeMode();
00182 
00183     /*
00184      * Clear the min-max registers in the Barometric Sensor
00185      */
00186     evbBaro->clearMinMaxRegs();
00187 
00188     evbBackLight->setLEDCurrent(0);
00189 
00190     return Board_Ok;
00191 }
00192 
00193 EBoardStatus CBoardEVB::readSensors ( BoardSensorData &sensorData )
00194 {
00195     MMA845x_DATA accel_data;
00196     uint8_t      result;
00197     int32_t      num_whole;
00198     uint32_t     pressure;
00199     int16_t      num_frac;
00200 
00201     sensorData.init();
00202 
00203     // Update accelerometer state
00204     evbLCD->startUpdate();
00205     evbLCD->clearBuffer();
00206 
00207     /*
00208      * Retrieve and print out accelerometer data
00209      */
00210     for(uint32_t i = 0; i < 10; i++)
00211     {
00212         osDelay(100); // allows other threads to process
00213 
00214         result = evbAccel->getStatus();
00215 
00216         if( result & MMA845x::XYZDR )
00217         {
00218             accel_data = evbAccel->getXYZ();
00219 
00220             sprintf(txtstr,"Accelerometer");
00221             evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr));
00222             sprintf(txtstr, "x = %d", accel_data._x);
00223             evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr));
00224             sprintf(txtstr, "y = %d", accel_data._y);
00225             evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr));
00226             sprintf(txtstr, "z = %d", accel_data._z );
00227             evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr));
00228 
00229             sensorData.accel_x = accel_data._x;
00230             sensorData.accel_y = accel_data._y;
00231             sensorData.accel_z = accel_data._z;
00232 
00233             sensorData.orientation.vertical =  abs(accel_data._x > 600);
00234 
00235             break;
00236         }
00237     }
00238 
00239     /*
00240      * Trigger a Pressure reading
00241      */
00242     evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
00243                            MPL3115A2::AT_1);
00244 
00245     evbBaro->triggerOneShot();
00246 
00247     /*
00248      * Retrieve and print out barometric pressure
00249      */
00250     for(uint32_t i = 0; i < 10; i++)
00251     {
00252         osDelay(100);           // allows other threads to process
00253         result = evbBaro->getStatus();
00254         if( result & MPL3115A2::PTDR)
00255         {
00256             pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
00257             num_whole = pressure >> 2;          // 18 bit integer significant
00258             num_frac = (pressure & 0x3) * 25;       // 2 bit fractional  0.25 per bit
00259             sensorData.pressure = pressure + (.25 * num_frac);
00260 
00261             sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
00262             evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00263 
00264             break;
00265         }
00266     }
00267 
00268 
00269     /*
00270      * Trigger a Altitude reading
00271      */
00272     evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
00273                            MPL3115A2::AT_1);
00274     evbBaro->triggerOneShot();
00275 
00276 
00277     /*
00278      * Retrieve and print out altitude and temperature
00279      */
00280     for(uint32_t i = 0; i < 10; i++)
00281     {
00282         osDelay(100);           // allows other threads to process
00283         result = evbBaro->getStatus();
00284         if( result & MPL3115A2::PTDR)
00285         {
00286             baro_data = evbBaro->getAllData(false);
00287             baro_data._baro /= 4096;                // convert 32 bit signed to 20 bit signed value
00288             num_whole = baro_data._baro / 16;       //  18 bit signed significant integer
00289             num_frac = (baro_data._baro & 0xF) * 625 / 100;     // 4 bit fractional .0625 per bit
00290             sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac);
00291             evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr));
00292             num_whole = baro_data._temp / 16;       // 8 bit signed significant integer
00293             num_frac = (baro_data._temp & 0x0F) * 625 / 100;        // 4 bit fractional .0625 per bit
00294             sensorData.temperature = num_whole  + ((float)num_frac / 100);
00295             sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac);
00296             evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr));
00297 
00298             break;
00299         }
00300     }
00301 
00302 
00303     /*
00304      * retrieve and print out Ambient Light level
00305      */
00306     uint16_t lux_data = evbAmbLight->getData();
00307     num_whole = lux_data * 24 / 100;        // 16000 lux full scale .24 lux per bit
00308     num_frac = lux_data * 24 % 100;
00309     sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac );
00310     evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr));
00311 
00312     evbLCD->endUpdate();
00313 
00314     return Board_Ok;
00315 }
00316 
00317 EBoardStatus CBoardEVB::setLED(uint8_t ledNum, bool on)
00318 {
00319     if(ledNum != 1)
00320         return Board_Invalid;
00321 
00322     evbBackLight->setLEDCurrent(on ? 16 : 0);
00323     evbLedState = on;
00324     return Board_Ok;
00325 }
00326 
00327 EBoardStatus CBoardEVB::toggleLED(uint8_t ledNum)
00328 {
00329     if(ledNum != 1)
00330         return Board_Invalid;
00331 
00332     evbLedState = !evbLedState;
00333     evbBackLight->setLEDCurrent(evbLedState ? 16 : 0);
00334     return Board_Ok;
00335 }
00336 
00337 void CBoardEVB::pb1ISR(void)
00338 {
00339     if (!pb1_low)
00340         pb1_low = true;
00341 }
00342 
00343 void CBoardEVB::pb1_debounce()
00344 {
00345     static uint8_t count = 0;
00346 
00347     while (true) {
00348         if (pb1_low && (mDot08 == 0))
00349             count++;
00350         else {
00351             count = 0;
00352             pb1_low = false;
00353         }
00354 
00355         if (count == 5){
00356             if(buttonCallback != 0)
00357                 buttonCallback(1);
00358         }
00359 
00360         Thread::wait(5);
00361     }
00362 }
00363 
00364 void CBoardEVB::pb2ISR(void)
00365 {
00366     if (!pb2_low)
00367         pb2_low = true;
00368 }
00369 
00370 void CBoardEVB::pb2_debounce()
00371 {
00372     static uint8_t count = 0;
00373 
00374     while (true) {
00375 
00376         if (pb2_low && (mDot09 == 0))
00377             count++;
00378         else {
00379             count = 0;
00380             pb2_low = false;
00381         }
00382 
00383         if (count == 5){
00384 
00385             if(buttonCallback != 0)
00386                 buttonCallback(2);
00387 
00388         }
00389         Thread::wait(5);
00390     }
00391  }
00392 
00393 
00394 #endif