VL53L1x working

This commit is contained in:
whowechina 2024-09-22 23:29:53 +08:00
parent 320479628b
commit c0005a20dc
2 changed files with 28 additions and 24 deletions

View File

@ -25,6 +25,7 @@ static i2c_inst_t *tof_ports[] = TOF_PORT_DEF;
#define TOF_NUM (count_of(tof_ports))
enum { TOF_VL53L0X = 1, TOF_VL53L1X = 2 };
uint8_t tof_models[TOF_NUM] = { 0 };
bool tof_init_ok[TOF_NUM] = { 0 };
static struct {
uint8_t port_id;
@ -70,11 +71,15 @@ void airkey_init()
if (vl53l0x_is_present()) {
tof_models[i] = TOF_VL53L0X;
vl53l0x_init_tof();
tof_init_ok[i] = vl53l0x_init_tof();
vl53l0x_start_continuous();
} else if (vl53l1x_is_present()) {
tof_models[i] = TOF_VL53L1X;
vl53l1x_init_tof();
tof_init_ok[i] = vl53l1x_init_tof();
vl53l1x_setDistanceMode(Short);
vl53l1x_setMeasurementTimingBudget(20000);
vl53l1x_startContinuous(20);
} else {
tof_models[i] = 0;
@ -85,17 +90,25 @@ void airkey_init()
static uint16_t tof_dist[count_of(tof_ports)];
static bool readings[AIRKEY_NUM];
static void print_tof(const char *name, uint16_t mm)
{
//printf("\t%s: %3d", name, mm > 1000 ? 0 : mm);
}
static void tof_read()
{
for (int i = 0; i < TOF_NUM; i++) {
if (tof_models[i] == TOF_VL53L0X) {
vl53l0x_use(i);
tof_dist[i] = readRangeContinuousMillimeters();
print_tof("L0x", tof_dist[i]);
} else if (tof_models[i] == TOF_VL53L1X) {
vl53l1x_use(i);
tof_dist[i] = vl53l1x_readContinuousMillimeters();
print_tof("L1x", tof_dist[i]);
}
}
printf("\n");
}
#define BETWEEN(x, a, b) (((x) >= (a)) && ((x) <= (b)))

View File

@ -1291,10 +1291,6 @@ static struct {
i2c_inst_t *port;
uint8_t addr;
uint16_t io_timeout;
bool did_timeout;
uint16_t timeout_start_ms;
uint16_t fast_osc_frequency;
uint16_t osc_calibrate_val;
@ -1415,9 +1411,6 @@ bool vl53l1x_is_present()
// VL53L1_StaticInit().
bool vl53l1x_init_tof()
{
// check model ID and module type registers (values specified in datasheet)
if (read_reg16(IDENTIFICATION__MODEL_ID) != 0xEACC) { return false; }
// VL53L1_software_reset() begin
write_reg(SOFT_RESET, 0x00);
@ -1445,7 +1438,7 @@ bool vl53l1x_init_tof()
// VL53L1_DataInit() begin
// sensor uses 1V8 mode for I/O by default; switch to 2V8 mode
write_reg(PAD_I2C_HV__EXTSUP_CONFIG, read_reg(PAD_I2C_HV__EXTSUP_CONFIG) | 0x01);
write_reg(PAD_I2C_HV__EXTSUP_CONFIG, read_reg(PAD_I2C_HV__EXTSUP_CONFIG) | 0x01);
// store oscillator info for later use
INSTANCE.fast_osc_frequency = read_reg16(OSC_MEASURED__FAST_OSC__FREQUENCY);
@ -1816,6 +1809,11 @@ void vl53l1x_stopContinuous()
// VL53L1_low_power_auto_data_stop_range() end
}
static bool dataReady()
{
return (read_reg(GPIO__TIO_HV_STATUS) & 0x01) == 0;
}
// Returns a range reading in millimeters when continuous mode is active. If
// blocking is true (the default), this function waits for a new measurement to
// be available. If blocking is false, it will try to return data immediately.
@ -1823,20 +1821,12 @@ void vl53l1x_stopContinuous()
// measurement)
uint16_t vl53l1x_readContinuousMillimeters()
{
/*
if (blocking)
{
startTimeout();
while (!dataReady())
{
if (checkTimeoutExpired())
{
did_timeout = true;
return 0;
}
}
static uint16_t cached = 0;
while (!dataReady()) {
return cached;
}
*/
readResults();
if (!INSTANCE.calibrated) {
@ -1848,7 +1838,8 @@ uint16_t vl53l1x_readContinuousMillimeters()
getRangingData();
write_reg(SYSTEM__INTERRUPT_CLEAR, 0x01); // sys_interrupt_clear_range
return INSTANCE.result.range_mm;
cached = INSTANCE.result.range_mm;
return cached;
}
// "Setup ranges after the first one in low power auto mode by turning off