Skip to content

Commit 92e8c33

Browse files
committed
Complete LSM6 implementation.
1 parent 5d17e37 commit 92e8c33

File tree

2 files changed

+143
-4
lines changed

2 files changed

+143
-4
lines changed

lsm6.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "lsm6.h"
2+
#include <stdexcept>
3+
4+
void lsm6::handle::open(const comm_config & config)
5+
{
6+
if (!config.use_sensor)
7+
{
8+
throw std::runtime_error("LSM6 configuration is null.");
9+
}
10+
11+
this->config = config;
12+
i2c.open(config.i2c_bus_name);
13+
}
14+
15+
void lsm6::handle::enable()
16+
{
17+
if (config.device == LSM6DS33)
18+
{
19+
//// LSM6DS33 gyro
20+
21+
// ODR = 1000 (1.66 kHz (high performance))
22+
// FS_G = 11 (2000 dps)
23+
write_reg(CTRL2_G, 0b10001100);
24+
25+
// defaults
26+
write_reg(CTRL7_G, 0b00000000);
27+
28+
//// LSM6DS33 accelerometer
29+
30+
// ODR = 1000 (1.66 kHz (high performance))
31+
// FS_XL = 11 (8 g full scale)
32+
// BW_XL = 00 (400 Hz filter bandwidth)
33+
write_reg(CTRL1_XL, 0b10001100);
34+
35+
//// common
36+
37+
// IF_INC = 1 (automatically increment address register)
38+
write_reg(CTRL3_C, 0b00000100);
39+
}
40+
}
41+
42+
void lsm6::handle::write_reg(reg_addr addr, uint8_t value)
43+
{
44+
i2c.write_two_bytes(config.i2c_address, addr, value);
45+
}
46+
47+
void lsm6::handle::read_gyro()
48+
{
49+
uint8_t block[6];
50+
i2c.write_byte_and_read(config.i2c_address,
51+
0x80 | OUTX_L_G, block, sizeof(block));
52+
g[0] = (int16_t)(block[0] | block[1] << 8);
53+
g[1] = (int16_t)(block[2] | block[3] << 8);
54+
g[2] = (int16_t)(block[4] | block[5] << 8);
55+
}
56+
57+
void lsm6::handle::read_acc()
58+
{
59+
uint8_t block[6];
60+
i2c.write_byte_and_read(config.i2c_address,
61+
0x80 | OUTX_L_XL, block, sizeof(block));
62+
a[0] = (int16_t)(block[0] | block[1] << 8);
63+
a[1] = (int16_t)(block[2] | block[3] << 8);
64+
a[2] = (int16_t)(block[4] | block[5] << 8);
65+
}

lsm6.h

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,74 @@ namespace lsm6
1818

1919
enum reg_addr
2020
{
21-
WHO_AM_I = 0x0F,
21+
FUNC_CFG_ACCESS = 0x01,
22+
23+
FIFO_CTRL1 = 0x06,
24+
FIFO_CTRL2 = 0x07,
25+
FIFO_CTRL3 = 0x08,
26+
FIFO_CTRL4 = 0x09,
27+
FIFO_CTRL5 = 0x0A,
28+
ORIENT_CFG_G = 0x0B,
29+
30+
INT1_CTRL = 0x0D,
31+
INT2_CTRL = 0x0E,
32+
WHO_AM_I = 0x0F,
33+
CTRL1_XL = 0x10,
34+
CTRL2_G = 0x11,
35+
CTRL3_C = 0x12,
36+
CTRL4_C = 0x13,
37+
CTRL5_C = 0x14,
38+
CTRL6_C = 0x15,
39+
CTRL7_G = 0x16,
40+
CTRL8_XL = 0x17,
41+
CTRL9_XL = 0x18,
42+
CTRL10_C = 0x19,
43+
44+
WAKE_UP_SRC = 0x1B,
45+
TAP_SRC = 0x1C,
46+
D6D_SRC = 0x1D,
47+
STATUS_REG = 0x1E,
48+
49+
OUT_TEMP_L = 0x20,
50+
OUT_TEMP_H = 0x21,
51+
OUTX_L_G = 0x22,
52+
OUTX_H_G = 0x23,
53+
OUTY_L_G = 0x24,
54+
OUTY_H_G = 0x25,
55+
OUTZ_L_G = 0x26,
56+
OUTZ_H_G = 0x27,
57+
OUTX_L_XL = 0x28,
58+
OUTX_H_XL = 0x29,
59+
OUTY_L_XL = 0x2A,
60+
OUTY_H_XL = 0x2B,
61+
OUTZ_L_XL = 0x2C,
62+
OUTZ_H_XL = 0x2D,
63+
64+
FIFO_STATUS1 = 0x3A,
65+
FIFO_STATUS2 = 0x3B,
66+
FIFO_STATUS3 = 0x3C,
67+
FIFO_STATUS4 = 0x3D,
68+
FIFO_DATA_OUT_L = 0x3E,
69+
FIFO_DATA_OUT_H = 0x3F,
70+
TIMESTAMP0_REG = 0x40,
71+
TIMESTAMP1_REG = 0x41,
72+
TIMESTAMP2_REG = 0x42,
73+
74+
STEP_TIMESTAMP_L = 0x49,
75+
STEP_TIMESTAMP_H = 0x4A,
76+
STEP_COUNTER_L = 0x4B,
77+
STEP_COUNTER_H = 0x4C,
78+
79+
FUNC_SRC = 0x53,
80+
81+
TAP_CFG = 0x58,
82+
TAP_THS_6D = 0x59,
83+
INT_DUR2 = 0x5A,
84+
WAKE_UP_THS = 0x5B,
85+
WAKE_UP_DUR = 0x5C,
86+
FREE_FALL = 0x5D,
87+
MD1_CFG = 0x5E,
88+
MD2_CFG = 0x5F,
2289
};
2390

2491
struct comm_config {
@@ -33,11 +100,18 @@ namespace lsm6
33100
public:
34101
void open(const comm_config &);
35102

36-
// acceleration readings
37-
int16_t a[3];
103+
void enable();
104+
105+
void write_reg(reg_addr addr, uint8_t value);
106+
107+
void read_gyro();
108+
void read_acc();
38109

39110
// gyro angular velocity readings
40-
int16_t g[3];
111+
int32_t g[3];
112+
113+
// acceleration readings
114+
int32_t a[3];
41115

42116
protected:
43117
i2c_bus i2c;

0 commit comments

Comments
 (0)