Advertisement
zmatt

pru ecap timestamp cycles.c

Oct 7th, 2020 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <stdint.h>
  2. #include "pru_ecap.h"
  3.  
  4. // initialization using py-uio:
  5. //      pruss.ecap.pwm.initialize( 2**32 )
  6. //
  7. // initialization using pru:
  8. static void timestamp_initialize()
  9. {
  10.     CT_ECAP.ECCTL2 = 0x0200;
  11.     CT_ECAP.ECCTL1 = 0;
  12.     CT_ECAP.TSCTR = 0;
  13.     CT_ECAP.CAP3 = 0xffffffff;
  14.     CT_ECAP.CAP4 = 0;
  15.     CT_ECAP.CAP1 = 0xffffffff;
  16.     CT_ECAP.CAP2 = 0;
  17.     CT_ECAP.ECCTL2 = 0x0210;
  18. }
  19.  
  20. // returns timestamp in units of 1 pru cycle (= 5ns).
  21. // timestamp wraps every 21.47483648 seconds (exactly).
  22. static inline uint32_t timestamp()
  23. {
  24.     return CT_ECAP.TSCTR;
  25. }
  26.  
  27. // returns timestamp in units of 65536 pru cycles (= 0.32768 ms).
  28. // must be called at least once every 21 seconds to ensure correct timekeeping.
  29. // timestamp wraps every 16 days (approximately).
  30. uint32_t timestamp_shr16()
  31. {
  32.     static uint32_t now = 0;
  33.     return now += (uint16_t)( ( timestamp() >> 16 ) - now );
  34. }
  35.  
  36. // returns 64-bit timestamp in units of pru cycles (= 5 ns).
  37. // must be called at least once every 21 seconds to ensure correct timekeeping.
  38. // timestamp never wraps.
  39. uint64_t timestamp64()
  40. {
  41.     static uint64_t now = 0;
  42.     return now += (uint32_t)( timestamp() - now );
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement