Use printf to output stream on STM32F4

It would be nice, if you can simply just use printf to direct output strings to USART, LCD, etc. With ARM C, you are able to do this. You just need to implement some things and you are ready to work.

New STDIO library is available here.

To enable printf functionality, first you need to create a new __FILE struct. This struct is then called with FILE struct. It can have only one dummy parameter, but it has to be created, because you need pointer to this structure if you want to output characters to stream. If you need any control for this, you can create your own. I created my own below:

After that, we have to create a variable with FILE struct:

Variable name __stdout is important. You can not use different name, otherwise printf will not work. Last thing we need to create is a function, that will be automatically called from printf and will actually print your charater by character to stream. Function has fixed name and can not be changed.

You are ready to use printf function. Below is simple example, that will output data to USART1 with printf using my USART library.

Example

You will need my USART library for this, download below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...