My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HAL_timers_ESP32.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef ARDUINO_ARCH_ESP32
  23. // --------------------------------------------------------------------------
  24. // Includes
  25. // --------------------------------------------------------------------------
  26. #include <stdio.h>
  27. #include "esp_types.h"
  28. #include "soc/timer_group_struct.h"
  29. #include "driver/periph_ctrl.h"
  30. #include "driver/timer.h"
  31. #include "HAL.h"
  32. #include "HAL_timers_ESP32.h"
  33. // --------------------------------------------------------------------------
  34. // Externals
  35. // --------------------------------------------------------------------------
  36. // --------------------------------------------------------------------------
  37. // Local defines
  38. // --------------------------------------------------------------------------
  39. #define NUM_HARDWARE_TIMERS 4
  40. // --------------------------------------------------------------------------
  41. // Types
  42. // --------------------------------------------------------------------------
  43. // --------------------------------------------------------------------------
  44. // Public Variables
  45. // --------------------------------------------------------------------------
  46. // --------------------------------------------------------------------------
  47. // Private Variables
  48. // --------------------------------------------------------------------------
  49. static timg_dev_t *TG[2] = {&TIMERG0, &TIMERG1};
  50. const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
  51. { TIMER_GROUP_0, TIMER_0, STEPPER_TIMER_PRESCALE, stepTC_Handler }, // 0 - Stepper
  52. { TIMER_GROUP_0, TIMER_1, TEMP_TIMER_PRESCALE, tempTC_Handler }, // 1 - Temperature
  53. { TIMER_GROUP_1, TIMER_0, 1, nullptr }, // 2
  54. { TIMER_GROUP_1, TIMER_1, 1, nullptr }, // 3
  55. };
  56. // --------------------------------------------------------------------------
  57. // Function prototypes
  58. // --------------------------------------------------------------------------
  59. // --------------------------------------------------------------------------
  60. // Private functions
  61. // --------------------------------------------------------------------------
  62. // --------------------------------------------------------------------------
  63. // Public functions
  64. // --------------------------------------------------------------------------
  65. void IRAM_ATTR timer_group0_isr(void *para) {
  66. const int timer_idx = (int)para;
  67. // Retrieve the interrupt status and the counter value
  68. // from the timer that reported the interrupt
  69. uint32_t intr_status = TIMERG0.int_st_timers.val;
  70. TIMERG0.hw_timer[timer_idx].update = 1;
  71. // Clear the interrupt
  72. if (intr_status & BIT(timer_idx)) {
  73. switch (timer_idx) {
  74. case TIMER_0: TIMERG0.int_clr_timers.t0 = 1; break;
  75. case TIMER_1: TIMERG0.int_clr_timers.t1 = 1; break;
  76. }
  77. }
  78. const tTimerConfig timer = TimerConfig[timer_idx];
  79. timer.fn();
  80. // After the alarm has been triggered
  81. // Enable it again so it gets triggered the next time
  82. TIMERG0.hw_timer[timer_idx].config.alarm_en = TIMER_ALARM_EN;
  83. }
  84. /**
  85. * Enable and initialize the timer
  86. * @param timer_num timer number to initialize
  87. * @param frequency frequency of the timer
  88. */
  89. void HAL_timer_start(const uint8_t timer_num, uint32_t frequency) {
  90. const tTimerConfig timer = TimerConfig[timer_num];
  91. timer_config_t config;
  92. config.divider = timer.divider;
  93. config.counter_dir = TIMER_COUNT_UP;
  94. config.counter_en = TIMER_PAUSE;
  95. config.alarm_en = TIMER_ALARM_EN;
  96. config.intr_type = TIMER_INTR_LEVEL;
  97. config.auto_reload = true;
  98. // Select and initialize the timer
  99. timer_init(timer.group, timer.idx, &config);
  100. // Timer counter initial value and auto reload on alarm
  101. timer_set_counter_value(timer.group, timer.idx, 0x00000000ULL);
  102. // Configure the alam value and the interrupt on alarm
  103. timer_set_alarm_value(timer.group, timer.idx, (HAL_TIMER_RATE) / timer.divider / frequency - 1);
  104. timer_enable_intr(timer.group, timer.idx);
  105. // TODO need to deal with timer_group1_isr
  106. timer_isr_register(timer.group, timer.idx, timer_group0_isr, (void*)timer.idx, 0, nullptr);
  107. timer_start(timer.group, timer.idx);
  108. }
  109. /**
  110. * Set the upper value of the timer, when the timer reaches this upper value the
  111. * interrupt should be triggered and the counter reset
  112. * @param timer_num timer number to set the count to
  113. * @param count threshold at which the interrupt is triggered
  114. */
  115. void HAL_timer_set_compare(const uint8_t timer_num, hal_timer_t count) {
  116. const tTimerConfig timer = TimerConfig[timer_num];
  117. timer_set_alarm_value(timer.group, timer.idx, count);
  118. }
  119. /**
  120. * Get the current upper value of the timer
  121. * @param timer_num timer number to get the count from
  122. * @return the timer current threshold for the alarm to be triggered
  123. */
  124. hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  125. const tTimerConfig timer = TimerConfig[timer_num];
  126. uint64_t alarm_value;
  127. timer_get_alarm_value(timer.group, timer.idx, &alarm_value);
  128. return alarm_value;
  129. }
  130. /**
  131. * Get the current counter value between 0 and the maximum count (HAL_timer_set_count)
  132. * @param timer_num timer number to get the current count
  133. * @return the current counter of the alarm
  134. */
  135. hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  136. const tTimerConfig timer = TimerConfig[timer_num];
  137. uint64_t counter_value;
  138. timer_get_counter_value(timer.group, timer.idx, &counter_value);
  139. return counter_value;
  140. }
  141. /**
  142. * Enable timer interrupt on the timer
  143. * @param timer_num timer number to enable interrupts on
  144. */
  145. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  146. //const tTimerConfig timer = TimerConfig[timer_num];
  147. //timer_enable_intr(timer.group, timer.idx);
  148. }
  149. /**
  150. * Disable timer interrupt on the timer
  151. * @param timer_num timer number to disable interrupts on
  152. */
  153. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  154. //const tTimerConfig timer = TimerConfig[timer_num];
  155. //timer_disable_intr(timer.group, timer.idx);
  156. }
  157. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  158. const tTimerConfig timer = TimerConfig[timer_num];
  159. return TG[timer.group]->int_ena.val | BIT(timer_num);
  160. }
  161. #endif // ARDUINO_ARCH_ESP32