i2c randombly fails

jesseb
Posts: 29
Joined: Tue Jan 10, 2017 5:36 pm

Re: i2c randombly fails

Postby jesseb » Thu Jul 27, 2017 12:23 am

Sorry for the misunderstanding on what you were asking. My driver still works around them but I have not tested yet whether I need to work around them or not. I'll let you know when I get a chance to test it.

User avatar
Jakobsen
Posts: 89
Joined: Mon Jan 16, 2017 8:12 am

Re: i2c randombly fails

Postby Jakobsen » Thu Jul 27, 2017 9:17 pm

Hi
Thanks - I have been look for that fix for a very long time - and even considering using a soft i2c driver.
No more random fails on i2c. Change from _FLASH to _RAM did the job.

Code: Select all

 ...
  ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  ...
Regards Jakobsen
Analog Digital IC designer / DevOps @ Merus Audio, Copenhagen, Denmark.
We do novel and best in class Audio amplifiers for consumer products.
Programmed assembler for C-64 back in 1980's, learned some electronics - hacking since then

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: i2c randombly fails

Postby WiFive » Thu Jul 27, 2017 10:34 pm

Wow that is strange WIFI_STORAGE_FLASH can break i2c.

Maq_aux
Posts: 7
Joined: Mon Feb 13, 2017 8:57 pm

Re: i2c randombly fails

Postby Maq_aux » Fri Jul 28, 2017 3:44 pm

Hi,

i really hoped that this fix is going to help me with my I2C problems (mainly the SCL gets stuck or keeps clocking forever sometimes in the first seconds after a power-on, only way to recover is a module reset). Once the I2C bus works for the first couple of seconds it seems to be stable for a long time so far.

Unfortunately i recognized that

Code: Select all

 ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
is already used in my code. I am happy this fix improves some parts of the I2C part but probably there still are some more issues.

Regards,
Markus

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: i2c randombly fails

Postby gregstewart90 » Fri Aug 04, 2017 9:19 pm

As Markus said, the problem still exists for me even with the wifi change. The SCL line clocks forever until I reset. The values I get back from my MCP23017 i2c device are wrong and take longer to get back than normal. I believe there is a fluctuation in the power or interference from relays close by which switch 120vac main lines. I can simulate the issue by pulling the i2c lines to ground for a second.

ammaree
Posts: 39
Joined: Tue Dec 27, 2016 2:10 am

Re: i2c randombly fails

Postby ammaree » Fri Jan 26, 2018 7:10 pm

Hi @jesseb ,

Read your comments on the I2C driver and your simplified replacement. I am at wits end with the ESP32 and I2C, my situation is as follows:
* Custom designed hardware using genuine Rev1 WROOM modules

* 8x LEDs and 8x relays attached to an I2C GPIO extender.
* implementing soft PWM (fadeIn, On, FadeOut, Off) functionality in a dedicated task running at 1ms (i tick) intervals
* based on the specific timing and stage each bit is toggled on and off.

* DS2482-800 I2C to 1-Wire (8 channel ) bridge reading iButtons for access and resource control
* DS2482-800 is read every 1 sec (1000 ticks)

Originally I wrote each bit change (only changes) using an individual I2C write. Very quickly this crashed the I2C bus when all 16 actuators running. I then changed to buffer all bit changes for a full cycle of all 16 actuators, and then only write the full 16 bit-mapped GPIO states to the GPIO expander in a single 16 bit write.

This reduced I2C activity hugely, but I still on a completely irregular basis have the I2C bus freeze and the GPIOs/actuators go into random states.

My questions are as follows:
1) Have you had reasonable success with your simplified I2C driver?
2) If so, can you please share the latest version with me so I can try to port my actuator engine to run on your driver instead?

All feedback much appreciated,

Andre

barth_bh
Posts: 2
Joined: Sun Sep 16, 2018 12:36 am

Re: i2c randombly fails

Postby barth_bh » Sun Sep 16, 2018 12:49 am

I have the same problem, accidentally the esp32 transmission from MCP23017 hangs.
It stops working after some time - randomly. I have not found a solution still

amruta
Posts: 18
Joined: Mon Aug 21, 2017 10:03 am

Re: i2c randombly fails

Postby amruta » Mon Apr 22, 2019 12:21 pm

I am facing the same issue with I2C example provided in idf.
ESP32 Arduino works as expected whereas esp-idf gives the following errors
  • timeout : 0x107 (generated by I2C_STATUS_TIMEOUT)
  • failed: -1 (generated by I2C_STATUS_ACK_ERROR)
The slave is an Arduino device.

