2009-12-10 02:59:46 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file pios_bmp085.c
|
2010-01-31 18:56:54 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2009-12-10 02:59:46 +01:00
|
|
|
* @brief IRQ Enable/Disable routines
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2009-12-10 05:28:21 +01:00
|
|
|
* @defgroup PIOS_BMP085 BMP085 Functions
|
2009-12-10 02:59:46 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Project Includes */
|
|
|
|
#include "pios.h"
|
|
|
|
|
2010-03-11 13:38:51 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
2010-03-22 03:09:56 +01:00
|
|
|
#if !defined(PIOS_INCLUDE_EXTI)
|
|
|
|
#error PIOS_EXTI Must be included in the project!
|
|
|
|
#endif
|
2009-12-11 01:24:03 +01:00
|
|
|
|
2009-12-10 05:28:21 +01:00
|
|
|
/* Glocal Variables */
|
|
|
|
ConversionTypeTypeDef CurrentRead;
|
2010-03-06 11:28:08 +01:00
|
|
|
xSemaphoreHandle PIOS_BMP085_EOC;
|
2009-12-11 01:24:03 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
/* Local Variables */
|
|
|
|
static BMP085CalibDataTypeDef CalibData;
|
2010-03-06 11:28:08 +01:00
|
|
|
static portBASE_TYPE xHigherPriorityTaskWoken;
|
|
|
|
/* Straight from the datasheet */
|
|
|
|
static int32_t X1, X2, X3, B3, B5, B6, P;
|
|
|
|
static uint32_t B4, B7;
|
2010-03-06 17:04:31 +01:00
|
|
|
static volatile uint16_t RawTemperature;
|
|
|
|
static volatile uint32_t RawPressure;
|
|
|
|
static volatile uint32_t Pressure;
|
|
|
|
static volatile uint16_t Temperature;
|
2009-12-10 05:28:21 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the BMP085 sensor
|
|
|
|
*/
|
|
|
|
void PIOS_BMP085_Init(void)
|
|
|
|
{
|
2009-12-10 07:08:28 +01:00
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
EXTI_InitTypeDef EXTI_InitStructure;
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
|
|
|
/* Enable EOC GPIO clock */
|
2010-02-28 08:01:18 +01:00
|
|
|
RCC_APB2PeriphClockCmd(PIOS_BMP085_EOC_CLK | RCC_APB2Periph_AFIO, ENABLE);
|
2009-12-10 07:08:28 +01:00
|
|
|
|
|
|
|
/* Configure EOC pin as input floating */
|
2010-02-28 08:01:18 +01:00
|
|
|
GPIO_InitStructure.GPIO_Pin = PIOS_BMP085_EOC_GPIO_PIN;
|
2009-12-10 07:08:28 +01:00
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
2010-02-28 08:01:18 +01:00
|
|
|
GPIO_Init(PIOS_BMP085_EOC_GPIO_PORT, &GPIO_InitStructure);
|
2009-12-10 07:08:28 +01:00
|
|
|
|
2010-02-28 08:01:18 +01:00
|
|
|
/* Configure the End Of Conversion (EOC) interrupt */
|
|
|
|
GPIO_EXTILineConfig(PIOS_BMP085_EOC_PORT_SOURCE, PIOS_BMP085_EOC_PIN_SOURCE);
|
|
|
|
EXTI_InitStructure.EXTI_Line = PIOS_BMP085_EOC_EXTI_LINE;
|
2009-12-10 07:08:28 +01:00
|
|
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
|
|
|
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
|
|
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
2010-03-06 07:09:18 +01:00
|
|
|
EXTI_Init(&EXTI_InitStructure);
|
2009-12-10 07:08:28 +01:00
|
|
|
|
|
|
|
/* Enable and set EOC EXTI Interrupt to the lowest priority */
|
2010-02-28 08:01:18 +01:00
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = PIOS_BMP085_EOC_IRQn;
|
2010-03-06 11:28:08 +01:00
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_BMP085_EOC_PRIO;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
2009-12-10 07:08:28 +01:00
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
2010-03-06 07:09:18 +01:00
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
2009-12-10 07:08:28 +01:00
|
|
|
|
2009-12-11 01:24:03 +01:00
|
|
|
/* Read all 22 bytes of calibration data in one transfer, this is a very optimised way of doing things */
|
2010-03-05 11:44:11 +01:00
|
|
|
uint8_t Data[BMP085_CALIB_LEN];
|
2009-12-11 01:24:03 +01:00
|
|
|
PIOS_BMP085_Read(BMP085_CALIB_ADDR, Data, BMP085_CALIB_LEN);
|
|
|
|
|
|
|
|
/* Parameters AC1-AC6 */
|
|
|
|
CalibData.AC1 = (Data[0] << 8) | Data[1];
|
|
|
|
CalibData.AC2 = (Data[2] << 8) | Data[3];
|
|
|
|
CalibData.AC3 = (Data[4] << 8) | Data[5];
|
|
|
|
CalibData.AC4 = (Data[6] << 8) | Data[7];
|
|
|
|
CalibData.AC5 = (Data[8] << 8) | Data[9];
|
|
|
|
CalibData.AC6 = (Data[10] << 8) | Data[11];
|
|
|
|
|
|
|
|
/* Parameters B1, B2 */
|
|
|
|
CalibData.B1 = (Data[12] << 8) | Data[13];
|
|
|
|
CalibData.B2 = (Data[14] << 8) | Data[15];
|
|
|
|
|
|
|
|
/* Parameters MB, MC, MD */
|
|
|
|
CalibData.MB = (Data[16] << 8) | Data[17];
|
|
|
|
CalibData.MC = (Data[18] << 8) | Data[19];
|
|
|
|
CalibData.MD = (Data[20] << 8) | Data[21];
|
2010-03-05 11:44:11 +01:00
|
|
|
|
2010-03-23 05:01:11 +01:00
|
|
|
/* Semaphore used by ISR to signal End-Of-Conversion */
|
2010-03-06 11:28:08 +01:00
|
|
|
vSemaphoreCreateBinary(PIOS_BMP085_EOC);
|
2010-03-23 05:01:11 +01:00
|
|
|
/* Must start off empty so that first transfer waits for EOC */
|
|
|
|
xSemaphoreTake(PIOS_BMP085_EOC, portMAX_DELAY);
|
2009-12-10 02:59:46 +01:00
|
|
|
}
|
|
|
|
|
2009-12-10 05:28:21 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
/**
|
|
|
|
* Start the ADC conversion
|
|
|
|
* \param[in] PresOrTemp BMP085_PRES_ADDR or BMP085_TEMP_ADDR
|
|
|
|
* \return Raw ADC value
|
|
|
|
*/
|
2009-12-10 05:28:21 +01:00
|
|
|
void PIOS_BMP085_StartADC(ConversionTypeTypeDef Type)
|
2009-12-10 02:59:46 +01:00
|
|
|
{
|
|
|
|
/* Start the conversion */
|
2010-03-06 11:28:08 +01:00
|
|
|
if(Type == TemperatureConv) {
|
2010-03-05 11:44:11 +01:00
|
|
|
PIOS_BMP085_Write(BMP085_CTRL_ADDR, BMP085_TEMP_ADDR);
|
2010-03-06 11:28:08 +01:00
|
|
|
} else if(Type == PressureConv) {
|
2010-03-05 11:44:11 +01:00
|
|
|
PIOS_BMP085_Write(BMP085_CTRL_ADDR, BMP085_PRES_ADDR);
|
2009-12-10 05:28:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CurrentRead = Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-03-06 11:28:08 +01:00
|
|
|
* Read the ADC conversion value (once ADC conversion has completed)
|
2009-12-10 05:28:21 +01:00
|
|
|
* \param[in] PresOrTemp BMP085_PRES_ADDR or BMP085_TEMP_ADDR
|
|
|
|
* \return Raw ADC value
|
|
|
|
*/
|
|
|
|
void PIOS_BMP085_ReadADC(void)
|
|
|
|
{
|
2009-12-11 01:24:03 +01:00
|
|
|
uint8_t Data[3];
|
2010-03-06 15:20:42 +01:00
|
|
|
Data[0] = 0;
|
|
|
|
Data[1] = 0;
|
|
|
|
Data[2] = 0;
|
2009-12-10 05:28:21 +01:00
|
|
|
|
2009-12-11 01:24:03 +01:00
|
|
|
/* Read and store the 16bit result */
|
2010-03-06 11:28:08 +01:00
|
|
|
if(CurrentRead == TemperatureConv) {
|
2009-12-11 01:24:03 +01:00
|
|
|
/* Read the temperature conversion */
|
|
|
|
PIOS_BMP085_Read(BMP085_ADC_MSB, Data, 2);
|
|
|
|
RawTemperature = ((Data[0] << 8) | Data[1]);
|
2010-03-06 11:28:08 +01:00
|
|
|
|
2010-03-06 15:20:42 +01:00
|
|
|
X1 = (RawTemperature - CalibData.AC6) * CalibData.AC5 >> 15;
|
|
|
|
X2 = ((int32_t)CalibData.MC << 11) / (X1 + CalibData.MD);
|
2010-03-06 11:28:08 +01:00
|
|
|
B5 = X1 + X2;
|
|
|
|
Temperature = (B5 + 8) >> 4;
|
2009-12-10 05:28:21 +01:00
|
|
|
} else {
|
2009-12-11 01:24:03 +01:00
|
|
|
/* Read the pressure conversion */
|
|
|
|
PIOS_BMP085_Read(BMP085_ADC_MSB, Data, 3);
|
|
|
|
RawPressure = ((Data[0] << 16) | (Data[1] << 8) | Data[2]) >> (8 - BMP085_OVERSAMPLING);
|
2010-03-06 11:28:08 +01:00
|
|
|
|
|
|
|
B6 = B5 - 4000;
|
2010-03-06 15:20:42 +01:00
|
|
|
X1 = (CalibData.B2 * (B6 * B6 >> 12)) >> 11;
|
|
|
|
X2 = CalibData.AC2 * B6 >> 11;
|
2010-03-06 11:28:08 +01:00
|
|
|
X3 = X1 + X2;
|
2010-03-22 03:07:06 +01:00
|
|
|
B3 = ((((int32_t)CalibData.AC1 * 4 + X3) << BMP085_OVERSAMPLING) + 2) >> 2;
|
2010-03-06 15:20:42 +01:00
|
|
|
X1 = CalibData.AC3 * B6 >> 13;
|
|
|
|
X2 = (CalibData.B1 * (B6 * B6 >> 12)) >> 16;
|
|
|
|
X3 = ((X1 + X2) + 2) >> 2;
|
|
|
|
B4 = (CalibData.AC4 * (uint32_t) (X3 + 32768)) >> 15;
|
2010-03-22 03:07:06 +01:00
|
|
|
B7 = ((uint32_t)RawPressure - B3) * (50000 >> BMP085_OVERSAMPLING);
|
2010-03-06 11:28:08 +01:00
|
|
|
P = B7 < 0x80000000 ? (B7 * 2) / B4 : (B7 / B4) * 2;
|
2010-03-06 15:20:42 +01:00
|
|
|
|
|
|
|
X1 = (P >> 8) * (P >> 8);
|
|
|
|
X1 = (X1 * 3038) >> 16;
|
|
|
|
X2 = (-7357 * P) >> 16;
|
2010-03-22 03:09:56 +01:00
|
|
|
Pressure = P + ((X1 + X2 + 3791) >> 4);
|
2009-12-10 05:28:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-06 11:28:08 +01:00
|
|
|
int16_t PIOS_BMP085_GetTemperature(void)
|
2009-12-10 05:28:21 +01:00
|
|
|
{
|
2010-03-06 11:28:08 +01:00
|
|
|
return Temperature;
|
2009-12-10 02:59:46 +01:00
|
|
|
}
|
|
|
|
|
2010-03-06 11:28:08 +01:00
|
|
|
int32_t PIOS_BMP085_GetPressure(void)
|
|
|
|
{
|
|
|
|
return Pressure;
|
|
|
|
}
|
2009-12-10 07:08:28 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
/**
|
|
|
|
* Reads one or more bytes into a buffer
|
|
|
|
* \param[in] address BMP085 register address (depends on size)
|
|
|
|
* \param[out] buffer destination buffer
|
2010-03-05 11:44:11 +01:00
|
|
|
* \param[in] len number of bytes which should be read
|
2009-12-10 02:59:46 +01:00
|
|
|
* \return 0 if operation was successful
|
|
|
|
* \return -1 if error during I2C transfer
|
|
|
|
* \return -2 if BMP085 blocked by another task (retry it!)
|
|
|
|
* \return -4 if invalid length
|
|
|
|
*/
|
2010-03-05 11:44:11 +01:00
|
|
|
int32_t PIOS_BMP085_Read(uint8_t address, uint8_t *buffer, uint8_t len)
|
2009-12-10 02:59:46 +01:00
|
|
|
{
|
2010-03-04 16:41:21 +01:00
|
|
|
/* Try to get the I2C peripheral */
|
2010-03-03 12:46:24 +01:00
|
|
|
if(PIOS_I2C_LockDevice(I2C_Non_Blocking) < 0) {
|
2009-12-10 02:59:46 +01:00
|
|
|
/* Request a retry */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send I2C address and EEPROM address */
|
2010-03-05 11:44:11 +01:00
|
|
|
/* To avoid issues copy address into temporary buffer */
|
|
|
|
uint8_t addr_buffer[1] = {(uint8_t)address};
|
|
|
|
int32_t error = PIOS_I2C_Transfer(I2C_Write_WithoutStop, BMP085_I2C_ADDR, addr_buffer, 1);
|
2010-03-13 13:23:55 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
/* Now receive byte(s) */
|
|
|
|
if(!error) {
|
2010-03-05 11:44:11 +01:00
|
|
|
error = PIOS_I2C_Transfer(I2C_Read, BMP085_I2C_ADDR, buffer, len);
|
2009-12-10 02:59:46 +01:00
|
|
|
}
|
2010-03-13 13:23:55 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
/* Release I2C peripheral */
|
2010-03-03 12:46:24 +01:00
|
|
|
PIOS_I2C_UnlockDevice();
|
2009-12-10 02:59:46 +01:00
|
|
|
|
|
|
|
/* Return error status */
|
|
|
|
return error < 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes one or more bytes to the BMP085
|
2010-03-05 11:44:11 +01:00
|
|
|
* \param[in] address Register address
|
2009-12-10 02:59:46 +01:00
|
|
|
* \param[in] buffer source buffer
|
|
|
|
* \return 0 if operation was successful
|
2010-03-05 11:44:11 +01:00
|
|
|
* \return -1 if error during I2C transfer
|
|
|
|
* \return -2 if BMP085 blocked by another task (retry it!)
|
2009-12-10 02:59:46 +01:00
|
|
|
*/
|
2010-03-05 11:44:11 +01:00
|
|
|
int32_t PIOS_BMP085_Write(uint8_t address, uint8_t buffer)
|
2009-12-10 02:59:46 +01:00
|
|
|
{
|
|
|
|
/* Try to get the IIC peripheral */
|
2010-03-03 12:46:24 +01:00
|
|
|
if(PIOS_I2C_LockDevice(I2C_Non_Blocking) < 0) {
|
2009-12-10 02:59:46 +01:00
|
|
|
/* Request a retry */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2009-12-11 01:24:03 +01:00
|
|
|
/* Send I2C address and data */
|
2010-03-05 11:44:11 +01:00
|
|
|
uint8_t WriteBuffer[2];
|
|
|
|
WriteBuffer[0] = address;
|
|
|
|
WriteBuffer[1] = buffer;
|
2009-12-11 01:24:03 +01:00
|
|
|
|
2010-03-05 11:44:11 +01:00
|
|
|
int32_t error = PIOS_I2C_Transfer(I2C_Write, BMP085_I2C_ADDR, WriteBuffer, 2);
|
2009-12-11 01:24:03 +01:00
|
|
|
|
2009-12-10 02:59:46 +01:00
|
|
|
/* Release I2C peripheral */
|
2010-03-03 12:46:24 +01:00
|
|
|
PIOS_I2C_UnlockDevice();
|
2009-12-10 02:59:46 +01:00
|
|
|
|
|
|
|
/* Return error status */
|
|
|
|
return error < 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2010-02-28 07:47:49 +01:00
|
|
|
#endif
|