sensor_bulk: New C file with helper code for sending bulk sensor measurements
Refactor the low-level "bulk sensor" management code in the mcu. This updates the sensor_adxl345.c, sensor_mpu9250.c, sensor_lis2dw.c, and sensor_angle.c code to use the same "bulk sensor" messages. All of these sensors will now send "sensor_bulk_data" and "sensor_bulk_status" messages. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "basecmd.h" // oid_alloc
|
||||
#include "command.h" // DECL_COMMAND
|
||||
#include "sched.h" // DECL_TASK
|
||||
#include "sensor_bulk.h" // sensor_bulk_report
|
||||
#include "spicmds.h" // spidev_transfer
|
||||
|
||||
#define LIS_AR_DATAX0 0x28
|
||||
@@ -18,13 +19,14 @@
|
||||
#define LIS_FIFO_CTRL 0x2E
|
||||
#define LIS_FIFO_SAMPLES 0x2F
|
||||
|
||||
#define BYTES_PER_SAMPLE 6
|
||||
|
||||
struct lis2dw {
|
||||
struct timer timer;
|
||||
uint32_t rest_ticks;
|
||||
struct spidev_s *spi;
|
||||
uint16_t sequence, limit_count;
|
||||
uint8_t flags, data_count;
|
||||
uint8_t data[48];
|
||||
uint8_t flags;
|
||||
struct sensor_bulk sb;
|
||||
};
|
||||
|
||||
enum {
|
||||
@@ -53,27 +55,6 @@ command_config_lis2dw(uint32_t *args)
|
||||
}
|
||||
DECL_COMMAND(command_config_lis2dw, "config_lis2dw oid=%c spi_oid=%c");
|
||||
|
||||
// Report local measurement buffer
|
||||
static void
|
||||
lis2dw_report(struct lis2dw *ax, uint8_t oid)
|
||||
{
|
||||
sendf("lis2dw_data oid=%c sequence=%hu data=%*s"
|
||||
, oid, ax->sequence, ax->data_count, ax->data);
|
||||
ax->data_count = 0;
|
||||
ax->sequence++;
|
||||
}
|
||||
|
||||
// Report buffer and fifo status
|
||||
static void
|
||||
lis2dw_status(struct lis2dw *ax, uint_fast8_t oid
|
||||
, uint32_t time1, uint32_t time2, uint_fast8_t fifo)
|
||||
{
|
||||
sendf("lis2dw_status oid=%c clock=%u query_ticks=%u next_sequence=%hu"
|
||||
" buffered=%c fifo=%c limit_count=%hu"
|
||||
, oid, time1, time2-time1, ax->sequence
|
||||
, ax->data_count, fifo, ax->limit_count);
|
||||
}
|
||||
|
||||
// Helper code to reschedule the lis2dw_event() timer
|
||||
static void
|
||||
lis2dw_reschedule_timer(struct lis2dw *ax)
|
||||
@@ -93,7 +74,7 @@ lis2dw_query(struct lis2dw *ax, uint8_t oid)
|
||||
uint8_t fifo_empty,fifo_ovrn = 0;
|
||||
|
||||
msg[0] = LIS_AR_DATAX0 | LIS_AM_READ ;
|
||||
uint8_t *d = &ax->data[ax->data_count];
|
||||
uint8_t *d = &ax->sb.data[ax->sb.data_count];
|
||||
|
||||
spidev_transfer(ax->spi, 1, sizeof(msg), msg);
|
||||
|
||||
@@ -108,13 +89,13 @@ lis2dw_query(struct lis2dw *ax, uint8_t oid)
|
||||
d[4] = msg[5]; // z low bits
|
||||
d[5] = msg[6]; // z high bits
|
||||
|
||||
ax->data_count += 6;
|
||||
if (ax->data_count + 6 > ARRAY_SIZE(ax->data))
|
||||
lis2dw_report(ax, oid);
|
||||
ax->sb.data_count += BYTES_PER_SAMPLE;
|
||||
if (ax->sb.data_count + BYTES_PER_SAMPLE > ARRAY_SIZE(ax->sb.data))
|
||||
sensor_bulk_report(&ax->sb, oid);
|
||||
|
||||
// Check fifo status
|
||||
if (fifo_ovrn)
|
||||
ax->limit_count++;
|
||||
ax->sb.possible_overflows++;
|
||||
|
||||
// check if we need to run the task again (more packets in fifo?)
|
||||
if (!fifo_empty) {
|
||||
@@ -165,8 +146,7 @@ command_query_lis2dw(uint32_t *args)
|
||||
ax->timer.waketime = args[1];
|
||||
ax->rest_ticks = args[2];
|
||||
ax->flags = LIS_HAVE_START;
|
||||
ax->sequence = ax->limit_count = 0;
|
||||
ax->data_count = 0;
|
||||
sensor_bulk_reset(&ax->sb);
|
||||
sched_add_timer(&ax->timer);
|
||||
}
|
||||
DECL_COMMAND(command_query_lis2dw,
|
||||
@@ -180,7 +160,8 @@ command_query_lis2dw_status(uint32_t *args)
|
||||
uint32_t time1 = timer_read_time();
|
||||
spidev_transfer(ax->spi, 1, sizeof(msg), msg);
|
||||
uint32_t time2 = timer_read_time();
|
||||
lis2dw_status(ax, args[0], time1, time2, msg[1]&0x1f);
|
||||
sensor_bulk_status(&ax->sb, args[0], time1, time2-time1
|
||||
, (msg[1] & 0x1f) * BYTES_PER_SAMPLE);
|
||||
}
|
||||
DECL_COMMAND(command_query_lis2dw_status, "query_lis2dw_status oid=%c");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user