测试下调试串口使用printf打印输出。
一、硬件电路
1.1、串口部分硬件电路图
1.2、芯片数据手册中,映射到了串口1
二、程序部分
2.1、usart.c
- #include "main.h"
- #include "usart/usart.h"
- void init_uart(uint32_t bps)
- {
- GPIO_InitTypeDef GPIO_InitStructure = {0};
- USART_InitTypeDef USART_InitStructure = {0};
-
- RCC_AHBPeriphClk_Enable(DEBUG_USART_GPIO_CLK, ENABLE);
- DEBUG_USART_APBClkENx(DEBUG_USART_CLK, ENABLE);
-
- DEBUG_USART_AFTX;
- DEBUG_USART_AFRX;
- GPIO_InitStructure.Pins = DEBUG_USART_TX_GPIO_PIN;
- GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.Pins = DEBUG_USART_RX_GPIO_PIN;
- GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
- GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
-
- USART_InitStructure.USART_BaudRate = bps;
- USART_InitStructure.USART_Over = USART_Over_16;
- USART_InitStructure.USART_Source = USART_Source_PCLK;
- USART_InitStructure.USART_UclkFreq = DEBUG_USART_UclkFreq;
- USART_InitStructure.USART_StartBit = USART_StartBit_FE;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(DEBUG_USARTx, &USART_InitStructure);
- }
- void uart_senddat(uint8_t dat)
- {
- USART_SendData_8bit(DEBUG_USARTx, dat);
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
- }
- int fputc(int ch, FILE *f)
- {
- uart_senddat((uint8_t)ch);
- return ch;
- }
2.2、main.c
- #include "main.h"
- #include "led/led.h"
- #include "key/key.h"
- #include "usart/usart.h"
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
- /******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- void RCC_Configuration(void);
- void NVIC_Configuration(void);
- int32_t main(void)
- {
- RCC_Configuration();
- NVIC_Configuration();
- init_uart(115200);
- init_led();
- init_key();
- while(1)
- {
- printf("cw32l031 board @21ic\r\n");
- SysTickDelay(10);
- }
- }
三、程序运行
程序运行后,串口输出
|