3

我正在尝试在嵌入式 ARM 设备上使用 gettimeofday,但似乎我无法使用它:

gnychis@ubuntu:~/Documents/coexisyst/econotag_firmware$ make
Building for board: redbee-econotag
       CC obj_redbee-econotag/econotag_coexisyst_firmware.o
LINK (romvars) econotag_coexisyst_firmware_redbee-econotag.elf
/home/gnychis/Documents/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none- eabi/4.3.2/../../../../arm-none-eabi/lib/libc.a(lib_a-gettimeofdayr.o): In function `_gettimeofday_r':
gettimeofdayr.c:(.text+0x1c): undefined reference to `_gettimeofday'
/home/gnychis/Documents/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-eabi/4.3.2/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0x18): undefined reference to `_sbrk'
collect2: ld returned 1 exit status
make[1]: *** [econotag_coexisyst_firmware_redbee-econotag.elf] Error 1
make: *** [mc1322x-default] Error 2

我假设我不能使用 gettimeofday() ?有没有人有任何关于能够分辨经过时间的建议?(例如,100 毫秒)

4

5 回答 5

6

您需要做的是创建自己的 _gettimeofday() 函数以使其正确链接。假设您有一个自由运行的系统计时器可用,此函数可以使用适当的代码为您的处理器获取时间。

#include <sys/time.h>

int _gettimeofday( struct timeval *tv, void *tzvp )
{
    uint64_t t = __your_system_time_function_here__();  // get uptime in nanoseconds
    tv->tv_sec = t / 1000000000;  // convert to seconds
    tv->tv_usec = ( t % 1000000000 ) / 1000;  // get remaining microseconds
    return 0;  // return non-zero for error
} // end _gettimeofday()
于 2014-07-08T18:43:20.710 回答
2

使用芯片中的一个定时器...

于 2011-08-10T03:26:06.777 回答
2

我通常做的是让一个以 1khz 运行的计时器,所以它会每毫秒产生一个中断,在中断处理程序中,我将一个全局变量加一,ms_ticks然后执行以下操作:

volatile unsigned int ms_ticks = 0;

void timer_isr() { //every ms
    ms_ticks++;
}

void delay(int ms) {
    ms += ms_ticks;
    while (ms > ms_ticks)
        ;
}

也可以将其用作时间戳,因此假设我想每 500 毫秒执行一次操作:

last_action = ms_ticks;

while (1) {  //app super loop

    if (ms_ticks - last_action >= 500) {
        last_action = ms_ticks;
        //action code here
    }

    //rest of the code
}

另一种选择,因为 ARM 是 32 位的,而你的定时器可能是 32 位的,所以不是生成 1khz 中断,而是让它自由运行并简单地使用计数器作为你的ms_ticks.

于 2011-08-10T01:01:09.533 回答
0

看起来您正在使用基于飞思卡尔的 MC13224v 的 Econotag。

MACA_CLK 寄存器提供了一个非常好的时基(假设无线电正在运行)。您还可以将 RTC 与 CRM->RTC_COUNT 一起使用。RTC 可能会也可能不会很好,这取决于您是否有外部 32kHz 晶振(econotag 没有)。

例如使用 MACA_CLK:

uint32_t t;

t = *MACA_CLK;

while (*MACA_CLK - t > SOMETIME);

另请参阅 libmc1322x 中的计时器示例:

http://git.devl.org/?p=malvira/libmc1322x.git;a=blob;f=tests/tmr.c

替代方法是在 Contiki 中使用 etimers 或 rtimers(它对 Econotag 有很好的支持)。(见http://www.sics.se/contiki/wiki/index.php/Timers

于 2012-08-05T02:32:24.113 回答
-2

我以前在我的一个应用程序中做过这个。只需使用:

while(1)
{
...
}
于 2011-08-10T02:28:22.383 回答