aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Kconfig4
-rw-r--r--src/Makefile3
-rw-r--r--src/sensor_adxl345.c51
-rw-r--r--src/sensor_angle.c33
-rw-r--r--src/sensor_bulk.c38
-rw-r--r--src/sensor_bulk.h15
-rw-r--r--src/sensor_lis2dw.c45
-rw-r--r--src/sensor_mpu9250.c44
8 files changed, 113 insertions, 120 deletions
diff --git a/src/Kconfig b/src/Kconfig
index aaf50653..91376910 100644
--- a/src/Kconfig
+++ b/src/Kconfig
@@ -112,6 +112,10 @@ config WANT_SOFTWARE_SPI
bool
depends on HAVE_GPIO && HAVE_GPIO_SPI
default y
+config NEED_SENSOR_BULK
+ bool
+ depends on WANT_SENSORS || WANT_LIS2DW
+ default y
menu "Optional features (to reduce code size)"
depends on HAVE_LIMITED_CODE_SIZE
config WANT_GPIO_BITBANGING
diff --git a/src/Makefile b/src/Makefile
index 8d771f9e..eddad978 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -16,6 +16,7 @@ src-$(CONFIG_WANT_SOFTWARE_SPI) += spi_software.c
src-$(CONFIG_WANT_SOFTWARE_I2C) += i2c_software.c
sensors-src-$(CONFIG_HAVE_GPIO_SPI) := thermocouple.c sensor_adxl345.c \
sensor_angle.c
-src-$(CONFIG_WANT_LIS2DW) += sensor_lis2dw.c
sensors-src-$(CONFIG_HAVE_GPIO_I2C) += sensor_mpu9250.c
src-$(CONFIG_WANT_SENSORS) += $(sensors-src-y)
+src-$(CONFIG_WANT_LIS2DW) += sensor_lis2dw.c
+src-$(CONFIG_NEED_SENSOR_BULK) += sensor_bulk.c
diff --git a/src/sensor_adxl345.c b/src/sensor_adxl345.c
index 5ec3945e..8cd471ef 100644
--- a/src/sensor_adxl345.c
+++ b/src/sensor_adxl345.c
@@ -10,15 +10,15 @@
#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
struct adxl345 {
struct timer timer;
uint32_t rest_ticks;
struct spidev_s *spi;
- uint16_t sequence, limit_count;
- uint8_t flags, data_count;
- uint8_t data[50];
+ uint8_t flags;
+ struct sensor_bulk sb;
};
enum {
@@ -47,27 +47,6 @@ command_config_adxl345(uint32_t *args)
}
DECL_COMMAND(command_config_adxl345, "config_adxl345 oid=%c spi_oid=%c");
-// Report local measurement buffer
-static void
-adxl_report(struct adxl345 *ax, uint8_t oid)
-{
- sendf("adxl345_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
-adxl_status(struct adxl345 *ax, uint_fast8_t oid
- , uint32_t time1, uint32_t time2, uint_fast8_t fifo)
-{
- sendf("adxl345_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 adxl345_event() timer
static void
adxl_reschedule_timer(struct adxl345 *ax)
@@ -87,6 +66,8 @@ adxl_reschedule_timer(struct adxl345 *ax)
#define SET_FIFO_CTL 0x90
+#define BYTES_PER_SAMPLE 5
+
// Query accelerometer data
static void
adxl_query(struct adxl345 *ax, uint8_t oid)
@@ -96,7 +77,7 @@ adxl_query(struct adxl345 *ax, uint8_t oid)
spidev_transfer(ax->spi, 1, sizeof(msg), msg);
// Extract x, y, z measurements
uint_fast8_t fifo_status = msg[8] & ~0x80; // Ignore trigger bit
- uint8_t *d = &ax->data[ax->data_count];
+ uint8_t *d = &ax->sb.data[ax->sb.data_count];
if (((msg[2] & 0xf0) && (msg[2] & 0xf0) != 0xf0)
|| ((msg[4] & 0xf0) && (msg[4] & 0xf0) != 0xf0)
|| ((msg[6] & 0xf0) && (msg[6] & 0xf0) != 0xf0)
@@ -112,12 +93,12 @@ adxl_query(struct adxl345 *ax, uint8_t oid)
d[3] = (msg[2] & 0x1f) | (msg[6] << 5); // x high bits and z high bits
d[4] = (msg[4] & 0x1f) | ((msg[6] << 2) & 0x60); // y high and z high
}
- ax->data_count += 5;
- if (ax->data_count + 5 > ARRAY_SIZE(ax->data))
- adxl_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_status >= 31)
- ax->limit_count++;
+ ax->sb.possible_overflows++;
if (fifo_status > 1 && fifo_status <= 32) {
// More data in fifo - wake this task again
sched_wake_task(&adxl345_wake);
@@ -166,8 +147,7 @@ command_query_adxl345(uint32_t *args)
ax->timer.waketime = args[1];
ax->rest_ticks = args[2];
ax->flags = AX_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_adxl345,
@@ -178,10 +158,17 @@ command_query_adxl345_status(uint32_t *args)
{
struct adxl345 *ax = oid_lookup(args[0], command_config_adxl345);
uint8_t msg[2] = { AR_FIFO_STATUS | AM_READ, 0x00 };
+
uint32_t time1 = timer_read_time();
spidev_transfer(ax->spi, 1, sizeof(msg), msg);
uint32_t time2 = timer_read_time();
- adxl_status(ax, args[0], time1, time2, msg[1]);
+
+ uint_fast8_t fifo_status = msg[1] & ~0x80; // Ignore trigger bit
+ if (fifo_status > 32)
+ // Query error - don't send response - host will retry
+ return;
+ sensor_bulk_status(&ax->sb, args[0], time1, time2-time1
+ , fifo_status * BYTES_PER_SAMPLE);
}
DECL_COMMAND(command_query_adxl345_status, "query_adxl345_status oid=%c");
diff --git a/src/sensor_angle.c b/src/sensor_angle.c
index 865670b8..54caecc2 100644
--- a/src/sensor_angle.c
+++ b/src/sensor_angle.c
@@ -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);
}
diff --git a/src/sensor_bulk.c b/src/sensor_bulk.c
new file mode 100644
index 00000000..9b5c782c
--- /dev/null
+++ b/src/sensor_bulk.c
@@ -0,0 +1,38 @@
+// Helper code for collecting and sending bulk sensor measurements
+//
+// Copyright (C) 2020-2023 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "command.h" // sendf
+#include "sensor_bulk.h" // sensor_bulk_report
+
+// Reset counters
+void
+sensor_bulk_reset(struct sensor_bulk *sb)
+{
+ sb->sequence = 0;
+ sb->possible_overflows = 0;
+ sb->data_count = 0;
+}
+
+// Report local measurement buffer
+void
+sensor_bulk_report(struct sensor_bulk *sb, uint8_t oid)
+{
+ sendf("sensor_bulk_data oid=%c sequence=%hu data=%*s"
+ , oid, sb->sequence, sb->data_count, sb->data);
+ sb->data_count = 0;
+ sb->sequence++;
+}
+
+// Report buffer and fifo status
+void
+sensor_bulk_status(struct sensor_bulk *sb, uint8_t oid
+ , uint32_t time1, uint32_t query_ticks, uint32_t fifo)
+{
+ sendf("sensor_bulk_status oid=%c clock=%u query_ticks=%u next_sequence=%hu"
+ " buffered=%u possible_overflows=%hu"
+ , oid, time1, query_ticks, sb->sequence
+ , sb->data_count + fifo, sb->possible_overflows);
+}
diff --git a/src/sensor_bulk.h b/src/sensor_bulk.h
new file mode 100644
index 00000000..9c130bea
--- /dev/null
+++ b/src/sensor_bulk.h
@@ -0,0 +1,15 @@
+#ifndef __SENSOR_BULK_H
+#define __SENSOR_BULK_H
+
+struct sensor_bulk {
+ uint16_t sequence, possible_overflows;
+ uint8_t data_count;
+ uint8_t data[52];
+};
+
+void sensor_bulk_reset(struct sensor_bulk *sb);
+void sensor_bulk_report(struct sensor_bulk *sb, uint8_t oid);
+void sensor_bulk_status(struct sensor_bulk *sb, uint8_t oid
+ , uint32_t time1, uint32_t query_ticks, uint32_t fifo);
+
+#endif // sensor_bulk.h
diff --git a/src/sensor_lis2dw.c b/src/sensor_lis2dw.c
index 06dd3206..579ee1f7 100644
--- a/src/sensor_lis2dw.c
+++ b/src/sensor_lis2dw.c
@@ -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");
diff --git a/src/sensor_mpu9250.c b/src/sensor_mpu9250.c
index 7792b4d8..d52de811 100644
--- a/src/sensor_mpu9250.c
+++ b/src/sensor_mpu9250.c
@@ -12,6 +12,7 @@
#include "basecmd.h" // oid_alloc
#include "command.h" // DECL_COMMAND
#include "sched.h" // DECL_TASK
+#include "sensor_bulk.h" // sensor_bulk_report
#include "board/gpio.h" // i2c_read
#include "i2ccmds.h" // i2cdev_oid_lookup
@@ -46,11 +47,9 @@ struct mpu9250 {
struct timer timer;
uint32_t rest_ticks;
struct i2cdev_s *i2c;
- uint16_t sequence, limit_count, fifo_max, fifo_pkts_bytes;
- uint8_t flags, data_count;
- // msg size must be <= 255 due to Klipper api
- // = SAMPLES_PER_BLOCK (from mpu9250.py) * BYTES_PER_FIFO_ENTRY + 1
- uint8_t data[BYTES_PER_BLOCK];
+ uint16_t fifo_max, fifo_pkts_bytes;
+ uint8_t flags;
+ struct sensor_bulk sb;
};
enum {
@@ -92,27 +91,6 @@ command_config_mpu9250(uint32_t *args)
}
DECL_COMMAND(command_config_mpu9250, "config_mpu9250 oid=%c i2c_oid=%c");
-// Report local measurement buffer
-static void
-mp9250_report(struct mpu9250 *mp, uint8_t oid)
-{
- sendf("mpu9250_data oid=%c sequence=%hu data=%*s"
- , oid, mp->sequence, mp->data_count, mp->data);
- mp->data_count = 0;
- mp->sequence++;
-}
-
-// Report buffer and fifo status
-static void
-mp9250_status(struct mpu9250 *mp, uint_fast8_t oid
- , uint32_t time1, uint32_t time2, uint16_t fifo)
-{
- sendf("mpu9250_status oid=%c clock=%u query_ticks=%u next_sequence=%hu"
- " buffered=%c fifo=%u limit_count=%hu"
- , oid, time1, time2-time1, mp->sequence
- , mp->data_count, fifo, mp->limit_count);
-}
-
// Helper code to reschedule the mpu9250_event() timer
static void
mp9250_reschedule_timer(struct mpu9250 *mp)
@@ -135,10 +113,10 @@ mp9250_query(struct mpu9250 *mp, uint8_t oid)
if (mp->fifo_pkts_bytes >= BYTES_PER_BLOCK) {
uint8_t reg = AR_FIFO;
i2c_read(mp->i2c->i2c_config, sizeof(reg), &reg
- , BYTES_PER_BLOCK, &mp->data[0]);
- mp->data_count = BYTES_PER_BLOCK;
+ , BYTES_PER_BLOCK, &mp->sb.data[0]);
+ mp->sb.data_count = BYTES_PER_BLOCK;
mp->fifo_pkts_bytes -= BYTES_PER_BLOCK;
- mp9250_report(mp, oid);
+ sensor_bulk_report(&mp->sb, oid);
}
// If we have enough bytes remaining to fill another report wake again
@@ -214,9 +192,7 @@ command_query_mpu9250(uint32_t *args)
mp->timer.waketime = args[1];
mp->rest_ticks = args[2];
mp->flags = AX_HAVE_START;
- mp->sequence = 0;
- mp->limit_count = 0;
- mp->data_count = 0;
+ sensor_bulk_reset(&mp->sb);
mp->fifo_max = 0;
mp->fifo_pkts_bytes = 0;
sched_add_timer(&mp->timer);
@@ -235,7 +211,7 @@ command_query_mpu9250_status(uint32_t *args)
i2c_read(mp->i2c->i2c_config, sizeof(int_reg), int_reg, sizeof(int_msg),
&int_msg);
if (int_msg & FIFO_OVERFLOW_INT)
- mp->limit_count++;
+ mp->sb.possible_overflows++;
// Read latest FIFO count (with precise timing)
uint8_t reg[] = {AR_FIFO_COUNT_H};
@@ -246,7 +222,7 @@ command_query_mpu9250_status(uint32_t *args)
uint16_t fifo_bytes = ((msg[0] & 0x1f) << 8) | msg[1];
// Report status
- mp9250_status(mp, args[0], time1, time2, fifo_bytes / BYTES_PER_FIFO_ENTRY);
+ sensor_bulk_status(&mp->sb, args[0], time1, time2-time1, fifo_bytes);
}
DECL_COMMAND(command_query_mpu9250_status, "query_mpu9250_status oid=%c");