简体   繁体   English

嵌入式系统时钟

[英]System clock on embedded

I'm developing a project, where I need to support a calender + clock with ms granularity. 我正在开发一个项目,我需要在其中支持ms粒度的日历+时钟。 I managed to configure the RTC of the chip to aid me with time keeping, but I'm having difficulty finding the correct data type to present the time. 我设法配置了芯片的RTC以帮助我保持时间,但是我很难找到正确的数据类型来显示时间。

I always worked with time_t (The Unix epoch), however it is seconds based, so it's not going to work. 我一直使用time_t (Unix时代),但是它是基于秒的,因此它不起作用。 What about struct timeval ? struct timeval呢? it appears that this structure is not supported as the struct tm in the context of presentation (asctime). 似乎在表示上下文(asctime)中不支持将此结构作为struct tm

What is the preferred way to represent the time in an embedded system ? 在嵌入式系统中表示时间的首选方式是什么?

note: This system interfaces a java based back-end server and needs to synchronize the time with it, so I'm looking for a structure that could be used out of the box in several languages. 注意:该系统连接一个基于Java的后端服务器,需要与它同步时间,因此我正在寻找一种可以以多种语言使用的现成结构。

Your need for millisecond granularity and to interact with something Java based both suggest that 64-bit time since epoch would be a suitable, and straightforward, choice. 您需要毫秒级的粒度以及与基于Java的交互的需求都表明,从纪元开始的64位时间将是合适且直接的选择。

Most compilers today have 64-bit types; 今天,大多数编译器都具有64位类型。 if not you can handle it as a struct of two 32-bit types in local endian appropriate order and put in code to handle the carry. 如果不是,则可以按本地端序适当的顺序将其作为两个32位类型的结构来处理,并放入代码中以处理进位。

There's milliseconds and milliseconds, one is easy, the other is hard. 有毫秒和毫秒,一个很容易,另一个很困难。

We run a 1ms hardware timer interrupt loop that handles stuff (motor control for example) which has to operate on a solid raster. 我们运行一个1ms的硬件计时器中断循环,该循环处理必须在实体栅格上运行的内容(例如,电动机控制)。 We increment a global 32-bit "ticks" value from within this routine which can then be used to time stuff that needs to happen at sub-second intervals (EG polling something every 50ms). 我们从该例程中增加一个全局32位“滴答”值,然后该值可用于计时需要在亚秒间隔内发生的事件(EG每隔50ms轮询一次)。

That's not the same as using a the micro's hardware timer as a timekeeping reference, there are issues with the accuracy of anything in a system like this - from the accuracy of your clock crystal to all the various prescalers, interrupt latency, etc. Now, we don't care if our motor control routine runs 999 times per second or 1001 times a second, or if we poll the state of a pin every 49.5ms rather than 50, because it's close enough, and what's important is that it happens in a timely manner. 这与使用Micro的硬件计时器作为计时参考是不同的,像这样的系统中的任何东西都存在准确性问题-从时钟晶体的准确性到所有各种预分频器,中断延迟等。现在,我们不在乎我们的电机控制例程是每秒运行999次还是每秒运行1001次,还是我们每49.5ms而不是50ms轮询一次引脚的状态,因为它足够接近,所以重要的是它发生在及时。 Over the course of 24 hours we may well end up with a load more "ticks" than there are milliseconds in the day, which would make for a terrible watch. 在24小时的过程中,我们可能最终会承受比一天中的毫秒数更多的“滴答声”负载,这将使手表变得非常糟糕。

For example - does the clock prescaler count to N and then reset, or n-1 and reset? 例如-时钟预分频器计数到N然后复位,还是n-1然后复位? Does it reset instantly or does it take one clock cycle? 它会立即复位还是需要一个时钟周期? This sort of detail makes for timing headaches in micros. 此类细节使计时头痛变得十分微妙。

I would use the RTC as the time-of-day reference and then perhaps synchronise the ms counter to the ticking of the seconds (reset "ticks" to 0 every 1Hz RTC interrupt) which would mean your ms-value would only ever be very slightly out relative to the RTC. 我将RTC用作时间参考,然后也许将ms计数器与秒的滴答滴答同步(每1Hz RTC中断将“滴答”重置为0),这意味着您的ms值只会非常高相对于RTC略有偏离。 You may even be able to read the input-clock register of the RTC directly to extract the much faster clock that runs the RTC (typically a 32.768kHz clock). 您甚至可以直接读取RTC的输入时钟寄存器,以提取运行RTC的更快的时钟(通常为32.768kHz时钟)。 We do this to get microsecond values from our 1kHz timer's prescaler clock register. 我们这样做是为了从1kHz定时器的预分频器时钟寄存器中获得微秒值。 It's not perfect, we don't use it to keep time, only to catch sub-ms events. 这不是完美的,我们不使用它来节省时间,仅用于捕获sub-ms事件。

Alternatively, look at if you really need ms at all for the application, or if you could just make up a number that's within 100ms and report that, it's not like JS is atomic-clock grade timing wise - it's not even mickey-mouse-watch-grade. 另外,看看应用程序是否真的需要 ms,或者您是否可以组成一个100ms以内的数字并报告,这并不意味着JS并不是原子钟级计时的明智之举-它甚至不是mickey-mouse-手表等级。 If you really do need that accuracy, you're doing it wrong. 如果您确实需要这种准确性,那就错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM