Chat freely about anything...

User avatar
By PickyBiker
#82679 I am trying to figure out the frequency and period of the code below. Here is what I think it might be:
    Start with a clock of 80MHz
    TIM_DIV16 suggest to me that 80 MHz is divided by 16 so that would be 5 MHz or .2 microseconds
    If I am correct, that would mean an interrupt after .0000002 * 5000 = .001 seconds or 1000 Hz.
Am I getting this right?
Also, not sure what TIM_EDGE and TIM_SINGLE mean. Is there a reference somewhere for those?

Code: Select all  timer1_attachInterrupt(TimerISR);
  timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
  timer1_write(5000);
User avatar
By btidey
#82706 The timers are one of the trickier areas from a documentation point of view as there does not seem to be 1 clear place where their functions, attributes and control are well documented.

For the timer1_enable call your timing calculations are correct.

The parameters are
prescaler which can be TIM_DIV1, TIM_DIV16, TIM_DIV256 from the 80MHz clock.

Interrupt type which can be TIM_EDGE or TIM_LEVEL. I think everybody uses TIM_EDGE as the interrupt does not need to be cleared.

Interrupt loop which can be TIM_SINGLE or TIM_LOOP. Single just means interrupt once after the delay, loop means interrupt repeatedly at same interval. So Single is used for a one shot or when you want to alter the period on each iteration by kicking it off again within the isr. Loop is used for a fixed frequency of interrupts.
User avatar
By rudy
#82710 https://github.com/esp8266/Arduino/blob ... /Arduino.h

From the ESP8266-Arduino repository.

Code: Select all//timer dividers
enum TIM_DIV_ENUM {
  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)
};


//timer int_types
#define TIM_EDGE   0
#define TIM_LEVEL   1

//timer reload values
#define TIM_SINGLE   0 //on interrupt routine you need to write a new value to start the timer again
#define TIM_LOOP   1 //on interrupt the counter will start with the same value again

#define timer1_read()           (T1V)
#define timer1_enabled()        ((T1C & (1 << TCTE)) != 0)
#define timer1_interrupted()    ((T1C & (1 << TCIS)) != 0)

typedef void(*timercallback)(void);

void timer1_isr_init(void);
void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload);
void timer1_disable(void);
void timer1_attachInterrupt(timercallback userFunc);
void timer1_detachInterrupt(void);
void timer1_write(uint32_t ticks); //maximum ticks 8388607

// timer0 is a special CPU timer that has very high resolution but with
// limited control.
// it uses CCOUNT (ESP.GetCycleCount()) as the non-resetable timer counter
// it does not support divide, type, or reload flags
// it is auto-disabled when the compare value matches CCOUNT
// it is auto-enabled when the compare value changes
#define timer0_interrupted()    (ETS_INTR_PENDING() & (_BV(ETS_COMPARE0_INUM)))
#define timer0_read() ((__extension__({uint32_t count;__asm__ __volatile__("esync; rsr %0,ccompare0":"=a" (count));count;})))
#define timer0_write(count) __asm__ __volatile__("wsr %0,ccompare0; esync"::"a" (count) : "memory")

void timer0_isr_init(void);
void timer0_attachInterrupt(timercallback userFunc);
void timer0_detachInterrupt(void);


From ESP8266 Technical Reference
counters.png
Last edited by rudy on Fri Jun 07, 2019 7:52 am, edited 1 time in total.