2017-09-27 00:30:23 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-27 00:30:23 -05:00
|
|
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
|
|
|
* Copyright (c) 2017 Victor Perez
|
|
|
|
*
|
|
|
|
* 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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-27 00:30:23 -05:00
|
|
|
*
|
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2017-09-27 00:30:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
|
|
|
|
*/
|
|
|
|
|
2021-05-05 06:32:54 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
#include "HAL.h"
|
|
|
|
|
2018-03-28 13:07:52 -05:00
|
|
|
#include <libmaple/timer.h>
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-09-27 00:30:23 -05:00
|
|
|
// Defines
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-09-27 00:30:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Check and confirm what timer we will use for each Temps and stepper driving.
|
|
|
|
* We should probable drive temps with PWM.
|
|
|
|
*/
|
|
|
|
|
2017-11-05 19:31:07 -06:00
|
|
|
typedef uint16_t hal_timer_t;
|
2017-09-27 00:30:23 -05:00
|
|
|
#define HAL_TIMER_TYPE_MAX 0xFFFF
|
|
|
|
|
2019-06-29 04:39:38 -05:00
|
|
|
#define HAL_TIMER_RATE uint32_t(F_CPU) // frequency of timers peripherals
|
2018-06-11 23:04:26 -05:00
|
|
|
|
2020-06-16 01:45:27 -05:00
|
|
|
#ifndef STEP_TIMER_CHAN
|
|
|
|
#define STEP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
|
|
|
|
#endif
|
|
|
|
#ifndef TEMP_TIMER_CHAN
|
|
|
|
#define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
|
|
|
|
#endif
|
2018-06-11 23:04:26 -05:00
|
|
|
|
2019-11-06 17:34:29 -06:00
|
|
|
/**
|
|
|
|
* Note: Timers may be used by platforms and libraries
|
|
|
|
*
|
|
|
|
* FAN PWMs:
|
|
|
|
* With FAN_SOFT_PWM disabled the Temperature class uses
|
|
|
|
* FANx_PIN timers to generate FAN PWM signals.
|
|
|
|
*
|
|
|
|
* Speaker:
|
|
|
|
* When SPEAKER is enabled, one timer is allocated by maple/tone.cpp.
|
|
|
|
* - If BEEPER_PIN has a timer channel (and USE_PIN_TIMER is
|
|
|
|
* defined in tone.cpp) it uses the pin's own timer.
|
|
|
|
* - Otherwise it uses Timer 8 on boards with STM32_HIGH_DENSITY
|
|
|
|
* or Timer 4 on other boards.
|
|
|
|
*/
|
2020-06-01 18:33:30 -05:00
|
|
|
#ifndef STEP_TIMER_NUM
|
|
|
|
#if defined(MCU_STM32F103CB) || defined(MCU_STM32F103C8)
|
|
|
|
#define STEP_TIMER_NUM 4 // For C8/CB boards, use timer 4
|
|
|
|
#else
|
|
|
|
#define STEP_TIMER_NUM 5 // for other boards, five is fine.
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifndef PULSE_TIMER_NUM
|
|
|
|
#define PULSE_TIMER_NUM STEP_TIMER_NUM
|
|
|
|
#endif
|
|
|
|
#ifndef TEMP_TIMER_NUM
|
|
|
|
#define TEMP_TIMER_NUM 2 // Timer Index for Temperature
|
|
|
|
//#define TEMP_TIMER_NUM 4 // 2->4, Timer 2 for Stepper Current PWM
|
[2.0.x] Multiple updates to STM32F1 HAL (#8733)
* STM32F1 HAL
Adding files for STM32F1 HAL based on libmaple/stm32duino core.
Current persistent_store uses cardreader changes to be sent in separate
commit, but could be changed to use i2c eeprom.
There is another persistent_store implementation that uses the MCU flash memory
to emulate eeprom
Adding readme with some information about the stm32 HAL.
* Switch to Timer4 to avoid a hard reset on STM32F103C6 boards
On bluepill STM32F103C6 boards, using Timer5 results in a error() vector call. Switch to 4 since these are both general purpose, 16 bit timers.
* Add support for EEPROM emulation using Flash
Some low end machines doe not have EEPROM support. Simulate it using the last two pages of flash. Flash does not allow rewrite between erases, so skip writing the working version if that's enabled.
* Basic Pins for a malyan M200
This is a work in progress to go hand in hand with the STM32 work.
* Add support for ADC with DMA. This work has exposed a problem with the pin enumerations in STM boards vs what marlin expects (i.e, try defining PA0 as a temp pin). The hack can be removed with we go to fastio completely. To see this work, set something in adc_pins to a value like PA0 and connect your pullup resistor'd thermistor.
* Missing file - change HAL_adc_init to actually do something
We have an actual ADC init function now.
* Remove pinmode hack
Remove the pin mode hack that I was using to init PA0.
Updated Readme.md
* Several changes to timers and GPIO
Faster GPIO, and faster timer functions by accesing registers and
libmaple.
Still more changes pending for the Timer's code to skip using the
HardwareTimer class altogether.
Switch all enums to be within #defines
This change allows a user to have, for instance, TEMP_4 and TEMP_BED definied but nothing else. The enums which are not defined move "out", allowing the first ones to take the slots in the enum, and since the array is sized on ADC_PIN_COUNT, we always have the right size data and in order.
* Update Malyan M200 pins
Update Malyan M200 pins with correct fan values.
* Test all pins on actual hardware, update definitions
Some of the pin definitions were from knowlege base/pdfs. Now they've been tested against actual hardware. This should be very close to final.
* Update HAL_timers_Stm32f1.cpp
* Add sample configurations for Malyan M200
Add sample configuration for Malyan M200 without bed leveling, and move fan to auto cool E0 since this printer by default has only one fan.
Choose the timer based on MCU defintion. Timer5 is not valid on C8/CB class boards, so use Timer4 for the step timer.
readme.md update
* Updates to timers, and some stm32 boards definitiions
* Correct pin toggle macro.
* Remove duplicated Malyan M200 entry from pins.h
* Update configuration_store.cpp
* Formatting, indentation
* Formatting in HAL_Stm32f1.cpp
2017-12-10 23:12:45 -06:00
|
|
|
#endif
|
2019-10-15 14:18:46 -05:00
|
|
|
|
2020-02-04 01:24:11 -06:00
|
|
|
#if MB(BTT_SKR_MINI_E3_V1_0, BTT_SKR_E3_DIP, BTT_SKR_MINI_E3_V1_2, MKS_ROBIN_LITE)
|
2019-10-15 14:18:46 -05:00
|
|
|
// SKR Mini E3 boards use PA8 as FAN_PIN, so TIMER 1 is used for Fan PWM.
|
2019-11-06 17:34:29 -06:00
|
|
|
#ifdef STM32_HIGH_DENSITY
|
|
|
|
#define SERVO0_TIMER_NUM 8 // tone.cpp uses Timer 4
|
|
|
|
#else
|
|
|
|
#define SERVO0_TIMER_NUM 3 // tone.cpp uses Timer 8
|
|
|
|
#endif
|
2019-10-15 14:18:46 -05:00
|
|
|
#else
|
|
|
|
#define SERVO0_TIMER_NUM 1 // SERVO0 or BLTOUCH
|
|
|
|
#endif
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2020-07-14 19:07:27 -05:00
|
|
|
#define STEP_TIMER_IRQ_PRIO 2
|
|
|
|
#define TEMP_TIMER_IRQ_PRIO 3
|
|
|
|
#define SERVO0_TIMER_IRQ_PRIO 1
|
2019-06-29 04:39:38 -05:00
|
|
|
|
2018-06-11 23:04:26 -05:00
|
|
|
#define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
|
|
|
|
#define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
|
2017-12-20 16:16:36 -06:00
|
|
|
|
|
|
|
#define STEPPER_TIMER_PRESCALE 18 // prescaler for setting stepper timer, 4Mhz
|
2018-06-12 16:32:22 -05:00
|
|
|
#define STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer
|
2018-06-11 23:04:26 -05:00
|
|
|
#define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
|
2017-12-09 19:59:12 -06:00
|
|
|
|
2018-06-11 23:04:26 -05:00
|
|
|
#define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
|
|
|
|
#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
|
|
|
|
#define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
|
2018-03-07 17:53:25 -06:00
|
|
|
|
2018-06-11 23:04:26 -05:00
|
|
|
timer_dev* get_timer_dev(int number);
|
|
|
|
#define TIMER_DEV(num) get_timer_dev(num)
|
|
|
|
#define STEP_TIMER_DEV TIMER_DEV(STEP_TIMER_NUM)
|
|
|
|
#define TEMP_TIMER_DEV TIMER_DEV(TEMP_TIMER_NUM)
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2017-12-20 16:16:36 -06:00
|
|
|
#define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
|
|
|
|
#define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
|
2018-01-11 20:59:16 -06:00
|
|
|
#define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
|
2017-12-20 16:16:36 -06:00
|
|
|
|
|
|
|
#define ENABLE_TEMPERATURE_INTERRUPT() timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
|
|
|
|
#define DISABLE_TEMPERATURE_INTERRUPT() timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
|
|
|
|
|
2018-02-10 20:42:00 -06:00
|
|
|
#define HAL_timer_get_count(timer_num) timer_get_count(TIMER_DEV(timer_num))
|
2017-09-27 00:30:23 -05:00
|
|
|
|
|
|
|
// TODO change this
|
|
|
|
|
2020-06-01 18:33:30 -05:00
|
|
|
#ifndef HAL_TEMP_TIMER_ISR
|
|
|
|
#define HAL_TEMP_TIMER_ISR() extern "C" void tempTC_Handler()
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_STEP_TIMER_ISR
|
|
|
|
#define HAL_STEP_TIMER_ISR() extern "C" void stepTC_Handler()
|
|
|
|
#endif
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2020-11-25 21:40:56 -06:00
|
|
|
extern "C" {
|
|
|
|
void tempTC_Handler();
|
|
|
|
void stepTC_Handler();
|
|
|
|
}
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
|
|
|
// Public Variables
|
|
|
|
// ------------------------
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
//static HardwareTimer StepperTimer(STEP_TIMER_NUM);
|
|
|
|
//static HardwareTimer TempTimer(TEMP_TIMER_NUM);
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-09-27 00:30:23 -05:00
|
|
|
// Public functions
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-09-27 00:30:23 -05:00
|
|
|
|
2018-01-11 20:59:16 -06:00
|
|
|
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
|
|
|
|
void HAL_timer_enable_interrupt(const uint8_t timer_num);
|
|
|
|
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
|
|
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
2017-09-27 00:30:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: By default libmaple sets ARPE = 1, which means the Auto reload register is preloaded (will only update with an update event)
|
|
|
|
* Thus we have to pause the timer, update the value, refresh, resume the timer.
|
|
|
|
* That seems like a big waste of time and may be better to change the timer config to ARPE = 0, so ARR can be updated any time.
|
|
|
|
* We are using a Channel in each timer in Capture/Compare mode. We could also instead use the Time Update Event Interrupt, but need to disable ARPE
|
|
|
|
* so we can change the ARR value on the fly (without calling refresh), and not get an interrupt right there because we caused an UEV.
|
|
|
|
* This mode pretty much makes 2 timers unusable for PWM since they have their counts updated all the time on ISRs.
|
|
|
|
* The way Marlin manages timer interrupts doesn't make for an efficient usage in STM32F1
|
|
|
|
* Todo: Look at that possibility later.
|
|
|
|
*/
|
|
|
|
|
2018-02-10 20:42:00 -06:00
|
|
|
FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
|
2017-09-27 00:30:23 -05:00
|
|
|
switch (timer_num) {
|
|
|
|
case STEP_TIMER_NUM:
|
2019-06-29 04:39:38 -05:00
|
|
|
// NOTE: WE have set ARPE = 0, which means the Auto reload register is not preloaded
|
|
|
|
// and there is no need to use any compare, as in the timer mode used, setting ARR to the compare value
|
|
|
|
// will result in exactly the same effect, ie trigerring an interrupt, and on top, set counter to 0
|
|
|
|
timer_set_reload(STEP_TIMER_DEV, compare); // We reload direct ARR as needed during counting up
|
|
|
|
break;
|
2017-09-27 00:30:23 -05:00
|
|
|
case TEMP_TIMER_NUM:
|
2018-02-10 20:42:00 -06:00
|
|
|
timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, compare);
|
2019-06-29 04:39:38 -05:00
|
|
|
break;
|
2017-09-27 00:30:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 20:00:10 -06:00
|
|
|
FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
|
2017-09-27 00:30:23 -05:00
|
|
|
switch (timer_num) {
|
|
|
|
case STEP_TIMER_NUM:
|
2019-06-29 04:39:38 -05:00
|
|
|
// No counter to clear
|
2017-12-20 16:16:36 -06:00
|
|
|
timer_generate_update(STEP_TIMER_DEV);
|
|
|
|
return;
|
2017-09-27 00:30:23 -05:00
|
|
|
case TEMP_TIMER_NUM:
|
2017-12-20 16:16:36 -06:00
|
|
|
timer_set_count(TEMP_TIMER_DEV, 0);
|
|
|
|
timer_generate_update(TEMP_TIMER_DEV);
|
|
|
|
return;
|
2017-09-27 00:30:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:05:07 -05:00
|
|
|
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
2019-06-29 04:39:38 -05:00
|
|
|
|
|
|
|
// No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
|
|
|
|
// Needed here to reset ARPE=0 for stepper timer
|
|
|
|
FORCE_INLINE static void timer_no_ARR_preload_ARPE(timer_dev *dev) {
|
|
|
|
bb_peri_set_bit(&(dev->regs).gen->CR1, TIMER_CR1_ARPE_BIT, 0);
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:07:27 -05:00
|
|
|
void timer_set_interrupt_priority(uint_fast8_t timer_num, uint_fast8_t priority);
|
|
|
|
|
2019-06-29 04:39:38 -05:00
|
|
|
#define TIMER_OC_NO_PRELOAD 0 // Need to disable preload also on compare registers.
|