46 lines
1.1 KiB
C
46 lines
1.1 KiB
C
/**
|
|
* @file i2c.c
|
|
* @author marisa (marisa@fwmari.net)
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2024-05-06
|
|
*
|
|
* @copyright Copyright (c) 2024
|
|
*
|
|
*/
|
|
|
|
#include "i2c.h"
|
|
|
|
i2c_master_bus_handle_t m_handle = NULL;
|
|
|
|
static bool busInitialized = false;
|
|
|
|
i2c_master_bus_handle_t i2cInitMasterBus(uint8_t sda, uint8_t scl) {
|
|
i2c_master_bus_config_t i2cMasterConfig = {
|
|
.clk_source = I2C_CLK_SRC_DEFAULT,
|
|
.i2c_port = I2C_NUM_0,
|
|
.scl_io_num = scl,
|
|
.sda_io_num = sda,
|
|
.glitch_ignore_cnt = 7,
|
|
.flags.enable_internal_pullup = true,
|
|
};
|
|
|
|
ESP_ERROR_CHECK(i2c_new_master_bus(&i2cMasterConfig, &m_handle));
|
|
busInitialized = true;
|
|
return m_handle;
|
|
}
|
|
|
|
i2c_master_dev_handle_t i2cInitDevice(uint8_t addr, uint32_t speed) {
|
|
assert(busInitialized);
|
|
|
|
i2c_device_config_t deviceCfg = {
|
|
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
|
.device_address = addr,
|
|
.scl_speed_hz = speed,
|
|
};
|
|
|
|
i2c_master_dev_handle_t devHandle;
|
|
ESP_ERROR_CHECK(i2c_master_bus_add_device(m_handle, &deviceCfg, &devHandle));
|
|
|
|
return devHandle;
|
|
} |