MPL3115A2 driver

Dependents:   lmic_NAmote_GPS_tjm lmic_NAmote_GPS_tjm Senet NAMote scpi_sx127x ... more

Committer:
dudmuck
Date:
Mon Aug 31 21:37:41 2015 +0000
Revision:
2:0eb8b0ad292b
Parent:
1:3cd1f21925e8
correct altitude reading

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudmuck 0:6bba2efea51e 1 #include "mbed.h"
dudmuck 0:6bba2efea51e 2
dudmuck 0:6bba2efea51e 3 /*
dudmuck 0:6bba2efea51e 4 * MPL3115A2 Registers
dudmuck 0:6bba2efea51e 5 */
dudmuck 0:6bba2efea51e 6 #define STATUS_REG 0x00 // STATUS Register
dudmuck 0:6bba2efea51e 7
dudmuck 0:6bba2efea51e 8 #define OUT_P_MSB_REG 0x01 // Bits 12-19 of 20-bit real-time Pressure sample
dudmuck 0:6bba2efea51e 9 #define OUT_P_CSB_REG 0x02 // Bits 4-11 of 20-bit real-time Pressure sample
dudmuck 0:6bba2efea51e 10 #define OUT_P_LSB_REG 0x03 // Bits 0-3 of 20-bit real-time Pressure sample
dudmuck 0:6bba2efea51e 11 #define OUT_T_MSB_REG 0x04 // Bits 4-11 of 12-bit real-time Temperature sample
dudmuck 0:6bba2efea51e 12 #define OUT_T_LSB_REG 0x05 // Bits 0-3 of 12-bit real-time Temperature sample
dudmuck 0:6bba2efea51e 13
dudmuck 0:6bba2efea51e 14 #define DR_STATUS 0x06 // Data Ready status information
dudmuck 0:6bba2efea51e 15
dudmuck 0:6bba2efea51e 16 #define OUT_P_DELTA_MSB_REG 0x07 // Bits 12-19 of 20-bit Pressure change data
dudmuck 0:6bba2efea51e 17 #define OUT_P_DELTA_CSB_REG 0x08 // Bits 4-11 of 20-bit Pressure change data
dudmuck 0:6bba2efea51e 18 #define OUT_P_DELTA_LSB_REG 0x09 // Bits 0-3 of 20-bit Pressure change data
dudmuck 0:6bba2efea51e 19 #define OUT_T_DELTA_MSB_REG 0x0A // Bits 4-11 of 12-bit Temperature change data
dudmuck 0:6bba2efea51e 20 #define OUT_T_DELTA_LSB_REG 0x0B // Bits 0-3 of 12-bit Temperature change data
dudmuck 0:6bba2efea51e 21
dudmuck 0:6bba2efea51e 22 #define MPL3115_ID 0x0C // Fixed Device ID Number = 0xC4
dudmuck 0:6bba2efea51e 23
dudmuck 0:6bba2efea51e 24 #define F_STATUS_REG 0x0D // FIFO Status
dudmuck 0:6bba2efea51e 25 #define F_DATA_REG 0x0E // FIFO 8-bit data access
dudmuck 0:6bba2efea51e 26 #define F_SETUP_REG 0x0F // FIFO setup
dudmuck 0:6bba2efea51e 27 #define TIME_DLY_REG 0x10 // Time since FIFO overflow
dudmuck 0:6bba2efea51e 28
dudmuck 0:6bba2efea51e 29 #define SYSMOD_REG 0x11 // Current system mode
dudmuck 0:6bba2efea51e 30 #define INT_SOURCE_REG 0x12 // Interrupt status
dudmuck 0:6bba2efea51e 31 #define PT_DATA_CFG_REG 0x13 // Data event flag configuration
dudmuck 0:6bba2efea51e 32
dudmuck 0:6bba2efea51e 33 #define BAR_IN_MSB_REG 0x14 // Barometric input for Altitude calculation bits 8-15
dudmuck 0:6bba2efea51e 34 #define BAR_IN_LSB_REG 0x15 // Barometric input for Altitude calculation bits 0-7
dudmuck 0:6bba2efea51e 35
dudmuck 0:6bba2efea51e 36 #define P_TGT_MSB_REG 0x16 // Pressure/Altitude target value bits 8-15
dudmuck 0:6bba2efea51e 37 #define P_TGT_LSB_REG 0x17 // Pressure/Altitude target value bits 0-7
dudmuck 0:6bba2efea51e 38 #define T_TGT_REG 0x18 // Temperature target value
dudmuck 0:6bba2efea51e 39
dudmuck 0:6bba2efea51e 40 #define P_WND_MSB_REG 0x19 // Pressure/Altitude window value bits 8-15
dudmuck 0:6bba2efea51e 41 #define P_WND_LSB_REG 0x1A // Pressure/Altitude window value bits 0-7
dudmuck 0:6bba2efea51e 42 #define T_WND_REG 0x1B // Temperature window value
dudmuck 0:6bba2efea51e 43
dudmuck 0:6bba2efea51e 44 #define P_MIN_MSB_REG 0x1C // Minimum Pressure/Altitude bits 12-19
dudmuck 0:6bba2efea51e 45 #define P_MIN_CSB_REG 0x1D // Minimum Pressure/Altitude bits 4-11
dudmuck 0:6bba2efea51e 46 #define P_MIN_LSB_REG 0x1E // Minimum Pressure/Altitude bits 0-3
dudmuck 0:6bba2efea51e 47 #define T_MIN_MSB_REG 0x1F // Minimum Temperature bits 8-15
dudmuck 0:6bba2efea51e 48 #define T_MIN_LSB_REG 0x20 // Minimum Temperature bits 0-7
dudmuck 0:6bba2efea51e 49
dudmuck 0:6bba2efea51e 50 #define P_MAX_MSB_REG 0x21 // Maximum Pressure/Altitude bits 12-19
dudmuck 0:6bba2efea51e 51 #define P_MAX_CSB_REG 0x22 // Maximum Pressure/Altitude bits 4-11
dudmuck 0:6bba2efea51e 52 #define P_MAX_LSB_REG 0x23 // Maximum Pressure/Altitude bits 0-3
dudmuck 0:6bba2efea51e 53 #define T_MAX_MSB_REG 0x24 // Maximum Pressure/Altitude bits 8-15
dudmuck 0:6bba2efea51e 54 #define T_MAX_LSB_REG 0x25 // Maximum Pressure/Altitude bits 0-7
dudmuck 0:6bba2efea51e 55
dudmuck 0:6bba2efea51e 56 #define CTRL_REG1 0x26 // CTRL_REG1 System Control 1 Register
dudmuck 0:6bba2efea51e 57 #define CTRL_REG2 0x27 // CTRL_REG2 System Control 2 Register
dudmuck 0:6bba2efea51e 58 #define CTRL_REG3 0x28 // CTRL_REG3 Interrupt Configuration Register
dudmuck 0:6bba2efea51e 59 #define CTRL_REG4 0x29 // CTRL_REG4 Interrupt Enable Register
dudmuck 0:6bba2efea51e 60 #define CTRL_REG5 0x2A // CTRL_REG5 Interrupt Output Pin Assignment Register
dudmuck 0:6bba2efea51e 61
dudmuck 0:6bba2efea51e 62 #define OFF_P_REG 0x2B // Pressure data offset
dudmuck 0:6bba2efea51e 63 #define OFF_T_REG 0x2C // Temperature data offset
dudmuck 0:6bba2efea51e 64 #define OFF_H_REG 0x2D // Altitude data offset
dudmuck 0:6bba2efea51e 65
dudmuck 2:0eb8b0ad292b 66
dudmuck 2:0eb8b0ad292b 67 typedef union {
dudmuck 2:0eb8b0ad292b 68 struct { // at 0x26
dudmuck 2:0eb8b0ad292b 69 uint8_t SBYB : 1; // 0 0==standby, 1=active
dudmuck 2:0eb8b0ad292b 70 uint8_t OST : 1; // 1 initiate measurement now
dudmuck 2:0eb8b0ad292b 71 uint8_t RST : 1; // 2 software reset
dudmuck 2:0eb8b0ad292b 72 uint8_t OS : 3; // 3,4,5 oversampling ratio
dudmuck 2:0eb8b0ad292b 73 uint8_t RAW : 1; // 6 raw output mode
dudmuck 2:0eb8b0ad292b 74 uint8_t ALT : 1; // 7 0=barometer 1=altitude
dudmuck 2:0eb8b0ad292b 75 } bits;
dudmuck 2:0eb8b0ad292b 76 uint8_t octet;
dudmuck 2:0eb8b0ad292b 77 } mpl_ctrl_reg1_t;
dudmuck 2:0eb8b0ad292b 78
dudmuck 1:3cd1f21925e8 79 typedef union {
dudmuck 1:3cd1f21925e8 80 struct { // at 0x
dudmuck 1:3cd1f21925e8 81 uint8_t SRC_TCHG : 1; // 0
dudmuck 1:3cd1f21925e8 82 uint8_t SRC_PCHG : 1; // 1
dudmuck 1:3cd1f21925e8 83 uint8_t SRC_TTH : 1; // 2
dudmuck 1:3cd1f21925e8 84 uint8_t SRC_PTH : 1; // 3
dudmuck 1:3cd1f21925e8 85 uint8_t SRC_TW : 1; // 4
dudmuck 1:3cd1f21925e8 86 uint8_t SRC_PW : 1; // 5
dudmuck 1:3cd1f21925e8 87 uint8_t SRC_FIFO : 1; // 6
dudmuck 1:3cd1f21925e8 88 uint8_t SRC_DRDY : 1; // 7
dudmuck 1:3cd1f21925e8 89 } bits;
dudmuck 1:3cd1f21925e8 90 uint8_t octet;
dudmuck 1:3cd1f21925e8 91 } mpl_int_source_t;
dudmuck 1:3cd1f21925e8 92
dudmuck 0:6bba2efea51e 93 class MPL3115A2 {
dudmuck 0:6bba2efea51e 94 public:
dudmuck 1:3cd1f21925e8 95 MPL3115A2(I2C& r, DigitalIn& int_pin);
dudmuck 0:6bba2efea51e 96 ~MPL3115A2();
dudmuck 0:6bba2efea51e 97 void write(uint8_t reg_addr, uint8_t reg_value);
dudmuck 0:6bba2efea51e 98 void init(void);
dudmuck 0:6bba2efea51e 99 uint8_t read(uint8_t a);
dudmuck 0:6bba2efea51e 100 void SetModeActive(void);
dudmuck 1:3cd1f21925e8 101 bool GetModeActive(void);
dudmuck 0:6bba2efea51e 102 void SetModeStandby(void);
dudmuck 2:0eb8b0ad292b 103 float ReadAltitude( void ); // returns meters above sea level
dudmuck 2:0eb8b0ad292b 104 float ReadBarometer(void); // returns pascals
dudmuck 2:0eb8b0ad292b 105 float ReadTemperature( void ); // returns celcius
dudmuck 0:6bba2efea51e 106 void SetModeAltimeter(void);
dudmuck 2:0eb8b0ad292b 107 void SetModeBarometer(void);
dudmuck 0:6bba2efea51e 108 void ToggleOneShot( void );
dudmuck 2:0eb8b0ad292b 109 void setOSR(uint8_t);
dudmuck 2:0eb8b0ad292b 110 uint8_t getOSR(void);
dudmuck 0:6bba2efea51e 111 float Altitude;
dudmuck 0:6bba2efea51e 112 float Temperature;
dudmuck 1:3cd1f21925e8 113 void service(void);
dudmuck 2:0eb8b0ad292b 114
dudmuck 2:0eb8b0ad292b 115 mpl_ctrl_reg1_t ctrl_reg1;
dudmuck 0:6bba2efea51e 116
dudmuck 0:6bba2efea51e 117 private:
dudmuck 0:6bba2efea51e 118 I2C& m_i2c;
dudmuck 1:3cd1f21925e8 119 DigitalIn& m_int_pin;
dudmuck 1:3cd1f21925e8 120 uint8_t ctrl_reg4;
dudmuck 0:6bba2efea51e 121 };
dudmuck 0:6bba2efea51e 122
dudmuck 0:6bba2efea51e 123