blackmagic-esp32-s2/main/i2c.c

66 lines
2.0 KiB
C

#include <driver/i2c.h>
static gpio_num_t i2c_gpio_sda = 18;
static gpio_num_t i2c_gpio_scl = 17;
static gpio_num_t i2c_pull_enable = 3;
static i2c_port_t i2c_port = I2C_NUM_0;
static uint32_t i2c_frequency = 100000;
// I2C master doesn't need buffers
#define I2C_MASTER_TX_BUF_DISABLE 0
#define I2C_MASTER_RX_BUF_DISABLE 0
#define WRITE_BIT I2C_MASTER_WRITE
#define ACK_CHECK_EN 0x1
void i2c_init(void) {
i2c_driver_install(
i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = i2c_gpio_sda,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = i2c_gpio_scl,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = i2c_frequency,
};
ESP_ERROR_CHECK(i2c_param_config(i2c_port, &conf));
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << i2c_pull_enable);
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
ESP_ERROR_CHECK(gpio_config(&io_conf));
// enable pullups
gpio_set_level(i2c_pull_enable, 0);
}
void i2c_scan(void) {
uint8_t address;
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
for(int i = 0; i < 128; i += 16) {
printf("%02x: ", i);
for(int j = 0; j < 16; j++) {
fflush(stdout);
address = i + j;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if(ret == ESP_OK) {
printf("%02x ", address);
} else if(ret == ESP_ERR_TIMEOUT) {
printf("UU ");
} else {
printf("-- ");
}
}
printf("\r\n");
}
}