Skip to content

Instantly share code, notes, and snippets.

@tcbennun
Created July 24, 2023 15:15
Show Gist options
  • Save tcbennun/320ef0f3b207ff06d5355465dcc57320 to your computer and use it in GitHub Desktop.
Save tcbennun/320ef0f3b207ff06d5355465dcc57320 to your computer and use it in GitHub Desktop.
vl53l0x platform impl for ESP32
#include "vl53l0x_platform.h"
#include <driver/i2c.h>
static const i2c_port_t i2c_port = I2C_NUM_0;
static bool i2c_initialized = false;
static void i2c_init()
{
const i2c_config_t i2c_config =
{
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_27,
.scl_io_num = GPIO_NUM_14,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master =
{
.clk_speed = 400000,
},
.clk_flags = 0,
};
ESP_ERROR_CHECK(i2c_param_config(i2c_port, &i2c_config));
i2c_initialized = true;
}
static void i2c_write_reg(uint8_t addr, uint8_t index, const uint8_t *data, size_t n_data)
{
if (!i2c_initialized)
i2c_init();
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, index, true));
ESP_ERROR_CHECK(i2c_master_write(cmd, data, n_data, true));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(100)));
i2c_cmd_link_delete(cmd);
}
static void i2c_read_reg(uint8_t addr, uint8_t index, uint8_t *data, size_t n_data)
{
if (!i2c_initialized)
i2c_init();
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, index, true));
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_READ, true));
ESP_ERROR_CHECK(i2c_master_read(cmd, data, n_data, I2C_MASTER_LAST_NACK));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(100)));
i2c_cmd_link_delete(cmd);
}
extern "C"
{
/**
* Lock comms interface to serialize all commands to a shared I2C interface for a specific device
* @param Dev Device Handle
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_LockSequenceAccess(VL53L0X_DEV Dev)
{
return VL53L0X_ERROR_NONE;
}
/**
* Unlock comms interface to serialize all commands to a shared I2C interface for a specific device
* @param Dev Device Handle
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_UnlockSequenceAccess(VL53L0X_DEV Dev)
{
return VL53L0X_ERROR_NONE;
}
/**
* Writes the supplied byte buffer to the device
* @param Dev Device Handle
* @param index The register index
* @param pdata Pointer to uint8_t buffer containing the data to be written
* @param count Number of bytes in the supplied byte buffer
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count)
{
i2c_write_reg(Dev->I2cDevAddr, index, pdata, count);
return VL53L0X_ERROR_NONE;
}
/**
* Reads the requested number of bytes from the device
* @param Dev Device Handle
* @param index The register index
* @param pdata Pointer to the uint8_t buffer to store read data
* @param count Number of uint8_t's to read
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count)
{
i2c_read_reg(Dev->I2cDevAddr, index, pdata, count);
return VL53L0X_ERROR_NONE;
}
/**
* Write single byte register
* @param Dev Device Handle
* @param index The register index
* @param data 8 bit register data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data)
{
i2c_write_reg(Dev->I2cDevAddr, index, &data, 1);
return VL53L0X_ERROR_NONE;
}
/**
* Write word register
* @param Dev Device Handle
* @param index The register index
* @param data 16 bit register data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data)
{
i2c_write_reg(Dev->I2cDevAddr, index, (uint8_t*)&data, 2);
return VL53L0X_ERROR_NONE;
}
/**
* Write double word (4 byte) register
* @param Dev Device Handle
* @param index The register index
* @param data 32 bit register data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data)
{
i2c_write_reg(Dev->I2cDevAddr, index, (uint8_t*)&data, 4);
return VL53L0X_ERROR_NONE;
}
/**
* Read single byte register
* @param Dev Device Handle
* @param index The register index
* @param data pointer to 8 bit data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data)
{
i2c_read_reg(Dev->I2cDevAddr, index, data, 1);
return VL53L0X_ERROR_NONE;
}
/**
* Read word (2byte) register
* @param Dev Device Handle
* @param index The register index
* @param data pointer to 16 bit data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data)
{
i2c_read_reg(Dev->I2cDevAddr, index, (uint8_t*)data, 2);
return VL53L0X_ERROR_NONE;
}
/**
* Read dword (4byte) register
* @param Dev Device Handle
* @param index The register index
* @param data pointer to 32 bit data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data)
{
i2c_read_reg(Dev->I2cDevAddr, index, (uint8_t*)data, 4);
return VL53L0X_ERROR_NONE;
}
/**
* Threat safe Update (read/modify/write) single byte register
*
* Final_reg = (Initial_reg & and_data) |or_data
*
* @param Dev Device Handle
* @param index The register index
* @param AndData 8 bit and data
* @param OrData 8 bit or data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData)
{
uint8_t read = 0;
i2c_read_reg(Dev->I2cDevAddr, index, &read, 1);
uint8_t write = (read & AndData) | OrData;
i2c_write_reg(Dev->I2cDevAddr, index, &write, 1);
return VL53L0X_ERROR_NONE;
}
/** @} end of VL53L0X_registerAccess_group */
/**
* @brief execute delay in all polling API call
*
* A typical multi-thread or RTOs implementation is to sleep the task for some 5ms (with 100Hz max rate faster polling is not needed)
* if nothing specific is need you can define it as an empty/void macro
* @code
* #define VL53L0X_PollingDelay(...) (void)0
* @endcode
* @param Dev Device Handle
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev) /* usually best implemented as a real function */
{
vTaskDelay(pdMS_TO_TICKS(5));
return VL53L0X_ERROR_NONE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment