#include "main.h"
#include "libBMP280/bmp280.h"
/* USER CODE BEGIN PV */
typedef struct
{
uint8_t dev_addr;
} interface_t;
// interface code
/// pointer to I2C hardware
static I2C_HandleTypeDef *hi2c;
/// @brief Generic I2C write function
/// @param i2c_addr I2C address
/// @param reg_addr I2C register address
/// @param reg_data pointer to data buffer to send
/// @param len length of data to send
/// @return
static int8_t
user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data,
uint16_t len)
{
HAL_StatusTypeDef st = HAL_I2C_Mem_Write(hi2c, i2c_addr << 1, reg_addr, 1,
reg_data, len, 1000);
return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK;
}
/// @brief Generic I2C read function
/// @param i2c_addr I2C address
/// @param reg_addr I2C register address
/// @param reg_data pointer to data buffer to fill
/// @param len length of data to receive
/// @return status code
static int8_t
user_i2c_read(uint8_t i2c_addr,
uint8_t reg_addr,
uint8_t *reg_data,
uint16_t len)
{
HAL_StatusTypeDef st = HAL_I2C_Mem_Read(hi2c, i2c_addr << 1, reg_addr, 1,
reg_data, len, 1000);
return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK;
}
static void
user_delay_ms(uint32_t ms)
{
HAL_Delay(ms);
}
struct bmp280_dev bmpManifold =
{
.intf = BMP280_I2C_INTF,
.read = user_i2c_read,
.write = user_i2c_write,
.delay_ms = user_delay_ms,
/* Update interface pointer with the structure that contains both device address and file descriptor */
.dev_id = BMP280_I2C_ADDR_PRIM};
struct bmp280_dev bmpAtmosphere =
{
.intf = BMP280_I2C_INTF,
.read = user_i2c_read,
.write = user_i2c_write,
.delay_ms = user_delay_ms,
/* Update interface pointer with the structure that contains both device address and file descriptor */
.dev_id = BMP280_I2C_ADDR_SEC};
// config for manifold sensor
struct bmp280_config confManifold;
// config for atmospheric sensor
struct bmp280_config confAtmosphere;
uint8_t init_bmp(I2C_HandleTypeDef *i2c,struct bmp280_dev * bmp,struct bmp280_config *conf)
{
// copy the I2c handle over
hi2c = i2c;
int8_t rslt = bmp280_init(bmp);
if (rslt != BMP280_OK)
return rslt;
rslt = bmp280_get_config(conf, bmp);
if (rslt != BMP280_OK)
return rslt;
/* configuring the temperature oversampling, filter coefficient and output data rate */
/* Overwrite the desired settings */
conf->filter = BMP280_FILTER_COEFF_4;
/* Temperature oversampling set at 1x */
conf->os_temp = BMP280_OS_1X;
/* Pressure over sampling 4x */
conf->os_pres = BMP280_OS_4X;
/* Setting the output data rate as 8HZ(125ms) */
conf->odr = BMP280_ODR_125_MS;
rslt = bmp280_set_config(conf, bmp);
if (rslt != BMP280_OK)
return rslt;
/* Always set the power mode after setting the configuration */
rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, bmp);
return rslt;
}