2018-10-03 03:26:07 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-10-03 03:26:07 -05:00
|
|
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
|
|
|
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-12-03 06:55:49 -06:00
|
|
|
#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
|
|
|
|
|
2018-10-03 03:26:07 -05:00
|
|
|
|
|
|
|
#include "HAL.h"
|
|
|
|
|
|
|
|
#include "HAL_timers_STM32.h"
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
// Local defines
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
|
|
|
|
#define NUM_HARDWARE_TIMERS 2
|
|
|
|
|
|
|
|
//#define PRESCALER 1
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
// Private Variables
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
|
|
|
|
stm32f4_timer_t TimerHandle[NUM_HARDWARE_TIMERS];
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
// Public functions
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2018-10-30 18:55:18 -05:00
|
|
|
bool timers_initialized[NUM_HARDWARE_TIMERS] = { false };
|
2018-10-03 03:26:07 -05:00
|
|
|
|
|
|
|
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
|
|
|
|
|
2018-10-30 18:55:18 -05:00
|
|
|
if (!timers_initialized[timer_num]) {
|
2018-10-03 22:33:24 -05:00
|
|
|
uint32_t step_prescaler = STEPPER_TIMER_PRESCALE - 1,
|
2018-10-03 03:26:07 -05:00
|
|
|
temp_prescaler = TEMP_TIMER_PRESCALE - 1;
|
|
|
|
switch (timer_num) {
|
|
|
|
case STEP_TIMER_NUM:
|
|
|
|
// STEPPER TIMER - use a 32bit timer if possible
|
|
|
|
TimerHandle[timer_num].timer = STEP_TIMER_DEV;
|
|
|
|
TimerHandle[timer_num].irqHandle = Step_Handler;
|
|
|
|
TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler);
|
2018-10-16 06:42:41 -05:00
|
|
|
HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0);
|
2018-10-03 03:26:07 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TEMP_TIMER_NUM:
|
|
|
|
// TEMP TIMER - any available 16bit Timer
|
|
|
|
TimerHandle[timer_num].timer = TEMP_TIMER_DEV;
|
|
|
|
TimerHandle[timer_num].irqHandle = Temp_Handler;
|
|
|
|
TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / temp_prescaler) / frequency) - 1, temp_prescaler);
|
2018-10-16 06:42:41 -05:00
|
|
|
HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0);
|
2018-10-03 03:26:07 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-10-30 18:55:18 -05:00
|
|
|
timers_initialized[timer_num] = true;
|
2018-10-03 03:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_timer_enable_interrupt(const uint8_t timer_num) {
|
|
|
|
const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
|
|
|
|
HAL_NVIC_EnableIRQ(IRQ_Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_timer_disable_interrupt(const uint8_t timer_num) {
|
|
|
|
const IRQn_Type IRQ_Id = IRQn_Type(getTimerIrq(TimerHandle[timer_num].timer));
|
|
|
|
HAL_NVIC_DisableIRQ(IRQ_Id);
|
|
|
|
|
|
|
|
// We NEED memory barriers to ensure Interrupts are actually disabled!
|
|
|
|
// ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
|
|
|
|
__DSB();
|
|
|
|
__ISB();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
|
|
|
|
const uint32_t IRQ_Id = getTimerIrq(TimerHandle[timer_num].timer);
|
|
|
|
return NVIC->ISER[IRQ_Id >> 5] & _BV32(IRQ_Id & 0x1F);
|
|
|
|
}
|
|
|
|
|
2019-06-28 23:04:33 -05:00
|
|
|
#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC
|