main.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // See LICENSE for license details.
  2. #include <stdio.h>
  3. #include "nuclei_sdk_soc.h"
  4. /* Define the interrupt handler name same as vector table in case download mode is flashxip. */
  5. #define systimer_irq_handler core_mtip_handler
  6. #define systimer_sw_irq_handler core_msip_handler
  7. static uint32_t int0_cnt = 0; /* msip timer interrupt test counter */
  8. static uint32_t int1_cnt = 0; /* mtip timer interrupt test counter */
  9. unsigned int msip_trig_flag = 1; /* sw trigger systimer sw interrupt flag */
  10. void systimer_irq_handler(void)
  11. {
  12. int0_cnt++;
  13. printf("SysTimer IRQ handler %d\n\r", int0_cnt);
  14. uint64_t now = SysTimer_GetLoadValue();
  15. SysTimer_SetCompareValue(now + SOC_TIMER_FREQ / 2);
  16. }
  17. void systimer_sw_irq_handler(void)
  18. {
  19. SysTimer_ClearSWIRQ();
  20. int1_cnt++;
  21. printf("SysTimer SW IRQ handler %d\n\r", int1_cnt);
  22. msip_trig_flag = 1;
  23. }
  24. void setup_timer()
  25. {
  26. printf("init timer and start\n\r");
  27. uint64_t now = SysTimer_GetLoadValue();
  28. uint64_t then = now + SOC_TIMER_FREQ / 2;
  29. SysTimer_SetCompareValue(then);
  30. }
  31. #ifdef CFG_SIMULATION
  32. #define LOOP_COUNT 2
  33. #else
  34. #define LOOP_COUNT 5
  35. #endif
  36. // This demo will show cpu working in clint interrupt mode not eclic interrupt mode
  37. // Mainly test timer interrupt and software interrupt
  38. // If you want to see how systimer working in ECLIC interrupt mode,
  39. // please check demo_timer or demo_eclic example
  40. // TODO Currently it only can build for evalsoc
  41. int main(void)
  42. {
  43. uint32_t returnCode;
  44. /* Initialize interrupt as CLINT interrupt mode, see MTVEC register description */
  45. CLINT_Interrupt_Init();
  46. returnCode = Core_Register_IRQ(SysTimer_IRQn, systimer_irq_handler); /* register system timer interrupt */
  47. __enable_irq(); /* enable global interrupt */
  48. setup_timer(); /* initialize timer */
  49. while (int0_cnt < LOOP_COUNT);
  50. __disable_core_irq(SysTimer_IRQn); /* Disable MTIP iterrupt */
  51. returnCode = Core_Register_IRQ(SysTimerSW_IRQn, systimer_sw_irq_handler); /* register system timer SW interrupt */
  52. do {
  53. if (msip_trig_flag == 1) {
  54. msip_trig_flag = 0;
  55. SysTimer_SetSWIRQ(); /* trigger timer sw interrupt */
  56. delay_1ms(200);
  57. }
  58. } while (int1_cnt < LOOP_COUNT); /* check test end condition */
  59. printf("SysTimer MTIP and MSIP CLINT interrupt test finish and pass\r\n");
  60. if (returnCode != 0) { /* Check return code for errors */
  61. return -1;
  62. }
  63. return 0;
  64. }