mirror of
https://github.com/apache/nuttx.git
synced 2026-05-31 23:40:19 +08:00
sensors/bmi270_uorb: support for fixed-point data
support for fixed-point data for bmi270_uorb Signed-off-by: raiden00pl <raiden00@railab.me>
This commit is contained in:
@@ -49,12 +49,6 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/* Only float data type supported now */
|
|
||||||
|
|
||||||
#ifdef CONFIG_SENSORS_USE_B16
|
|
||||||
# error fixed-point data type not supported yet
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define CONSTANTS_ONE_G 9.8f
|
#define CONSTANTS_ONE_G 9.8f
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -72,7 +66,7 @@ struct bmi270_sensor_s
|
|||||||
{
|
{
|
||||||
struct sensor_lowerhalf_s lower;
|
struct sensor_lowerhalf_s lower;
|
||||||
uint64_t last_update;
|
uint64_t last_update;
|
||||||
float scale;
|
sensor_data_t scale;
|
||||||
FAR void *dev;
|
FAR void *dev;
|
||||||
bool enabled;
|
bool enabled;
|
||||||
#ifdef CONFIG_SENSORS_BMI270_POLL
|
#ifdef CONFIG_SENSORS_BMI270_POLL
|
||||||
@@ -256,9 +250,9 @@ static int bmi270_fetch(FAR struct sensor_lowerhalf_s *lower,
|
|||||||
(FAR uint8_t *)data, 6);
|
(FAR uint8_t *)data, 6);
|
||||||
|
|
||||||
accel.timestamp = sensor_get_timestamp();
|
accel.timestamp = sensor_get_timestamp();
|
||||||
accel.x = data[0] * priv->scale;
|
accel.x = sensor_data_muli(priv->scale, data[0]);
|
||||||
accel.y = data[1] * priv->scale;
|
accel.y = sensor_data_muli(priv->scale, data[1]);
|
||||||
accel.z = data[2] * priv->scale;
|
accel.z = sensor_data_muli(priv->scale, data[2]);
|
||||||
|
|
||||||
memcpy(buffer, &accel, sizeof(accel));
|
memcpy(buffer, &accel, sizeof(accel));
|
||||||
ret = sizeof(accel);
|
ret = sizeof(accel);
|
||||||
@@ -274,9 +268,9 @@ static int bmi270_fetch(FAR struct sensor_lowerhalf_s *lower,
|
|||||||
(FAR uint8_t *)data, 6);
|
(FAR uint8_t *)data, 6);
|
||||||
|
|
||||||
gyro.timestamp = sensor_get_timestamp();
|
gyro.timestamp = sensor_get_timestamp();
|
||||||
gyro.x = data[0] * priv->scale;
|
gyro.x = sensor_data_muli(priv->scale, data[0]);
|
||||||
gyro.y = data[1] * priv->scale;
|
gyro.y = sensor_data_muli(priv->scale, data[1]);
|
||||||
gyro.z = data[2] * priv->scale;
|
gyro.z = sensor_data_muli(priv->scale, data[2]);
|
||||||
|
|
||||||
memcpy(buffer, &gyro, sizeof(gyro));
|
memcpy(buffer, &gyro, sizeof(gyro));
|
||||||
ret = sizeof(gyro);
|
ret = sizeof(gyro);
|
||||||
@@ -363,22 +357,22 @@ static int bmi270_accel_scale(FAR struct bmi270_sensor_s *priv,
|
|||||||
if (scale < bmi270_midpoint(2, 4))
|
if (scale < bmi270_midpoint(2, 4))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_2G);
|
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_2G);
|
||||||
priv->scale = CONSTANTS_ONE_G / 16384.f;
|
priv->scale = sensor_data_ftof(CONSTANTS_ONE_G / 16384.f);
|
||||||
}
|
}
|
||||||
else if (scale < bmi270_midpoint(4, 8))
|
else if (scale < bmi270_midpoint(4, 8))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_4G);
|
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_4G);
|
||||||
priv->scale = CONSTANTS_ONE_G / 8192.f;
|
priv->scale = sensor_data_ftof(CONSTANTS_ONE_G / 8192.f);
|
||||||
}
|
}
|
||||||
else if (scale < bmi270_midpoint(8, 16))
|
else if (scale < bmi270_midpoint(8, 16))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_8G);
|
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_8G);
|
||||||
priv->scale = CONSTANTS_ONE_G / 4096.f;
|
priv->scale = sensor_data_ftof(CONSTANTS_ONE_G / 4096.f);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_16G);
|
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_16G);
|
||||||
priv->scale = CONSTANTS_ONE_G / 2048.f;
|
priv->scale = sensor_data_ftof(CONSTANTS_ONE_G / 2048.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -396,27 +390,27 @@ static int bmi270_gyro_scale(FAR struct bmi270_sensor_s *priv,
|
|||||||
if (scale < bmi270_midpoint(125, 250))
|
if (scale < bmi270_midpoint(125, 250))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_125);
|
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_125);
|
||||||
priv->scale = (M_PI / 180.0f) * 125.f / 32768.f;
|
priv->scale = sensor_data_ftof((M_PI / 180.0f) * 125.f / 32768.f);
|
||||||
}
|
}
|
||||||
else if (scale < bmi270_midpoint(250, 500))
|
else if (scale < bmi270_midpoint(250, 500))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_250);
|
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_250);
|
||||||
priv->scale = (M_PI / 180.0f) * 250.f / 32768.f;
|
priv->scale = sensor_data_ftof((M_PI / 180.0f) * 250.f / 32768.f);
|
||||||
}
|
}
|
||||||
else if (scale < bmi270_midpoint(500, 1000))
|
else if (scale < bmi270_midpoint(500, 1000))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_500);
|
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_500);
|
||||||
priv->scale = (M_PI / 180.0f) * 500.f / 32768.f;
|
priv->scale = sensor_data_ftof((M_PI / 180.0f) * 500.f / 32768.f);
|
||||||
}
|
}
|
||||||
else if (scale < bmi270_midpoint(1000, 2000))
|
else if (scale < bmi270_midpoint(1000, 2000))
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_1000);
|
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_1000);
|
||||||
priv->scale = (M_PI / 180.0f) * 1000.f / 32768.f;
|
priv->scale = sensor_data_ftof((M_PI / 180.0f) * 1000.f / 32768.f);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_2000);
|
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_2000);
|
||||||
priv->scale = (M_PI / 180.0f) * 2000.f / 32768.f;
|
priv->scale = sensor_data_ftof((M_PI / 180.0f) * 2000.f / 32768.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -453,9 +447,9 @@ static void bmi270_accel_data(FAR struct bmi270_sensor_s *priv,
|
|||||||
priv->last_update = now;
|
priv->last_update = now;
|
||||||
|
|
||||||
accel.timestamp = now;
|
accel.timestamp = now;
|
||||||
accel.x = buf[0] * priv->scale;
|
accel.x = sensor_data_muli(priv->scale, buf[0]);
|
||||||
accel.y = buf[1] * priv->scale;
|
accel.y = sensor_data_muli(priv->scale, buf[1]);
|
||||||
accel.z = buf[2] * priv->scale;
|
accel.z = sensor_data_muli(priv->scale, buf[2]);
|
||||||
accel.temperature = 0;
|
accel.temperature = 0;
|
||||||
|
|
||||||
lower->push_event(lower->priv, &accel, sizeof(accel));
|
lower->push_event(lower->priv, &accel, sizeof(accel));
|
||||||
@@ -491,9 +485,9 @@ static void bmi270_gyro_data(FAR struct bmi270_sensor_s *priv,
|
|||||||
priv->last_update = now;
|
priv->last_update = now;
|
||||||
|
|
||||||
gyro.timestamp = now;
|
gyro.timestamp = now;
|
||||||
gyro.x = buf[0] * priv->scale;
|
gyro.x = sensor_data_muli(priv->scale, buf[0]);
|
||||||
gyro.y = buf[1] * priv->scale;
|
gyro.y = sensor_data_muli(priv->scale, buf[1]);
|
||||||
gyro.z = buf[2] * priv->scale;
|
gyro.z = sensor_data_muli(priv->scale, buf[2]);
|
||||||
gyro.temperature = 0;
|
gyro.temperature = 0;
|
||||||
|
|
||||||
lower->push_event(lower->priv, &gyro, sizeof(gyro));
|
lower->push_event(lower->priv, &gyro, sizeof(gyro));
|
||||||
|
|||||||
Reference in New Issue
Block a user