Skip to content

Commit

Permalink
application: Fix irqc_mtip_handler maybe not working as expected due …
Browse files Browse the repository at this point in the history
…to 24bit

24b timer is easy to be overflow

Signed-off-by: Huaqi Fang <[email protected]>
  • Loading branch information
fanghuaqi committed Sep 13, 2024
1 parent 604ebcf commit b45d25e
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions application/baremetal/demo_timer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ volatile unsigned int msip_trig_flag = 1; /* sw trigger mtimer sw interrupt flag

#define LOOP_COUNT 5

/* TODO uncomment below to enable timer interrupt via reset timer load register
* and not update timecmp register */
//#define TIMER_RELOAD

#define RELOAD_TICKS SOC_TIMER_FREQ / 10

__INTERRUPT void irqc_mtip_handler(void)
{
int0_cnt++;
printf("MTimer IRQ handler %d\n\r", int0_cnt);
uint32_t now = SysTimer_GetLoadValue();
SysTimer_SetCompareValue(now + SOC_TIMER_FREQ / 10);

#ifdef TIMER_RELOAD
SysTimer_SetLoadValue(0);
#else
SysTick_Reload(RELOAD_TICKS);
#endif
}

__INTERRUPT void irqc_msip_handler(void)
Expand All @@ -30,10 +40,15 @@ __INTERRUPT void irqc_msip_handler(void)
void setup_timer()
{
printf("init timer and start\n\r");
uint32_t now = SysTimer_GetLoadValue();
uint32_t then = now + SOC_TIMER_FREQ / 10;
SysTimer_SetCompareValue(then);

#ifdef TIMER_RELOAD
SysTimer_SetLoadValue(0);
SysTimer_SetCompareValue(RELOAD_TICKS);
#else
SysTick_Reload(RELOAD_TICKS);
#endif
}

#ifdef CFG_SIMULATION
#define RUN_LOOPS 2
#else
Expand Down Expand Up @@ -63,6 +78,7 @@ int main(void)
if (msip_trig_flag == 1) {
msip_trig_flag = 0;
SysTimer_SetSWIRQ(); /* trigger timer sw interrupt */
// WARN take care when you modify time load register, below function may fail
delay_1ms(10);
}
} while (int1_cnt < RUN_LOOPS); /* check test end condition */
Expand Down

0 comments on commit b45d25e

Please sign in to comment.