ESP-idf version: v3.1
Code:
  1.  
  2. #include <stdio.h>
  3. #include "driver/i2c.h"
  4.  
  5. #define I2C_EXAMPLE_MASTER_SDA_IO 22           /*!< gpio number for I2C master data  */
  6. #define I2C_EXAMPLE_MASTER_SCL_IO 23           /*!< gpio number for I2C master clock */
  7. #define I2C_EXAMPLE_MASTER_NUM I2C_NUM_0       /*!< I2C port number for master dev */
  8. #define I2C_EXAMPLE_MASTER_TX_BUF_DISABLE 0    /*!< I2C master do not need buffer */
  9. #define I2C_EXAMPLE_MASTER_RX_BUF_DISABLE 0    /*!< I2C master do not need buffer */
  10. #define I2C_EXAMPLE_MASTER_FREQ_HZ (10 * 1000) /*!< I2C master clock frequency */
  11.  
  12. #define DATA_LENGTH 1   //18       /*!<Data buffer length for test buffer*/
  13. #define I2C_SLAVE_ADDR (8)         /*!< slave address*/
  14. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  15. #define READ_BIT I2C_MASTER_READ   /*!< I2C master read */
  16. #define ACK_CHECK_EN 0x1           /*!< I2C master will check ack from slave*/
  17. #define ACK_CHECK_DIS 0x0          /*!< I2C master will not check ack from slave */
  18. #define ACK_VAL 0x0                /*!< I2C ack value */
  19. #define NACK_VAL 0x1               /*!< I2C nack value */
  20.  
  21.  
  22. static esp_err_t i2c_example_master_read_slave(i2c_port_t i2c_num, uint8_t *data_rd, size_t size)
  23. {
  24.     esp_err_t err = ESP_OK;
  25.  
  26.     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  27.     i2c_master_start(cmd);
  28.     i2c_master_write_byte(cmd, (I2C_SLAVE_ADDR << 1) | READ_BIT, ACK_CHECK_EN);
  29.     if (size > 1)
  30.     {
  31.         i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
  32.     }
  33.     i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
  34.     i2c_master_stop(cmd);
  35.     err = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
  36.     i2c_cmd_link_delete(cmd);
  37.  
  38.     return err;
  39. }
  40.  
  41.  
  42. static void i2c_example_master_init()
  43. {
  44.     int i2c_master_port = I2C_EXAMPLE_MASTER_NUM;
  45.     i2c_config_t conf;
  46.     conf.mode = I2C_MODE_MASTER;
  47.     conf.sda_io_num = I2C_EXAMPLE_MASTER_SDA_IO;
  48.     conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
  49.     conf.scl_io_num = I2C_EXAMPLE_MASTER_SCL_IO;
  50.     conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
  51.     conf.master.clk_speed = I2C_EXAMPLE_MASTER_FREQ_HZ;
  52.     esp_err_t err = i2c_param_config(i2c_master_port, &conf);
  53.     if (ESP_OK == err)
  54.     {
  55.         err = i2c_driver_install(i2c_master_port, conf.mode,
  56.                                  I2C_EXAMPLE_MASTER_RX_BUF_DISABLE,
  57.                                  I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0);
  58.         printf("i2c_driver_install 0x%x \n", err);
  59.     }
  60.     else
  61.     {
  62.         printf("i2c_param_config fail 0x%x \n", err);
  63.     }
  64. }
  65.  
  66. static void i2c_test_task(void *arg)
  67. {
  68.     uint8_t data_rd[DATA_LENGTH] = {0};
  69.     int cnt = 0;
  70.     esp_err_t ret = 0;
  71.  
  72.     while (1)
  73.     {
  74.         printf("cnt: %d\n", cnt++);
  75.  
  76.         vTaskDelay(100 / portTICK_RATE_MS);
  77.  
  78.         ret = i2c_example_master_read_slave(I2C_EXAMPLE_MASTER_NUM, data_rd, DATA_LENGTH);
  79.  
  80.         if (ESP_OK == ret)
  81.         {
  82.             data_rd[DATA_LENGTH] = 0;
  83.             printf("Data: %s \n", data_rd);
  84.         }
  85.         else
  86.         {
  87.             printf("err: 0x%x %s \n", ret, esp_err_to_name(ret));
  88.         }
  89.     }
  90. }
  91.  
  92. void app_main()
  93. {
  94.     i2c_example_master_init();
  95.     xTaskCreate(i2c_test_task, "i2c_test_task_1", 1024 * 5, (void *)1, 10, NULL);
  96. }
  97.  

doglike
Posts: 63
Joined: Fri Aug 18, 2017 4:21 pm

Re: i2c randombly fails

Postby doglike » Wed May 08, 2019 6:02 pm

Hi,
I had the same issues driving MCP23017 over i2c.
Hanging bus and getting randomly and not reproducible output to console:

Code: Select all

esp32-hal-i2c.c:1378] i2cCheckLineState():Bus Invalid State, TwoWire() Can't init sda=1, scl=1
After days of trying everything I found a solution that finally worked for me
:arrow: Run the whole i2c and MCP23017 stuff in an own task.

Code: Select all

void setup (void)
{
	xTaskCreatePinnedToCore (mcpTask, "mcpTask", 2048, (void *)1, 1, NULL, CORE1);
}


void mcpTask (void *pvParameters)
{
	while(1) {
			delay(100);				//  calc. only every 100ms

			static bool flgSetup = false;
			if (!flgSetup)
			{
				configPinsMCP();		// wire.begin(..) + mcp.begin(..) + some setup stuff
				flgSetup = true;
			}
			else
			{
				Inputs_100ms();			// cyclic task, switches MCP ports
			}
	}
}
Actually this code is running for some hours successfully without hanging bus or console outputs :D
Hope this will work for others, too.

PS: I'm using the latest Adafruit lib for MCP23017 @ 400kHz
https://www.arduinolibraries.info/libra ... no-library

schopra
Posts: 2
Joined: Thu Dec 17, 2020 8:23 pm

Re: i2c randombly fails

Postby schopra » Thu Dec 17, 2020 9:04 pm

Hi,

I am encountering the same issue and wanted to know if there was a fix.

When I do an SPI FLASH command while i2c is running, it causes an ESP_FAIL error on the i2c transaction. The scl pin is pulled low and then the sda pin as can be seen in the attached image. It does recover, but I wanted to know if there was a way to solve this issue?
Attachments
i2cbug2.png
i2cbug2.png (162.65 KiB) Viewed 10302 times

Who is online

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