Auto slave address for instances

This commit is contained in:
whowechina 2024-10-13 20:23:31 +08:00
parent 0205c3f71c
commit e370fcc4a2
6 changed files with 34 additions and 10 deletions

Binary file not shown.

View File

@ -64,8 +64,8 @@ void airkey_init()
gpio_pull_up(scl);
gpio_pull_up(sda);
vl53l0x_init(i, tof_ports[i], 0);
vl53l1x_init(i, tof_ports[i], 0);
vl53l0x_init(i, tof_ports[i]);
vl53l1x_init(i, tof_ports[i]);
vl53l0x_use(i);
vl53l1x_use(i);

View File

@ -148,6 +148,7 @@ static int current_instance = 0;
#define INSTANCE_NUM (sizeof(instances) / sizeof(instances[0]))
#define I2C_PORT instances[current_instance].port
#define I2C_ADDR instances[current_instance].addr
#define INSTANCE instances[current_instance]
static void write_reg(uint8_t reg, uint8_t value)
{
@ -216,14 +217,24 @@ const uint16_t reg_tuning[] = { 80,
0xff00, 0x8001, 0x01f8, 0xff01, 0x8e01, 0x0001, 0xff00, 0x8000,
};
void vl53l0x_init(unsigned instance, i2c_inst_t *i2c_port, uint8_t i2c_addr)
void vl53l0x_init(unsigned instance, i2c_inst_t *i2c_port)
{
if (instance < INSTANCE_NUM) {
instances[instance].port = i2c_port;
instances[instance].addr = i2c_addr ? i2c_addr : VL53L0X_DEF_ADDR;
current_instance = instance;
INSTANCE.port = i2c_port;
INSTANCE.addr = VL53L0X_DEF_ADDR;
vl53l0x_change_addr(VL53L0X_DEF_ADDR + 1 + instance);
}
}
bool vl53l0x_change_addr(uint8_t i2c_addr)
{
write_reg(I2C_SLAVE_DEVICE_ADDRESS, i2c_addr);
INSTANCE.addr = i2c_addr;
read_reg(I2C_SLAVE_DEVICE_ADDRESS); // Dummy read
return read_reg(I2C_SLAVE_DEVICE_ADDRESS) == i2c_addr;
}
void vl53l0x_use(unsigned instance)
{
if (instance < INSTANCE_NUM) {

View File

@ -13,7 +13,9 @@
#include "hardware/i2c.h"
void vl53l0x_init(unsigned instance, i2c_inst_t *i2c_port, uint8_t i2c_addr);
void vl53l0x_init(unsigned instance, i2c_inst_t *i2c_port);
bool vl53l0x_change_addr(uint8_t i2c_addr);
void vl53l0x_use(unsigned instance);
bool vl53l0x_is_present();
bool vl53l0x_init_tof();

View File

@ -1387,14 +1387,23 @@ static void read_many(uint16_t reg, uint8_t *dst, uint8_t len)
false, time_us_64() + IO_TIMEOUT_US * len);
}
void vl53l1x_init(unsigned instance, i2c_inst_t *i2c_port, uint8_t i2c_addr)
void vl53l1x_init(unsigned instance, i2c_inst_t *i2c_port)
{
if (instance < INSTANCE_NUM) {
instances[instance].port = i2c_port;
instances[instance].addr = i2c_addr ? i2c_addr : VL53L1X_DEF_ADDR;
current_instance = instance;
INSTANCE.port = i2c_port;
INSTANCE.addr = VL53L1X_DEF_ADDR;
}
}
bool vl53l1x_change_addr(uint8_t i2c_addr)
{
write_reg(I2C_SLAVE__DEVICE_ADDRESS, i2c_addr);
INSTANCE.addr = i2c_addr;
read_reg(I2C_SLAVE__DEVICE_ADDRESS); // dummy read
return read_reg(I2C_SLAVE__DEVICE_ADDRESS) == I2C_ADDR;
}
void vl53l1x_use(unsigned instance)
{
if (instance < INSTANCE_NUM) {
@ -1421,6 +1430,7 @@ bool vl53l1x_init_tof()
// call below and the Arduino 101 doesn't seem to handle that well
sleep_us(1000);
vl53l1x_change_addr(VL53L1X_DEF_ADDR + 1 + current_instance);
// VL53L1_poll_for_boot_completion() begin
uint64_t start = time_us_64();

View File

@ -12,7 +12,8 @@
#include <stdbool.h>
#include "hardware/i2c.h"
void vl53l1x_init(unsigned instance, i2c_inst_t *i2c_port, uint8_t i2c_addr);
void vl53l1x_init(unsigned instance, i2c_inst_t *i2c_port);
bool vl53l1x_change_addr(uint8_t i2c_addr);
void vl53l1x_use(unsigned instance);
bool vl53l1x_is_present();
bool vl53l1x_init_tof();