From 3dd760ee24de84d54407121270741d4ea6e14ad4 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 17 Nov 2026 10:22:27 +0100 Subject: [PATCH 1/8] 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 07d980552b4e588b253d593f6dfe709b4b34b06c Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 17 Nov 2026 15:15:44 +0100 Subject: [PATCH 2/8] 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 --- 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 f90ae2c8aef10e74043f61498702e94635a200fb Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 17 Nov 2026 15:22:13 +0100 Subject: [PATCH 3/8] 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 4ecc6f9a3c5cbf6dee283ed4500d7ae7b145897c Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 19 Nov 2026 09:45:24 +0100 Subject: [PATCH 4/8] 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 --- 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; } From ab26b4406bedab4dbecd0041144323782d544a3a Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 23 Nov 2026 14:39:00 +0100 Subject: [PATCH 5/8] refactor(core): implement _write() in the core Move _write() from the Print.cpp API patch to cores/arduino/stm32/write.cpp, so it is no longer part of the ArduinoCore-API patches. The Print::printf() and VIRTIO_LOG paths are kept. The member of core.a is only extracted when _write() is requested by the C library. The output of stdout and stderr (printf(), core_debug()) is sent to the debug UART by uart_debug_write(), which initializes it through uart_debug_init() when no Serial instance uses it. The core logs therefore work with or without Serial. Remove the _write() patch from CI/update/patch/api. The matching change in the cores/arduino/api submodule (stm32duino/ArduinoCore-API) must be applied before updating the submodule reference. Document the behavior in Tools-menu.md. Behavior change: a build with the core logs enabled (NDEBUG not defined, e.g. arduino-cli --optimize-for-debug) links the UART driver in every sketch, so a sketch defining its own USARTx_IRQHandler() fails to link. Define NDEBUG or provide its own _write(). Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 --- ...implement-weak-_write-hook-for-Print.patch | 65 ------------------- cores/arduino/CMakeLists.txt | 1 + cores/arduino/stm32/write.cpp | 55 ++++++++++++++++ docs/Tools-menu.md | 4 ++ libraries/SrcWrapper/src/syscalls.c | 2 +- 5 files changed, 61 insertions(+), 66 deletions(-) delete mode 100644 CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch create mode 100644 cores/arduino/stm32/write.cpp diff --git a/CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch b/CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch deleted file mode 100644 index 9563d94cf3..0000000000 --- a/CI/update/patch/api/0001-feat-implement-weak-_write-hook-for-Print.patch +++ /dev/null @@ -1,65 +0,0 @@ -From ed7ffc8cc475b05bf50fed6ef91007c3d75f0393 Mon Sep 17 00:00:00 2001 -From: Aymane Bahssain -Date: Fri, 23 Jan 2026 17:05:29 +0100 -Subject: [PATCH] feat: implement weak _write() hook for Print - -Signed-off-by: Aymane Bahssain ---- - api/Print.cpp | 34 ++++++++++++++++++++++++++++++++++ - 1 file changed, 34 insertions(+) - -diff --git a/api/Print.cpp b/api/Print.cpp -index d827f2e..54319f1 100644 ---- a/api/Print.cpp -+++ b/api/Print.cpp -@@ -24,6 +24,14 @@ - - #include "Print.h" - -+#ifdef ARDUINO_ARCH_STM32 -+#include -+#include "uart.h" -+#if defined (VIRTIO_LOG) -+ #include "virtio_log.h" -+#endif -+#endif -+ - using namespace arduino; - - // Public Methods ////////////////////////////////////////////////////////////// -@@ -248,6 +256,32 @@ size_t Print::println(const Printable& x) - return n; - } - -+#ifdef ARDUINO_ARCH_STM32 -+extern "C" { -+ __attribute__((weak)) -+ int _write(int file, char *ptr, int len) -+ { -+ switch (file) { -+ case STDOUT_FILENO: -+ case STDERR_FILENO: -+ /* Used for core_debug() */ -+#if defined (VIRTIO_LOG) -+ virtio_log((uint8_t *)ptr, (uint32_t)len); -+#elif defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) -+ uart_debug_write((uint8_t *)ptr, (uint32_t)len); -+#endif -+ break; -+ case STDIN_FILENO: -+ break; -+ default: -+ ((class Print *)file)->write((uint8_t *)ptr, len); -+ break; -+ } -+ return len; -+ } -+} -+#endif -+ - int Print::printf(const char *format, ...) - { - va_list ap; --- -2.51.2.windows.1 - diff --git a/cores/arduino/CMakeLists.txt b/cores/arduino/CMakeLists.txt index 9e3fce4242..cae66eb888 100644 --- a/cores/arduino/CMakeLists.txt +++ b/cores/arduino/CMakeLists.txt @@ -42,6 +42,7 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL stm32/startup_stm32yyxx.c stm32/startup_stm32yyxx.S stm32/uart.c + stm32/write.cpp Tone.cpp WInterrupts.cpp wiring_analog.c diff --git a/cores/arduino/stm32/write.cpp b/cores/arduino/stm32/write.cpp new file mode 100644 index 0000000000..367dbd7bc6 --- /dev/null +++ b/cores/arduino/stm32/write.cpp @@ -0,0 +1,55 @@ +/** + * \file write.cpp + * + * Implementation of the newlib _write() syscall, used by the printf() family, + * by Print::printf() and by core_debug(). + * + */ + +#include +#include "Arduino.h" +#include "uart.h" +#if defined(VIRTIO_LOG) + #include "virtio_log.h" +#endif + +extern "C" { + /** + * @brief Write to a file descriptor, called by the C library. + * + * The output of stdout and stderr (printf(), core_debug()...) is sent to the + * debug UART by uart_debug_write(), which initializes it when it is not + * already used by a Serial instance. + * + * @note The UART driver is linked as soon as this function is requested + * by the C library: using printf() or enabling the core logs (NDEBUG not + * defined) links it, with its strong USARTx_IRQHandler(). + * @note Weak, a sketch can provide its own. + * + * @param file STDOUT_FILENO and STDERR_FILENO for the debug output, STDIN_FILENO + * is ignored, otherwise a pointer to a Print object (Print::printf()). + * @param ptr Data to write. + * @param len Number of bytes to write. + * @return len. + */ + __attribute__((weak)) + int _write(int file, char *ptr, int len) + { + switch (file) { + case STDOUT_FILENO: + case STDERR_FILENO: +#if defined(VIRTIO_LOG) + virtio_log((uint8_t *)ptr, (uint32_t)len); +#elif defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) + uart_debug_write((uint8_t *)ptr, (uint32_t)len); +#endif + break; + case STDIN_FILENO: + break; + default: + ((class Print *)file)->write((uint8_t *)ptr, len); + break; + } + return len; + } +} diff --git a/docs/Tools-menu.md b/docs/Tools-menu.md index 06e289c65a..72b1550c11 100644 --- a/docs/Tools-menu.md +++ b/docs/Tools-menu.md @@ -77,6 +77,10 @@ Menu: "**Tools > Debug symbols and core logs**" | Core logs Enabled | _(`NDEBUG` not defined)_ | Core logs are enabled. No debug symbols. | | Core Logs and Symbols Enabled (-g) | `-g` | Debug symbols are generated and core logs are enabled. | +When the core logs are enabled (`NDEBUG` not defined), including the `arduino-cli compile --optimize-for-debug` build (`-Og -g`), the logs and the `printf()` family output (`stdout`, `stderr`) are sent to the debug UART by `_write()`, which initializes the debug UART if it is not already used by a `Serial` instance. + +This links the UART driver, with its `USARTx_IRQHandler()`, in every sketch, even one that does not use `Serial`. A sketch defining its own `USARTx_IRQHandler()` then fails to link with a "multiple definition" error. Define `NDEBUG` (e.g. `-DNDEBUG` in the extra compiler flags) to disable the core logs, or provide your own `_write()`. In a build with `NDEBUG` defined, the UART driver is linked only if `printf()` is used. + ## C Runtime Library Menu: "**Tools > C Runtime Library**" diff --git a/libraries/SrcWrapper/src/syscalls.c b/libraries/SrcWrapper/src/syscalls.c index b9ff7d5289..ce6f11b7da 100644 --- a/libraries/SrcWrapper/src/syscalls.c +++ b/libraries/SrcWrapper/src/syscalls.c @@ -112,7 +112,7 @@ int _read(int file, char *ptr, int len) return 0; } -/* Moved to Print.cpp to support Print::printf() +/* Moved to cores/arduino/stm32/write.cpp to support Print::printf() __attribute__((weak)) int _write(int file, char *ptr, int len) { From cd6e70a61a07fe9f7070b09ee6d38fc09f256fc3 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 24 Nov 2026 09:23:06 +0100 Subject: [PATCH 6/8] perf(uart): split the interrupt driven code from the UART driver The debug UART (core logs, printf()) only needs uart_init() and the polling uart_debug_write(): its interrupt is never enabled. But the strong USARTx_IRQHandler(), the HAL callbacks and the interrupt driven functions were in uart.c, so they were linked with it. A sketch with the core logs enabled (NDEBUG not defined, e.g. arduino-cli --optimize-for-debug) could not define its own USARTx_IRQHandler(), and every such sketch carried the Serial code. Move to cores/arduino/stm32/uart_serial.c: - the USARTx_IRQHandler(); - uart_getc(), uart_attach_rx_callback(), uart_attach_tx_callback(), uart_enable_tx(), uart_enable_rx() and serial_rx_active(); - HAL_UART_RxCpltCallback(), HAL_UART_TxCpltCallback(), HAL_UART_ErrorCallback() and HAL_UARTEx_WakeupCallback(). HardwareSerial references these functions, so the member of core.a is only extracted when it is used, and its strong handlers override the weak ones as before. uart.c keeps the initialization, the clock, the deinitialization, the low power configuration and the debug output. The new uart_private.h shares uart_index_t, uart_handlers[] (not static anymore) and get_serial_obj() between the two files. Flash, before -> after: - Blink, debug build: F103RB 13588 -> 11964 (-12%), H743ZI2 28068 -> 23500 (-16%) - printf() only, release build, F103RB: 9868 -> 8440 - Serial, release build: F103RB 9396 -> 9436, H743ZI2 21836 -> 21884 Behavior change: a sketch not using Serial can define its own USARTx_IRQHandler() in a build with the core logs enabled. A sketch using Serial or a HardwareSerial still links all the handlers. Update Tools-menu.md. Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 --- cores/arduino/CMakeLists.txt | 1 + cores/arduino/stm32/uart.c | 533 +---------------------------- cores/arduino/stm32/uart_private.h | 98 ++++++ cores/arduino/stm32/uart_serial.c | 516 ++++++++++++++++++++++++++++ docs/Tools-menu.md | 2 +- 5 files changed, 619 insertions(+), 531 deletions(-) create mode 100644 cores/arduino/stm32/uart_private.h create mode 100644 cores/arduino/stm32/uart_serial.c diff --git a/cores/arduino/CMakeLists.txt b/cores/arduino/CMakeLists.txt index cae66eb888..5dccf416ec 100644 --- a/cores/arduino/CMakeLists.txt +++ b/cores/arduino/CMakeLists.txt @@ -42,6 +42,7 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL stm32/startup_stm32yyxx.c stm32/startup_stm32yyxx.S stm32/uart.c + stm32/uart_serial.c stm32/write.cpp Tone.cpp WInterrupts.cpp diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index a2d1289342..97429714c8 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -13,6 +13,7 @@ #include "core_debug.h" #include "lock_resource.h" #include "uart.h" +#include "uart_private.h" #include "Arduino.h" #include "PinAF_STM32F1.h" #include "stm32yyxx_ll_rcc.h" @@ -37,60 +38,10 @@ extern "C" { #define DEBUG_UART_BAUDRATE 9600 #endif -/* @brief uart characteristics */ -typedef enum { -#if defined(USART1_BASE) - UART1_INDEX, -#endif -#if defined(USART2_BASE) - UART2_INDEX, -#endif -#if defined(USART3_BASE) - UART3_INDEX, -#endif -#if defined(UART4_BASE) || defined(USART4_BASE) - UART4_INDEX, -#endif -#if defined(UART5_BASE) || defined(USART5_BASE) - UART5_INDEX, -#endif -#if defined(USART6_BASE) - UART6_INDEX, -#endif -#if defined(UART7_BASE) || defined(USART7_BASE) - UART7_INDEX, -#endif -#if defined(UART8_BASE) || defined(USART8_BASE) - UART8_INDEX, -#endif -#if defined(UART9_BASE) - UART9_INDEX, -#endif -#if defined(UART10_BASE) || defined(USART10_BASE) - UART10_INDEX, -#endif -#if defined(USART11_BASE) - UART11_INDEX, -#endif -#if defined(UART12_BASE) - UART12_INDEX, -#endif -#if defined(LPUART1_BASE) - LPUART1_INDEX, -#endif -#if defined(LPUART2_BASE) - LPUART2_INDEX, -#endif -#if defined(LPUART3_BASE) - LPUART3_INDEX, -#endif - UART_NUM -} uart_index_t; - #if defined(USE_HALV2_DRIVER) -static hal_uart_handle_t *uart_handlers[UART_NUM] = {NULL}; +hal_uart_handle_t *uart_handlers[UART_NUM] = {NULL}; #else -static UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL}; +UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL}; #endif static serial_t serial_debug = { @@ -1009,15 +960,6 @@ size_t uart_debug_write(uint8_t *data, uint32_t size) return size; } -uint8_t serial_rx_active(serial_t *obj) -{ -#if defined(USE_HALV2_DRIVER) - return ((HAL_UART_GetRxState(uart_handlers[obj->index]) & HAL_UART_RX_STATE_ACTIVE) == HAL_UART_RX_STATE_ACTIVE); -#else - return ((HAL_UART_GetState(uart_handlers[obj->index]) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX); -#endif -} - uint8_t serial_tx_active(serial_t *obj) { #if defined(USE_HALV2_DRIVER) @@ -1027,99 +969,6 @@ uint8_t serial_tx_active(serial_t *obj) #endif } -int uart_getc(serial_t *obj, unsigned char *c) -{ - if (obj == NULL) { - return -1; - } - - if (serial_rx_active(obj)) { - return -1; /* Transaction ongoing */ - } - - *c = (unsigned char)(obj->recv); - /* Restart RX irq */ - HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); - - return 0; -} - -void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *)) -{ - if (obj == NULL) { - return; - } - - /* Exit if a reception is already on-going */ - if (serial_rx_active(obj)) { - return; - } - obj->rx_callback = callback; - - /* Must disable interrupt to prevent handle lock contention */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_DisableIRQ(obj->irq); -#else - HAL_NVIC_DisableIRQ(obj->irq); -#endif - - HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); - - /* Enable interrupt */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_EnableIRQ(obj->irq); -#else - HAL_NVIC_EnableIRQ(obj->irq); -#endif -} - -void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size) -{ - if (obj == NULL) { - return; - } - obj->tx_callback = callback; - - /* Must disable interrupt to prevent handle lock contention */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_DisableIRQ(obj->irq); -#else - HAL_NVIC_DisableIRQ(obj->irq); -#endif - - /* The following function will enable UART_IT_TXE and error interrupts */ - HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], size); - - /* Enable interrupt */ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_EnableIRQ(obj->irq); -#else - HAL_NVIC_EnableIRQ(obj->irq); -#endif -} - -void uart_enable_tx(serial_t *obj) -{ - if (obj != NULL && obj->pin_rx == NC) { -#if defined(USE_HALV2_DRIVER) - HAL_UART_EnableTransmitter(uart_handlers[obj->index]); -#else - HAL_HalfDuplex_EnableTransmitter(uart_handlers[obj->index]); -#endif - } -} - -void uart_enable_rx(serial_t *obj) -{ - if (obj != NULL && obj->pin_rx == NC) { -#if defined(USE_HALV2_DRIVER) - HAL_UART_EnableReceiver(uart_handlers[obj->index]); -#else - HAL_HalfDuplex_EnableReceiver(uart_handlers[obj->index]); -#endif - } -} - /* * Unused: return the index of the serial handler (UART_NUM if not found) * @@ -1140,382 +989,6 @@ uint8_t uart_index(UART_HandleTypeDef *huart) } */ -/** - * @brief Rx Transfer completed callback: call the RX callback of the serial object - * @param huart Pointer on the uart reference - * @param size_byte Number of received bytes (HALv2 driver only, unused) - * @param rx_event Reception event type (HALv2 driver only, unused) - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UART_RxCpltCallback(hal_uart_handle_t *huart, uint32_t size_byte, hal_uart_rx_event_types_t rx_event) -#else -void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) -#endif -{ - serial_t *obj = get_serial_obj(huart); - if (obj) { -#if defined(USE_HALV2_DRIVER) - (void)size_byte; - (void)rx_event; -#endif - obj->rx_callback(obj); - } -} - -/** - * @brief Tx Transfer completed callback: call the TX callback of the serial object - * @param huart Pointer on the uart reference - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UART_TxCpltCallback(hal_uart_handle_t *huart) -#else -void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) -#endif -{ - serial_t *obj = get_serial_obj(huart); - if (obj) { - obj->tx_callback(obj); - } -} - -/** - * @brief Error callback from UART: clear the error flag and restart the reception - * @param huart Pointer on the uart reference - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UART_ErrorCallback(hal_uart_handle_t *huart) -#else -void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) -#endif -{ -#if defined(USE_HALV2_DRIVER) - USART_TypeDef *p_uartx = ((USART_TypeDef *)((uint32_t)(huart)->instance)); - if (LL_USART_IsActiveFlag_PE(p_uartx)) { - LL_USART_ClearFlag_PE(p_uartx); /* Clear PE flag */ - } else if (LL_USART_IsActiveFlag_FE(p_uartx)) { - LL_USART_ClearFlag_FE(p_uartx); /* Clear FE flag */ - } else if (LL_USART_IsActiveFlag_NE(p_uartx)) { - LL_USART_ClearFlag_NE(p_uartx); /* Clear NE flag */ - } else if (LL_USART_IsActiveFlag_ORE(p_uartx)) { - LL_USART_ClearFlag_ORE(p_uartx); /* Clear ORE flag */ - } -#else -#if defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32L1xx) - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - __HAL_UART_CLEAR_PEFLAG(huart); /* Clear PE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - __HAL_UART_CLEAR_FEFLAG(huart); /* Clear FE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - __HAL_UART_CLEAR_NEFLAG(huart); /* Clear NE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - __HAL_UART_CLEAR_OREFLAG(huart); /* Clear ORE flag */ - } -#else - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); /* Clear PE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); /* Clear FE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); /* Clear NE flag */ - } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); /* Clear ORE flag */ - } -#endif -#endif /* USE_HALV2_DRIVER */ - /* Restart receive interrupt after any error */ - serial_t *obj = get_serial_obj(huart); - if (obj && !serial_rx_active(obj)) { - HAL_UART_Receive_IT(huart, &(obj->recv), 1); - } -} - -/** - * @brief USART 1 IRQ handler - */ -#if defined(USART1_BASE) -void USART1_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART1_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART1_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART1_INDEX]); -} -#endif - -/** - * @brief USART 2 IRQ handler - */ -#if defined(USART2_BASE) -void USART2_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART2_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART2_IRQn); -#endif - if (uart_handlers[UART2_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART2_INDEX]); - } -#if (defined(STM32G0xx) || defined(STM32U0xx)) && defined(LPUART2_BASE) - if (uart_handlers[LPUART2_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[LPUART2_INDEX]); - } -#endif -} -#endif - -/** - * @brief USART 3 IRQ handler - */ -#if defined(USART3_BASE) -void USART3_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART3_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART3_IRQn); -#endif -#if defined(STM32F091xC) || defined (STM32F098xx) - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART3) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART4) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART5) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART6) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART7) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); - } - if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART8) != RESET) { - HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); - } -#else - if (uart_handlers[UART3_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); - } -#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32U0xx) - /* USART3_4_IRQn */ - if (uart_handlers[UART4_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } -#if defined(STM32F030xC) || defined(STM32G0xx) && (defined(LPUART2_BASE) || defined(USART5_BASE)) - if (uart_handlers[UART5_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); - } - if (uart_handlers[UART6_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); - } -#endif /* STM32F030xC */ -#if (defined(STM32G0xx) || STM32U0xx) && defined(LPUART1_BASE) - if (uart_handlers[LPUART1_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); - } -#endif /* (STM32G0xx || STM32U0xx) && LPUART1_BASE */ -#endif /* STM32F0xx || STM32G0xx || STM32U0xx */ -#endif /* STM32F091xC || STM32F098xx */ -} -#endif - -/** - * @brief UART 4 IRQ handler - */ -#if defined(UART4_BASE) -void UART4_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(UART4_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(UART4_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); -} -#endif - -/** - * @brief USART 4/5 IRQ handler - */ -#if defined(STM32L0xx) -#if defined(USART4_BASE) || defined(USART5_BASE) -void USART4_5_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART4_IRQn); - if (uart_handlers[UART4_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } - if (uart_handlers[UART5_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); - } -} -#endif -#endif - -/** - * @brief USART 4 IRQ handler - */ -#if defined(STM32U0xx) -#if defined(USART4_BASE) -void USART4_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART4_IRQn); - if (uart_handlers[UART4_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); - } -#if defined(LPUART3_BASE) - if (uart_handlers[LPUART3_INDEX] != NULL) { - HAL_UART_IRQHandler(uart_handlers[LPUART3_INDEX]); - } -#endif -} -#endif -#endif - -/** - * @brief USART 5 IRQ handler - */ -#if defined(UART5_BASE) -void UART5_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(UART5_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(UART5_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); -} -#endif - -/** - * @brief USART 6 IRQ handler - */ -#if defined(USART6_BASE) && !defined(STM32F0xx) && !defined(STM32G0xx) -void USART6_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(USART6_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(USART6_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); -} -#endif - -/** - * @brief LPUART 1 IRQ handler - */ -#if defined(LPUART1_BASE) -void LPUART1_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(LPUART1_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(LPUART1_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); -} -#endif - -/** - * @brief UART 7 IRQ handler - */ -#if defined(UART7_BASE) -void UART7_IRQHandler(void) -{ -#if defined(USE_HALV2_DRIVER) - HAL_CORTEX_NVIC_ClearPendingIRQ(UART7_IRQn); -#else - HAL_NVIC_ClearPendingIRQ(UART7_IRQn); -#endif - HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); -} -#endif - -/** - * @brief UART 8 IRQ handler - */ -#if defined(UART8_BASE) -void UART8_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART8_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); -} -#endif - -/** - * @brief UART 9 IRQ handler - */ -#if defined(UART9_BASE) -void UART9_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART9_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART9_INDEX]); -} -#endif - -/** - * @brief UART 10 IRQ handler - */ -#if defined(UART10_BASE) -void UART10_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART10_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); -} -#endif - -/** - * @brief USART 10 IRQ handler - */ -#if defined(USART10_BASE) -void USART10_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART10_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); -} -#endif - -/** - * @brief USART 11 IRQ handler - */ -#if defined(USART11_BASE) -void USART11_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(USART11_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART11_INDEX]); -} -#endif - -/** - * @brief UART 12 IRQ handler - */ -#if defined(UART12_BASE) -void UART12_IRQHandler(void) -{ - HAL_NVIC_ClearPendingIRQ(UART12_IRQn); - HAL_UART_IRQHandler(uart_handlers[UART12_INDEX]); -} -#endif - -/** - * @brief HAL UART wakeup callback: restart the reception - * @param huart UART handle - */ -#if defined(USE_HALV2_DRIVER) -void HAL_UARTEx_WakeupCallback(hal_uart_handle_t *huart) -#else -void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) -#endif -{ - serial_t *obj = get_serial_obj(huart); - HAL_UART_Receive_IT(huart, &(obj->recv), 1); -} - #if defined(USE_HALV2_DRIVER) hal_uart_prescaler_t uart_compute_prescaler(hal_uart_handle_t *huart, hal_uart_config_t *config) { diff --git a/cores/arduino/stm32/uart_private.h b/cores/arduino/stm32/uart_private.h new file mode 100644 index 0000000000..2d07248048 --- /dev/null +++ b/cores/arduino/stm32/uart_private.h @@ -0,0 +1,98 @@ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ +/** + * @file uart_private.h + * @brief Definitions shared by uart.c and uart_serial.c, not part of the API. + */ +#ifndef _UART_PRIVATE_H_ +#define _UART_PRIVATE_H_ + +#include "uart.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) + +/* @brief uart characteristics */ +typedef enum { +#if defined(USART1_BASE) + UART1_INDEX, +#endif +#if defined(USART2_BASE) + UART2_INDEX, +#endif +#if defined(USART3_BASE) + UART3_INDEX, +#endif +#if defined(UART4_BASE) || defined(USART4_BASE) + UART4_INDEX, +#endif +#if defined(UART5_BASE) || defined(USART5_BASE) + UART5_INDEX, +#endif +#if defined(USART6_BASE) + UART6_INDEX, +#endif +#if defined(UART7_BASE) || defined(USART7_BASE) + UART7_INDEX, +#endif +#if defined(UART8_BASE) || defined(USART8_BASE) + UART8_INDEX, +#endif +#if defined(UART9_BASE) + UART9_INDEX, +#endif +#if defined(UART10_BASE) || defined(USART10_BASE) + UART10_INDEX, +#endif +#if defined(USART11_BASE) + UART11_INDEX, +#endif +#if defined(UART12_BASE) + UART12_INDEX, +#endif +#if defined(LPUART1_BASE) + LPUART1_INDEX, +#endif +#if defined(LPUART2_BASE) + LPUART2_INDEX, +#endif +#if defined(LPUART3_BASE) + LPUART3_INDEX, +#endif + UART_NUM +} uart_index_t; + +/* HAL handles of the initialized U(S)ART, indexed by uart_index_t */ +#if defined(USE_HALV2_DRIVER) +extern hal_uart_handle_t *uart_handlers[UART_NUM]; +#else +extern UART_HandleTypeDef *uart_handlers[UART_NUM]; +#endif + +/* Get the serial_t object from its HAL UART handle, defined in uart.c */ +#if defined(USE_HALV2_DRIVER) +serial_t *get_serial_obj(hal_uart_handle_t *huart); +#else +serial_t *get_serial_obj(UART_HandleTypeDef *huart); +#endif + +#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */ + +#ifdef __cplusplus +} +#endif + +#endif /* _UART_PRIVATE_H_ */ diff --git a/cores/arduino/stm32/uart_serial.c b/cores/arduino/stm32/uart_serial.c new file mode 100644 index 0000000000..59ac4fd4f6 --- /dev/null +++ b/cores/arduino/stm32/uart_serial.c @@ -0,0 +1,516 @@ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ +/** + * @file uart_serial.c + * @brief Interrupt driven part of the U(S)ART driver, used by HardwareSerial: + * reception and transmission, HAL callbacks and IRQ handlers. + * + * Kept apart from uart.c, so a sketch which only uses the debug UART + * (core logs, printf()) does not link the strong USARTx_IRQHandler() and can + * define its own. This file is a member of core.a, it is extracted when + * HardwareSerial references one of its functions. + */ +#include "Arduino.h" +#include "uart.h" +#include "uart_private.h" + +#ifdef __cplusplus +extern "C" { +#endif +#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) + +uint8_t serial_rx_active(serial_t *obj) +{ +#if defined(USE_HALV2_DRIVER) + return ((HAL_UART_GetRxState(uart_handlers[obj->index]) & HAL_UART_RX_STATE_ACTIVE) == HAL_UART_RX_STATE_ACTIVE); +#else + return ((HAL_UART_GetState(uart_handlers[obj->index]) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX); +#endif +} + +int uart_getc(serial_t *obj, unsigned char *c) +{ + if (obj == NULL) { + return -1; + } + + if (serial_rx_active(obj)) { + return -1; /* Transaction ongoing */ + } + + *c = (unsigned char)(obj->recv); + /* Restart RX irq */ + HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); + + return 0; +} + +void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *)) +{ + if (obj == NULL) { + return; + } + + /* Exit if a reception is already on-going */ + if (serial_rx_active(obj)) { + return; + } + obj->rx_callback = callback; + + /* Must disable interrupt to prevent handle lock contention */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_DisableIRQ(obj->irq); +#else + HAL_NVIC_DisableIRQ(obj->irq); +#endif + + HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); + + /* Enable interrupt */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_EnableIRQ(obj->irq); +#else + HAL_NVIC_EnableIRQ(obj->irq); +#endif +} + +void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size) +{ + if (obj == NULL) { + return; + } + obj->tx_callback = callback; + + /* Must disable interrupt to prevent handle lock contention */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_DisableIRQ(obj->irq); +#else + HAL_NVIC_DisableIRQ(obj->irq); +#endif + + /* The following function will enable UART_IT_TXE and error interrupts */ + HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], size); + + /* Enable interrupt */ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_EnableIRQ(obj->irq); +#else + HAL_NVIC_EnableIRQ(obj->irq); +#endif +} + +void uart_enable_tx(serial_t *obj) +{ + if (obj != NULL && obj->pin_rx == NC) { +#if defined(USE_HALV2_DRIVER) + HAL_UART_EnableTransmitter(uart_handlers[obj->index]); +#else + HAL_HalfDuplex_EnableTransmitter(uart_handlers[obj->index]); +#endif + } +} + +void uart_enable_rx(serial_t *obj) +{ + if (obj != NULL && obj->pin_rx == NC) { +#if defined(USE_HALV2_DRIVER) + HAL_UART_EnableReceiver(uart_handlers[obj->index]); +#else + HAL_HalfDuplex_EnableReceiver(uart_handlers[obj->index]); +#endif + } +} + +/** + * @brief Rx Transfer completed callback: call the RX callback of the serial object + * @param huart Pointer on the uart reference + * @param size_byte Number of received bytes (HALv2 driver only, unused) + * @param rx_event Reception event type (HALv2 driver only, unused) + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UART_RxCpltCallback(hal_uart_handle_t *huart, uint32_t size_byte, hal_uart_rx_event_types_t rx_event) +#else +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +#endif +{ + serial_t *obj = get_serial_obj(huart); + if (obj) { +#if defined(USE_HALV2_DRIVER) + (void)size_byte; + (void)rx_event; +#endif + obj->rx_callback(obj); + } +} + +/** + * @brief Tx Transfer completed callback: call the TX callback of the serial object + * @param huart Pointer on the uart reference + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UART_TxCpltCallback(hal_uart_handle_t *huart) +#else +void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) +#endif +{ + serial_t *obj = get_serial_obj(huart); + if (obj) { + obj->tx_callback(obj); + } +} + +/** + * @brief Error callback from UART: clear the error flag and restart the reception + * @param huart Pointer on the uart reference + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UART_ErrorCallback(hal_uart_handle_t *huart) +#else +void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) +#endif +{ +#if defined(USE_HALV2_DRIVER) + USART_TypeDef *p_uartx = ((USART_TypeDef *)((uint32_t)(huart)->instance)); + if (LL_USART_IsActiveFlag_PE(p_uartx)) { + LL_USART_ClearFlag_PE(p_uartx); /* Clear PE flag */ + } else if (LL_USART_IsActiveFlag_FE(p_uartx)) { + LL_USART_ClearFlag_FE(p_uartx); /* Clear FE flag */ + } else if (LL_USART_IsActiveFlag_NE(p_uartx)) { + LL_USART_ClearFlag_NE(p_uartx); /* Clear NE flag */ + } else if (LL_USART_IsActiveFlag_ORE(p_uartx)) { + LL_USART_ClearFlag_ORE(p_uartx); /* Clear ORE flag */ + } +#else +#if defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32L1xx) + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { + __HAL_UART_CLEAR_PEFLAG(huart); /* Clear PE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { + __HAL_UART_CLEAR_FEFLAG(huart); /* Clear FE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { + __HAL_UART_CLEAR_NEFLAG(huart); /* Clear NE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + __HAL_UART_CLEAR_OREFLAG(huart); /* Clear ORE flag */ + } +#else + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); /* Clear PE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); /* Clear FE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); /* Clear NE flag */ + } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); /* Clear ORE flag */ + } +#endif +#endif /* USE_HALV2_DRIVER */ + /* Restart receive interrupt after any error */ + serial_t *obj = get_serial_obj(huart); + if (obj && !serial_rx_active(obj)) { + HAL_UART_Receive_IT(huart, &(obj->recv), 1); + } +} + +/** + * @brief HAL UART wakeup callback: restart the reception + * @param huart UART handle + */ +#if defined(USE_HALV2_DRIVER) +void HAL_UARTEx_WakeupCallback(hal_uart_handle_t *huart) +#else +void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) +#endif +{ + serial_t *obj = get_serial_obj(huart); + HAL_UART_Receive_IT(huart, &(obj->recv), 1); +} + +/** + * @brief USART 1 IRQ handler + */ +#if defined(USART1_BASE) +void USART1_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART1_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART1_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART1_INDEX]); +} +#endif + +/** + * @brief USART 2 IRQ handler + */ +#if defined(USART2_BASE) +void USART2_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART2_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART2_IRQn); +#endif + if (uart_handlers[UART2_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART2_INDEX]); + } +#if (defined(STM32G0xx) || defined(STM32U0xx)) && defined(LPUART2_BASE) + if (uart_handlers[LPUART2_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[LPUART2_INDEX]); + } +#endif +} +#endif + +/** + * @brief USART 3 IRQ handler + */ +#if defined(USART3_BASE) +void USART3_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART3_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART3_IRQn); +#endif +#if defined(STM32F091xC) || defined (STM32F098xx) + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART3) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART4) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART5) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART6) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART7) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); + } + if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART8) != RESET) { + HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); + } +#else + if (uart_handlers[UART3_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART3_INDEX]); + } +#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32U0xx) + /* USART3_4_IRQn */ + if (uart_handlers[UART4_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } +#if defined(STM32F030xC) || defined(STM32G0xx) && (defined(LPUART2_BASE) || defined(USART5_BASE)) + if (uart_handlers[UART5_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); + } + if (uart_handlers[UART6_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); + } +#endif /* STM32F030xC */ +#if (defined(STM32G0xx) || STM32U0xx) && defined(LPUART1_BASE) + if (uart_handlers[LPUART1_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); + } +#endif /* (STM32G0xx || STM32U0xx) && LPUART1_BASE */ +#endif /* STM32F0xx || STM32G0xx || STM32U0xx */ +#endif /* STM32F091xC || STM32F098xx */ +} +#endif + +/** + * @brief UART 4 IRQ handler + */ +#if defined(UART4_BASE) +void UART4_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(UART4_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(UART4_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); +} +#endif + +/** + * @brief USART 4/5 IRQ handler + */ +#if defined(STM32L0xx) +#if defined(USART4_BASE) || defined(USART5_BASE) +void USART4_5_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART4_IRQn); + if (uart_handlers[UART4_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } + if (uart_handlers[UART5_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); + } +} +#endif +#endif + +/** + * @brief USART 4 IRQ handler + */ +#if defined(STM32U0xx) +#if defined(USART4_BASE) +void USART4_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART4_IRQn); + if (uart_handlers[UART4_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]); + } +#if defined(LPUART3_BASE) + if (uart_handlers[LPUART3_INDEX] != NULL) { + HAL_UART_IRQHandler(uart_handlers[LPUART3_INDEX]); + } +#endif +} +#endif +#endif + +/** + * @brief USART 5 IRQ handler + */ +#if defined(UART5_BASE) +void UART5_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(UART5_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(UART5_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]); +} +#endif + +/** + * @brief USART 6 IRQ handler + */ +#if defined(USART6_BASE) && !defined(STM32F0xx) && !defined(STM32G0xx) +void USART6_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(USART6_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(USART6_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART6_INDEX]); +} +#endif + +/** + * @brief LPUART 1 IRQ handler + */ +#if defined(LPUART1_BASE) +void LPUART1_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(LPUART1_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(LPUART1_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[LPUART1_INDEX]); +} +#endif + +/** + * @brief UART 7 IRQ handler + */ +#if defined(UART7_BASE) +void UART7_IRQHandler(void) +{ +#if defined(USE_HALV2_DRIVER) + HAL_CORTEX_NVIC_ClearPendingIRQ(UART7_IRQn); +#else + HAL_NVIC_ClearPendingIRQ(UART7_IRQn); +#endif + HAL_UART_IRQHandler(uart_handlers[UART7_INDEX]); +} +#endif + +/** + * @brief UART 8 IRQ handler + */ +#if defined(UART8_BASE) +void UART8_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART8_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART8_INDEX]); +} +#endif + +/** + * @brief UART 9 IRQ handler + */ +#if defined(UART9_BASE) +void UART9_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART9_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART9_INDEX]); +} +#endif + +/** + * @brief UART 10 IRQ handler + */ +#if defined(UART10_BASE) +void UART10_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART10_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); +} +#endif + +/** + * @brief USART 10 IRQ handler + */ +#if defined(USART10_BASE) +void USART10_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART10_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART10_INDEX]); +} +#endif + +/** + * @brief USART 11 IRQ handler + */ +#if defined(USART11_BASE) +void USART11_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(USART11_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART11_INDEX]); +} +#endif + +/** + * @brief UART 12 IRQ handler + */ +#if defined(UART12_BASE) +void UART12_IRQHandler(void) +{ + HAL_NVIC_ClearPendingIRQ(UART12_IRQn); + HAL_UART_IRQHandler(uart_handlers[UART12_INDEX]); +} +#endif + +#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */ + +#ifdef __cplusplus +} +#endif + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/docs/Tools-menu.md b/docs/Tools-menu.md index 72b1550c11..188ac06fb4 100644 --- a/docs/Tools-menu.md +++ b/docs/Tools-menu.md @@ -79,7 +79,7 @@ Menu: "**Tools > Debug symbols and core logs**" When the core logs are enabled (`NDEBUG` not defined), including the `arduino-cli compile --optimize-for-debug` build (`-Og -g`), the logs and the `printf()` family output (`stdout`, `stderr`) are sent to the debug UART by `_write()`, which initializes the debug UART if it is not already used by a `Serial` instance. -This links the UART driver, with its `USARTx_IRQHandler()`, in every sketch, even one that does not use `Serial`. A sketch defining its own `USARTx_IRQHandler()` then fails to link with a "multiple definition" error. Define `NDEBUG` (e.g. `-DNDEBUG` in the extra compiler flags) to disable the core logs, or provide your own `_write()`. In a build with `NDEBUG` defined, the UART driver is linked only if `printf()` is used. +The debug UART is driven by polling: it links the UART driver but not its interrupt handlers. The `USARTx_IRQHandler()` are only linked when the sketch uses `Serial` or a `HardwareSerial`, so a sketch which does not use them can define its own `USARTx_IRQHandler()` in a debug build. In a build with `NDEBUG` defined, the UART driver is linked only if `printf()` is used. Provide your own `_write()` to send the output elsewhere. ## C Runtime Library Menu: "**Tools > C Runtime Library**" From a9d381eac96e6b8dafec3ed1b2a8f68899873495 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 24 Nov 2026 09:47:37 +0100 Subject: [PATCH 7/8] chore(api): update ArduinoCore-API submodule Update cores/arduino/api to the revert of the weak _write() hook in Print.cpp: ed8a91e Revert "feat: implement weak _write() hook for Print" _write() is now implemented in the core, in cores/arduino/stm32/write.cpp, and the matching patch is removed from CI/update/patch/api. Without this update, both Print.cpp and write.cpp define a weak _write(). Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 --- cores/arduino/api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/api b/cores/arduino/api index bce6000b5b..ed8a91e44a 160000 --- a/cores/arduino/api +++ b/cores/arduino/api @@ -1 +1 @@ -Subproject commit bce6000b5b1aaa6cc15374ecdc0ba5c80136e93c +Subproject commit ed8a91e44a43e67603a66c4f33338919c093a136 From 90476a1e2e5f98583e5a8ac3068cf71aed86fd23 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 24 Nov 2026 13:40:11 +0100 Subject: [PATCH 8/8] fix(cmake): scan the core again after the C library The C library requests _write() when it is linked, for example through vdprintf() used by Print::printf(), but it is scanned after libcore_bin.a: write.cpp.o was never extracted and the link of the CMake build failed with "undefined reference to `_write'". The Arduino build does not have the issue as platform.txt puts the core archive and the C library in the same --start-group ... --end-group. Do the same in overall_settings(): add the core archive and the C library in a LINK_GROUP:RESCAN (CMake 3.24 or later). The nano C library is c_nano, as --specs=nano.specs adds it after the libraries of the link line. The file of the archive is used, not the target, to not create a dependency cycle. Signed-off-by: Frederic Pillon Assisted-by: GitHub Copilot: Claude Sonnet 5.5 --- cmake/overall_settings.cmake | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmake/overall_settings.cmake b/cmake/overall_settings.cmake index ec74e7221e..e20b24202b 100644 --- a/cmake/overall_settings.cmake +++ b/cmake/overall_settings.cmake @@ -50,6 +50,21 @@ function(overall_settings) endif() endif() + # The C library needs symbols defined in the core, but it is scanned after it + # (e.g. _write() requested by vdprintf()): scan the core again together with + # the C library, like the --start-group ... -lc --end-group of platform.txt. + # The file of the library is used, not the target, to not create a dependency cycle. + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) + if(${WANT_STANDARD_LIBC}) + set(CORE_LIBC c) + else() + set(CORE_LIBC c_nano) + endif() + target_link_libraries(user_settings INTERFACE + "$,${CORE_LIBC}>" + ) + endif() + if(NOT DEFINED WANT_OPTIMIZATION) set(WANT_OPTIMIZATION "s") endif()