1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

Updates to BMP085 readings.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@260 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
gussy 2010-03-06 10:28:08 +00:00 committed by gussy
parent 7cd8f93d5c
commit fd889b918b
5 changed files with 88 additions and 75 deletions

View File

@ -80,6 +80,7 @@
#define PIOS_BMP085_EOC_CLK RCC_APB2Periph_GPIOC
#define PIOS_BMP085_EOC_EXTI_LINE EXTI_Line15
#define PIOS_BMP085_EOC_IRQn EXTI15_10_IRQn
#define PIOS_BMP085_EOC_PRIO PIOS_IRQ_PRIO_HIGH
//-------------------------
// PIOS_USART1 (TELEM)

View File

@ -33,9 +33,6 @@
/* Global Variables */
/* Local Variables */
#define STRING_MAX 1024
static uint8_t line_buffer[STRING_MAX];
static uint16_t line_ix;
static uint8_t sdcard_available;
/* Function Prototypes */
@ -105,7 +102,7 @@ int main()
/* Create a FreeRTOS task */
xTaskCreate(TaskTick, (signed portCHAR *)"Test", configMINIMAL_STACK_SIZE , NULL, 1, NULL);
xTaskCreate(TaskTesting, (signed portCHAR *)"TaskTesting", configMINIMAL_STACK_SIZE , NULL, 4, NULL);
xTaskCreate(TaskTesting, (signed portCHAR *)"TaskTesting", configMINIMAL_STACK_SIZE , NULL, 1, NULL);
//xTaskCreate(TaskServos, (signed portCHAR *)"Servos", configMINIMAL_STACK_SIZE , NULL, 4, NULL);
//xTaskCreate(TaskSDCard, (signed portCHAR *)"SDCard", configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 2), NULL);
@ -143,16 +140,29 @@ static void TaskTick(void *pvParameters)
static void TaskTesting(void *pvParameters)
{
portTickType xDelay = 1000 / portTICK_RATE_MS;
portTickType xTimeout = 1 / portTICK_RATE_MS;
portTickType xTimeout = 10 / portTICK_RATE_MS;
for(;;)
{
/* This blocks the task until the BMP085 EOC */
PIOS_BMP085_StartADC(TemperatureConv);
xSemaphoreTake(PIOS_BMP085_EOC, xTimeout);
PIOS_BMP085_ReadADC();
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "Temp: %u\r", PIOS_BMP085_GetTemperature());
PIOS_BMP085_StartADC(PressureConv);
xSemaphoreTake(PIOS_BMP085_EOC, xTimeout);
PIOS_BMP085_ReadADC();
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "Pressure: %u\r", PIOS_BMP085_GetPressure());
vTaskDelay(xDelay);
/* This blocks the task until there is something on the buffer */
xSemaphoreTake(PIOS_USART1_Buffer, portMAX_DELAY);
/*xSemaphoreTake(PIOS_USART1_Buffer, portMAX_DELAY);
int32_t len = PIOS_COM_ReceiveBufferUsed(COM_USART1);
for(int32_t i = 0; i < len; i++) {
PIOS_COM_SendFormattedString(COM_DEBUG_USART, ">%c\r", PIOS_COM_ReceiveBuffer(COM_USART1));
}
}*/
//int32_t state = PIOS_USB_CableConnected();
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "State: %d\r", state);

View File

