gpt4 book ai didi

c++11 - 在裸机微 Controller 中使用 作为定时器?

转载 作者:行者123 更新时间:2023-12-04 23:38:26 25 4
gpt4 key购买 nike

chrono 能否用作裸机微 Controller (例如运行 RTOS 的 MSP432)中的定时器/计数器?是否可以配置 high_resolution_clock(以及 chrono 中的其他 API),使其根据给定微 Controller 的实际计时器滴答/寄存器递增?

Real-Time C++ book(第 16.5 节)似乎表明这是可能的,但我还没有找到任何应​​用的例子,尤其是在裸机微 Controller 中。

如何实现?这甚至会被推荐吗?如果没有,那么 chrono 在哪里可以帮助基于 RTOS 的嵌入式软件?

最佳答案

我将通过从您的定时器寄存器中读取来创建一个现在实现的时钟:

#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 滴答/毫秒。以下是 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

关于c++11 - 在裸机微 Controller 中使用 <chrono> 作为定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com