ROM Comm / Mbed 2 deprecated espar_mini_control_CAN

Dependencies:   mbed mbed-STM32F103C8T6

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers spl0601.cpp Source File

spl0601.cpp

00001 /*
00002  MPL3115A2 Barometric Pressure Sensor Library
00003  By: Nathan Seidle
00004  SparkFun Electronics
00005  Date: September 22nd, 2013
00006  License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
00007 
00008  This library allows an Arduino to read from the MPL3115A2 low-cost high-precision pressure sensor.
00009 
00010  If you have feature suggestions or need support please use the github support page: https://github.com/sparkfun/MPL3115A2_Breakout
00011 
00012  Hardware Setup: The MPL3115A2 lives on the I2C bus. Attach the SDA pin to A4, SCL to A5. Use inline 10k resistors
00013  if you have a 5V board. If you are using the SparkFun breakout board you *do not* need 4.7k pull-up resistors
00014  on the bus (they are built-in).
00015 
00016  Link to the breakout board product:
00017 
00018  Software:
00019  .begin() Gets sensor on the I2C bus.
00020  .readAltitude() Returns float with meters above sealevel. Ex: 1638.94
00021  .readAltitudeFt() Returns float with feet above sealevel. Ex: 5376.68
00022  .readPressure() Returns float with barometric pressure in Pa. Ex: 83351.25
00023  .readTemp() Returns float with current temperature in Celsius. Ex: 23.37
00024  .readTempF() Returns float with current temperature in Fahrenheit. Ex: 73.96
00025  .setModeBarometer() Puts the sensor into Pascal measurement mode.
00026  .setModeAltimeter() Puts the sensor into altimetery mode.
00027  .setModeStandy() Puts the sensor into Standby mode. Required when changing CTRL1 register.
00028  .setModeActive() Start taking measurements!
00029  .setOversampleRate(byte) Sets the # of samples from 1 to 128. See datasheet.
00030  .enableEventFlags() Sets the fundamental event flags. Required during setup.
00031 
00032  Updated by PaulZC: October 19th, 2019
00033 
00034  */
00035 #include "spl0601.h"
00036 
00037 
00038 #include "wiced_types.h"
00039 #include "i2c_base.h"
00040 //extern wiced_mutex_t i2c_mutex;
00041 extern char printbuff[256];
00042 extern void DebugWrite(const char* str);
00043 
00044 #define SPL0601_ADR 0x76
00045 
00046 static int i2cfailcount = 0;
00047 
00048 int16_t c0;
00049 int16_t c1;
00050 int32_t c00, c10;
00051 int16_t c01, c11, c20, c21, c30;
00052 
00053 I2C* spl_i2c;
00054 
00055 /*
00056 const wiced_i2c_device_t i2c_SPL0601 =
00057 { .port = WICED_I2C_1, //WICED_I2C_1,
00058         .address = SPL0601_ADR,
00059         .address_width = I2C_ADDRESS_WIDTH_7BIT,
00060         .speed_mode = I2C_HIGH_SPEED_MODE, };
00061 */
00062 
00063 
00064 
00065 void DeInit_SPL0601()
00066 {
00067 
00068     wiced_result_t result;
00069     /* Init I2C master on the master CPU */
00070 //    wiced_rtos_lock_mutex(&i2c_mutex);
00071 //    result = wiced_i2c_deinit(&i2c_SPL0601);
00072 //    wiced_rtos_unlock_mutex(&i2c_mutex);
00073 //   if (result != WICED_SUCCESS)
00074 //    {
00075 //       printf("I2C Init failed\n\r");
00076 //   }
00077 }
00078 void Init_SPL0601()
00079 {
00080 //    wiced_result_t result;
00081     /* Init I2C master on the master CPU */
00082 //    wiced_rtos_lock_mutex(&i2c_mutex);
00083 //    result = wiced_i2c_init(&i2c_SPL0601);
00084 //    wiced_rtos_unlock_mutex(&i2c_mutex);
00085 //    if (result != WICED_SUCCESS)
00086 //    {
00087 //        printf("I2C Init failed\n\r");
00088 //   }
00089 }
00090 
00091 wiced_bool_t getPressure(double* result)
00092 {
00093     wiced_result_t queryresult;
00094     uint8_t buff[20];
00095     double temperature = 0;
00096     double prawsc;
00097 
00098     int tempreg;
00099     double c0d;
00100     int32_t presreg;
00101     double trawsc;
00102     buff[0] = 0x03;
00103     queryresult = writeRegs(PRS_CFG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE);
00104     if (queryresult != WICED_SUCCESS) {
00105         return WICED_FALSE;
00106     }
00107     readRegs(PRS_CFG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00108 
00109 //             sprintf(printbuff, "PRS_CFG is %02X\r\n", buff[0]);
00110 //           DebugWrite(printbuff);
00111 
00112     readRegs(COEF_SRC, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00113 //             sprintf(printbuff, "COEF_SRC is %02X\r\n", buff[0]);
00114 //             DebugWrite(printbuff);
00115 
00116     buff[0] = TEMP_RD_RATE_1_SEC;
00117     writeRegs(TRS_CFG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE);
00118     readRegs(TRS_CFG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00119 //             sprintf(printbuff, "TRS_CFG is %02X\r\n", buff[0]);
00120 //             DebugWrite(printbuff);
00121 
00122     buff[0] = 0;
00123     writeRegs(CFG_REG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE);
00124     readRegs(CFG_REG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00125 //              sprintf(printbuff, "CFG_REG is %02X\r\n", buff[0]);
00126     //            DebugWrite(printbuff);
00127 
00128     buff[0] = MEAS_RATE;
00129     writeRegs(MEAS_CFG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE);
00130     readRegs(MEAS_CFG, buff, 1, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00131 //    sprintf(printbuff, "MEAS_CFG is %02X\r\n", buff[0]);
00132 //    DebugWrite(printbuff);
00133 
00134     readRegs(CAL_C0, buff, 18, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00135 //    sprintf(printbuff, "all cal: is is %02X %02X %02X %02X %02X %02X %02X %02X|%02X %02X %02X %02X %02X %02X %02X %02X|%02X %02X\r\n", buff[0],
00136   //          buff[1], buff[2], buff[3], buff[4], buff[5], buff[6], buff[7], buff[8], buff[9], buff[10], buff[11], buff[12], buff[13], buff[14],
00137     //        buff[15], buff[16], buff[17]);
00138   //  DebugWrite(printbuff);
00139 
00140     c0 = getInt12IntoInt16(buff, WICED_FALSE);
00141     c1 = getInt12IntoInt16(buff + 1, WICED_TRUE);
00142     c00 = getInt20IntoInt32(buff + 3, WICED_FALSE);
00143     c10 = getInt20IntoInt32(buff + 5, WICED_TRUE);
00144     c01 = getInt16(&buff[8]);
00145     c11 = getInt16(&buff[10]);
00146     c20 = getInt16(&buff[12]);
00147     c21 = getInt16(&buff[14]);
00148     c30 = getInt16(&buff[16]);
00149 
00150     // 20 byte register, 2s complement
00151     readRegs(TMP_REG, buff, 3, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00152 
00153     tempreg = getInt24IntoInt32(buff);
00154 
00155     c0d = c0;
00156     c0d = c0d * 0.5;
00157 
00158     // 212530/
00159     trawsc = tempreg;
00160     trawsc = trawsc / 7864320;
00161     temperature = trawsc * c1;
00162 
00163     temperature = c0d + temperature;
00164  //   sprintf(printbuff, "Temperature is %0.1lf\r\n", temperature);
00165    // DebugWrite(printbuff);
00166 
00167     readRegs(PRS_REG, buff, 3, spl_i2c, SPL0601_ADR, &i2cfailcount, WICED_FALSE, WICED_FALSE);
00168 
00169     presreg = getInt24IntoInt32(buff);
00170 
00171     prawsc = presreg;
00172     prawsc = prawsc / 7864320;
00173 
00174     double presComp;
00175     double prestemp;
00176 
00177     //Pcomp(Pa) = c00 +
00178     prestemp = c00;
00179     presComp = prestemp;
00180 
00181     //Praw_sc*(c10 + Praw_sc *(c20+ Praw_sc *c30))
00182     prestemp = c30;
00183     prestemp *= prawsc;
00184     prestemp += c20;
00185     prestemp *= prawsc;
00186     prestemp += c10;
00187     prestemp *= prawsc;
00188     presComp += prestemp;
00189     //+ Traw_sc *c01
00190     prestemp = c01;
00191     prestemp *= trawsc;
00192     presComp += prestemp;
00193     //+ Traw_sc *Praw_sc *(c11+Praw_sc*c21)
00194     prestemp = c21;
00195     prestemp *= prawsc;
00196     prestemp += c11;
00197     prestemp *= prawsc;
00198     prestemp *= trawsc;
00199     presComp += prestemp;
00200     *result = presComp;
00201     return WICED_TRUE;
00202 }
00203 
00204 int16_t getInt16(uint8_t *importval)
00205 {
00206     int16_t result = 0;
00207     for (int i = 0; i < 2; i++) {
00208         result <<= 8;
00209         result |= importval[i];
00210     }
00211     return result;
00212 }
00213 int16_t getInt12IntoInt16(uint8_t* importval, wiced_bool_t leading4Bits)
00214 {
00215     int16_t result = 0;
00216     for (int i = 0; i < 2; i++) {
00217         result <<= 8;
00218         result |= importval[i];
00219     }
00220     if (!leading4Bits) {
00221         result >>= 4; // remove trailing nibble
00222     } else {
00223         result = result & 0x0FFF; // strip leading nibble
00224     }
00225     if ((0x0800 & result) > 0) {
00226         // negate
00227         result |= 0xF000;
00228     }
00229     return result;
00230 }
00231 int32_t getInt20IntoInt32(uint8_t* importval, wiced_bool_t leading4Bits)
00232 {
00233     int32_t result = 0;
00234     for (int i = 0; i < 3; i++) {
00235         result <<= 8;
00236         result |= importval[i];
00237     }
00238     if (!leading4Bits) {
00239         result >>= 4; // remove trailing nibble
00240     } else {
00241         result = result & 0x000FFFFF; // strip leading nibble
00242     }
00243     if ((0x00080000 & result) > 0) {
00244         // negate
00245         result |= 0xFFF00000;
00246     }
00247     return result;
00248 }
00249 int32_t getInt24IntoInt32(uint8_t* importval)
00250 {
00251     int32_t result = 0;
00252     for (int i = 0; i < 3; i++) {
00253         result <<= 8;
00254         result |= importval[i];
00255     }
00256 
00257     if ((0x00800000 & result) > 0) {
00258         // negate
00259         result |= 0xFF000000;
00260     }
00261     return result;
00262 }
00263 
00264