sensor_ldc1612: Convert homing code to use trigger_analog system
Remove the homing code from sensor_ldc1612.c and utilize the generic homing support found in trigger_analog.c . Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
1 parent
147022dee2
commit
58cc059e31
4 files changed
+40
-146
No files matched your search
+1
-1
@@ -179,7 +179,7 @@ config NEED_SENSOR_BULK
|
||||
default y
|
||||
config WANT_TRIGGER_ANALOG
|
||||
bool
|
||||
depends on WANT_HX71X || WANT_ADS1220
|
||||
depends on WANT_HX71X || WANT_ADS1220 || WANT_LDC1612
|
||||
default y
|
||||
config NEED_SOS_FILTER
|
||||
bool
|
||||
|
||||
+15
-87
@@ -13,11 +13,10 @@
|
||||
#include "i2ccmds.h" // i2cdev_oid_lookup
|
||||
#include "sched.h" // DECL_TASK
|
||||
#include "sensor_bulk.h" // sensor_bulk_report
|
||||
#include "trsync.h" // trsync_do_trigger
|
||||
#include "trigger_analog.h" // trigger_analog_update
|
||||
|
||||
enum {
|
||||
LDC_PENDING = 1<<0, LDC_HAVE_INTB = 1<<1,
|
||||
LH_AWAIT_HOMING = 1<<1, LH_CAN_TRIGGER = 1<<2
|
||||
};
|
||||
|
||||
struct ldc1612 {
|
||||
@@ -27,12 +26,7 @@ struct ldc1612 {
|
||||
uint8_t flags;
|
||||
struct sensor_bulk sb;
|
||||
struct gpio_in intb_pin;
|
||||
// homing
|
||||
struct trsync *ts;
|
||||
uint8_t homing_flags;
|
||||
uint8_t trigger_reason, error_reason;
|
||||
uint32_t trigger_threshold;
|
||||
uint32_t homing_clock;
|
||||
struct trigger_analog *ta;
|
||||
};
|
||||
|
||||
static struct task_wake ldc1612_wake;
|
||||
@@ -91,83 +85,15 @@ DECL_COMMAND(command_config_ldc1612_with_intb,
|
||||
"config_ldc1612_with_intb oid=%c i2c_oid=%c intb_pin=%c");
|
||||
|
||||
void
|
||||
command_ldc1612_setup_home(uint32_t *args)
|
||||
{
|
||||
ldc1612_attach_trigger_analog(uint32_t *args) {
|
||||
struct ldc1612 *ld = oid_lookup(args[0], command_config_ldc1612);
|
||||
|
||||
ld->trigger_threshold = args[2];
|
||||
if (!ld->trigger_threshold) {
|
||||
ld->ts = NULL;
|
||||
ld->homing_flags = 0;
|
||||
return;
|
||||
}
|
||||
ld->homing_clock = args[1];
|
||||
ld->ts = trsync_oid_lookup(args[3]);
|
||||
ld->trigger_reason = args[4];
|
||||
ld->error_reason = args[5];
|
||||
ld->homing_flags = LH_AWAIT_HOMING | LH_CAN_TRIGGER;
|
||||
ld->ta = trigger_analog_oid_lookup(args[1]);
|
||||
}
|
||||
DECL_COMMAND(command_ldc1612_setup_home,
|
||||
"ldc1612_setup_home oid=%c clock=%u threshold=%u"
|
||||
" trsync_oid=%c trigger_reason=%c error_reason=%c");
|
||||
DECL_COMMAND(ldc1612_attach_trigger_analog,
|
||||
"ldc1612_attach_trigger_analog oid=%c trigger_analog_oid=%c");
|
||||
|
||||
void
|
||||
command_query_ldc1612_home_state(uint32_t *args)
|
||||
{
|
||||
struct ldc1612 *ld = oid_lookup(args[0], command_config_ldc1612);
|
||||
sendf("ldc1612_home_state oid=%c homing=%c trigger_clock=%u"
|
||||
, args[0], !!(ld->homing_flags & LH_CAN_TRIGGER), ld->homing_clock);
|
||||
}
|
||||
DECL_COMMAND(command_query_ldc1612_home_state,
|
||||
"query_ldc1612_home_state oid=%c");
|
||||
|
||||
// Cancel homing due to an error
|
||||
static void
|
||||
cancel_homing(struct ldc1612 *ld, int error_code)
|
||||
{
|
||||
if (!(ld->homing_flags & LH_CAN_TRIGGER))
|
||||
return;
|
||||
ld->homing_flags = 0;
|
||||
trsync_do_trigger(ld->ts, ld->error_reason + error_code);
|
||||
}
|
||||
|
||||
#define MAX_VALID_RAW_VALUE 0x03ffffff
|
||||
#define DATA_ERROR_AMPLITUDE (1L << 28)
|
||||
|
||||
static int
|
||||
check_data_bits(struct ldc1612 *ld, uint32_t raw_data) {
|
||||
// Ignore amplitude errors
|
||||
raw_data &= ~DATA_ERROR_AMPLITUDE;
|
||||
// Datasheet define valid frequency input as < F_ref / 4
|
||||
if (raw_data < MAX_VALID_RAW_VALUE)
|
||||
return 0;
|
||||
cancel_homing(ld, SE_SENSOR_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Check if a sample should trigger a homing event
|
||||
static void
|
||||
check_home(struct ldc1612 *ld, uint32_t raw_data)
|
||||
{
|
||||
uint8_t homing_flags = ld->homing_flags;
|
||||
if (!(homing_flags & LH_CAN_TRIGGER))
|
||||
return;
|
||||
if (check_data_bits(ld, raw_data))
|
||||
return;
|
||||
uint32_t data = raw_data & 0x0fffffff;
|
||||
uint32_t time = timer_read_time();
|
||||
if ((homing_flags & LH_AWAIT_HOMING)
|
||||
&& timer_is_before(time, ld->homing_clock))
|
||||
return;
|
||||
homing_flags &= ~LH_AWAIT_HOMING;
|
||||
if (data > ld->trigger_threshold) {
|
||||
homing_flags = 0;
|
||||
ld->homing_clock = time;
|
||||
trsync_do_trigger(ld->ts, ld->trigger_reason);
|
||||
}
|
||||
ld->homing_flags = homing_flags;
|
||||
}
|
||||
|
||||
// Chip registers
|
||||
#define REG_DATA0_MSB 0x00
|
||||
#define REG_DATA0_LSB 0x01
|
||||
@@ -196,7 +122,7 @@ read_reg_status(struct ldc1612 *ld, uint16_t *status)
|
||||
static void
|
||||
report_sample_error(struct ldc1612 *ld, int error_code)
|
||||
{
|
||||
cancel_homing(ld, error_code);
|
||||
trigger_analog_note_error(ld->ta, error_code);
|
||||
|
||||
uint8_t *d = &ld->sb.data[ld->sb.data_count];
|
||||
d[0] = 0xff;
|
||||
@@ -238,12 +164,14 @@ ldc1612_query(struct ldc1612 *ld, uint8_t oid)
|
||||
goto out;
|
||||
}
|
||||
|
||||
// Check for endstop trigger
|
||||
uint32_t data = ((uint32_t)d[0] << 24)
|
||||
| ((uint32_t)d[1] << 16)
|
||||
| ((uint32_t)d[2] << 8)
|
||||
| ((uint32_t)d[3]);
|
||||
check_home(ld, data);
|
||||
// Check for homing trigger
|
||||
uint32_t raw_data = (((uint32_t)d[0] << 24) | ((uint32_t)d[1] << 16)
|
||||
| ((uint32_t)d[2] << 8) | ((uint32_t)d[3]));
|
||||
if (raw_data & 0xe0000000)
|
||||
trigger_analog_note_error(ld->ta, SE_SENSOR_ERROR);
|
||||
else
|
||||
trigger_analog_update(ld->ta, raw_data & 0x0fffffff);
|
||||
|
||||
out:
|
||||
ld->sb.data_count += BYTES_PER_SAMPLE;
|
||||
// Flush local buffer if needed
|
||||
|
||||
Reference in new issue
Block a user