Skip to content

Commit

Permalink
[sensor] add new sensor mpl3115 (i2c baro)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautierhattenberger committed Dec 11, 2012
1 parent 60475a2 commit c66d844
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 5 deletions.
12 changes: 8 additions & 4 deletions conf/airframes/ENAC/fixed-wing/mythe.xml
Expand Up @@ -22,13 +22,17 @@
<define name="I2C0_SCLL" value="25"/>
<define name="I2C0_SCLH" value="25"/>
</load-->
<load name="baro_bmp.xml">
<!--load name="baro_bmp.xml">
<define name="BMP_I2C_DEV" value="i2c1"/>
</load>
<load name="baro_ms5611_i2c.xml">
</load-->
<!--load name="baro_ms5611_i2c.xml">
<define name="MS5611_I2C_DEV" value="i2c0"/>
</load-->
<load name="baro_mpl3115.xml">
<define name="MPL3115_I2C_DEV" value="i2c0"/>
<define name="SENSOR_SYNC_SEND"/>
</load>
<!-- load name="airspeed_ads1114.xml"/-->
<!--load name="airspeed_ads1114.xml"/-->
<!--load name="mcp355x.xml"/-->
<!--load name="sys_mon.xml"/-->
</modules>
Expand Down
6 changes: 5 additions & 1 deletion conf/messages.xml
Expand Up @@ -536,7 +536,11 @@
<field name="Err_Pos" type="float"></field>
</message>

<!-- 68 is free -->
<message name="MPL3115_BARO" id="68">
<field name="pressure" type="uint32" alt_unit="Pa" alt_unit_coef="0.25"/>
<field name="temp" type="int16" alt_unit="degC" alt_unit_coef="0.0625"/>
<field name="alt" type="float" unit="m"/>
</message>

<message name="AOA_adc" id="69">
<field name="adcVal" type="uint16"></field>
Expand Down
21 changes: 21 additions & 0 deletions conf/modules/baro_mpl3115.xml
@@ -0,0 +1,21 @@
<!DOCTYPE module SYSTEM "module.dtd">

<module name="baro_mpl3115" dir="sensors">
<doc>
<description>Baro MPL3115A2 module (I2C)</description>
<define name="MPL3115_I2C_DEV" value="i2cX" description="select which i2c peripheral to use (default i2c0)"/>
</doc>

<header>
<file name="baro_mpl3115.h"/>
</header>
<init fun="baro_mpl3115_init()"/>
<periodic fun="baro_mpl3115_read_periodic()" freq="5."/>
<event fun="baro_mpl3115_read_event()"/>

<makefile>
<file name="baro_mpl3115.c"/>
<file name="mpl3115.c" dir="peripherals"/>
</makefile>

</module>
66 changes: 66 additions & 0 deletions sw/airborne/modules/sensors/baro_mpl3115.c
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2012 Gautier Hattenberger (ENAC)
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/

#include "modules/sensors/baro_mpl3115.h"

//Messages
#include "mcu_periph/uart.h"
#include "messages.h"
#include "subsystems/datalink/downlink.h"

#ifndef DOWNLINK_DEVICE
#define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
#endif

void baro_mpl3115_init( void ) {
mpl3115_init();
}


void baro_mpl3115_read_periodic( void ) {
#ifdef SENSOR_SYNC_SEND
if (mpl3115_data_available) {
DOWNLINK_SEND_MPL3115_BARO(DefaultChannel, DefaultDevice, &mpl3115_pressure, &mpl3115_temperature, &mpl3115_alt);
}
#endif
Mpl3115Periodic();
}


void baro_mpl3115_read_event( void ) {
mpl3115_event();
}















32 changes: 32 additions & 0 deletions sw/airborne/modules/sensors/baro_mpl3115.h
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2012 Gautier Hattenberger (ENAC)
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/

#ifndef BARO_MPL3115_H
#define BARO_MPL3115_H

#include "peripherals/mpl3115.h"

extern void baro_mpl3115_init( void );
extern void baro_mpl3115_read_periodic( void );
extern void baro_mpl3115_read_event( void );

#endif
161 changes: 161 additions & 0 deletions sw/airborne/peripherals/mpl3115.c
@@ -0,0 +1,161 @@
/*
*
* Copyright (C) 2011 Gautier Hattenberger
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

/** @file peripherals/mpl3115.c
* Driver for MPL3115A2 baro sensor.
*/

#include "peripherals/mpl3115.h"
#include "std.h"

#define MPL_CONF_UNINIT 0
#define MPL_CONF_PT_DATA 1
#define MPL_CONF_CTRL1 2
#define MPL_CONF_DONE 3


// Data ready flag
volatile bool_t mpl3115_data_available;
// Data
uint32_t mpl3115_pressure;
int16_t mpl3115_temperature;
float mpl3115_alt;
// I2C transaction for reading and configuring
struct i2c_transaction mpl3115_trans;
// I2C transaction for conversion request
struct i2c_transaction mpl3115_req_trans;
// Init flag
bool_t mpl3115_initialized;
uint8_t mpl3115_init_status;

