Hopefully this is the right area of the forum to post this.
I have some i2c code that uses pin 22 and 21, when I connect a scope to the pins in question I see no data coming from the ESP32, this is my code:
Code: Select all
#include <driver/i2c.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>
#include "driver/gpio.h"
#include "sdkconfig.h"
#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
#define READ_BIT I2C_MASTER_READ /*!< I2C master read */
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */
void i2c_master_init(){
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = 22;
conf.scl_io_num = 21;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 100000;
i2c_param_config(I2C_NUM_0, &conf);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
}
static esp_err_t i2c_master_device_read(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* data_l){
int ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, 0x23 << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0xFF, ACK_CHECK_EN);
i2c_master_read_byte(cmd, data_h, ACK_VAL);
i2c_master_read_byte(cmd, data_l, NACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
void task_i2c(void *ignore) {
esp_err_t ret;
uint8_t device_l = 0xFF;
uint8_t device_h = 0xFF;
while(1){
ret = i2c_master_device_read(I2C_NUM_0, &device_h, &device_l);
if(ret == ESP_ERR_TIMEOUT){
printf("I2C Timeout\n");
} else if(ret != ESP_OK){
printf("I2C Error\n");
} else {
printf("I2C Device ID\n");
printf("Device Low: %02x\n", device_l);
printf("Device High: %02x\n", device_h);
}
vTaskDelay(1000 / portTICK_RATE_MS);
}
vTaskDelete(NULL);
}
void app_main(){
i2c_master_init();
xTaskCreate(task_i2c, "i2c__task_0", 1024 * 2, (void* ) 0, 10, NULL);
}
Thanks
Docmur