From 63f91292cee367eba447f2a186f4fcc28b1b073e Mon Sep 17 00:00:00 2001 From: Makuna Date: Mon, 25 May 2015 12:24:39 -0700 Subject: [PATCH] Fixed timer bug and cleaned up Decided to not expose compare timer in ESP object to minimize the exposure surface Fixed incorrect timer callback being used and initialized timer callbacks --- cores/esp8266/Arduino.h | 4 ++-- cores/esp8266/Esp.h | 16 ---------------- cores/esp8266/core_esp8266_timer.c | 10 ++++++---- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 7ebc77b79..dcaa47b09 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -114,8 +114,8 @@ void timer1_write(uint32_t ticks); //maximum ticks 8388607 // 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() (ESP.getCycleCompare0()) -#define timer0_write(ticks) (ESP.setCycleCompare0(ticks)) +#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(void(*userFunc)(void)); diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index c887c89e9..fc5237196 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -99,8 +99,6 @@ class EspClass { uint32_t getFlashChipSizeByChipId(void); inline uint32_t getCycleCount(void); - inline uint32_t getCycleCompare0(void); - inline void setCycleCompare0(uint32_t count); }; uint32_t EspClass::getCycleCount(void) @@ -110,20 +108,6 @@ uint32_t EspClass::getCycleCount(void) return ccount; } -// this returns a value in the range of (0 - 2^32) -uint32_t EspClass::getCycleCompare0(void) -{ - uint32_t count; - __asm__ __volatile__("esync; rsr %0,ccompare0":"=a" (count)); - return count; -} - -// this takes a value in the range of (0 - 2^32) -void EspClass::setCycleCompare0(uint32_t count) -{ - __asm__ __volatile__("wsr %0,ccompare0; esync"::"a" (count) : "memory"); -} - extern EspClass ESP; #endif //ESP_H diff --git a/cores/esp8266/core_esp8266_timer.c b/cores/esp8266/core_esp8266_timer.c index 979ab0bfc..b9b2f1256 100644 --- a/cores/esp8266/core_esp8266_timer.c +++ b/cores/esp8266/core_esp8266_timer.c @@ -22,7 +22,9 @@ #include "pins_arduino.h" #include "c_types.h" -void (*timer1_user_cb)(void); +typedef void(*_timercallback)(void); + +static volatile _timercallback timer1_user_cb = NULL; void timer1_isr_handler(void *para){ if((T1C & ((1 << TCAR) | (1 << TCIT))) == 0) TEIE &= ~TEIE1;//edge int disable @@ -60,7 +62,7 @@ void timer1_disable(){ T1I = 0; } -void(*timer0_user_cb)(void); +static volatile _timercallback timer0_user_cb = NULL; void timer0_isr_handler(void *para){ if (timer0_user_cb) { @@ -73,11 +75,11 @@ void timer0_isr_init(){ } void timer0_attachInterrupt(void(*userFunc)(void)) { - timer1_user_cb = userFunc; + timer0_user_cb = userFunc; ETS_CCOMPARE0_ENABLE(); } void timer0_detachInterrupt() { - timer1_user_cb = NULL; + timer0_user_cb = NULL; ETS_CCOMPARE0_DISABLE(); } \ No newline at end of file