openwch / ch32v003

CH32V003 is an ultra-cheap RISC-V MCU with 2KB SRAM, 16KB flash, and up to 18 GPIOs that sells for under $0.10
393 stars 58 forks source link

IRQ for cpp is not working #15

Open inafucoAugusto opened 1 year ago

inafucoAugusto commented 1 year ago

We tried to reproduce the IRQ D0 example from the project shown bellow (path: openwch/ch32v003/EVT/EXAM/EXTI/EXTI0).

image

If we use the main file as a .C file the issue is not reproducible. However if we change the file to a .CPP (executing all steps to converting the project from .C to .CPP) we are not able to use the IRQ.

Using the debug tool we reached the following result:

image

Right after pressing the D0 button, we expect the IRQ defined in the .CPP file (this IRQ is the same from exaple EXTI0), but it got stuck at the "EXTI7_0_IRQHandler: 1: j 1b".

maxgerhardt commented 1 year ago

However if we change the file to a .CPP (executing all steps to converting the project from .C to .CPP) we are not able to use the IRQ.

Bruh.

If you use a .cpp file you will get name mangling, so the symbol emitted by a function like

void IRQHandler() {

}

it will emit a mangled symbol (due to C++ supporting overloaded functions that must be disambiguated somehow).

So you need to add an extern "C" declaration to have C linkage (disabling of name manling)

extern "C" void IRQHandler() {

}

or in your above case where you have the function declaration above the implementation, that line just needs to be prefixed with extern "C" (line 86).

See