Skip to content

Commit

Permalink
update bmp280, i2c sitl and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Dec 17, 2023
1 parent c9f5d6b commit 341b1fe
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 14 deletions.
3 changes: 3 additions & 0 deletions platform_specific/hal_i2c/hal_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
extern "C" {
#endif

/**
* @return either LIBPERIPH_OK on success or LIBPERIPH_ERROR on failure
*/
int8_t i2cTransmit(uint8_t id, const uint8_t tx[], uint8_t len);
int8_t i2cReceive(uint8_t id, uint8_t* rx, uint8_t len);

Expand Down
14 changes: 10 additions & 4 deletions platform_specific/hal_i2c/ubuntu/hal_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@ int8_t i2cTransmit(uint8_t id, const uint8_t tx[], uint8_t len) {
return LIBPERIPH_ERROR;
}

bool is_transmited = false;
for (size_t idx = 0; idx < SitlI2CSensor::number_of_sensor; idx++) {
SitlI2CSensor::i2c_sensors[idx]->callback_on_i2c_transmit(id, tx, len);
if (SitlI2CSensor::i2c_sensors[idx]->callback_on_i2c_transmit(id, tx, len) >= 0) {
is_transmited = true;
}
}

return LIBPERIPH_OK;
return is_transmited ? LIBPERIPH_OK : LIBPERIPH_ERROR;
}

int8_t i2cReceive(uint8_t id, uint8_t* rx, uint8_t len) {
if (rx == NULL || len == 0) {
return LIBPERIPH_ERROR;
}

bool is_received = false;
for (size_t idx = 0; idx < SitlI2CSensor::number_of_sensor; idx++) {
SitlI2CSensor::i2c_sensors[idx]->callback_on_i2c_receive(id, rx, len);
if (SitlI2CSensor::i2c_sensors[idx]->callback_on_i2c_receive(id, rx, len) >= 0) {
is_received = true;
}
}

return LIBPERIPH_OK;
return is_received ? LIBPERIPH_OK : LIBPERIPH_ERROR;
}

std::array<SitlI2CSensor*, SitlI2CSensor::MAX_NUMBER_OF_SENSOR> SitlI2CSensor::i2c_sensors;
Expand Down
15 changes: 11 additions & 4 deletions sensors/barometer/bmp280.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,21 @@ void bmp280Calibrate() {

int8_t bmp280CollectData() {
uint8_t tx[1] = {PRESS_REG};
BMP280_measurement_registers_t data;
i2cTransmit(I2C_ID, tx, 1);
i2cReceive(I2C_ID + 1, (uint8_t*)&data, 6);
BMP280_measurement_registers_t data = {};

int8_t res = LIBPERIPH_BPM280_OK;
if (i2cTransmit(I2C_ID, tx, 1) < 0) {
res = LIBPERIPH_BPM280_NO_RESPONSE;
}

if (i2cReceive(I2C_ID + 1, (uint8_t*)&data, 6) < 0) {
res = LIBPERIPH_BPM280_NO_RESPONSE;
}

bmp280.static_pressure = (float)(data.p_msb << 12 | data.p_lsb << 4 | data.p_xlsb >> 4);
bmp280.static_temperature = (float)(data.t_msb << 12 | data.t_lsb << 4 | data.t_xlsb >> 4);

return LIBPERIPH_OK;
return res;
}


Expand Down
9 changes: 9 additions & 0 deletions sensors/barometer/bmp280.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ extern "C" {

#define BMP280_MAX_MEASUREMENT_FREQUENCY 50

#define LIBPERIPH_BPM280_OK 0
#define LIBPERIPH_BPM280_NO_RESPONSE -1
#define LIBPERIPH_BPM280_BAD_RESPONSE -2

int8_t bmp280Init();
bool bmp280IsInitialized();
void bmp280Calibrate();

/**
* @return LIBPERIPH_BPM280_OK is ok, otherwise error code < 0
*/
int8_t bmp280CollectData();

void bmp280ParseData();

float bmp280GetStaticPressure();
Expand Down
14 changes: 14 additions & 0 deletions tests/periphery/test_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@
#include <gtest/gtest.h>
#include "hal_i2c.h"
#include "libperiph_common.h"
#include "ubuntu/sitl_i2c.hpp"

uint8_t ubuntu_i2c_id = 42;
uint8_t ubuntu_i2c_buffer[256] = {};

class SimpleSitlI2CSensor : public SitlI2CSensor {
public:
SimpleSitlI2CSensor() : SitlI2CSensor(0xFF) {}
int8_t callback_on_i2c_transmit(uint8_t id, const uint8_t tx[], uint8_t len) override {
return 0;
}
int8_t callback_on_i2c_receive(uint8_t id, uint8_t* rx, uint8_t len) override {
return 0;
}
};

static SimpleSitlI2CSensor simple_sitl_i2c_sensor;

TEST(hal_i2c, test_i2cTransmit) {
uint8_t tx_buffer = 42;
ASSERT_EQ(LIBPERIPH_OK, i2cTransmit(0, &tx_buffer, 1));
Expand Down
47 changes: 41 additions & 6 deletions tests/sensors/test_bmp280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,55 @@
#include <iostream>
#include <gtest/gtest.h>
#include "barometer/bmp280.h"
#include "ubuntu/sitl_i2c.hpp"

struct Bmp280 : public SitlI2CSensor {
Bmp280() : SitlI2CSensor(0xC4) {}
int8_t callback_on_i2c_transmit(uint8_t id, const uint8_t tx[], uint8_t len) override {
return i2c_tx_status;
}
int8_t callback_on_i2c_receive(uint8_t id, uint8_t* rx, uint8_t len) override {
return i2c_rx_status;
}

int8_t i2c_tx_status = 0;
int8_t i2c_rx_status = 0;
};

static Bmp280 bmp280;

TEST(BMP280, test_bmp280ParseData_normal) {
bmp280.i2c_tx_status = 0;
bmp280.i2c_rx_status = 0;

TEST(BMP280, test_bmp280ParseData) {
bmp280Init();
bmp280Calibrate();

bmp280CollectData();
ASSERT_TRUE(bmp280CollectData() >= 0);
bmp280ParseData();

float pressure = bmp280GetStaticPressure();
float temperature = bmp280GetStaticTemperature();
std::cout << "pressure: " << pressure << std::endl;
std::cout << "temperature: " << temperature << std::endl;
bmp280GetStaticPressure();
bmp280GetStaticTemperature();
}

TEST(BMP280, test_bmp280ParseData_no_rx_response) {
bmp280Init();
bmp280Calibrate();

bmp280.i2c_tx_status = -1;
bmp280.i2c_rx_status = 0;

ASSERT_FALSE(bmp280CollectData() >= 0);
}

TEST(BMP280, test_bmp280ParseData_no_tx_response) {
bmp280Init();
bmp280Calibrate();

bmp280.i2c_tx_status = 0;
bmp280.i2c_rx_status = -1;

ASSERT_FALSE(bmp280CollectData() >= 0);
}

TEST(BMP280, test_bmp280IsInitialized) {
Expand Down

0 comments on commit 341b1fe

Please sign in to comment.