Post topics, source code that relate to the Arduino Platform

User avatar
By arnab321
#27803 Hi. I am trying to port an Arduino project to ESP. It used the AVR timer registers. It is necessary for me to use hardware timers of ESP.
I learned from the Servo library and Arduino.h about the timer0 and timer1 functions , though there seems to be no official documentation on this.

Test code:

Code: Select all 
void inline handler (void){
  digitalWrite(BUILTIN_LED,1  /* !digitalRead(BUILTIN_LED) */ );  //the led doesnt toggle, nor glow even faintly
  }
 
 void setup() {
  pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);

  noInterrupts();
  timer0_isr_init();
  timer0_attachInterrupt(handler);
  timer0_write(4166);
  interrupts();
 
 
}

void loop() {
  delay(1000);
}


Im getting:
Code: Select all ets Jan  8 2013,rst cause:4, boot mode:(3,3)

wdt reset
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x42
csum 0x42
~ld
À



What is the correct way to use timer0, and trigger a callback, after every X ticks?
User avatar
By arnab321
#27873 okay, so, i learnt that timer0 is a non-resettable, count up, wierd timer.


Code: Select all noInterrupts();
  timer0_isr_init();
  timer0_attachInterrupt(handler);
 
  timer0_write(ESP.getCycleCount() +4166);
  interrupts();


And in the handler function, update the timer value to ESP.getCycleCount() + 4166

That worked!

Also, my prev code with the built in led didnt work. Nor did analogWrite, (my other thread) which uses h/w timers. But this worked with an external LED.


Still i dont know why a watchdog reset was firing earlier when i hadnt included ESP.getCycleCount().
User avatar
By jcard0na
#34531 Thanks a lot for posting that example. It help me a lot.

With the changes below you can see the built-in LED blinking approximately once per second. So there is no need for an external LED to test the timer.

Code: Select all#include "AudioDriver.h"

volatile int toggle;

void inline handler (void){
  toggle = (toggle == 1) ? 0 : 1;
  digitalWrite(BUILTIN_LED, toggle);
  timer0_write(ESP.getCycleCount() + 41660000);
}
 
void setup() {
  pinMode(BUILTIN_LED, OUTPUT);

  noInterrupts();
  timer0_isr_init();
  timer0_attachInterrupt(handler);
  timer0_write(ESP.getCycleCount() + 41660000);
  interrupts();
}

void loop() {
  delay(1000);
}
User avatar
By tonus@gmx.us
#59214 I have a cloned Wemos D1 mini board. That works great and stable for weeks.
However if I use a simple timer interrupt that each second updates an integer, it becomes unstable and resets within 10 minutes.

It is powered by an usb-cable from my display. (The Wemos D1 mini has a built-in serial converter and voltage regulator.)

If the problem is in my software, please explain why and how to work around? Still using the interrupt please!
I cannot imagine, yet, that this has something to do with the power supply.
If the problem is in my hardware what hardware is proven flawless?

>>

void timer0_ISR (void)
{
timer0_write(ESP.getCycleCount() + 80000000L); // 80MHz ~ 1sec
ticks++;
}

<<

>>
Exception (0):
epc1=0x40201c24 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys
sp: 3ffffc50 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffdf0: 00000006 00000006 42d80000 42d80000
3ffffe00: 402218aa 3ffefcf4 3fffc248 4000050c
3ffffe10: 40004384 00000030 00000016 ffffffff
3ffffe20: 60000200 00000002 74007004 80000000
3ffffe30: 20000000 3fff05e8 80000000 203fc160
3ffffe40: 00000000 3fffc6fc 3ffe91f4 3fff05ec
3ffffe50: 00000194 003fc160 60000600 00000030
3ffffe60: 7fffffff 3ffe9c7c 3ffe9c7c 00000001
3ffffe70: 402222e1 3ffe8da0 00000000 00000000
3ffffe80: 40101180 0af4bda3 3ffefd0c 00000000
3ffffe90: 00000000 00000000 0000001f 4010103d
3ffffea0: 4000050c 0ad8216b 4022697b 3ffefd0c
3ffffeb0: 40000f68 00000030 00000011 ffffffff
3ffffec0: 40000f58 00000000 00000020 00000000
3ffffed0: 00000007 4021accc 3ffe91f4 bfffffff
3ffffee0: ffffffff 3ffefd0c 3ffe91f4 3fffdab0
3ffffef0: 00000000 3fffdcb0 3ffe8918 00000030
3fffff00: 00000000 400042db 00000064 60000600
3fffff10: 40004b31 3fff0484 000002f4 003fc000
3fffff20: 40101f6e 3ffe8900 3ffe90a0 40106fec
3fffff30: 4021a79d 3ffe90a0 3ffe8900 23bc956a
3fffff40: 3fff0484 00001000 4021ac32 00000008
3fffff50: 4021cbb4 3ffe91f4 4021acdf 3ffe9154
3fffff60: 3ffe8900 0ad89471 3ffe8900 60000600
3fffff70: 40203249 3ffe9154 3ffe8900 23bc78c6
3fffff80: 4020328e 3fffdab0 00000000 3fffdcb0
3fffff90: 3ffe8920 00000000 40000f65 3fffdab0
3fffffa0: 40000f49 000181d6 3fffdab0 40000f49
<<<stack<<<

ets Jan 8 2013,rst cause:2, boot mode:(1,6)


ets Jan 8 2013,rst cause:4, boot mode:(1,6)

wdt reset
<<