Skip to content

Commit

Permalink
MS5611, BMP280 SPI update
Browse files Browse the repository at this point in the history
  • Loading branch information
MJ666 committed Feb 16, 2016
1 parent 470e7b9 commit 24ffc0f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 12 deletions.
52 changes: 47 additions & 5 deletions src/main/drivers/barometer_bmp280.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include <platform.h>

Expand All @@ -26,6 +27,7 @@

#include "system.h"
#include "bus_i2c.h"
#include "bus_spi.h"

#include "barometer_bmp280.h"

Expand Down Expand Up @@ -105,21 +107,61 @@ static void bmp280_start_up(void);
static void bmp280_get_up(void);
STATIC_UNIT_TESTED void bmp280_calculate(int32_t *pressure, int32_t *temperature);

#if defined(USE_SPI) && defined(BMP280_SPI_INSTANCE)
#define DISABLE_BMP280 GPIO_SetBits(BMP280_CS_GPIO, BMP280_CS_PIN)
#define ENABLE_BMP280 GPIO_ResetBits(BMP280_CS_GPIO, BMP280_CS_PIN)

bool baroBMP280Write(uint8_t reg, uint8_t data)
{
ENABLE_BMP280;
delayMicroseconds(1);
spiTransferByte(BMP280_SPI_INSTANCE, reg);
spiTransferByte(BMP280_SPI_INSTANCE, data);
DISABLE_BMP280;

return true;
}

bool baroBMP280Read(uint8_t reg, uint8_t length, uint8_t *data)
{
bool ack = false;

ENABLE_BMP280;
delayMicroseconds(1);
spiTransferByte(BMP280_SPI_INSTANCE, reg | 0x80); // read transaction
ack = spiTransfer(BMP280_SPI_INSTANCE, data, NULL, length);
DISABLE_BMP280;

return ack;
}
#else
bool baroBMP280Write(uint8_t reg, uint8_t data)
{
return i2cWrite(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, reg, data);
}

bool baroBMP280Read(uint8_t reg, uint8_t length, uint8_t *data)
{
return i2cRead(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, reg, length, data);
}
#endif


