So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By TobyEggitt
#75595 Hi all, I'm hoping the ESP8266 can provide a regular timer interrupt. Unfortunately, after a couple of days of poking around in various locations, I've failed to find out if this is even something the hardware can provide, let alone how to do it.

My need would be to generate a continuous interrupt stream at a rate that the program can alter (ideally within in the range of perhaps 10 Hz on the low end, to maybe 4kHz on the high end (faster is fine, I guess, I can always divide in software).

If the interrupt rate cannot be configured, I'd need a much faster interrupt so I could divide it down with the resolution that I'll need.

I don't need the WiFi to work for this program (I mention this because something I read in my searching seemed to hint that this might be possible, but that it would screw up the WiFi system).

I need the interrupt stream to be such that the timing doesn't "slip". That is, I need the next cycle to start automatically and immediately in hardware, so that any jitter int he time that the software takes to service the interrupt does not create any kind of accumulation of timing error (though it's fine if the service routine for a particular interrupt is delayed, e.g. by a higher priority interrupt or NMI).

For more background, I have achieved this in the arduino, using timer 1 in it's compare mode (CTC), I'd like to reproduce that on smaller/cheaper/better hardware :)

Can anyone tell me if this is even possible? And better yet, if it is, where I might find docs to help with the hardware configuration?

Cheers
Toby.
User avatar
By btidey
#75596 Use timer1

Code: Select all      timer1_isr_init();
      timer1_attachInterrupt(bitTxIsr);
      timer1_enable(TIM_DIV256, TIM_EDGE, TIM_LOOP);
      timer1_write(1000);


Note in some versions of esp libraries TIM_265 (typ0) was used instead of TIM_256. It can be
Code: Select all  TIM_DIV1 = 0,   //80MHz (80 ticks/us - 104857.588 us max)
  TIM_DIV16 = 1,  //5MHz (5 ticks/us - 1677721.4 us max)
  TIM_DIV256 = 3 //312.5Khz (1 tick = 3.2us - 26843542.4 us max)


isr routines should be kept as short as possible, no serial.print statements, and have ICACHE_RAM_ATTR attribute. I would keep the repetition rate < 10KHz. Avoid using PWM if using timer interrupts.
User avatar
By TobyEggitt
#75598 Excellent, thanks. I've clearly been looking in all the wrong places! Could you also, in hope that I won't need to ask too many too-basic questions, point me at where you found this info? Also you refer to a library... Where is that?

Many thanks for your help!
Toby
User avatar
By btidey
#75604 By library I really meant the core esp8266 support code.

The timer1 core routines are in
Arduino15\packages\esp8266\hardware\esp8266\2.4.1\cores\esp8266\core_esp8266_timer.c
with some constants defined in Arduino.h

ESP8266 timer information is a bit spread out. I have got most of the information I needed from a timer register map ( an attachment in https://bbs.espressif.com/viewtopic.php?t=2537 ) and by looking at some of the existing code in things like the PWM support.

timer1 is actually pretty simple hardware so there is nothing complex in the software routines.