hard_system_timer.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "board.h"
  2. #include "hpm_gptmr_drv.h"
  3. // #include "soft_time.h"
  4. #include "hard_system_timer.h"
  5. #include "test.h"
  6. // 后续要设置定时器分频 一个定时器可以配置4个通道,每个通道的计数重装载值可以不同 由于定时器太多了 就不用同一个了
  7. // 共享同一个硬件模块、时钟源、中断向量 每个通道的配置、计数器、比较值、工作模式都是独立的
  8. #define SYSTEM_TIMER_1 HPM_GPTMR1
  9. #define SYSTEM_TIMER_1_CLK_NAME clock_gptmr1
  10. #define SYSTEM_TIMER_1_CH 0
  11. #define SYSTEM_TIMER_1_IRQ IRQn_GPTMR1
  12. #define SYSTEM_TIMER_1_TICK_US (1)
  13. #define SYSTEM_TIMER_1_RELOAD 2500
  14. void system_timer_init(void)
  15. {
  16. uint32_t gptmr_freq;
  17. gptmr_channel_config_t config;
  18. clock_set_source_divider(SYSTEM_TIMER_1_CLK_NAME, clk_src_osc24m, 24); // 1Mhz
  19. clock_add_to_group(SYSTEM_TIMER_1_CLK_NAME, 0); // 添加到组
  20. gptmr_freq = clock_get_frequency(SYSTEM_TIMER_1_CLK_NAME);
  21. printf("timer1 fre: %d\r\n", gptmr_freq);
  22. gptmr_channel_get_default_config(SYSTEM_TIMER_1, &config);
  23. config.reload = SYSTEM_TIMER_1_RELOAD; // 32bit timer1 2.5ms
  24. gptmr_channel_config(SYSTEM_TIMER_1, SYSTEM_TIMER_1_CH, &config, false);
  25. gptmr_start_counter(SYSTEM_TIMER_1, SYSTEM_TIMER_1_CH);
  26. gptmr_enable_irq(SYSTEM_TIMER_1, GPTMR_CH_RLD_IRQ_MASK(SYSTEM_TIMER_1_CH));
  27. intc_m_enable_irq_with_priority(SYSTEM_TIMER_1_IRQ, 1);
  28. }
  29. extern void timer_hookfunction(void);
  30. static uint32_t s_count = 0;
  31. SDK_DECLARE_EXT_ISR_M(SYSTEM_TIMER_1_IRQ, timer1_250us_isr)
  32. void timer1_250us_isr(void)
  33. {
  34. if (gptmr_check_status(SYSTEM_TIMER_1, GPTMR_CH_RLD_STAT_MASK(SYSTEM_TIMER_1_CH))) {
  35. gptmr_clear_status(SYSTEM_TIMER_1, GPTMR_CH_RLD_STAT_MASK(SYSTEM_TIMER_1_CH));
  36. // timer_hookfunction();
  37. s_count++;
  38. }
  39. }
  40. /*timer1 test 2026/3/14 测试通过*/
  41. #ifdef TIMER1_TEST
  42. #include "bsp_V8M_YY_led.h"
  43. void timer1_test(void)
  44. {
  45. system_timer_init();
  46. v8m_yy_led_init();
  47. // 注:如果while(1)轮询太快可能导致401中断触发变灯 然后下一次中断没触发呢 又扫过来了再次进入应用 也就是1个周期触发两次
  48. // 实际while(1)的运行一次的时间要大于中断1次的时间最好 也就是2.5ms
  49. while(1)
  50. {
  51. if(s_count % 400 == 1)
  52. {
  53. board_delay_ms(3);
  54. v8m_yy_led_toggle(V8M_YY_LED_R);
  55. printf("s_count %d\r\n", s_count);
  56. printf("1000ms \r\n");
  57. }
  58. }
  59. }
  60. /*
  61. 异常
  62. s_count 7201
  63. 1000ms
  64. s_count 7201
  65. 1000ms
  66. 正常
  67. s_count 17602
  68. 1000ms
  69. */
  70. #endif