First commit
authorKevin Townsend <kevin@ktownsend.com>
Wed, 20 Jun 2012 02:02:41 +0000 (04:02 +0200)
committerKevin Townsend <kevin@ktownsend.com>
Wed, 20 Jun 2012 02:02:41 +0000 (04:02 +0200)
drivers/sensors/mpl115a2/mpl115a2.c [new file with mode: 0644]
drivers/sensors/mpl115a2/mpl115a2.h [new file with mode: 0644]

diff --git a/drivers/sensors/mpl115a2/mpl115a2.c b/drivers/sensors/mpl115a2/mpl115a2.c
new file mode 100644 (file)
index 0000000..c815f14
--- /dev/null
@@ -0,0 +1,185 @@
+/**************************************************************************/
+/*! 
+    @file     mpl115a2.c
+    @author   K. Townsend (microBuilder.eu)
+       
+    @brief    Drivers for the Freescale MPL115A2 I2C Digital Barometer
+
+    @section DESCRIPTION
+
+    The MPL115A2 is an I2C absolute pressure sensor, employing a MEMS
+    pressure sensor with a conditioning IC to provide accurate pressure
+    measurements from 50 to 115 kPa.
+
+    @section LICENSE
+
+    Software License Agreement (BSD License)
+
+    Copyright (c) 2012, K. Townsend
+    All rights reserved.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+    1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+    3. Neither the name of the copyright holders nor the
+    names of its contributors may be used to endorse or promote products
+    derived from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
+    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/**************************************************************************/
+#include "mpl115a2.h"
+#include "core/systick/systick.h"
+
+extern volatile uint8_t   I2CMasterBuffer[I2C_BUFSIZE];
+extern volatile uint8_t   I2CSlaveBuffer[I2C_BUFSIZE];
+extern volatile uint32_t  I2CReadLength, I2CWriteLength;
+
+static float _mpl115a2_a0;
+static float _mpl115a2_b1;
+static float _mpl115a2_b2;
+static float _mpl115a2_c12;
+
+static bool _mpl115a2Initialised = false;
+
+/**************************************************************************/
+/*! 
+    @brief  Reads an 8 bit value over I2C
+*/
+/**************************************************************************/
+mpl115a2Error_t mpl115a2ReadPressureTemp(uint16_t *pressure, uint16_t *temp)
+{
+  // Clear write buffers
+  uint32_t i;
+  for ( i = 0; i < I2C_BUFSIZE; i++ )
+  {
+    I2CMasterBuffer[i] = 0x00;
+    I2CSlaveBuffer[i] = 0x00;
+  }
+
+  I2CWriteLength = 3;
+  I2CReadLength = 1;
+  I2CMasterBuffer[0] = MPL115A2_ADDRESS;
+  I2CMasterBuffer[1] = MPL115A2_REGISTER_STARTCONVERSION;
+  I2CMasterBuffer[2] = 0x00;  // Why is this necessary to get results?
+  i2cEngine();
+
+  // Wait a bit for the conversion to complete (3ms max)
+  systickDelay(5);
+
+  I2CWriteLength = 2;
+  I2CReadLength = 4;
+  I2CMasterBuffer[0] = MPL115A2_ADDRESS;
+  I2CMasterBuffer[1] = MPL115A2_REGISTER_PRESSURE_MSB;
+  I2CMasterBuffer[2] = MPL115A2_ADDRESS | MPL115A2_READBIT;
+  i2cEngine();
+
+  // Shift values to create properly formed integers
+  *pressure = ((I2CSlaveBuffer[0] << 8) | (I2CSlaveBuffer[1])) >> 6;
+  *temp = ((I2CSlaveBuffer[2] << 8) | (I2CSlaveBuffer[3])) >> 6;
+
+  return MPL115A2_ERROR_OK;
+}
+
+/**************************************************************************/
+/*! 
+    @brief  Reads the factory-set coefficients
+*/
+/**************************************************************************/
+mpl115a2Error_t mpl115a2ReadCoefficients(void)
+{
+  int16_t a0coeff;
+  int16_t b1coeff;
+  int16_t b2coeff;
+  int16_t c12coeff;
+
+  // Clear write buffers
+  uint32_t i;
+  for ( i = 0; i < I2C_BUFSIZE; i++ )
+  {
+    I2CMasterBuffer[i] = 0x00;
+    I2CSlaveBuffer[i] = 0x00;
+  }
+
+  I2CWriteLength = 2;
+  I2CReadLength = 8;
+  I2CMasterBuffer[0] = MPL115A2_ADDRESS;
+  I2CMasterBuffer[1] = MPL115A2_REGISTER_A0_COEFF_MSB;
+  I2CMasterBuffer[2] = MPL115A2_ADDRESS | MPL115A2_READBIT;  
+  i2cEngine();
+
+  a0coeff = (I2CSlaveBuffer[0] << 8 ) | I2CSlaveBuffer[1];
+  b1coeff = (I2CSlaveBuffer[2] << 8 ) | I2CSlaveBuffer[3];
+  b2coeff = (I2CSlaveBuffer[4] << 8 ) | I2CSlaveBuffer[5];
+  c12coeff = ((I2CSlaveBuffer[6] << 8 ) | I2CSlaveBuffer[7]) >> 2; 
+
+  _mpl115a2_a0 = (float)a0coeff / 8;
+  _mpl115a2_b1 = (float)b1coeff / 8192;
+  _mpl115a2_b2 = (float)b2coeff / 16384;
+  _mpl115a2_c12 = (float)c12coeff / 4194304;
+
+  return MPL115A2_ERROR_OK;
+}
+
+/**************************************************************************/
+/*! 
+    @brief  Initialises the I2C block
+*/
+/**************************************************************************/
+mpl115a2Error_t mpl115a2Init(void)
+{
+  mpl115a2Error_t error = MPL115A2_ERROR_OK;
+
+  // Initialise I2C
+  if (i2cInit(I2CMASTER) == false)
+  {
+    return MPL115A2_ERROR_I2CINIT;    /* Fatal error */
+  }  
+
+  // Coefficients only need to be read once
+  mpl115a2ReadCoefficients();
+
+  _mpl115a2Initialised = true;
+  return error;
+}
+
+/**************************************************************************/
+/*! 
+    @brief  Gets the compensated pressure level in kPa
+*/
+/**************************************************************************/
+mpl115a2Error_t mpl115a2GetPressure(float *pressure)
+{
+  uint16_t  Padc, Tadc;
+  float     Pcomp;
+
+  mpl115a2Error_t error = MPL115A2_ERROR_OK;
+
+  // Make sure the coefficients have been read, etc.
+  if (!_mpl115a2Initialised) mpl115a2Init();
+
+  // Get raw pressure and temperature settings
+  error = mpl115a2ReadPressureTemp(&Padc, &Tadc);
+  if (error) return error;
+
+  // See datasheet p.6 for evaluation sequence
+  Pcomp = _mpl115a2_a0 + (_mpl115a2_b1 + _mpl115a2_c12 * Tadc ) * Padc + _mpl115a2_b2 * Tadc;
+
+  // Return pressure as floating point value
+  *pressure = ((65.0 / 1023.0)*(float)Pcomp) + 50;
+
+  return error;
+}
diff --git a/drivers/sensors/mpl115a2/mpl115a2.h b/drivers/sensors/mpl115a2/mpl115a2.h
new file mode 100644 (file)
index 0000000..354a3a4
--- /dev/null
@@ -0,0 +1,77 @@
+/**************************************************************************/
+/*! 
+    @file     mpl115a2.h
+    @author   K. Townsend (microBuilder.eu)
+
+    @section LICENSE
+
+    Software License Agreement (BSD License)
+
+    Copyright (c) 2012, K. Townsend
+    All rights reserved.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+    1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+    3. Neither the name of the copyright holders nor the
+    names of its contributors may be used to endorse or promote products
+    derived from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
+    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/**************************************************************************/
+
+#ifndef _MPL115A2_H_
+#define _MPL115A2_H_
+
+#include "projectconfig.h"
+#include "core/i2c/i2c.h"
+
+#define MPL115A2_ADDRESS              (0xC0)    // 1100 000 shifted left 1 bit = 0xC0
+#define MPL115A2_READBIT              (0x01)
+
+enum
+{
+  MPL115A2_REGISTER_PRESSURE_MSB     = 0x00,
+  MPL115A2_REGISTER_PRESSURE_LSB     = 0x01,
+  MPL115A2_REGISTER_TEMP_MSB         = 0x02,
+  MPL115A2_REGISTER_TEMP_LSB         = 0x03,
+  MPL115A2_REGISTER_A0_COEFF_MSB     = 0x04,
+  MPL115A2_REGISTER_A0_COEFF_LSB     = 0x05,
+  MPL115A2_REGISTER_B1_COEFF_MSB     = 0x06,
+  MPL115A2_REGISTER_B1_COEFF_LSB     = 0x07,
+  MPL115A2_REGISTER_B2_COEFF_MSB     = 0x08,
+  MPL115A2_REGISTER_B2_COEFF_LSB     = 0x09,
+  MPL115A2_REGISTER_C12_COEFF_MSB    = 0x0A,
+  MPL115A2_REGISTER_C12_COEFF_LSB    = 0x0B,
+  MPL115A2_REGISTER_STARTCONVERSION  = 0x12
+};
+
+typedef enum
+{
+  MPL115A2_ERROR_OK = 0,               // Everything executed normally
+  MPL115A2_ERROR_I2CINIT,              // Unable to initialise I2C
+  MPL115A2_ERROR_I2CBUSY,              // I2C already in use
+  MPL115A2_ERROR_LAST
+}
+mpl115a2Error_t;
+
+mpl115a2Error_t mpl115a2Init(void);
+mpl115a2Error_t mpl115a2GetPressure(float *pressure);
+
+#endif
+
+
This page took 0.040154 seconds and 4 git commands to generate.