You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ender3V2S1/Marlin/src/HAL/AVR/Servo.cpp

227 lines
8.5 KiB
C++

**New Year 2024 (20240107)** * The background of hot-end icon and bed icon blinks when they are being heating * Fix for https://github.com/mriscoc/Ender3V2S1/issues/1175 * Adds mesh inset to the mesh validation test * Fix screen background in trammingWizard regardless an error in the first corner **New in source code:** * Enabled M48 menu item only if Z_MIN_PROBE_REPEATABILITY_TEST is enabled * Moves Bed size menu items after XY MIN/MAX position * Enables the storage of parameters in the EEPROM only if the respective feature is enabled **From Marlin Bugfix** * Slimmer null T command (#26615) * Followup to optional M111/M115 (#26626) * Fix hangs in DUE native USB (#26572) * Fix Bed PID Autotune output (#26606) * CONFIGURE_FILAMENT_CHANGE - Optional M603 (#26613) * I3DBEE TECH Beez Mini 12864 (#26596) * Options to slim M111, remove M115 (#26603) * BSD string workaround (#26532) * Fix homing with FT_MOTION (#26595) * Fix, extend FAN / AUTOFAN confict check (#26591) * BigTreeTech Manta M8P V2.0 (STM32H723ZE) (#26578) * Optimize FT_MOTION (#26557) * TriGorilla Pro default to ONBOARD_SDIO (#26586) * Specify U8glib-HAL@0.5.4 (#26582) * Newer Platform & Toolchain for STM32G0 (#26585) * Initial support for HC32 U8G LCD (#26568) * Move U8G defines to HALs * BigTreeTech Kraken V1.0 (STM32H723ZG) (#26565) * Fix string buffer warning (#26550) * Fix MARKFORGED_INVERSE typos (#26558) * Creality Free Runs fixups (#26562) * Orca 3D Modular Controller (#26534) * Jerk correction for LIN_ADVANCE + CLASSIC_JERK (#26551) * Optional FAN_INVERTING * Just "warn" on SD fail * FT_MOTION improvements (#26074) * Fix Creality E3 "Free-runs" (#26533) * Creality E3 Free-runs Silent Motherboard (#25636) * Fix planner jerk limits (#26529) * MARKFORGED_INVERSE (#26516) * Fix MKS TS35 with BTT SKR 1.3/1.4 (#26176) * SERIAL_DMA (for some STM32Fx) (#26328) * Adjust Progress / Completion (#26466) * Encoder improvements (#26501) * Use strlcpy with buffer size (#26513) * Use PIO versioning (including HC32) (#26512) * Voxelab Aquila N32 (via Maple) (#26470) * Fix tool-change E prime (#26494) * Fix thermistor 14 & 60 constexprness (#26499) * UI refresh for some events (#26487) * Fix a NeoPixel override (#26492) * Fix ftostrX3sign (#26497) * DOUBLE_LCD_FRAMERATE (#26500) * Fix some action labels (#26490) * More num-to-string digits / precisions (#26343) * Fix BLTouch HSMode deploy (#26311) * Touch fixes (#26455) * XY_AFTER_HOMING, EVENT_GCODE_AFTER_HOMING (#26469) * BlackBeezMini 3D by I3DBEE (#26406) * HAL for HC32F460 (#26414) * MAX Thermocouples for Heated Bed (#26441) * Pins for FYSETC Spider King 4.07 (#26461) * Use ftpl for item strings (#26462) * BD_SENSOR_PROBE_NO_STOP (#26353) * Fix PANDA ZHU missing #endif (#26460) * Update Teensy 4.0/4.1 Serial (#26457) * Configurable FR/Flow edit ranges (#26446) * Fix MMU2 sscanf bug, optimize (#26449) * Fix GT2560_V41b Z2 pin (#26370) * BTT Octopus Pro V1.0.1 (STM32H723ZE) (#26159) * Fix Ender-5 S1 env * Fix more MarlinUI title centering (#26440) * Fix MarlinUI axis move / UBL strings (#26439) * Minor touch calibration improvement (#26445) * Fix rotational AxisFlags (#26444) * Rotational move items (#26438) * Define MarlinUI axis moves with lists (#26344) * Creality STM32F401RC w/out bootloader (#26373) * Fix runout state in menu (#26394) And many other Bug fixes and improvements from the Marlin bugfix branch
5 months ago
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
* Copyright (c) 2009 Michael Margolis. All right reserved.
*/
/**
* A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method.
* The servos are pulsed in the background using the value most recently written using the write() method
*
* Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached.
* Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four.
*
* The methods are:
*
* Servo - Class for manipulating servo motors connected to Arduino pins.
*
* attach(pin) - Attach a servo motor to an i/o pin.
* attach(pin, min, max) - Attach to a pin, setting min and max values in microseconds
* Default min is 544, max is 2400
*
* write() - Set the servo angle in degrees. (Invalid angles over MIN_PULSE_WIDTH are treated as µs.)
* writeMicroseconds() - Set the servo pulse width in microseconds.
* move(pin, angle) - Sequence of attach(pin), write(angle), safe_delay(servo_delay[servoIndex]).
* With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after servo_delay[servoIndex].
* read() - Get the last-written servo pulse width as an angle between 0 and 180.
* readMicroseconds() - Get the last-written servo pulse width in microseconds.
* attached() - Return true if a servo is attached.
* detach() - Stop an attached servo from pulsing its i/o pin.
*/
#ifdef __AVR__
#include "../../inc/MarlinConfig.h"
#if HAS_SERVOS
#include <avr/interrupt.h>
#include "../shared/servo.h"
#include "../shared/servo_private.h"
static volatile int8_t Channel[_Nbr_16timers]; // counter for the servo being pulsed for each timer (or -1 if refresh interval)
/************ static functions common to all instances ***********************/
static inline void handle_interrupts(const timer16_Sequence_t timer, volatile uint16_t* TCNTn, volatile uint16_t* OCRnA) {
int8_t cho = Channel[timer]; // Handle the prior Channel[timer] first
if (cho < 0) // Channel -1 indicates the refresh interval completed...
*TCNTn = 0; // ...so reset the timer
else if (SERVO_INDEX(timer, cho) < ServoCount) // prior channel handled?
extDigitalWrite(SERVO(timer, cho).Pin.nbr, LOW); // pulse the prior channel LOW
Channel[timer] = ++cho; // Handle the next channel (or 0)
if (cho < SERVOS_PER_TIMER && SERVO_INDEX(timer, cho) < ServoCount) {
*OCRnA = *TCNTn + SERVO(timer, cho).ticks; // set compare to current ticks plus duration
if (SERVO(timer, cho).Pin.isActive) // activated?
extDigitalWrite(SERVO(timer, cho).Pin.nbr, HIGH); // yes: pulse HIGH
}
else {
// finished all channels so wait for the refresh period to expire before starting over
const unsigned int cval = ((unsigned)*TCNTn) + 32 / (SERVO_TIMER_PRESCALER), // allow 32 cycles to ensure the next OCR1A not missed
ival = (unsigned int)usToTicks(REFRESH_INTERVAL); // at least REFRESH_INTERVAL has elapsed
*OCRnA = max(cval, ival);
Channel[timer] = -1; // reset the timer counter to 0 on the next call
}
}
#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform
// Interrupt handlers for Arduino
#ifdef _useTimer1
SIGNAL(TIMER1_COMPA_vect) { handle_interrupts(_timer1, &TCNT1, &OCR1A); }
#endif
#ifdef _useTimer3
SIGNAL(TIMER3_COMPA_vect) { handle_interrupts(_timer3, &TCNT3, &OCR3A); }
#endif
#ifdef _useTimer4
SIGNAL(TIMER4_COMPA_vect) { handle_interrupts(_timer4, &TCNT4, &OCR4A); }
#endif
#ifdef _useTimer5
SIGNAL(TIMER5_COMPA_vect) { handle_interrupts(_timer5, &TCNT5, &OCR5A); }
#endif
#else // WIRING
// Interrupt handlers for Wiring
#ifdef _useTimer1
void Timer1Service() { handle_interrupts(_timer1, &TCNT1, &OCR1A); }
#endif
#ifdef _useTimer3
void Timer3Service() { handle_interrupts(_timer3, &TCNT3, &OCR3A); }
#endif
#endif // WIRING
/****************** end of static functions ******************************/
void initISR(const timer16_Sequence_t timer_index) {
switch (timer_index) {
default: break;
#ifdef _useTimer1
case _timer1:
TCCR1A = 0; // normal counting mode
TCCR1B = _BV(CS11); // set prescaler of 8
TCNT1 = 0; // clear the timer count
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
SBI(TIFR, OCF1A); // clear any pending interrupts;
SBI(TIMSK, OCIE1A); // enable the output compare interrupt
#else
// here if not ATmega8 or ATmega128
SBI(TIFR1, OCF1A); // clear any pending interrupts;
SBI(TIMSK1, OCIE1A); // enable the output compare interrupt
#endif
#ifdef WIRING
timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service);
#endif
break;
#endif
#ifdef _useTimer3
case _timer3:
TCCR3A = 0; // normal counting mode
TCCR3B = _BV(CS31); // set prescaler of 8
TCNT3 = 0; // clear the timer count
#ifdef __AVR_ATmega128__
SBI(TIFR, OCF3A); // clear any pending interrupts;
SBI(ETIMSK, OCIE3A); // enable the output compare interrupt
#else
SBI(TIFR3, OCF3A); // clear any pending interrupts;
SBI(TIMSK3, OCIE3A); // enable the output compare interrupt
#endif
#ifdef WIRING
timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only
#endif
break;
#endif
#ifdef _useTimer4
case _timer4:
TCCR4A = 0; // normal counting mode
TCCR4B = _BV(CS41); // set prescaler of 8
TCNT4 = 0; // clear the timer count
TIFR4 = _BV(OCF4A); // clear any pending interrupts;
TIMSK4 = _BV(OCIE4A); // enable the output compare interrupt
break;
#endif
#ifdef _useTimer5
case _timer5:
TCCR5A = 0; // normal counting mode
TCCR5B = _BV(CS51); // set prescaler of 8
TCNT5 = 0; // clear the timer count
TIFR5 = _BV(OCF5A); // clear any pending interrupts;
TIMSK5 = _BV(OCIE5A); // enable the output compare interrupt
break;
#endif
}
}
void finISR(const timer16_Sequence_t timer_index) {
// Disable use of the given timer
#ifdef WIRING
switch (timer_index) {
default: break;
case _timer1:
CBI(
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
TIMSK1
#else
TIMSK
#endif
, OCIE1A // disable timer 1 output compare interrupt
);
timerDetach(TIMER1OUTCOMPAREA_INT);
break;
case _timer3:
CBI(
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
TIMSK3
#else
ETIMSK
#endif
, OCIE3A // disable the timer3 output compare A interrupt
);
timerDetach(TIMER3OUTCOMPAREA_INT);
break;
}
#else // !WIRING
// For arduino - in future: call here to a currently undefined function to reset the timer
UNUSED(timer_index);
#endif
}
#endif // HAS_SERVOS
#endif // __AVR__