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

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