首页 > 解决方案 > 无法在嵌入式 c 中为 stm32 使用 sleep()

问题描述

我尝试为 stm32-microcontorller 学习嵌入式 c。我尝试编写一个简单的闪烁程序,在其中使用 sleep() 函数。

代码:

/* Includes ------------------------------------------------------------------*/
#include <unistd.h>
#include "main.h"

int main(void)
{
  HAL_Init();

  while (1)
  { 
    HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
    sleep(1);  // this line throws a error, when compiling
  }
}

编译器给了我以下错误:

/usr/lib/gcc/arm-none-eabi/7.4.0/../../../../arm-none-eabi/bin/ld: CMakeFiles/untitled2.elf.dir/Src/main.c.obj: in function `main':
/home/heinrich/CLionProjects/untitled2/Src/main.c:106: undefined reference to `sleep'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/untitled2.elf.dir/build.make:391: untitled2.elf] Fehler 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/untitled2.elf.dir/all] Fehler 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/untitled2.elf.dir/rule] Fehler 2
make: *** [Makefile:118: untitled2.elf] Fehler 2

我认为,问题在于未安装的库,但我在 fedora-repos 中为 arm-gcc 安装了所有内容

操作系统:Fedora 30 IDE:CLion 工具链:Arm-gcc-none-eabi

标签: csleepstm32

解决方案


您不能在带有 arm-none-eabi-gcc 编译器的裸机目标上使用 POSIX 函数。没有操作系统。没有sleep(), gettimeofday(), clock_gettime(), getpid(), fork(), stat(),open()pthread_create()很多很多其他的 C 和 posix 和 *unix 特定函数。这些函数的声明可以在标准头文件中找到,但链接器会undefined reference报错而放弃。你必须自己实现它们。

默认情况下,您的编译器 arm-none-eabi-gcc 使用C 标准库的newlib实现。它带有最基本的和不支持操作系统的功能的实现,比如snprintfmktime. 对于像printfputc回调这样的函数_write()_write_r()应该实现它们以使其工作。为了malloc()工作,你必须提供sbrk()。对于大多数其他功能,您必须自己实现它们。

常用的-specs=nosys.specs编译器选项只是指定使用某些函数的“默认”无系统实现,例如fseek()orwrite()sbrk()。这些函数中的大多数只是返回-1并将 errno 设置为ENOSYS,但它们都存在,以便您可以编译程序。可以在这里找到实现。

如果您碰巧使用 stm32 hal 库,您可以将 systick 中断初始化 1 毫秒,并使用 stm32 世界HAL_Delay()函数中的标准并提供您自己的实现sleep()

unsigned int sleep(unsigned int seconds) {
   HAL_Delay(seconds * 1000);
   return 0;
}

另一种方法是在您的设备上使用提供这些功能实现的操作系统。例如,RIOT OS旨在提供 POSIX 兼容性并且已经提供了许多调用。


推荐阅读