Keep the device-ID driver and overlay for Chapter 08.
1. Wire the accelerometer
Use a Raspberry Pi 3, SPI0 CE0, and an ADXL345 board compatible with 3.3 V power and logic. Unplug the Pi and follow the module’s pin labels.
| ADXL345 signal | Raspberry Pi 3 physical pin | Function |
|---|---|---|
VCC or 3V3 |
1 | 3.3 V |
GND |
6 | Ground |
SCL or SCLK |
23 | SCLK |
SDA or SDI |
19 | MOSI |
SDO |
21 | MISO |
CS |
24 | CE0 |
2. Describe the SPI device
Create adxl345-lab-overlay.dts. The overlay assigns CE0 to the sensor and disables the competing spidev node.
/dts-v1/;
/plugin/;
/ {
fragment@0 {
target = <&spi0>;
__overlay__ {
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
accelerometer@0 {
compatible = "example,adxl345-id";
reg = <0>;
spi-max-frequency = <1000000>;
spi-cpol;
spi-cpha;
};
};
};
fragment@1 {
target = <&spidev0>;
__overlay__ {
status = "disabled";
};
};
};
reg = <0> selects chip select 0. The combination of spi-cpol and spi-cpha selects SPI mode 3. The lab starts at 1 MHz.
dtc -@ -I dts -O dtb \
-o adxl345-lab.dtbo adxl345-lab-overlay.dts
Copy the overlay into the firmware’s active boot partition under overlays/. Add this line to config.txt.
dtoverlay=adxl345-lab
Reboot and look for spi0.0 under /sys/bus/spi/devices/.
3. Read the chip ID with regmap
Create adxl345_id.c.
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <linux/spi/spi.h>
#define ADXL345_REG_DEVID 0x00
#define ADXL345_DEVICE_ID 0xe5
static const struct regmap_config adxl345_id_regmap = {
.reg_bits = 8,
.val_bits = 8,
.read_flag_mask = 0x80,
};
static int adxl345_id_probe(struct spi_device *spi)
{
struct regmap *map;
unsigned int id;
int ret;
map = devm_regmap_init_spi(spi, &adxl345_id_regmap);
if (IS_ERR(map))
return dev_err_probe(&spi->dev, PTR_ERR(map),
"cannot initialize SPI regmap\n");
ret = regmap_read(map, ADXL345_REG_DEVID, &id);
if (ret)
return dev_err_probe(&spi->dev, ret, "cannot read device ID\n");
if (id != ADXL345_DEVICE_ID) {
dev_err(&spi->dev, "unexpected device ID: 0x%02x\n", id);
return -ENODEV;
}
dev_info(&spi->dev, "ADXL345 detected, device ID: 0x%02x\n", id);
return 0;
}
static const struct of_device_id adxl345_id_match[] = {
{ .compatible = "example,adxl345-id" },
{ }
};
MODULE_DEVICE_TABLE(of, adxl345_id_match);
static struct spi_driver adxl345_id_driver = {
.driver = {
.name = "adxl345-id",
.of_match_table = adxl345_id_match,
},
.probe = adxl345_id_probe,
};
module_spi_driver(adxl345_id_driver);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ADXL345 SPI device ID lab");
Create the Makefile.
obj-m := adxl345_id.o
Build against the target kernel with REGMAP_SPI support. If matching headers are installed on the Pi, run the following there.
make -C "/lib/modules/$(uname -r)/build" M="$PWD" modules
sudo insmod ./adxl345_id.ko
sudo dmesg | grep -E 'adxl345-id|ADXL345 detected' | tail
Expect ADXL345 detected, device ID: 0xe5. The register address passed to regmap_read() is 0x00. The read flag mask sets bit 7 when the SPI command is sent. With a logic analyzer, check that CS goes low, MOSI sends 0x80, and MISO returns 0xe5 during the data phase.
4. Read three axes through IIO
Change the overlay’s compatible string.
compatible = "adi,adxl345";
Rebuild and deploy the overlay, then reboot. Enable IIO and CONFIG_ADXL345_SPI in the target kernel so the system ADXL345 driver can take over CE0.
Locate the IIO device named adxl345.
for dev in /sys/bus/iio/devices/iio:device*; do
[ -r "$dev/name" ] || continue
printf '%s: ' "$dev"
cat "$dev/name"
done
The example assumes iio:device0.
dev=/sys/bus/iio/devices/iio:device0
for axis in x y z; do
printf '%s: ' "$axis"
cat "$dev/in_accel_${axis}_raw"
done
cat "$dev/in_accel_scale"
Record the three readings with the board flat, rotate it by 90 degrees, and repeat. Multiply raw readings by in_accel_scale to obtain m/s². At rest, the axis aligned with gravity should read about +9.8 or −9.8 m/s², depending on orientation.
5. Verify and extend
- Confirm that the lab driver reads
0xe5and the system driver exposes the raw axis values and scale. - Read sysfs at fixed intervals to observe changes. For buffered streaming, check the target driver’s support and configure the interrupt wiring, IIO buffer, and scan elements before reading
/dev/iio:deviceN.
References