token-vesting icon indicating copy to clipboard operation
token-vesting copied to clipboard

ATTiny404: After 1556 calls to analogWrite, sketch restarts.

Open magoldsm opened this issue 1 year ago • 3 comments

Discussed in https://github.com/SpenceKonde/megaTinyCore/discussions/1129

Originally posted by magoldsm July 18, 2024 In the attached code, I make continuous calls to analogWrite. After 1556 calls, the sketch appears to crash and restart. It occurs regardless of which port I use. It also seems unaffected by malloc'ing 1k of memory or adding a useless 1k byte array, which suggests the problem is not a memory leak.

The attached file is actually an INO file, renamed to TXT to make it acceptable as an attachment.

Thanks for any help anyone is able to render!

Michael

AnalogWrite_Test.txt

magoldsm avatar Jul 18 '24 20:07 magoldsm

Swapped out the 404 for a 1614. Interestingly, it now runs for 1590 calls before restarting!

magoldsm avatar Jul 18 '24 20:07 magoldsm

I tried your code on an ATtiny814 and didn't see a problem. I was too lazy to hook up a device to capture the Serial output, but I added some code to blink an LED during setup to detect if the 814 crashed and restarted. It did not crash and restart.

My test code:

#define PINKY   1   // pin 3, PIN_PA5
#define RING    0   // pin 2, PIN_PA4
#define MIDDLE  6   // pin 8, PIN_PB1
#define INDEX   7   // pin 9, PIN_PB0
#define THUMB   10  // pin 13, PIN_PA3

int port = INDEX;
int writes;

void setup() {
  // put your setup code here, to run once:
  pinMode(port, OUTPUT);
  for(int i=0; i<5; i++) { // blink the LED to show the ATtiny crash/restart
    digitalWrite(port, 0); delay(200);
    digitalWrite(port, 1); delay(200);
  }

  Serial.begin(115200);
  delay(1000);

  Serial.println("Starting");

  auto cp = malloc(1024);
  writes = 0;
}

byte data[1024];      // Taking up memory for this array made no difference.

void loop() {
  Serial.print("writes: "); Serial.println(writes);
  for (int i=0; i<256; i += 2) {
    Serial.print('+');
    analogWrite(port, i);
    writes++;
    delay(5);
  }
  
  Serial.println("");
  Serial.print("writes: "); Serial.println(writes);
  for (int i=254; i>=0; i -= 2) {
    Serial.print('-');
    analogWrite(port, i);
    writes++;
    delay(5);
  }
  Serial.println("");
}

moose4lord avatar Jul 25 '24 13:07 moose4lord

byte data[1024]; will not use any memory if the data is never accessed - it will be optimized out. To prevent that, declare the array volatile. That will allow you test if it's a memory problem, which dollars to donuts it is. malloc and it's minion, the String class, are the hands of the devil on embedded systems like this. Avoid at almost any cost.

SpenceKonde avatar Jul 26 '24 16:07 SpenceKonde

Starting<\r><\n>
writes: 0<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 128<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 256<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 384<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 512<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 640<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 768<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 896<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 1024<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 1152<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 1280<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 1408<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 1536<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 1664<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 1792<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 1920<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 2048<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 2176<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 2304<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 2432<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 2560<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 2688<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 2816<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 2944<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 3072<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 3200<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 3328<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 3456<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 3584<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 3712<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 3840<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 3968<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 4096<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 4224<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 4352<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 4480<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 4608<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 4736<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 4864<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 4992<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 5120<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 5248<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 5376<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 5504<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 5632<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 5760<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 5888<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 6016<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 6144<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 6272<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 6400<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 6528<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 6656<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 6784<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 6912<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 7040<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 7168<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 7296<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 7424<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 7552<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 7680<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 7808<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 7936<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 8064<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 8192<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 8320<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 8448<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 8576<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 8704<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 8832<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 8960<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 9088<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 9216<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 9344<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 9472<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 9600<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 9728<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 9856<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 9984<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 10112<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 10240<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 10368<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 10496<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 10624<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 10752<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 10880<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 11008<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 11136<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 11264<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 11392<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>
writes: 11520<\r><\n>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\r><\n>
writes: 11648<\r><\n>
--------------------------------------------------------------------------------------------------------------------------------<\r><\n>

CNR on 1614. please repopen if you have a verified test case that I can use to fix it with.

SpenceKonde avatar Dec 20 '24 10:12 SpenceKonde