Port from Avnet's Internet Of Things full WiGo demo: SmartConfig - WebServer - Exosite - Android sensor Fusion App
Dependencies: NVIC_set_all_priorities mbed cc3000_hostdriver_mbedsocket TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger
MPL3115A2.cpp
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "MPL3115A2.h" 00020 00021 #define REG_WHO_AM_I 0x0C // return 0xC4 by default 00022 #define REG_STATUS 0x00 00023 #define REG_CTRL_REG_1 0x26 00024 #define REG_CTRL_REG_3 0x28 00025 #define REG_CTRL_REG_4 0x29 00026 #define REG_CTRL_REG_5 0x2A 00027 #define REG_PRESSURE_MSB 0x01 // 3 byte pressure data 00028 #define REG_ALTIMETER_MSB 0x01 // 3 byte altimeter data 00029 #define REG_TEMP_MSB 0x04 // 2 byte temperature data 00030 #define REG_PT_DATA_CFG 0x13 00031 #define REG_P_TGT_MSB 0x16 00032 #define REG_P_WND_MSB 0x19 00033 00034 // Status flag for data ready. 00035 #define PTDR_STATUS 0x03 // Pressure Altitude and Temperature ready 00036 #define PDR_STATUS 0x02 // Pressure and Altitude data ready 00037 #define TDR_STATUS 0x01 // Temperature data ready 00038 00039 MPL3115A2::MPL3115A2(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { 00040 unsigned char data[2] = {REG_CTRL_REG_1, 0xB8}; 00041 m_i2c.frequency(375000); 00042 writeRegs(data, 2); 00043 data[0] = REG_PT_DATA_CFG; 00044 data[1] = 0x07; 00045 writeRegs(data, 2); 00046 data[0] = REG_CTRL_REG_1; 00047 data[1] = 0xB9; 00048 writeRegs(data, 2); 00049 } 00050 00051 uint8_t MPL3115A2::isDataAvailable( void) 00052 { 00053 unsigned char status; 00054 readRegs( REG_STATUS, &status, 1); 00055 return ((status>>1) & 0x07); 00056 00057 } 00058 00059 uint8_t MPL3115A2::getAltimeterRaw( unsigned char *dt) 00060 { 00061 if ( isDataAvailable() & PDR_STATUS) 00062 { 00063 readRegs( REG_ALTIMETER_MSB, &dt[0], 2); 00064 return 1; 00065 } 00066 else 00067 return 0; 00068 } 00069 00070 uint8_t MPL3115A2::getTemperatureRaw( unsigned char *dt) 00071 { 00072 if ( isDataAvailable() & TDR_STATUS) 00073 { 00074 readRegs( REG_TEMP_MSB, &dt[0], 1); 00075 return 1; 00076 } 00077 else 00078 return 0; 00079 } 00080 00081 void MPL3115A2::readRegs(int addr, uint8_t * data, int len) { 00082 char t[1] = {addr}; 00083 m_i2c.write(m_addr, t, 1, true); 00084 m_i2c.read(m_addr, (char *)data, len); 00085 } 00086 00087 void MPL3115A2::writeRegs(uint8_t * data, int len) { 00088 m_i2c.write(m_addr, (char *)data, len); 00089 }
Generated on Thu Jul 14 2022 00:58:11 by 1.7.2