Skip to content

Commit

Permalink
Merge pull request #273 from es-ude/272-fix-bmi323-library-main
Browse files Browse the repository at this point in the history
fix(sensor): fix bmi323 library
  • Loading branch information
DavidFederl authored Sep 4, 2024
2 parents bc2ad9b + 47632bf commit 63545ac
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 73 deletions.
13 changes: 7 additions & 6 deletions src/sensor/bmi323/Bmi323.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ void bmi323SetRegister(bmi323SensorConfiguration_t *sensor, uint8_t address, dat
void bmi323EnableFeature(bmi323SensorConfiguration_t *sensor, bmi323Features_t *features) {
bmi323ProcessErrorCode(bmi323_select_sensor(features, sensor));
}
void bmi323SetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t feature,
void bmi323SetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t numberOfFeatures,
bmi323FeatureConfiguration_t *config) {
bmi323ProcessErrorCode(bmi323_set_sensor_config(config, feature, sensor));
bmi323ProcessErrorCode(bmi323_set_sensor_config(config, numberOfFeatures, sensor));
}
void bmi323GetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t feature,
void bmi323GetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t numberOfFeatures,
bmi323FeatureConfiguration_t *config) {
bmi323ProcessErrorCode(bmi323_get_sensor_config(config, feature, sensor));
bmi323ProcessErrorCode(bmi323_get_sensor_config(config, numberOfFeatures, sensor));
}

