add vendor accelerometer library

vendor accelerometer library is added, and initial code to use it has also been adapted from an example. this is incomplete; need to implement interrupts if using them. for now this may be just enough to work in polled mode.
This commit is contained in:
true 2024-10-15 04:24:45 -07:00
parent caaa30d67e
commit 8a5e11592c
8 changed files with 4506 additions and 25 deletions

View File

@ -1,8 +0,0 @@
/*
* lis2dw.c
*
* Created on: Oct 11, 2024
* Author: true
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
/*
* lis2dw.c
*
* Created on: Oct 11, 2024
* Author: true
*/
#include "lis2dw_i2c.h"
#include <stdint.h>
void lis2dw_init()
{
uint8_t buf[2];
/*
buf[0] = ADXL345_REG_DATAX0 | ADXL345_MODE_RD | ADXL345_MODE_MB;
memset(&buf[1], 0x00, 6);
lis2dw_i2c_write(LIS2DW_I2C_ADDR);
adxl_spi_xfer(ADXL345_SPI_DEV, buf, buf, 7);
*/
}
void lis2dw_set_power_mode(uint8_t mode)
{
}
void lis2dw_read_axes(uint16_t *x, uint16_t *y, uint16_t *z)
{
uint8_t buf[6];
buf[0] = LIS2DW_REG_OUT_X_L;
lis2dw_i2c_write(LIS2DW_I2C_ADDR, buf, 1);
lis2dw_i2c_read(LIS2DW_I2C_ADDR, buf, 6);
*x = buf[0] | buf[1] << 8;
*y = buf[2] | buf[3] << 8;
*z = buf[4] | buf[5] << 8;
}

View File

@ -9,9 +9,18 @@
#define USER_HW_LIS2DW_H_
#include "comm/i2c.h"
#define LIS2DW_I2C_ADDR 0x30
#define LIS2DW_I2C_ADDR_SDO_LOW 0x32
#define lis2dw_i2c_read(a, d, s) i2c_read(a, d, s);
#define lis2dw_i2c_write(a, d, s) i2c_write(a, d, s);
// driver constants
#define LIS2DW_ADDR 0x30
#define LIS2DW_ADDR_SDO_LOW 0x31
// registers
#define LIS2DW_REG_OUT_T_L 0x0d
@ -56,25 +65,31 @@
// register properties
#define LIS2DW_WHO_AM_I 0x44
#define LIS2DW_CTRL1_LP_MODE1 (0 << 0)
#define LIS2DW_CTRL1_LP_MODE2 (1 << 0)
#define LIS2DW_CTRL1_LP_MODE3 (2 << 0)
#define LIS2DW_CTRL1_LP_MODE4 (3 << 0)
#define LIS2DW_CTRL1_LP_MODE_SHIFT 0
enum LIS2DW_CTRL1_LP_Mode {
LIS2DW_CTRL1_LP_MODE1 = 0,
LIS2DW_CTRL1_LP_MODE2,
LIS2DW_CTRL1_LP_MODE3,
LIS2DW_CTRL1_LP_MODE4
};
#define LIS2DW_CTRL1_MODE_LO_POWR (0 << 2)
#define LIS2DW_CTRL1_MODE_HI_PERF (1 << 2)
#define LIS2DW_CTRL1_MODE_SNGL_DATA (2 << 2)
#define LIS2DW_CTRL1_DATA_RATE_PWR_DOWN (0 << 4)
#define LIS2DW_CTRL1_DATA_RATE_1_6 (1 << 4) // 12.5 in high power mode
#define LIS2DW_CTRL1_DATA_RATE_12_5 (2 << 4)
#define LIS2DW_CTRL1_DATA_RATE_25 (3 << 4)
#define LIS2DW_CTRL1_DATA_RATE_50 (4 << 4)
#define LIS2DW_CTRL1_DATA_RATE_100 (5 << 4)
#define LIS2DW_CTRL1_DATA_RATE_200 (6 << 4)
#define LIS2DW_CTRL1_DATA_RATE_400 (7 << 4) // 200 in low power mode
#define LIS2DW_CTRL1_DATA_RATE_800 (8 << 4) // 200 in low power mode
#define LIS2DW_CTRL1_DATA_RATE_1600 (9 << 4) // 200 in low power mode
#define LI22DW_CTRL1_DATA_RATE_SHIFT 4
enum LIS2DW_CTRL1_Data_Rate {
LIS2DW_CTRL1_DATA_RATE_PWR_DOWN = 0,
LIS2DW_CTRL1_DATA_RATE_1_6, // 12.5 in high power mode
LIS2DW_CTRL1_DATA_RATE_12_5,
LIS2DW_CTRL1_DATA_RATE_25,
LIS2DW_CTRL1_DATA_RATE_50,
LIS2DW_CTRL1_DATA_RATE_100,
LIS2DW_CTRL1_DATA_RATE_200,
LIS2DW_CTRL1_DATA_RATE_400, // 200 in low power mode
LIS2DW_CTRL1_DATA_RATE_800, // 200 in low power mode
LIS2DW_CTRL1_DATA_RATE_1600 // 200 in low power mode
};
#define LIS2DW_CTRL2_SPI_4_WIRE (0 << 0)
#define LIS2DW_CTRL2_SPI_3_WIRE (1 << 0)

View File

@ -15,7 +15,7 @@
#include "rgbled.h"
#include "hw/lis2dw.h"
#include "misc/accel.h"
#include "../misc/intscale.h"
#include "../misc/sin7.h"

View File

@ -7,8 +7,12 @@
#include "accel.h"
#include "hw/lis2dw12_reg.h"
#include "comm/i2c.h"
// user data
AccelData accel;
AccelData accel_last[4];
AccelData accel_smoothing;
@ -16,6 +20,59 @@ AccelData accel_smoothing;
int16_t movement;
// hardware
static stmdev_ctx_t dev_ctx;
int32_t accel_i2c_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
{
(void)(handle);
i2c_write_reg8(LIS2DW_I2C_ADDR, reg, bufp, len);
return 0;
}
int32_t accel_i2c_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
(void)(handle);
i2c_read_reg8(LIS2DW_I2C_ADDR, reg, bufp, len);
return 0;
}
void accel_init()
{
uint8_t devid;
uint8_t reset;
dev_ctx.write_reg = accel_i2c_write;
dev_ctx.read_reg = accel_i2c_read;
// make sure we've got the right device
lis2dw12_device_id_get(&dev_ctx, &devid);
if (devid != LIS2DW12_ID) {
while (1) {
// might as well crash the user's badge
}
}
// reset accelerometer
lis2dw12_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
lis2dw12_reset_get(&dev_ctx, &reset);
} while (reset);
// configure scale, power mode
lis2dw12_full_scale_set(&dev_ctx, LIS2DW12_2g);
lis2dw12_power_mode_set(&dev_ctx, LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4);
// configure output data rate
lis2dw12_data_rate_set(&dev_ctx, LIS2DW12_XL_ODR_100Hz);
}
int8_t accel_get_rotation()
{

View File

@ -13,6 +13,11 @@
#define LIS2DW_I2C_ADDR 0x30
#define LIS2DW_I2C_ADDR_SDO_LOW 0x32
typedef struct AccelData {
int16_t x;
int16_t y;
@ -26,6 +31,8 @@ extern uint16_t movement_worst;
void accel_init();
int8_t accel_get_rotation();
int16_t accel_get_movement();