Author Topic: [Kinetis KL03] Print timestamp  (Read 3199 times)

Offline Raffaele

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
[Kinetis KL03] Print timestamp
« on: January 16, 2019, 03:41:41 AM »
Hi,
I'm looking for a simple way to print a timestamp via UART, or the time elapsed between two events. The code that I have is normally working. I tried the function time() (I included time.h from the C standard libraries), the building doesn't give errors but the .bin file is not generated.
What I did was simply

#include <time.h>
...
time_t now = time(NULL);


The function time(NULL) is creating the issue.
Any suggestion?

Thank you
« Last Edit: January 16, 2019, 03:45:03 AM by Raffaele »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: [Kinetis KL03] Print timestamp
« Reply #1 on: January 16, 2019, 04:32:58 AM »
Hi

I expect that the library requires a hook into the hardware (which needs to be supplied somehow).

It also depends on what resolution you require for the time stamps. If the TICK resolution is adequate (default 50ms) you can just use the tick counter:

ulTimeSmapl1 = uTaskerSystemTick;
...
ulTimeStamp2 = uTaskerSystemTick;

ulDifference = (ulTimeStamp2 - ulTimeStamp1);
fnDebugMsg("Time measured (Ticks) = ");
fnDebugDec(ulDifference, WITH_CR_LF);



Otherwise use a TPM as follows.

One time initialisation:
static void fnStartTimer(void)
{
    TIMER_INTERRUPT_SETUP timer_setup = { 0 };                           // interrupt configuration parameters
    timer_setup.int_type = TIMER_INTERRUPT;
    timer_setup.int_priority = PRIORITY_TIMERS;
    timer_setup.int_handler = 0;                                         // no interrupt
    timer_setup.timer_reference = 1;                                     // timer 1
    timer_setup.timer_mode = TIMER_PERIODIC;
    timer_setup.timer_value = 0xffff;                                    // maximum period so that it is free running (without pre-scaler)
    fnConfigureInterrupt((void *)&timer_setup);                          // configure timer
}


To measure


usTimeStamp1 = (unsigned short)FTM1_CNT;
...
usTimeStamp2 = (unsigned short)FTM1_CNT;
usDifference = (usTimeStamp1 - usTimeStamp1);
fnDebugMsg("Time measured (clocks) = ");
fnDebugDec(usDifference, WITH_CR_LF);


Although the possible measurement period is shorter due to the fact that it is a 16 bit timer its resolution can be very high - if the TPM is clocked by the 48MHz source it will be in 20.83ns steps (max. about 1.3ms)

Or use

    timer_setup.timer_value = (0xffff * 128);                                    // maximum period so that it is free running (with max. pre-scaler)

for

2.66us steps (max. about 170ms)

Regards

Mark