@ -47,12 +47,19 @@ Example of how to use this module:
/* Glocal Variables */
ConversionTypeTypeDef CurrentRead;
xSemaphoreHandle PIOS_BMP085_EOC;
/* Local Variables */
static BMP085CalibDataTypeDef CalibData;
static volatile uint32_t RawPressure;
static volatile uint32_t RawTemperature;
static portBASE_TYPE xHigherPriorityTaskWoken;
/* Straight from the datasheet */
static int32_t X1, X2, X3, B3, B5, B6, P;
static uint32_t B4, B7;
static uint16_t RawTemperature;
static uint32_t RawPressure;
static uint32_t Pressure;
static uint16_t Temperature;
static uint32_t Altitude;
/**
@ -82,8 +89,8 @@ void PIOS_BMP085_Init(void)
/* Enable and set EOC EXTI Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = PIOS_BMP085_EOC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_BMP085_EOC_PRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
@ -121,6 +128,8 @@ void PIOS_BMP085_Init(void)
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "MB = %d\r", CalibData.MB);
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "MC = %d\r", CalibData.MC);
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "MD = %d\r", CalibData.MD);
vSemaphoreCreateBinary(PIOS_BMP085_EOC);
}
@ -132,9 +141,9 @@ void PIOS_BMP085_Init(void)
void PIOS_BMP085_StartADC(ConversionTypeTypeDef Type)
{
/* Start the conversion */
if(Type == Temperature) {
if(Type == TemperatureConv) {
PIOS_BMP085_Write(BMP085_CTRL_ADDR, BMP085_TEMP_ADDR);
} else if(Type == Pressure) {
} else if(Type == PressureConv) {
PIOS_BMP085_Write(BMP085_CTRL_ADDR, BMP085_PRES_ADDR);
}
@ -143,7 +152,7 @@ void PIOS_BMP085_StartADC(ConversionTypeTypeDef Type)
/**
* Read the ADC conversion value (once ADC converion has completed)
* Read the ADC conversion value (once ADC conversion has completed)
* \param[in] PresOrTemp BMP085_PRES_ADDR or BMP085_TEMP_ADDR
* \return Raw ADC value
*/
@ -152,68 +161,55 @@ void PIOS_BMP085_ReadADC(void)
uint8_t Data[3];
/* Read and store the 16bit result */
if(CurrentRead == Temperature) {
if(CurrentRead == TemperatureConv) {
/* Read the temperature conversion */
PIOS_BMP085_Read(BMP085_ADC_MSB, Data, 2);
RawTemperature = ((Data[0] << 8) | Data[1]);
X1 = (RawTemperature - CalibData.AC6) * (CalibData.AC5 / pow(2, 15));
X2 = ((int32_t)CalibData.MC * pow(2, 11)) / (X1 + CalibData.MD);
B5 = X1 + X2;
Temperature = (B5 + 8) >> 4;
} else {
/* Read the pressure conversion */
PIOS_BMP085_Read(BMP085_ADC_MSB, Data, 3);
RawPressure = ((Data[0] << 16) | (Data[1] << 8) | Data[2]) >> (8 - BMP085_OVERSAMPLING);
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "UP = %d\r", RawPressure);
B6 = B5 - 4000;
X1 = (CalibData.B2 * (B6 * (B6 / pow(2, 12)))) / pow(2, 11);
X2 = CalibData.AC2 * (B6 / pow(2, 11));
X3 = X1 + X2;
B3 = (((int32_t)CalibData.AC1 * 4 + X3) << (BMP085_OVERSAMPLING + 2)) / 2;
X1 = (CalibData.AC3 * B6) / pow(2, 13);
X2 = (CalibData.B1 * (B6 * (B6 / pow(2, 12)))) / pow(2, 16);
X3 = ((X1 + X2) + 2) / 4;
B4 = (CalibData.AC4 * (uint32_t)(X3 + 32768)) / pow(2, 15);
B7 = (RawPressure - B3) * 50000;
P = B7 < 0x80000000 ? (B7 * 2) / B4 : (B7 / B4) * 2;
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "P = %d\r", P);
X1 = (P / pow(2, 8)) * (P / pow(2, 8));
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "X1 = %d\r", X1);
X1 = (X1 * 3038) / pow(2, 16);
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "X1 = %d\r", X1);
X2 = (-7357 * P) / pow(2, 16);
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "X2 = %d\r", X2);
Pressure = P + ((X1 + X2 + 3791) / pow(2, 4));
//Altitude = 44330 * (1 - (pow((101300/BMP085_P0), (1/5.255))));
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "Altitude = %u\r", Altitude);
}
}
/**
* Get the converted raw values
* \param[out] Pressure Pointer to the pressure variable
* \param[out] Altitude Pointer to the altitude variable
* \param[out] Temperature Pointer to the temperature variable
*/
void PIOS_BMP085_GetValues(uint16_t *Pressure, uint16_t *Altitude, uint16_t *Temperature)
int16_t PIOS_BMP085_GetTemperature(void)
{
uint16_t Pre = 0;
uint16_t Alt = 0;
uint16_t Temp = 0;
/* Straight from the datasheet */
int32_t X1, X2, X3, B3, B5, B6, P;
uint32_t B4, B7;
/* Convert Temperature */
X1 = (RawTemperature - CalibData.AC6) * (CalibData.AC5 >> 15);
X2 = ((int32_t) CalibData.MC << 11) / (X1 + CalibData.MD);
B5 = X1 + X2;
//Temperature = (B5 + 8) >> 4;
Temp = (B5 + 8) >> 4;
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "UT = %u\r", RawTemperature);
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "X1 = %u\r", X1);
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "X2 = %u\r", X2);
/* Calculate Pressure */
B6 = B5 - 4000;
X1 = (CalibData.B2 * (B6 * B6 >> 12)) >> 11;
X2 = CalibData.AC2 * B6 >> 11;
X3 = X1 + X2;
B3 = ((int32_t) CalibData.AC1 * 4 + X3 + 2) >> 2;
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;
B7 = (RawPressure - B3) * 50000;
P = B7 < 0x80000000 ? (B7 * 2) / B4 : (B7 / B4) * 2;
X1 = (P >> 8) * (P >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * P) >> 16;
//Pressure = P + ((X1 + X2 + 3791) >> 4);
Pre = P + ((X1 + X2 + 3791) >> 4);
/* Calculate Altitude */
//Altitude = (uint16_t) 44330 * (1 - (pow((*Pressure/BMP085_P0), (1/5.255))));
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "T = %u P = %u A = %u\r", Temp, Pre, Alt);
return Temperature;
}
int32_t PIOS_BMP085_GetPressure(void)
{
return Pressure;
}
/**
* Reads one or more bytes into a buffer
@ -299,7 +295,8 @@ void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(PIOS_BMP085_EOC_EXTI_LINE) != RESET) {
/* Read the ADC Value */
PIOS_BMP085_ReadADC();
//PIOS_BMP085_ReadADC();
xSemaphoreGiveFromISR(PIOS_BMP085_EOC, &xHigherPriorityTaskWoken);
/* Clear the EOC EXTI line pending bit */
EXTI_ClearITPendingBit(PIOS_BMP085_EOC_EXTI_LINE);

