1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Drivers for the Freescale MPL115A2 I2C Digital Barometer
10 The MPL115A2 is an I2C absolute pressure sensor, employing a MEMS
11 pressure sensor with a conditioning IC to provide accurate pressure
12 measurements from 50 to 115 kPa.
16 Software License Agreement (BSD License)
18 Copyright (c) 2012, K. Townsend
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23 1. Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
25 2. Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
28 3. Neither the name of the copyright holders nor the
29 names of its contributors may be used to endorse or promote products
30 derived from this software without specific prior written permission.
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
33 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
36 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 /**************************************************************************/
45 #include "core/systick/systick.h"
47 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
48 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
49 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
51 static float _mpl115a2_a0
;
52 static float _mpl115a2_b1
;
53 static float _mpl115a2_b2
;
54 static float _mpl115a2_c12
;
56 static bool _mpl115a2Initialised
= false;
58 /**************************************************************************/
60 @brief Reads an 8 bit value over I2C
62 /**************************************************************************/
63 mpl115a2Error_t
mpl115a2ReadPressureTemp(uint16_t *pressure
, uint16_t *temp
)
65 // Clear write buffers
67 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
69 I2CMasterBuffer
[i
] = 0x00;
70 I2CSlaveBuffer
[i
] = 0x00;
75 I2CMasterBuffer
[0] = MPL115A2_ADDRESS
;
76 I2CMasterBuffer
[1] = MPL115A2_REGISTER_STARTCONVERSION
;
77 I2CMasterBuffer
[2] = 0x00; // Why is this necessary to get results?
80 // Wait a bit for the conversion to complete (3ms max)
85 I2CMasterBuffer
[0] = MPL115A2_ADDRESS
;
86 I2CMasterBuffer
[1] = MPL115A2_REGISTER_PRESSURE_MSB
;
87 I2CMasterBuffer
[2] = MPL115A2_ADDRESS
| MPL115A2_READBIT
;
90 // Shift values to create properly formed integers
91 *pressure
= ((I2CSlaveBuffer
[0] << 8) | (I2CSlaveBuffer
[1])) >> 6;
92 *temp
= ((I2CSlaveBuffer
[2] << 8) | (I2CSlaveBuffer
[3])) >> 6;
94 return MPL115A2_ERROR_OK
;
97 /**************************************************************************/
99 @brief Reads the factory-set coefficients
101 /**************************************************************************/
102 mpl115a2Error_t
mpl115a2ReadCoefficients(void)
109 // Clear write buffers
111 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
113 I2CMasterBuffer
[i
] = 0x00;
114 I2CSlaveBuffer
[i
] = 0x00;
119 I2CMasterBuffer
[0] = MPL115A2_ADDRESS
;
120 I2CMasterBuffer
[1] = MPL115A2_REGISTER_A0_COEFF_MSB
;
121 I2CMasterBuffer
[2] = MPL115A2_ADDRESS
| MPL115A2_READBIT
;
124 a0coeff
= (I2CSlaveBuffer
[0] << 8 ) | I2CSlaveBuffer
[1];
125 b1coeff
= (I2CSlaveBuffer
[2] << 8 ) | I2CSlaveBuffer
[3];
126 b2coeff
= (I2CSlaveBuffer
[4] << 8 ) | I2CSlaveBuffer
[5];
127 c12coeff
= ((I2CSlaveBuffer
[6] << 8 ) | I2CSlaveBuffer
[7]) >> 2;
129 _mpl115a2_a0
= (float)a0coeff
/ 8;
130 _mpl115a2_b1
= (float)b1coeff
/ 8192;
131 _mpl115a2_b2
= (float)b2coeff
/ 16384;
132 _mpl115a2_c12
= (float)c12coeff
/ 4194304;
134 return MPL115A2_ERROR_OK
;
137 /**************************************************************************/
139 @brief Initialises the I2C block
141 /**************************************************************************/
142 mpl115a2Error_t
mpl115a2Init(void)
144 mpl115a2Error_t error
= MPL115A2_ERROR_OK
;
147 if (i2cInit(I2CMASTER
) == false)
149 return MPL115A2_ERROR_I2CINIT
; /* Fatal error */
152 // Coefficients only need to be read once
153 mpl115a2ReadCoefficients();
155 _mpl115a2Initialised
= true;
159 /**************************************************************************/
161 @brief Gets the compensated pressure level in kPa
163 /**************************************************************************/
164 mpl115a2Error_t
mpl115a2GetPressure(float *pressure
)
169 mpl115a2Error_t error
= MPL115A2_ERROR_OK
;
171 // Make sure the coefficients have been read, etc.
172 if (!_mpl115a2Initialised
) mpl115a2Init();
174 // Get raw pressure and temperature settings
175 error
= mpl115a2ReadPressureTemp(&Padc
, &Tadc
);
176 if (error
) return error
;
178 // See datasheet p.6 for evaluation sequence
179 Pcomp
= _mpl115a2_a0
+ (_mpl115a2_b1
+ _mpl115a2_c12
* Tadc
) * Padc
+ _mpl115a2_b2
* Tadc
;
181 // Return pressure as floating point value
182 *pressure
= ((65.0F
/ 1023.0F
)*(float)Pcomp
) + 50;