使用Printf在ARM微控制器的串口上显示
我想使用 printf 在 ARM 微控制器的串行端口上显示文本。我无法这样做。任何帮助表示赞赏。
我的 init_serial 看起来像这样,
void init_serial (void)
{
PINSEL0 = 0x00050000; /* Enable RXD1 TxD1 */
U1LCR = 0x00000083; /*8 bits, 1 Stop bit */
U1DLL = 0x000000C2; /*9600 Baud Rate @12MHz VPB Clock */
U1LCR = 0x00000003; /* DLAB=0*/
}
这显然是错误的。
I would like to use printf to diplay text on a serial port of an ARM microcontroller. I am unable to do so. Any help is appreciated.
My init_serial looks like this
void init_serial (void)
{
PINSEL0 = 0x00050000; /* Enable RXD1 TxD1 */
U1LCR = 0x00000083; /*8 bits, 1 Stop bit */
U1DLL = 0x000000C2; /*9600 Baud Rate @12MHz VPB Clock */
U1LCR = 0x00000003; /* DLAB=0*/
}
which is obviously wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于微控制器,您通常必须定义自己的
putc
函数来将字节发送到您正在使用的任何 UART。然后print
将调用您的putc
。检查编译器附带的库的文档。
请注意,这与您初始化 UART 的方式完全无关。重要的是您正在使用哪个 UART。
(在一个不相关的问题上,而不是说:
寄存器通常有
#define
,它们(通常)有助于可读性,提供文档中位名称的链接,并减少注释的需要在每一行上添加和维护,例如:..等等。)
For microcontollers, you typically have to define your own
putc
function to send bytes to whichever UART you're using.print
will then call yourputc
.Check the documentation for the libraries supplied with your compiler.
Note that this is entirely unrelated to how you intialise your UART. All that matters is which UART you're using.
(On an unrelated issue, rather than saying:
there are typically
#define
s for registers which (usually) aid readability, provide a link to the bit names in the documentation, and reduce the need for comments to be added and maintained on every line like these. For example:..and so on.)
要使
printf()
、puts()
等在嵌入式平台上工作,您需要实现一些钩子与 C 库一起使用。这通常依赖于编译器提供的 C 库,因此可能依赖于编译器。但在许多情况下,库只要求您提供一个putc()
函数(或类似的名称),该函数接受一个字符(由生成) printf()
库函数)并将其发送到您选择的输出设备。这可以是内存缓冲区、串行端口、USB 消息等等。从 C 库的角度来看,
putc()
函数将运行到完成,因此是否将其实现为简单的函数取决于您阻塞功能(等待串口空闲并发送字符),或非阻塞(将其放入缓冲区,由后台中断任务发送;但如果您足够快地输出足够的字节,缓冲区可能会填满,然后你必须阻止或丢弃字符)。如果您有 RTOS,您还可以使其与 RTOS 一起正常工作,实现阻塞写入,该写入在信号量上休眠,直到串行端口可用。因此,总而言之,请阅读编译器及其 C 库的文档,它应该告诉您需要做什么才能使
printf()
工作。带 GCC 编译器的 AVR micro 的示例链接:
使用 newlib C 库的 ARM GCC 编译器:
To make
printf()
,puts()
etc work on an embedded platform, you need to implement some hooks that work with the C library. This is typically dependent on the C libraries provided with your compiler, so is probably compiler-dependent. But in many cases the library just requires you to provide aputc()
function (or similar name), which takes a character (generated by theprintf()
library function) and sends it to your chosen output device. That could be a memory buffer, serial port, USB message, whatever.From the point of view of the C library, the
putc()
function would be run-to-completion, so it's up to you whether you implement it to be a simple blocking function (waiting until the serial port is free and sending the character), or non-blocking (putting it into a buffer, to be sent by a background interrupt task; but the buffer might fill up if you output enough bytes fast enough, and then you have to either block or discard characters). You can also make it work properly with your RTOS if you have one, implementing a blocking write that sleeps on a semaphore until the serial port is available.So, in summary, read the documentation for your compiler and its C library, and it should tell you what you need to do to make
printf()
work.Example links for AVR micro with GCC compiler:
ARM GCC compiler using newlib C library:
我对 ARM 特别不确定...
对于某些芯片,在 IDE 中,您需要指定您需要一个堆来使用 printf,以及它应该有多大。程序员不会自动穿上它。
检查你的程序员/IDE 的菜单,看看是否有地方可以指定堆大小。
我同意史蒂夫的观点,只有当你真正可以使用 printf 时,才可以,否则编写你自己的小片段。
I'm not sure about ARM in particular...
For some chips, within the IDE, you need to specify that you need a heap to use the printf, and how big it should be. The programmer won't automatically put one on.
Check in the menus of your programmer/IDE and see if there is a place to specify the heap size.
And I agree with Steve, this is only if you can actually use the printf, otherwise write your own little snippet.