Skip to main content
Visitor II
February 20, 2025
Question

integration of lsm303AGR on Android 10 and kernel 5.4 versions

  • February 20, 2025
  • 3 replies
  • 2872 views

Dear Team,

We are integrating lsm303AGR accelerometer and magnetometer sensors on NXP's imx8 Android 10 platform.

https://github.com/STMicroelectronics/st-mems-android-linux-sensors-hal for HAL

https://github.com/STMicroelectronics/st-mems-android-linux-drivers-iio for linux 5.4 kernel

device registers as iio:device0 and iio:device1 x,y,z raw value we can see in cat sysfs files.

HAL finds 2 IIO devices but further

there is lack of implementation of loading bias and save bias functions in legacy 

because of this the sensors app is not able to  read the sensor data values . apps says there are 2 sensors from stm accel and magn but data is not able to read.

it is going into un-calibrated type. 

kindly help us in fixing the issue.

    This topic has been closed for replies.

    3 replies

    Technical Moderator
    February 20, 2025

    Hi @prasannakulkarni ,

    Can you check if the Linux driver works correctly on your platform?
    To verify this, once the sensor is activated, the accelerometer or magnetomer interrupts must increment regularly, otherwise there could be an issue with the configuration of the interrupt lines in the device tree. To see if the interrupts are triggered regularly, you can read the file/proc/interrupts, checking for example if the lsm303agr_accel counter increases.

    Visitor II
    February 21, 2025

    Yes you are true, interrupts are getting incremented. kindly find the attached files there are no implementations for loadbias and savebias in legacy mode

    legacy/SensorsLegacyInterface.cpp


    /**
    247 * onSaveDataRequest: receive data to store,
    248 * reference: ISTMSensorsCallbackData class
    249 */
    250 int SensorsLegacyInterface::onSaveDataRequest(const std::string& resourceID,
    251 const void *data, ssize_t len)
    252 {
    253 (void) resourceID;
    254 (void) data;
    255 (void) len;
    256
    257 return -EIO;
    258 }
    259
    260 /**
    261 * onLoadDataRequest: load data from disk,
    262 * reference: ISTMSensorsCallbackData class
    263 */
    264 int SensorsLegacyInterface::onLoadDataRequest(const std::string& resourceID,
    265 void *data, ssize_t len)
    266 {
    267 (void) resourceID;
    268 (void) data;
    269 (void) len;
    270
    271 return -EIO;
    272 }

    Visitor II
    February 21, 2025

    Screenshot from 2025-02-21 19-40-56.png

    Screenshot from 2025-02-21 19-41-12.png

    Screenshot from 2025-02-21 19-41-33.png

    Visitor II
    February 21, 2025

    Kindly help

    Technical Moderator
    March 3, 2025

    Hi @prasannakulkarni ,

    Can you try this patch on Linux driver?

    diff --git a/drivers/iio/stm/magnetometer/st_mag40_buffer.c b/drivers/iio/stm/magnetometer/st_mag40_buffer.c
    index 468f26db..a853f611 100644
    --- a/drivers/iio/stm/magnetometer/st_mag40_buffer.c
    +++ b/drivers/iio/stm/magnetometer/st_mag40_buffer.c
    @@ -131,6 +131,10 @@ static int st_mag40_buffer_postdisable(struct iio_dev *indio_dev)

    static const struct iio_buffer_setup_ops st_mag40_buffer_setup_ops = {
           .preenable = st_mag40_buffer_preenable,
    +#if KERNEL_VERSION(5, 10, 0) > LINUX_VERSION_CODE
    +       .postenable = iio_triggered_buffer_postenable,
    +       .predisable = iio_triggered_buffer_predisable,
    +#endif /* LINUX_VERSION_CODE */
           .postdisable = st_mag40_buffer_postdisable,
    };

    Visitor II
    March 4, 2025

                As in First post I have explained, I have taken code from your github latest

    We are integrating lsm303AGR accelerometer and magnetometer sensors on NXP's imx8 Android 10 platform.

    https://github.com/STMicroelectronics/st-mems-android-linux-sensors-hal for HAL

    https://github.com/STMicroelectronics/st-mems-android-linux-drivers-iio for linux 5.4 kernel

     

         We are using Linux 5.4 kernel, accordingly we have 

    following lines in the driver, lets concentrate only on Accelerometer , I have attached patch just check it

     

    Visitor II
    March 7, 2025

    Hi @Federica Bossi 

             We have tried with HIDL2.0 also but same behaviour... android apps are not recognising the sensors .. the same screen as attached here before.

    Few findings are ************************ the device boot logcat looks like this

    --------- beginning of main
    --------- beginning of system
    01-01 00:00:22.570 306 306 I stm-sensors: gyroscope rotation matrix: 1.000000,0.000000,0.000000;0.000000,1.000000,0.000000;0.000000,0.000000,1.000000
    01-01 00:00:22.571 306 306 I stm-sensors: magnetometer rotation matrix: 1.000000,0.000000,0.000000;0.000000,1.000000,0.000000;0.000000,0.000000,1.000000
    01-01 00:00:22.571 306 306 I stm-sensors: accelerometer rotation matrix: 1.000000,0.000000,0.000000;0.000000,1.000000,0.000000;0.000000,0.000000,1.000000
    01-01 00:00:22.571 306 306 I stm-sensors: gyroscope placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:22.571 306 306 I stm-sensors: magnetometer placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:22.571 306 306 I stm-sensors: pressure placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:22.571 306 306 I stm-sensors: accelerometer placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:22.572 306 306 D stm-sensors: found 2 IIO devices available under /sys/bus/iio/devices
    01-01 00:00:22.582 306 306 D stm-sensors: LSM303AGR Magnetometer Sensor: injection mode not available
    01-01 00:00:22.583 306 306 I stm-sensors: magn calibration library: stm-magn-lib-mock
    01-01 00:00:22.583 306 306 D stm-sensors: LSM303AGR Accelerometer Sensor: injection mode not available
    01-01 00:00:22.583 306 306 I stm-sensors: accel calibration library: stm-accel-lib-mock
    01-01 00:00:22.583 306 306 I stm-sensors: sensors fusion (6X) library: stm-sensors-fusion-6X-lib-mock
    01-01 00:00:22.584 306 306 I stm-sensors: sensors fusion (9X) library: stm-sensors-fusion-9X-lib-mock
    01-01 00:00:22.584 306 306 I stm-sensors: sensors fusion (6X) library: stm-sensors-fusion-6X-lib-mock
    01-01 00:00:22.584 306 306 I stm-sensors: sensors fusion (9X) library: stm-sensors-fusion-9X-lib-mock
    01-01 00:00:22.587 306 306 E stm-sensors: selftest functions cannot be loaded correctly
    01-01 00:00:22.587 306 306 D stm-sensors: 4 sensors available and ready
    01-01 00:00:22.588 306 306 I stm-sensors: timesync library: stm-timesync-lib-mock
    01-01 00:00:22.588 306 306 D stm-sensors: onLoadDataRequest is entered
    01-01 00:00:22.588 306 306 D stm-sensors: onLoadDataRequest: file is opened
    01-01 00:00:22.588 306 306 W stm-sensors: failed to load accel bias
    01-01 00:00:22.588 306 306 D stm-sensors: onLoadDataRequest is entered
    01-01 00:00:22.588 306 306 D stm-sensors: onLoadDataRequest: file is opened
    01-01 00:00:22.588 306 306 E stm-sensors: failed to load magn bias
    03-07 10:17:31.795 306 306 D stm-sensors: LSM303AGR Accelerometer Sensor: changed pollrate to 14.999925Hz, timeout=0ms
    03-07 10:17:31.795 306 306 D stm-sensors: LSM303AGR Accelerometer Sensor: power-on

    when Opened the accelerometer from APP following log

    03-07 10:18:02.955 306 534 E stm-sensors: received unrecognized event type 30
    03-07 10:18:33.364 306 534 E stm-sensors: received unrecognized event type 30

     

    Where to add the following lines?

    persist.vendor.stm.sensors.max-odr = 250 #max odr that can be used is 250Hz

    persist.vendor.stm.sensors.rot-matrix-1.accel = "1,0,0,0,1,0,0,0,1" #accel rotation matrix
    persist.vendor.stm.sensors.placement-1.accel = "10,20,30" #accel position in cm

    persist.vendor.stm.sensors.max-range.accel = 70 #accel full-scale to support reading of at least 70m/s^2
    persist.vendor.stm.sensors.max-range.magn = 2000 #magn full-scale to support reading of at least 2000uT
    persist.vendor.stm.sensors.max-range.gyro = 8 #gyro full-scale to support reading of at least 8rad/s

     

    we have /vendor/build.prop file. if I add these in this file is that OK?

     

    * in Android.bp file of 2.0 refers to a file a directory

                       DHAL_PRIVATE_DATA_PATH=\"/data/vendor/stm-sensors-hal\

       but this folder is not getting created...

    I have removed all the SElinux permissions

    androidboot.selinux=permissive device is in permissive=1 mode.

     

    Kindly help

     

     

    Technical Moderator
    March 12, 2025

    Hi @prasannakulkarni ,

    The Android sensor hal reads the calibration offsets from files that are saved on the file system. For this to happen, you need to correctly configure the sepolicy and init.rc files in order to create the folders that will contain such calibration files, define their owners and access flags.

    In our tests we generally use Android reference boards, which already have predefined folders where to save the sensor hal calibration data, typically these are defined by sepolicy rules files prepared by vendors (sepolicy file_contexts file):

    /data/vendor/sensor(/.*)? u:object_r:sensor_vendor_data_file:s0

    Of course in your case you might have chosen another place to save this persistent data, so it depends on how you have organized your file system, but in our configuration this SELinux rule assigns the security context u:object_r:sensor_vendor_data_file:s0 to all files and directories under /data/vendor/sensor.

     

    In order to write to these files, the specified path (/data/vendor/sensor) must exist and be made accessible to the system user before sensorhal even starts. To do this you need to add a file init.sensorhal.rc containing the following instructions:

     

    on post-fs-data
        mkdir /data/vendor/sensor 
        chown root system /data/vendor/sensor
        chmod 0770 /data/vendor/sensor

     

    You also need to instruct the aosp build to copy this file to the vendor partition (typically from the device.mk file of the device):

     

    PRODUCT_COPY_FILES += \
        device/linaro/hikey/init.sensorhal.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.sensorhal.rc

     

    Finally you need to define the variable HAL_PRIVATE_DATA_PATH in <sensorHal>/2.0/Android.bp in the following way:
    "-DHAL_PRIVATE_DATA_PATH=\"/data/vendor/sensor\""

     

    Of course the paths where to save the sensor data can be changed, this is just an example of implementation.

     

    I hope this clarifies the procedure to follow.

     

    Please see the aosp SELinux documentation page for debugging rules: https://source.android.com/docs/security/features/selinux/concepts and https://source.android.com/docs/security/features/selinux/validate

     

    Please note that in order for the calibration data to be actually produced it is necessary to install the STMicroelectronics calibration libraries (MotionAC for accelerometer calibration and MotionMC for magnetometer calibration). The release of STM proprietary libraries is subject to signature of a License User Agreement (LUA); please contact an STMicroelectronics sales office and representatives for further information.

    Visitor II
    March 13, 2025

    Hi @Federica Bossi ,

             As I have mentioned in earlier post the device is in permissive mode.

    it creates files in 

    #ls /data/vendor/stm-sensors-hal/
    accel_bias_1.dat magn_bias_1.dat

    following is the boot logcat and tried open sensor test apps but magnetometer is giving zero values only... drivers is giving RAW values but the HAL is unable to read

    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    01-01 00:00:12.322 310 310 I ServiceManagement: Registered android.hardware.sensors@2.0::ISensors/default (start delay of 686ms)
    01-01 00:00:12.329 310 310 I ServiceManagement: Removing namespace from process name android.hardware.sensors@2.0-service.stmicroelectronics to sensors@2.0-service.stmicroelectronics.
    01-01 00:00:13.284 293 293 I FslCameraHAL: enumSensorSet
    01-01 00:00:20.632 463 540 D SystemServerInitThreadPool: Started executing StartSensorService
    01-01 00:00:20.633 463 540 D SensorService: nuSensorService starting...
    01-01 00:00:20.652 310 310 D stm-sensors: persist.vendor.stm.sensors.max-odr *************sensors available and ready
    01-01 00:00:20.652 310 310 D stm-sensors: 250 *************sensors available and ready
    01-01 00:00:20.652 310 310 I stm-sensors: gyroscope rotation matrix: 1.000000,0.000000,0.000000;0.000000,1.000000,0.000000;0.000000,0.000000,1.000000
    01-01 00:00:20.652 310 310 I stm-sensors: magnetometer rotation matrix: 1.000000,0.000000,0.000000;0.000000,1.000000,0.000000;0.000000,0.000000,1.000000
    01-01 00:00:20.653 310 310 I stm-sensors: accelerometer rotation matrix: 1.000000,0.000000,0.000000;0.000000,1.000000,0.000000;0.000000,0.000000,1.000000
    01-01 00:00:20.653 310 310 I stm-sensors: gyroscope placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:20.653 310 310 I stm-sensors: magnetometer placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:20.653 310 310 I stm-sensors: pressure placement: 0.000000, 0.000000, 0.000000
    01-01 00:00:20.653 310 310 I stm-sensors: accelerometer placement: 10.000000, 20.000000, 30.000000
    01-01 00:00:20.653 310 310 D stm-sensors: found 2 IIO devices available under /sys/bus/iio/devices
    01-01 00:00:20.661 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: injection mode not available
    01-01 00:00:20.662 310 310 I stm-sensors: magn calibration library: stm-magn-lib-mock
    01-01 00:00:20.662 310 310 D stm-sensors: LSM303AGR Accelerometer Sensor: injection mode not available
    01-01 00:00:20.663 310 310 I stm-sensors: accel calibration library: stm-accel-lib-mock
    01-01 00:00:20.663 310 310 I stm-sensors: sensors fusion (6X) library: stm-sensors-fusion-6X-lib-mock
    01-01 00:00:20.663 310 310 I stm-sensors: sensors fusion (9X) library: stm-sensors-fusion-9X-lib-mock
    01-01 00:00:20.665 310 310 E stm-sensors: selftest functions cannot be loaded correctly
    01-01 00:00:20.666 310 310 D stm-sensors: 4 sensors available and ready
    01-01 00:00:20.666 310 310 D stm-sensors: Old Status:0new Status:0
    01-01 00:00:20.666 310 310 D stm-sensors: Before Trying to save bias
    01-01 00:00:20.666 310 310 D stm-sensors: After Trying to save bias
    01-01 00:00:20.666 310 310 I stm-sensors: timesync library: stm-timesync-lib-mock
    01-01 00:00:20.667 310 310 D stm-sensors: onLoadDataRequest is entered
    01-01 00:00:20.676 310 310 D stm-sensors: onLoadDataRequest: read successful
    01-01 00:00:20.676 310 310 D stm-sensors: onLoadDataRequest is entered
    01-01 00:00:20.678 310 310 D stm-sensors: onLoadDataRequest: read successful
    01-01 00:00:20.680 463 540 I SensorService: Reported power 0.000000 not deemed sane, clamping to 0.001000
    01-01 00:00:20.681 463 540 I SensorService: Reported power 0.000000 not deemed sane, clamping to 0.001000
    01-01 00:00:20.681 463 540 I SensorService: Reported power 0.000000 not deemed sane, clamping to 0.001000
    01-01 00:00:20.681 463 540 I SensorService: Reported power 0.000000 not deemed sane, clamping to 0.001000
    01-01 00:00:20.686 463 550 D SensorService: new thread SensorEventAckReceiver
    01-01 00:00:20.686 463 551 D SensorService: nuSensorService thread starting...
    03-11 05:50:34.347 310 310 D stm-sensors: LSM303AGR Accelerometer Sensor: HWSensor changed pollrate to 14.999925Hz, timeout=0ms
    03-11 05:50:34.348 310 310 D stm-sensors: LSM303AGR Accelerometer Sensor: power-on
    03-11 05:50:34.357 310 310 D stm-sensors: Old Status:0new Status:1
    03-11 05:50:34.357 310 310 D stm-sensors: Before Trying to save bias
    03-11 05:50:34.357 310 310 D stm-sensors: After Trying to save bias

     

     

    03-11 05:51:10.032 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: HWSensor changed pollrate to 14.999925Hz, timeout=0ms
    03-11 05:51:10.033 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: power-on
    03-11 05:52:24.783 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: power-off
    03-11 05:52:24.790 310 310 D stm-sensors: onSaveDataRequest is entered
    03-11 05:52:24.797 310 310 D stm-sensors: onSaveDataRequest: write successful
    03-11 05:52:30.094 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: HWSensor changed pollrate to 10.000000Hz, timeout=0ms
    03-11 05:52:30.094 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: power-on
    03-11 05:52:42.649 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: power-off
    03-11 05:52:42.652 310 310 D stm-sensors: onSaveDataRequest is entered
    03-11 05:52:42.654 310 310 D stm-sensors: onSaveDataRequest: write successful
    03-11 05:53:00.028 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: HWSensor changed pollrate to 10.000000Hz, timeout=0ms
    03-11 05:53:00.028 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: power-on
    03-11 05:53:03.094 310 310 D stm-sensors: LSM303AGR Magnetometer Sensor: power-off
    03-11 05:53:03.103 310 310 D stm-sensors: onSaveDataRequest is entered
    03-11 05:53:03.105 310 310 D stm-sensors: onSaveDataRequest: write successful
    03-11 05:53:38.369 310 546 D stm-sensors: Im calling event type from here
    03-11 05:53:38.369 310 546 D stm-sensors: LSM303AGR Accelerometer Sensor: write flush event to pipe
    03-11 05:53:47.842 310 310 D stm-sensors: Old Status:1new Status:1
    03-11 05:53:47.842 310 310 D stm-sensors: Before Trying to save bias
    03-11 05:53:47.842 310 310 D stm-sensors: After Trying to save bias