无法链接到嵌入式系统上的 gettimeofday,经过时间建议?

发布于 2024-11-28 12:42:01 字数 976 浏览 1 评论 0原文

我正在尝试在嵌入式 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 毫秒)

I am trying to use gettimeofday on an embedded ARM device, however it seems as though I am unable to use it:

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

I am assuming I cannot use gettimeofday() ? Does anyone have any suggestions for being able to tell elapsed time? (e.g., 100ms)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请别遗忘我 2024-12-05 12:42:01

您需要做的是创建自己的 _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()

What you need to do is create your own _gettimeofday() function to get it to link properly. This function could use the appropriate code to get the time for your processor, assuming you have a free-running system timer available.

#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()
笑脸一如从前 2024-12-05 12:42:01

我通常做的是让一个定时器以 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

What I usually do, is to have a timer running at 1khz, so it will generate an interrupt every millisecond, in the interrupt handler I increment a global var by one, say ms_ticks then do something like:

volatile unsigned int ms_ticks = 0;

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

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

It is also possible to use this as a timestamp, so let's say I want to do something every 500ms:

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
}

Another alternative, since ARMs are 32bits and your timer will probably be a 32bits one, is to instead of generating a 1khz interrupt, you leave it free running and simply use the counter as your ms_ticks.

浊酒尽余欢 2024-12-05 12:42:01

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

Use one of the timers in the chip...

红墙和绿瓦 2024-12-05 12:42:01

看起来您正在使用基于 Freescale MC13224v 的 Econotag。

MACA_CLK 寄存器提供了一个非常好的时基(假设无线电正在运行)。您还可以使用带有 CRM->RTC_COUNT 的 RTC。 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 /计时器

It looks like you are using the Econotag which is based on the MC13224v from Freescale.

The MACA_CLK register provides a very good timebase (assuming the radio is running). You can also use the the RTC with CRM->RTC_COUNT. The RTC may or may not be very good depending on if you have an external 32kHz crystal or not (the econotag does NOT).

e.g. with MACA_CLK:

uint32_t t;

t = *MACA_CLK;

while (*MACA_CLK - t > SOMETIME);

See also the timer examples in libmc1322x:

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

Alternate methods are to use etimers or rtimers in Contiki (which has good support for the Econotag). (see http://www.sics.se/contiki/wiki/index.php/Timers )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文