c - 无法在嵌入式 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
解决方案
您不能在带有 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实现。它带有最基本的和不支持操作系统的功能的实现,比如snprintf
和mktime
. 对于像printf
或putc
回调这样的函数_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 兼容性并且已经提供了许多调用。
推荐阅读
- java - 如何从 SqlExceptionHelper 降低死锁日志的日志级别?
- mysql - MySQL存储过程:如何缩短语句?
- c - 无法从中断访问数组指针
- python - 在 QT 设计器中放置在小部件内时组件样式发生变化
- python - 在由 IPython 内核提供支持的 Jupyter 笔记本中重置下划线 (`_`) 变量
- javascript - 如何使用 Cloud Functions for Firebase 检测浏览器类型?
- javascript - html2canvas 产生模糊的图像
- spring-boot - docker swarm 中的 Spring Boot Eureka 注册了错误的内部 docker ip
- java - Spring自定义错误页面没有为/error返回显式映射
- javascript - FileReader onload 函数未触发