clock.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #pragma once
  5. #include <uavcan_stm32/build_config.hpp>
  6. #include <uavcan/driver/system_clock.hpp>
  7. namespace uavcan_stm32
  8. {
  9. namespace clock
  10. {
  11. /**
  12. * Starts the clock.
  13. * Can be called multiple times, only the first call will be effective.
  14. */
  15. void init();
  16. /**
  17. * Returns current monotonic time since the moment when clock::init() was called.
  18. * This function is thread safe.
  19. */
  20. uavcan::MonotonicTime getMonotonic();
  21. /**
  22. * Sets the driver's notion of the system UTC. It should be called
  23. * at startup and any time the system clock is updated from an
  24. * external source that is not the UAVCAN Timesync master.
  25. * This function is thread safe.
  26. */
  27. void setUtc(uavcan::UtcTime time);
  28. /**
  29. * Returns UTC time if it has been set, otherwise returns zero time.
  30. * This function is thread safe.
  31. */
  32. uavcan::UtcTime getUtc();
  33. /**
  34. * Performs UTC phase and frequency adjustment.
  35. * The UTC time will be zero until first adjustment has been performed.
  36. * This function is thread safe.
  37. */
  38. void adjustUtc(uavcan::UtcDuration adjustment);
  39. /**
  40. * UTC clock synchronization parameters
  41. */
  42. struct UtcSyncParams
  43. {
  44. float offset_p; ///< PPM per one usec error
  45. float rate_i; ///< PPM per one PPM error for second
  46. float rate_error_corner_freq;
  47. float max_rate_correction_ppm;
  48. float lock_thres_rate_ppm;
  49. uavcan::UtcDuration lock_thres_offset;
  50. uavcan::UtcDuration min_jump; ///< Min error to jump rather than change rate
  51. UtcSyncParams()
  52. : offset_p(0.01F)
  53. , rate_i(0.02F)
  54. , rate_error_corner_freq(0.01F)
  55. , max_rate_correction_ppm(300.0F)
  56. , lock_thres_rate_ppm(2.0F)
  57. , lock_thres_offset(uavcan::UtcDuration::fromMSec(4))
  58. , min_jump(uavcan::UtcDuration::fromMSec(10))
  59. { }
  60. };
  61. /**
  62. * Clock rate error.
  63. * Positive if the hardware timer is slower than reference time.
  64. * This function is thread safe.
  65. */
  66. float getUtcRateCorrectionPPM();
  67. /**
  68. * Number of non-gradual adjustments performed so far.
  69. * Ideally should be zero.
  70. * This function is thread safe.
  71. */
  72. uavcan::uint32_t getUtcJumpCount();
  73. /**
  74. * Whether UTC is synchronized and locked.
  75. * This function is thread safe.
  76. */
  77. bool isUtcLocked();
  78. /**
  79. * UTC sync params get/set.
  80. * Both functions are thread safe.
  81. */
  82. UtcSyncParams getUtcSyncParams();
  83. void setUtcSyncParams(const UtcSyncParams& params);
  84. }
  85. /**
  86. * Adapter for uavcan::ISystemClock.
  87. */
  88. class SystemClock : public uavcan::ISystemClock, uavcan::Noncopyable
  89. {
  90. SystemClock() { }
  91. virtual void adjustUtc(uavcan::UtcDuration adjustment) { clock::adjustUtc(adjustment); }
  92. public:
  93. virtual uavcan::MonotonicTime getMonotonic() const { return clock::getMonotonic(); }
  94. virtual uavcan::UtcTime getUtc() const { return clock::getUtc(); }
  95. /**
  96. * Calls clock::init() as needed.
  97. * This function is thread safe.
  98. */
  99. static SystemClock& instance();
  100. };
  101. }