查看: 2809|回复: 1

FM33LC0X系列,需要使用UART5作为调试串口,使用UART5答应数据,使用重定向printf打印和不使用重定向UART5_printf打印

[复制链接]

1

主题

2

帖子

140

积分

中级工程师

Rank: 2

积分
140
发表于 2023-3-7 19:38:42 | 显示全部楼层 |阅读模式
直接上代码1、重定向printf打印的代码
  1. #if 1
  2. #pragma import(__use_no_semihosting)                          
  3. struct __FILE
  4. {
  5.     int handle;
  6. };
  7. FILE __stdout;
  8. void _sys_exit(int x)
  9. {
  10.     x = x;
  11. }
  12. int fputc(int ch, FILE *f)
  13. {
  14.         UART5->TXBUF = ch;
  15.     while (!(UART5->ISR & (0x1 << 0)));
  16.     UART5->ISR |= (0x1 << 0);
  17.     return ch;
  18. }
  19. #endif
复制代码
2、不使用重定向UART5_printf打印的代码,

  1. void UART5_printf (char *fmt, ...)
  2. {
  3.         char buffer[UART5_SEND_LEN+1];  
  4.         int i = 0;
  5.         va_list arg_ptr;
  6.         va_start(arg_ptr, fmt);   
  7.         vsnprintf(buffer, UART5_SEND_LEN+1, fmt, arg_ptr);
  8.         while ((i < UART5_SEND_LEN) && buffer[i])
  9.         {
  10.                                         FL_UART_WriteTXBuff(UART5, (uint8_t) buffer[i++]);
  11.         while(FL_SET != FL_UART_IsActiveFlag_TXShiftBuffEmpty(UART5));       //等待发送完成
  12.         }
  13.         va_end(arg_ptr);
  14. }
复制代码
以上代码均是借鉴网络的,把我的成果也分享成了。完整代码
3、print.c文件完整代码
  1. <blockquote>#include "print.h"
复制代码


4、print.h文件完整代码

  1. <blockquote>#ifndef __PRINT_1_H__
复制代码

注意:一定要将UART5初始化
回复

使用道具 举报

1

主题

2

帖子

140

积分

中级工程师

Rank: 2

积分
140
 楼主| 发表于 2023-3-10 15:46:33 | 显示全部楼层
  1. #include "print.h"




  2. #if 1
  3. #pragma import(__use_no_semihosting)                          
  4. struct __FILE
  5. {
  6.     int handle;
  7. };
  8. FILE __stdout;      

  9. void _sys_exit(int x)
  10. {
  11.     x = x;
  12. }

  13. int fputc(int ch, FILE *f)
  14. {
  15.         UART5->TXBUF = ch;
  16.     while (!(UART5->ISR & (0x1 << 0)));
  17.     UART5->ISR |= (0x1 << 0);
  18.    
  19.     return ch;
  20. }
  21. #endif



  22. /**
  23.   * @brief  UART0 Initialization function
  24.   * @param  void
  25.   * @retval None
  26.   */
  27. void MF_UART5_Init(void)
  28. {
  29.     FL_GPIO_InitTypeDef    GPIO_InitStruct;

  30.     FL_UART_InitTypeDef    UART5_InitStruct;

  31.     GPIO_InitStruct.pin = FL_GPIO_PIN_4;
  32.     GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
  33.     GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
  34.     GPIO_InitStruct.pull = FL_ENABLE;
  35.     GPIO_InitStruct.remapPin = FL_ENABLE;
  36.     FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  37.     GPIO_InitStruct.pin = FL_GPIO_PIN_5;
  38.     GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
  39.     GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
  40.     GPIO_InitStruct.pull = FL_DISABLE;
  41.     GPIO_InitStruct.remapPin = FL_ENABLE;
  42.     FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  43.     UART5_InitStruct.clockSrc = NULL;
  44.     UART5_InitStruct.baudRate = 115200;
  45.     UART5_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
  46.     UART5_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
  47.     UART5_InitStruct.parity = FL_UART_PARITY_NONE;
  48.     UART5_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;

  49.     FL_UART_Init(UART5, &UART5_InitStruct);   
  50. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表