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.

Servo.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "../platforms.h"
  24. #ifdef HAL_STM32
  25. #include "../../inc/MarlinConfig.h"
  26. #if HAS_SERVOS
  27. #include "Servo.h"
  28. static uint_fast8_t servoCount = 0;
  29. static libServo *servos[NUM_SERVOS] = {0};
  30. constexpr millis_t servoDelay[] = SERVO_DELAY;
  31. static_assert(COUNT(servoDelay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
  32. // Initialize to the default timer priority. This will be overridden by a call from timers.cpp.
  33. // This allows all timer interrupt priorities to be managed from a single location in the HAL.
  34. static uint32_t servo_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), TIM_IRQ_PRIO, TIM_IRQ_SUBPRIO);
  35. // This must be called after the STM32 Servo class has initialized the timer.
  36. // It may only be needed after the first call to attach(), but it is possible
  37. // that is is necessary after every detach() call. To be safe this is currently
  38. // called after every call to attach().
  39. static void fixServoTimerInterruptPriority() {
  40. NVIC_SetPriority(getTimerUpIrq(TIMER_SERVO), servo_interrupt_priority);
  41. }
  42. libServo::libServo()
  43. : delay(servoDelay[servoCount]),
  44. was_attached_before_pause(false),
  45. value_before_pause(0)
  46. {
  47. servos[servoCount++] = this;
  48. }
  49. int8_t libServo::attach(const int pin) {
  50. if (servoCount >= MAX_SERVOS) return -1;
  51. if (pin > 0) servo_pin = pin;
  52. auto result = stm32_servo.attach(servo_pin);
  53. fixServoTimerInterruptPriority();
  54. return result;
  55. }
  56. int8_t libServo::attach(const int pin, const int min, const int max) {
  57. if (servoCount >= MAX_SERVOS) return -1;
  58. if (pin > 0) servo_pin = pin;
  59. auto result = stm32_servo.attach(servo_pin, min, max);
  60. fixServoTimerInterruptPriority();
  61. return result;
  62. }
  63. void libServo::move(const int value) {
  64. if (attach(0) >= 0) {
  65. stm32_servo.write(value);
  66. safe_delay(delay);
  67. TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach());
  68. }
  69. }
  70. void libServo::pause() {
  71. was_attached_before_pause = stm32_servo.attached();
  72. if (was_attached_before_pause) {
  73. value_before_pause = stm32_servo.read();
  74. stm32_servo.detach();
  75. }
  76. }
  77. void libServo::resume() {
  78. if (was_attached_before_pause) {
  79. attach();
  80. move(value_before_pause);
  81. }
  82. }
  83. void libServo::pause_all_servos() {
  84. for (auto& servo : servos)
  85. if (servo) servo->pause();
  86. }
  87. void libServo::resume_all_servos() {
  88. for (auto& servo : servos)
  89. if (servo) servo->resume();
  90. }
  91. void libServo::setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority) {
  92. servo_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), preemptPriority, subPriority);
  93. }
  94. #endif // HAS_SERVOS
  95. #endif // HAL_STM32