2011-12-28 05:32:21 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2015-12-01 12:07:15 -04:00
|
|
|
#pragma once
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2015-12-01 12:07:15 -04:00
|
|
|
#include "AP_Baro_Backend.h"
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/** Abstract serial bus device driver for I2C/SPI. */
|
|
|
|
class AP_SerialBus
|
2013-01-03 14:06:22 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/** Read a 24-bit value */
|
|
|
|
virtual uint32_t read_24bits(uint8_t reg) = 0;
|
2013-01-03 14:06:22 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/** Write to a register with no data. */
|
2015-08-17 13:43:16 -03:00
|
|
|
virtual bool write(uint8_t reg) = 0;
|
2013-01-03 14:06:22 -04:00
|
|
|
|
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) */
|
2014-10-19 16:22:51 -03:00
|
|
|
virtual bool sem_take_nonblocking() = 0;
|
|
|
|
virtual bool sem_take_blocking() = 0;
|
2013-01-03 14:06:22 -04:00
|
|
|
|
|
|
|
/** Release the internal semaphore for this device. */
|
2014-10-19 16:22:51 -03:00
|
|
|
virtual void sem_give() = 0;
|
2013-01-03 14:06:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** SPI serial device. */
|
2014-10-19 16:22:51 -03:00
|
|
|
class AP_SerialBus_SPI : public AP_SerialBus
|
2013-01-03 14:06:22 -04:00
|
|
|
{
|
|
|
|
public:
|
2014-10-19 16:22:51 -03:00
|
|
|
AP_SerialBus_SPI(enum AP_HAL::SPIDevice device, enum AP_HAL::SPIDeviceDriver::bus_speed speed);
|
|
|
|
void init();
|
|
|
|
uint16_t read_16bits(uint8_t reg);
|
|
|
|
uint32_t read_24bits(uint8_t reg);
|
|
|
|
uint32_t read_adc(uint8_t reg);
|
2015-08-17 13:43:16 -03:00
|
|
|
bool write(uint8_t reg);
|
2014-10-19 16:22:51 -03:00
|
|
|
bool sem_take_nonblocking();
|
|
|
|
bool sem_take_blocking();
|
|
|
|
void sem_give();
|
2013-01-03 14:06:22 -04:00
|
|
|
|
|
|
|
private:
|
2014-10-19 16:22:51 -03:00
|
|
|
enum AP_HAL::SPIDevice _device;
|
2015-04-24 02:13:02 -03:00
|
|
|
enum AP_HAL::SPIDeviceDriver::bus_speed _speed;
|
2013-01-03 14:06:22 -04:00
|
|
|
AP_HAL::SPIDeviceDriver *_spi;
|
|
|
|
AP_HAL::Semaphore *_spi_sem;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** I2C serial device. */
|
2014-10-19 16:22:51 -03:00
|
|
|
class AP_SerialBus_I2C : public AP_SerialBus
|
2013-01-03 14:06:22 -04:00
|
|
|
{
|
|
|
|
public:
|
2015-07-10 00:56:06 -03:00
|
|
|
AP_SerialBus_I2C(AP_HAL::I2CDriver *i2c, uint8_t addr);
|
2014-10-19 16:22:51 -03:00
|
|
|
void init();
|
|
|
|
uint16_t read_16bits(uint8_t reg);
|
|
|
|
uint32_t read_24bits(uint8_t reg);
|
2015-08-17 13:43:16 -03:00
|
|
|
bool write(uint8_t reg);
|
2014-10-19 16:22:51 -03:00
|
|
|
bool sem_take_nonblocking();
|
|
|
|
bool sem_take_blocking();
|
|
|
|
void sem_give();
|
2013-01-04 18:26:26 -04:00
|
|
|
|
|
|
|
private:
|
2015-07-10 00:56:06 -03:00
|
|
|
AP_HAL::I2CDriver *_i2c;
|
2014-10-19 16:22:51 -03:00
|
|
|
uint8_t _addr;
|
2013-01-04 18:26:26 -04:00
|
|
|
AP_HAL::Semaphore *_i2c_sem;
|
2013-01-03 14:06:22 -04:00
|
|
|
};
|
|
|
|
|
2015-07-10 00:56:06 -03:00
|
|
|
class AP_Baro_MS56XX : public AP_Baro_Backend
|
2011-11-27 01:43:34 -04:00
|
|
|
{
|
2012-08-17 03:09:24 -03:00
|
|
|
public:
|
2014-10-19 16:22:51 -03:00
|
|
|
void update();
|
2015-01-06 01:28:11 -04:00
|
|
|
void accumulate();
|
2013-01-03 14:06:22 -04:00
|
|
|
|
2015-11-26 10:56:10 -04:00
|
|
|
protected:
|
|
|
|
AP_Baro_MS56XX(AP_Baro &baro, AP_SerialBus *serial, bool use_timer);
|
|
|
|
void _init();
|
|
|
|
|
2015-07-10 00:56:06 -03:00
|
|
|
virtual void _calculate() = 0;
|
2015-11-26 10:56:10 -04:00
|
|
|
virtual bool _read_prom(uint16_t prom[8]);
|
|
|
|
void _timer();
|
2015-11-04 18:17:38 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
AP_SerialBus *_serial;
|
2014-07-07 00:11:41 -03:00
|
|
|
|
2012-08-17 03:09:24 -03:00
|
|
|
/* Asynchronous state: */
|
2014-10-19 16:22:51 -03:00
|
|
|
volatile bool _updated;
|
|
|
|
volatile uint8_t _d1_count;
|
|
|
|
volatile uint8_t _d2_count;
|
|
|
|
volatile uint32_t _s_D1, _s_D2;
|
|
|
|
uint8_t _state;
|
|
|
|
uint32_t _last_timer;
|
2012-11-19 21:23:26 -04:00
|
|
|
|
2015-01-06 01:28:11 -04:00
|
|
|
bool _use_timer;
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// Internal calibration registers
|
2015-12-07 16:23:48 -04:00
|
|
|
uint16_t _c1,_c2,_c3,_c4,_c5,_c6;
|
2015-07-10 00:56:06 -03:00
|
|
|
float _D1,_D2;
|
|
|
|
uint8_t _instance;
|
2011-11-05 22:11:25 -03:00
|
|
|
};
|
|
|
|
|
2015-07-10 00:56:06 -03:00
|
|
|
class AP_Baro_MS5611 : public AP_Baro_MS56XX
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AP_Baro_MS5611(AP_Baro &baro, AP_SerialBus *serial, bool use_timer);
|
|
|
|
private:
|
|
|
|
void _calculate();
|
|
|
|
};
|
|
|
|
|
|
|
|
class AP_Baro_MS5607 : public AP_Baro_MS56XX
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AP_Baro_MS5607(AP_Baro &baro, AP_SerialBus *serial, bool use_timer);
|
|
|
|
private:
|
|
|
|
void _calculate();
|
|
|
|
};
|
2015-09-28 15:31:12 -03:00
|
|
|
|
|
|
|
class AP_Baro_MS5637 : public AP_Baro_MS56XX
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AP_Baro_MS5637(AP_Baro &baro, AP_SerialBus *serial, bool use_timer);
|
|
|
|
private:
|
|
|
|
void _calculate();
|
2015-11-26 10:56:10 -04:00
|
|
|
bool _read_prom(uint16_t prom[8]) override;
|
2015-09-28 15:31:12 -03:00
|
|
|
};
|