sensor_adxl345: No need to schedule start of bulk reading

It's simpler and faster to enable the adxl345 in the python code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2023-12-27 13:14:53 -05:00
parent 2dc4cfc5df
commit 6f0e91f69f
2 changed files with 20 additions and 55 deletions

View File

@ -184,8 +184,6 @@ def read_axes_map(config):
raise config.error("Invalid axes_map parameter")
return [am[a.strip()] for a in axes_map]
MIN_MSG_TIME = 0.100
BYTES_PER_SAMPLE = 5
SAMPLES_PER_BLOCK = bulk_sensor.MAX_BULK_MSG_SIZE // BYTES_PER_SAMPLE
@ -207,7 +205,7 @@ class ADXL345:
self.query_adxl345_cmd = None
mcu.add_config_cmd("config_adxl345 oid=%d spi_oid=%d"
% (oid, self.spi.get_oid()))
mcu.add_config_cmd("query_adxl345 oid=%d clock=0 rest_ticks=0"
mcu.add_config_cmd("query_adxl345 oid=%d rest_ticks=0"
% (oid,), on_restart=True)
mcu.register_config_callback(self._build_config)
self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, oid=oid)
@ -228,7 +226,7 @@ class ADXL345:
def _build_config(self):
cmdqueue = self.spi.get_command_queue()
self.query_adxl345_cmd = self.mcu.lookup_command(
"query_adxl345 oid=%c clock=%u rest_ticks=%u", cq=cmdqueue)
"query_adxl345 oid=%c rest_ticks=%u", cq=cmdqueue)
self.clock_updater.setup_query_command(
self.mcu, "query_adxl345_status oid=%c", oid=self.oid, cq=cmdqueue)
def read_reg(self, reg):
@ -302,19 +300,17 @@ class ADXL345:
self.set_reg(REG_FIFO_CTL, SET_FIFO_CTL)
# Start bulk reading
self.bulk_queue.clear_samples()
systime = self.printer.get_reactor().monotonic()
print_time = self.mcu.estimated_print_time(systime) + MIN_MSG_TIME
reqclock = self.mcu.print_time_to_clock(print_time)
rest_ticks = self.mcu.seconds_to_clock(4. / self.data_rate)
self.query_adxl345_cmd.send([self.oid, reqclock, rest_ticks],
reqclock=reqclock)
self.query_adxl345_cmd.send([self.oid, rest_ticks])
self.set_reg(REG_POWER_CTL, 0x08)
logging.info("ADXL345 starting '%s' measurements", self.name)
# Initialize clock tracking
self.clock_updater.note_start()
self.last_error_count = 0
def _finish_measurements(self):
# Halt bulk reading
self.query_adxl345_cmd.send_wait_ack([self.oid, 0, 0])
self.set_reg(REG_POWER_CTL, 0x00)
self.query_adxl345_cmd.send_wait_ack([self.oid, 0])
self.bulk_queue.clear_samples()
logging.info("ADXL345 finished '%s' measurements", self.name)
def _process_batch(self, eventtime):

View File

@ -1,6 +1,6 @@
// Support for gathering acceleration data from ADXL345 chip
//
// Copyright (C) 2020 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2020-2023 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@ -22,7 +22,7 @@ struct adxl345 {
};
enum {
AX_HAVE_START = 1<<0, AX_RUNNING = 1<<1, AX_PENDING = 1<<2,
AX_PENDING = 1<<0,
};
static struct task_wake adxl345_wake;
@ -58,7 +58,6 @@ adxl_reschedule_timer(struct adxl345 *ax)
}
// Chip registers
#define AR_POWER_CTL 0x2D
#define AR_DATAX0 0x32
#define AR_FIFO_STATUS 0x39
#define AM_READ 0x80
@ -99,59 +98,33 @@ adxl_query(struct adxl345 *ax, uint8_t oid)
// Check fifo status
if (fifo_status >= 31)
ax->sb.possible_overflows++;
if (fifo_status > 1 && fifo_status <= 32) {
if (fifo_status > 1) {
// More data in fifo - wake this task again
sched_wake_task(&adxl345_wake);
} else if (ax->flags & AX_RUNNING) {
} else {
// Sleep until next check time
sched_del_timer(&ax->timer);
ax->flags &= ~AX_PENDING;
adxl_reschedule_timer(ax);
}
}
// Startup measurements
static void
adxl_start(struct adxl345 *ax, uint8_t oid)
{
sched_del_timer(&ax->timer);
ax->flags = AX_RUNNING;
uint8_t msg[2] = { AR_POWER_CTL, 0x08 };
spidev_transfer(ax->spi, 0, sizeof(msg), msg);
adxl_reschedule_timer(ax);
}
// End measurements
static void
adxl_stop(struct adxl345 *ax, uint8_t oid)
{
// Disable measurements
sched_del_timer(&ax->timer);
ax->flags = 0;
uint8_t msg[2] = { AR_POWER_CTL, 0x00 };
spidev_transfer(ax->spi, 0, sizeof(msg), msg);
}
void
command_query_adxl345(uint32_t *args)
{
struct adxl345 *ax = oid_lookup(args[0], command_config_adxl345);
if (!args[2]) {
// End measurements
adxl_stop(ax, args[0]);
return;
}
// Start new measurements query
sched_del_timer(&ax->timer);
ax->timer.waketime = args[1];
ax->rest_ticks = args[2];
ax->flags = AX_HAVE_START;
ax->flags = 0;
if (!args[1])
// End measurements
return;
// Start new measurements query
ax->rest_ticks = args[1];
sensor_bulk_reset(&ax->sb);
sched_add_timer(&ax->timer);
adxl_reschedule_timer(ax);
}
DECL_COMMAND(command_query_adxl345,
"query_adxl345 oid=%c clock=%u rest_ticks=%u");
DECL_COMMAND(command_query_adxl345, "query_adxl345 oid=%c rest_ticks=%u");
void
command_query_adxl345_status(uint32_t *args)
@ -181,11 +154,7 @@ adxl345_task(void)
struct adxl345 *ax;
foreach_oid(oid, ax, command_config_adxl345) {
uint_fast8_t flags = ax->flags;
if (!(flags & AX_PENDING))
continue;
if (flags & AX_HAVE_START)
adxl_start(ax, oid);
else
if (flags & AX_PENDING)
adxl_query(ax, oid);
}
}