DE10-Nano: HPS I2C accelerometer example


Introduction


There is a 3-axis digital accelerometer (Analog Devices ADXL345) on the DE10-Nano development board. It is configured for the I2C interface instead of the SPI which it also supports. The I2C clock (Pin14 = SCLK) and data (Pin13 = SDIO) pins are connected to the HPS I2C0 controller, and the optional interrupt pin (INT1) is connected to the HPS GPIO controller pin number 61 (HPS GPIO61).

The Terasic DE10-Nano System CD contains an example linux program that reads and then displays results to the terminal.

My example is a standalone program. Since I2C is a fairly straighforward I've decided to write my own HPS I2C controller library that I can use for reading out the accelerometer, and then display the 12-bit results over the UART-USB.

Standalone example


Get the source code from here: adxl345

Parameters


I've tried to parameterised most of the accelerometer's settings through defines, located at the top of the main.c file.

Parameter Description
OPT_ADXL345_INT1_ENABLE Sets interrupt or polling mode: 0 = polling, 1 = interrupt via INT1 pin
OPT_ADXL345_FIFO_ENABLE Enables the 32 entry FIFO mode: 0 = FIFO disabled, 1 = FIFO
OPT_ADXL345_WATERLEVEL When FIFO mode is enabled, this sets the trigger level (number of entries) when a readout is made
OPT_ADXL345_RATE Sets the output rate, available values are:
  • TRU_ADXL345_RATE_0P10_HZ
  • TRU_ADXL345_RATE_0P20_HZ
  • TRU_ADXL345_RATE_0P39_HZ
  • TRU_ADXL345_RATE_0P78_HZ
  • TRU_ADXL345_RATE_1P56_HZ
  • TRU_ADXL345_RATE_3P13_HZ
  • TRU_ADXL345_RATE_6P25_HZ
  • TRU_ADXL345_RATE_12P5_HZ
  • TRU_ADXL345_RATE_25_HZ
  • TRU_ADXL345_RATE_50_HZ
  • TRU_ADXL345_RATE_100_HZ
  • TRU_ADXL345_RATE_200_HZ
  • TRU_ADXL345_RATE_400_HZ
  • TRU_ADXL345_RATE_800_HZ
  • TRU_ADXL345_RATE_1600_HZ
  • TRU_ADXL345_RATE_3200_HZ
OPT_ADXL345_RANGE Sets the measuring range:
  • TRU_ADXL345_RANGE_2G
  • TRU_ADXL345_RANGE_4G
  • TRU_ADXL345_RANGE_8G
  • TRU_ADXL345_RANGE_16G
OPT_ADXL345_OFSX
OPT_ADXL345_OFSY
OPT_ADXL345_OFSZ
Sets a scaled offset calibration. The offset calibration is scaled by the following formula:
OFFSETX = OFSX * 15.6mg
OFFSETY = OFSY * 15.6mg
OFFSETZ = OFSZ * 15.6mg
OPT_ADXL345_TAP_SINGLE_ENABLE Enable single tap detection: 0 = disable, 1 = enable. This needs a fast output rate, such as 50Hz or more.
OPT_ADXL345_TAP_DOUBLE_ENABLE Enable single tap detection: 0 = disable, 1 = enable. This needs a fast output rate, such as 100Hz or more.
OPT_ADXL345_TAP_X_ENABLE
OPT_ADXL345_TAP_Y_ENABLE
OPT_ADXL345_TAP_Z_ENABLE
Enable an axis to participate in the tap detection: 0 = disable, 1 = enable
OPT_ADXL345_TAP_THR Tap detection threshold value: THRESHOLD = THR * 62.5 mg
OPT_ADXL345_TAP_DUR Tap detection duration value: DURATION = DUR * 625 us
OPT_ADXL345_TAP_LAT Tap detection latency value: LATENT = LAT * 1.25ms
OPT_ADXL345_TAP_WIN Tap detection window value: WINDOW = WIN * 1.25ms

Document date: Rev 1: 06 Jan 2025