-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmx055.cpp
185 lines (155 loc) · 4.41 KB
/
bmx055.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "bmx055.h"
BMX055::BMX055() : _spi()
{
pinMode(ACC_CS, OUTPUT);
this->_spi_settings = SPISettings(SPI_SPEED_CLOCK_DEFAULT, MSBFIRST, SPI_MODE_0);
this->_spi = new SPIClass(BMX_MOSI, BMX_MISO, BMX_SCK);
}
BMX055::~BMX055() {}
void BMX055::init(POWER_MODE_T power, RANGE_T range, BW_T bw)
{
// In case we use low power mode, we want to make sure it works in low power mode 2
this->setLowPowerMode2();
// Set the default power mode to normal
this->setPowerMode(power);
// Set range to 2G
this->setRange(range);
// Set bandwidth
this->setBandwidth(bw);
// enable output filtering, register shadowing, FIFO
this->enableOutputFiltering();
this->enableRegisterShadowing();
this->FIFOConfig();
this->enableFIFO();
return;
}
void BMX055::update()
{
uint8_t reg = REG_FIFO_DATA;
int buffer_length = FIFO_BUF_LEN;
int64_t result = this->SPIReadAccel(reg, FIFO_BUF_LEN);
int16_t value = 0;
float divisor = 16.0;
for (int i = 0; i < 3; i++)
{
value = result >> 8 * 2 * i;
value = ((0x00FF & value) << 8) | ((0xFF00 & value) >> 8);
acceleration[i] = float(value / divisor);
}
return;
}
uint8_t BMX055::getChipID()
{
uint8_t result = this->SPIReadAccel(REG_CHIP_ID, 1);
return result;
}
void BMX055::getAccelerometer(float *x, float *y, float *z)
{
*x = (acceleration[2] * acc_scale) / 1000.0;
*y = (acceleration[1] * acc_scale) / 1000.0;
*z = (acceleration[0] * acc_scale) / 1000.0;
return;
}
void BMX055::setRange(RANGE_T range)
{
switch (range)
{
case RANGE_2G:
this->acc_scale = 0.91;
break;
case RANGE_4G:
this->acc_scale = 1.95;
break;
case RANGE_8G:
this->acc_scale = 3.91;
break;
case RANGE_16G:
this->acc_scale = 7.81;
break;
}
this->SPIWriteAccel(REG_PMU_RANGE, range);
}
void BMX055::setBandwidth(BW_T bw)
{
this->SPIWriteAccel(REG_PMU_BW, bw);
}
void BMX055::FIFOConfig(FIFO_MODE_T mode, FIFO_DATA_SEL_T data_select)
{
uint8_t config = ((mode << _FIFO_CONFIG_1_FIFO_MODE_SHIFT) | (data_select << _FIFO_CONFIG_1_FIFO_DATA_SHIFT));
this->SPIWriteAccel(REG_FIFO_CONFIG_1, config);
}
void BMX055::enableFIFO(bool enable)
{
this->use_fifo = enable;
}
void BMX055::enableOutputFiltering(bool enable)
{
uint8_t filtering = (uint8_t)this->SPIReadAccel(REG_ACC_HBW, 1) & ~_ACC_HBW_RESERVED_BITS;
if (enable)
{
filtering &= ~ACC_HBW_DATA_HIGH_BW;
}
else
{
filtering |= ACC_HBW_DATA_HIGH_BW;
}
this->SPIWriteAccel(REG_ACC_HBW, filtering);
}
void BMX055::enableRegisterShadowing(bool enable)
{
uint8_t shadowing = (uint8_t)this->SPIReadAccel(REG_ACC_HBW, 1) & ~_ACC_HBW_RESERVED_BITS;
if (enable)
{
shadowing &= ~ACC_HBW_SHADOW_DIS;
}
else
{
shadowing |= ACC_HBW_SHADOW_DIS;
}
this->SPIWriteAccel(REG_ACC_HBW, shadowing);
}
void BMX055::setPowerMode(POWER_MODE_T power)
{
uint8_t power_mode = (uint8_t)this->SPIReadAccel(REG_PMU_LPW, 1) & ~_PMU_LPW_RESERVED_MASK;
power_mode &= ~(_PMU_LPW_POWER_MODE_MASK << _PMU_LPW_POWER_MODE_SHIFT);
power_mode |= (power << _PMU_LPW_POWER_MODE_SHIFT);
this->SPIWriteAccel(REG_PMU_LPW, power_mode);
}
void BMX055::setLowPowerMode2()
{
uint8_t low_power_config = (uint8_t)this->SPIReadAccel(REG_PMU_LOW_POWER, 1) & ~_LOW_POWER_RESERVED_BITS;
low_power_config |= LOW_POWER_LOWPOWER_MODE;
this->SPIWriteAccel(REG_PMU_LOW_POWER, low_power_config);
}
int64_t BMX055::SPIReadAccel(uint8_t reg, uint8_t bytes_to_read)
{
uint8_t in_byte = 0;
int64_t result = 0;
uint8_t command = reg | READ;
this->_spi->beginTransaction(this->_spi_settings);
digitalWrite(ACC_CS, LOW);
this->_spi->transfer(command);
result = this->_spi->transfer(0x00);
bytes_to_read--;
while (bytes_to_read > 0)
{
result = result << 8;
in_byte = this->_spi->transfer(0x00);
result |= in_byte;
bytes_to_read--;
}
digitalWrite(ACC_CS, HIGH);
this->_spi->endTransaction();
return result;
}
void BMX055::SPIWriteAccel(uint8_t reg, uint8_t write_data)
{
uint8_t command = reg | WRITE;
this->_spi->beginTransaction(this->_spi_settings);
digitalWrite(ACC_CS, LOW);
this->_spi->transfer(command);
this->_spi->transfer(write_data);
digitalWrite(ACC_CS, HIGH);
this->_spi->endTransaction();
return;
}