hrt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) Siddharth Bharat Purohit 2017
  3. * This file is free software: you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the
  5. * Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // High Resolution Timer
  17. #include "ch.h"
  18. #include "hal.h"
  19. #include "hrt.h"
  20. #include <stdint.h>
  21. static uint64_t timer_base_us64;
  22. #if CH_CFG_ST_RESOLUTION == 16
  23. static uint32_t timer_base_us32;
  24. #endif
  25. static uint32_t timer_base_ms;
  26. static volatile systime_t last_systime;
  27. #if CH_CFG_ST_RESOLUTION == 16
  28. static uint32_t get_systime_us32(void)
  29. {
  30. systime_t now = chVTGetSystemTimeX();
  31. #if CH_CFG_ST_FREQUENCY != 1000000
  32. now *= (1000000UL / CH_CFG_ST_FREQUENCY);
  33. #endif
  34. if (now < last_systime) {
  35. uint32_t last_u32 = timer_base_us32;
  36. timer_base_us32 += (uint32_t)TIME_MAX_SYSTIME;
  37. if (timer_base_us32 < last_u32) {
  38. timer_base_us64 += ((uint32_t)-1);
  39. timer_base_ms += ((uint32_t)-1)/1000;
  40. }
  41. }
  42. last_systime = now;
  43. return timer_base_us32 + (uint32_t)now;
  44. }
  45. #elif CH_CFG_ST_RESOLUTION == 32
  46. static uint32_t get_systime_us32(void)
  47. {
  48. systime_t now = chVTGetSystemTimeX();
  49. #if CH_CFG_ST_FREQUENCY != 1000000
  50. now *= (1000000UL / CH_CFG_ST_FREQUENCY);
  51. #endif
  52. if (now < last_systime) {
  53. timer_base_us64 += TIME_MAX_SYSTIME;
  54. timer_base_ms += TIME_MAX_SYSTIME/1000;
  55. }
  56. last_systime = now;
  57. return now;
  58. }
  59. #else
  60. #error "unsupported timer resolution"
  61. #endif
  62. /*
  63. we use chSysGetStatusAndLockX() to prevent an interrupt while
  64. allowing this call from any context
  65. */
  66. uint64_t hrt_micros64()
  67. {
  68. syssts_t sts = chSysGetStatusAndLockX();
  69. uint32_t now = get_systime_us32();
  70. uint64_t ret = timer_base_us64 + now;
  71. chSysRestoreStatusX(sts);
  72. return ret;
  73. }
  74. uint32_t hrt_micros32()
  75. {
  76. syssts_t sts = chSysGetStatusAndLockX();
  77. uint32_t ret = get_systime_us32();
  78. chSysRestoreStatusX(sts);
  79. return ret;
  80. }
  81. uint32_t hrt_millis32()
  82. {
  83. syssts_t sts = chSysGetStatusAndLockX();
  84. uint32_t now = get_systime_us32();
  85. uint32_t ret = (now / 1000U) + timer_base_ms;
  86. chSysRestoreStatusX(sts);
  87. return ret;
  88. }