C语言 如何在树莓派上运行没有操作系统的 C 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29837892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:53:14  来源:igfitidea点击:

How to run a C program with no OS on the Raspberry Pi?

carmembeddedraspberry-pilow-level

提问by watswat5

I'd like to experiment using the Raspberry Pi for some different low level embedded applications. The only problem is that, unlike the AVR and PIC microcontroller boards available, Raspberry Pi typically runs an OS (like Raspbian) that distributes CPU time across all running programs and makes it impractical for certain real time applications.

我想尝试将 Raspberry Pi 用于一些不同的低级嵌入式应用程序。唯一的问题是,与可用的 AVR 和 PIC 微控制器板不同,Raspberry Pi 通常运行一个操作系统(如 Raspbian),该操作系统将 CPU 时间分配给所有正在运行的程序,这使得它对某些实时应用程序不切实际。

I've recently learned that, assuming you have a bootloader like GRUB installed, running a C program on x86 (in the form of a kernel)takes very little actual setup, just an assembly program to call the main function and the actual C code.

我最近了解到,假设你安装了一个像 GRUB 这样的引导加载程序,在 x86 上运行一个 C 程序(以内核的形式)只需要很少的实际设置,只需要一个汇编程序来调用 main 函数和实际的 C 代码.

Is there a way to achieve this with a Raspberry Pi? It'd be a great way to learn about low level ARM programming, and it already has a few complex peripherals to mess around with (USB, Ethernet, etc.)

有没有办法用树莓派来实现这一点?这将是学习低级 ARM 编程的好方法,而且它已经有一些复杂的外设可以处理(USB、以太网等)

采纳答案by It'sPete

While bare metal is possible on the Pi, I would avoid it since Linux is getting so lightweight and handles a whole bunch of stuff for you.

虽然在 Pi 上可以使用裸机,但我会避免使用它,因为 Linux 变得如此轻量级并且可以为您处理一大堆东西。

Here's a tutorial to get you started if you want to still learn bare metal stuff: http://www.valvers.com/open-software/raspberry-pi/step01-bare-metal-programming-in-cpt1/

如果您仍然想学习裸机,这里有一个教程可以帮助您入门:http: //www.valvers.com/open-software/raspberry-pi/step01-bare-metal-programming-in-cpt1/

With all that said, I would just load up your favorite embedded linux distro (RT patched might be preferred based on your requirements) and call it good.

尽管如此,我只想加载您最喜欢的嵌入式 linux 发行版(根据您的要求,RT 补丁可能是首选)并称其为好。

回答by joshbooks

https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/is a great tutorial, and as they'll tell you the best quick and dirty way to run code on bare metal is to hiHyman a linux distro, to do that, just compile to kernel.img (with the appropriate architecture options) and use it to replace the existing one in the linux distro for just this section of the tutorial you can go to: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ok01.html#pitime

https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/是一个很棒的教程,他们会告诉你在裸机上运行代码的最好的快速和肮脏的方法是劫持一个linux 发行版,要做到这一点,只需编译到 kernel.img(使用适当的架构选项)并使用它来替换 linux 发行版中的现有发行版,仅在本教程的这一部分中,您可以访问: https://www。 cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ok01.html#pitime

回答by unixsmurf

The Pi may be a bit suboptimal for what you are wanting to do, since the SoC design is such that the ARM CPU is a second-class citizen - meaning there are some hoops to jump through to get a bare metal program running on it.

对于您想要做的事情,Pi 可能有点不太理想,因为 SoC 设计使得 ARM CPU 是二等公民 - 这意味着要在其上运行裸机程序需要跳过一些障碍。

However, you could cheat a bit and use the U-Boot APIto give you access to some of the features U-Boot provides but be able to add your own features on the side.

但是,您可以稍微作弊并使用U-Boot API来访问 U-Boot 提供的一些功能,但也可以添加自己的功能。