Accelerometer
We will use the LSM6DSL accelerometer sensor located on our board using the I²C device driver.
The LSM6DSL documentation is accessible here.
⚠️ Important: in this part, you are supposed to send and receive I²C commands directly. Do not use the lsm6dsl driver included in Zephyr.
Check that you can address the LSM6DSL sensor
Check that you are able to read the WHO_AM_I
register from the LSM6DSL sensor and that it returns the expected value.
Did you hardcode the address of the device and the I²C bus to use? If you did, you can do better: the I2C_DT_SPEC_GET()
macro let you retrieve a struct i2c_dt_spec
structure which contains the bus and the device address corresponding to a node located on a I²C bus.
Locate the node corresponding to the LSM6DSL accelerometer in your device tree (you should now remember that it is available in build/zephyr/zephyr.dts
) and statically initialize a struct i2c_dt_spec
for it.
Since you used a static initialization, do not forget to check with device_is_ready()
that the I²C device is ready before using it for the first time.
You can use I²C functions ending in _dt
to avoid explicitely giving the bus and the device address.
Configure the LSM6DSL accelerometer
Configure the LSM6DSL sensor so that you it performs measures at 1.6Hz. Check that reading the accelerometer registers give you a credible value when you turn the board on one side or another.
Configure the interrupt line connected to the microcontroller so that you know when a new measure has been done. When this happens, trigger a read of the accelerometer axes and display the value.
Pitfalls and advices
Are some things not working? Or you think you can do better? Only if you are stuck or when you are done, check the following items:
- Do you use
i2c_dt_spec
for the I²C bus and address? - Do you use
gpio_dt_spec
for the interrupt line? - Did you configure the interrupt line as an input?
- Did you register the interrupt callback before the first measure?
- Did you use a work queue to perform the I²C data reading transaction in order not to do it in an interrupt callback?
- Did you reset the LSM6DSL to ensure that the interrupt line is low initially?
- In order to read the accelerometer data with one I²C transaction, did you keep the auto-increment of addresses enabled?
- Did you use the logging module in order to selectively display data?