USB device (STM32) and get_time() - Page 1
Forum section: electronics / general

author: joseph1235
author_rank: Newbie
timestamp: February 19, 2025, 10:57:30 am
content: Dear all,I working on datalogger (STM32 with external flash / FATfs for mass storage / USB connector).It's work great and i would like to add a time stamp on my data.-> RTC ? check i will use the internal STM32 rtc.My aim, is to developp a device without any specific software on PC side.So now i would like to find a simply way to get time form the PC to my USB device.My question, is there a specific USB configuration ( HID , audio class, ..) who allow the device ask time to the pc without any humans actions ?regards

author: voltsandjolts
author_rank: Unknown
timestamp: February 19, 2025, 11:44:52 am
content: AFAIK datetime is not available to a usb device by any standard usb protocol.Maybe the user could set time by making a text file with specific name "logger-settime.txt" which contains the approximate time, and drag-drops that to your logger flash drive. The mcu detects/intercepts that and sets it's rtc from it. (All assuming you are using mass storage device class for fatfs-flash access).

author: peter-h
author_rank: Super Contributor
timestamp: February 19, 2025, 12:04:45 pm
content: Then you will wear out the FLASH chip which forms the FatFS volume...Unless it is a RAM disk. You could do that partially e.g. have the first few sectors (512 byte sectors if you want windows USB MSC compatibility) implemented in RAM. And make sure that file doesn't move around. Under FAT, if you rewrite a file, it will stay in the same place, AFAIK.I've done this in my latest project. but I did it by the PC visiting a URL in a (very minimalist) HTTP server and in that GET request embedding the time. You could probably do the same with USB i.e. embed date/time in some USB query packet which is mostly unused.Time in embedded is a challenge because 32768Hz xtals all have poor accuracy, even with RTC calibration. Temperature compensation and 2-point calibration gets pretty good but is a lot of faff for production. The RTC is off by some minutes pretty quickly. This may or may not matter...Other ways are GPS (I did that, setting the RTC from GPS when secs=00), NTP (needs internet access), timecode (needs to be multistandard; rarely done in embedded systems) or GSM tower signals (if anybody knows whether you can get time without a paid-up SIM card, or even without a SIM card, they aren't talkingbut I reckon you probably can).

author: abeyer
author_rank: Frequent Contributor
timestamp: February 19, 2025, 07:43:10 pm
content: Why not a usb serial device and expose some management commands there?Then someone without any special software can use a terminal interface, but you could also provide a nicer webserial and/or native software interfacing to it if/when users want to use that. Seems way less janky to me, and potentially valuable for debugging/testing commands in addition to setting the time.

author: kloetpatra
author_rank: Regular Contributor
timestamp: February 19, 2025, 07:57:00 pm
content: Only way I know would be implementing a RNDIS driver, then you can use NTP (internet / ntp server required) as suggested. Another USB-less solution could be DCF77.

author: elektryk
author_rank: Regular Contributor
timestamp: February 19, 2025, 08:11:41 pm
content: Quote from: peter-h on February 19, 2025, 12:04:45 pmDS3231 should do the job. There are also cheaper alternatives such as SD3031 both with integrated XTAL and a few ppm accuracy.Quote from: abeyer on February 19, 2025, 07:43:10 pmI do:Code:date "+t %H %M %S" > /dev/ttyUSB1on Linux and there's apropriate sscanf() call in stm32 code.

author: peter-h
author_rank: Super Contributor
timestamp: February 20, 2025, 10:27:06 am
content: QuoteThat's a lot of seconds, pretty fast.But it depends on the target market.QuoteBuying from Maxim is a fool's game. Avoid any single sourced product from that company at all costs. It is also a very expensive chip.The OP has not been back with any detail anyway. There are issues with batteries and battery life.

author: voltsandjolts
author_rank: Unknown
timestamp: February 20, 2025, 12:15:54 pm
content: Quote from: peter-h on February 20, 2025, 10:27:06 amCalm down with the hyperbole.DS3231 isabout $3 at 1 off and $2 at >100 off, with an accuracy of about 1 minute a year (or better) at 0..40C.Saves a lot of hassle messing about with bare crystals and firmware calibration routines.

author: peter-h
author_rank: Super Contributor
timestamp: February 20, 2025, 01:51:15 pm
content: OK; the pricing I saw at mouser.com was way higher. There must be a cheaper variant.Re Maxim, no, I would not touch them with a 20ft bargepole now.I agree re factory cal being a PITA, unless you are doing a single point cal in which case it is easy.

author: xvr
author_rank: Frequent Contributor
timestamp: February 20, 2025, 02:25:30 pm
content: QuoteA quick rant - it's not Maxim, it's Analog Devices. Or, if you want to track down its origins, Dallas Semiconductor.

author: peter-h
author_rank: Super Contributor
timestamp: February 20, 2025, 02:43:36 pm
content: I am indeed familiar with Dallas, having used the DS1302 c. 1991. At ~200nA, it was then, and is still today (with one recent exceptionhttps://www.eevblog.com/forum/chat/cheapest-way-to-get-datetime-from-gps/msg5358611/#msg5358611)https://ambiq.com/artasie-am1815/the lowest Icc RTC available.

author: NorthGuy
author_rank: Super Contributor
timestamp: February 20, 2025, 03:43:08 pm
content: When the system modifies or deletes a file, the time recorded for the directory changes. You can use that, but the time preservation is a bigger problem. Time maintained with a crystal/oscillator will eventually move away from the real time.If you want accurate time, use GPS.

author: peter-h
author_rank: Super Contributor
timestamp: February 20, 2025, 06:28:44 pm
content: Indeedhttps://www.eevblog.com/forum/chat/cheapest-way-to-get-datetime-from-gps/but it isn't dirt cheap, and in some applications you won't get a signal (not that you need much of a signal to extract time).