Sensorian  1.0
C API Reference Guide Library
MPL3115A2.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
29 #include "MPL3115A2.h"
30 #include "i2c.h"
31 
35 
41 {
43  MPL3115A2_WriteByte(PT_DATA_CFG, DREM | PDEFE | TDEFE); //Enable data ready flags for pressure and temperature )
44  MPL3115A2_WriteByte(CTRL_REG1, OS_128 | SBYB); //Set sensor to active state with oversampling ratio 128 (512 ms between samples)
45 }
46 
51 unsigned char MPL3115A2_ID(void)
52 {
53  unsigned char id = MPL3115A2_ReadByte(WHO_AM_I);
54  return id;
55 }
56 
61 unsigned char MPL3115A2_GetMode(void)
62 {
63  unsigned char status = MPL3115A2_ReadByte(SYSMOD);
64  return (status & 0x01) ? 1 : 0;
65 }
66 
72 {
73  unsigned char ctrl_reg = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
74  ctrl_reg &= ~SBYB; //Clear SBYB bit for Standby mode
75  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg); //Put device in Standby mode
76 }
77 
83 {
84  unsigned char tempSetting = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
85  tempSetting |= SBYB; //Set SBYB bit for Active mode
86  MPL3115A2_WriteByte(CTRL_REG1, tempSetting);
87 }
88 
94 {
95  unsigned char ctrl_reg = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
96  ctrl_reg &= ~(SBYB); //Go to Standby mode
97  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg);
98 
99  ctrl_reg = ALT|OS_128; //Set ALT bit to one and enable back Active mode
100  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg);
101 }
102 
108 {
109  char altpbyte[3] = {0x00};
110  MPL3115A2_ReadByteArray(OUT_P_MSB,altpbyte,3); //Read altitude data
111  int m_altitude = altpbyte[0];
112  int c_altitude = altpbyte[1];
113  int l_altitude = altpbyte[2];
114  uint32_t al = (m_altitude << 24)|(c_altitude < 16)| (l_altitude);
115  return (float)(al/65536);
116 }
117 
123 void MPL3115A2_SetAltimeterOffset(unsigned char H_Offset)
124 {
125  if((H_Offset > -128) || (H_Offset < 128))
126  {
127  MPL3115A2_WriteByte(OFF_H,H_Offset);
128  }
129 }
130 
136 {
137  unsigned char ctrl_reg = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
138  ctrl_reg &= ~(SBYB); //Set SBYB to 0 and go to Standby mode
139  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg);
140 
141  ctrl_reg = OS_128 ; //Set ALT bit to zero and enable back Active mode
142  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg);
143 }
144 
150 void MPL3115A2_SetPressureOffset(unsigned char P_Offset)
151 {
152  if((P_Offset > -128) || (P_Offset < 128))
153  {
154  MPL3115A2_WriteByte(OFF_P,P_Offset);
155  }
156 }
157 
163 {
164  char minPressure[3] = {0x00};
165  MPL3115A2_ReadByteArray(P_MIN_MSB,minPressure,3);
166 
167  unsigned char m_altitude = minPressure[0];
168  unsigned char c_altitude = minPressure[1];
169  float l_altitude = (float)(minPressure[2]>>4)/4; //dividing by 4, since two lowest bits are fractional value
170  return((float)(m_altitude<<10 | c_altitude<<2)+l_altitude); //shifting 2 to the left to make room for LSB
171 
172 }
173 
179 {
180  char maxPressure[3] = {0x00};
181  MPL3115A2_ReadByteArray(P_MAX_MSB,maxPressure,3);
182 
183  unsigned char m_altitude = maxPressure[0];
184  unsigned char c_altitude = maxPressure[1];
185  float l_altitude = (float)(maxPressure[2]>>4)/4; //dividing by 4, since two lowest bits are fractional value
186  return((float)(m_altitude<<10 | c_altitude<<2)+l_altitude); //shifting 2 to the left to make room for LSB
187 }
188 
194 {
195  unsigned char barMSB = MPL3115A2_ReadByte(BAR_IN_MSB);
196  unsigned char barLSB = MPL3115A2_ReadByte(BAR_IN_LSB);
197 
198  return ((barMSB << 8) | barLSB);
199 }
200 
206 {
207  unsigned char status = MPL3115A2_ReadByte(STATUS);
208  char pbyte[3] = {0x00};
209 
210  //Check PDR bit, if it's not set then toggle OST
211  if((status & (1<< PDR)) == 0)
212  {
213  MPL3115A2_ToggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading
214  }
215 
217 
218  //this function takes values from the read buffer and converts them to pressure units
219  unsigned long m_altitude = pbyte[0];
220  unsigned long c_altitude = pbyte[1];
221  float l_altitude = (float)(pbyte[2]>>4)/4; //dividing by 4, since two lowest bits are fractional value
222  return((float)(m_altitude<<10 | c_altitude<<2)+l_altitude); //shifting 2 to the left to make room for LSB
223 }
224 
231 {
232  float pressure = MPL3115A2_ReadBarometricPressure();
233 
234  switch (units)
235  {
236  case PSI:
237  return pressure * 0.000145037738;
238  case INHG:
239  return pressure * 0.00029529983071;
240  case MMHG:
241  return pressure * 0.007500615613;
242  }
243  return pressure;
244 }
245 
251 void MPL3115A2_SetPressureAlarmThreshold(unsigned int thresh)
252 {
253  unsigned char threshMSB = (unsigned char) (thresh << 8);
254  unsigned char threshLSB = (unsigned char) thresh;
255  MPL3115A2_WriteByte(P_TGT_MSB,threshMSB);
256  MPL3115A2_WriteByte(P_TGT_LSB,threshLSB);
257 }
258 
265 void MPL3115A2_SetPressureTargetWindow(unsigned int target,unsigned int window)
266 {
267  unsigned char tmpMSB = (unsigned char) (window << 8);
268  unsigned char tmpLSB = (unsigned char) (window);
271 
272  tmpMSB = (unsigned char) (target << 8);
273  tmpLSB = (unsigned char) (target);
276 }
277 
283 {
284  char temperature[2] = {0x00};
285  MPL3115A2_ReadByteArray(OUT_T_MSB,temperature,2);
286 
287  float templsb = (temperature[1]>>4) / 16.0; //temp, fraction of a degree
288  float unpacked = (float)(temperature[0] + templsb);
289 
290  return unpacked;
291 }
292 
298 {
299  char temperature[2] = {0x00};
300  MPL3115A2_ReadByteArray(T_MIN_MSB,temperature,2);
301 
302  float templsb = (temperature[1]>>4) / 16.0; //temp, fraction of a degree
303  float minTemp = (float)(temperature[0] + templsb);
304 
305  return minTemp;
306 }
307 
313 {
314  char temperature[2] = {0x00};
315  MPL3115A2_ReadByteArray(T_MAX_MSB,temperature,2);
316 
317  float templsb = (temperature[1]>>4) / 16.0; //temp, fraction of a degree
318  float maxTemp = (float)(temperature[0] + templsb);
319 
320  return maxTemp;
321 }
322 
329 void MPL3115A2_SetTempTargetWindow(unsigned int target,unsigned int window)
330 {
331  MPL3115A2_WriteByte(T_TGT,target);
332  MPL3115A2_WriteByte(T_WND,window);
333 }
334 
340 void MPL3115A2_SetTemperatureThreshold(unsigned char thresh)
341 {
342  MPL3115A2_WriteByte(CTRL_REG4,INT_EN_TTH); //enable temperature interrupt
343  MPL3115A2_WriteByte(CTRL_REG5,INT_EN_TTH); //map to interrupt
344 }
345 
351 void MPL3115A2_SetTempOffset(char T_Offset)
352 {
353  MPL3115A2_WriteByte(OFF_T,T_Offset);
354 }
355 
361 void MPL3115A2_OutputSampleRate(unsigned char sampleRate)
362 {
363  unsigned char ctrl_reg = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
364  ctrl_reg &= ~(1 << SBYB);
365  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg); //Put sensor in Standby mode
366 
367  if(sampleRate > 7) sampleRate = 7; //OS cannot be larger than 0b.0111
368  sampleRate <<= 3; //Align it for the CTRL_REG1 register
369 
370  ctrl_reg = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
371  ctrl_reg |= sampleRate; //Mask in new Output Sample bits
372  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg); //Update sample rate settings
373 }
374 
380 void MPL3115A2_SetAcquisitionTimeStep(unsigned char ST_Value)
381 {
382  unsigned char ctrl_reg = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
383  ctrl_reg &= ~(1 << SBYB);
384  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg); //Put sensor in Standby mode
385 
386  if (ST_Value <= 0xF)
387  {
388  ctrl_reg = MPL3115A2_ReadByte(CTRL_REG2); //Read current settings
389  MPL3115A2_WriteByte(CTRL_REG1, ctrl_reg|ST_Value);
390  }
391 }
392 
399 {
400  MPL3115A2_WriteByte(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags
401 }
402 
408 {
409  unsigned char tempSetting = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings
410  tempSetting &= ~(1<<1); //Clear OST bit
411  MPL3115A2_WriteByte(CTRL_REG1, tempSetting);
412  tempSetting = MPL3115A2_ReadByte(CTRL_REG1); //Read current settings to be safe
413  tempSetting |= (1<<1); //Set OST bit
414  MPL3115A2_WriteByte(CTRL_REG1, tempSetting);
415 }
416 
423 void MPL3115A2_ConfigureInterruptPin(unsigned char intrrpt,unsigned char pin)
424 {
425  MPL3115A2_WriteByte(CTRL_REG5,(pin << intrrpt));
426 }
427 
433 {
434  MPL3115A2_WriteByte(CTRL_REG3,PP_OD1|PP_OD2); //Configure Interrupt pins for open drain active low
435 }
436 
442 {
443  MPL3115A2_WriteByte(CTRL_REG3,PP_OD1|PP_OD2); //Configure Interrupt pins for open drain active low
444 }
445 
451 {
453  MPL3115A2_ReadByte(OUT_P_CSB); //Configure Interrupt pins for open drain active low
458 }
459 
461 
467 unsigned char MPL3115A2_ReadByte(char reg)
468 {
469  return I2C_ReadByteRegister(reg); //Read register current value
470 }
471 
479 void MPL3115A2_ReadByteArray(char reg,char *buffer, unsigned int length)
480 {
481  I2C_ReadByteArray(reg,buffer, length);
482 }
483 
490 void MPL3115A2_WriteByte(char reg, char value)
491 {
492  I2C_WriteByteRegister(reg,value); //Write value to register
493 }
494 
502 void MPL3115A2_WriteByteArray(char reg, char* buffer, unsigned int length)
503 {
504  I2C_WriteByteArray(reg,buffer,length); //Write value to register
505 }
float MPL3115A2_ReadPressure(unitsType units)
Get a barometric pressure reading.
Definition: MPL3115A2.c:230
#define ALT
Definition: MPL3115A2.h:99
#define P_TGT_LSB
Definition: MPL3115A2.h:65
#define F_STATUS
Definition: MPL3115A2.h:55
unitsType
Definition: MPL3115A2.h:188
void MPL3115A2_SetPressureAlarmThreshold(unsigned int thresh)
Configure alarm threshold.
Definition: MPL3115A2.c:251
unsigned char buffer[256]
Definition: main.c:74
#define OFF_P
Definition: MPL3115A2.h:85
#define CTRL_REG3
Definition: MemoryMap.h:70
#define BAR_IN_LSB
Definition: MPL3115A2.h:63
unsigned char MPL3115A2_ID(void)
Returns the Factory Chip ID.
Definition: MPL3115A2.c:51
#define P_MAX_MSB
Definition: MPL3115A2.h:75
void MPL3115A2_SetTempTargetWindow(unsigned int target, unsigned int window)
Configures the temperature target and window for the interrupts.
Definition: MPL3115A2.c:329
#define OFF_H
Definition: MPL3115A2.h:87
#define T_MAX_MSB
Definition: MPL3115A2.h:78
#define OUT_T_MSB
Definition: MPL3115A2.h:46
#define PDR
Definition: MPL3115A2.h:94
void MPL3115A2_WriteByteArray(char reg, char *buffer, unsigned int length)
Writes an array of bytes to the sensor.
Definition: MPL3115A2.c:502
#define PP_OD1
Definition: MPL3115A2.h:134
#define SYSMOD
Definition: MemoryMap.h:41
#define PP_OD2
Definition: MPL3115A2.h:136
float MPL3115A2_ReadTemperature(void)
Get a temperature reading from the sensor.
Definition: MPL3115A2.c:282
#define OUT_P_LSB
Definition: MPL3115A2.h:45
void MPL3115A2_WriteByte(char reg, char value)
Write a single byte to the register.
Definition: MPL3115A2.c:490
float MPL3115A2_ReadAltitude(void)
Returns the number of meters above sea level,Returns -1 if no new data is available.
Definition: MPL3115A2.c:107
Definition: MPL3115A2.h:188
void MPL3115A2_BarometerMode(void)
Configure the sensor in Barometer mode.
Definition: MPL3115A2.c:135
void MPL3115A2_ConfigurePressureInterrupt(void)
Configure Pressure Interrupt.
Definition: MPL3115A2.c:432
#define TDEFE
Definition: MPL3115A2.h:173
#define STATUS
Definition: MemoryMap.h:32
void MPL3115A2_ClearInterrupts(void)
Clear any existing interrupts.
Definition: MPL3115A2.c:450
void MPL3115A2_ToggleOneShot(void)
Causes the sensor to immediately take another reading , needed to sample faster than 1Hz...
Definition: MPL3115A2.c:407
void I2C_WriteByteArray(char reg, char *data, unsigned int length)
Writes a buffer array to the registers.
Definition: i2c.c:100
#define INT_EN_TTH
Definition: MPL3115A2.h:144
#define OUT_P_CSB
Definition: MPL3115A2.h:44
void MPL3115A2_SetPressureOffset(unsigned char P_Offset)
Set the pressure offset correction.
Definition: MPL3115A2.c:150
void MPL3115A2_SetAcquisitionTimeStep(unsigned char ST_Value)
Set the acquisition time step in seconds.
Definition: MPL3115A2.c:380
I2C driver header.
#define WHO_AM_I
Definition: MemoryMap.h:43
#define OS_128
Definition: MPL3115A2.h:116
#define CTRL_REG2
Definition: FXOS8700CQ.h:313
#define CTRL_REG5
Definition: MemoryMap.h:72
float MPL3115A2_GetMinimumPressure(void)
Get minimum stored pressure.
Definition: MPL3115A2.c:162
#define PT_DATA_CFG
Definition: MPL3115A2.h:61
#define P_WND_LSB
Definition: MPL3115A2.h:68
void I2C_WriteByteRegister(unsigned char reg, unsigned char data)
Writes a byte value to a register address.
Definition: i2c.c:66
float MPL3115A2_GetMinimumTemperature(void)
Get minimum recorded temperature reading.
Definition: MPL3115A2.c:297
#define P_WND_MSB
Definition: MPL3115A2.h:67
unsigned char MPL3115A2_GetMode(void)
Return a bool value indicating wheather the sensor is in Active or Standby mode.
Definition: MPL3115A2.c:61
float MPL3115A2_ReadBarometricPressure(void)
Reads the current pressure in Pa.
Definition: MPL3115A2.c:205
#define CTRL_REG4
Definition: MemoryMap.h:71
void MPL3115A2_SetTempOffset(char T_Offset)
Used to correct the measured sensor temeperature by a particular offset.
Definition: MPL3115A2.c:351
void MPL3115A2_ConfigureInterruptPin(unsigned char intrrpt, unsigned char pin)
Configure Interrupt pins.
Definition: MPL3115A2.c:423
void MPL3115A2_Initialize(void)
Initialize the sensor and enable interrupts.
Definition: MPL3115A2.c:40
#define P_TGT_MSB
Definition: MPL3115A2.h:64
void MPL3115A2_SetTemperatureThreshold(unsigned char thresh)
Configure the temeperature threshold.
Definition: MPL3115A2.c:340
void MPL3115A2_EnableEventFlags(void)
Enables the pressure and temp measurement event flags so that we can test against them...
Definition: MPL3115A2.c:398
#define DREM
Definition: MPL3115A2.h:171
#define OUT_P_MSB
Definition: MPL3115A2.h:43
#define P_MIN_MSB
Definition: MPL3115A2.h:70
#define T_MIN_MSB
Definition: MPL3115A2.h:73
void MPL3115A2_ReadByteArray(char reg, char *buffer, unsigned int length)
Read byte value from register.
Definition: MPL3115A2.c:479
MPL3115A2 driver header.
#define CTRL_REG1
Definition: MemoryMap.h:68
#define OFF_T
Definition: MPL3115A2.h:86
void MPL3115A2_AltimeterMode(void)
Sets the mode to Altimeter.
Definition: MPL3115A2.c:93
#define T_WND
Definition: MPL3115A2.h:69
#define PDEFE
Definition: MPL3115A2.h:172
#define BAR_IN_MSB
Definition: MPL3115A2.h:62
void MPL3115A2_SetAltimeterOffset(unsigned char H_Offset)
Set the altimeter offset correction.
Definition: MPL3115A2.c:123
float MPL3115A2_GetMaximumTemperature(void)
Get maximum recorded temperature reading.
Definition: MPL3115A2.c:312
unsigned char I2C_ReadByteRegister(char reg)
Reads a byte from a register.
Definition: i2c.c:122
void MPL3115A2_ActiveMode(void)
Puts the sensor in active mode, needed is the sensor is in standby mode.
Definition: MPL3115A2.c:82
void MPL3115A2_StandbyMode(void)
Puts the sensor in standby mode, the user must do this in order to modify the major control registers...
Definition: MPL3115A2.c:71
#define T_TGT
Definition: MPL3115A2.h:66
void MPL3115A2_SetPressureTargetWindow(unsigned int target, unsigned int window)
Configure alarm target and window.
Definition: MPL3115A2.c:265
void MPL3115A2_OutputSampleRate(unsigned char sampleRate)
Configures the output sample rate.The higher the oversample rate the greater the time between data sa...
Definition: MPL3115A2.c:361
#define OUT_T_LSB
Definition: MPL3115A2.h:47
#define SBYB
Definition: MPL3115A2.h:106
unsigned char MPL3115A2_ReadByte(char reg)
Read byte value from register.
Definition: MPL3115A2.c:467
unsigned int MPL3115A2_ReadBarometicPressureInput(void)
Returns barometric pressure input used for calculating altitude.
Definition: MPL3115A2.c:193
void I2C_ReadByteArray(char reg, char *buffer, unsigned int length)
Initializes the I2C peripheral.
Definition: i2c.c:138
float MPL3115A2_GetMaximumPressure(void)
Get maximum stored pressure.
Definition: MPL3115A2.c:178
void MPL3115A2_ConfigureAltitudeInterrupt(void)
Configure altitude interrupt pin.
Definition: MPL3115A2.c:441