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.

timers.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include <stdio.h>
  24. #include <esp_types.h>
  25. #include <soc/timer_group_struct.h>
  26. #include <driver/periph_ctrl.h>
  27. #include <driver/timer.h>
  28. #include "HAL.h"
  29. #include "timers.h"
  30. // ------------------------
  31. // Local defines
  32. // ------------------------
  33. #define NUM_HARDWARE_TIMERS 4
  34. // ------------------------
  35. // Private Variables
  36. // ------------------------
  37. static timg_dev_t *TG[2] = {&TIMERG0, &TIMERG1};
  38. const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
  39. { TIMER_GROUP_0, TIMER_0, STEPPER_TIMER_PRESCALE, stepTC_Handler }, // 0 - Stepper
  40. { TIMER_GROUP_0, TIMER_1, TEMP_TIMER_PRESCALE, tempTC_Handler }, // 1 - Temperature
  41. { TIMER_GROUP_1, TIMER_0, PWM_TIMER_PRESCALE, pwmTC_Handler }, // 2 - PWM
  42. { TIMER_GROUP_1, TIMER_1, 1, nullptr }, // 3
  43. };
  44. // ------------------------
  45. // Public functions
  46. // ------------------------
  47. void IRAM_ATTR timer_isr(void *para) {
  48. const tTimerConfig& timer = TimerConfig[(int)para];
  49. // Retrieve the interrupt status and the counter value
  50. // from the timer that reported the interrupt
  51. uint32_t intr_status = TG[timer.group]->int_st_timers.val;
  52. TG[timer.group]->hw_timer[timer.idx].update = 1;
  53. // Clear the interrupt
  54. if (intr_status & BIT(timer.idx)) {
  55. switch (timer.idx) {
  56. case TIMER_0: TG[timer.group]->int_clr_timers.t0 = 1; break;
  57. case TIMER_1: TG[timer.group]->int_clr_timers.t1 = 1; break;
  58. case TIMER_MAX: break;
  59. }
  60. }
  61. timer.fn();
  62. // After the alarm has been triggered
  63. // Enable it again so it gets triggered the next time
  64. TG[timer.group]->hw_timer[timer.idx].config.alarm_en = TIMER_ALARM_EN;
  65. }
  66. /**
  67. * Enable and initialize the timer
  68. * @param timer_num timer number to initialize
  69. * @param frequency frequency of the timer
  70. */
  71. void HAL_timer_start(const uint8_t timer_num, uint32_t frequency) {
  72. const tTimerConfig timer = TimerConfig[timer_num];
  73. timer_config_t config;
  74. config.divider = timer.divider;
  75. config.counter_dir = TIMER_COUNT_UP;
  76. config.counter_en = TIMER_PAUSE;
  77. config.alarm_en = TIMER_ALARM_EN;
  78. config.intr_type = TIMER_INTR_LEVEL;
  79. config.auto_reload = true;
  80. // Select and initialize the timer
  81. timer_init(timer.group, timer.idx, &config);
  82. // Timer counter initial value and auto reload on alarm
  83. timer_set_counter_value(timer.group, timer.idx, 0x00000000ULL);
  84. // Configure the alam value and the interrupt on alarm
  85. timer_set_alarm_value(timer.group, timer.idx, (HAL_TIMER_RATE) / timer.divider / frequency - 1);
  86. timer_enable_intr(timer.group, timer.idx);
  87. timer_isr_register(timer.group, timer.idx, timer_isr, (void*)timer_num, 0, nullptr);
  88. timer_start(timer.group, timer.idx);
  89. }
  90. /**
  91. * Set the upper value of the timer, when the timer reaches this upper value the
  92. * interrupt should be triggered and the counter reset
  93. * @param timer_num timer number to set the count to
  94. * @param count threshold at which the interrupt is triggered
  95. */
  96. void HAL_timer_set_compare(const uint8_t timer_num, hal_timer_t count) {
  97. const tTimerConfig timer = TimerConfig[timer_num];
  98. timer_set_alarm_value(timer.group, timer.idx, count);
  99. }
  100. /**
  101. * Get the current upper value of the timer
  102. * @param timer_num timer number to get the count from
  103. * @return the timer current threshold for the alarm to be triggered
  104. */
  105. hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  106. const tTimerConfig timer = TimerConfig[timer_num];
  107. uint64_t alarm_value;
  108. timer_get_alarm_value(timer.group, timer.idx, &alarm_value);
  109. return alarm_value;
  110. }
  111. /**
  112. * Get the current counter value between 0 and the maximum count (HAL_timer_set_count)
  113. * @param timer_num timer number to get the current count
  114. * @return the current counter of the alarm
  115. */
  116. hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  117. const tTimerConfig timer = TimerConfig[timer_num];
  118. uint64_t counter_value;
  119. timer_get_counter_value(timer.group, timer.idx, &counter_value);
  120. return counter_value;
  121. }
  122. /**
  123. * Enable timer interrupt on the timer
  124. * @param timer_num timer number to enable interrupts on
  125. */
  126. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  127. //const tTimerConfig timer = TimerConfig[timer_num];
  128. //timer_enable_intr(timer.group, timer.idx);
  129. }
  130. /**
  131. * Disable timer interrupt on the timer
  132. * @param timer_num timer number to disable interrupts on
  133. */
  134. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  135. //const tTimerConfig timer = TimerConfig[timer_num];
  136. //timer_disable_intr(timer.group, timer.idx);
  137. }
  138. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  139. const tTimerConfig timer = TimerConfig[timer_num];
  140. return TG[timer.group]->int_ena.val | BIT(timer_num);
  141. }
  142. #endif // ARDUINO_ARCH_ESP32