core_esp8266_timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. timer.c - Timer1 library for esp8266
  3. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  4. This file is part of the esp8266 core for Arduino environment.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. // Use PWM from core 2.4.0 as all other version produce LED flickering when settings are saved to flash. Still true for 2.5.0
  18. //#include <core_version.h>
  19. //#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
  20. //#warning **** Tasmota is using v2.4.0 timer.c as planned ****
  21. #include "wiring_private.h"
  22. #include "pins_arduino.h"
  23. #include "c_types.h"
  24. #include "ets_sys.h"
  25. // ------------------------------------------------------------------ -
  26. // timer 1
  27. static volatile timercallback timer1_user_cb = NULL;
  28. void ICACHE_RAM_ATTR timer1_isr_handler(void *para){
  29. (void) para;
  30. if ((T1C & ((1 << TCAR) | (1 << TCIT))) == 0) TEIE &= ~TEIE1;//edge int disable
  31. T1I = 0;
  32. if (timer1_user_cb) {
  33. // to make ISR compatible to Arduino AVR model where interrupts are disabled
  34. // we disable them before we call the client ISR
  35. uint32_t savedPS = xt_rsil(15); // stop other interrupts
  36. timer1_user_cb();
  37. xt_wsr_ps(savedPS);
  38. }
  39. }
  40. void ICACHE_RAM_ATTR timer1_isr_init(void){
  41. ETS_FRC_TIMER1_INTR_ATTACH(timer1_isr_handler, NULL);
  42. }
  43. void timer1_attachInterrupt(timercallback userFunc) {
  44. timer1_user_cb = userFunc;
  45. ETS_FRC1_INTR_ENABLE();
  46. }
  47. void ICACHE_RAM_ATTR timer1_detachInterrupt(void) {
  48. timer1_user_cb = 0;
  49. TEIE &= ~TEIE1;//edge int disable
  50. ETS_FRC1_INTR_DISABLE();
  51. }
  52. void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload){
  53. T1C = (1 << TCTE) | ((divider & 3) << TCPD) | ((int_type & 1) << TCIT) | ((reload & 1) << TCAR);
  54. T1I = 0;
  55. }
  56. void ICACHE_RAM_ATTR timer1_write(uint32_t ticks){
  57. T1L = ((ticks)& 0x7FFFFF);
  58. if ((T1C & (1 << TCIT)) == 0) TEIE |= TEIE1;//edge int enable
  59. }
  60. void ICACHE_RAM_ATTR timer1_disable(void){
  61. T1C = 0;
  62. T1I = 0;
  63. }
  64. //-------------------------------------------------------------------
  65. // timer 0
  66. static volatile timercallback timer0_user_cb = NULL;
  67. void ICACHE_RAM_ATTR timer0_isr_handler(void* para){
  68. (void) para;
  69. if (timer0_user_cb) {
  70. // to make ISR compatible to Arduino AVR model where interrupts are disabled
  71. // we disable them before we call the client ISR
  72. uint32_t savedPS = xt_rsil(15); // stop other interrupts
  73. timer0_user_cb();
  74. xt_wsr_ps(savedPS);
  75. }
  76. }
  77. void timer0_isr_init(void){
  78. ETS_CCOMPARE0_INTR_ATTACH(timer0_isr_handler, NULL);
  79. }
  80. void timer0_attachInterrupt(timercallback userFunc) {
  81. timer0_user_cb = userFunc;
  82. ETS_CCOMPARE0_ENABLE();
  83. }
  84. void ICACHE_RAM_ATTR timer0_detachInterrupt(void) {
  85. timer0_user_cb = NULL;
  86. ETS_CCOMPARE0_DISABLE();
  87. }
  88. //#endif // ARDUINO_ESP8266_RELEASE