I2C slave don't work

maurizio.scian
Posts: 14
Joined: Mon Jun 26, 2023 8:09 am

I2C slave don't work

Postby maurizio.scian » Wed Feb 14, 2024 6:30 pm

I have 2 ESP32S3, and they need communicate by I2C (all my uart are busy).
Master sends a stream (i can see it and I2C decode by oscilloscope) but the slave seems doesn't receive nothing. For double check, i tried inputs and also i tried to start a I2C master communication on the slave one, only for check the hardware, and all seems work and the pins are correctly configured. The micros are near (the distance is about 10cm). I don't use external pullups, but i enable pullups in both the microcontrollers. I also tried to place an external pullup but nothing seems change. Another thing, the device was able to communicate until a pair of days ago. In last, the slave address is the same on both the devices, it's a 7 bit address as I2C need, and i use i2c_master_write_read_device method on the master and i2c_slave_read_buffer and i2c_slave_write_buffer routines on the slave.
About the software, i took ispiration from here: https://esp32tutorials.com/esp32-i2c-co ... l-esp-idf/.
From the oscilloscope seems that the slave doesn't send acknowledge, but in reality the slave doesn't receive anything.
The slave initialization is this:

Code: Select all

memset(&I2CCfg, 0, sizeof(i2c_config_t));

I2CCfg.sda_io_num = I2CSLAVE_SDA;
I2CCfg.sda_pullup_en = GPIO_PULLUP_ENABLE;
I2CCfg.scl_io_num = I2CSLAVE_SCL;
I2CCfg.scl_pullup_en = GPIO_PULLUP_ENABLE;
I2CCfg.mode = I2C_MODE_SLAVE;
I2CCfg.slave.addr_10bit_en = 0;
I2CCfg.slave.slave_addr = OIDA_SLAVE_ADDRESS;
I2CCfg.clk_flags = 0;

esp_err_t res = i2c_param_config(channelI2C, &I2CCfg);
if (res != ESP_OK) {
	return res;
}

res = i2c_driver_install(channelI2C, I2C_MODE_SLAVE, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
if (res != ESP_OK) {
	ESP_LOGE(TAG, "Error i2c_driver_install");
}
And the application loop is the following code (where BufTx and BufRx are both uint8_t array of 255 elements). The master should write 12 bytes on the slave (but stops after the device addres byte) and the slave should send 90 bytes to the master. I implemented a crc, but the problem is that rxLen is ever 0.

Code: Select all

		LoadBufTx(BufTx);	// here i load my tx buffer

		size_t rxLen = i2c_slave_read_buffer(channelI2C, BufRx, I2C_SLAVE_RX_BUF_LEN, pdMS_TO_TICKS(1000));
		i2c_reset_rx_fifo(channelI2C);
		
		if (rxLen == 12) {
			uint16_t respCRC = crc16(0xFFFF, BufRx, 10);
			uint16_t rxCRC = BufRx[10] | (BufRx[11] << 8);
			if (respCRC != rxCRC) {
				ESP_LOGE(TAG, "CRC Error: %04X - %04X", respCRC, rxCRC);
			}
			else {
				ESP_LOGI(TAG, "CRC OK: %04X - %04X", respCRC, rxCRC);
				GetFromBufRx(BufRx);	// here i want load my rx buffer
				i2c_slave_write_buffer(channelI2C, BufTx, 90, pdMS_TO_TICKS(2000));
			}	
		}
		else {
			ESP_LOGI(TAG, "Char in buf : %d -> RESET", rxLen);			
		}
Really, the code is simply but it doesnt't work at all. I'm using IDF 5.0.
Thanks for all that will help me!

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2C slave don't work

Postby MicroController » Thu Feb 15, 2024 10:34 am

I don't use external pullups, but i enable pullups in both the microcontrollers.
That'd be about 50k/2=25kOhms, which is definitely larger than the 2-10k normally used.
Another thing, the device was able to communicate until a pair of days ago
which naturally leads to the question "what was changed?"

maurizio.scian
Posts: 14
Joined: Mon Jun 26, 2023 8:09 am

Re: I2C slave don't work

Postby maurizio.scian » Thu Feb 15, 2024 11:30 am

MicroController wrote:
Thu Feb 15, 2024 10:34 am
That'd be about 50k/2=25kOhms, which is definitely larger than the 2-10k normally used.
Thanks Microcontroller.
The devices communicated for a while, and for that i was thinking that the internal pullups was enough and the problem was the firmware implementation. Moreover, i was able to see the master's signal and decode it in my oscilloscope, so i was thinking that the problem was the slave implementation. I was wrong. As you said, we need stronger pullups for see something on the slave. I added externally 10K pullup and seems works well. Thank you very match, you helped me more than you can think!

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2C slave don't work

Postby MicroController » Thu Feb 15, 2024 7:27 pm

Glad to hear it was such an easy fix :)

Who is online

Users browsing this forum: Bing [Bot] and 351 guests