From 8b5e51c9aae01ca955f9fcce0975c016806ba068 Mon Sep 17 00:00:00 2001 From: Karl Andersson Date: Tue, 16 Oct 2018 13:42:41 +0200 Subject: [PATCH] STM32F4xx modifications for HAL_STM32 (#12080) --- Marlin/src/HAL/HAL_LPC1768/watchdog.cpp | 2 +- Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.cpp | 4 +- Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.h | 63 ++++++++++++------- Marlin/src/HAL/HAL_STM32/endstop_interrupts.h | 6 ++ .../HAL/HAL_STM32/persistent_store_impl.cpp | 34 +++++++--- Marlin/src/HAL/HAL_STM32/watchdog_STM32.cpp | 9 ++- .../HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp | 2 +- Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp | 2 +- Marlin/src/HAL/HAL_STM32F4/HAL.cpp | 2 +- .../src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.cpp | 2 +- .../src/HAL/HAL_STM32F4/HAL_spi_STM32F4.cpp | 2 +- .../HAL/HAL_STM32F4/HAL_timers_STM32F4.cpp | 4 +- .../HAL_STM32F4/persistent_store_eeprom.cpp | 2 +- .../src/HAL/HAL_STM32F4/watchdog_STM32F4.cpp | 2 +- Marlin/src/pins/pins_REMRAM_V1.h | 6 ++ 15 files changed, 96 insertions(+), 46 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp b/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp index 9138c04ac..dde282d24 100644 --- a/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp @@ -67,7 +67,7 @@ uint8_t HAL_get_reset_source(void) { void watchdog_reset() { WDT_Feed(); - #if PIN_EXISTS(LED) + #if !defined(PINS_DEBUGGING) && PIN_EXISTS(LED) TOGGLE(LED_PIN); // heart beat indicator #endif } diff --git a/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.cpp b/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.cpp index 24a627aa3..217c8796e 100644 --- a/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.cpp +++ b/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.cpp @@ -79,7 +79,7 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { 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); - HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, 6, 0); + HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0); break; case TEMP_TIMER_NUM: @@ -87,7 +87,7 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { 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); - HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, 2, 0); + HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0); break; } timers_initialised[timer_num] = true; diff --git a/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.h b/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.h index 0e24767ce..4c353b478 100644 --- a/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.h +++ b/Marlin/src/HAL/HAL_STM32/HAL_timers_STM32.h @@ -34,42 +34,66 @@ #define FORCE_INLINE __attribute__((always_inline)) inline #define hal_timer_t uint32_t -#define HAL_TIMER_TYPE_MAX 0xFFFF +#define HAL_TIMER_TYPE_MAX 0xFFFFFFFF // Timers can be 16 or 32 bit #ifdef STM32F0xx - #define HAL_TIMER_RATE (HAL_RCC_GetSysClockFreq()) // frequency of timer peripherals + #define HAL_TIMER_RATE (F_CPU) // frequency of timer peripherals - #define STEP_TIMER 16 - #define TEMP_TIMER 17 + #ifndef STEP_TIMER + #define STEP_TIMER 16 + #endif + + #ifndef TEMP_TIMER + #define TEMP_TIMER 17 + #endif #elif defined STM32F1xx - #define HAL_TIMER_RATE (HAL_RCC_GetPCLK2Freq()) // frequency of timer peripherals + #define HAL_TIMER_RATE (F_CPU) // frequency of timer peripherals - #define STEP_TIMER 4 - #define TEMP_TIMER 2 + #ifndef STEP_TIMER + #define STEP_TIMER 4 + #endif + + #ifndef TEMP_TIMER + #define TEMP_TIMER 2 + #endif #elif defined STM32F4xx - #define HAL_TIMER_RATE (HAL_RCC_GetPCLK2Freq()) // frequency of timer peripherals + #define HAL_TIMER_RATE (F_CPU/2) // frequency of timer peripherals + + #ifndef STEP_TIMER + #define STEP_TIMER 5 + #endif - #define STEP_TIMER 4 - #define TEMP_TIMER 5 + #ifndef TEMP_TIMER + #define TEMP_TIMER 7 + #endif #elif defined STM32F7xx - #define HAL_TIMER_RATE (HAL_RCC_GetSysClockFreq()/2) // frequency of timer peripherals + #define HAL_TIMER_RATE (F_CPU/2) // frequency of timer peripherals - #define STEP_TIMER 5 - #define TEMP_TIMER 7 + #ifndef STEP_TIMER + #define STEP_TIMER 5 + #endif - #if MB(REMRAM_V1) - #define STEP_TIMER 2 + #ifndef TEMP_TIMER + #define TEMP_TIMER 7 #endif #endif +#ifndef STEP_TIMER_IRQ_PRIO + #define STEP_TIMER_IRQ_PRIO 1 +#endif + +#ifndef TEMP_TIMER_IRQ_PRIO + #define TEMP_TIMER_IRQ_PRIO 2 +#endif + #define STEP_TIMER_NUM 0 // index of timer to use for stepper #define TEMP_TIMER_NUM 1 // index of timer to use for temperature #define PULSE_TIMER_NUM STEP_TIMER_NUM @@ -110,9 +134,6 @@ #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM) #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM) -#define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM) -#define TEMP_ISR_ENABLED() HAL_timer_interrupt_enabled(TEMP_TIMER_NUM) - extern void Step_Handler(stimer_t *htim); extern void Temp_Handler(stimer_t *htim); #define HAL_STEP_TIMER_ISR void Step_Handler(stimer_t *htim) @@ -153,11 +174,5 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) { return __HAL_TIM_GET_AUTORELOAD(&TimerHandle[timer_num].handle); } -FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) { - const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks; - if (HAL_timer_get_compare(timer_num) < mincmp) - HAL_timer_set_compare(timer_num, mincmp); -} - #define HAL_timer_isr_prologue(TIMER_NUM) #define HAL_timer_isr_epilogue(TIMER_NUM) diff --git a/Marlin/src/HAL/HAL_STM32/endstop_interrupts.h b/Marlin/src/HAL/HAL_STM32/endstop_interrupts.h index b7d47d3df..5f40be405 100644 --- a/Marlin/src/HAL/HAL_STM32/endstop_interrupts.h +++ b/Marlin/src/HAL/HAL_STM32/endstop_interrupts.h @@ -52,6 +52,12 @@ void setup_endstop_interrupts(void) { #if HAS_Z2_MIN attachInterrupt(Z2_MIN_PIN, endstop_ISR, CHANGE); #endif + #if HAS_Z3_MAX + attachInterrupt(Z3_MAX_PIN, endstop_ISR, CHANGE); + #endif + #if HAS_Z3_MIN + attachInterrupt(Z3_MIN_PIN, endstop_ISR, CHANGE); + #endif #if HAS_Z_MIN_PROBE_PIN attachInterrupt(Z_MIN_PROBE_PIN, endstop_ISR, CHANGE); #endif diff --git a/Marlin/src/HAL/HAL_STM32/persistent_store_impl.cpp b/Marlin/src/HAL/HAL_STM32/persistent_store_impl.cpp index b89e29ae9..31715ac66 100644 --- a/Marlin/src/HAL/HAL_STM32/persistent_store_impl.cpp +++ b/Marlin/src/HAL/HAL_STM32/persistent_store_impl.cpp @@ -28,20 +28,20 @@ #include "../shared/persistent_store_api.h" -#if DISABLED(EEPROM_EMULATED_WITH_SRAM) +#if DISABLED(EEPROM_EMULATED_WITH_SRAM) && DISABLED(SPI_EEPROM) && DISABLED(I2C_EEPROM) #include static bool eeprom_data_written = false; #endif bool PersistentStore::access_start() { - #if DISABLED(EEPROM_EMULATED_WITH_SRAM) + #if DISABLED(EEPROM_EMULATED_WITH_SRAM) && DISABLED(SPI_EEPROM) && DISABLED(I2C_EEPROM) eeprom_buffer_fill(); #endif return true; } bool PersistentStore::access_finish() { - #if DISABLED(EEPROM_EMULATED_WITH_SRAM) + #if DISABLED(EEPROM_EMULATED_WITH_SRAM) && DISABLED(SPI_EEPROM) && DISABLED(I2C_EEPROM) if (eeprom_data_written) { eeprom_buffer_flush(); eeprom_data_written = false; @@ -54,8 +54,20 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui while (size--) { uint8_t v = *value; - // Save to either program flash or Backup SRAM - #if DISABLED(EEPROM_EMULATED_WITH_SRAM) + // Save to either external EEPROM, program flash or Backup SRAM + #if ENABLED(SPI_EEPROM) || ENABLED(I2C_EEPROM) + // EEPROM has only ~100,000 write cycles, + // so only write bytes that have changed! + uint8_t * const p = (uint8_t * const)pos; + if (v != eeprom_read_byte(p)) { + eeprom_write_byte(p, v); + if (eeprom_read_byte(p) != v) { + SERIAL_ECHO_START(); + SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE); + return true; + } + } + #elif DISABLED(EEPROM_EMULATED_WITH_SRAM) eeprom_buffered_write_byte(pos, v); #else *(__IO uint8_t *)(BKPSRAM_BASE + (uint8_t * const)pos) = v; @@ -65,7 +77,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui pos++; value++; }; - #if DISABLED(EEPROM_EMULATED_WITH_SRAM) + #if DISABLED(EEPROM_EMULATED_WITH_SRAM) && DISABLED(SPI_EEPROM) && DISABLED(I2C_EEPROM) eeprom_data_written = true; #endif @@ -74,9 +86,11 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing) { do { - // Read from either program flash or Backup SRAM + // Read from either external EEPROM, program flash or Backup SRAM const uint8_t c = ( - #if DISABLED(EEPROM_EMULATED_WITH_SRAM) + #if ENABLED(SPI_EEPROM) || ENABLED(I2C_EEPROM) + eeprom_read_byte((uint8_t*)pos) + #elif DISABLED(EEPROM_EMULATED_WITH_SRAM) eeprom_buffered_read_byte(pos) #else (*(__IO uint8_t *)(BKPSRAM_BASE + ((uint8_t*)pos))) @@ -92,7 +106,9 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t } size_t PersistentStore::capacity() { - #if DISABLED(EEPROM_EMULATED_WITH_SRAM) + #if ENABLED(SPI_EEPROM) || ENABLED(I2C_EEPROM) + return E2END + 1; + #elif DISABLED(EEPROM_EMULATED_WITH_SRAM) return E2END + 1; #else return 4096; // 4kB diff --git a/Marlin/src/HAL/HAL_STM32/watchdog_STM32.cpp b/Marlin/src/HAL/HAL_STM32/watchdog_STM32.cpp index cc2566b14..ceb18bed1 100644 --- a/Marlin/src/HAL/HAL_STM32/watchdog_STM32.cpp +++ b/Marlin/src/HAL/HAL_STM32/watchdog_STM32.cpp @@ -31,7 +31,14 @@ void watchdog_init() { IWatchdog.begin(4000000); } // 4 sec timeout - void watchdog_reset() { IWatchdog.reload(); } + void watchdog_reset() + { + IWatchdog.reload(); + + #if PIN_EXISTS(LED) + TOGGLE(LED_PIN); // heart beat indicator + #endif + } #endif // USE_WATCHDOG #endif // ARDUINO_ARCH_STM32 diff --git a/Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp b/Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp index 0e313c1b4..c13d42818 100644 --- a/Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp @@ -47,7 +47,7 @@ /** @addtogroup EEPROM_Emulation * @{ */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) /* Includes ------------------------------------------------------------------*/ #include "eeprom_emul.h" diff --git a/Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp b/Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp index 4820bd6ab..a1ffab376 100644 --- a/Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp @@ -17,7 +17,7 @@ * */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) /** * Description: functions for I2C connected external EEPROM. diff --git a/Marlin/src/HAL/HAL_STM32F4/HAL.cpp b/Marlin/src/HAL/HAL_STM32F4/HAL.cpp index c79af5859..56823ccc8 100644 --- a/Marlin/src/HAL/HAL_STM32F4/HAL.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/HAL.cpp @@ -21,7 +21,7 @@ * */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) // -------------------------------------------------------------------------- // Includes diff --git a/Marlin/src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.cpp b/Marlin/src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.cpp index 2877e88d5..9d9c722e5 100644 --- a/Marlin/src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.cpp @@ -21,7 +21,7 @@ * */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) #include "../../inc/MarlinConfig.h" diff --git a/Marlin/src/HAL/HAL_STM32F4/HAL_spi_STM32F4.cpp b/Marlin/src/HAL/HAL_STM32F4/HAL_spi_STM32F4.cpp index 5e3ff4d9d..34582c30a 100644 --- a/Marlin/src/HAL/HAL_STM32F4/HAL_spi_STM32F4.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/HAL_spi_STM32F4.cpp @@ -30,7 +30,7 @@ * Adapted to the STM32F4 HAL */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) // -------------------------------------------------------------------------- // Includes diff --git a/Marlin/src/HAL/HAL_STM32F4/HAL_timers_STM32F4.cpp b/Marlin/src/HAL/HAL_STM32F4/HAL_timers_STM32F4.cpp index 64da993ef..6a23090a2 100644 --- a/Marlin/src/HAL/HAL_STM32F4/HAL_timers_STM32F4.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/HAL_timers_STM32F4.cpp @@ -20,7 +20,7 @@ * */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) // -------------------------------------------------------------------------- // Includes @@ -91,7 +91,7 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { TimerHandle[timer_num].irqHandle = TC5_Handler; TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler); #endif - HAL_NVIC_SetPriority(STEP_TIMER_IRQ_ID, 6, 0); + HAL_NVIC_SetPriority(STEP_TIMER_IRQ_ID, 1, 0); break; case TEMP_TIMER_NUM: diff --git a/Marlin/src/HAL/HAL_STM32F4/persistent_store_eeprom.cpp b/Marlin/src/HAL/HAL_STM32F4/persistent_store_eeprom.cpp index 576ecc6de..648c29768 100644 --- a/Marlin/src/HAL/HAL_STM32F4/persistent_store_eeprom.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/persistent_store_eeprom.cpp @@ -21,7 +21,7 @@ * */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) #include "../shared/persistent_store_api.h" diff --git a/Marlin/src/HAL/HAL_STM32F4/watchdog_STM32F4.cpp b/Marlin/src/HAL/HAL_STM32F4/watchdog_STM32F4.cpp index f5dd6bdfa..e7143759b 100644 --- a/Marlin/src/HAL/HAL_STM32F4/watchdog_STM32F4.cpp +++ b/Marlin/src/HAL/HAL_STM32F4/watchdog_STM32F4.cpp @@ -20,7 +20,7 @@ * */ -#if defined(STM32F4) || defined(STM32F4xx) +#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F4xx)) #include "../../inc/MarlinConfig.h" diff --git a/Marlin/src/pins/pins_REMRAM_V1.h b/Marlin/src/pins/pins_REMRAM_V1.h index 193f56713..fc065166e 100644 --- a/Marlin/src/pins/pins_REMRAM_V1.h +++ b/Marlin/src/pins/pins_REMRAM_V1.h @@ -124,3 +124,9 @@ #define BTN_EN1 54 // BTN_EN1 #define BTN_EN2 55 // BTN_EN2 #define BTN_ENC 47 // BTN_ENC + +// +// Timers +// + +#define STEP_TIMER 2