Skip to content

Commit cfbdc5c

Browse files
drivers: sensor: bmm350: add attr_get
Add the attr_get api. Also rename functions where they implied accelerometer to imply magnetometer. Signed-off-by: Ryan McClelland <[email protected]>
1 parent 8f8b223 commit cfbdc5c

File tree

1 file changed

+123
-4
lines changed

1 file changed

+123
-4
lines changed

drivers/sensor/bosch/bmm350/bmm350.c

+123-4
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ static int bmm350_channel_get(const struct device *dev, enum sensor_channel chan
676676

677677
return 0;
678678
}
679-
static uint8_t acc_odr_to_reg(const struct sensor_value *val)
679+
680+
static uint8_t mag_odr_to_reg(const struct sensor_value *val)
680681
{
681682
double odr = sensor_value_to_double((struct sensor_value *)val);
682683

@@ -706,7 +707,7 @@ static uint8_t acc_odr_to_reg(const struct sensor_value *val)
706707
return reg;
707708
}
708709

709-
static uint8_t acc_osr_to_reg(const struct sensor_value *val)
710+
static uint8_t mag_osr_to_reg(const struct sensor_value *val)
710711
{
711712
switch (val->val1) {
712713
case 0:
@@ -767,6 +768,7 @@ int8_t bmm350_set_odr_performance(enum bmm350_data_rates odr,
767768

768769
return rslt;
769770
}
771+
770772
static int set_mag_odr_osr(const struct device *dev, const struct sensor_value *odr,
771773
const struct sensor_value *osr)
772774
{
@@ -792,10 +794,10 @@ static int set_mag_odr_osr(const struct device *dev, const struct sensor_value *
792794
}
793795

794796
if (odr) {
795-
odr_bits = acc_odr_to_reg(odr);
797+
odr_bits = mag_odr_to_reg(odr);
796798
}
797799
if (osr) {
798-
osr_bits = acc_osr_to_reg(osr);
800+
osr_bits = mag_osr_to_reg(osr);
799801
if (osr_bits == 0xFF) {
800802
LOG_ERR("unsupported oversampling rate");
801803
return -EINVAL;
@@ -836,8 +838,125 @@ static int bmm350_attr_set(const struct device *dev, enum sensor_channel chan,
836838

837839
return 0;
838840
}
841+
842+
void mag_reg_to_odr(uint8_t bits, struct sensor_value *val)
843+
{
844+
switch (bits) {
845+
case BMM350_DATA_RATE_1_5625HZ:
846+
val->val1 = 1;
847+
val->val2 = 562500;
848+
break;
849+
case BMM350_DATA_RATE_3_125HZ:
850+
val->val1 = 3;
851+
val->val2 = 125000;
852+
break;
853+
case BMM350_DATA_RATE_6_25HZ:
854+
val->val1 = 6;
855+
val->val2 = 250000;
856+
break;
857+
case BMM350_DATA_RATE_12_5HZ:
858+
val->val1 = 12;
859+
val->val2 = 500000;
860+
break;
861+
case BMM350_DATA_RATE_25HZ:
862+
val->val1 = 25;
863+
val->val2 = 0;
864+
break;
865+
case BMM350_DATA_RATE_50HZ:
866+
val->val1 = 50;
867+
val->val2 = 0;
868+
break;
869+
case BMM350_DATA_RATE_100HZ:
870+
val->val1 = 100;
871+
val->val2 = 0;
872+
break;
873+
case BMM350_DATA_RATE_200HZ:
874+
val->val1 = 200;
875+
val->val2 = 0;
876+
break;
877+
case BMM350_DATA_RATE_400HZ:
878+
val->val1 = 400;
879+
val->val2 = 0;
880+
break;
881+
default:
882+
val->val1 = 0;
883+
val->val2 = 0;
884+
break;
885+
}
886+
}
887+
888+
void mag_reg_to_osr(uint8_t bits, struct sensor_value *val)
889+
{
890+
val->val2 = 0;
891+
892+
switch (bits) {
893+
case BMM350_NO_AVERAGING:
894+
val->val1 = 1;
895+
break;
896+
case BMM350_AVERAGING_2:
897+
val->val1 = 2;
898+
break;
899+
case BMM350_AVERAGING_4:
900+
val->val1 = 4;
901+
break;
902+
case BMM350_AVERAGING_8:
903+
val->val1 = 8;
904+
break;
905+
default:
906+
val->val1 = 0;
907+
break;
908+
}
909+
}
910+
911+
static int get_mag_odr_osr(const struct device *dev, struct sensor_value *odr,
912+
struct sensor_value *osr)
913+
{
914+
int ret;
915+
uint8_t rx_buf[3] = {0x00};
916+
uint8_t osr_bits;
917+
uint8_t odr_bits;
918+
919+
/* read current state */
920+
ret = bmm350_reg_read(dev, BMM350_REG_PMU_CMD_AGGR_SET, &rx_buf[0], 3);
921+
if (ret < 0) {
922+
LOG_ERR("failed to read PMU_CMD_AGGR_SET");
923+
return -EIO;
924+
}
925+
osr_bits = ((rx_buf[2] & BMM350_AVG_MSK) >> BMM350_AVG_POS);
926+
odr_bits = ((rx_buf[2] & BMM350_ODR_MSK) >> BMM350_ODR_POS);
927+
928+
if (odr) {
929+
mag_reg_to_odr(odr_bits, odr);
930+
}
931+
if (osr) {
932+
mag_reg_to_osr(osr_bits, osr);
933+
}
934+
935+
return 0;
936+
}
937+
938+
static int bmm350_attr_get(const struct device *dev, enum sensor_channel chan,
939+
enum sensor_attribute attr, struct sensor_value *val)
940+
{
941+
int ret;
942+
943+
switch (attr) {
944+
case SENSOR_ATTR_SAMPLING_FREQUENCY:
945+
ret = get_mag_odr_osr(dev, val, NULL);
946+
break;
947+
case SENSOR_ATTR_OVERSAMPLING:
948+
ret = get_mag_odr_osr(dev, NULL, val);
949+
break;
950+
default:
951+
ret = -EINVAL;
952+
}
953+
954+
return ret;
955+
}
956+
839957
static DEVICE_API(sensor, bmm350_api_funcs) = {
840958
.attr_set = bmm350_attr_set,
959+
.attr_get = bmm350_attr_get,
841960
.sample_fetch = bmm350_sample_fetch,
842961
.channel_get = bmm350_channel_get,
843962
#ifdef CONFIG_BMM350_TRIGGER

0 commit comments

Comments
 (0)