void mpl3115_init(void)
{
mpl3115_trans.status = I2CTransDone;
mpl3115_req_trans.status = I2CTransDone;
mpl3115_initialized = FALSE;
mpl3115_init_status = MPL_CONF_UNINIT;

mpl3115_pressure = 0;
mpl3115_temperature = 0;
mpl3115_alt = 0.;
}

// Configuration function called once before normal use
static void mpl3115_send_config(void)
{
switch (mpl3115_init_status) {
case MPL_CONF_PT_DATA:
mpl3115_trans.buf[0] = MPL3115_REG_PT_DATA_CFG;
mpl3115_trans.buf[1] = MPL3115_PT_DATA_CFG;
I2CTransmit(MPL3115_I2C_DEV, mpl3115_trans, MPL3115_I2C_ADDR, 2);
mpl3115_init_status++;
break;
case MPL_CONF_CTRL1:
mpl3115_trans.buf[0] = MPL3115_REG_CTRL_REG1;
mpl3115_trans.buf[1] = MPL3115_CTRL_REG1;
I2CTransmit(MPL3115_I2C_DEV, mpl3115_trans, MPL3115_I2C_ADDR, 2);
mpl3115_init_status++;
break;
case MPL_CONF_DONE:
mpl3115_initialized = TRUE;
mpl3115_trans.status = I2CTransDone;
break;
default:
break;
}
}

// Configure
void mpl3115_configure(void)
{
if (mpl3115_init_status == MPL_CONF_UNINIT) {
mpl3115_init_status++;
if (mpl3115_trans.status == I2CTransSuccess || mpl3115_trans.status == I2CTransDone) {
mpl3115_send_config();
}
}
}

// Normal reading
void mpl3115_read(void)
{
// ask for a reading and then prepare next conversion
if (mpl3115_initialized && mpl3115_trans.status == I2CTransDone) {
mpl3115_trans.buf[0] = MPL3115_REG_STATUS;
I2CTransceive(MPL3115_I2C_DEV, mpl3115_trans, MPL3115_I2C_ADDR, 1, 6);
if (mpl3115_req_trans.status == I2CTransDone) {
mpl3115_req_trans.buf[0] = MPL3115_REG_CTRL_REG1;
mpl3115_req_trans.buf[1] = MPL3115_CTRL_REG1 | MPL3115_OST_BIT;
I2CTransmit(MPL3115_I2C_DEV, mpl3115_req_trans, MPL3115_I2C_ADDR, 2);
}
}
}

void mpl3115_event(void)
{
if (mpl3115_initialized) {
if (mpl3115_trans.status == I2CTransFailed) {
mpl3115_trans.status = I2CTransDone;
}
else if (mpl3115_trans.status == I2CTransSuccess) {
// Successfull reading and new pressure data available
if (mpl3115_trans.buf[0] & (1<<2)) {
#if MPL3115_RAW_OUTPUT
// New data available
mpl3115_pressure = ((uint32_t)mpl3115_trans.buf[1]<<16)|((uint16_t)mpl3115_trans.buf[2]<<8)|mpl3115_trans.buf[3];
mpl3115_temperature = ((int16_t)mpl3115_trans.buf[4]<<8)|mpl3115_trans.buf[5];
mpl3115_data_available = TRUE;
#else // Not in raw mode
#if MPL3115_ALT_MODE
uint32_t tmp = ((uint32_t)mpl3115_trans.buf[1]<<16)|((uint16_t)mpl3115_trans.buf[2]<<8)|mpl3115_trans.buf[3];
mpl3115_alt = (float)(tmp>>4)/(1<<4);
tmp = ((int16_t)mpl3115_trans.buf[4]<<8)|mpl3115_trans.buf[5];
mpl3115_temperature = (tmp>>4);
mpl3115_data_available = TRUE;
#else // Pressure mode
uint32_t tmp = ((uint32_t)mpl3115_trans.buf[1]<<16)|((uint16_t)mpl3115_trans.buf[2]<<8)|mpl3115_trans.buf[3];
mpl3115_pressure = (tmp>>4);
tmp = ((int16_t)mpl3115_trans.buf[4]<<8)|mpl3115_trans.buf[5];
mpl3115_temperature = (tmp>>4);
mpl3115_data_available = TRUE;
#endif // end alt mode
#endif // end raw mode
}
mpl3115_trans.status = I2CTransDone;
}
}
else if (!mpl3115_initialized && mpl3115_init_status != MPL_CONF_UNINIT) { // Configuring
if (mpl3115_trans.status == I2CTransSuccess || mpl3115_trans.status == I2CTransDone) {
mpl3115_trans.status = I2CTransDone;
mpl3115_send_config();
}
if (mpl3115_trans.status == I2CTransFailed) {
mpl3115_init_status--;
mpl3115_trans.status = I2CTransDone;
mpl3115_send_config(); // Retry config (TODO max retry)
}
}
if (mpl3115_req_trans.status == I2CTransSuccess || mpl3115_req_trans.status == I2CTransFailed) {
mpl3115_req_trans.status = I2CTransDone;
}
}

0 comments on commit c66d844

Please sign in to comment.