mirror of
https://github.com/whowechina/chu_pico.git
synced 2025-01-31 12:03:43 +01:00
VL53L0x update but still crosstalks and shortsight
This commit is contained in:
parent
273d05d625
commit
a98ba5b6ae
@ -46,7 +46,7 @@ void air_init()
|
||||
}
|
||||
if (tof_model[i] == 1) {
|
||||
vl53l0x_init_tof(true);
|
||||
vl53l0x_start_continuous(0);
|
||||
vl53l0x_start_continuous();
|
||||
} else if (tof_model[i] == 2) {
|
||||
gp2y03_init(I2C_PORT);
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "hardware/i2c.h"
|
||||
@ -15,7 +16,8 @@
|
||||
|
||||
#define VL53L0X_DEF_ADDR 0x29
|
||||
|
||||
#define IO_TIMEOUT_US 25000
|
||||
#define IO_TIMEOUT_US 1000
|
||||
#define TOF_WAIT_US 200000
|
||||
|
||||
// Decode VCSEL (vertical cavity surface emitting laser) pulse period in PCLKs
|
||||
#define decodeVcselPeriod(reg_val) (((reg_val) + 1) << 1)
|
||||
@ -30,219 +32,6 @@
|
||||
static i2c_inst_t *port;
|
||||
static uint8_t addr = VL53L0X_DEF_ADDR;
|
||||
|
||||
static uint8_t stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API
|
||||
static uint32_t measurement_timing_budget_us;
|
||||
|
||||
void vl53l0x_init(i2c_inst_t *i2c_port, uint8_t i2c_addr)
|
||||
{
|
||||
port = i2c_port;
|
||||
if (i2c_addr != 0) {
|
||||
addr = i2c_addr;
|
||||
}
|
||||
}
|
||||
|
||||
bool vl53l0x_is_present()
|
||||
{
|
||||
return read_reg(IDENTIFICATION_MODEL_ID) == 0xEE;
|
||||
}
|
||||
|
||||
bool vl53l0x_init_tof(bool io_2v8)
|
||||
{
|
||||
// check model ID register (value specified in datasheet)
|
||||
if (!vl53l0x_is_present()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// sensor uses 1V8 mode for I/O by default; switch to 2V8 mode if necessary
|
||||
if (io_2v8) {
|
||||
write_reg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
|
||||
read_reg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01); // set bit 0
|
||||
}
|
||||
|
||||
// "Set I2C standard mode"
|
||||
write_reg(0x88, 0x00);
|
||||
|
||||
write_reg(0x80, 0x01);
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x00, 0x00);
|
||||
stop_variable = read_reg(0x91);
|
||||
write_reg(0x00, 0x01);
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x00);
|
||||
|
||||
// disable SIGNAL_RATE_MSRC (bit 1) and SIGNAL_RATE_PRE_RANGE (bit 4) limit checks
|
||||
write_reg(MSRC_CONFIG_CONTROL, read_reg(MSRC_CONFIG_CONTROL) | 0x12);
|
||||
|
||||
// set final range signal rate limit to 0.25 MCPS (million counts per second)
|
||||
setSignalRateLimit(0.25);
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0xFF);
|
||||
|
||||
uint8_t spad_count;
|
||||
bool spad_type_is_aperture;
|
||||
if (!getSpadInfo(&spad_count, &spad_type_is_aperture)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The SPAD map (RefGoodSpadMap) is read by VL53L0X_get_info_from_device() in
|
||||
// the API, but the same data seems to be more easily readable from
|
||||
// GLOBAL_CONFIG_SPAD_ENABLES_REF_0 through _6, so read it from there
|
||||
uint8_t ref_spad_map[6];
|
||||
read_many(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(DYNAMIC_SPAD_REF_EN_START_OFFSET, 0x00);
|
||||
write_reg(DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD, 0x2C);
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(GLOBAL_CONFIG_REF_EN_START_SELECT, 0xB4);
|
||||
|
||||
uint8_t first_spad_to_enable = spad_type_is_aperture ? 12 : 0; // 12 is the first aperture spad
|
||||
uint8_t spads_enabled = 0;
|
||||
|
||||
for (uint8_t i = 0; i < 48; i++)
|
||||
{
|
||||
if (i < first_spad_to_enable || spads_enabled == spad_count)
|
||||
{
|
||||
// This bit is lower than the first one that should be enabled, or
|
||||
// (reference_spad_count) bits have already been enabled, so zero this bit
|
||||
ref_spad_map[i / 8] &= ~(1 << (i % 8));
|
||||
}
|
||||
else if ((ref_spad_map[i / 8] >> (i % 8)) & 0x1)
|
||||
{
|
||||
spads_enabled++;
|
||||
}
|
||||
}
|
||||
|
||||
write_many(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x00, 0x00);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x09, 0x00);
|
||||
write_reg(0x10, 0x00);
|
||||
write_reg(0x11, 0x00);
|
||||
|
||||
write_reg(0x24, 0x01);
|
||||
write_reg(0x25, 0xFF);
|
||||
write_reg(0x75, 0x00);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x4E, 0x2C);
|
||||
write_reg(0x48, 0x00);
|
||||
write_reg(0x30, 0x20);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x30, 0x09);
|
||||
write_reg(0x54, 0x00);
|
||||
write_reg(0x31, 0x04);
|
||||
write_reg(0x32, 0x03);
|
||||
write_reg(0x40, 0x83);
|
||||
write_reg(0x46, 0x25);
|
||||
write_reg(0x60, 0x00);
|
||||
write_reg(0x27, 0x00);
|
||||
write_reg(0x50, 0x06);
|
||||
write_reg(0x51, 0x00);
|
||||
write_reg(0x52, 0x96);
|
||||
write_reg(0x56, 0x08);
|
||||
write_reg(0x57, 0x30);
|
||||
write_reg(0x61, 0x00);
|
||||
write_reg(0x62, 0x00);
|
||||
write_reg(0x64, 0x00);
|
||||
write_reg(0x65, 0x00);
|
||||
write_reg(0x66, 0xA0);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x22, 0x32);
|
||||
write_reg(0x47, 0x14);
|
||||
write_reg(0x49, 0xFF);
|
||||
write_reg(0x4A, 0x00);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x7A, 0x0A);
|
||||
write_reg(0x7B, 0x00);
|
||||
write_reg(0x78, 0x21);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x23, 0x34);
|
||||
write_reg(0x42, 0x00);
|
||||
write_reg(0x44, 0xFF);
|
||||
write_reg(0x45, 0x26);
|
||||
write_reg(0x46, 0x05);
|
||||
write_reg(0x40, 0x40);
|
||||
write_reg(0x0E, 0x06);
|
||||
write_reg(0x20, 0x1A);
|
||||
write_reg(0x43, 0x40);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x34, 0x03);
|
||||
write_reg(0x35, 0x44);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x31, 0x04);
|
||||
write_reg(0x4B, 0x09);
|
||||
write_reg(0x4C, 0x05);
|
||||
write_reg(0x4D, 0x04);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x44, 0x00);
|
||||
write_reg(0x45, 0x20);
|
||||
write_reg(0x47, 0x08);
|
||||
write_reg(0x48, 0x28);
|
||||
write_reg(0x67, 0x00);
|
||||
write_reg(0x70, 0x04);
|
||||
write_reg(0x71, 0x01);
|
||||
write_reg(0x72, 0xFE);
|
||||
write_reg(0x76, 0x00);
|
||||
write_reg(0x77, 0x00);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x0D, 0x01);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x01);
|
||||
write_reg(0x01, 0xF8);
|
||||
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x8E, 0x01);
|
||||
write_reg(0x00, 0x01);
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x00);
|
||||
|
||||
write_reg(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x04);
|
||||
write_reg(GPIO_HV_MUX_ACTIVE_HIGH, read_reg(GPIO_HV_MUX_ACTIVE_HIGH) & ~0x10); // active low
|
||||
write_reg(SYSTEM_INTERRUPT_CLEAR, 0x01);
|
||||
|
||||
measurement_timing_budget_us = getMeasurementTimingBudget();
|
||||
|
||||
// "Disable MSRC and TCC by default"
|
||||
// MSRC = Minimum Signal Rate Check
|
||||
// TCC = Target CentreCheck
|
||||
// -- VL53L0X_SetSequenceStepEnable() begin
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0xE8);
|
||||
|
||||
// -- VL53L0X_SetSequenceStepEnable() end
|
||||
|
||||
// "Recalculate timing budget"
|
||||
setMeasurementTimingBudget(measurement_timing_budget_us);
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0x01);
|
||||
if (!performSingleRefCalibration(0x40)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0x02);
|
||||
if (!performSingleRefCalibration(0x00)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// "restore the previous Sequence Config"
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0xE8);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Write an 8-bit register
|
||||
void write_reg(uint8_t reg, uint8_t value)
|
||||
{
|
||||
@ -257,12 +46,12 @@ void write_reg16(uint8_t reg, uint16_t value)
|
||||
i2c_write_blocking_until(I2C_PORT, addr, data, 3, false, time_us_64() + IO_TIMEOUT_US);
|
||||
}
|
||||
|
||||
// Write a 32-bit register
|
||||
void write_reg32(uint8_t reg, uint32_t value)
|
||||
static void write_reg_list(const uint16_t *list)
|
||||
{
|
||||
uint8_t data[5] = { reg, value >> 24, (value >> 16) & 0xff,
|
||||
(value >> 8) & 0xff, value & 0xff };
|
||||
i2c_write_blocking_until(I2C_PORT, addr, data, 5, false, time_us_64() + IO_TIMEOUT_US);
|
||||
const uint16_t *regs = list + 1;
|
||||
for (int i = 0; i < *list; i++) {
|
||||
write_reg(regs[i] >> 8, regs[i] & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
// Read an 8-bit register
|
||||
@ -280,16 +69,7 @@ uint16_t read_reg16(uint8_t reg)
|
||||
uint8_t value[2];
|
||||
i2c_write_blocking_until(I2C_PORT, addr, ®, 1, true, time_us_64() + IO_TIMEOUT_US);
|
||||
i2c_read_blocking_until(I2C_PORT, addr, value, 2, false, time_us_64() + IO_TIMEOUT_US);
|
||||
return value[0] << 8 | value[1];
|
||||
}
|
||||
|
||||
// Read a 32-bit register
|
||||
uint32_t read_reg32(uint8_t reg)
|
||||
{
|
||||
uint8_t value[4];
|
||||
i2c_write_blocking_until(I2C_PORT, addr, ®, 1, true, time_us_64() + IO_TIMEOUT_US);
|
||||
i2c_read_blocking_until(I2C_PORT, addr, value, 4, false, time_us_64() + IO_TIMEOUT_US);
|
||||
return (uint32_t)((value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3]);
|
||||
return (value[0] << 8) | value[1];
|
||||
}
|
||||
|
||||
// Write an arbitrary number of bytes from the given array to the sensor,
|
||||
@ -305,23 +85,113 @@ void write_many(uint8_t reg, const uint8_t *src, uint8_t len)
|
||||
void read_many(uint8_t reg, uint8_t *dst, uint8_t len)
|
||||
{
|
||||
i2c_write_blocking_until(I2C_PORT, addr, ®, 1, true, time_us_64() + IO_TIMEOUT_US);
|
||||
i2c_read_blocking_until(I2C_PORT, addr, dst, len, false, time_us_64() + IO_TIMEOUT_US);
|
||||
i2c_read_blocking_until(I2C_PORT, addr, dst, len, false, time_us_64() + IO_TIMEOUT_US * len);
|
||||
}
|
||||
|
||||
// Set the return signal rate limit check value in units of MCPS (mega counts
|
||||
// per second). "This represents the amplitude of the signal reflected from the
|
||||
// target and detected by the device"; setting this limit presumably determines
|
||||
// the minimum measurement necessary for the sensor to report a valid reading.
|
||||
// Setting a lower limit increases the potential range of the sensor but also
|
||||
// seems to increase the likelihood of getting an inaccurate reading because of
|
||||
// unwanted reflections from objects other than the intended target.
|
||||
// Defaults to 0.25 MCPS as initialized by the ST API and this library.
|
||||
bool setSignalRateLimit(float limit_Mcps)
|
||||
|
||||
uint16_t reg_mode1[] = { 4, 0x8800, 0x8001, 0xff01, 0x0000 };
|
||||
uint16_t reg_mode2[] = { 3, 0x0001, 0xff00, 0x8000 };
|
||||
uint16_t reg_spad0[] = { 4, 0x8001, 0xff01, 0x0000, 0xff06 };
|
||||
uint16_t reg_spad1[] = { 5, 0xff07, 0x8101, 0x8001, 0x946b, 0x8300 };
|
||||
uint16_t reg_spad2[] = { 4, 0xff01, 0x0001, 0xff00, 0x8000 };
|
||||
uint16_t reg_spad[] = { 5, 0xff01, 0x4f00, 0x4e2c, 0xff00, 0xb6b4 };
|
||||
uint16_t reg_tuning[] = { 80,
|
||||
0xff01, 0x0000, 0xff00, 0x0900, 0x1000, 0x1100, 0x2401, 0x25ff,
|
||||
0x7500, 0xff01, 0x4e2c, 0x4800, 0x3020, 0xff00, 0x3009, 0x5400,
|
||||
0x3104, 0x3203, 0x4083, 0x4625, 0x6000, 0x2700, 0x5006, 0x5100,
|
||||
0x5296, 0x5608, 0x5730, 0x6100, 0x6200, 0x6400, 0x6500, 0x66a0,
|
||||
0xff01, 0x2232, 0x4714, 0x49ff, 0x4a00, 0xff00, 0x7a0a, 0x7b00,
|
||||
0x7821, 0xff01, 0x2334, 0x4200, 0x44ff, 0x4526, 0x4605, 0x4040,
|
||||
0x0e06, 0x201a, 0x4340, 0xff00, 0x3403, 0x3544, 0xff01, 0x3104,
|
||||
0x4b09, 0x4c05, 0x4d04, 0xff00, 0x4400, 0x4520, 0x4708, 0x4828,
|
||||
0x6700, 0x7004, 0x7101, 0x72fe, 0x7600, 0x7700, 0xff01, 0x0d01,
|
||||
0xff00, 0x8001, 0x01f8, 0xff01, 0x8e01, 0x0001, 0xff00, 0x8000,
|
||||
};
|
||||
|
||||
|
||||
static uint8_t stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API
|
||||
static uint32_t measurement_timing_budget_us;
|
||||
|
||||
void vl53l0x_init(i2c_inst_t *i2c_port, uint8_t i2c_addr)
|
||||
{
|
||||
if (limit_Mcps < 0 || limit_Mcps > 511.99) { return false; }
|
||||
port = i2c_port;
|
||||
if (i2c_addr != 0) {
|
||||
addr = i2c_addr;
|
||||
}
|
||||
}
|
||||
|
||||
bool vl53l0x_is_present()
|
||||
{
|
||||
return read_reg(IDENTIFICATION_MODEL_ID) == 0xEE;
|
||||
}
|
||||
|
||||
bool vl53l0x_init_tof()
|
||||
{
|
||||
write_reg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
|
||||
read_reg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01);
|
||||
|
||||
write_reg_list(reg_mode1);
|
||||
stop_variable = read_reg(0x91);
|
||||
write_reg_list(reg_mode2);
|
||||
|
||||
// disable SIGNAL_RATE_MSRC (bit 1) and SIGNAL_RATE_PRE_RANGE (bit 4) limit checks
|
||||
write_reg(MSRC_CONFIG_CONTROL, read_reg(MSRC_CONFIG_CONTROL) | 0x12);
|
||||
|
||||
// Q9.7 fixed point format (9 integer bits, 7 fractional bits)
|
||||
write_reg16(FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, limit_Mcps * (1 << 7));
|
||||
write_reg16(FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, 32);
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0xFF);
|
||||
|
||||
uint8_t spad_count;
|
||||
bool is_aperture;
|
||||
if (!getSpadInfo(&spad_count, &is_aperture)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The SPAD map (RefGoodSpadMap) is read by VL53L0X_get_info_from_device() in
|
||||
// the API, but the same data seems to be more easily readable from
|
||||
// GLOBAL_CONFIG_SPAD_ENABLES_REF_0 through _6, so read it from there
|
||||
uint8_t ref_spad_map[6];
|
||||
read_many(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);
|
||||
write_reg_list(reg_spad);
|
||||
|
||||
uint8_t first_spad = is_aperture ? 12 : 0; // 12 is the first aperture spad
|
||||
uint8_t spads_enabled = 0;
|
||||
|
||||
for (int i = 0; i < 48; i++) {
|
||||
if (i < first_spad || spads_enabled == spad_count) {
|
||||
// This bit is lower than the first one that should be enabled, or
|
||||
// (reference_spad_count) bits have already been enabled, so zero this bit
|
||||
ref_spad_map[i / 8] &= ~(1 << (i % 8));
|
||||
} else if (ref_spad_map[i / 8] & (1 << i % 8)) {
|
||||
spads_enabled++;
|
||||
}
|
||||
}
|
||||
|
||||
write_many(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);
|
||||
|
||||
write_reg_list(reg_tuning);
|
||||
|
||||
write_reg(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x04);
|
||||
write_reg(GPIO_HV_MUX_ACTIVE_HIGH, read_reg(GPIO_HV_MUX_ACTIVE_HIGH) & ~0x10); // active low
|
||||
write_reg(SYSTEM_INTERRUPT_CLEAR, 0x01);
|
||||
|
||||
measurement_timing_budget_us = getMeasurementTimingBudget();
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0xE8);
|
||||
setMeasurementTimingBudget(measurement_timing_budget_us);
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0x01);
|
||||
if (!performSingleRefCalibration(0x40)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0x02);
|
||||
if (!performSingleRefCalibration(0x00)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
write_reg(SYSTEM_SEQUENCE_CONFIG, 0xE8);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -344,10 +214,9 @@ uint16_t decodeTimeout(uint16_t reg_val)
|
||||
|
||||
// Encode sequence step timeout register value from timeout in MCLKs
|
||||
// based on VL53L0X_encode_timeout()
|
||||
uint16_t encodeTimeout(uint32_t timeout_mclks)
|
||||
static uint16_t encodeTimeout(uint16_t timeout_mclks)
|
||||
{
|
||||
// format: "(LSByte * 2^MSByte) + 1"
|
||||
|
||||
uint32_t ls_byte = 0;
|
||||
uint16_t ms_byte = 0;
|
||||
|
||||
@ -361,9 +230,8 @@ uint16_t encodeTimeout(uint32_t timeout_mclks)
|
||||
|
||||
return (ms_byte << 8) | (ls_byte & 0xFF);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Convert sequence step timeout from MCLKs to microseconds with given VCSEL period in PCLKs
|
||||
@ -396,40 +264,40 @@ bool setMeasurementTimingBudget(uint32_t budget_us)
|
||||
SequenceStepEnables enables;
|
||||
SequenceStepTimeouts timeouts;
|
||||
|
||||
uint16_t const StartOverhead = 1910;
|
||||
uint16_t const EndOverhead = 960;
|
||||
uint16_t const MsrcOverhead = 660;
|
||||
uint16_t const TccOverhead = 590;
|
||||
uint16_t const DssOverhead = 690;
|
||||
uint16_t const PreRangeOverhead = 660;
|
||||
uint16_t const StartOverhead = 1320; // different than the value in get_
|
||||
uint16_t const EndOverhead = 960;
|
||||
uint16_t const MsrcOverhead = 660;
|
||||
uint16_t const TccOverhead = 590;
|
||||
uint16_t const DssOverhead = 690;
|
||||
uint16_t const PreRangeOverhead = 660;
|
||||
uint16_t const FinalRangeOverhead = 550;
|
||||
|
||||
uint32_t const MinTimingBudget = 20000;
|
||||
|
||||
if (budget_us < MinTimingBudget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t used_budget_us = StartOverhead + EndOverhead;
|
||||
|
||||
getSequenceStepEnables(&enables);
|
||||
getSequenceStepTimeouts(&enables, &timeouts);
|
||||
|
||||
if (enables.tcc)
|
||||
{
|
||||
if (enables.tcc) {
|
||||
used_budget_us += (timeouts.msrc_dss_tcc_us + TccOverhead);
|
||||
}
|
||||
|
||||
if (enables.dss)
|
||||
{
|
||||
if (enables.dss) {
|
||||
used_budget_us += 2 * (timeouts.msrc_dss_tcc_us + DssOverhead);
|
||||
}
|
||||
else if (enables.msrc)
|
||||
{
|
||||
} else if (enables.msrc) {
|
||||
used_budget_us += (timeouts.msrc_dss_tcc_us + MsrcOverhead);
|
||||
}
|
||||
|
||||
if (enables.pre_range)
|
||||
{
|
||||
if (enables.pre_range) {
|
||||
used_budget_us += (timeouts.pre_range_us + PreRangeOverhead);
|
||||
}
|
||||
|
||||
if (enables.final_range)
|
||||
{
|
||||
if (enables.final_range) {
|
||||
used_budget_us += FinalRangeOverhead;
|
||||
|
||||
// "Note that the final range timeout is determined by the timing
|
||||
@ -458,8 +326,7 @@ bool setMeasurementTimingBudget(uint32_t budget_us)
|
||||
timeoutMicrosecondsToMclks(final_range_timeout_us,
|
||||
timeouts.final_range_vcsel_period_pclks);
|
||||
|
||||
if (enables.pre_range)
|
||||
{
|
||||
if (enables.pre_range) {
|
||||
final_range_timeout_mclks += timeouts.pre_range_mclks;
|
||||
}
|
||||
|
||||
@ -481,12 +348,12 @@ uint32_t getMeasurementTimingBudget()
|
||||
SequenceStepEnables enables;
|
||||
SequenceStepTimeouts timeouts;
|
||||
|
||||
uint16_t const StartOverhead = 1910;
|
||||
uint16_t const EndOverhead = 960;
|
||||
uint16_t const MsrcOverhead = 660;
|
||||
uint16_t const TccOverhead = 590;
|
||||
uint16_t const DssOverhead = 690;
|
||||
uint16_t const PreRangeOverhead = 660;
|
||||
uint16_t const StartOverhead = 1910;
|
||||
uint16_t const EndOverhead = 960;
|
||||
uint16_t const MsrcOverhead = 660;
|
||||
uint16_t const TccOverhead = 590;
|
||||
uint16_t const DssOverhead = 690;
|
||||
uint16_t const PreRangeOverhead = 660;
|
||||
uint16_t const FinalRangeOverhead = 550;
|
||||
|
||||
// "Start and end overhead times always present"
|
||||
@ -495,31 +362,25 @@ uint32_t getMeasurementTimingBudget()
|
||||
getSequenceStepEnables(&enables);
|
||||
getSequenceStepTimeouts(&enables, &timeouts);
|
||||
|
||||
if (enables.tcc)
|
||||
{
|
||||
if (enables.tcc) {
|
||||
budget_us += (timeouts.msrc_dss_tcc_us + TccOverhead);
|
||||
}
|
||||
|
||||
if (enables.dss)
|
||||
{
|
||||
if (enables.dss) {
|
||||
budget_us += 2 * (timeouts.msrc_dss_tcc_us + DssOverhead);
|
||||
}
|
||||
else if (enables.msrc)
|
||||
{
|
||||
} else if (enables.msrc) {
|
||||
budget_us += (timeouts.msrc_dss_tcc_us + MsrcOverhead);
|
||||
}
|
||||
|
||||
if (enables.pre_range)
|
||||
{
|
||||
if (enables.pre_range) {
|
||||
budget_us += (timeouts.pre_range_us + PreRangeOverhead);
|
||||
}
|
||||
|
||||
if (enables.final_range)
|
||||
{
|
||||
if (enables.final_range) {
|
||||
budget_us += (timeouts.final_range_us + FinalRangeOverhead);
|
||||
}
|
||||
|
||||
measurement_timing_budget_us = budget_us; // store for internal reuse
|
||||
measurement_timing_budget_us = budget_us; // cache for reuse
|
||||
return budget_us;
|
||||
}
|
||||
|
||||
@ -713,13 +574,9 @@ uint8_t getVcselPulsePeriod(vcselPeriodType type)
|
||||
else { return 255; }
|
||||
}
|
||||
|
||||
// Start continuous ranging measurements. If period_ms (optional) is 0 or not
|
||||
// given, continuous back-to-back mode is used (the sensor takes measurements as
|
||||
// often as possible); otherwise, continuous timed mode is used, with the given
|
||||
// inter-measurement period in milliseconds determining how often the sensor
|
||||
// takes a measurement.
|
||||
// Start continuous ranging measurements.
|
||||
// based on VL53L0X_StartMeasurement()
|
||||
void vl53l0x_start_continuous(uint32_t period_ms)
|
||||
void vl53l0x_start_continuous()
|
||||
{
|
||||
write_reg(0x80, 0x01);
|
||||
write_reg(0xFF, 0x01);
|
||||
@ -729,30 +586,7 @@ void vl53l0x_start_continuous(uint32_t period_ms)
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x00);
|
||||
|
||||
if (period_ms != 0)
|
||||
{
|
||||
// continuous timed mode
|
||||
|
||||
// VL53L0X_SetInterMeasurementPeriodMilliSeconds() begin
|
||||
|
||||
uint16_t osc_calibrate_val = read_reg16(OSC_CALIBRATE_VAL);
|
||||
|
||||
if (osc_calibrate_val != 0)
|
||||
{
|
||||
period_ms *= osc_calibrate_val;
|
||||
}
|
||||
|
||||
write_reg32(SYSTEM_INTERMEASUREMENT_PERIOD, period_ms);
|
||||
|
||||
// VL53L0X_SetInterMeasurementPeriodMilliSeconds() end
|
||||
|
||||
write_reg(SYSRANGE_START, 0x04); // VL53L0X_REG_SYSRANGE_MODE_TIMED
|
||||
}
|
||||
else
|
||||
{
|
||||
// continuous back-to-back mode
|
||||
write_reg(SYSRANGE_START, 0x02); // VL53L0X_REG_SYSRANGE_MODE_BACKTOBACK
|
||||
}
|
||||
write_reg(SYSRANGE_START, 0x02); // VL53L0X_REG_SYSRANGE_MODE_BACKTOBACK
|
||||
}
|
||||
|
||||
// Stop continuous measurements
|
||||
@ -792,26 +626,34 @@ uint16_t readRangeContinuousMillimeters()
|
||||
// based on VL53L0X_PerformSingleRangingMeasurement()
|
||||
uint16_t readRangeSingleMillimeters()
|
||||
{
|
||||
write_reg(0x80, 0x01);
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x00, 0x00);
|
||||
write_reg(0x91, stop_variable);
|
||||
write_reg(0x00, 0x01);
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x00);
|
||||
static uint16_t range = 65535;
|
||||
static bool reading = false;
|
||||
static uint64_t start_time = 0;
|
||||
|
||||
write_reg(SYSRANGE_START, 0x01);
|
||||
|
||||
// "Wait until start bit has been cleared"
|
||||
uint32_t start = time_us_32();
|
||||
while (read_reg(SYSRANGE_START) & 0x01)
|
||||
{
|
||||
if (time_us_32() - start > IO_TIMEOUT_US) {
|
||||
return 65535;
|
||||
}
|
||||
uint64_t now = time_us_64();
|
||||
if (now - start_time > TOF_WAIT_US) {
|
||||
reading = false;
|
||||
}
|
||||
|
||||
return readRangeContinuousMillimeters();
|
||||
if (reading) {
|
||||
if ((read_reg(SYSRANGE_START) & 0x01) == 0) {
|
||||
range = readRangeContinuousMillimeters();
|
||||
reading = false;
|
||||
}
|
||||
} else {
|
||||
write_reg(0x80, 0x01);
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x00, 0x00);
|
||||
write_reg(0x91, stop_variable);
|
||||
write_reg(0x00, 0x01);
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x00);
|
||||
|
||||
write_reg(SYSRANGE_START, 0x01);
|
||||
start_time = now;
|
||||
reading = true;
|
||||
}
|
||||
return range;
|
||||
}
|
||||
|
||||
// Private Methods /////////////////////////////////////////////////////////////
|
||||
@ -819,45 +661,32 @@ uint16_t readRangeSingleMillimeters()
|
||||
// Get reference SPAD (single photon avalanche diode) count and type
|
||||
// based on VL53L0X_get_info_from_device(),
|
||||
// but only gets reference SPAD count and type
|
||||
bool getSpadInfo(uint8_t * count, bool * type_is_aperture)
|
||||
bool getSpadInfo(uint8_t *count, bool *type_is_aperture)
|
||||
{
|
||||
uint8_t tmp;
|
||||
|
||||
write_reg(0x80, 0x01);
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x00, 0x00);
|
||||
|
||||
write_reg(0xFF, 0x06);
|
||||
write_reg_list(reg_spad0);
|
||||
write_reg(0x83, read_reg(0x83) | 0x04);
|
||||
write_reg(0xFF, 0x07);
|
||||
write_reg(0x81, 0x01);
|
||||
write_reg_list(reg_spad1);
|
||||
|
||||
write_reg(0x80, 0x01);
|
||||
|
||||
write_reg(0x94, 0x6b);
|
||||
write_reg(0x83, 0x00);
|
||||
|
||||
uint32_t start = time_us_32();
|
||||
uint64_t start = time_us_64();
|
||||
while (read_reg(0x83) == 0x00) {
|
||||
if (time_us_32 - start) {
|
||||
if (time_us_64() - start > TOF_WAIT_US) {
|
||||
return false;
|
||||
}
|
||||
sleep_ms(1);
|
||||
}
|
||||
|
||||
write_reg(0x83, 0x01);
|
||||
tmp = read_reg(0x92);
|
||||
|
||||
uint8_t tmp = read_reg(0x92);
|
||||
*count = tmp & 0x7f;
|
||||
*type_is_aperture = (tmp >> 7) & 0x01;
|
||||
*type_is_aperture = (tmp & 0x80);
|
||||
|
||||
write_reg(0x81, 0x00);
|
||||
write_reg(0xFF, 0x06);
|
||||
write_reg(0x83, read_reg(0x83) & ~0x04);
|
||||
write_reg(0xFF, 0x01);
|
||||
write_reg(0x00, 0x01);
|
||||
|
||||
write_reg(0xFF, 0x00);
|
||||
write_reg(0x80, 0x00);
|
||||
write_reg(0x83, read_reg(0x83) & ~0x04);
|
||||
|
||||
write_reg_list(reg_spad2);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -913,15 +742,15 @@ bool performSingleRefCalibration(uint8_t vhv_init_byte)
|
||||
{
|
||||
write_reg(SYSRANGE_START, 0x01 | vhv_init_byte); // VL53L0X_REG_SYSRANGE_MODE_START_STOP
|
||||
|
||||
uint32_t start = time_us_32();
|
||||
uint64_t start = time_us_64();
|
||||
while ((read_reg(RESULT_INTERRUPT_STATUS) & 0x07) == 0) {
|
||||
if (time_us_32() - start > IO_TIMEOUT_US) {
|
||||
if (time_us_64() - start > TOF_WAIT_US) {
|
||||
return false;
|
||||
}
|
||||
sleep_ms(1);
|
||||
}
|
||||
|
||||
write_reg(SYSTEM_INTERRUPT_CLEAR, 0x01);
|
||||
|
||||
write_reg(SYSRANGE_START, 0x00);
|
||||
|
||||
return true;
|
||||
|
@ -105,17 +105,7 @@ typedef enum {
|
||||
|
||||
void vl53l0x_init(i2c_inst_t *i2c_port, uint8_t i2c_addr);
|
||||
bool vl53l0x_is_present();
|
||||
bool vl53l0x_init_tof(bool io_2v8);
|
||||
|
||||
void write_reg(uint8_t reg, uint8_t value);
|
||||
void write_reg16(uint8_t reg, uint16_t value);
|
||||
void write_reg32(uint8_t reg, uint32_t value);
|
||||
uint8_t read_reg(uint8_t reg);
|
||||
uint16_t read_reg16(uint8_t reg);
|
||||
uint32_t read_reg32(uint8_t reg);
|
||||
|
||||
void write_many(uint8_t reg, uint8_t const * src, uint8_t count);
|
||||
void read_many(uint8_t reg, uint8_t * dst, uint8_t count);
|
||||
bool vl53l0x_init_tof();
|
||||
|
||||
bool setSignalRateLimit(float limit_Mcps);
|
||||
float getSignalRateLimit();
|
||||
@ -126,7 +116,7 @@ uint32_t getMeasurementTimingBudget();
|
||||
bool setVcselPulsePeriod(vcselPeriodType type, uint8_t period_pclks);
|
||||
uint8_t getVcselPulsePeriod(vcselPeriodType type);
|
||||
|
||||
void vl53l0x_start_continuous(uint32_t period_ms);
|
||||
void vl53l0x_start_continuous();
|
||||
void vl53l0x_stop_continuous();
|
||||
|
||||
uint16_t readRangeContinuousMillimeters();
|
||||
|
Loading…
x
Reference in New Issue
Block a user