bool bmp280Detect(baro_t *baro)
{
if (bmp280InitDone)
return true;

delay(20);

i2cRead(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, BMP280_CHIP_ID_REG, 1, &bmp280_chip_id); /* read Chip Id */
baroBMP280Read(BMP280_CHIP_ID_REG, 1, &bmp280_chip_id); /* read Chip Id */
if (bmp280_chip_id != BMP280_DEFAULT_CHIP_ID)
return false;

// read calibration
i2cRead(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG, 24, (uint8_t *)&bmp280_cal);
baroBMP280Read(BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG, 24, (uint8_t *)&bmp280_cal);
// set oversampling + power mode (forced), and start sampling
i2cWrite(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, BMP280_CTRL_MEAS_REG, BMP280_MODE);
baroBMP280Write(BMP280_CTRL_MEAS_REG, BMP280_MODE);

bmp280InitDone = true;

Expand Down Expand Up @@ -151,15 +193,15 @@ static void bmp280_start_up(void)
{
// start measurement
// set oversampling + power mode (forced), and start sampling
i2cWrite(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, BMP280_CTRL_MEAS_REG, BMP280_MODE);
baroBMP280Write(BMP280_CTRL_MEAS_REG, BMP280_MODE);
}

static void bmp280_get_up(void)
{
uint8_t data[BMP280_DATA_FRAME_SIZE];

// read data from sensor
i2cRead(BMP280_I2C_INSTANCE, BMP280_I2C_ADDR, BMP280_PRESSURE_MSB_REG, BMP280_DATA_FRAME_SIZE, data);
baroBMP280Read(BMP280_PRESSURE_MSB_REG, BMP280_DATA_FRAME_SIZE, data);
bmp280_up = (int32_t)((((uint32_t)(data[0])) << 12) | (((uint32_t)(data[1])) << 4) | ((uint32_t)data[2] >> 4));
bmp280_ut = (int32_t)((((uint32_t)(data[3])) << 12) | (((uint32_t)(data[4])) << 4) | ((uint32_t)data[5] >> 4));
}
Expand Down
91 changes: 85 additions & 6 deletions src/main/drivers/barometer_ms5611.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include <platform.h>
#include "debug.h"

#include "barometer.h"

#include "gpio.h"
#include "system.h"
#include "bus_i2c.h"
#include "bus_spi.h"

#include "build_config.h"

#define DEBUG_MS5611

#ifndef MS5611_I2C_INSTANCE
#define MS5611_I2C_INSTANCE I2C_DEVICE
#endif
Expand Down Expand Up @@ -64,6 +69,57 @@ STATIC_UNIT_TESTED uint32_t ms5611_up; // static result of pressure measurement
STATIC_UNIT_TESTED uint16_t ms5611_c[PROM_NB]; // on-chip ROM
static uint8_t ms5611_osr = CMD_ADC_4096;

typedef bool (*baroMS5611ReadRegisterFunc)(uint8_t reg_, uint8_t len_, uint8_t *buf);
typedef bool (*baroMS5611WriteRegisterFunc)(uint8_t reg_, uint8_t data);

typedef struct baroMS5611Config_s {
baroMS5611ReadRegisterFunc read;
baroMS5611WriteRegisterFunc write;
} baroMS5611Config_t;

baroMS5611Config_t baroMS5611config;

#ifdef USE_I2C
bool baroMS5611WriteI2C(uint8_t reg, uint8_t data)
{
return i2cWrite(MS5611_I2C_INSTANCE, MS5611_ADDR, reg, data);
}

bool baroMS5611ReadI2C(uint8_t reg, uint8_t length, uint8_t *data)
{
return i2cRead(MS5611_I2C_INSTANCE, MS5611_ADDR, reg, length, data);
}
#endif

#if defined(USE_SPI) && defined(MS5611_SPI_INSTANCE)
#define DISABLE_MS5611 GPIO_SetBits(MS5611_CS_GPIO, MS5611_CS_PIN)
#define ENABLE_MS5611 GPIO_ResetBits(MS5611_CS_GPIO, MS5611_CS_PIN)

bool baroMS5611WriteSPI(uint8_t reg, uint8_t data)
{
ENABLE_MS5611;
delayMicroseconds(1);
spiTransferByte(MS5611_SPI_INSTANCE, reg);
spiTransferByte(MS5611_SPI_INSTANCE, data);
DISABLE_MS5611;

return true;
}

bool baroMS5611ReadSPI(uint8_t reg, uint8_t length, uint8_t *data)
{
bool ack = false;

ENABLE_MS5611;
delayMicroseconds(1);
spiTransferByte(MS5611_SPI_INSTANCE, reg | 0x80); // read transaction
ack = spiTransfer(MS5611_SPI_INSTANCE, data, NULL, length);
DISABLE_MS5611;

return ack;
}
#endif

bool ms5611Detect(baro_t *baro)
{
bool ack = false;
Expand All @@ -72,8 +128,21 @@ bool ms5611Detect(baro_t *baro)

delay(10); // No idea how long the chip takes to power-up, but let's make it 10ms

#ifdef USE_I2C
ack = i2cRead(MS5611_I2C_INSTANCE, MS5611_ADDR, CMD_PROM_RD, 1, &sig);
if (!ack)
if (ack) {
baroMS5611config.read = baroMS5611ReadI2C;
baroMS5611config.write = baroMS5611WriteI2C;
}
#endif
#if defined(USE_SPI) && defined(MS5611_SPI_INSTANCE)
ack = baroMS5611ReadSPI(CMD_PROM_RD, 1, &sig);
if (ack) {
baroMS5611config.read = baroMS5611ReadSPI;
baroMS5611config.write = baroMS5611WriteSPI;
}
#endif
else
return false;

ms5611_reset();
Expand All @@ -98,14 +167,14 @@ bool ms5611Detect(baro_t *baro)

static void ms5611_reset(void)
{
i2cWrite(MS5611_I2C_INSTANCE, MS5611_ADDR, CMD_RESET, 1);
baroMS5611config.write(CMD_RESET, 1);
delayMicroseconds(2800);
}

static uint16_t ms5611_prom(int8_t coef_num)
{
uint8_t rxbuf[2] = { 0, 0 };
i2cRead(MS5611_I2C_INSTANCE, MS5611_ADDR, CMD_PROM_RD + coef_num * 2, 2, rxbuf); // send PROM READ command
baroMS5611config.read(CMD_PROM_RD + coef_num * 2, 2, rxbuf); // send PROM READ command
return rxbuf[0] << 8 | rxbuf[1];
}

Expand Down Expand Up @@ -142,28 +211,34 @@ STATIC_UNIT_TESTED int8_t ms5611_crc(uint16_t *prom)
static uint32_t ms5611_read_adc(void)
{
uint8_t rxbuf[3];
i2cRead(MS5611_I2C_INSTANCE, MS5611_ADDR, CMD_ADC_READ, 3, rxbuf); // read ADC
baroMS5611config.read(CMD_ADC_READ, 3, rxbuf); // read ADC
return (rxbuf[0] << 16) | (rxbuf[1] << 8) | rxbuf[2];
}

static void ms5611_start_ut(void)
{
i2cWrite(MS5611_I2C_INSTANCE, MS5611_ADDR, CMD_ADC_CONV + CMD_ADC_D2 + ms5611_osr, 1); // D2 (temperature) conversion start!
baroMS5611config.write(CMD_ADC_CONV + CMD_ADC_D2 + ms5611_osr, 1); // D2 (temperature) conversion start!
}

static void ms5611_get_ut(void)
{
ms5611_ut = ms5611_read_adc();
#ifdef DEBUG_MS5611
debug[0] = ms5611_ut;
#endif
}

static void ms5611_start_up(void)
{
i2cWrite(MS5611_I2C_INSTANCE, MS5611_ADDR, CMD_ADC_CONV + CMD_ADC_D1 + ms5611_osr, 1); // D1 (pressure) conversion start!
baroMS5611config.write(CMD_ADC_CONV + CMD_ADC_D1 + ms5611_osr, 1); // D1 (pressure) conversion start!
}

static void ms5611_get_up(void)
{
ms5611_up = ms5611_read_adc();
#ifdef DEBUG_MS5611
debug[1] = ms5611_up;
#endif
}

STATIC_UNIT_TESTED void ms5611_calculate(int32_t *pressure, int32_t *temperature)
Expand Down Expand Up @@ -191,6 +266,10 @@ STATIC_UNIT_TESTED void ms5611_calculate(int32_t *pressure, int32_t *temperature
}
press = ((((int64_t)ms5611_up * sens) >> 21) - off) >> 15;

#ifdef DEBUG_MS5611
debug[2] = temp;
debug[3] = press;
#endif

if (pressure)
*pressure = press;
Expand Down
12 changes: 11 additions & 1 deletion src/main/target/ALIENFLIGHTF4/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,19 @@
#define USE_MAG_AK8963
#define MAG_AK8963_ALIGN CW270_DEG

#define MS5611_CS_GPIO GPIOA
#define MS5611_CS_PIN GPIO_Pin_15
#define MS5611_CS_GPIO_CLK_PERIPHERAL RCC_AHB1Periph_GPIOA
#define MS5611_SPI_INSTANCE SPI3

//#define BMP280_CS_GPIO GPIOA
//#define BMP280_CS_PIN GPIO_Pin_15
//#define BMP280_CS_GPIO_CLK_PERIPHERAL RCC_AHB1Periph_GPIOA
//#define BMP280_SPI_INSTANCE SPI3

#define BARO
#define USE_BARO_MS5611
#define USE_BARO_BMP280
//#define USE_BARO_BMP280

#define M25P16_CS_PIN PB12
#define M25P16_SPI_INSTANCE SPI2
Expand Down

0 comments on commit 24ffc0f

Please sign in to comment.