Skip to content

Commit

Permalink
Add HMC5883/5983 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
MJ666 committed Jan 29, 2016
1 parent 41f0271 commit b75721d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 62 deletions.
12 changes: 12 additions & 0 deletions src/main/drivers/barometer_bmp280.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include "barometer_bmp280.h"

//#define DEBUG_BMP280

#ifdef BARO

// BMP280, address 0x76
Expand Down Expand Up @@ -200,6 +202,11 @@ static void bmp280_get_up(void)
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));

#ifdef DEBUG_BMP280
debug[0] = bmp280_up;
debug[0] = bmp280_ut;
#endif
}

// Returns temperature in DegC, resolution is 0.01 DegC. Output value of "5123" equals 51.23 DegC
Expand Down Expand Up @@ -245,6 +252,11 @@ STATIC_UNIT_TESTED void bmp280_calculate(int32_t *pressure, int32_t *temperature
t = bmp280_compensate_T(bmp280_ut);
p = bmp280_compensate_P(bmp280_up);

#ifdef DEBUG_BMP280
debug[2] = t;
debug[3] = p;
#endif

if (pressure)
*pressure = (int32_t)(p / 256);
if (temperature)
Expand Down
65 changes: 20 additions & 45 deletions src/main/drivers/barometer_ms5611.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "build_config.h"

#define DEBUG_MS5611
//#define DEBUG_MS5611

// MS5611, Standard address 0x77
#ifndef MS5611_ADDR
Expand Down Expand Up @@ -65,33 +65,11 @@ 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_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)
bool baroMS5611Write(uint8_t reg, uint8_t data)
{
ENABLE_MS5611;
delayMicroseconds(1);
Expand All @@ -102,7 +80,7 @@ bool baroMS5611WriteSPI(uint8_t reg, uint8_t data)
return true;
}

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

Expand All @@ -114,6 +92,16 @@ bool baroMS5611ReadSPI(uint8_t reg, uint8_t length, uint8_t *data)

return ack;
}
#else
bool baroMS5611Write(uint8_t reg, uint8_t data)
{
return i2cWrite(MS5611_ADDR, reg, data);
}

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

bool ms5611Detect(baro_t *baro)
Expand All @@ -124,21 +112,8 @@ 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) {
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
ack = baroMS5611Read(CMD_PROM_RD, 1, &sig);
if (!ack)
return false;

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

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

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

Expand Down Expand Up @@ -207,13 +182,13 @@ STATIC_UNIT_TESTED int8_t ms5611_crc(uint16_t *prom)
static uint32_t ms5611_read_adc(void)
{
uint8_t rxbuf[3];
baroMS5611config.read(CMD_ADC_READ, 3, rxbuf); // read ADC
baroMS5611Read(CMD_ADC_READ, 1, rxbuf); // read ADC
return (rxbuf[0] << 16) | (rxbuf[1] << 8) | rxbuf[2];
}

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

static void ms5611_get_ut(void)
Expand All @@ -226,7 +201,7 @@ static void ms5611_get_ut(void)

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

static void ms5611_get_up(void)
Expand Down
69 changes: 57 additions & 12 deletions src/main/drivers/compass_hmc5883l.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "nvic.h"
#include "gpio.h"
#include "bus_i2c.h"
#include "bus_spi.h"
#include "light_led.h"

#include "sensor.h"
Expand Down Expand Up @@ -197,15 +198,59 @@ static void hmc5883lConfigureDataReadyInterruptHandling(void)
#endif
}

#if defined(USE_SPI) && defined(HMC5883_SPI_INSTANCE)
#define DISABLE_HMC5883 GPIO_SetBits(HMC5883_CS_GPIO, HMC5883_CS_PIN)
#define ENABLE_HMC5883 GPIO_ResetBits(HMC5883_CS_GPIO, HMC5883_CS_PIN)

bool baroHMC5883Write(uint8_t reg, uint8_t data)
{
// spiSetDivisor(HMC5883_SPI_INSTANCE, 16);

ENABLE_HMC5883;
delayMicroseconds(1);
spiTransferByte(HMC5883_SPI_INSTANCE, reg);
spiTransferByte(HMC5883_SPI_INSTANCE, data);
DISABLE_HMC5883;

return true;
}

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

// spiSetDivisor(HMC5883_SPI_INSTANCE, 16);

ENABLE_HMC5883;
delayMicroseconds(1);
spiTransferByte(HMC5883_SPI_INSTANCE, reg | 0x80); // read transaction
ack = spiTransfer(HMC5883_SPI_INSTANCE, data, NULL, length);
DISABLE_HMC5883;

return ack;
}
#else
bool baroHMC5883Write(uint8_t reg, uint8_t data)
{
return i2cWrite(MAG_ADDRESS, reg, data);
}

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


