使用< chrono>作为裸机微控制器中的计时器? [英] Using <chrono> as a timer in bare-metal microcontroller?

查看:89
本文介绍了使用< chrono>作为裸机微控制器中的计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

chrono可以在裸机微控制器(例如运行RTOS的MSP432)中用作计时器/计数器吗?可以配置high_resolution_clock(以及chrono中的其他API),使其基于给定的微控制器的实际计时器滴答/寄存器来递增吗?

Can chrono be used as a timer/counter in a bare-metal microcontroller (e.g. MSP432 running an RTOS)? Can the high_resolution_clock (and other APIs in chrono) be configured so that it increments based on the given microcontroller's actual timer tick/register?

实时C ++书籍(本节) 16.5)似乎暗示这是可能的,但是我还没有发现这种应用的任何示例,尤其是在裸机微控制器中。

The Real-Time C++ book (section 16.5) seems to suggest this is possible, but I haven't found any examples of this being applied, especially within bare-metal microcontrollers.

如何实现?甚至会推荐吗?如果没有,那么在基于RTOS的嵌入式软件中,什么地方可以提供时间帮助?

How could this be implemented? Would this be even recommended? If not, where can chrono aid in RTOS-based embedded software?

推荐答案

我会创建一个时钟,该时钟现在可以通过读取您的计时器寄存器:

I would create a clock that implements now by reading from your timer register:

#include <chrono>
#include <cstdint>

struct clock
{
    using rep        = std::int64_t;
    using period     = std::milli;
    using duration   = std::chrono::duration<rep, period>;
    using time_point = std::chrono::time_point<clock>;
    static constexpr bool is_steady = true;

    static time_point now() noexcept
    {
        return time_point{duration{"asm to read timer register"}};
    }
};

根据处理器的速度调整周期(但它必须是编译时常数)。在上面,我将其设置为1 tick / ms。这是1滴== 2ns的读数:

Adjust period to whatever speed your processor ticks at (but it does have to be a compile-time constant). Above I've set it for 1 tick/ms. Here is how it should read for 1 tick == 2ns:

using period = std::ratio<1, 500'000'000>;

现在您可以说:

auto t = clock::now();  // a chrono::time_point

auto d = clock::now() - t;  // a chrono::duration

这篇关于使用&lt; chrono&gt;作为裸机微控制器中的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