void bmi323GetInterruptConfig(bmi323SensorConfiguration_t *sensor,
Expand Down Expand Up @@ -165,8 +165,9 @@ void bmi323SetRemappingOfAxes(bmi323SensorConfiguration_t *sensor, bmi323AxesRem

/* region DATA */

void bmi323GetData(bmi323SensorConfiguration_t *sensor, uint8_t feature, bmi323SensorData_t *data) {
bmi323ProcessErrorCode(bmi323_get_sensor_data(data, feature, sensor));
void bmi323GetData(bmi323SensorConfiguration_t *sensor, uint8_t numberOfFeatures,
bmi323SensorData_t *data) {
bmi323ProcessErrorCode(bmi323_get_sensor_data(data, numberOfFeatures, sensor));
}

float bmi323GetSensorTime(bmi323SensorConfiguration_t *sensor) {
Expand Down
40 changes: 17 additions & 23 deletions src/sensor/bmi323/include/Bmi323.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @brief possible error codes for the API
* @ingroup bmi323Types
*/
enum bmi323ErrorCodes {
typedef enum bmi323ErrorCodes {
BMI323_ERROR_NULL_PTR = BMI3_E_NULL_PTR,
BMI323_ERROR_COM_FAIL = BMI3_E_COM_FAIL,
BMI323_ERROR_DEV_NOT_FOUND = BMI3_E_DEV_NOT_FOUND,
Expand All @@ -40,14 +40,7 @@ enum bmi323ErrorCodes {
BMI323_ERROR_INVALID_ST_SELECTION = BMI3_E_INVALID_ST_SELECTION,
BMI323_ERROR_OUT_OF_RANGE = BMI3_E_OUT_OF_RANGE,
BMI323_ERROR_FEATURE_ENGINE_STATUS = BMI3_E_FEATURE_ENGINE_STATUS,
};

enum bmi323StatusRegister {
BMI323_POWER_UP_DETECTED = 0x0000,
BMI323_DATA_READY_TEMPERATURE = 0x0020,
BMI323_DATA_READY_GYROSCOPE = 0x0040,
BMI323_DATA_READY_ACCELEROMETER = 0x0080,
};
} bmi323ErrorCodes_t;

/*!
* @brief struct holding the configuration for the sensor
Expand Down Expand Up @@ -248,14 +241,14 @@ void bmi323EnableFeature(bmi323SensorConfiguration_t *sensor, bmi323Features_t *
* @code
* void bmi323SetSensorConfiguration (
* bmi323SensorConfiguration_t *sensor,
* uint8_t feature,
* uint8_t numberOfFeatures,
* bmi323FeatureConfiguration *config
* );
* @endcode
*
* @param[in] sensor struct holding the sensor configuration
* @param[in] feature sensor feature to configure
* @param[in] config feature configuration
* @param[in] numberOfFeatures number of sensor features to configure
* @param[in] config array containing one or more feature configurations
*
* @Features
* @verbatim
Expand All @@ -280,7 +273,7 @@ void bmi323EnableFeature(bmi323SensorConfiguration_t *sensor, bmi323Features_t *
*
* @throws ErrorCodes See #bmi323ErrorCodes
*/
void bmi323SetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t feature,
void bmi323SetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t numberOfFeatures,
bmi323FeatureConfiguration_t *config);

/*!
Expand All @@ -290,14 +283,14 @@ void bmi323SetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t f
* @code
* void bmi323GetSensorConfiguration(
* bmi323SensorConfiguration_t *sensor,
* uint8_t feature,
* uint8_t numberOfFeatures,
* bmi323FeatureConfiguration *config
* );
* @endcode
*
* @param[in] sensor struct holding the sensor configuration
* @param[in] feature sensor feature to configure
* @param[out] config feature configuration
* @param[in] numberOfFeatures number of sensor features to read configuration from
* @param[out] config array containing one or more feature configurations
*
* @Features
* @verbatim
Expand All @@ -322,7 +315,7 @@ void bmi323SetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t f
*
* @throws ErrorCodes See #bmi323ErrorCodes
*/
void bmi323GetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t feature,
void bmi323GetSensorConfiguration(bmi323SensorConfiguration_t *sensor, uint8_t numberOfFeatures,
bmi323FeatureConfiguration_t *config);

/*!
Expand Down Expand Up @@ -458,14 +451,14 @@ void bmi323SetRemappingOfAxes(bmi323SensorConfiguration_t *sensor, bmi323AxesRem
* @code
* void bmi323GetData(
* bmi323SensorConfiguration_t *sensor,
* uint8_t feature,
* uint8_t numberOfFeatures,
* bmi323SensorData_t *data
* );
* @endcode
*
* @param[in] sensor struct holding the sensor configuration
* @param[in] feature sensor feature to read from
* @param[out]data buffer to store the read data
* @param[in] numberOfFeatures number of sensor features to read from
* @param[out]data array holding the buffer to store the read data
*
* @Features
* @verbatim
Expand All @@ -490,7 +483,8 @@ void bmi323SetRemappingOfAxes(bmi323SensorConfiguration_t *sensor, bmi323AxesRem
*
* @throws ErrorCodes See #bmi323ErrorCodes
*/
void bmi323GetData(bmi323SensorConfiguration_t *sensor, uint8_t feature, bmi323SensorData_t *data);
void bmi323GetData(bmi323SensorConfiguration_t *sensor, uint8_t numberOfFeatures,
bmi323SensorData_t *data);

/*!
* @brief read temperature from the sensor
Expand Down Expand Up @@ -787,14 +781,14 @@ void bmi323SoftReset(bmi323SensorConfiguration_t *sensor);
* @endcode
*
* @param[in] sensor struct holding the sensor configuration
* @param[in] feature sensor features to test
* @param[in] features sensor features to test
* @param[out] results results of the performed tests
*
* @verbatim
* feature | Value
* -------------------- |--------
* BMI3_ST_ACCEL_ONLY | 1
* BMI3_ST_ACCEL_ONLY | 2
* BMI3_ST_GYRO_ONLY | 2
* BMI3_ST_BOTH_ACC_GYR | 3
* @endverbatim
*
Expand Down
148 changes: 104 additions & 44 deletions test/hardware/Sensors/HardwaretestBmi323.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "Spi.h"
#include "SpiTypedefs.h"

#define LIMIT 5
#define LIMIT (100)

spiConfiguration_t spi = {
.spiInstance = BMI323_SPI_MODULE,
Expand All @@ -25,7 +25,7 @@ spiConfiguration_t spi = {
.baudrate = BMI323_SPI_BAUDRATE,
};

bmi323SensorConfiguration_t bmi323 = {0};
bmi323SensorConfiguration_t bmi323[1];

static void initializeCommunication(void) {
env5HwControllerInit();
Expand All @@ -48,7 +48,7 @@ static void initializeBmi323(void) {

CEXCEPTION_T exception;
Try {
bmi323Init(&bmi323, &spi);
bmi323Init(bmi323, &spi);
}
Catch(exception) {
PRINT("ERROR: %i", exception);
Expand All @@ -60,9 +60,9 @@ static void initializeBmi323(void) {
static void getChipId(void) {
CEXCEPTION_T exception;
Try {
volatile uint8_t chipId[2];
uint8_t chipId[2];
data_t idData = {.data = chipId, .length = sizeof(chipId)};
bmi323GetRegister(&bmi323, BMI3_REG_CHIP_ID, &idData);
bmi323GetRegister(bmi323, BMI3_REG_CHIP_ID, &idData);
PRINT("Chip-ID: 0x%04X", chipId[0] << 8 | chipId[1]);
}
Catch(exception) {
Expand All @@ -71,28 +71,87 @@ static void getChipId(void) {
}
}

static void initializeAccelerometer(void) {
CEXCEPTION_T exception;
Try {
bmi323InterruptMapping_t mapping = {.acc_drdy_int = BMI323_INTERRUPT_1};
bmi323SetInterruptMapping(bmi323, mapping);
PRINT("Interrupt Mapped");

bmi323FeatureConfiguration_t config[1];
config[0].type = BMI323_ACCEL;
bmi323GetSensorConfiguration(bmi323, 1, config);
config[0].cfg.acc.odr = BMI3_ACC_ODR_100HZ;
config[0].cfg.acc.range = BMI3_ACC_RANGE_16G;
config[0].cfg.gyr.bwp = BMI3_ACC_BW_ODR_QUARTER;
config[0].cfg.gyr.avg_num = BMI3_ACC_AVG1;
config[0].cfg.gyr.gyr_mode = BMI3_ACC_MODE_NORMAL;
bmi323SetSensorConfiguration(bmi323, 1, config);
PRINT("Accelerometer configured");
}
Catch(exception) {
PRINT("Error: %i", exception);
gotoBootloader();
}
}
static void getAccelerometerData() {
CEXCEPTION_T exception;
uint8_t index = 0;
while (index <= LIMIT) {
Try {
uint16_t interrupt = bmi323GetInterruptStatus(bmi323, BMI323_INTERRUPT_1);
if (interrupt & BMI3_INT_STATUS_ACC_DRDY) {
bmi323SensorData_t data[1];
data[0].type = BMI323_ACCEL;
bmi323GetData(bmi323, 1, data);

float x = bmi323LsbToMps2(data[0].sens_data.acc.x, BMI3_ACC_RANGE_16G,
bmi323[0].resolution);
float y = bmi323LsbToMps2(data[0].sens_data.acc.y, BMI3_ACC_RANGE_16G,
bmi323[0].resolution);
float z = bmi323LsbToMps2(data[0].sens_data.acc.z, BMI3_ACC_RANGE_16G,
bmi323[0].resolution);

PRINT("%d:\n"
"\tRange=%dg\n"
"\tRaw: X=0x%X, Y=0x%X, Z=0x%X\n"
"\tMPS2: X=%4.2f, Y=%4.2f, Z=%4.2f",
index, 16, data[0].sens_data.acc.x, data[0].sens_data.acc.y,
data[0].sens_data.acc.z, x, y, z);

index++;
}
}
Catch(exception) {
PRINT("ERROR: %i", exception);
}
}
}
static void testAccelerometer(void) {
initializeAccelerometer();
getAccelerometerData();
}

static void initializeGyroscope(void) {
CEXCEPTION_T exception;
Try {
bmi323FeatureConfiguration_t config = {0};
config.type = BMI323_GYRO;
bmi323GetSensorConfiguration(&bmi323, BMI323_GYRO, &config);

config.cfg.gyr.odr = BMI3_GYR_ODR_100HZ;
config.cfg.gyr.range = BMI3_GYR_RANGE_2000DPS;
config.cfg.gyr.bwp = BMI3_GYR_BW_ODR_HALF;
config.cfg.gyr.gyr_mode = BMI3_GYR_MODE_NORMAL;
config.cfg.gyr.avg_num = BMI3_GYR_AVG1;
bmi323SetSensorConfiguration(&bmi323, BMI323_GYRO, &config);
bmi323FeatureConfiguration_t config[0];
config[0].type = BMI323_GYRO;
bmi323GetSensorConfiguration(bmi323, 1, config);
config[0].cfg.gyr.odr = BMI3_GYR_ODR_100HZ;
config[0].cfg.gyr.range = BMI3_GYR_RANGE_2000DPS;
config[0].cfg.gyr.bwp = BMI3_GYR_BW_ODR_HALF;
config[0].cfg.gyr.gyr_mode = BMI3_GYR_MODE_NORMAL;
config[0].cfg.gyr.avg_num = BMI3_GYR_AVG1;
bmi323SetSensorConfiguration(bmi323, 1, config);
PRINT("Gyroscope configured");

bmi323InterruptMapping_t mapping = {0};
mapping.gyr_drdy_int = BMI323_INTERRUPT_1;
bmi323SetInterruptMapping(&bmi323, mapping);
bmi323InterruptMapping_t mapping = {.gyr_drdy_int = BMI323_INTERRUPT_1};
bmi323SetInterruptMapping(bmi323, mapping);
PRINT("Interrupt Mapped");

bmi323SelfCalibrationResults_t calibration;
bmi323PerformGyroscopeSelfCalibration(&bmi323, BMI3_SC_OFFSET_EN, BMI3_SC_APPLY_CORR_EN,
bmi323PerformGyroscopeSelfCalibration(bmi323, BMI3_SC_OFFSET_EN, BMI3_SC_APPLY_CORR_EN,
&calibration);
PRINT("Calibration Results: %X", calibration.gyro_sc_rslt);
}
Expand All @@ -106,28 +165,28 @@ static void getGyroscopeData() {
uint8_t index = 0;
while (index <= LIMIT) {
Try {
uint16_t interrupt = bmi323GetInterruptStatus(&bmi323, BMI323_INTERRUPT_1);
uint16_t interrupt = bmi323GetInterruptStatus(bmi323, BMI323_INTERRUPT_1);
if (!(interrupt & BMI3_INT_STATUS_GYR_DRDY)) {
continue;
}

bmi323SensorData_t data = {0};
data.type = BMI323_GYRO;
bmi323GetData(&bmi323, BMI323_GYRO, &data);
bmi323SensorData_t data[1];
data[0].type = BMI323_GYRO;
bmi323GetData(bmi323, 1, data);

float x =
bmi323LsbToDps(data.sens_data.gyr.x, BMI3_GYR_RANGE_2000DPS, bmi323.resolution);
float y =
bmi323LsbToDps(data.sens_data.gyr.y, BMI3_GYR_RANGE_2000DPS, bmi323.resolution);
float z =
bmi323LsbToDps(data.sens_data.gyr.z, BMI3_GYR_RANGE_2000DPS, bmi323.resolution);
float x = bmi323LsbToDps(data[0].sens_data.gyr.x, BMI3_GYR_RANGE_2000DPS,
bmi323[0].resolution);
float y = bmi323LsbToDps(data[0].sens_data.gyr.y, BMI3_GYR_RANGE_2000DPS,
bmi323[0].resolution);
float z = bmi323LsbToDps(data[0].sens_data.gyr.z, BMI3_GYR_RANGE_2000DPS,
bmi323[0].resolution);

PRINT("%d:\n"
"\tRange=%dDPS\n"
"\tRaw: X=0x%X, Y=0x%X, Z=0x%X\n"
"\tDPS: X=%4.2f, Y=%4.2f, Z=%4.2f",
index, 2000, data.sens_data.gyr.x, data.sens_data.gyr.y, data.sens_data.gyr.z, x,
y, z);
index, 2000, data[0].sens_data.gyr.x, data[0].sens_data.gyr.y,
data[0].sens_data.gyr.z, x, y, z);

index++;
}
Expand All @@ -144,17 +203,16 @@ static void testGyroscope(void) {
static void initializeTemperature(void) {
CEXCEPTION_T exception;
Try {
bmi323FeatureConfiguration_t config = {0};
bmi323GetSensorConfiguration(&bmi323, BMI323_GYRO, &config);
config.type = BMI3_ACCEL;
config.cfg.acc.acc_mode = BMI3_ACC_MODE_NORMAL;
bmi323SetSensorConfiguration(&bmi323, BMI323_GYRO, &config);
PRINT("Sensor Configured");

bmi323InterruptMapping_t mapping = {0};
mapping.temp_drdy_int = BMI323_INTERRUPT_1;
bmi323SetInterruptMapping(&bmi323, mapping);
bmi323InterruptMapping_t mapping = {.temp_drdy_int = BMI323_INTERRUPT_1};
bmi323SetInterruptMapping(bmi323, mapping);
PRINT("Interrupt Enabled");

bmi323FeatureConfiguration_t config[1];
config[0].type = BMI3_ACCEL;
bmi323GetSensorConfiguration(bmi323, 1, config);
config[0].cfg.acc.acc_mode = BMI3_ACC_MODE_NORMAL;
bmi323SetSensorConfiguration(bmi323, 1, config);
PRINT("Sensor Configured");
}
Catch(exception) {
PRINT("ERROR: %i", exception);
Expand All @@ -166,10 +224,10 @@ static void getTemperatureData() {
while (index <= LIMIT) {
CEXCEPTION_T exception;
Try {
uint16_t interruptStatus = bmi323GetInterruptStatus(&bmi323, BMI323_INTERRUPT_1);
uint16_t interruptStatus = bmi323GetInterruptStatus(bmi323, BMI323_INTERRUPT_1);
if (interruptStatus & BMI3_INT_STATUS_TEMP_DRDY) {
float temperature = bmi323GetTemperature(&bmi323);
float sensorTime = bmi323GetSensorTime(&bmi323);
float temperature = bmi323GetTemperature(bmi323);
float sensorTime = bmi323GetSensorTime(bmi323);
PRINT("%04i (%.4fs): %f°C", index, sensorTime, temperature);

index++;
Expand All @@ -193,6 +251,8 @@ int main(void) {
PRINT("===== START TEST =====");
initializeBmi323();
getChipId();

testAccelerometer();
testTemperature();
testGyroscope();

Expand Down

0 comments on commit 63545ac

Please sign in to comment.