-
Notifications
You must be signed in to change notification settings - Fork 35
/
motion_task.cpp
340 lines (300 loc) · 10.5 KB
/
motion_task.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "motion_task.h"
#include <bma423.h>
#include <Arduino.h>
#include <Wire.h>
#include "board_def.h"
#include "FreeRTOS.h"
#include "freertos/queue.h"
#include "struct_def.h"
#include "lv_swatch.h"
#define BMA423_FEATURE_SIZE 64
#define ACCEL_ENABLE 1
#define ACCEL_DISABLE 0
extern xQueueHandle g_event_queue_handle;
static struct bma4_dev bmd4_dev;
static EventGroupHandle_t motionEventGroup = NULL;
static motion_struct_t motion_data;
static uint32_t step_counter = 0;
static int scanI2Cdevice(void);
static uint16_t _bma423_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *read_data, uint16_t len);
static uint16_t _bma423_write(uint8_t dev_addr, uint8_t reg_addr, uint8_t *write_data, uint16_t len);
static uint16_t bma423_accel_enable();
static uint16_t bma_get_dir(direction_t *result);
static uint16_t configure_interrupt();
static uint16_t bma423_read_temp(float *temp, uint8_t unit = BMA4_DEG);
static void bma423_soft_reset();
#define MOTION_GET_DATA_BIT 0X01
#define MOTION_GET_SETP_BIT 0X02
static void motion_task(void *param)
{
float temp;
struct bma4_accel sens_data;
char buf[64];
for (;;) {
EventBits_t bits = xEventGroupWaitBits(motionEventGroup,
MOTION_GET_DATA_BIT | MOTION_GET_SETP_BIT,
pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & MOTION_GET_DATA_BIT) {
direction_t result;
if ( bma_get_dir(&result) == BMA4_OK) {
motion_dir_update((uint8_t)result);
}
} else if (bits & MOTION_GET_SETP_BIT) {
uint16_t int_status = 0;
uint32_t stepCount;
uint16_t rlst;
do {
rlst = bma423_read_int_status(&int_status, &bmd4_dev);
} while (rlst != BMA4_OK);
// Serial.printf("Read MOTION_GET_SETP_BIT[0x%x]\n", int_status);
if (int_status & BMA423_STEP_CNTR_INT) {
if (bma423_step_counter_output(&stepCount, &bmd4_dev) == BMA4_OK) {
snprintf(buf, sizeof(buf), "%u", stepCount);
Serial.println(stepCount);
lv_main_step_counter_update(buf);
}
} else if (int_status & BMA423_WAKEUP_INT) {
// Serial.printf("BMA423_WAKEUP_INT\n");
}
xEventGroupClearBits(motionEventGroup, MOTION_GET_SETP_BIT );
}
}
}
void motion_handle(void *arg)
{
motion_struct_t *p = (motion_struct_t *)arg;
switch ((p->event)) {
case LVGL_MOTION_STOP:
Serial.println("[BMA] bma423 power off ...");
xEventGroupClearBits(motionEventGroup, MOTION_GET_DATA_BIT | MOTION_GET_SETP_BIT);
break;
case LVGL_MOTION_GET_ACCE:
xEventGroupSetBits(motionEventGroup, MOTION_GET_DATA_BIT);
break;
case LVGL_MOTION_GET_STEP:
xEventGroupSetBits(motionEventGroup, MOTION_GET_SETP_BIT);
break;
default:
break;
}
}
bool motion_task_init()
{
uint16_t rslt = BMA4_OK;
motionEventGroup = xEventGroupCreate();
bmd4_dev.dev_addr = BMA4_I2C_ADDR_SECONDARY;
bmd4_dev.interface = BMA4_I2C_INTERFACE;
bmd4_dev.bus_read = _bma423_read;
bmd4_dev.bus_write = _bma423_write;
bmd4_dev.delay = delay;
bmd4_dev.read_write_len = 8;
bmd4_dev.resolution = 12;
bmd4_dev.feature_len = BMA423_FEATURE_SIZE;
bma423_soft_reset();
rslt = bma423_init(&bmd4_dev);
if (rslt != BMA4_OK) {
Serial.println("bma4 init fail");
return false;
}
rslt = bma423_write_config_file(&bmd4_dev);
if (rslt != BMA4_OK) {
Serial.println("bma4 write config fail");
return false;
}
configure_interrupt();
xTaskCreatePinnedToCore(motion_task, "motion", 4096, NULL, 20, NULL, 0);
return true;
}
static void bma423_soft_reset()
{
uint8_t reg = 0xB6;
_bma423_write(BMA4_I2C_ADDR_SECONDARY, 0x7E, ®, 1);
delay(5);
}
#define ENABLE_BMA_INT1
static uint16_t configure_interrupt()
{
uint16_t rslt = BMA4_OK;
rslt |= bma423_accel_enable();
rslt |= bma423_step_counter_set_watermark(100, &bmd4_dev);
rslt |= bma423_reset_step_counter(&bmd4_dev);
rslt |= bma423_feature_enable(BMA423_STEP_CNTR, BMA4_ENABLE, &bmd4_dev);
rslt |= bma423_feature_enable(BMA423_WAKEUP, BMA4_ENABLE, &bmd4_dev);
rslt |= bma423_step_detector_enable(BMA4_ENABLE, &bmd4_dev);
#ifdef ENABLE_BMA_INT1
rslt |= bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_STEP_CNTR_INT | BMA423_WAKEUP_INT, BMA4_ENABLE, &bmd4_dev);
struct bma4_int_pin_config config ;
config.edge_ctrl = BMA4_LEVEL_TRIGGER;
config.lvl = BMA4_ACTIVE_HIGH;
config.od = BMA4_PUSH_PULL;
config.output_en = BMA4_OUTPUT_ENABLE;
config.input_en = BMA4_INPUT_DISABLE;
#endif
#ifdef ENABLE_BMA_INT2
rslt |= bma423_map_interrupt(BMA4_INTR2_MAP, BMA423_STEP_CNTR_INT | BMA423_WAKEUP_INT, BMA4_ENABLE, &bmd4_dev);
Serial.printf("configure_interrupt rslt : %x\n", rslt);
struct bma4_int_pin_config config ;
config.edge_ctrl = BMA4_LEVEL_TRIGGER;
config.lvl = BMA4_ACTIVE_LOW;
config.od = BMA4_PUSH_PULL;
config.output_en = BMA4_OUTPUT_ENABLE;
config.input_en = BMA4_INPUT_DISABLE;
#endif
#ifdef ENABLE_BMA_INT1
rslt |= bma4_set_int_pin_config(&config, BMA4_INTR1_MAP, &bmd4_dev);
#endif
#ifdef ENABLE_BMA_INT2
rslt |= bma4_set_int_pin_config(&config, BMA4_INTR2_MAP, &bmd4_dev);
#endif
Serial.printf("configure_interrupt rslt : %x\n", rslt);
#ifdef ENABLE_BMA_INT1
pinMode(BMA423_INT1, INPUT);
attachInterrupt(BMA423_INT1, [] {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
task_event_data_t event_data;
event_data.type = MESS_EVENT_MOTI;
event_data.motion.event = LVGL_MOTION_GET_STEP;
xQueueSendFromISR(g_event_queue_handle, &event_data, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
{
portYIELD_FROM_ISR ();
}
}, RISING/*FALLING*/);
#endif
#ifdef ENABLE_BMA_INT2
pinMode(BMA423_INT2, INPUT_PULLUP);
attachInterrupt(BMA423_INT2, [] {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
task_event_data_t event_data;
event_data.type = MESS_EVENT_MOTI;
event_data.motion.event = LVGL_MOTION_GET_STEP;
xQueueSendFromISR(g_event_queue_handle, &event_data, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
{
portYIELD_FROM_ISR ();
}
}, FALLING);
#endif
}
static uint16_t bma_get_dir(direction_t *result)
{
uint16_t rslt = BMA4_OK;
struct bma4_accel accel;
uint16_t absX, absY, absZ;
rslt = bma4_read_accel_xyz(&accel, &bmd4_dev);
if (rslt != BMA4_OK) {
return rslt;
}
absX = abs(accel.x);
absY = abs(accel.y);
absZ = abs(accel.z);
if ((absZ > absX) && (absZ > absY)) {
if (accel.z > 0) {
*result = DIRECTION_DISP_DOWN;
} else {
*result = DIRECTION_DISP_UP;
}
} else if ((absY > absX) && (absY > absZ)) {
if (accel.y > 0) {
*result = DIRECTION_BOTTOM_EDGE;
} else {
*result = DIRECTION_TOP_EDGE;
}
} else {
if (accel.x < 0) {
*result = DIRECTION_RIGHT_EDGE;
} else {
*result = DIRECTION_LEFT_EDGE;
}
}
return rslt;
}
static uint16_t bma423_read_temp(float *temp, uint8_t unit)
{
uint16_t rslt;
int32_t get_temp = 0;
if (unit > 0 && unit < 4) {
rslt = bma4_get_temperature(&get_temp, unit, &bmd4_dev);
*temp = (float)get_temp / (float)BMA4_SCALE_TEMP;
/* 0x80 - temp read from the register and 23 is the ambient temp added.
* If the temp read from register is 0x80, it means no valid
* information is available */
if (unit == BMA4_DEG) {
if (((get_temp - 23) / BMA4_SCALE_TEMP) == 0x80) {
rslt = BMA4_E_FAIL;
}
}
} else {
rslt = BMA4_E_FAIL;
}
return rslt;
}
static uint16_t bma423_accel_enable()
{
uint16_t rslt = BMA4_OK;
//! 3. Configuring the accelerometer
/* Enable the accelerometer */
rslt = bma4_set_accel_enable(ACCEL_ENABLE, &bmd4_dev);
if (rslt != BMA4_OK) {
Serial.println("bma4 enable accel fail");
return rslt;
}
/* Declare an accelerometer configuration structure */
struct bma4_accel_config accel_conf;
/* Assign the desired settings */
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
accel_conf.range = BMA4_ACCEL_RANGE_2G;
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
accel_conf.perf_mode = BMA4_CONTINUOUS_MODE;
/* Set the configuration */
rslt = bma4_set_accel_config(&accel_conf, &bmd4_dev);
if (rslt != BMA4_OK) {
Serial.println("bma4 set accel config fail");
return rslt;
}
return rslt;
}
void bma423_disable_interrupt()
{
uint16_t rslt = BMA4_OK;
rslt = bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_STEP_CNTR_INT | BMA423_WAKEUP_INT, BMA4_DISABLE, &bmd4_dev);
Serial.printf("bma423_disable:%x\n", rslt);
}
void bma423_enable_interrupt()
{
uint16_t rslt = BMA4_OK;
rslt = bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_STEP_CNTR_INT | BMA423_WAKEUP_INT, BMA4_ENABLE, &bmd4_dev);
Serial.printf("bma423_disable:%x\n", rslt);
}
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific read and write functions of the user
*/
static uint16_t _bma423_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *read_data, uint16_t len)
{
uint16_t ret = 0;
Wire1.beginTransmission(dev_addr);
Wire1.write(reg_addr);
Wire1.endTransmission(false);
uint8_t cnt = Wire1.requestFrom(dev_addr, (uint8_t)len, (uint8_t)1);
if (!cnt) {
ret = 1 << 13;
}
uint16_t index = 0;
while (Wire1.available()) {
read_data[index++] = Wire1.read();
}
return ret;
}
static uint16_t _bma423_write(uint8_t dev_addr, uint8_t reg_addr, uint8_t *write_data, uint16_t len)
{
uint16_t ret = 0;
char *err = NULL;
Wire1.beginTransmission(dev_addr);
Wire1.write(reg_addr);
for (uint16_t i = 0; i < len; i++) {
Wire1.write(write_data[i]);
}
ret = Wire1.endTransmission();
return ret ? 1 << 12 : ret;
}