zmatt icon

pru ecap timestamp cycles.c

zmatt | PRO | 08/17/22 12:27:20 PM UTC (Edited) | 0 ⭐ | 520 👁️ | Never ⏰ | []
C++ |

1.15 KB

|

None

|

0 👍

/

0 👎

#include <stdint.h>
#include "pru_ecap.h"
 
// initialization using py-uio:
//      pruss.ecap.pwm.initialize( 2**32 )
//
// initialization using pru:
static void timestamp_initialize()
{
    CT_ECAP.ECCTL2 = 0x0200;
    CT_ECAP.ECCTL1 = 0;
    CT_ECAP.TSCTR = 0;
    CT_ECAP.CAP3 = 0xffffffff;
    CT_ECAP.CAP4 = 0;
    CT_ECAP.CAP1 = 0xffffffff;
    CT_ECAP.CAP2 = 0;
    CT_ECAP.ECCTL2 = 0x0210;
}
 
// returns timestamp in units of 1 pru cycle (= 5ns).
// timestamp wraps every 21.47483648 seconds (exactly).
static inline uint32_t timestamp()
{
    return CT_ECAP.TSCTR;
}
 
// returns timestamp in units of 65536 pru cycles (= 0.32768 ms).
// must be called at least once every 21 seconds to ensure correct timekeeping.
// timestamp wraps every 16 days (approximately).
uint32_t timestamp_shr16()
{
    static uint32_t now = 0;
    return now += (uint16_t)( ( timestamp() >> 16 ) - now );
}
 
// returns 64-bit timestamp in units of pru cycles (= 5 ns).
// must be called at least once every 21 seconds to ensure correct timekeeping.
// timestamp never wraps.
uint64_t timestamp64()
{
    static uint64_t now = 0;
    return now += (uint32_t)( timestamp() - now );
}

Comments