View File

@ -101,6 +101,8 @@ int32_t PIOS_USB_HID_Init(uint32_t mode)
return -1;
}
vSemaphoreCreateBinary(PIOS_HID_Buffer);
return 0; /* No error */
}

View File

@ -31,11 +31,10 @@
#define BMP085_CALIB_ADDR 0xAA
#define BMP085_CALIB_LEN 22
#define BMP085_CTRL_ADDR 0xF4
#define BMP085_OVERSAMPLING 0
#define BMP085_OVERSAMPLING 2
#define BMP085_PRES_ADDR (0x34 + (BMP085_OVERSAMPLING << 6))
#define BMP085_TEMP_ADDR 0x2E
#define BMP085_ADC_MSB 0xF6
#define BMP085_ADC_LSB 0xF7
#define BMP085_P0 101325
/* Local Types */
@ -54,16 +53,20 @@ typedef struct {
} BMP085CalibDataTypeDef;
typedef enum {
Pressure,
Temperature
PressureConv,
TemperatureConv
} ConversionTypeTypeDef;
/* Global Variables */
extern xSemaphoreHandle PIOS_BMP085_EOC;
/* Public Functions */
void PIOS_BMP085_Init(void);
void PIOS_BMP085_StartADC(ConversionTypeTypeDef Type);
void PIOS_BMP085_ReadADC(void);
void PIOS_BMP085_GetValues(uint16_t *Pressure, uint16_t *Altitude, uint16_t *Temperature);
int32_t PIOS_BMP085_Read(uint8_t address, uint8_t *buffer, uint8_t len);
int32_t PIOS_BMP085_Write(uint8_t address, uint8_t buffer);
extern void PIOS_BMP085_Init(void);
extern void PIOS_BMP085_StartADC(ConversionTypeTypeDef Type);
extern void PIOS_BMP085_ReadADC(void);
extern int16_t PIOS_BMP085_GetTemperature(void);
extern int32_t PIOS_BMP085_GetPressure(void);
extern int32_t PIOS_BMP085_Read(uint8_t address, uint8_t *buffer, uint8_t len);
extern int32_t PIOS_BMP085_Write(uint8_t address, uint8_t buffer);
#endif /* PIOS_BMP085_H */