cant use sleep() in embedded c for stm32

roe*_*ler -1 c sleep stm32

I try to learn embedded c for a stm32-microcontorller. I try to wirte a easy blink program, where i use the sleep()-function.

code:

/* 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
  }
}
Run Code Online (Sandbox Code Playgroud)

the compiler gives me following error:

/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
Run Code Online (Sandbox Code Playgroud)

I think, the problem is a not-installed libary, but i installed everything in the fedora-repos for arm-gcc

OS: Fedora 30 IDE: CLion Toolchain: Arm-gcc-none-eabi

Kam*_*Cuk 7

您不能通过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()write()sbrk()。这些函数大多数只是返回-1并将errno设置为ENOSYS,但是在那里,因此您可以编译程序。可以在这里找到实现。

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

unsigned int sleep(unsigned int seconds) {
   HAL_Delay(seconds * 1000);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

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