zmatt icon

pru ecap timestamp seconds.c

zmatt | PRO | 01/09/22 07:03:20 AM UTC (Edited) | 0 ⭐ | 656 👁️ | Never ⏰ | []
C |

690 B

|

None

|

0 👍

/

0 👎

// This assumes PRUSS is clocked at 200 MHz, which is its typical clock frequency.
//
// A more generic version of this code for any frequency and period: https://pastebin.com/rBR4v35a
 
// Initialization from python (using py-uio):
//    pruss.ecap.pwm.initialize( 200000000 )  # 1 second period
 
struct Timestamp {
    uint32_t s;
    uint32_t ns;
};
 
// Returns timestamp in seconds and nanoseconds (with 5ns precision).
// Must be called at least once every second to ensure correct timekeeping.
struct Timestamp timestamp()
{
    static struct Timestamp now = { 0, 0 };
    uint32_t ns = CT_ECAP.TSCTR * 5;
    if( ns < now.ns )  // ecap counter has wrapped
        ++now.s;
    now.ns = ns;
    return now;
}

Comments