Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for arduinostm32 4.108 #17970

Closed
Closed
14 changes: 11 additions & 3 deletions Marlin/src/HAL/STM32/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
//
HardwareTimer SoftwareSerial::timer(TIMER_SERIAL);
const IRQn_Type SoftwareSerial::timer_interrupt_number = static_cast<IRQn_Type>(getTimerUpIrq(TIMER_SERIAL));
uint32_t SoftwareSerial::timer_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), TIM_IRQ_PRIO, TIM_IRQ_SUBPRIO);
SoftwareSerial *SoftwareSerial::active_listener = nullptr;
SoftwareSerial *volatile SoftwareSerial::active_out = nullptr;
SoftwareSerial *volatile SoftwareSerial::active_in = nullptr;
Expand All @@ -113,7 +112,13 @@ int32_t SoftwareSerial::rx_bit_cnt = -1; // rx_bit_cnt = -1 : waiting for start
uint32_t SoftwareSerial::cur_speed = 0;

void SoftwareSerial::setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority) {
timer_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), preemptPriority, subPriority);
timer.setInterruptPriority(preemptPriority, subPriority);

// Arduino_Core_STM32 1.8 does not apply the interrupt priority until the
// timer is completely re-initialized, which may not even be possible
// through the HardwareTimer interface. Work around this until it is
// fixed by setting directly to the STM32 HAL.
NVIC_SetPriority(timer_interrupt_number, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), preemptPriority, subPriority));
}

//
Expand All @@ -137,12 +142,15 @@ void SoftwareSerial::setSpeed(uint32_t speed) {
pre *= 2;
}
} while (cmp_value >= UINT16_MAX);
// Arduino_Core_STM32 1.8 requires setMode to be called on a channel to start the timer.
// This is fixed in the following PR, which has not yet been released.
// https://github.com/stm32duino/Arduino_Core_STM32/pull/849
timer.setMode(1, TIMER_OUTPUT_COMPARE, NC);
timer.setPrescaleFactor(pre);
timer.setOverflow(cmp_value);
timer.setCount(0);
timer.attachInterrupt(&handleInterrupt);
timer.resume();
NVIC_SetPriority(timer_interrupt_number, timer_interrupt_priority);
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
}
else
timer.detachInterrupt();
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/HAL/STM32/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class SoftwareSerial : public Stream {
// static data
static HardwareTimer timer;
static const IRQn_Type timer_interrupt_number;
static uint32_t timer_interrupt_priority;
static SoftwareSerial *active_listener;
static SoftwareSerial *volatile active_out;
static SoftwareSerial *volatile active_in;
Expand Down
48 changes: 24 additions & 24 deletions Marlin/src/HAL/STM32/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
// ------------------------

HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { NULL };
bool timer_enabled[NUM_HARDWARE_TIMERS] = { false };

// ------------------------
// Public functions
Expand All @@ -110,44 +109,57 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
* which changes the prescaler when an IRQ frequency change is needed
* (for example when steppers are turned on)
*/

// Arduino_Core_STM32 1.8 requires setMode to be called on a channel to start the timer.
// This is fixed in the following PR, which has not yet been released.
// https://github.com/stm32duino/Arduino_Core_STM32/pull/849
timer_instance[timer_num]->setMode(1, TIMER_OUTPUT_COMPARE, NC);
timer_instance[timer_num]->setPrescaleFactor(STEPPER_TIMER_PRESCALE); //the -1 is done internally
timer_instance[timer_num]->setOverflow(_MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (HAL_TIMER_RATE) / (STEPPER_TIMER_PRESCALE) /* /frequency */), TICK_FORMAT);
break;
case TEMP_TIMER_NUM: // TEMP TIMER - any available 16bit timer
timer_instance[timer_num] = new HardwareTimer(TEMP_TIMER_DEV);

// Arduino_Core_STM32 1.8 requires setMode to be called on a channel to start the timer.
// This is fixed in the following PR, which has not yet been released.
// https://github.com/stm32duino/Arduino_Core_STM32/pull/849
timer_instance[timer_num]->setMode(1, TIMER_OUTPUT_COMPARE, NC);
// The prescale factor is computed automatically for HERTZ_FORMAT
timer_instance[timer_num]->setOverflow(frequency, HERTZ_FORMAT);
break;
}

HAL_timer_enable_interrupt(timer_num);

/*
* Initializes (and unfortunately starts) the timer.
* This is needed to set correct IRQ priority at the moment but causes
* no harm since every call to HAL_timer_start() is actually followed by
* a call to HAL_timer_enable_interrupt() which means that there isn't
* a case in which you want the timer to run without a callback.
*/
// Start the timer.
timer_instance[timer_num]->resume(); // First call to resume() MUST follow the attachInterrupt()

