Implement HAL and apply macros across code-base
Implement AVR Platform
This commit is contained in:
committed by
Scott Lahteine
parent
fb04dfcda8
commit
4b16fa3272
@@ -48,11 +48,16 @@
|
||||
#include "stepper.h"
|
||||
#include "endstops.h"
|
||||
#include "planner.h"
|
||||
#if MB(ALLIGATOR)
|
||||
#include "dac_dac084s085.h"
|
||||
#endif
|
||||
#include "temperature.h"
|
||||
#include "ultralcd.h"
|
||||
#include "language.h"
|
||||
#include "cardreader.h"
|
||||
#include "speed_lookuptable.h"
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
#include "speed_lookuptable.h"
|
||||
#endif
|
||||
|
||||
#if HAS_DIGIPOTSS
|
||||
#include <SPI.h>
|
||||
@@ -99,11 +104,11 @@ volatile uint32_t Stepper::step_events_completed = 0; // The number of step even
|
||||
|
||||
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
||||
|
||||
constexpr uint16_t ADV_NEVER = 65535;
|
||||
constexpr HAL_TIMER_TYPE ADV_NEVER = HAL_TIMER_TYPE_MAX;
|
||||
|
||||
uint16_t Stepper::nextMainISR = 0,
|
||||
Stepper::nextAdvanceISR = ADV_NEVER,
|
||||
Stepper::eISR_Rate = ADV_NEVER;
|
||||
HAL_TIMER_TYPE Stepper::nextMainISR = 0,
|
||||
Stepper::nextAdvanceISR = ADV_NEVER,
|
||||
Stepper::eISR_Rate = ADV_NEVER;
|
||||
|
||||
#if ENABLED(LIN_ADVANCE)
|
||||
volatile int Stepper::e_steps[E_STEPPERS];
|
||||
@@ -144,9 +149,9 @@ volatile signed char Stepper::count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
|
||||
long Stepper::counter_m[MIXING_STEPPERS];
|
||||
#endif
|
||||
|
||||
unsigned short Stepper::acc_step_rate; // needed for deceleration start point
|
||||
HAL_TIMER_TYPE Stepper::acc_step_rate; // needed for deceleration start point
|
||||
uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
|
||||
unsigned short Stepper::OCR1A_nominal;
|
||||
HAL_TIMER_TYPE Stepper::OCR1A_nominal;
|
||||
|
||||
volatile long Stepper::endstops_trigsteps[XYZ];
|
||||
|
||||
@@ -213,66 +218,7 @@ volatile long Stepper::endstops_trigsteps[XYZ];
|
||||
#define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
|
||||
#endif
|
||||
|
||||
// intRes = longIn1 * longIn2 >> 24
|
||||
// uses:
|
||||
// r26 to store 0
|
||||
// r27 to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
|
||||
// note that the lower two bytes and the upper byte of the 48bit result are not calculated.
|
||||
// this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
|
||||
// B0 A0 are bits 24-39 and are the returned value
|
||||
// C1 B1 A1 is longIn1
|
||||
// D2 C2 B2 A2 is longIn2
|
||||
//
|
||||
#define MultiU24X32toH16(intRes, longIn1, longIn2) \
|
||||
asm volatile ( \
|
||||
"clr r26 \n\t" \
|
||||
"mul %A1, %B2 \n\t" \
|
||||
"mov r27, r1 \n\t" \
|
||||
"mul %B1, %C2 \n\t" \
|
||||
"movw %A0, r0 \n\t" \
|
||||
"mul %C1, %C2 \n\t" \
|
||||
"add %B0, r0 \n\t" \
|
||||
"mul %C1, %B2 \n\t" \
|
||||
"add %A0, r0 \n\t" \
|
||||
"adc %B0, r1 \n\t" \
|
||||
"mul %A1, %C2 \n\t" \
|
||||
"add r27, r0 \n\t" \
|
||||
"adc %A0, r1 \n\t" \
|
||||
"adc %B0, r26 \n\t" \
|
||||
"mul %B1, %B2 \n\t" \
|
||||
"add r27, r0 \n\t" \
|
||||
"adc %A0, r1 \n\t" \
|
||||
"adc %B0, r26 \n\t" \
|
||||
"mul %C1, %A2 \n\t" \
|
||||
"add r27, r0 \n\t" \
|
||||
"adc %A0, r1 \n\t" \
|
||||
"adc %B0, r26 \n\t" \
|
||||
"mul %B1, %A2 \n\t" \
|
||||
"add r27, r1 \n\t" \
|
||||
"adc %A0, r26 \n\t" \
|
||||
"adc %B0, r26 \n\t" \
|
||||
"lsr r27 \n\t" \
|
||||
"adc %A0, r26 \n\t" \
|
||||
"adc %B0, r26 \n\t" \
|
||||
"mul %D2, %A1 \n\t" \
|
||||
"add %A0, r0 \n\t" \
|
||||
"adc %B0, r1 \n\t" \
|
||||
"mul %D2, %B1 \n\t" \
|
||||
"add %B0, r0 \n\t" \
|
||||
"clr r1 \n\t" \
|
||||
: \
|
||||
"=&r" (intRes) \
|
||||
: \
|
||||
"d" (longIn1), \
|
||||
"d" (longIn2) \
|
||||
: \
|
||||
"r26" , "r27" \
|
||||
)
|
||||
|
||||
// Some useful constants
|
||||
|
||||
#define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
|
||||
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
|
||||
|
||||
/**
|
||||
* __________________________
|
||||
@@ -345,6 +291,8 @@ void Stepper::set_directions() {
|
||||
* Stepper Driver Interrupt
|
||||
*
|
||||
* Directly pulses the stepper motors at high frequency.
|
||||
*
|
||||
* AVR :
|
||||
* Timer 1 runs at a base frequency of 2MHz, with this ISR using OCR1A compare mode.
|
||||
*
|
||||
* OCR1A Frequency
|
||||
@@ -355,7 +303,9 @@ void Stepper::set_directions() {
|
||||
* 2000 1 KHz - sleep rate
|
||||
* 4000 500 Hz - init rate
|
||||
*/
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
|
||||
HAL_STEP_TIMER_ISR {
|
||||
HAL_timer_isr_prologue(STEP_TIMER_NUM);
|
||||
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
||||
Stepper::advance_isr_scheduler();
|
||||
#else
|
||||
@@ -363,23 +313,23 @@ ISR(TIMER1_COMPA_vect) {
|
||||
#endif
|
||||
}
|
||||
|
||||
#define _ENABLE_ISRs() do { cli(); if (thermalManager.in_temp_isr) CBI(TIMSK0, OCIE0B); else SBI(TIMSK0, OCIE0B); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
|
||||
|
||||
void Stepper::isr() {
|
||||
|
||||
uint16_t ocr_val;
|
||||
HAL_TIMER_TYPE ocr_val;
|
||||
|
||||
#define ENDSTOP_NOMINAL_OCR_VAL 3000 // check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch
|
||||
#define OCR_VAL_TOLERANCE 1000 // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms
|
||||
|
||||
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
|
||||
// Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
|
||||
CBI(TIMSK0, OCIE0B); // Temperature ISR
|
||||
DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
|
||||
DISABLE_STEPPER_DRIVER_INTERRUPT();
|
||||
sei();
|
||||
#if !defined(CPU_32_BIT)
|
||||
sei();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define _SPLIT(L) (ocr_val = (uint16_t)L)
|
||||
#define _SPLIT(L) (ocr_val = (HAL_TIMER_TYPE)L)
|
||||
#if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
|
||||
#define SPLIT(L) _SPLIT(L)
|
||||
#else // sample endstops in between step pulses
|
||||
@@ -405,10 +355,13 @@ void Stepper::isr() {
|
||||
}
|
||||
|
||||
_NEXT_ISR(ocr_val);
|
||||
#ifdef CPU_32_BIT
|
||||
//todo: HAL?
|
||||
#else
|
||||
NOLESS(OCR1A, TCNT1 + 16);
|
||||
#endif
|
||||
|
||||
NOLESS(OCR1A, TCNT1 + 16);
|
||||
|
||||
_ENABLE_ISRs(); // re-enable ISRs
|
||||
HAL_ENABLE_ISRs(); // re-enable ISRs
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@@ -420,8 +373,8 @@ void Stepper::isr() {
|
||||
#ifdef SD_FINISHED_RELEASECOMMAND
|
||||
if (!cleaning_buffer_counter && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
|
||||
#endif
|
||||
_NEXT_ISR(200); // Run at max speed - 10 KHz
|
||||
_ENABLE_ISRs(); // re-enable ISRs
|
||||
_NEXT_ISR(HAL_STEPPER_TIMER_RATE / 10000); // Run at max speed - 10 KHz
|
||||
HAL_ENABLE_ISRs(); // re-enable ISRs
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -450,8 +403,8 @@ void Stepper::isr() {
|
||||
#if ENABLED(Z_LATE_ENABLE)
|
||||
if (current_block->steps[Z_AXIS] > 0) {
|
||||
enable_Z();
|
||||
_NEXT_ISR(2000); // Run at slow speed - 1 KHz
|
||||
_ENABLE_ISRs(); // re-enable ISRs
|
||||
_NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
|
||||
HAL_ENABLE_ISRs(); // re-enable ISRs
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@@ -461,8 +414,8 @@ void Stepper::isr() {
|
||||
// #endif
|
||||
}
|
||||
else {
|
||||
_NEXT_ISR(2000); // Run at slow speed - 1 KHz
|
||||
_ENABLE_ISRs(); // re-enable ISRs
|
||||
_NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
|
||||
HAL_ENABLE_ISRs(); // re-enable ISRs
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -613,7 +566,7 @@ void Stepper::isr() {
|
||||
* 10µs = 160 or 200 cycles.
|
||||
*/
|
||||
#if EXTRA_CYCLES_XYZE > 20
|
||||
uint32_t pulse_start = TCNT0;
|
||||
uint32_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM);
|
||||
#endif
|
||||
|
||||
#if HAS_X_STEP
|
||||
@@ -645,8 +598,8 @@ void Stepper::isr() {
|
||||
|
||||
// For minimum pulse time wait before stopping pulses
|
||||
#if EXTRA_CYCLES_XYZE > 20
|
||||
while (EXTRA_CYCLES_XYZE > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
|
||||
pulse_start = TCNT0;
|
||||
while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ }
|
||||
pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM);
|
||||
#elif EXTRA_CYCLES_XYZE > 0
|
||||
DELAY_NOPS(EXTRA_CYCLES_XYZE);
|
||||
#endif
|
||||
@@ -686,7 +639,7 @@ void Stepper::isr() {
|
||||
|
||||
// For minimum pulse time wait after stopping pulses also
|
||||
#if EXTRA_CYCLES_XYZE > 20
|
||||
if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
|
||||
if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ }
|
||||
#elif EXTRA_CYCLES_XYZE > 0
|
||||
if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
|
||||
#endif
|
||||
@@ -716,14 +669,18 @@ void Stepper::isr() {
|
||||
// Calculate new timer value
|
||||
if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
|
||||
|
||||
MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
|
||||
#ifdef CPU_32_BIT
|
||||
MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate);
|
||||
#else
|
||||
MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
|
||||
#endif
|
||||
acc_step_rate += current_block->initial_rate;
|
||||
|
||||
// upper limit
|
||||
NOMORE(acc_step_rate, current_block->nominal_rate);
|
||||
|
||||
// step_rate to timer interval
|
||||
const uint16_t timer = calc_timer(acc_step_rate);
|
||||
const HAL_TIMER_TYPE timer = calc_timer(acc_step_rate);
|
||||
|
||||
SPLIT(timer); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
|
||||
_NEXT_ISR(ocr_val);
|
||||
@@ -764,12 +721,17 @@ void Stepper::isr() {
|
||||
#endif // ADVANCE or LIN_ADVANCE
|
||||
|
||||
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
||||
// TODO: HAL
|
||||
eISR_Rate = adv_rate(e_steps[TOOL_E_INDEX], timer, step_loops);
|
||||
#endif
|
||||
}
|
||||
else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
|
||||
uint16_t step_rate;
|
||||
MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
|
||||
HAL_TIMER_TYPE step_rate;
|
||||
#ifdef CPU_32_BIT
|
||||
MultiU32X24toH32(step_rate, deceleration_time, current_block->acceleration_rate);
|
||||
#else
|
||||
MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
|
||||
#endif
|
||||
|
||||
if (step_rate < acc_step_rate) { // Still decelerating?
|
||||
step_rate = acc_step_rate - step_rate;
|
||||
@@ -779,7 +741,7 @@ void Stepper::isr() {
|
||||
step_rate = current_block->final_rate;
|
||||
|
||||
// step_rate to timer interval
|
||||
const uint16_t timer = calc_timer(step_rate);
|
||||
const HAL_TIMER_TYPE timer = calc_timer(step_rate);
|
||||
|
||||
SPLIT(timer); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
|
||||
_NEXT_ISR(ocr_val);
|
||||
@@ -834,13 +796,19 @@ void Stepper::isr() {
|
||||
|
||||
SPLIT(OCR1A_nominal); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
|
||||
_NEXT_ISR(ocr_val);
|
||||
|
||||
// ensure we're running at the correct step rate, even if we just came off an acceleration
|
||||
step_loops = step_loops_nominal;
|
||||
}
|
||||
|
||||
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
|
||||
NOLESS(OCR1A, TCNT1 + 16);
|
||||
#ifdef CPU_32_BIT
|
||||
// Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
|
||||
uint32_t stepper_timer_count = HAL_timer_get_count(STEP_TIMER_NUM);
|
||||
uint32_t stepper_timer_current_count = HAL_timer_get_current_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
|
||||
HAL_timer_set_count(STEP_TIMER_NUM, stepper_timer_count < stepper_timer_current_count ? stepper_timer_current_count : stepper_timer_count);
|
||||
#else
|
||||
NOLESS(OCR1A, TCNT1 + 16);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// If current block is finished, reset pointer
|
||||
@@ -849,7 +817,7 @@ void Stepper::isr() {
|
||||
planner.discard_current_block();
|
||||
}
|
||||
#if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
|
||||
_ENABLE_ISRs(); // re-enable ISRs
|
||||
HAL_ENABLE_ISRs(); // re-enable ISRs
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -951,7 +919,7 @@ void Stepper::isr() {
|
||||
|
||||
void Stepper::advance_isr_scheduler() {
|
||||
// Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
|
||||
CBI(TIMSK0, OCIE0B); // Temperature ISR
|
||||
DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
|
||||
DISABLE_STEPPER_DRIVER_INTERRUPT();
|
||||
sei();
|
||||
|
||||
@@ -984,7 +952,7 @@ void Stepper::isr() {
|
||||
NOLESS(OCR1A, TCNT1 + 16);
|
||||
|
||||
// Restore original ISR settings
|
||||
_ENABLE_ISRs();
|
||||
HAL_ENABLE_ISRs();
|
||||
}
|
||||
|
||||
#endif // ADVANCE or LIN_ADVANCE
|
||||
@@ -996,6 +964,15 @@ void Stepper::init() {
|
||||
digipot_init();
|
||||
#endif
|
||||
|
||||
#if MB(ALLIGATOR)
|
||||
const float motor_current[] = MOTOR_CURRENT;
|
||||
unsigned int digipot_motor = 0;
|
||||
for (uint8_t i = 0; i < 3 + EXTRUDERS; i++) {
|
||||
digipot_motor = 255 * (motor_current[i] / 2.5);
|
||||
dac084s085::setValue(i, digipot_motor);
|
||||
}
|
||||
#endif//MB(ALLIGATOR)
|
||||
|
||||
// Init Microstepping Pins
|
||||
#if HAS_MICROSTEPS
|
||||
microstep_init();
|
||||
@@ -1152,6 +1129,7 @@ void Stepper::init() {
|
||||
E_AXIS_INIT(4);
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
// waveform generation = 0100 = CTC
|
||||
SET_WGM(1, CTC_OCRnA);
|
||||
|
||||
@@ -1168,6 +1146,11 @@ void Stepper::init() {
|
||||
// Init Stepper ISR to 122 Hz for quick starting
|
||||
OCR1A = 0x4000;
|
||||
TCNT1 = 0;
|
||||
#else
|
||||
// Init Stepper ISR to 122 Hz for quick starting
|
||||
HAL_timer_start (STEP_TIMER_NUM, 122);
|
||||
#endif
|
||||
|
||||
ENABLE_STEPPER_DRIVER_INTERRUPT();
|
||||
|
||||
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
||||
@@ -1663,6 +1646,9 @@ void Stepper::report_positions() {
|
||||
case 4: microstep_ms(driver, MICROSTEP4); break;
|
||||
case 8: microstep_ms(driver, MICROSTEP8); break;
|
||||
case 16: microstep_ms(driver, MICROSTEP16); break;
|
||||
#if MB(ALLIGATOR)
|
||||
case 32: microstep_ms(driver, MICROSTEP32); break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user