Marlin_Firmware/Marlin/src/HAL/STM32_F4_F7/STM32F7/timers.cpp

131 lines
4.5 KiB
C++
Raw Normal View History

2018-01-11 15:29:08 -06:00
/**
* Marlin 3D Printer Firmware
*
2020-02-03 08:00:57 -06:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2018-01-11 15:29:08 -06: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/>.
*
*/
2019-07-09 23:54:34 -05:00
#if defined(STM32GENERIC) && defined(STM32F7)
2018-01-11 15:29:08 -06:00
2019-07-09 23:54:34 -05:00
#include "../HAL.h"
2019-09-02 19:49:58 -05:00
#include "timers.h"
2018-01-11 15:29:08 -06:00
2019-07-09 22:30:06 -05:00
// ------------------------
2018-01-11 15:29:08 -06:00
// Local defines
2019-07-09 22:30:06 -05:00
// ------------------------
2018-01-11 15:29:08 -06:00
#define NUM_HARDWARE_TIMERS 2
//#define PRESCALER 1
2019-07-09 23:54:34 -05:00
2019-07-09 22:30:06 -05:00
// ------------------------
2018-01-11 15:29:08 -06:00
// Private Variables
2019-07-09 22:30:06 -05:00
// ------------------------
2018-01-11 15:29:08 -06:00
tTimerConfig timerConfig[NUM_HARDWARE_TIMERS];
2019-07-09 22:30:06 -05:00
// ------------------------
2018-01-11 15:29:08 -06:00
// Public functions
2019-07-09 22:30:06 -05:00
// ------------------------
2018-01-11 15:29:08 -06:00
2019-07-09 23:54:34 -05:00
bool timers_initialized[NUM_HARDWARE_TIMERS] = { false };
2018-01-11 15:29:08 -06:00
2018-01-15 02:28:39 -06:00
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
2018-01-11 15:29:08 -06:00
2018-08-22 17:14:38 -05:00
if (!timers_initialized[timer_num]) {
2018-01-11 15:29:08 -06:00
switch (timer_num) {
case STEP_TIMER_NUM:
2018-01-15 02:28:39 -06:00
//STEPPER TIMER TIM5 //use a 32bit timer
2018-01-11 15:29:08 -06:00
__HAL_RCC_TIM5_CLK_ENABLE();
timerConfig[0].timerdef.Instance = TIM5;
timerConfig[0].timerdef.Init.Prescaler = (STEPPER_TIMER_PRESCALE);
timerConfig[0].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
timerConfig[0].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timerConfig[0].IRQ_Id = TIM5_IRQn;
timerConfig[0].callback = (uint32_t)TC5_Handler;
HAL_NVIC_SetPriority(timerConfig[0].IRQ_Id, 1, 0);
2019-08-18 23:24:55 -05:00
#if PIN_EXISTS(STEPPER_ENABLE)
OUT_WRITE(STEPPER_ENABLE_PIN, HIGH);
#endif
2018-01-11 15:29:08 -06:00
break;
case TEMP_TIMER_NUM:
//TEMP TIMER TIM7 // any available 16bit Timer (1 already used for PWM)
__HAL_RCC_TIM7_CLK_ENABLE();
timerConfig[1].timerdef.Instance = TIM7;
2018-01-15 02:28:39 -06:00
timerConfig[1].timerdef.Init.Prescaler = (TEMP_TIMER_PRESCALE);
timerConfig[1].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
2018-01-11 15:29:08 -06:00
timerConfig[1].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timerConfig[1].IRQ_Id = TIM7_IRQn;
timerConfig[1].callback = (uint32_t)TC7_Handler;
HAL_NVIC_SetPriority(timerConfig[1].IRQ_Id, 2, 0);
break;
}
2018-08-22 17:14:38 -05:00
timers_initialized[timer_num] = true;
2018-01-11 15:29:08 -06:00
}
2018-01-15 02:28:39 -06:00
timerConfig[timer_num].timerdef.Init.Period = (((HAL_TIMER_RATE) / timerConfig[timer_num].timerdef.Init.Prescaler) / frequency) - 1;
2018-01-11 15:29:08 -06:00
2018-01-15 02:28:39 -06:00
if (HAL_TIM_Base_Init(&timerConfig[timer_num].timerdef) == HAL_OK)
2018-01-11 15:29:08 -06:00
HAL_TIM_Base_Start_IT(&timerConfig[timer_num].timerdef);
}
//forward the interrupt
2018-01-15 02:28:39 -06:00
extern "C" void TIM5_IRQHandler() {
2019-09-16 20:31:08 -05:00
((void(*)())timerConfig[0].callback)();
2018-01-11 15:29:08 -06:00
}
2018-01-15 02:28:39 -06:00
extern "C" void TIM7_IRQHandler() {
2019-09-16 20:31:08 -05:00
((void(*)())timerConfig[1].callback)();
2018-01-11 15:29:08 -06:00
}
void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
__HAL_TIM_SetAutoreload(&timerConfig[timer_num].timerdef, compare);
2018-01-11 15:29:08 -06:00
}
2018-01-15 02:28:39 -06:00
void HAL_timer_enable_interrupt(const uint8_t timer_num) {
2018-01-11 15:29:08 -06:00
HAL_NVIC_EnableIRQ(timerConfig[timer_num].IRQ_Id);
}
2018-01-15 02:28:39 -06:00
void HAL_timer_disable_interrupt(const uint8_t timer_num) {
2018-01-11 15:29:08 -06:00
HAL_NVIC_DisableIRQ(timerConfig[timer_num].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();
2018-01-11 15:29:08 -06:00
}
hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
2018-01-11 15:29:08 -06:00
return __HAL_TIM_GetAutoreload(&timerConfig[timer_num].timerdef);
}
uint32_t HAL_timer_get_count(const uint8_t timer_num) {
2018-01-11 15:29:08 -06:00
return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
}
2018-01-15 02:28:39 -06:00
void HAL_timer_isr_prologue(const uint8_t timer_num) {
2018-01-11 15:29:08 -06:00
if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
__HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);
}
}
bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
const uint32_t IRQ_Id = uint32_t(timerConfig[timer_num].IRQ_Id);
return NVIC->ISER[IRQ_Id >> 5] & _BV32(IRQ_Id & 0x1F);
}
2019-07-09 23:54:34 -05:00
#endif // STM32GENERIC && STM32F7