[开发板] 【CW32L031CxTx StartKit评估板测评】串口printf

[复制链接]
716|0
 楼主| tlled 发表于 2023-10-26 23:50 | 显示全部楼层 |阅读模式
测试下调试串口使用printf打印输出。

一、硬件电路

1.1、串口部分硬件电路图
001.jpg

1.2、芯片数据手册中,映射到了串口1
002.jpg

二、程序部分

2.1、usart.c
  1. #include "main.h"
  2. #include "usart/usart.h"

  3. void init_uart(uint32_t bps)
  4. {
  5.         GPIO_InitTypeDef GPIO_InitStructure = {0};
  6.         USART_InitTypeDef USART_InitStructure = {0};
  7.        
  8.         RCC_AHBPeriphClk_Enable(DEBUG_USART_GPIO_CLK, ENABLE);
  9.         DEBUG_USART_APBClkENx(DEBUG_USART_CLK, ENABLE);
  10.        
  11.         DEBUG_USART_AFTX;
  12.         DEBUG_USART_AFRX;

  13.         GPIO_InitStructure.Pins = DEBUG_USART_TX_GPIO_PIN;
  14.         GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  15.         GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

  16.         GPIO_InitStructure.Pins = DEBUG_USART_RX_GPIO_PIN;
  17.         GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  18.         GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
  19.        
  20.         USART_InitStructure.USART_BaudRate = bps;
  21.         USART_InitStructure.USART_Over = USART_Over_16;
  22.         USART_InitStructure.USART_Source = USART_Source_PCLK;
  23.         USART_InitStructure.USART_UclkFreq = DEBUG_USART_UclkFreq;
  24.         USART_InitStructure.USART_StartBit = USART_StartBit_FE;
  25.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  26.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  27.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  28.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  29.         USART_Init(DEBUG_USARTx, &USART_InitStructure);
  30. }

  31. void uart_senddat(uint8_t dat)
  32. {
  33.         USART_SendData_8bit(DEBUG_USARTx, dat);
  34.         while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
  35. }

  36. int fputc(int ch, FILE *f)
  37. {
  38.                 uart_senddat((uint8_t)ch);
  39.     return ch;
  40. }


2.2、main.c
  1. #include "main.h"
  2. #include "led/led.h"
  3. #include "key/key.h"
  4. #include "usart/usart.h"

  5. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  6. /******************************************************************************
  7. * Local function prototypes ('static')
  8. ******************************************************************************/
  9. void RCC_Configuration(void);
  10. void NVIC_Configuration(void);



  11. int32_t main(void)
  12. {
  13.   RCC_Configuration();  
  14.   NVIC_Configuration();
  15.         init_uart(115200);
  16.         init_led();
  17.         init_key();  


  18.         while(1)
  19.         {       
  20.                 printf("cw32l031 board @21ic\r\n");               
  21.                 SysTickDelay(10);
  22.         }
  23. }



三、程序运行

程序运行后,串口输出
003.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部