forked from xiaozhi/xiaozhi-esp32
adjust board structure
This commit is contained in:
31
main/boards/common/i2c_device.cc
Normal file
31
main/boards/common/i2c_device.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "i2c_device.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "I2cDevice"
|
||||
|
||||
|
||||
I2cDevice::I2cDevice(i2c_master_bus_handle_t i2c_bus, uint8_t addr) {
|
||||
i2c_device_config_t i2c_device_cfg = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = addr,
|
||||
.scl_speed_hz = 100000,
|
||||
.scl_wait_us = 0,
|
||||
.flags = {
|
||||
.disable_ack_check = 0,
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus, &i2c_device_cfg, &i2c_device_));
|
||||
assert(i2c_device_ != NULL);
|
||||
}
|
||||
|
||||
void I2cDevice::WriteReg(uint8_t reg, uint8_t value) {
|
||||
uint8_t buffer[2] = {reg, value};
|
||||
ESP_ERROR_CHECK(i2c_master_transmit(i2c_device_, buffer, 2, 100));
|
||||
}
|
||||
|
||||
uint8_t I2cDevice::ReadReg(uint8_t reg) {
|
||||
uint8_t buffer[1];
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(i2c_device_, ®, 1, buffer, 1, 100));
|
||||
return buffer[0];
|
||||
}
|
||||
Reference in New Issue
Block a user