From ebc51c25371f4db9bf07b98ec1949befacf8002e Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 17 Nov 2026 10:22:27 +0100 Subject: [PATCH 1/4] perf(core): decouple pinMode from the PWM and DAC code pinMode() called pwm_stop() and dac_stop() directly, so every sketch using pinMode() or digitalWrite() linked the whole HardwareTimer, TIM and ADC/DAC stack. Move the pin configured map to wiring_digital.c and stop the analog output through a weak analog_output_stop(), defined in wiring_analog.c and so only linked when the sketch uses analogWrite(). Use the same TIM condition as analogWrite() (HAL_TIM_MODULE_ENABLED or USE_HAL_TIM_MODULE) in pinMode() and analog_output_stop(). Before, HAL v2 targets (STM32C5), which only define USE_HAL_TIM_MODULE, never stopped the PWM in pinMode(). It now links pwm_stop(): +592 bytes on a PWM sketch on C562RE. Blink on F103 goes from 9744 to 7408 bytes (-24%). A sketch using analogWrite() is unchanged on HAL v1 targets. Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 --- cores/arduino/wiring_analog.c | 24 ++++++++++++++++++-- cores/arduino/wiring_digital.c | 28 ++++++++++++------------ libraries/SrcWrapper/inc/PinConfigured.h | 17 ++++++++++++++ 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 96b34f3c79..9502a5154c 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -33,9 +33,29 @@ extern "C" { (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\ (!defined(HAL_TIM_MODULE_ONLY) &&\ (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))) -//This is the list of the IOs configured -uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; +/** + * @brief Stop the analog output (PWM or DAC) of a pin. Called by pinMode(). + * @param pin Pin name. + */ +void analog_output_stop(PinName pin) +{ +#if !defined(HAL_DAC_MODULE_ONLY) &&\ + (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) + if (pin_in_pinmap(pin, PinMap_DAC)) { + dac_stop(pin); + } else +#endif + { +#if !defined(HAL_TIM_MODULE_ONLY) &&\ + (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1))) + if (pin_in_pinmap(pin, PinMap_TIM)) { + pwm_stop(pin); + } +#endif /* !HAL_TIM_MODULE_ONLY && (HAL_TIM_MODULE_ENABLED || (USE_HAL_TIM_MODULE && (USE_HAL_TIM_MODULE == 1))) */ + } +} #endif + #define MAX_PWM_RESOLUTION 16 static int _writeResolution = PWM_RESOLUTION; static int _internalWriteResolution = diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index b366891715..78ee8d5e6f 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -29,7 +29,16 @@ extern "C" { #endif -extern pin_size_t g_anOutputPinConfigured[MAX_NB_PORT]; +#if !defined(HAL_DAC_MODULE_ONLY) &&\ + (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\ + (!defined(HAL_TIM_MODULE_ONLY) &&\ + (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))) +//This is the list of the IOs configured as analog output +uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0}; +#endif + +// Weak: only defined (wiring_analog.c) when the sketch uses analogWrite() +void analog_output_stop(PinName pin) __attribute__((weak)); /** * @brief Set the mode of a pin. @@ -48,20 +57,11 @@ void pinMode(pin_size_t pinNumber, PinMode pinMode) // If the pin that support PWM or DAC output, we need to turn it off #if !defined(HAL_DAC_MODULE_ONLY) &&\ (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\ - (defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)) + (!defined(HAL_TIM_MODULE_ONLY) &&\ + (defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))) if (is_pin_configured(p, g_anOutputPinConfigured)) { -#if !defined(HAL_DAC_MODULE_ONLY) &&\ - (defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) - if (pin_in_pinmap(p, PinMap_DAC)) { - dac_stop(p); - } else -#endif - { -#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY) - if (pin_in_pinmap(p, PinMap_TIM)) { - pwm_stop(p); - } -#endif //HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY + if (analog_output_stop != NULL) { + analog_output_stop(p); } /* Unconditionally reset the pin configuration */ reset_pin_configured(p, g_anOutputPinConfigured); diff --git a/libraries/SrcWrapper/inc/PinConfigured.h b/libraries/SrcWrapper/inc/PinConfigured.h index 1aeb8abc9b..c07a961f93 100644 --- a/libraries/SrcWrapper/inc/PinConfigured.h +++ b/libraries/SrcWrapper/inc/PinConfigured.h @@ -69,6 +69,23 @@ extern "C" { #define reset_pin_configured(pin, map) \ (map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin))) +/** + * @brief Map of the pins configured as analog output (PWM or DAC) by analogWrite(). + * + * Defined in wiring_digital.c so pinMode() does not depend on wiring_analog.c. + */ +extern uint32_t g_anOutputPinConfigured[]; + +/** + * @brief Stop the analog output (PWM or DAC) of a pin. + * + * Defined in wiring_analog.c and referenced weakly by pinMode(): the PWM and + * DAC code is only linked when the sketch uses analogWrite(). + * + * @param pin Pin name. + */ +void analog_output_stop(PinName pin); + #ifdef __cplusplus } #endif From 9020153ee9b60bb27d291b627085ef33e96eae0c Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 17 Nov 2026 15:15:44 +0100 Subject: [PATCH 2/4] perf(core): link the peripheral drivers lazily SrcWrapper objects are linked unconditionally, so the strong IRQ handlers of uart.c, HardwareTimer.cpp and interrupt.cpp were always kept alive by the vector table, even in a sketch that does not use these peripherals. Move uart.c, interrupt.cpp, HardwareTimer.cpp and analog.cpp from libraries/SrcWrapper to cores/arduino/stm32. They become members of core.a and are only extracted when a sketch or a library references them. The strong handlers and HAL callbacks they define still override the weak ones as soon as the member is extracted. An override in a core.a member is never used when nothing references the member (see #797), so the HAL MSP functions must not be overrides. Rename the ADC, DAC and TIM ones adc_msp_init(), adc_msp_deinit(), dac_msp_init(), dac_msp_deinit(), timer_msp_init() and timer_msp_deinit(), make them static and call them explicitly from analog.cpp and HardwareTimer.cpp: before HAL_xxx_Init(), as the HAL accesses the registers right away and needs the clock, and after HAL_xxx_DeInit(). This does not depend on the weak vs strong symbol resolution anymore. The HAL_TIM_OC and HAL_TIM_IC MSP overrides are removed: the core never calls HAL_TIM_OC_Init() nor HAL_TIM_IC_Init(). Behavior change: a sketch calling HAL_TIM_Base_Init(), HAL_ADC_Init() or HAL_DAC_Init() directly no longer gets the core MSP functions and has to set the clock and the GPIO itself. The IRQ handlers of the moved files only act on handles owned by the core drivers, so they are not needed when these are not extracted. Update the CMake source lists and License.md. Blink on F103 goes from 7408 to 6192 bytes, an EXTI-only sketch from 10756 to 7680 bytes (-29%). A sketch using every peripheral is unchanged. Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- License.md | 7 +- cores/arduino/CMakeLists.txt | 4 + .../arduino/stm32}/HardwareTimer.cpp | 45 +++++++++- .../src => cores/arduino}/stm32/analog.cpp | 20 +++-- .../src => cores/arduino}/stm32/interrupt.cpp | 0 .../src => cores/arduino}/stm32/uart.c | 0 docs/customization/HAL-configuration.md | 2 +- libraries/SrcWrapper/CMakeLists.txt | 4 - libraries/SrcWrapper/src/stm32/timer.c | 82 ------------------- 9 files changed, 65 insertions(+), 99 deletions(-) rename {libraries/SrcWrapper/src => cores/arduino/stm32}/HardwareTimer.cpp (97%) rename {libraries/SrcWrapper/src => cores/arduino}/stm32/analog.cpp (98%) rename {libraries/SrcWrapper/src => cores/arduino}/stm32/interrupt.cpp (100%) rename {libraries/SrcWrapper/src => cores/arduino}/stm32/uart.c (100%) diff --git a/License.md b/License.md index f8167a817a..5ac7d6469f 100644 --- a/License.md +++ b/License.md @@ -8,7 +8,8 @@ This files includes licensing information for parts of this repository. * `cores/arduino/*.*` mainly contains modified files from Arduino, except the files listed under [BSD 3-Clause License](#bsd-3-clause-license) and [MIT License](#mit-license) -* `cores/arduino/stm32` +* `cores/arduino/stm32` - except the files listed under + [BSD 3-Clause License](#bsd-3-clause-license) and [MIT License](#mit-license) * `cores/arduino/api` (own copy of the license in `cores/arduino/api/LICENSE`), except the files listed under [MIT License](#mit-license) * `libraries/EEPROM`, `libraries/IWatchdog`, `libraries/Keyboard`, @@ -31,6 +32,8 @@ This files includes licensing information for parts of this repository. * `libraries/I3C`, `libraries/RGB_LED_TLC59731` and `libraries/SubGhz` * `libraries/SrcWrapper` - files with a BSD 3-Clause header, mainly `libraries/SrcWrapper/src/stm32` and `libraries/SrcWrapper/inc` +* `cores/arduino/stm32/analog.cpp`, `cores/arduino/stm32/interrupt.cpp` and + `cores/arduino/stm32/uart.c` * `variants` - files with a BSD 3-Clause header, such as `PeripheralPins.c`, `variant_generic.*` and `generic_clock.c` @@ -47,7 +50,7 @@ This files includes licensing information for parts of this repository. [MIT License](#mit-license) is used for: * `libraries/VirtIO/*/virtio*` and `libraries/VirtIO/*/VirtIOSerial.*` -* `libraries/SrcWrapper/*/HardwareTimer.*` +* `libraries/SrcWrapper/inc/HardwareTimer.h` and `cores/arduino/stm32/HardwareTimer.cpp` * `libraries/SrcWrapper/src/new.cpp` * `cores/arduino/api/Udp.h` and `cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h` diff --git a/cores/arduino/CMakeLists.txt b/cores/arduino/CMakeLists.txt index e45b024207..9e3fce4242 100644 --- a/cores/arduino/CMakeLists.txt +++ b/cores/arduino/CMakeLists.txt @@ -36,8 +36,12 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL main.cpp pins_arduino.c Serial.cpp + stm32/analog.cpp + stm32/HardwareTimer.cpp + stm32/interrupt.cpp stm32/startup_stm32yyxx.c stm32/startup_stm32yyxx.S + stm32/uart.c Tone.cpp WInterrupts.cpp wiring_analog.c diff --git a/libraries/SrcWrapper/src/HardwareTimer.cpp b/cores/arduino/stm32/HardwareTimer.cpp similarity index 97% rename from libraries/SrcWrapper/src/HardwareTimer.cpp rename to cores/arduino/stm32/HardwareTimer.cpp index 147f082816..504bc350fb 100644 --- a/libraries/SrcWrapper/src/HardwareTimer.cpp +++ b/cores/arduino/stm32/HardwareTimer.cpp @@ -50,6 +50,44 @@ timerObj_t *HardwareTimer_Handle[TIMER_NUM] = {NULL}; #endif #endif +#if !defined(USE_HALV2_DRIVER) +/** + * @brief Timer MSP initialization: enable the timer clock, then set priority + * of and enable the update interrupt and, if different, the capture + * compare interrupt. + * Called before HAL_TIM_Base_Init() as the registers are accessed by the HAL. + * @param obj timer object + */ +static void timer_msp_init(timerObj_t *obj) +{ + TIM_TypeDef *instance = obj->handle.Instance; + enableTimerClock(instance); + + // configure Update interrupt + HAL_NVIC_SetPriority(getTimerUpIrq(instance), obj->preemptPriority, obj->subPriority); + HAL_NVIC_EnableIRQ(getTimerUpIrq(instance)); + + if (getTimerCCIrq(instance) != getTimerUpIrq(instance)) { + // configure Capture Compare interrupt + HAL_NVIC_SetPriority(getTimerCCIrq(instance), obj->preemptPriority, obj->subPriority); + HAL_NVIC_EnableIRQ(getTimerCCIrq(instance)); + } +} + +/** + * @brief Timer MSP de-initialization: disable the timer clock and interrupts. + * Called after HAL_TIM_Base_DeInit(). + * @param obj timer object + */ +static void timer_msp_deinit(timerObj_t *obj) +{ + TIM_TypeDef *instance = obj->handle.Instance; + disableTimerClock(instance); + HAL_NVIC_DisableIRQ(getTimerUpIrq(instance)); + HAL_NVIC_DisableIRQ(getTimerCCIrq(instance)); +} +#endif /* !USE_HALV2_DRIVER */ + HardwareTimer::HardwareTimer() { _timerObj.instance = nullptr; @@ -76,8 +114,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance) _timerObj.preemptPriority = TIM_IRQ_PRIO; _timerObj.subPriority = TIM_IRQ_SUBPRIO; - /* Enable timer clock. Even if it is also done in HAL_TIM_Base_MspInit(), - it is done there so that it is possible to write registers right now */ + /* Enable timer clock so that it is possible to write registers right now */ enableTimerClock(instance); // Initialize NULL callbacks @@ -106,7 +143,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance) // HAL_TIM_SetUpdateSource(&_timerObj.handle, HAL_TIM_UPDATE_REGULAR); // HAL_TIM_EnableUpdateGeneration(&_timerObj.handle); - // configure Update interrupt done in HAL_TIM_Base_MspInit for V1 + // configure Update interrupt done in timer_msp_init for V1 HAL_CORTEX_NVIC_SetPriority(getTimerUpIrq(_timerObj.instance), (hal_cortex_nvic_preemp_priority_t)_timerObj.preemptPriority, (hal_cortex_nvic_sub_priority_t)_timerObj.subPriority); @@ -140,6 +177,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance) _timerObj.handle.Init.RepetitionCounter = 0; #endif _timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + timer_msp_init(&_timerObj); HAL_TIM_Base_Init(&(_timerObj.handle)); #endif } @@ -1591,6 +1629,7 @@ void HardwareTimer::timerHandleDeinit() #else HAL_TIM_Base_Stop_IT(&(_timerObj.handle)); HAL_TIM_Base_DeInit(&(_timerObj.handle)); + timer_msp_deinit(&_timerObj); #endif } diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/cores/arduino/stm32/analog.cpp similarity index 98% rename from libraries/SrcWrapper/src/stm32/analog.cpp rename to cores/arduino/stm32/analog.cpp index ffe9ec22b9..aaae3037e3 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/cores/arduino/stm32/analog.cpp @@ -548,7 +548,7 @@ uint32_t get_adc_internal_channel(PinName pin) * the GPIO pins. * @param hadc ADC handle pointer. */ -void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) +static void adc_msp_init(ADC_HandleTypeDef *hadc) { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC Periph clock enable */ @@ -634,7 +634,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) * @brief ADC MSP de-initialization: disable the peripheral clock. * @param hadc ADC handle pointer. */ -void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc) +static void adc_msp_deinit(ADC_HandleTypeDef *hadc) { #ifdef __HAL_RCC_ADC_FORCE_RESET __HAL_RCC_ADC_FORCE_RESET(); @@ -978,8 +978,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) AdcHandle.Lock = HAL_UNLOCKED; /* Some other ADC_HandleTypeDef fields exists but not required */ - g_current_pin = pin; /* Needed for HAL_ADC_MspInit*/ + g_current_pin = pin; /* Needed for adc_msp_init */ + adc_msp_init(&AdcHandle); if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { return 0; } @@ -1115,7 +1116,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) return 0; } - if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) { + HAL_StatusTypeDef deinit_status = HAL_ADC_DeInit(&AdcHandle); + adc_msp_deinit(&AdcHandle); + if (deinit_status != HAL_OK) { return 0; } @@ -1293,7 +1296,7 @@ uint32_t get_dac_channel(PinName pin) * the GPIO pins. * @param hdac DAC handle pointer. */ -void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac) +static void dac_msp_init(DAC_HandleTypeDef *hdac) { /* DAC Periph clock enable */ if (hdac->Instance == DAC1) { @@ -1358,6 +1361,7 @@ void dac_write_value(PinName pin, uint32_t value, bool do_init) if (do_init) { /*##-1- Configure the DAC peripheral #######################################*/ g_current_pin = pin; + dac_msp_init(&DacHandle); if (HAL_DAC_Init(&DacHandle) != HAL_OK) { /* Initialization Error */ return; @@ -1415,7 +1419,7 @@ void dac_write_value(PinName pin, uint32_t value, bool do_init) * @brief DAC MSP de-initialization: disable the peripheral clock. * @param hdac DAC handle pointer. */ -void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac) +static void dac_msp_deinit(DAC_HandleTypeDef *hdac) { /* DAC Periph clock disable */ if (hdac->Instance == DAC1) { @@ -1517,7 +1521,9 @@ void dac_stop(PinName pin) HAL_DAC_Stop(&DacHandle, dacChannel); - if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) { + HAL_StatusTypeDef deinit_status = HAL_DAC_DeInit(&DacHandle); + dac_msp_deinit(&DacHandle); + if (deinit_status != HAL_OK) { /* DeInitialization Error */ return; } diff --git a/libraries/SrcWrapper/src/stm32/interrupt.cpp b/cores/arduino/stm32/interrupt.cpp similarity index 100% rename from libraries/SrcWrapper/src/stm32/interrupt.cpp rename to cores/arduino/stm32/interrupt.cpp diff --git a/libraries/SrcWrapper/src/stm32/uart.c b/cores/arduino/stm32/uart.c similarity index 100% rename from libraries/SrcWrapper/src/stm32/uart.c rename to cores/arduino/stm32/uart.c diff --git a/docs/customization/HAL-configuration.md b/docs/customization/HAL-configuration.md index 34ab5747c7..12e402a2bd 100644 --- a/docs/customization/HAL-configuration.md +++ b/docs/customization/HAL-configuration.md @@ -180,7 +180,7 @@ The following HAL system configuration values can be redefined: * `USE_SD_TRANSCEIVER`: Enable the microSD transceiver ## Use HAL modules without Arduino ownership -STM32 peripherals have many powerful features. Some of them are used by default by the Arduino API: I2C, SPI, TIM, U(S)ART, ... and take over IRQ Handlers (ex: `TIMx_IRQHandler`) and other HAL weaked functions (ex: `HAL_XXX_MspInit()`). +STM32 peripherals have many powerful features. Some of them are used by default by the Arduino API: I2C, SPI, TIM, U(S)ART, ... and take over IRQ Handlers (ex: `TIMx_IRQHandler`) and other HAL weaked functions (ex: `HAL_UART_RxCpltCallback()`). For advanced user applications, it could be useful to take control of those IRQ handlers. Since core version 1.8.0, it is possible to disable the use of selected HAL modules by the Arduino API. diff --git a/libraries/SrcWrapper/CMakeLists.txt b/libraries/SrcWrapper/CMakeLists.txt index 0a7b1665a6..b62388678c 100644 --- a/libraries/SrcWrapper/CMakeLists.txt +++ b/libraries/SrcWrapper/CMakeLists.txt @@ -156,7 +156,6 @@ add_library(SrcWrapper_bin OBJECT EXCLUDE_FROM_ALL src/HAL/stm32yyxx_utils_fdcan.c src/HAL/stm32yyxx_utils_i2c.c src/HAL/stm32yyxx_utils_i3c.c - src/HardwareTimer.cpp src/LL/stm32yyxx_ll_adc.c src/LL/stm32yyxx_ll_bdma.c src/LL/stm32yyxx_ll_comp.c @@ -200,20 +199,17 @@ add_library(SrcWrapper_bin OBJECT EXCLUDE_FROM_ALL src/LL/stm32yyxx_ll_usb.c src/LL/stm32yyxx_ll_utils.c src/new.cpp - src/stm32/analog.cpp src/stm32/bootloader.c src/stm32/clock.c src/stm32/core_callback.c src/stm32/dwt.c src/stm32/hw_config.c - src/stm32/interrupt.cpp src/stm32/otp.c src/stm32/pinmap.c src/stm32/PortNames.c src/stm32/stm32_def.c src/stm32/system_stm32yyxx.c src/stm32/timer.c - src/stm32/uart.c src/syscalls.c ) target_link_libraries(SrcWrapper_bin PUBLIC SrcWrapper_usage) diff --git a/libraries/SrcWrapper/src/stm32/timer.c b/libraries/SrcWrapper/src/stm32/timer.c index aa475576ea..f76a07ad79 100644 --- a/libraries/SrcWrapper/src/stm32/timer.c +++ b/libraries/SrcWrapper/src/stm32/timer.c @@ -34,88 +34,6 @@ timerObj_t *get_timer_obj(TIM_HandleTypeDef *htim) return (obj); } -#if !defined(USE_HALV2_DRIVER) -/** - * @brief TIMER Initialization - enable the timer clock, then set priority of - * and enable the update interrupt and, if different, the capture compare interrupt - * @param htim_base TIM handle - */ -void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base) -{ - timerObj_t *obj = get_timer_obj(htim_base); - enableTimerClock(htim_base->Instance); - - // configure Update interrupt - HAL_NVIC_SetPriority(getTimerUpIrq(htim_base->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerUpIrq(htim_base->Instance)); - - if (getTimerCCIrq(htim_base->Instance) != getTimerUpIrq(htim_base->Instance)) { - // configure Capture Compare interrupt - HAL_NVIC_SetPriority(getTimerCCIrq(htim_base->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerCCIrq(htim_base->Instance)); - } -} - -/** - * @brief TIMER Deinitialization - clock and nvic - * @param htim_base TIM handle - */ -void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base) -{ - disableTimerClock(htim_base->Instance); - HAL_NVIC_DisableIRQ(getTimerUpIrq(htim_base->Instance)); - HAL_NVIC_DisableIRQ(getTimerCCIrq(htim_base->Instance)); -} - -/** - * @brief Initializes the TIM Output Compare MSP. - * @param htim TIM handle - */ -void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim) -{ - timerObj_t *obj = get_timer_obj(htim); - enableTimerClock(htim->Instance); - - // configure Update interrupt - HAL_NVIC_SetPriority(getTimerUpIrq(htim->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerUpIrq(htim->Instance)); - - if (getTimerCCIrq(htim->Instance) != getTimerUpIrq(htim->Instance)) { - // configure Capture Compare interrupt - HAL_NVIC_SetPriority(getTimerCCIrq(htim->Instance), obj->preemptPriority, obj->subPriority); - HAL_NVIC_EnableIRQ(getTimerCCIrq(htim->Instance)); - } -} - -/** - * @brief DeInitialize TIM Output Compare MSP. - * @param htim TIM handle - */ -void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim) -{ - disableTimerClock(htim->Instance); - HAL_NVIC_DisableIRQ(getTimerUpIrq(htim->Instance)); - HAL_NVIC_DisableIRQ(getTimerCCIrq(htim->Instance)); -} - -/** - * @brief Initializes the TIM Input Capture MSP. - * @param htim TIM handle - */ -void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim) -{ - enableTimerClock(htim->Instance); -} - -/** - * @brief DeInitialize TIM Input Capture MSP. - * @param htim TIM handle - */ -void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim) -{ - disableTimerClock(htim->Instance); -} -#endif /* !defined(USE_HALV2_DRIVER) */ /* Exported functions */ void enableTimerClock(TIM_TypeDef *instance) { From ce1cacc0fcefe4e2d4817a0e28dd483846d34b93 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 17 Nov 2026 15:22:13 +0100 Subject: [PATCH 3/4] perf(core): add a weak exit() to avoid linking the UART driver main() never returns, but crt0 still references exit(). The C library exit() pulls the stdio cleanup, hence _write() in Print.cpp, hence uart_debug_write() and so the whole UART driver (now an archive member), in every sketch. Provide a weak exit() that halts through _exit(). A sketch can still override it. Blink on F103 goes from 6192 to 4660 bytes (-25%), and from 9744 to 4660 bytes (-52%) with the two previous commits. Behavior change: exit() no longer runs atexit handlers or static destructors and does not flush stdio. Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 --- cores/arduino/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 643dbd95ac..9096fde524 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -23,6 +23,7 @@ */ #define ARDUINO_MAIN +#include #include "Arduino.h" /** @@ -54,6 +55,25 @@ __attribute__((constructor(101))) void premain() init(); } +extern "C" { + /** + * @brief Halt on exit(). + * + * Replaces the C library exit(), which pulls in the stdio cleanup. That + * links _write() and, through it, the whole UART driver in every sketch, + * even when main() never returns. Weak, so a sketch can provide its own. + * + * @note The atexit() handlers and static destructors are not run and stdio + * buffers are not flushed. + * + * @param status Exit status, unused. + */ + WEAK __attribute__((noreturn)) void exit(int status) + { + _exit(status); + } +} + /** * @brief Main entry point of the Arduino application. * From 151ff66c851eb5f0b1ed01fabd7c2fbceafcc377 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 19 Nov 2026 09:45:24 +0100 Subject: [PATCH 4/4] refactor(usb): call the PCD MSP functions explicitly HAL_PCD_MspInit() and HAL_PCD_MspDeInit() overrode the HAL weak functions. Rename them pcd_msp_init() and pcd_msp_deinit(), make them static and call them from USBD_LL_Init() and USBD_LL_DeInit(), so the result no longer depends on the weak vs strong symbol resolution. pcd_msp_init() is called before HAL_PCD_Init() as the HAL accesses the registers right away and needs the clock. pcd_msp_deinit() is called after HAL_PCD_DeInit(). Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- libraries/USBDevice/src/usbd_conf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libraries/USBDevice/src/usbd_conf.c b/libraries/USBDevice/src/usbd_conf.c index b778741ca0..0c20fd3179 100644 --- a/libraries/USBDevice/src/usbd_conf.c +++ b/libraries/USBDevice/src/usbd_conf.c @@ -45,9 +45,10 @@ PCD_HandleTypeDef g_hpcd; /** * @brief Initializes the PCD MSP. + * Called before HAL_PCD_Init(). * @param hpcd PCD handle */ -void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) +static void pcd_msp_init(PCD_HandleTypeDef *hpcd) { const PinMap *map = NULL; @@ -222,9 +223,10 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) /** * @brief De-Initializes the PCD MSP. + * Called after HAL_PCD_DeInit(). * @param hpcd PCD handle */ -void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) +static void pcd_msp_deinit(PCD_HandleTypeDef *hpcd) { /* Disable USB FS Clock */ #if defined (USB) @@ -507,6 +509,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) pdev->pData = &g_hpcd; /* Initialize LL Driver */ + pcd_msp_init(&g_hpcd); if (HAL_PCD_Init(&g_hpcd) != HAL_OK) { Error_Handler(); } @@ -534,6 +537,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) { HAL_PCD_DeInit(pdev->pData); + pcd_msp_deinit(pdev->pData); return USBD_OK; }