From c66d844292fadec4df250a08a9afe6690064cf36 Mon Sep 17 00:00:00 2001 From: Gautier Hattenberger Date: Tue, 11 Dec 2012 14:14:07 +0100 Subject: [PATCH] [sensor] add new sensor mpl3115 (i2c baro) --- conf/airframes/ENAC/fixed-wing/mythe.xml | 12 +- conf/messages.xml | 6 +- conf/modules/baro_mpl3115.xml | 21 +++ sw/airborne/modules/sensors/baro_mpl3115.c | 66 +++++++++ sw/airborne/modules/sensors/baro_mpl3115.h | 32 ++++ sw/airborne/peripherals/mpl3115.c | 161 +++++++++++++++++++++ sw/airborne/peripherals/mpl3115.h | 98 +++++++++++++ 7 files changed, 391 insertions(+), 5 deletions(-) create mode 100644 conf/modules/baro_mpl3115.xml create mode 100644 sw/airborne/modules/sensors/baro_mpl3115.c create mode 100644 sw/airborne/modules/sensors/baro_mpl3115.h create mode 100644 sw/airborne/peripherals/mpl3115.c create mode 100644 sw/airborne/peripherals/mpl3115.h diff --git a/conf/airframes/ENAC/fixed-wing/mythe.xml b/conf/airframes/ENAC/fixed-wing/mythe.xml index 9424bb28467..39d17c44e8f 100644 --- a/conf/airframes/ENAC/fixed-wing/mythe.xml +++ b/conf/airframes/ENAC/fixed-wing/mythe.xml @@ -22,13 +22,17 @@ - + + + + + - + diff --git a/conf/messages.xml b/conf/messages.xml index 1b2412b9675..6635fc0a5aa 100644 --- a/conf/messages.xml +++ b/conf/messages.xml @@ -536,7 +536,11 @@ - + + + + + diff --git a/conf/modules/baro_mpl3115.xml b/conf/modules/baro_mpl3115.xml new file mode 100644 index 00000000000..91c572cc6df --- /dev/null +++ b/conf/modules/baro_mpl3115.xml @@ -0,0 +1,21 @@ + + + + + Baro MPL3115A2 module (I2C) + + + +
+ +
+ + + + + + + + + +
diff --git a/sw/airborne/modules/sensors/baro_mpl3115.c b/sw/airborne/modules/sensors/baro_mpl3115.c new file mode 100644 index 00000000000..f44e248c643 --- /dev/null +++ b/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(); +} + + + + + + + + + + + + + + + diff --git a/sw/airborne/modules/sensors/baro_mpl3115.h b/sw/airborne/modules/sensors/baro_mpl3115.h new file mode 100644 index 00000000000..ef9f8b11241 --- /dev/null +++ b/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 diff --git a/sw/airborne/peripherals/mpl3115.c b/sw/airborne/peripherals/mpl3115.c new file mode 100644 index 00000000000..fa70a7bdb37 --- /dev/null +++ b/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; + } +} + diff --git a/sw/airborne/peripherals/mpl3115.h b/sw/airborne/peripherals/mpl3115.h new file mode 100644 index 00000000000..5c723ed3791 --- /dev/null +++ b/sw/airborne/peripherals/mpl3115.h @@ -0,0 +1,98 @@ +/* + * 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.h + * Driver for the baro MPL3115A2 from Freescale (i2c) + */ + +#ifndef MPL3115_H +#define MPL3115_H + +#include "std.h" +#include "math/pprz_algebra_int.h" +#include "mcu_periph/i2c.h" + +/* Device address (8 bits) */ +#define MPL3115_I2C_ADDR 0xC0 + +/* Default I2C device */ +#ifndef MPL3115_I2C_DEV +#define MPL3115_I2C_DEV i2c0 +#endif + +/* Registers */ +#define MPL3115_REG_STATUS 0x00 +#define MPL3115_REG_OUT_P_MSB 0x01 +#define MPL3115_REG_OUT_P_CSB 0x02 +#define MPL3115_REG_OUT_P_LSB 0x03 +#define MPL3115_REG_OUT_T_MSB 0x04 +#define MPL3115_REG_OUT_T_LSB 0x05 +#define MPL3115_REG_WHO_AM_I 0x0C +#define MPL3115_REG_PT_DATA_CFG 0x13 +#define MPL3115_REG_CTRL_REG1 0x26 +#define MPL3115_REG_CTRL_REG2 0x27 +#define MPL3115_REG_CTRL_REG3 0x28 +#define MPL3115_REG_CTRL_REG4 0x29 +#define MPL3115_REG_CTRL_REG5 0x2A + +#define MPL3115_OST_BIT (1<<1) // One Shot control bit + +/* Default conf */ +#ifndef MPL3115_PT_DATA_CFG +#define MPL3115_PT_DATA_CFG 0x2 // Enable data event flag for pressure +#endif +#ifndef MPL3115_OVERSAMPLING +#define MPL3115_OVERSAMPLING 0x5 // Oversample ratio (0x5: 130ms between data sample) +#endif +#ifndef MPL3115_RAW_OUTPUT +#define MPL3115_RAW_OUTPUT 0x0 // Raw output (disable alt mode if true) +#endif +#ifndef MPL3115_ALT_MODE +#define MPL3115_ALT_MODE 0x1 // 0: baro, 1: alti (disable by raw mode) +#endif + +#define MPL3115_CTRL_REG1 ((MPL3115_OVERSAMPLING<<3)|(MPL3115_RAW_OUTPUT<<6)|(MPL3115_ALT_MODE<<7)) + +// Config done flag +extern bool_t mpl3115_initialized; +// Data ready flag +extern volatile bool_t mpl3115_data_available; +// Data +extern uint32_t mpl3115_pressure; +extern int16_t mpl3115_temperature; +extern float mpl3115_alt; +// I2C transaction structure +//extern struct i2c_transaction mpl3115_trans; + +// Functions +extern void mpl3115_init(void); +extern void mpl3115_configure(void); +extern void mpl3115_read(void); +extern void mpl3115_event(void); + +// Macro for using MPL3115 in periodic function +#define Mpl3115Periodic() { \ + if (mpl3115_initialized) mpl3115_read(); \ + else mpl3115_configure(); \ +} + +#endif // MPL3115_H