飞虎神鹰前传之英雄:embedded To make printf(), puts() etc work on an embedded platform

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 01:38:22

To make printf(), puts()etc work on an embedded platform, you need to implement some hooks thatwork with the C library. This is typically dependent on the C librariesprovided with your compiler, so is probably compiler-dependent. But inmany cases the library just requires you to provide a putc() function (or similar name), which takes a character (generated by the printf() 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 youimplement it to be a simple blocking function (waiting until the serialport is free and sending the character), or non-blocking (putting itinto a buffer, to be sent by a background interrupt task; but the buffermight fill up if you output enough bytes fast enough, and then you haveto either block or discard characters). You can also make it workproperly with your RTOS if you have one, implementing a blocking writethat 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:

  • AVR libc stdio docs
  • a blog post

ARM GCC compiler using newlib C library:

  • Newlib C library docs
    • Defining host interface - syscalls - write() function