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:
Kevin O'Connor 2023-12-17 17:59:25 -05:00
parent dc6182f3b3
commit 266e96621c
13 changed files with 153 additions and 189 deletions

View file

@ -10,6 +10,7 @@
#include "board/irq.h" // irq_disable
#include "command.h" // DECL_COMMAND
#include "sched.h" // DECL_TASK
#include "sensor_bulk.h" // sensor_bulk_report
#include "spicmds.h" // spidev_transfer
enum { SA_CHIP_A1333, SA_CHIP_AS5047D, SA_CHIP_TLE5012B, SA_CHIP_MAX };
@ -29,15 +30,16 @@ struct spi_angle {
struct timer timer;
uint32_t rest_ticks;
struct spidev_s *spi;
uint16_t sequence;
uint8_t flags, chip_type, data_count, time_shift, overflow;
uint8_t data[48];
uint8_t flags, chip_type, time_shift, overflow;
struct sensor_bulk sb;
};
enum {
SA_PENDING = 1<<2,
};
#define BYTES_PER_SAMPLE 3
static struct task_wake angle_wake;
// Event handler that wakes spi_angle_task() periodically
@ -72,32 +74,22 @@ command_config_spi_angle(uint32_t *args)
DECL_COMMAND(command_config_spi_angle,
"config_spi_angle oid=%c spi_oid=%c spi_angle_type=%c");
// Report local measurement buffer
static void
angle_report(struct spi_angle *sa, uint8_t oid)
{
sendf("spi_angle_data oid=%c sequence=%hu data=%*s"
, oid, sa->sequence, sa->data_count, sa->data);
sa->data_count = 0;
sa->sequence++;
}
// Send spi_angle_data message if buffer is full
static void
angle_check_report(struct spi_angle *sa, uint8_t oid)
{
if (sa->data_count + 3 > ARRAY_SIZE(sa->data))
angle_report(sa, oid);
if (sa->sb.data_count + BYTES_PER_SAMPLE > ARRAY_SIZE(sa->sb.data))
sensor_bulk_report(&sa->sb, oid);
}
// Add an entry to the measurement buffer
static void
angle_add(struct spi_angle *sa, uint_fast8_t tcode, uint_fast16_t data)
{
sa->data[sa->data_count] = tcode;
sa->data[sa->data_count + 1] = data;
sa->data[sa->data_count + 2] = data >> 8;
sa->data_count += 3;
sa->sb.data[sa->sb.data_count] = tcode;
sa->sb.data[sa->sb.data_count + 1] = data;
sa->sb.data[sa->sb.data_count + 2] = data >> 8;
sa->sb.data_count += BYTES_PER_SAMPLE;
}
// Add an error indicator to the measurement buffer
@ -237,8 +229,7 @@ command_query_spi_angle(uint32_t *args)
// Start new measurements query
sa->timer.waketime = args[1];
sa->rest_ticks = args[2];
sa->sequence = 0;
sa->data_count = 0;
sensor_bulk_reset(&sa->sb);
sa->time_shift = args[3];
sched_add_timer(&sa->timer);
}