// This is fixed in Arduino_Core_STM32 1.8.
// These calls can be removed and replaced with
// timer_instance[timer_num]->setInterruptPriority
switch (timer_num) {
case STEP_TIMER_NUM:
timer_instance[timer_num]->setInterruptPriority(STEP_TIMER_IRQ_PRIO, 0);

// Arduino_Core_STM32 1.8 does not apply the interrupt priority until the
// timer is completely re-initialized, which never happens in this code.
// Explicitly set the interrupt priority until fixed in a future Arduino_Core_STM32.
HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0);
break;
case TEMP_TIMER_NUM:
timer_instance[timer_num]->setInterruptPriority(TEMP_TIMER_IRQ_PRIO, 0);

// Arduino_Core_STM32 1.8 does not apply the interrupt priority until the
// timer is completely re-initialized, which never happens in this code.
// Explicitly set the interrupt priority until fixed in a future Arduino_Core_STM32.
HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0);
break;
}
}
}

void HAL_timer_enable_interrupt(const uint8_t timer_num) {
if (HAL_timer_initialized(timer_num) && !timer_enabled[timer_num]) {
timer_enabled[timer_num] = true;
if (HAL_timer_initialized(timer_num) && !timer_instance[timer_num]->hasInterrupt()) {
switch (timer_num) {
case STEP_TIMER_NUM:
timer_instance[timer_num]->attachInterrupt(Step_Handler);
Expand All @@ -160,23 +172,11 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num) {
}

void HAL_timer_disable_interrupt(const uint8_t timer_num) {
if (HAL_timer_interrupt_enabled(timer_num)) {
timer_instance[timer_num]->detachInterrupt();
timer_enabled[timer_num] = false;
}
if (HAL_timer_initialized(timer_num)) timer_instance[timer_num]->detachInterrupt();
}

bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
return HAL_timer_initialized(timer_num) && timer_enabled[timer_num];
}

// Only for use within the HAL
TIM_TypeDef * HAL_timer_device(const uint8_t timer_num) {
switch (timer_num) {
case STEP_TIMER_NUM: return STEP_TIMER_DEV;
case TEMP_TIMER_NUM: return TEMP_TIMER_DEV;
}
return nullptr;
return HAL_timer_initialized(timer_num) && timer_instance[timer_num]->hasInterrupt();
}

void SetSoftwareSerialTimerInterruptPriority() {
Expand Down
2 changes: 0 additions & 2 deletions Marlin/src/HAL/STM32/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
// Exposed here to allow all timer priority information to reside in timers.cpp
void SetSoftwareSerialTimerInterruptPriority();

//TIM_TypeDef* HAL_timer_device(const uint8_t timer_num); no need to be public for now. not public = not used externally

// FORCE_INLINE because these are used in performance-critical situations
FORCE_INLINE bool HAL_timer_initialized(const uint8_t timer_num) {
return timer_instance[timer_num] != NULL;
Expand Down
2 changes: 1 addition & 1 deletion buildroot/share/PlatformIO/boards/malyanM200v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"extra_flags": "-DSTM32F070xB",
"f_cpu": "48000000L",
"mcu": "stm32f070rbt6",
"variant": "MALYANM200_F070CB",
"variant": "MALYANMx00_F070CB",
"vec_tab_addr": "0x8002000"
},
"debug": {
Expand Down
4 changes: 2 additions & 2 deletions buildroot/share/PlatformIO/variants/BIGTREE_GTR_V1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ extern "C" {

// Timer Definitions
//Do not use timer used by PWM pins when possible. See PinMap_PWM in PeripheralPins.c
#define TIMER_TONE TIM2
#define TIMER_TONE TIM7
#define TIMER_SERVO TIM5 // Only 1 Servo PIN on SKR-PRO, so use the same timer as defined in PeripheralPins
#define TIMER_SERIAL TIM7
#define TIMER_SERIAL TIM2

// UART Definitions
//#define ENABLE_HWSERIAL1 done automatically by the #define SERIAL_UART_INSTANCE below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ extern "C" {

// Timer Definitions
//Do not use timer used by PWM pins when possible. See PinMap_PWM in PeripheralPins.c
#define TIMER_TONE TIM2
#define TIMER_TONE TIM7
#define TIMER_SERVO TIM5 // Only 1 Servo PIN on SKR-PRO, so use the same timer as defined in PeripheralPins
#define TIMER_SERIAL TIM7
#define TIMER_SERIAL TIM2

// UART Definitions
//#define ENABLE_HWSERIAL1 done automatically by the #define SERIAL_UART_INSTANCE below
Expand Down
Loading