clock.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #pragma once
  5. #include <uavcan/driver/system_clock.hpp>
  6. namespace uavcan_lpc11c24
  7. {
  8. namespace clock
  9. {
  10. /**
  11. * Starts the clock.
  12. * Can be called multiple times, only the first call will be effective.
  13. */
  14. void init();
  15. /**
  16. * Returns current monotonic time passed since the moment when clock::init() was called.
  17. * Note that both monotonic and UTC clocks are implemented using SysTick timer.
  18. */
  19. uavcan::MonotonicTime getMonotonic();
  20. /**
  21. * Returns UTC time if it has been set, otherwise returns zero time.
  22. * Note that both monotonic and UTC clocks are implemented using SysTick timer.
  23. */
  24. uavcan::UtcTime getUtc();
  25. /**
  26. * Performs UTC time adjustment.
  27. * The UTC time will be zero until first adjustment has been performed.
  28. */
  29. void adjustUtc(uavcan::UtcDuration adjustment);
  30. /**
  31. * Returns clock error sampled at previous UTC adjustment.
  32. * Positive if the hardware timer is slower than reference time.
  33. */
  34. uavcan::UtcDuration getPrevUtcAdjustment();
  35. }
  36. /**
  37. * Adapter for uavcan::ISystemClock.
  38. */
  39. class SystemClock : public uavcan::ISystemClock, uavcan::Noncopyable
  40. {
  41. static SystemClock self;
  42. SystemClock() { }
  43. uavcan::MonotonicTime getMonotonic() const override { return clock::getMonotonic(); }
  44. uavcan::UtcTime getUtc() const override { return clock::getUtc(); }
  45. void adjustUtc(uavcan::UtcDuration adjustment) override { clock::adjustUtc(adjustment); }
  46. public:
  47. /**
  48. * Calls clock::init() as needed.
  49. */
  50. static SystemClock& instance();
  51. };
  52. }