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

【经验分享】STM32G0学习手册——FreeRTOS中使用printf-stdarg.c进行串口打印

[复制链接]
STMCU小助手 发布时间:2021-11-10 01:00
1.创建好FreeRTOS代码,在User文件夹中添加printf-stdarg.c。并添加到工程中

2.重定向printf到串口,在usart.c中添加代码

  1. /* USER CODE BEGIN 0 */

  2. #include <stdio.h>
  3. #ifdef __GNUC__
  4.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  5.      set to 'Yes') calls __io_putchar() */
  6.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  7. #else
  8.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  9. #endif /* __GNUC__ */
  10. ///**
  11. //  * @brief  Retargets the C library printf function to the USART.
  12. //  * @param  None
  13. //  * @retval None
  14. //  */
  15. PUTCHAR_PROTOTYPE
  16. {
  17.   /* Place your implementation of fputc here */
  18.   /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  19.   HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);

  20.   return ch;
  21. }

  22. /* USER CODE END 0 */
复制代码

3.修改printf-stdarg.c

  1. /*
  2. #define putchar(c) c
  3. */

  4. static void printchar(char **str, int c)
  5. {
  6.         extern int putchar(int c);       //去掉原本的注释
  7.         
  8.         if (str) {
  9.                 **str = (char)c;
  10.                 ++(*str);
  11.         }
  12.         else
  13.         {
  14.                 (void)putchar(c);
  15.         }
  16. }
复制代码
  1. /*修改printf为f_printf。sprintf,snprintf也是一样修改*/
  2. int f_printf(const char *format, ...)
  3. {
  4.         va_list args;

  5.         va_start( args, format );
  6.         return print( 0, format, args );
  7. }

  8. int f_sprintf(char *out, const char *format, ...)
  9. {
  10.         va_list args;

  11.         va_start( args, format );
  12.         return print( &out, format, args );
  13. }


  14. int f_snprintf( char *buf, unsigned int count, const char *format, ... )
  15. {
  16.         va_list args;

  17.         ( void ) count;

  18.         va_start( args, format );
  19.         return print( &buf, format, args );
  20. }
复制代码

4.修改main.c

  1. #include <stdio.h>
  2. extern int f_printf(const char *format, ...);

  3. /*
  4. *
  5. */
  6. int main()
  7. {
  8.   /**/
  9. }

  10. static void LED1_Task(void* pvPrameters)
  11. {
  12.         while(1)
  13.         {
  14.                 temp_count++;
  15.                 HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);

  16.         /*串口输出字符*/
  17.                 f_printf("LED1_TASK running.");     

  18.                 vTaskDelay(1000);
  19.         }
  20. }
复制代码


收藏 评论0 发布时间:2021-11-10 01:00

举报

0个回答

所属标签

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