hard_system_time.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "board.h"
  2. #include "hpm_gptmr_drv.h"
  3. #include "hard_system_time.h"
  4. #include "test.h"
  5. // 需要修改分频配置
  6. #define SYSTEM_TIMER_0 HPM_GPTMR0
  7. #define SYSTEM_TIMER_0_CLK_NAME clock_gptmr0
  8. #define SYSTEM_TIMER_0_CH 0
  9. #define SYSTEM_TIMER_0_IRQ IRQn_GPTMR0
  10. #define SYSTEM_TIMER_0_TICK_US (1)
  11. #define SYSTEM_TIMER_0_RELOAD 0xFFFFFFFF
  12. void system_time_init(void)
  13. {
  14. uint32_t gptmr_freq;
  15. gptmr_channel_config_t config;
  16. clock_set_source_divider(SYSTEM_TIMER_0_CLK_NAME, clk_src_osc24m, 24); // 1Mhz
  17. clock_add_to_group(SYSTEM_TIMER_0_CLK_NAME, 0); // 添加到组
  18. gptmr_freq = clock_get_frequency(SYSTEM_TIMER_0_CLK_NAME);
  19. printf("timer0 fre: %d\r\n", gptmr_freq);
  20. gptmr_channel_get_default_config(SYSTEM_TIMER_0, &config);
  21. config.reload = SYSTEM_TIMER_0_RELOAD; // 32bit timer0 71.58min
  22. gptmr_channel_config(SYSTEM_TIMER_0, SYSTEM_TIMER_0_CH, &config, false);
  23. gptmr_start_counter(SYSTEM_TIMER_0, SYSTEM_TIMER_0_CH);
  24. gptmr_enable_irq(SYSTEM_TIMER_0, GPTMR_CH_RLD_IRQ_MASK(SYSTEM_TIMER_0_CH));
  25. intc_m_enable_irq_with_priority(SYSTEM_TIMER_0_IRQ, 1);
  26. }
  27. SDK_DECLARE_EXT_ISR_M(SYSTEM_TIMER_0_IRQ, timer0_isr)
  28. void timer0_isr(void)
  29. {
  30. if (gptmr_check_status(SYSTEM_TIMER_0, GPTMR_CH_RLD_STAT_MASK(SYSTEM_TIMER_0_CH))) {
  31. gptmr_clear_status(SYSTEM_TIMER_0, GPTMR_CH_RLD_STAT_MASK(SYSTEM_TIMER_0_CH));
  32. //time_hookfunction();
  33. // printf("timer test\r\n");
  34. }
  35. }
  36. unsigned int hard_micros(void)
  37. {
  38. unsigned int ret;
  39. // 返回当前定时器通道计数值
  40. ret = gptmr_channel_get_counter(SYSTEM_TIMER_0,SYSTEM_TIMER_0_CH, gptmr_counter_type_normal);
  41. return ret;
  42. }
  43. /**
  44. * @brief 微秒延时
  45. */
  46. static void hard_delay_us(unsigned int us)
  47. {
  48. unsigned int start = hard_micros();
  49. while ((hard_micros() - start) < us) {
  50. // 忙等待
  51. }
  52. }
  53. /**
  54. * @brief 毫秒延时
  55. */
  56. static void hard_delay_ms(unsigned int ms)
  57. {
  58. hard_delay_us(ms * 1000);
  59. }
  60. /*timer0 test 2026/3/14 测试通过*/
  61. #ifdef TIMER0_TEST
  62. #include "bsp_V8M_YY_led.h"
  63. void timer0_test(void)
  64. {
  65. system_time_init();
  66. v8m_yy_led_init();
  67. while(1)
  68. {
  69. hard_delay_ms(1000);
  70. v8m_yy_led_toggle(V8M_YY_LED_G);
  71. printf("1000ms \r\n");
  72. }
  73. }
  74. #endif