Miguel Luis / lib_mpl3115a2

Dependents:   LoRaWAN-NAMote72-Application-Demo_IoTium LoRaWAN-NAMote72-BVS-confirmed-tester-0-7v1_copy LoRaWAN-NAMote72-Application-Demo-good LoRaWAN-NAMote72-Application-Demo

Fork of lib_mpl3115a2 by wayne roberts

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mpl3115a2.cpp Source File

mpl3115a2.cpp

00001 #include "mpl3115a2.h"
00002  
00003 #define MPL3115A_I2C_ADDRESS                        0xc0  //0x60
00004 
00005 MPL3115A2::MPL3115A2(I2C& r, DigitalIn& int_pin) : m_i2c(r), m_int_pin(int_pin)
00006 {
00007     write(CTRL_REG3, 0x10); // PP_OD1: INT1 to open-drain
00008 }
00009 
00010 MPL3115A2::~MPL3115A2()
00011 {
00012 }
00013 
00014 void MPL3115A2::init()
00015 {
00016     //MPL3115Reset( );
00017     ctrl_reg1.octet = 0;
00018     ctrl_reg1.bits.RST = 1;
00019     write(CTRL_REG1, /*4*/ ctrl_reg1.octet);
00020     wait(0.05);
00021     
00022     do
00023     {   // Wait for the RST bit to clear 
00024         wait(0.01);
00025         ctrl_reg1.octet = read(CTRL_REG1);
00026     } while (ctrl_reg1.octet);
00027    
00028     write( PT_DATA_CFG_REG, 0x07 ); // Enable data flags 
00029     write( CTRL_REG3, 0x11 );       // Open drain, active low interrupts 
00030     write( CTRL_REG4, 0x80 );       // Enable DRDY interrupt 
00031     write( CTRL_REG5, 0x00 );       // DRDY interrupt routed to INT2 - PTD3 
00032     
00033     ctrl_reg1.bits.ALT = 1; // altitude mode
00034     ctrl_reg1.bits.OS = 5;  // OSR = 32
00035     ctrl_reg1.bits.SBYB = 1; // Active
00036     write(CTRL_REG1, ctrl_reg1.octet);       
00037     
00038     SetModeActive( );
00039 }
00040 
00041 void MPL3115A2::setOSR(uint8_t osr)
00042 {
00043     ctrl_reg1.bits.OS = osr;
00044     write(CTRL_REG1, ctrl_reg1.octet);
00045 }
00046 
00047 uint8_t MPL3115A2::getOSR(void)
00048 {
00049     ctrl_reg1.octet = read(CTRL_REG1);
00050     return ctrl_reg1.bits.OS;
00051 }
00052 
00053 bool MPL3115A2::GetModeActive( )
00054 {
00055     ctrl_reg1.octet = read(CTRL_REG1);
00056     return ctrl_reg1.bits.SBYB;
00057 }
00058 
00059 void MPL3115A2::SetModeActive( )
00060 {
00061     ctrl_reg1.bits.SBYB = 1;
00062     write(CTRL_REG1, ctrl_reg1.octet); 
00063 }
00064 
00065 void MPL3115A2::SetModeStandby( )
00066 {
00067     ctrl_reg1.bits.SBYB = 0;
00068     write(CTRL_REG1, ctrl_reg1.octet);     
00069 }
00070 
00071 void MPL3115A2::write(uint8_t a, uint8_t d)
00072 {
00073     char cmd[2];
00074     
00075     cmd[0] = a;
00076     cmd[1] = d;
00077 
00078     if (m_i2c.write(MPL3115A_I2C_ADDRESS, cmd, 2))
00079     {
00080         //printf("MPL write-fail %02x %02x\n", cmd[0], cmd[1]);
00081     }
00082         
00083     if (a == CTRL_REG4)
00084         ctrl_reg4 = d;
00085 }
00086 
00087 uint8_t MPL3115A2::read(uint8_t a)
00088 {
00089     char cmd[2];
00090     
00091     cmd[0] = a;
00092     if (m_i2c.write(MPL3115A_I2C_ADDRESS, cmd, 1, true))
00093     {
00094         //printf("MPL write-fail %02x\n", cmd[0]);
00095     }
00096     if (m_i2c.read(MPL3115A_I2C_ADDRESS, cmd, 1))
00097     {
00098         //printf("MPL read-fail\n");
00099     }
00100         
00101     if (a == CTRL_REG4)
00102         ctrl_reg4 = cmd[0];
00103                       
00104     return cmd[0];
00105 }
00106 
00107 float MPL3115A2::ReadBarometer(void)
00108 {
00109     uint32_t pasc;
00110     volatile uint8_t stat;
00111 
00112     SetModeBarometer();
00113     ToggleOneShot( );
00114        
00115     stat = read(STATUS_REG);       
00116     while( (stat & 0x04) != 0x04 ) {
00117         wait(0.01);   
00118         stat = read(STATUS_REG);
00119     }
00120     
00121     pasc = read(OUT_P_MSB_REG);
00122     pasc <<= 8;
00123     pasc |= read(OUT_P_CSB_REG);
00124     pasc <<= 8;
00125     pasc |= read(OUT_P_LSB_REG);
00126     
00127     return pasc / 64.0;
00128 }
00129 
00130 float MPL3115A2::ReadAltitude( void )
00131 {
00132     uint8_t counter = 0;
00133     uint8_t val = 0;
00134     uint8_t msb = 0, csb = 0, lsb = 0;
00135     float decimal = 0;
00136 
00137     /*if( MPL3115Initialized == false )
00138     {
00139         return 0;
00140     }*/
00141 
00142     SetModeAltimeter( );
00143     ToggleOneShot( );
00144 
00145     while( ( val & 0x04 ) != 0x04 )
00146     {    
00147         val = read( STATUS_REG);
00148         wait(0.01); //DelayMs( 10 );
00149         counter++;
00150     
00151         if( counter > 20 )
00152         {    
00153             //MPL3115Initialized = false;
00154             init( );
00155             SetModeAltimeter( );
00156             ToggleOneShot( );
00157             counter = 0;
00158             while( ( val & 0x04 ) != 0x04 )
00159             {  
00160                 val = read( STATUS_REG);
00161                 wait(0.01); //DelayMs( 10 );
00162                 counter++;
00163                 if( counter > 20 )
00164                 {
00165                     write( CTRL_REG4, 0x00 );
00166                     return( 0 ); //Error out after max of 512ms for a read
00167                 }
00168             }
00169         }
00170     }
00171 
00172     msb = read( OUT_P_MSB_REG); // High byte of integer part of altitude,  
00173     csb = read( OUT_P_CSB_REG); // Low byte of integer part of altitude 
00174     lsb = read( OUT_P_LSB_REG); // Decimal part of altitude in bits 7-4
00175     
00176     decimal = ( ( float )( lsb >> 4 ) ) / 16.0;
00177     //Altitude = ( float )( ( msb << 8 ) | csb ) + decimal;
00178     Altitude = ( float )( ( int16_t )( ( msb << 8 ) | csb ) ) + decimal;
00179     
00180     write( CTRL_REG4, 0x00 );
00181 
00182     return( Altitude );
00183 }
00184 
00185 void MPL3115A2::SetModeAltimeter( void )
00186 {
00187     SetModeStandby( );
00188 
00189     ctrl_reg1.bits.ALT = 1;
00190     write(CTRL_REG1, ctrl_reg1.octet); 
00191     
00192     SetModeActive( );
00193 }
00194 
00195 void MPL3115A2::SetModeBarometer(void)
00196 {
00197     SetModeStandby( );
00198 
00199     ctrl_reg1.bits.ALT = 0;
00200     write(CTRL_REG1, ctrl_reg1.octet); 
00201     
00202     SetModeActive( );    
00203 }
00204 
00205 void MPL3115A2::ToggleOneShot( void )
00206 {
00207     SetModeStandby( );
00208     
00209     ctrl_reg1.bits.OST = 0;
00210     write(CTRL_REG1, ctrl_reg1.octet); 
00211     
00212     ctrl_reg1.bits.OST = 1;
00213     write(CTRL_REG1, ctrl_reg1.octet); 
00214 
00215     SetModeActive( );
00216 }
00217 
00218 float MPL3115A2::ReadTemperature( void )
00219 {
00220     uint8_t counter = 0;
00221     bool negSign = false;
00222     uint8_t val = 0;
00223     uint8_t msb = 0, lsb = 0;
00224 
00225     /*if( MPL3115Initialized == false )
00226     {
00227         return 0;
00228     }*/
00229 
00230     ToggleOneShot( );
00231 
00232     while( ( val & 0x02 ) != 0x02 )
00233     {    
00234         val = read( STATUS_REG);
00235         wait(0.01);
00236         counter++;
00237     
00238         if( counter > 20 )
00239         { 
00240             //MPL3115Initialized = false;
00241             init( );
00242             ToggleOneShot( );
00243             counter = 0;
00244             while( ( val & 0x02 ) != 0x02 )
00245             {
00246                 val = read( STATUS_REG);
00247                 wait(0.01);
00248                 counter++;
00249             
00250                 if( counter > 20 )
00251                 { 
00252                     write( CTRL_REG4, 0x00 );
00253                     return( 0 ); //Error out after max of 512ms for a read
00254                 }
00255             }
00256                 
00257         }
00258     }
00259 
00260     msb = read( OUT_T_MSB_REG); // Integer part of temperature 
00261     lsb = read( OUT_T_LSB_REG); // Decimal part of temperature in bits 7-4
00262 
00263     if( msb > 0x7F )
00264     {
00265         val = ~( ( msb << 8 ) + lsb ) + 1;  //2?s complement
00266         msb = val >> 8;
00267         lsb = val & 0x00F0; 
00268         negSign = true;
00269     }
00270 
00271     if( negSign == true )
00272     {
00273         Temperature = 0 - ( msb + ( float )( ( lsb >> 4 ) / 16.0 ) );
00274     }
00275     else
00276     {
00277         Temperature = msb + ( float )( ( lsb >> 4 ) / 16.0 );
00278     }
00279 
00280     ToggleOneShot( );
00281 
00282     write( CTRL_REG4, 0x00 );
00283 
00284     return( Temperature );
00285 }
00286 
00287 void MPL3115A2::service()
00288 {
00289     mpl_int_source_t int_src;
00290     
00291     if ((ctrl_reg4 == 0x00) || m_int_pin)   // if no interrupts enabled and no interrupt occuring
00292         return;
00293         
00294     int_src.octet = read(INT_SOURCE_REG);
00295     
00296     if (int_src.bits.SRC_TCHG) {
00297     }
00298     if (int_src.bits.SRC_PCHG) {
00299     }
00300     if (int_src.bits.SRC_TTH) {
00301     }
00302     if (int_src.bits.SRC_PTH) {
00303     }
00304     if (int_src.bits.SRC_TW) {
00305     }
00306     if (int_src.bits.SRC_PW) {
00307     }
00308     if (int_src.bits.SRC_FIFO) {
00309         read(F_STATUS_REG);
00310     }
00311     if (int_src.bits.SRC_DRDY) {
00312         read(STATUS_REG);
00313         
00314         read( OUT_T_MSB_REG); // Integer part of temperature 
00315         read( OUT_T_LSB_REG); // Decimal part of temperature in bits 7-4
00316         
00317         read( OUT_P_MSB_REG); // High byte of integer part of altitude,  
00318         read( OUT_P_CSB_REG); // Low byte of integer part of altitude 
00319         read( OUT_P_LSB_REG); // Decimal part of altitude in bits 7-4              
00320     }
00321             
00322 }