Skip to content

TIMER 2 interrupts not working #158

Open

Description

@anupam-dubey

Hi have used stm8s103 in sduino , i cannot able to write ISR for timer2 please help
INTERRUPT_HANDLER(TIM2_UPD_OVF_IRQHandler, 13) /* TIM4 UPD/OVF */
{

i++;
TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
TIM2_ClearFlag(TIM2_FLAG_UPDATE);

}
void TIM2_Config(void)
{
//set at 100usec on overflow aims

/* TIM2 configuration:

  • TIM2CLK is set to 16 MHz, the TIM2 Prescaler is equal to 16 so the TIM2 counter
    clock used is 16 MHz / 16 = 1000 000 Hz
  • With 1000 000 Hz we can generate time base:
    max time base is 65536us if TIM2_PERIOD = 65535 --> (65535 + 1) * 1000000 = 65536 us
    min time base is 2 us if TIM2_PERIOD = 1 --> ( 1 + 1) * 1000000 = 2 us
  • In this example we need to generate a time base equal to 256 us
    so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */

/* Time base configuration /
TIM2_TimeBaseInit(TIM2_PRESCALER_16, TIM2_PERIOD);
/
Clear TIM4 update flag /
TIM2_ClearFlag(TIM2_FLAG_UPDATE);
/
Enable update interrupt */
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

/* enable interrupts */
// enableInterrupts();

/* Enable TIM4 */
TIM2_Cmd(ENABLE);
}

Activity

  1. dirkx commented on Feb 19, 2025

    @dirkx

    Having the same issue - and notice that the entry for the interrupt function does not seem to be made in the interrupt table:

                                         48 ;--------------------------------------------------------
                                         49 ; interrupt vector
                                         50 ;--------------------------------------------------------
                                         51         .area HOME
          000000                         52 __interrupt_vect:
          000000 82v00u00u00             53         int s_GSINIT ; reset
    ....
          000034 82 00 00 00             66         int 0x000000 ; int11
          000038 82v00u00u00             67         int _TIM1_CAP_COM_IRQHandler ; int12
          00003C 82 00 00 00             68         int 0x000000 ; int13
          000040 82 00 00 00             69         int 0x000000 ; int14
    .....
    
    
    

    For similar trivial code:

    #include <stm8s.h>
    volatile uint16_t now = 0;
    
    INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
    {
      TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
    }
    
    void setup() {
      TIM2_DeInit();
      TIM2_TimeBaseInit(TIM2_PRESCALER_1024, 60000);
      TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
      TIM2_Cmd(ENABLE);
    }
    
    void loop() {
    };
    
    
  2. dirkx commented on Feb 19, 2025

    @dirkx

    Confirmed that this indeed requires

    INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13);      /* TIM2 UPD/OVF/BRK */
    INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14);          /* TIM2 CAP/COM */
    

    to be uncommented in

    // INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13); /* TIM2 UPD/OVF/BRK */
    for this example code to work.

    I guess what we need here is something like a '__weak' compiler trick. Or something fundamentally different when linking.

  3. sajadkkkkkk commented on Nov 13, 2025

    @sajadkkkkkk

    **hello i had same problem

    by changning interrupt vector 13 to 15 , it solved for me .

    #pragma vector=15 // ok 
    __interrupt void TIM2_UPDATE(void)    //4ms interrupt 
    {    // toggle_led1;
    
    

    my microntroller is stm8s105k6

    best regards.**

  4. sajadkkkkkk commented on Nov 13, 2025

    @sajadkkkkkk

    **hello i had same problem

    by changning interrupt vector 13 to 15 , it solved for me .

    #pragma vector=15 // ok 
    __interrupt void TIM2_UPDATE(void)    //4ms interrupt 
    {    // toggle_led1;
    

    my microntroller is stm8s105k6

    best regards.**

    my ide is IAR

  5. halfrafu commented on Mar 28, 2026

    @halfrafu

    Well.. yesterday I've spent like half the day to find this out while trying to get timer 2 interrupts to work... (well I can only blame myself for not checking github issues after that much time).

    So it guess interrupt vectors must be declared in all translation units so in in case of sduino they must be uncommented in the stm8s_it.h file where all those declarations live... but are commented out. Then it's possible to define them in own code in any translation unit that includes stm8s_it.h (with the same name and interrupt vector number) and everything works as it should.

    I don't understand why those declarations are commented out, especially why are they only commented out for SDCC and not for other compilers. I've checked builds with original file and uncommented declarations and leaving them in doesn't change the binary in any way. I've got linker errors from some vectors though that I had to keep commented out:

    ?ASlink-Warning-Undefined Global '_CLK_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_TLI_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_SPI_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_AWU_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_EEPROM_EEC_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_TRAP_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_TIM1_UPD_OVF_TRG_BRK_IRQHandler' referenced by module 'main'
    ?ASlink-Warning-Undefined Global '_ADC1_IRQHandler' referenced by module 'main'
    

    This was tested with build for STM8S003, using sduino in PlatformIO (sduino version 0.5.0, platformio stm8 package version 2.1.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions