2011-12-28 05:32:21 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2011-11-27 01:49:17 -04:00
|
|
|
#ifndef __AP_BARO_MS5611_H__
|
|
|
|
#define __AP_BARO_MS5611_H__
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2012-11-19 21:23:26 -04:00
|
|
|
#include <AP_HAL.h>
|
2011-11-30 00:31:10 -04:00
|
|
|
#include "AP_Baro.h"
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2014-08-01 03:03:39 -03:00
|
|
|
#if CONFIG_HAL_BOARD != HAL_BOARD_APM2 && CONFIG_HAL_BOARD != HAL_BOARD_APM1
|
2014-07-08 00:27:06 -03:00
|
|
|
#define MS5611_WITH_I2C 1
|
|
|
|
#else
|
|
|
|
#define MS5611_WITH_I2C 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
/** Abstract serial device driver for MS5611. */
|
|
|
|
class AP_Baro_MS5611_Serial
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Initialize the driver. */
|
|
|
|
virtual void init() = 0;
|
|
|
|
|
|
|
|
/** Read a 16-bit value from register "reg". */
|
|
|
|
virtual uint16_t read_16bits(uint8_t reg) = 0;
|
|
|
|
|
|
|
|
/** Read a 24-bit value from the ADC. */
|
|
|
|
virtual uint32_t read_adc() = 0;
|
|
|
|
|
|
|
|
/** Write a single byte command. */
|
|
|
|
virtual void write(uint8_t reg) = 0;
|
|
|
|
|
2013-01-03 15:05:00 -04:00
|
|
|
/** Acquire the internal semaphore for this device.
|
|
|
|
* take_nonblocking should be used from the timer process,
|
|
|
|
* take_blocking from synchronous code (i.e. init) */
|
|
|
|
virtual bool sem_take_nonblocking() { return true; }
|
|
|
|
virtual bool sem_take_blocking() { return true; }
|
2013-01-03 14:06:22 -04:00
|
|
|
|
|
|
|
/** Release the internal semaphore for this device. */
|
2013-01-03 15:05:00 -04:00
|
|
|
virtual void sem_give() {}
|
2013-01-03 14:06:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** SPI serial device. */
|
|
|
|
class AP_Baro_MS5611_SPI : public AP_Baro_MS5611_Serial
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void init();
|
|
|
|
virtual uint16_t read_16bits(uint8_t reg);
|
|
|
|
virtual uint32_t read_adc();
|
|
|
|
virtual void write(uint8_t reg);
|
2013-01-03 15:05:00 -04:00
|
|
|
virtual bool sem_take_nonblocking();
|
|
|
|
virtual bool sem_take_blocking();
|
|
|
|
virtual void sem_give();
|
2013-01-03 14:06:22 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
AP_HAL::SPIDeviceDriver *_spi;
|
|
|
|
AP_HAL::Semaphore *_spi_sem;
|
|
|
|
};
|
|
|
|
|
2014-07-08 00:27:06 -03:00
|
|
|
#if MS5611_WITH_I2C
|
2013-01-03 14:06:22 -04:00
|
|
|
/** I2C serial device. */
|
|
|
|
class AP_Baro_MS5611_I2C : public AP_Baro_MS5611_Serial
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void init();
|
|
|
|
virtual uint16_t read_16bits(uint8_t reg);
|
|
|
|
virtual uint32_t read_adc();
|
|
|
|
virtual void write(uint8_t reg);
|
2013-01-04 18:26:26 -04:00
|
|
|
virtual bool sem_take_nonblocking();
|
|
|
|
virtual bool sem_take_blocking();
|
|
|
|
virtual void sem_give();
|
|
|
|
|
|
|
|
private:
|
|
|
|
AP_HAL::Semaphore *_i2c_sem;
|
2013-01-03 14:06:22 -04:00
|
|
|
};
|
2014-07-08 00:27:06 -03:00
|
|
|
#endif // MS5611_WITH_I2C
|
2013-01-03 14:06:22 -04:00
|
|
|
|
2011-11-30 00:31:10 -04:00
|
|
|
class AP_Baro_MS5611 : public AP_Baro
|
2011-11-27 01:43:34 -04:00
|
|
|
{
|
2012-08-17 03:09:24 -03:00
|
|
|
public:
|
2013-01-03 14:06:22 -04:00
|
|
|
AP_Baro_MS5611(AP_Baro_MS5611_Serial *serial)
|
|
|
|
{
|
|
|
|
_serial = serial;
|
|
|
|
}
|
2012-08-17 03:09:24 -03:00
|
|
|
|
|
|
|
/* AP_Baro public interface: */
|
2012-10-11 14:53:21 -03:00
|
|
|
bool init();
|
2012-08-17 03:09:24 -03:00
|
|
|
uint8_t read();
|
|
|
|
float get_pressure(); // in mbar*100 units
|
2013-09-21 08:30:41 -03:00
|
|
|
float get_temperature(); // in celsius degrees
|
2012-08-17 03:09:24 -03:00
|
|
|
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
/* Serial port drivers to pass to "init". */
|
|
|
|
static AP_Baro_MS5611_SPI spi;
|
2014-07-08 00:27:06 -03:00
|
|
|
#if MS5611_WITH_I2C
|
2013-01-03 14:06:22 -04:00
|
|
|
static AP_Baro_MS5611_I2C i2c;
|
2014-07-08 00:27:06 -03:00
|
|
|
#endif
|
2013-01-03 14:06:22 -04:00
|
|
|
|
2012-08-17 03:09:24 -03:00
|
|
|
private:
|
2014-02-08 02:47:45 -04:00
|
|
|
void _calculate();
|
2012-08-17 03:09:24 -03:00
|
|
|
/* Asynchronous handler functions: */
|
2013-09-28 03:30:26 -03:00
|
|
|
void _update();
|
2014-07-07 00:11:41 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD != HAL_BOARD_APM2
|
|
|
|
bool check_crc(void);
|
|
|
|
#endif
|
|
|
|
|
2012-08-17 03:09:24 -03:00
|
|
|
/* Asynchronous state: */
|
|
|
|
static volatile bool _updated;
|
|
|
|
static volatile uint8_t _d1_count;
|
|
|
|
static volatile uint8_t _d2_count;
|
|
|
|
static volatile uint32_t _s_D1, _s_D2;
|
|
|
|
static uint8_t _state;
|
|
|
|
static uint32_t _timer;
|
2013-01-03 14:06:22 -04:00
|
|
|
static AP_Baro_MS5611_Serial *_serial;
|
2012-08-17 03:09:24 -03:00
|
|
|
/* Gates access to asynchronous state: */
|
|
|
|
static bool _sync_access;
|
|
|
|
|
|
|
|
float Temp;
|
|
|
|
float Press;
|
|
|
|
|
|
|
|
int32_t _raw_press;
|
|
|
|
int32_t _raw_temp;
|
|
|
|
// Internal calibration registers
|
|
|
|
uint16_t C1,C2,C3,C4,C5,C6;
|
|
|
|
float D1,D2;
|
2012-11-19 21:23:26 -04:00
|
|
|
|
2011-11-05 22:11:25 -03:00
|
|
|
};
|
|
|
|
|
2011-11-27 01:49:17 -04:00
|
|
|
#endif // __AP_BARO_MS5611_H__
|