Go to file
2024-01-01 11:05:45 +00:00
doc working simple example 2023-12-21 23:29:22 +00:00
example examples and more debug flags 2023-12-30 13:10:40 +00:00
.gitignore INT dur table 2023-12-31 22:31:22 +00:00
i2c.c examples and more debug flags 2023-12-30 13:10:40 +00:00
i2c.h working simple example 2023-12-21 23:29:22 +00:00
interrupt.c interrupt generation 2023-12-28 18:10:34 +00:00
interrupt.h interrupt generation 2023-12-28 18:10:34 +00:00
lis3dh.c more src reg interaction 2024-01-01 10:15:57 +00:00
lis3dh.h more src reg interaction 2024-01-01 10:15:57 +00:00
main.c single click detection 2024-01-01 11:05:45 +00:00
Makefile examples and more debug flags 2023-12-30 13:10:40 +00:00
README.md examples and more debug flags 2023-12-30 13:10:40 +00:00
registers.h register map 2023-12-19 10:21:48 +00:00

LIS3DH

A C89 driver for the 3-axis accelerometer LIS3DH. Supports both i2c and SPI.

Features

  • FIFO of varying watermark level, up to 32
  • HP filter (4 c/o freq)
  • 2G, 4G, 8G and 16G
  • All power modes
  • Interrupt generation (partial)
  • Free-fall detection (soon)
  • Single and double click detection (soon)
  • 4D/6D orientation detection (soon)

Implementation

This driver requires the user to provide pointers to the following abstractely named functions:

This project has example interface code for I2C used on Raspberry Pi.

/* initialise the "interface" */
int init(void);
/* read from device register `reg`, `size` amount of bytes and write them to `dst` */
int read(uint8_t reg, uint8_t *dst, uint32_t size);
/* write `value` to device register `reg` */
int write(uint8_t reg, uint8_t value);
/* sleep for `dur_us` microseconds */
int sleep(uint32_t dur_us);
/* deinitalise the "interface" */
int deinit(void);

All above functions return 0 on success.

The init and deinit pointers can both be set to NULL and they won't be run.

Using i2c on STM32

Simple example code

#define LIS3DH_I2C_ADDR 0x18

int i2c_write(uint8_t reg, uint8_t value) {
    uint8_t buf[2] = { reg, value };
    HAL_I2C_Master_Transmit(&hi2c2, LIS3DH_I2C_ADDR << 1, buf, 2, HAL_MAX_DELAY);
    return 0;
}

int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) {
    uint8_t send[2] = { reg, 0x00 };
    HAL_I2C_Master_Transmit(&hi2c2, LIS3DH_I2C_ADDR << 1, send, 2, HAL_MAX_DELAY);
    HAL_I2C_Master_Receive(&hi2c2, LIS3DH_I2C_ADDR << 1, dst, size, HAL_MAX_DELAY);
    return 0;
}