Skip to content

Commit 458dc0a

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 69f3ee5 commit 458dc0a

File tree

1 file changed

+1
-45
lines changed

1 file changed

+1
-45
lines changed

system/CH32X035/SRC/Startup/startup_ch32x035.S

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -79,51 +79,7 @@ _vector_base:
7979

8080
.option rvc;
8181
.section .text.vector_handler, "ax", @progbits
82-
.weak NMI_Handler /* NMI */
83-
.weak HardFault_Handler /* Hard Fault */
84-
.weak Ecall_M_Mode_Handler /* Ecall M Mode */
85-
.weak Ecall_U_Mode_Handler /* Ecall U Mode */
86-
.weak Break_Point_Handler /* Break Point */
87-
.weak SysTick_Handler /* SysTick */
88-
.weak SW_Handler /* SW */
89-
.weak WWDG_IRQHandler /* Window Watchdog */
90-
.weak PVD_IRQHandler /* PVD through EXTI Line detect */
91-
.weak FLASH_IRQHandler /* Flash */
92-
.weak EXTI7_0_IRQHandler /* EXTI Line 7..0 */
93-
.weak AWU_IRQHandler /* Auto Wake up */
94-
.weak DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */
95-
.weak DMA1_Channel2_IRQHandler /* DMA1 Channel 2 */
96-
.weak DMA1_Channel3_IRQHandler /* DMA1 Channel 3 */
97-
.weak DMA1_Channel4_IRQHandler /* DMA1 Channel 4 */
98-
.weak DMA1_Channel5_IRQHandler /* DMA1 Channel 5 */
99-
.weak DMA1_Channel6_IRQHandler /* DMA1 Channel 6 */
100-
.weak DMA1_Channel7_IRQHandler /* DMA1 Channel 7 */
101-
.weak ADC1_IRQHandler /* ADC1 */
102-
.weak I2C1_EV_IRQHandler /* I2C1 Event */
103-
.weak I2C1_ER_IRQHandler /* I2C1 Error */
104-
.weak USART1_IRQHandler /* USART1 */
105-
.weak SPI1_IRQHandler /* SPI1 */
106-
.weak TIM1_BRK_IRQHandler /* TIM1 Break */
107-
.weak TIM1_UP_IRQHandler /* TIM1 Update */
108-
.weak TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */
109-
.weak TIM1_CC_IRQHandler /* TIM1 Capture Compare */
110-
.weak TIM2_UP_IRQHandler /* TIM2 Update */
111-
.weak USART2_IRQHandler /* USART2 */
112-
.weak EXTI15_8_IRQHandler /* EXTI Line 15..8 */
113-
.weak EXTI25_16_IRQHandler /* EXTI Line 25..16 */
114-
.weak USART3_IRQHandler /* USART3 */
115-
.weak USART4_IRQHandler /* USART4 */
116-
.weak DMA1_Channel8_IRQHandler /* DMA1 Channel8 */
117-
.weak USBFS_IRQHandler /* USBFS Break */
118-
.weak USBFSWakeUp_IRQHandler /* USBFS Wake up from suspend */
119-
.weak PIOC_IRQHandler /* PIOC */
120-
.weak OPA_IRQHandler /* OPA */
121-
.weak USBPD_IRQHandler /* USBPD */
122-
.weak USBPDWakeUp_IRQHandler /* USBPD Wake up */
123-
.weak TIM2_CC_IRQHandler /* TIM2 Capture Compare */
124-
.weak TIM2_TRG_COM_IRQHandler /* TIM2 Trigger and Commutation */
125-
.weak TIM2_BRK_IRQHandler /* TIM2 Break */
126-
.weak TIM3_IRQHandler /* TIM3 */
82+
12783

12884
NMI_Handler:
12985
HardFault_Handler:

0 commit comments

Comments
 (0)