cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate a timestamp for datalogging ?

johngj
Senior

I need to generate a timestamp for data logging, but I'm not sure how to do it.

The data packets are received via the UART at a 100ms rate and each packet of data consists of 10 bytes of serial data

  • Byte 0: Temperature
  • Byte 1: Voltage high byte
  • Byte 2: Voltage low byte
  • Byte 3: Current high byte
  • Byte 4: Current low byte
  • Byte 5: Consumption high byte
  • Byte 6: Consumption low byte
  • Byte 7: Rpm high byte
  • Byte 8: Rpm low byte
  • Byte 9: 8-bit CRC

These data packets are written to an SD card, but I need to prepend each data packet with a timestamp.

I initially considered using the RTC then converting it to a 32-bit UNIX time, but then realised several issues:

1.  The precision of UNIX time is only 1 second, but the rate of the data packets is currently 100ms (this could be changed to a faster rate such as 10ms).  

2. The other extreme is that the UNIX time contains year, month and day.  This would not be needed for every data packet, its wasteful to store this when each data packet is being logged at a 100ms rate !

 

What is the best way to time stamp each data packet ? The only thing I can think of, is to capture the date and time and write it to the start of the log file.  This would be the reference time for the rest of the log file.

Then setup a timer (e.g with a 1ms resolution) and use that timer to stamp each data packet.  When plotting the data, the time stamp for each data packet would be added to the reference time captured at the start of the log file.

 

Or is there a better solution ?  

 

PS. The data on the SD card will need to be post processed using MATLAB, Python etc

PPS. I am using the NUCLEO-L433RC-P dev board.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

You can also use Systick timer using HAL_GetTick() that provides 1ms rate.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
SofLit
ST Employee

You can also use Systick timer using HAL_GetTick() that provides 1ms rate.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

I implemented Microsoft's SYSTEMTIME for my own purposes.

I don't much care for HAL_GetTick() beyond timeouts, but the SysTick can do additional things. Can use a 32-bit or 64-bit count over whatever epoch you want, UNIX, MICROSOFT, GPS, etc. Sub milli-second you want to do with HW counts, not interrupts.

The RTC's can hold time and date.

A system with GPS/GNSS can have highly accurate time, and doesn't need user input. With a network connection (S)NTP. A lot of routers these days support NTP, and acquisition from pool.ntp.org type resources, but don't abuse.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
johngj
Senior

Thanks all, I used HAL_GetTick() and it seems to work fine.