ESP32 as a I2C slave device

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 as a I2C slave device

Postby ESP_Sprite » Wed Jan 30, 2019 3:51 am

Do you happen to have the code that implements this public somewhere?

dkaufmann
Posts: 24
Joined: Wed May 17, 2017 9:06 am

Re: ESP32 as a I2C slave device

Postby dkaufmann » Wed Jan 30, 2019 7:56 am

Hi ESP-Sprite

I changed the code a bit in order to have an easier case without CRC check and other error handling. Basically it works like this:

Code: Select all

 
 int8_t i2c_rw (uint8_t nofTx, uint8_t* nofRx)
{
	uint8_t i2c_TxBuf[nofTx+3];			// reserve memory for nofTx bytes + 1 Header Byte + 2 CRC bytes
	if (nofRx != NULL) *nofRx = 0; 			// init

	// set send buffer bytes according command
	i2c_TxBuf[0] = (0x40*nofTx)+(I2C_ADDR-0x70)*2;				
	for (uint8_t i = 1; i <= nofTx; i++)
	{
		i2c_TxBuf[i] = send_data[i-1];					// fill send buff with gloval send_data
	}
	CRCval_calc = CRC16_2(i2c_TxBuf, nofTx+1);			// calculate outgoing packet CRC
	i2c_TxBuf[nofTx+1] = (uint8_t) CRCval_calc;			// CRC low byte
	i2c_TxBuf[nofTx+2] = (uint8_t) (CRCval_calc>>8);		// CRC high byte

	i2c_slave_init();								// enable I2C slave to receive a Master Read

	//emtpy tx and rx fifo befor starting a new command
	i2c_reset_tx_fifo(I2C_SLAVE_NUM);
	i2c_reset_rx_fifo(I2C_SLAVE_NUM);

	// Master Read, Slave Write
	i2c_slave_write_buffer(I2C_SLAVE_NUM, &i2c_TxBuf[0], sizeof(i2c_TxBuf), 50 / portTICK_RATE_MS);
	
	// Master Write, Slave Read
	size_t size = i2c_slave_read_buffer(I2C_SLAVE_NUM, rec_data, 1, I2C_REC_TIMEOUT / portTICK_RATE_MS); 	
	size_t datasize = ((rec_data[0] & 0xF0) >> 4) + 2;
	size += i2c_slave_read_buffer(I2C_SLAVE_NUM, &(rec_data[1]), datasize, I2C_REC_TIMEOUT / portTICK_RATE_MS)
	i2c_driver_delete(I2C_SLAVE_NUM);					// disable I2C slave interface
	
	if (size >= 4)	// if received at least: Header, CMD, 2xCRC
	{
		ESP_LOGI(TAG,"OK");
	}
	return ESP_OK;
}

kalpanavengala
Posts: 3
Joined: Sat Jun 12, 2021 2:04 pm

Re: ESP32 as a I2C slave device

Postby kalpanavengala » Fri Jul 16, 2021 9:45 pm

Hi ESP,
There is no code in the i2c driver in the ESP forum related to ESP32 as a slave to send ACK and NACK pulses.
Can you please provide me any documentation/ link that really helps me a lot

Who is online

Users browsing this forum: Google [Bot] and 90 guests