bool hmc5883lDetect(mag_t* mag, const hmc5883Config_t *hmc5883ConfigToUse)
{
bool ack = false;
uint8_t sig = 0;
uint8_t buf[3];

hmc5883Config = hmc5883ConfigToUse;

ack = i2cRead(MAG_ADDRESS, 0x0A, 1, &sig);
if (!ack || sig != 'H')
ack = baroHMC5883Read(0x0A, 3, buf);
if (!ack || buf[0] != 'H') // || buf[1] != '4' || buf[2] != '3')
return false;

mag->init = hmc5883lInit;
Expand Down Expand Up @@ -246,15 +291,15 @@ void hmc5883lInit(void)
}

delay(50);
i2cWrite(MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to pos bias
baroHMC5883Write(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to pos bias
// Note that the very first measurement after a gain change maintains the same gain as the previous setting.
// The new gain setting is effective from the second measurement and on.
i2cWrite(MAG_ADDRESS, HMC58X3_R_CONFB, 0x60); // Set the Gain to 2.5Ga (7:5->011)
baroHMC5883Write(HMC58X3_R_CONFB, 0x60); // Set the Gain to 2.5Ga (7:5->011)
delay(100);
hmc5883lRead(magADC);

for (i = 0; i < 10; i++) { // Collect 10 samples
i2cWrite(MAG_ADDRESS, HMC58X3_R_MODE, 1);
baroHMC5883Write(HMC58X3_R_MODE, 1);
delay(50);
hmc5883lRead(magADC); // Get the raw values in case the scales have already been changed.

Expand All @@ -272,9 +317,9 @@ void hmc5883lInit(void)
}

// Apply the negative bias. (Same gain)
i2cWrite(MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to negative bias.
baroHMC5883Write(HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to negative bias.
for (i = 0; i < 10; i++) {
i2cWrite(MAG_ADDRESS, HMC58X3_R_MODE, 1);
baroHMC5883Write(HMC58X3_R_MODE, 1);
delay(50);
hmc5883lRead(magADC); // Get the raw values in case the scales have already been changed.

Expand All @@ -296,9 +341,9 @@ void hmc5883lInit(void)
magGain[Z] = fabsf(660.0f * HMC58X3_Z_SELF_TEST_GAUSS * 2.0f * 10.0f / xyz_total[Z]);

// leave test mode
i2cWrite(MAG_ADDRESS, HMC58X3_R_CONFA, 0x70); // Configuration Register A -- 0 11 100 00 num samples: 8 ; output rate: 15Hz ; normal measurement mode
i2cWrite(MAG_ADDRESS, HMC58X3_R_CONFB, 0x20); // Configuration Register B -- 001 00000 configuration gain 1.3Ga
i2cWrite(MAG_ADDRESS, HMC58X3_R_MODE, 0x00); // Mode register -- 000000 00 continuous Conversion Mode
baroHMC5883Write(HMC58X3_R_CONFA, 0x70); // Configuration Register A -- 0 11 100 00 num samples: 8 ; output rate: 15Hz ; normal measurement mode
baroHMC5883Write(HMC58X3_R_CONFB, 0x20); // Configuration Register B -- 001 00000 configuration gain 1.3Ga
baroHMC5883Write(HMC58X3_R_MODE, 0x00); // Mode register -- 000000 00 continuous Conversion Mode
delay(100);

if (!bret) { // Something went wrong so get a best guess
Expand All @@ -314,7 +359,7 @@ bool hmc5883lRead(int16_t *magData)
{
uint8_t buf[6];

bool ack = i2cRead(MAG_ADDRESS, MAG_DATA_REGISTER, 6, buf);
bool ack = baroHMC5883Read(MAG_DATA_REGISTER, 6, buf);
if (!ack) {
return false;
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/target/ALIENFLIGHTF4/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@
#define USE_GYRO_SPI_MPU9250
#define GYRO_MPU9250_ALIGN CW270_DEG

#define HMC5883_CS_GPIO GPIOA
#define HMC5883_CS_PIN GPIO_Pin_15
#define HMC5883_CS_GPIO_CLK_PERIPHERAL RCC_AHB1Periph_GPIOA
#define HMC5883_SPI_INSTANCE SPI3

#define MAG
#define USE_MAG_HMC5883
//#define MAG_HMC5883_ALIGN CW180_DEG
#define USE_MAG_AK8963
#define MAG_AK8963_ALIGN CW270_DEG
#define MAG_HMC5883_ALIGN CW180_DEG
//#define USE_MAG_AK8963
//#define MAG_AK8963_ALIGN CW270_DEG

#define MS5611_CS_GPIO GPIOA
#define MS5611_CS_PIN GPIO_Pin_15
Expand All @@ -69,8 +74,8 @@
//#define BMP280_SPI_INSTANCE SPI3

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

#define INVERTER
#define BEEPER
Expand Down

0 comments on commit b75721d

Please sign in to comment.