我试图使用来自clock()库的"time.h"函数实现一个错误处理程序。代码运行在一个嵌入式系统中(Colibri IMX7 - M4处理器)。该函数用于监视特定范围内的当前值,如果当前值不正确,则该函数应返回一条错误消息。
函数将查看错误是否锁定,在第一次运行时,它将将错误的第一次外观保存在clock_t中作为引用,然后在下一次运行时,如果错误仍然存在,它将比较使用clock()的当前时间和前面的引用,并查看它是否会超过特定时间。
问题是函数clock()总是返回-1。我该怎么做才能避免这种情况?另外,为什么我不能将clock_t变量声明为静态变量(例如,static clock_t start_t = clock() )
请参见下文的职能:
bool CrossLink_check_error_LED_UV_current_clock(int current_state, int current_at_LED_UV)
{
bool has_LED_UV_current_deviated = false;
static int current_number_of_errors_Current_LED_CANNON = 0;
clock_t startTimeError = clock();
const int maximum_operational_current_when_on = 2000;
const int minimum_turned_on_LED_UV_current = 45;
if( (current_at_LED_UV > maximum_operational_current_when_on)
||(current_state!=STATE_EMITTING && (current_at_LED_UV > minimum_turned_on_LED_UV_current))
||(current_state==STATE_EMITTING && (current_at_LED_UV < minimum_turned_on_LED_UV_current)) ){
current_number_of_errors_Current_LED_CANNON++;
if(current_number_of_errors_Current_LED_CANNON > 1) {
if (clock() - startTimeError > 50000){ // 50ms
has_LED_UV_current_deviated = true;
PRINTF("current_at_LED_UV: %d", current_at_LED_UV);
if(current_state==STATE_EMITTING){
PRINTF(" at state emitting");
}
PRINTF("\n\r");
}
}else{
if(startTimeError == -1){
startTimeError = clock();
}
}
}else{
startTimeError = 0;
current_number_of_errors_Current_LED_CANNON = 0;
}
return has_LED_UV_current_deviated;
}编辑:我忘了前面提到过,但我们使用GCC 9.3.1 arm-无eabi编译器与CMake构建可执行文件。我们有一个嵌入式系统(Colibri IMX7由Toradex制造),它由两个A7处理器组成,运行我们的Linux (更直观的界面),而用于控制我们设备的程序运行在一个没有操作系统的M4处理器中,仅仅是纯裸金属。
发布于 2022-04-18 11:50:25
最后的问题是,由于我们在裸金属上运行我们的代码,clock()函数无法工作。最后,我们在我们找到的M4处理器上使用了内部计时器,所以现在一切都很好。谢谢你的回答。
发布于 2022-04-05 20:06:32
对于c标准库中提供的许多函数,如果安装了文档(通常与编译器一起安装),则可以在shell中使用man命令查看文档。对于man clock,它告诉我:
NAME
clock - determine processor time
SYNOPSIS
#include <time.h>
clock_t clock(void);
DESCRIPTION
The clock() function returns an approximation of processor time used by the program.
RETURN VALUE
The value returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide by
CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function
returns the value (clock_t) -1.
etc.这告诉我们-1意味着处理器时间(CLOCK_PROCESS_CPUTIME_ID)不可用。解决方案是使用CLOCK_MONOTONIC代替。我们可以选择与clock_gettime一起使用的时钟。
timespec clock_time;
if (clock_gettime(CLOCK_MONOTONIC, &clock_time)) {
printf("CLOCK_MONOTONIC is unavailable!\n");
exit(1);
}
printf("Seconds: %d Nanoseconds: %ld\n", clock_time.tv_sec, clock_time.tv_nsec);发布于 2022-04-05 22:50:23
要回答你问题的第二部分:
static clock_t start_time = clock();不允许使用,因为函数clock()的返回值直到运行时才知道,但是在C中,静态变量的初始化程序必须是编译时常量。
你可以写:
static clock_t start_time = 0;
if (start_time == 0)
{
start_time = clock();
}但在这种情况下,这可能适合使用,也可能不适合使用,这取决于零是否是函数的合法返回值。如果可能的话,你需要这样的东西:
static bool start_time_initialized = false;
static clock_t start_time;
if (!start_time_initialized)
{
start_time_initialized = true;
start_time = clock();
}只有当您不能同时运行该函数的两个副本(它不是可重入者)时,上面的内容才是可靠的。
如果您有一个POSIX库可用,您可以使用一个pthread_once_t来做上述的bool,但以可重入的方式。详情请参见man pthread_once。
请注意,C++在这方面允许更复杂的选项,但您已经询问了C。
还请注意,将"start time“缩写为start_t是一个非常糟糕的主意,因为_t后缀的意思是" type”,应该只用于类型名称。
https://stackoverflow.com/questions/71757716
复制相似问题