Skip to content

Commit 3dab209

Browse files
authored
Fix for using LTO in Arduino IDE 2.x to reduce flash usage #215
Fix #204, #179, #25. First only for CH32V003, CH32VM00X, other may follow... Flash memory fills up quickly, especially on chips like the V003 and V002 that have only 16kB of flash. As mentioned in this comment, one of the ways to reduce flash usage, is to use Link Time Optimization (LTO). LTO removes code that is never called; potentially saving multiple kilobytes of valuable flash memory. This optimization worked great in version 1.8.19 of the Arduino IDE, but didn't work in any v2.x IDE (see also another comment). Note that LTO can also reduce RAM usage. After lengthy research (see #204) the main cause appeared to be a known issue in the used GCC compiler/linker, related to how weak functions defined in assembly files can take precedence over regular functions that were supposed to redefine their weak counterparts. For the CH32 Arduino core this issue caused important interrupt handlers such as the SysTick handler to be removed when using LTO, leaving the microcontroller hanging in the endless loop of the default handler when such an interrupt occurred. So far the best workaround found to get LTO working again, was to move the assembly implementation of the default handlers from startup_ch32....S to a C implementation in ch32...._misc.c. Note: both assembly and C implementations use just two bytes of flash. For the CH32V003 the code is somewhat smaller than before (even without LTO!). Thanks @dfleck for contributing this solution. This PR gives priority to fixing LTO on 16kB processors (mainly V003/V002). Others may follow later. Although the fix is fairly straightforward, I prefer to only submit tested changes and don't have all member of the CH32 family in my collection. See #204 for more information. Related issues: #179, #25, openwch/arduino_core_ch32#215
1 parent b03ee92 commit 3dab209

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

system/CH32VM00X/SRC/Peripheral/src/ch32v00X_misc.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,40 @@ void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
7979
NVIC_DisableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
8080
}
8181
}
82+
83+
84+
85+
/* MMOLE 250727: moved from startup_ch32v00X.S to ch32v00X_misc.c (see issue #204) */
86+
__attribute__ ((weak)) void while1_handler(void) {while(1);};
87+
__attribute__ ((weak)) void SW_Handler (void) __attribute__ ((weak, alias ("while1_handler")));
88+
__attribute__ ((weak)) void WWDG_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
89+
__attribute__ ((weak)) void PVD_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
90+
__attribute__ ((weak)) void FLASH_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
91+
__attribute__ ((weak)) void RCC_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
92+
__attribute__ ((weak)) void EXTI7_0_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
93+
__attribute__ ((weak)) void AWU_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
94+
__attribute__ ((weak)) void DMA1_Channel1_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
95+
__attribute__ ((weak)) void DMA1_Channel2_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
96+
__attribute__ ((weak)) void DMA1_Channel3_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
97+
__attribute__ ((weak)) void DMA1_Channel4_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
98+
__attribute__ ((weak)) void DMA1_Channel5_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
99+
__attribute__ ((weak)) void DMA1_Channel6_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
100+
__attribute__ ((weak)) void DMA1_Channel7_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
101+
__attribute__ ((weak)) void ADC1_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
102+
__attribute__ ((weak)) void I2C1_EV_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
103+
__attribute__ ((weak)) void I2C1_ER_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
104+
__attribute__ ((weak)) void USART1_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
105+
__attribute__ ((weak)) void SPI1_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
106+
__attribute__ ((weak)) void TIM1_BRK_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
107+
__attribute__ ((weak)) void TIM1_UP_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
108+
__attribute__ ((weak)) void TIM1_TRG_COM_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
109+
__attribute__ ((weak)) void TIM1_CC_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
110+
__attribute__ ((weak)) void TIM2_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
111+
__attribute__ ((weak)) void USART2_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
112+
__attribute__ ((weak)) void OPCM_IRQHandler(void) __attribute__ ((weak, alias ("while1_handler")));
113+
114+
/*
115+
.weak NMI_Handler
116+
.weak HardFault_Handler
117+
.weak SysTick_Handler
118+
*/

0 commit comments

Comments
 (0)