你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32H7系列其二

[复制链接]
STMCU小助手 发布时间:2021-12-18 17:43
printf重定向介绍
在学C语言的时候,会经常用标准库<stdio.h>中printf(), scanf()这两个函数, 来实现数据的输入与输出. 格式化输入输出的功能还是挺强大的, 那已经有了现成的轮子, 就要寻思一下如何进一步在单片机上使用它.
下面是ARM Compiler6文档中对printf的介绍:

The printf family consists of _printf(), printf(), _fprintf(), fprintf(), vprintf(), and vfprintf().
All these functions use __FILE opaquely and depend only on the functions fputc() and ferror(). The functions _printf() and _fprintf() are identical to printf() and fprintf() except that they cannot format floating-point values.
The standard output functions of the form _printf(…) are equivalent to:
fprintf(& __stdout, …)where __stdout has type __FILE.

大致是说printf家族有这么些函数<_printf(), printf(), _fprintf(), fprintf(), vprintf(), and vfprintf()>, 这些函数都隐式的使用__FILE<文件流>, 并且只依靠fputc(), ferror()这两个函数.

If you define your own version of __FILE, your own fputc() and ferror() functions, and the __stdout object, you can use all of the printf() family, fwrite(), fputs(), puts() and the C++ object std::cout unchanged from the library.

重新定义__FILE, fputc(), ferror(), __stdout, 就可以使用printf()家族的各种函数了.

STM32中printf重定向

下面是官方给出的重映射模板:
<遇到不会的先看看文档往往能得到最直接的答案>

  1. #include <stdio.h>
  2. struct __FILE
  3. {
  4.   int handle;
  5.   /* Whatever you require here. If the only file you are using is */
  6.   /* standard output using printf() for debugging, no file handling */
  7.   /* is required. */
  8. };
  9. /* FILE is typedef’d in stdio.h. */
  10. FILE __stdout;
  11. int fputc(int ch, FILE *f)
  12. {
  13.   /* Your implementation of fputc(). */
  14.   return ch;
  15. }
  16. int ferror(FILE *f)
  17. {
  18.   /* Your implementation of ferror(). */
  19.   return 0;
  20. }
复制代码

要将printf()重定向到STM32中的串口, 只需要重新实现 fputc() 函数即可, 在fputc()中通过串口发送一个char. 在上一次的工程上进行修改, 将printf()重定向到串口3.

  1. #include <stdio.h>
  2. struct __FILE
  3. {
  4.   int handle;
  5.   /* Whatever you require here. If the only file you are using is */
  6.   /* standard output using printf() for debugging, no file handling */
  7.   /* is required. */
  8. };
  9. /* FILE is typedef’d in stdio.h. */
  10. FILE __stdout;
  11. int fputc(int ch, FILE *f)
  12. {
  13.   /* Your implementation of fputc(). */
  14.   HAL_UART_Transmit( &huart3, ( uint8_t* )&ch, 1, 0xFFFF );
  15.   return ch;
  16. }
  17. int ferror(FILE *f)
  18. {
  19.   /* Your implementation of ferror(). */
  20.   HAL_UART_Transmit( &huart3, ( uint8_t* )"Printf Error\n", 13, 0xFFFF );// 可选
  21.   return 0;
  22. }
复制代码

写个简单的程序试一试:

  1. int main(void)
  2. {
  3.     MPU_Config();
  4.     SCB_EnableICache();
  5.     SCB_EnableDCache();
  6.     HAL_Init();
  7.     SystemClock_Config();
  8.     MX_GPIO_Init();
  9.     MX_ETH_Init();
  10.     MX_USART3_UART_Init();
  11.     /* USER CODE BEGIN 2 */
  12.         double d = 1.234;
  13.         int i = 60;
  14.         char str[] = "Do not be lazy.\n";
  15.         printf("Hello World\n");
  16.         printf("d = %lf\ni = %d\n", d, i);
  17.         printf("this is a string : %s", str);

  18.     while (1){}
  19. }
复制代码

输出结果:
20190313212812741.png


可以开始愉快的使用printf()了.



收藏 评论0 发布时间:2021-12-18 17:43

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版