Author Topic: CH32V003 - Hardware Fault Interrupt when using TIM2 periodic interrupt  (Read 3305 times)

0 Members and 9 Guests are viewing this topic.

Offline quevedoTopic starter

  • Newbie
  • Posts: 2
  • Country: br
Hello MCU lovers

I am starting to experiment with RISC-V, so I decided to get some devboards with the CH32V003 MCU. As I am used to work with Eclipse, I decided to try the official MounRiver Studio IDE. I started with some basic polling UART, and then I moved to a periodical interrupt in TIM2 toggling a GPIO output with a LED.

I found an example code at: https://pallavaggarwal.in/2023/09/21/ch32v003-programming-gpio-as-output/

I made a few changes to toggle a LED inside the Timer handler, but when trying to run the code (in debug mode), a "Hardware Fault" exception is thrown when the Handler is called. I even removed all GPIO config, leaving the program like the original one, just adding a variable increment inside the handler (see attached main.c).

I checked the functions to configure the timer and the ISR, as well as the flag cleaning function, they look OK.

I see 2 hypotheses for the problem: either there is a software bug in the functions that I could not find, or there is a hardware bug in my MCUs (I tried running the program in 2 boards, same result).

Any ideas?

 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6885
  • Country: es
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: quevedo

Offline rhodges

  • Frequent Contributor
  • **
  • Posts: 363
  • Country: us
  • Available for embedded projects.
    • My public libraries, code samples, and projects for STM8.
Re: CH32V003 - Hardware Fault Interrupt when using TIM2 periodic interrupt
« Reply #2 on: November 02, 2023, 11:10:43 pm »
I just added this to one of my programs:
Code: [Select]
void TIM2_IRQHandler(void)
{
}
And the resulting assembly language is:
Code: [Select]
00000276 <TIM2_IRQHandler>:
 276:   8082                    ret
You need to tell gcc that this is an interrupt handler. Something like this:
Code: [Select]
void SysTick_Handler(void) __attribute__((interrupt));
void SysTick_Handler(void)
{
    SysTick->SR = 0;

    timer_ms();
    clock_ms++;
    if (clock_ms < 100)
        return;
    clock_ms -= 100;

    timer_10();
}
Currently developing embedded RISC-V. Recently STM32 and STM8. All are excellent choices. Past includes 6809, Z80, 8086, PIC, MIPS, PNX1302, and some 8748 and 6805. Check out my public code on github. https://github.com/unfrozen
 
The following users thanked this post: quevedo

Offline quevedoTopic starter

  • Newbie
  • Posts: 2
  • Country: br
Re: CH32V003 - Hardware Fault Interrupt when using TIM2 periodic interrupt
« Reply #3 on: November 03, 2023, 01:39:55 pm »
Prototyping the handler with the specification "__attribute__((interrupt))" just did the trick! Thank you!
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1861
  • Country: gb
Re: CH32V003 - Hardware Fault Interrupt when using TIM2 periodic interrupt
« Reply #4 on: November 03, 2023, 02:15:47 pm »
Note if you're using the WCH MounRiver IDE and compiler, you should really be using __attribute__((interrupt("WCH-Interrupt-fast"))) instead.

Otherwise, if you use regular __attribute__((interrupt), while it will work, if HPE is not turned off (default is on with WCH startup code) then you'll get the CPU registers saved/restored twice in your ISR (once by HPE, once by code generated by the compiler), which is a waste of time.
 

Offline rhodges

  • Frequent Contributor
  • **
  • Posts: 363
  • Country: us
  • Available for embedded projects.
    • My public libraries, code samples, and projects for STM8.
Re: CH32V003 - Hardware Fault Interrupt when using TIM2 periodic interrupt
« Reply #5 on: November 03, 2023, 03:02:06 pm »
Yes, I agree. In my case, I decided to use "any" gcc version and modified startup_ch32v00x.S to not enable HPE. This might be from code you posted.
Code: [Select]
/* Only enable nesting, not HPE */
    li t0, 0x2
    csrw 0x804, t0
 

I might enable HPE some day and use "naked" interrupt handlers, and end them with inline assembly "mret". This was discussed on another thread, and I think Bruce said it was dangerous. I would visually inspect the assembly output to verify them. But for now, I am staying with standard interrupt handlers.
Currently developing embedded RISC-V. Recently STM32 and STM8. All are excellent choices. Past includes 6809, Z80, 8086, PIC, MIPS, PNX1302, and some 8748 and 6805. Check out my public code on github. https://github.com/unfrozen
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf