Strange ATTINY 85 i2C problem

Hello,

I have created a project using attiny85, programming it with arduinoIDE. Attiny communicate with another device via I2C, so far so good, but at some point where my code go around 5-6KB I2C communication start to fails. At the start I was looking for hardware problem, but since do not found one, start to looking into software removing recently added parts from the program. At the end I discovered that as code grow I2C communication fail. That of course do not make any sense to me, I continue to investigate and finally found a solution:

I need to place I2C interrupt functions at the start of the code, instead at the end. It worked that way. Still got no idea why, perhaps someone with proper knowledge gonna enlight me.

What core are you using? Your answer is going to depend on that. Some cores (like TinyCore) let you use I2C just like normal so you don't even need to have an I2C interrupt.

ATtinyCore

https://github.com/SpenceKonde/ATTinyCore/blob/master/Installation.md Before start writing, gone thro few of them, this one works best for me.

What you mean use I2C like normal, all I see into example and guides for I2C on arduino IDE is based on interrupts when receive data. Is there another way?

nedoskiv:
ATtinyCore

https://github.com/SpenceKonde/ATTinyCore/blob/master/Installation.md

That’s the best choice. The creator is quite active on the forum, he goes by DrAzzy. I wouldn’t be surprised if he popped in here at some point.

nedoskiv:
What you mean use I2C like normal, all I see into example and guides for I2C on arduino IDE is based on interrupts when receive data. Is there another way?

I think it is time for you to share your code, because what you’re saying here doesn’t make any sense. Here is some I2C example code straight from the IDE Examples:

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

I don’t see any interrupts in that code, do you?
There are interrupts going on in the background but nothing that we need to use or insert ourselves.

OKay but it a bit messy :slight_smile:
Also forgot to mention that it is in slave mode.

Little explanation of the purpose of the device:

It monitor an battery and shutdown device after it, if voltage goes below specified value,
Power back on if external (charging voltage) is restored and battery is charged enough.
Sent status via i2c requests, also accept some configuration changes via that way, recently added inactivity timer to restart device, if I2C is not polled for specified period of time.
Also have a buzzer control function (but is not used at present time, due to failing use reset pin as normal GPIO)

14_04_2019_discharge_protection_i2c_config_buzzer_innactivity.ino (12.4 KB)