2023-12-19 10:21:40 +00:00
|
|
|
# LIS3DH
|
|
|
|
|
2024-01-03 15:41:58 +00:00
|
|
|
A C89 driver for the 3-axis accelerometer LIS3DH. Supports both I2C and SPI.
|
2023-12-25 00:06:59 +00:00
|
|
|
|
2024-01-03 15:41:58 +00:00
|
|
|
### Features
|
2024-01-01 12:05:53 +00:00
|
|
|
> - FIFO
|
|
|
|
> - HP filter
|
2023-12-25 00:06:59 +00:00
|
|
|
> - 2G, 4G, 8G and 16G
|
2024-01-01 13:34:11 +00:00
|
|
|
> - Low-power mode, normal mode and high-resolution mode
|
2024-01-03 15:41:58 +00:00
|
|
|
> - ADC and temperature sensing
|
2024-01-01 12:05:53 +00:00
|
|
|
> - Interrupt generation
|
2024-01-03 15:41:58 +00:00
|
|
|
> - Free-fall detection
|
2024-01-01 12:05:53 +00:00
|
|
|
> - Single-click detection
|
2024-01-01 13:08:06 +00:00
|
|
|
> - Double-click detection
|
2024-01-03 16:48:22 +00:00
|
|
|
> - 4D/6D orientation detection
|
2023-12-25 00:06:59 +00:00
|
|
|
|
2024-01-03 15:41:58 +00:00
|
|
|
|
2024-01-01 12:05:53 +00:00
|
|
|
## Examples
|
2024-01-03 15:41:58 +00:00
|
|
|
See the `examples/` dir for complete code examples
|
2024-01-01 12:05:53 +00:00
|
|
|
|
2023-12-27 20:59:47 +00:00
|
|
|
## Implementation
|
2024-01-03 15:41:58 +00:00
|
|
|
This driver requires the user to implement the following interface functions:
|
2023-12-27 19:54:47 +00:00
|
|
|
|
2024-01-04 14:37:44 +00:00
|
|
|
This project has example interface code for I2C and SPI (broken) used on Raspberry Pi 4.
|
2023-12-27 20:59:47 +00:00
|
|
|
```c
|
|
|
|
/* initialise the "interface" */
|
|
|
|
int init(void);
|
2024-01-03 15:41:58 +00:00
|
|
|
/* read from register `reg`, `size` amount of bytes, and write them to `dst` */
|
2023-12-27 20:59:47 +00:00
|
|
|
int read(uint8_t reg, uint8_t *dst, uint32_t size);
|
2024-01-03 15:41:58 +00:00
|
|
|
/* write `value` to register `reg` */
|
2023-12-27 20:59:47 +00:00
|
|
|
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);
|
|
|
|
```
|
2024-01-03 15:41:58 +00:00
|
|
|
All above functions return `0` on success, and any non-zero value on error.
|
2023-12-27 19:54:47 +00:00
|
|
|
|
2024-01-04 14:37:44 +00:00
|
|
|
If `init` and/or `deinit` are set to `NULL`, they will be ignored. Useful on microcontrollers.
|
2023-12-29 23:24:15 +00:00
|
|
|
|
2024-01-03 15:41:58 +00:00
|
|
|
---
|
2023-12-27 19:54:47 +00:00
|
|
|
|
|
|
|
### Using i2c on STM32
|
2024-01-03 15:41:58 +00:00
|
|
|
Example code because I couldn't previously find working examples.
|
2023-12-25 00:06:59 +00:00
|
|
|
```c
|
|
|
|
#define LIS3DH_I2C_ADDR 0x18
|
|
|
|
|
|
|
|
int i2c_write(uint8_t reg, uint8_t value) {
|
2023-12-27 19:59:29 +00:00
|
|
|
uint8_t buf[2] = { reg, value };
|
|
|
|
HAL_I2C_Master_Transmit(&hi2c2, LIS3DH_I2C_ADDR << 1, buf, 2, HAL_MAX_DELAY);
|
|
|
|
return 0;
|
2023-12-25 00:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
2023-12-27 19:59:29 +00:00
|
|
|
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;
|
2023-12-25 00:06:59 +00:00
|
|
|
}
|
2023-12-27 19:54:47 +00:00
|
|
|
```
|
|
|
|
|