Files
@ 645e46f4898d
Branch filter:
Location: windsonde/Source/lps25h.c
645e46f4898d
3.1 KiB
text/plain
Update scale for bme280 to tenths of percent, disable lps25h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | //
// Sensors: Reads pressure and e-compass sensors over I2C and stores readings in queryable internal data structures
//
#include "lps25h.h"
#include "stm32f0xx_hal.h"
#include "config.h"
// Static variables
static I2C_HandleTypeDef hi2c1;
static int32_t pressure_hPa = 0;
static int32_t temp_celsius = 0;
static uint8_t sensors_pressurebuf[8]; // Buffer for i2c rx pressure data
static uint8_t sensors_pressure_freshdata = 0; // If pressure sensor has fresh data
// Initialize pressure sensor and E-compass
void lps25h_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
// I2C Pins
GPIO_InitStruct.Pin = PIN_SENSORS_SCL|PIN_SENSORS_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
HAL_GPIO_Init(PORT_SENSORS, &GPIO_InitStruct);
// Initialize I2C peripheral
__I2C1_CLK_ENABLE();
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00108EFB;
hi2c1.Init.OwnAddress1 = 0x0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
volatile HAL_StatusTypeDef res = HAL_I2C_Init(&hi2c1);
// Configure Analog filter
res = HAL_I2CEx_AnalogFilter_Config(&hi2c1, I2C_ANALOGFILTER_ENABLED);
// Enable I2C interrupt
HAL_NVIC_SetPriority(I2C1_IRQn, 7, 4);
HAL_NVIC_EnableIRQ(I2C1_IRQn);
// Blocking initialization of sensors
// Power on the pressure sensor and configure //////////////////////////////////////////////////
uint8_t temp[2];
temp[0] = PRESSURE_CTRL1_PWRUP | PRESSURE_CTRL1_7HZ;
HAL_Delay(10);
res = HAL_I2C_Mem_Write(&hi2c1, PRESSURE_ADDRESS, 0x20, 1, temp, 1, 500);
}
// Initiate interrupt-based read of engineering sensors
void lps25h_read(void)
{
// Start reading pressure sensor
HAL_Delay(10);
volatile HAL_StatusTypeDef res = HAL_I2C_Mem_Read(&hi2c1, PRESSURE_ADDRESS, PRESSURE_PRESS_REGXL | PRESSURE_AUTOINC, 1, sensors_pressurebuf, 5, 500);
// Consume fresh data flag
sensors_pressure_freshdata = 0;
// Convert received bytes to pressure in hPa
pressure_hPa = sensors_pressurebuf[0]<<8 | sensors_pressurebuf[1]<<16 | sensors_pressurebuf[2]<<24;
pressure_hPa >>= 8; // Sign extend
pressure_hPa /= 4096; // convert to hPa
// Convert received bytes to temperature in Celsius
int16_t temp_out = (sensors_pressurebuf[3] | sensors_pressurebuf[4]<<8);
if(!temp_out)
temp_celsius = 42;
else
temp_celsius = 42.5 + temp_out / 480.0;
}
// Get current temperature in Celsius
int32_t lps25h_get_temperature(void)
{
return temp_celsius;
}
// Get current pressure in hPa
int32_t lps25h_get_pressure(void)
{
return pressure_hPa;
}
inline I2C_HandleTypeDef* lps25h_get_i2c_handle(void)
{
return &hi2c1;
}
// vim:softtabstop=4 shiftwidth=4 expandtab
|