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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1)
  24. #include "../../inc/MarlinConfig.h"
  25. #if HAS_SERVOS
  26. #include "Servo.h"
  27. static uint_fast8_t servoCount = 0;
  28. static libServo *servos[NUM_SERVOS] = {0};
  29. constexpr millis_t servoDelay[] = SERVO_DELAY;
  30. static_assert(COUNT(servoDelay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
  31. // Initialize to the default timer priority. This will be overridden by a call from timers.cpp.
  32. // This allows all timer interrupt priorities to be managed from a single location in the HAL.
  33. static uint32_t servo_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), TIM_IRQ_PRIO, TIM_IRQ_SUBPRIO);
  34. // This must be called after the STM32 Servo class has initialized the timer.
  35. // It may only be needed after the first call to attach(), but it is possible
  36. // that is is necessary after every detach() call. To be safe this is currently
  37. // called after every call to attach().
  38. static void fixServoTimerInterruptPriority() {
  39. NVIC_SetPriority(getTimerUpIrq(TIMER_SERVO), servo_interrupt_priority);
  40. }
  41. libServo::libServo()
  42. : delay(servoDelay[servoCount]),
  43. was_attached_before_pause(false),
  44. value_before_pause(0)
  45. {
  46. servos[servoCount++] = this;
  47. }
  48. int8_t libServo::attach(const int pin) {
  49. if (servoCount >= MAX_SERVOS) return -1;
  50. if (pin > 0) servo_pin = pin;
  51. auto result = stm32_servo.attach(servo_pin);
  52. fixServoTimerInterruptPriority();
  53. return result;
  54. }
  55. int8_t libServo::attach(const int pin, const int min, const int max) {
  56. if (servoCount >= MAX_SERVOS) return -1;
  57. if (pin > 0) servo_pin = pin;
  58. auto result = stm32_servo.attach(servo_pin, min, max);
  59. fixServoTimerInterruptPriority();
  60. return result;
  61. }
  62. void libServo::move(const int value) {
  63. if (attach(0) >= 0) {
  64. stm32_servo.write(value);
  65. safe_delay(delay);
  66. TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach());
  67. }
  68. }
  69. void libServo::pause() {
  70. was_attached_before_pause = stm32_servo.attached();
  71. if (was_attached_before_pause) {
  72. value_before_pause = stm32_servo.read();
  73. stm32_servo.detach();
  74. }
  75. }
  76. void libServo::resume() {
  77. if (was_attached_before_pause) {
  78. attach();
  79. move(value_before_pause);
  80. }
  81. }
  82. void libServo::pause_all_servos() {
  83. for (auto& servo : servos)
  84. if (servo) servo->pause();
  85. }
  86. void libServo::resume_all_servos() {
  87. for (auto& servo : servos)
  88. if (servo) servo->resume();
  89. }
  90. void libServo::setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority) {
  91. servo_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), preemptPriority, subPriority);
  92. }
  93. #endif // HAS_SERVOS
  94. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1