Install Analog Devices 3-axis digital accelerometer ADXL345 on SP7021 demo board V3

The goal of this document is to illustrate how to install Analog Devices 3-axis digital accelerometer ADXL345 on SP7021 (Plus 1) demo board V3. ADXL345 supports both I2C and SPI interfaces. This document is going to illustrate how to install ADXL345 using SPI interface.

ADXL345 is supplied in small, thin, 14-pin LGA package. The real size of ADXL345 is only 5.0 x 3.0 mm2. Height is only 1.0 mm. Refer to picture of an ADXL345 module which is bought from market below:

 

Please follow the following steps to install ADXL345:

 1. Install hardware

Refer to schematics of ADXL345 digital 3-axis accelerometer module:

Interface of ADXL345 is selected by state of pin CS. I2C mode is enabled if CS is connected to +3.3V. In SPI mode, CS is connected to SPI master. CS should always be tied to +3.3V (I2C mode) or driven by SPI master (SPI mode) because there is no default mode if CS is left unconnected.

In I2C mode, pin SDO is used to select I2C address of ADXL345. Connecting SDO to GND results in I2C address to be 0x53 while connecting SDO to +3.3V results in I2C address to be 0x1D.

Refer to pin-assignment of Raspberry Pi compatible 40-pin pin-header of SP7021 demo board V3:

Connect ADXL345 module to SP7021 demo board V3 as shown in following table:

Pin # of ADX345

Pin name of ADXL345

Pin # of 40-pin pin-header

Pin name of 40-pin pin-header

1

GND

9

GND

2

VCC

2

+5V_VDD

3

CS

24

GPIO_P3_2/G_MX26 (SPI_CE0_N)

4

INT1

-

-

5

INT2

-

-

6

SDO (ALT ADDRESS)

21

GPIO_P3_1/G_MX25 (SPI_MISO)

7

SDA (SDI/SDIO)

19

GPIO_P2_7/G_MX23 (SPI_MOSI)

8

SCL (SCLK)

23

GPIO_P3_3/G_MX27 (SPI_SCLK)

Refer to photograph which shows ADXL345 module is connected to 40-pin pin-header of SP7021 demo board V3.

2. Modify device-tree source file

Modify node spi_controller0 in device-tree source file linux/kernel/arch/arm/boot/dts/sp7021-demov3.dts to setup channel 0 of SPI master for ADXL345 as shown below:

&spi_controller0 { spi-max-frequency = <50000000>; pinctrl-names = "default"; pinctrl-0 = <&spim0_pins>; #address-cells = <1>; #size-cells = <0>; adxl345@0 { compatible = "adi,adxl345"; reg = <0x0>; spi-max-frequency = <5000000>; spi-cpol; spi-cpha; status = "okay"; }; };

Note that spi_controller0 is a label of node spi@9c002d80. According to specification of ADXL345, spi-max-frequency should be less than or equal to 5,000,000. Properties spi-cpol and spi-cpha (SPI mode 3) should be set.

Also, add dts node pinmux_spim0-pins to setup pins for channel 0 of SP master as shown below:

spim0_pins: pinmux_spim0-pins { sunplus,pins = < SPPCTL_IOPAD(23, SPPCTL_PCTL_G_PMUX, MUXF_SPIM0_DO, 0) SPPCTL_IOPAD(25, SPPCTL_PCTL_G_PMUX, MUXF_SPIM0_DI, 0) SPPCTL_IOPAD(26, SPPCTL_PCTL_G_PMUX, MUXF_SPIM0_EN, 0) SPPCTL_IOPAD(27, SPPCTL_PCTL_G_PMUX, MUXF_SPIM0_CLK, 0) >; };

Note that node pinmux_spim0-pins is sub-node of node pctl. G_MX23 is set as DO (SPI_MOSI). G_MX25 is set as DI (SPI_MISO). G_MX26 is set as EN (SPI_CE0_N). G_MX27 is set as CLK (SPI_SCLK).

Please note that to comply with Linux rules, after version 5.10.59, 4 property-names of pin node of SP7021 are changed as shown in table below:

5.4.35

5.10.59

5.4.35

5.10.59

sppctl,function

function

sppctl,groups

groups

sppctl,pins

sunplus,pins

sppctl,zero_func

sunplus,zerofunc

3. Enable Linux device drivers

Run make kconfig in project top directory. When “Linux/arm Kernel Configuration” menu pops up, move cursor to go to “Device Drivers”. Enable “SPI support”. Refer to screenshot below:

Enter menu “SPI support”. Enable “Sunplus SPI controller” and “SPI slave protocol handlers”. Refer to screenshot below:

Move cursor to go to “Device Drivers” → “Industrial I/O support”. Enable “Industrial I/O support”. Refer to screenshot below:

Move cursor to go to “Device Drivers” → “Industrial I/O support” → “Accelerometers”. Enable “Analog Devices ADXL345 3-Axis Digital Accelerometer SPI Driver”. Refer to screenshot below:

Finally, save configuration.

4. Build Linux image

Go to top folder. Run make all to build Linux image.

5. Boot Linux

Boot Linux with the built image.

 6. Read value of acceleration

After Linux boots up successfully, run ls /sys/bus/iio/devices/iio:device0 to list sysfs of device iio:device0 (adxl345). Refer to screenshot below:

Please run cat command to show information of device iio:device0. Refer to screenshot below:

Device name is adxl345. Device number is 252:0. Available sampling frequencies are 0.09765625, 0.1953125, 0.390625, … 3200 (Hz). Current sampling frequency is 100.000000000 (Hz). Scaling factor is 0.038300 (m/S2) per LSB. Calibration bias are all zero.

Run cat /sys/bus/iio/devices/iio:device0/in_accel_x_raw to read x-direction acceleration (raw data). Run cat /sys/bus/iio/devices/iio:device0/in_accel_y_raw to read y-direction acceleration (raw data). Run cat /sys/bus/iio/devices/iio:device0/in_accel_z_raw to read z-direction acceleration (raw data). Refer to screenshot below:

The read-back values are raw data. Acceleration in x-direction is

in_accel_x_raw * in_accel_scale + in_accel_x_calibbias = 7 * 0.0383 + 0 = 0.2681 (m/S2)

Acceleration in y-direction is

in_accel_y_raw * in_accel_scale + in_accel_y_calibbias = -9 * 0.0383 + 0 = -0.3447 (m/S2)

Acceleration in z-direction is

in_accel_z_raw * in_accel_scale + in_accel_z_calibbias = 251 * 0.0383 + 0 = 9.6133 (m/S2).

7. User space SPI device

If you want to use user-space applications to access SPI device file, instead of IIO interface via sysfs, please enable “User mode SPI device driver support”. Refer to screenshot below:

Also, modify spi node of device-tree source as below:

&spi_controller0 { spi-max-frequency = <50000000>; pinctrl-names = "default"; pinctrl-0 = <&spim0_pins>; #address-cells = <1>; #size-cells = <0>; spi@0 { compatible = "rohm,dh2228fv"; spi-max-frequency = <5000000>; reg = <0x0>; spi-cpol; spi-cpha; status = "okay"; }; };

Note spi-max-frequency should be less than or equal to 5,000,000. Properties spi-cpol and spi-cpha (SPI mode 3) should be set.

When spidev probes successfully, it creates character device spidevB.C in directory /dev, where B represents bus number and C represents chip-select. Refer to ls report, a SPI device, spidev0.0, is created.

7. Example C code of using user-space device

Refer to the following C code for accessing ADXL345 via Linux device file, /dev/spidev0.0.

Refer to screenshot captured when executable adxl345 was running: