Updated examples of new I2C master/slave driver for IDF 5.2.x...

skiddd
Posts: 11
Joined: Sat May 28, 2022 8:58 am

Updated examples of new I2C master/slave driver for IDF 5.2.x...

Postby skiddd » Wed Mar 13, 2024 3:13 pm

Greetings esteemed colleagues,

I am currently migrating a project from IDF 5.1.3 into IDF 5.2.1 and I am having problems
using the new i2c_slave.c driver...

I already updated my source code accordingly to match the instructions in the API Reference here:
https://docs.espressif.com/projects/esp ... s/i2c.html

However, I get a early startup error + crash making my current project unusable:

Code: Select all

Waiting for the device to reconnect...
I (1441) esp_psram: SPI SRAM memory test OK
I (1450) cpu_start: Pro cpu start user code
I (1450) cpu_start: cpu freq: 240000000 Hz
I (1450) heap_init: Initializing. RAM available for dynamic allocation:
I (1451) heap_init: At 3FCA69F8 len 00042D18 (267 KiB): RAM
I (1451) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (1451) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (1451) heap_init: At 600FE014 len 00001FD4 (7 KiB): RTCRAM
I (1452) esp_psram: Adding pool of 8192K of PSRAM memory to heap allocator
I (1453) spi_flash: detected chip: gd
I (1453) spi_flash: flash io: dio
I (1453) flash_encrypt: Flash encryption mode is RELEASE
E (1454) i2c: CONFLICT! driver_ng is not allowed to be used with this old driver
I am already using
#include "driver/i2c_slave.h"

and removed
#include "driver/i2c.h"

from my source code....

I also tried to manually add
CONFIG_SOC_I2C_SUPPORT_SLAVE=y

in sdkconfig because I could not find this option in the menuconfig GUI.


I then tried to check the i2c examples inside
C:\Espressif\frameworks\esp-idf-v5.2.1\examples\peripherals\i2c\

but it turns out that they are all still using the old (already deprecated) i2c driver.

Are there working examples using the new i2c_slave drivers available?

Should I hold off updating to the new i2c drivers?


Best Regards,

skiddd
Posts: 11
Joined: Sat May 28, 2022 8:58 am

Re: Updated examples of new I2C master/slave driver for IDF 5.2.x...

Postby skiddd » Wed Mar 27, 2024 3:30 pm

Can anybody from Espressif confirm that there are in fact no examples based on the new i2c driver from IDF 5.2 / 5.2.1 ?

User avatar
eccentricOrange
Posts: 1
Joined: Mon Jun 17, 2024 11:37 am

Re: Updated examples of new I2C master/slave driver for IDF 5.2.x...

Postby eccentricOrange » Fri Jun 21, 2024 3:38 pm

I'm also struggling with the example code provided in the documentation. https://docs.espressif.com/projects/esp ... slave-read.

After filling in some of the undefined variables and things, I'm not even able to get the following to compile. Please help me out, I'm on IDF 5.2.2.
  1. #pragma once
  2.  
  3. #include "driver/i2c_slave.h"
  4. #include "esp_err.h"
  5. #include "freertos/queue.h"
  6.  
  7. #define DATA_LENGTH 8
  8.  
  9. static IRAM_ATTR bool i2c_slave_rx_done_callback(i2c_slave_dev_handle_t channel, const i2c_slave_rx_done_event_data_t *edata, void *user_data)
  10. {
  11.     BaseType_t high_task_wakeup = pdFALSE;
  12.     QueueHandle_t receive_queue = (QueueHandle_t)user_data;
  13.     xQueueSendFromISR(receive_queue, edata, &high_task_wakeup);
  14.     return high_task_wakeup == pdTRUE;
  15. }
  16.  
  17. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  18. uint32_t size_rd = 0;
  19.  
  20. i2c_slave_config_t i2c_slv_config = {
  21.     .addr_bit_len = I2C_ADDR_BIT_LEN_7,
  22.     .clk_source = I2C_CLK_SRC_DEFAULT,
  23.     .i2c_port = I2C_NUM_1,
  24.     .send_buf_depth = 256,
  25.     .scl_io_num = 15,
  26.     .sda_io_num = 13,
  27.     .slave_addr = 0x10,
  28. };
  29.  
  30. i2c_slave_dev_handle_t slave_handle;
  31. ESP_ERROR_CHECK(i2c_new_slave_device(&i2c_slv_config, &slave_handle));
  32.  
  33. s_receive_queue = xQueueCreate(1, sizeof(i2c_slave_rx_done_event_data_t));
  34. i2c_slave_event_callbacks_t cbs = {
  35.     .on_recv_done = i2c_slave_rx_done_callback,
  36. };
  37. ESP_ERROR_CHECK(i2c_slave_register_event_callbacks(slave_handle, &cbs, s_receive_queue));
  38.  
  39. i2c_slave_rx_done_event_data_t rx_data;
  40. ESP_ERROR_CHECK(i2c_slave_receive(slave_handle, data_rd, DATA_LENGTH));
  41. xQueueReceive(s_receive_queue, &rx_data, pdMS_TO_TICKS(10000));
  42. // Receive done.

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

Re: Updated examples of new I2C master/slave driver for IDF 5.2.x...

Postby dkaufmann » Tue Sep 10, 2024 8:02 am

Hello all

I was using ESP-IDF v5.0 and I had a running I2C Slave code. Then I was updating to ESP-IDF V5.3 and I run into fatal errors with unchanged code. So I tried to update my code to the new I2C-Slave functions according to the API description with i2c_slave_config_t, i2c_new_slave_device, i2c_slave_rx_done_callback etc.
What I don't understand is the flow. What I tried is the following flow:
1. Enter I2C slave send and receive function when my program (ESP32-C3 I2C Slave) wants to communicate with the I2C Master (another MCU).
2. Fill tx buffer. Once the tx buffer is filled it will be sent out automatically. If it is not filled, the I2C-Slave should return a NACK. Here is the first problem: The I2C Slave always return ACK + the ADDR+Read. In my case the Master read command is 0x61+Read = 0xC3. This is what is returned on every Master read command from the host. I think this is not good. Earlier I deleted I2C driver and re-init it all the time to force to send the NACK. It should only be ACKed if the tx buffer was filled and the driver is inited.
3. After filling the tx buffer, the FIFO will be sent out automatically (OK)
4. Then I want to receive the response from the I2C Master. So I start the receive process with i2c_slave_receive and prepare the data_rd buffer accordingly.
5. Then I use the xQueueReceive such as in the example to wait for reveive completion
6. Then I read out the data_rd and analyse the data. But the data is invalid. But before it all worked with the same HW with the same controller with ESP-IDF V5.0.

So what is wrong? Do I need to call i2c_slave_receive again after xQueueReceive to read out the data?
Does it only work if the send and receive part is embed in a separate task instead of a function?

My goal is that I can call a function which sends out the data (already works) and then receive some data. For all other Master read requests when the function is not active, the ESP32 shlould just return a NACK as no data is requested.

Thanks for your help. A nice example would help. Or at least some more information how the i2c_slave_receive should be used in combination with the xQueueReceive and the callback.

Who is online

Users browsing this forum: No registered users and 163 guests