2018-01-11 15:29:08 -06:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef STM32F7
|
2018-01-15 02:28:39 -06:00
|
|
|
|
2018-01-11 15:29:08 -06:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Includes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2018-04-12 20:25:08 -05:00
|
|
|
#include "HAL.h"
|
2018-01-11 15:29:08 -06:00
|
|
|
|
|
|
|
#include "HAL_timers_STM32F7.h"
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Externals
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Local defines
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define NUM_HARDWARE_TIMERS 2
|
|
|
|
|
|
|
|
//#define PRESCALER 1
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Private Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
tTimerConfig timerConfig[NUM_HARDWARE_TIMERS];
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Function prototypes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Private functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2018-08-22 17:14:38 -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);
|
2018-10-26 15:23:02 -05:00
|
|
|
SET_OUTPUT(STEPPER_ENABLE_PIN);
|
|
|
|
WRITE(STEPPER_ENABLE_PIN);
|
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() {
|
|
|
|
((void(*)(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() {
|
|
|
|
((void(*)(void))timerConfig[1].callback)();
|
2018-01-11 15:29:08 -06:00
|
|
|
}
|
|
|
|
|
2018-02-10 20:42:00 -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);
|
2018-05-16 14:38:17 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-02-10 20:42:00 -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);
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:42:00 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 03:26:15 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-15 02:28:39 -06:00
|
|
|
#endif // STM32F7
|