clock_time_systick.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-04 RT-Thread SysTick adapter for clock_time (Cortex-M example)
  9. */
  10. /**
  11. * @file clock_time_systick.c
  12. * @brief ARM Cortex-M SysTick adapter for the unified clock_time subsystem
  13. *
  14. * This file demonstrates how to adapt SysTick timer to the clock_time framework.
  15. * SysTick is commonly used on ARM Cortex-M processors.
  16. *
  17. * Features:
  18. * - Uses DWT CYCCNT for high-resolution counter (clocksource)
  19. * - Falls back to tick counter if DWT is not available
  20. * - SysTick itself provides system tick, not high-res timeout
  21. *
  22. * Note: This example provides only clocksource capability. For clockevent
  23. * capability, you would need to use a different hardware timer.
  24. *
  25. * Usage:
  26. * 1. Enable DWT cycle counter in your board initialization
  27. * 2. This adapter will automatically register during initialization
  28. */
  29. #include <rtthread.h>
  30. #include <rtdevice.h>
  31. #if defined(RT_USING_CLOCK_TIME) && defined(ARCH_ARM_CORTEX_M)
  32. /* DWT (Data Watchpoint and Trace) registers */
  33. #define DWT_BASE 0xE0001000UL
  34. #define DWT_CYCCNT (*(volatile rt_uint32_t *)(DWT_BASE + 0x004))
  35. #define DWT_CTRL (*(volatile rt_uint32_t *)(DWT_BASE + 0x000))
  36. #define DWT_CTRL_CYCCNTENA (1 << 0)
  37. /* DEM (Debug Exception and Monitor) registers */
  38. #define DEM_BASE 0xE000EDFC
  39. #define DEM_CR (*(volatile rt_uint32_t *)(DEM_BASE + 0x000))
  40. #define DEM_CR_TRCENA (1 << 24)
  41. static rt_bool_t _dwt_available = RT_FALSE;
  42. static rt_uint32_t _cpu_freq_hz = 0;
  43. /**
  44. * @brief Enable DWT cycle counter
  45. *
  46. * @return RT_TRUE if DWT is available and enabled, RT_FALSE otherwise
  47. */
  48. static rt_bool_t _dwt_enable(void)
  49. {
  50. /* Enable DWT */
  51. DEM_CR |= DEM_CR_TRCENA;
  52. /* Reset counter */
  53. DWT_CYCCNT = 0;
  54. /* Enable counter */
  55. DWT_CTRL |= DWT_CTRL_CYCCNTENA;
  56. /* Verify counter is counting */
  57. rt_uint32_t start = DWT_CYCCNT;
  58. for (volatile int i = 0; i < 100; i++);
  59. rt_uint32_t end = DWT_CYCCNT;
  60. return (end > start);
  61. }
  62. static rt_uint64_t _systick_get_freq(void)
  63. {
  64. if (_dwt_available)
  65. {
  66. /* Return CPU frequency (DWT CYCCNT increments at CPU clock rate) */
  67. return _cpu_freq_hz ? _cpu_freq_hz : SystemCoreClock;
  68. }
  69. else
  70. {
  71. /* Fall back to tick frequency */
  72. return RT_TICK_PER_SECOND;
  73. }
  74. }
  75. static rt_uint64_t _systick_get_counter(void)
  76. {
  77. if (_dwt_available)
  78. {
  79. /* Return DWT cycle counter */
  80. return DWT_CYCCNT;
  81. }
  82. else
  83. {
  84. /* Fall back to tick counter */
  85. return rt_tick_get();
  86. }
  87. }
  88. static const struct rt_clock_time_ops _systick_ops =
  89. {
  90. .get_freq = _systick_get_freq,
  91. .get_counter = _systick_get_counter,
  92. .set_timeout = RT_NULL, /* SysTick doesn't support programmable timeout */
  93. };
  94. static struct rt_clock_time_device _systick_device;
  95. /**
  96. * @brief Initialize SysTick/DWT as clock_time device
  97. *
  98. * @return RT_EOK on success, error code otherwise
  99. */
  100. int rt_clock_time_systick_init(void)
  101. {
  102. /* Try to enable DWT cycle counter */
  103. _dwt_available = _dwt_enable();
  104. _systick_device.ops = &_systick_ops;
  105. /* Register with clocksource capability only */
  106. rt_err_t result = rt_clock_time_device_register(&_systick_device,
  107. "systick",
  108. RT_CLOCK_TIME_CAP_CLOCKSOURCE);
  109. if (result == RT_EOK)
  110. {
  111. if (_dwt_available)
  112. {
  113. rt_kprintf("SysTick/DWT: freq=%d Hz (DWT cycle counter enabled)\n",
  114. (rt_uint32_t)_systick_get_freq());
  115. }
  116. else
  117. {
  118. rt_kprintf("SysTick: freq=%d Hz (DWT not available, using tick counter)\n",
  119. RT_TICK_PER_SECOND);
  120. }
  121. }
  122. return result;
  123. }
  124. INIT_DEVICE_EXPORT(rt_clock_time_systick_init);
  125. /**
  126. * @brief Set CPU frequency for DWT counter
  127. *
  128. * This should be called if SystemCoreClock is not accurate or not available.
  129. *
  130. * @param freq_hz CPU frequency in Hz
  131. */
  132. void rt_clock_time_systick_set_freq(rt_uint32_t freq_hz)
  133. {
  134. _cpu_freq_hz = freq_hz;
  135. }
  136. #endif /* RT_USING_CLOCK_TIME && ARCH_ARM_CORTEX_M */