main.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // See LICENSE for license details.
  2. #include <stdio.h>
  3. #include "nuclei_sdk_soc.h"
  4. /* NOTE: Make sure the interrupt function name for time interrupt and software time interrupt are
  5. * irqc_mtip_handler and irqc_msip_handler */
  6. static volatile uint32_t int0_cnt = 0; /* mtip timer interrupt test counter */
  7. static volatile uint32_t int1_cnt = 0; /* msip timer interrupt test counter */
  8. volatile unsigned int msip_trig_flag = 1; /* sw trigger mtimer sw interrupt flag */
  9. #define LOOP_COUNT 5
  10. /* TODO uncomment below to enable timer interrupt via reset timer load register
  11. * and not update timecmp register */
  12. //#define TIMER_RELOAD
  13. #define RELOAD_TICKS SOC_TIMER_FREQ / 10
  14. __INTERRUPT void irqc_mtip_handler(void)
  15. {
  16. int0_cnt++;
  17. printf("MTimer IRQ handler %d\n\r", int0_cnt);
  18. #ifdef TIMER_RELOAD
  19. SysTimer_SetLoadValue(0);
  20. #else
  21. SysTick_Reload(RELOAD_TICKS);
  22. #endif
  23. }
  24. __INTERRUPT void irqc_msip_handler(void)
  25. {
  26. SysTimer_ClearSWIRQ();
  27. int1_cnt++;
  28. printf("MTimer SW IRQ handler %d\n\r", int1_cnt);
  29. msip_trig_flag = 1;
  30. }
  31. void setup_timer()
  32. {
  33. printf("init timer and start\n\r");
  34. #ifdef TIMER_RELOAD
  35. SysTimer_SetLoadValue(0);
  36. SysTimer_SetCompareValue(RELOAD_TICKS);
  37. #else
  38. SysTick_Reload(RELOAD_TICKS);
  39. #endif
  40. }
  41. #ifdef CFG_SIMULATION
  42. #define RUN_LOOPS 2
  43. #else
  44. #define RUN_LOOPS 5
  45. #endif
  46. int main(void)
  47. {
  48. uint32_t returnCode;
  49. setup_timer(); /* initialize timer */
  50. // TODO you can register new handler only when your vector table is RW
  51. // otherwise please make sure pass NULL, and use the correct interrupt handler name defined
  52. // in system_<Device>.c
  53. returnCode = IRQC_Register_IRQ(SysTimer_IRQn, NULL); /* register system timer interrupt */
  54. __enable_irq(); /* enable global interrupt */
  55. while (int0_cnt < RUN_LOOPS);
  56. IRQC_DisableIRQ(SysTimer_IRQn); /* Disable MTIP iterrupt */
  57. // TODO you can register new handler only when your vector table is RW
  58. returnCode = IRQC_Register_IRQ(SysTimerSW_IRQn, NULL); /* register system timer SW interrupt */
  59. do {
  60. if (msip_trig_flag == 1) {
  61. msip_trig_flag = 0;
  62. SysTimer_SetSWIRQ(); /* trigger timer sw interrupt */
  63. // WARN take care when you modify time load register, below function may fail
  64. delay_1ms(10);
  65. }
  66. } while (int1_cnt < RUN_LOOPS); /* check test end condition */
  67. printf("MTimer msip and mtip interrupt test finish and pass\r\n");
  68. if (returnCode != 0) { /* Check return code for errors */
  69. return -1;
  70. }
  71. return 0;
  72. }