Skip to content

Commit

Permalink
MS5611 SPI update
Browse files Browse the repository at this point in the history
  • Loading branch information
MJ666 committed Jan 18, 2016
1 parent 465eb9b commit 219667c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
78 changes: 72 additions & 6 deletions src/main/drivers/barometer_ms5611.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 @@ -25,6 +26,7 @@
#include "gpio.h"
#include "system.h"
#include "bus_i2c.h"
#include "bus_spi.h"

#include "build_config.h"

Expand Down Expand Up @@ -60,6 +62,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_ADDR, reg, data);
}

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

#if defined(USE_SPI) && defined(MS5611_CS_GPIO)
#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 @@ -68,8 +121,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_ADDR, CMD_PROM_RD, 1, &sig);
if (!ack)
if (ack) {
baroMS5611config.read = baroMS5611ReadI2C;
baroMS5611config.write = baroMS5611WriteI2C;
}
#endif
#if defined(USE_SPI) && defined(MS5611_CS_GPIO)
ack = baroMS5611ReadSPI(CMD_PROM_RD, 1, &sig);
if (ack) {
baroMS5611config.read = baroMS5611ReadSPI;
baroMS5611config.write = baroMS5611WriteSPI;
}
#endif
else
return false;

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

static void ms5611_reset(void)
{
i2cWrite(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_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 @@ -138,13 +204,13 @@ STATIC_UNIT_TESTED int8_t ms5611_crc(uint16_t *prom)
static uint32_t ms5611_read_adc(void)
{
uint8_t rxbuf[3];
i2cRead(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_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)
Expand All @@ -154,7 +220,7 @@ static void ms5611_get_ut(void)

static void ms5611_start_up(void)
{
i2cWrite(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)
Expand Down
7 changes: 6 additions & 1 deletion src/main/target/ALIENFLIGHTF4/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@
#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 BARO
#define USE_BARO_MS5611
#define USE_BARO_BMP280
//#define USE_BARO_BMP280

#define INVERTER
#define BEEPER
Expand Down

0 comments on commit 219667c

Please sign in to comment.