Skip to content

Commit 69f3ee5

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 3dab209 commit 69f3ee5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

system/CH32X035/SRC/Peripheral/src/ch32x035_misc.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,54 @@ void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
7979
NVIC_DisableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
8080
}
8181
}
82+
83+
/* MMOLE 250805: moved from startup_ch32x035.S to ch32x035_misc.c (see issue #204) */
84+
__attribute__ ((weak)) void while1_handler(void) {while(1);};
85+
__attribute__ ((weak)) void Ecall_M_Mode_Handler (void) __attribute__ ((weak, alias ("while1_handler")));
86+
__attribute__ ((weak)) void Ecall_U_Mode_Handler (void) __attribute__ ((weak, alias ("while1_handler")));
87+
__attribute__ ((weak)) void Break_Point_Handler (void) __attribute__ ((weak, alias ("while1_handler")));
88+
__attribute__ ((weak)) void SW_Handler (void) __attribute__ ((weak, alias ("while1_handler")));
89+
__attribute__ ((weak)) void WWDG_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
90+
__attribute__ ((weak)) void PVD_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
91+
__attribute__ ((weak)) void FLASH_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_UP_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
111+
__attribute__ ((weak)) void USART2_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
112+
__attribute__ ((weak)) void EXTI15_8_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
113+
__attribute__ ((weak)) void EXTI25_16_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
114+
__attribute__ ((weak)) void USART3_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
115+
__attribute__ ((weak)) void USART4_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
116+
__attribute__ ((weak)) void DMA1_Channel8_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
117+
__attribute__ ((weak)) void USBFS_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
118+
__attribute__ ((weak)) void USBFSWakeUp_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
119+
__attribute__ ((weak)) void PIOC_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
120+
__attribute__ ((weak)) void OPA_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
121+
__attribute__ ((weak)) void USBPD_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
122+
__attribute__ ((weak)) void USBPDWakeUp_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
123+
__attribute__ ((weak)) void TIM2_CC_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
124+
__attribute__ ((weak)) void TIM2_TRG_COM_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
125+
__attribute__ ((weak)) void TIM2_BRK_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
126+
__attribute__ ((weak)) void TIM3_IRQHandler (void) __attribute__ ((weak, alias ("while1_handler")));
127+
128+
/*
129+
NMI_Handler: 1: j 1b
130+
HardFault_Handler: 1: j 1b
131+
SysTick_Handler: 1: j 1b
132+
*/

0 commit comments

Comments
 (0)