【CW32L052测评】 printf输出测试
[复制链接]
本帖最后由 TL-LED 于 2023-7-18 23:30 编辑
这篇来测试下printf输出。
一、硬件电路
测试串口输出使用到开发板上的USB转串口。
1.1、电路图部分
1.2、引脚功能
使用到PD02和PC12端口,看下手册中这两个引脚的定义,对应的串口2。
1.3、时钟配置
时钟框图
UART2使用PCLK时钟。
二、程序部分
2.1、时钟配置
时钟PCLK配置为48MHz,配置串口2时钟也要相应的配置为48MHz。
void rcc_config(void)
{
uint8_t res = 0U;
RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_FLASH, ENABLE);
RCC_LSI_Enable( ); //开启外部高速时钟LSI
res = RCC_SysClk_Switch( RCC_SYSCLKSRC_LSI ); //切换系统时钟到LSI
if( res == 0x00 ) //系统时钟切换成功
{
RCC_HSI_Disable(); //切换时钟到PLL后关闭源时钟HSI
FirmwareDelay( 400 ); //about 100mS
}
RCC_HSI_Enable( RCC_HSIOSC_DIV1 ); //开启内部高速时钟HSI = HSIOSC / 2
FLASH_SetLatency(FLASH_Latency_3);
res = RCC_SysClk_Switch( RCC_SYSCLKSRC_HSI ); //切换系统时钟到HSI
if( res == 0x00 ) //系统时钟切换成功
{
RCC_LSI_Disable(); //切换时钟到HSI后关闭LSI时钟
FirmwareDelay( 275000 ); //about 100mS
}
RCC_HCLKPRS_Config(RCC_HCLK_DIV1);
RCC_HCLK_OUT(); //通过PA04观察HCLK频率
}
2.2、fun_uart.c
#include "main.h"
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
void init_uart(void)
{
UART_InitTypeDef UART_InitStructure = {0};
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_AHBPeriphClk_Enable(DEBUG_UART_GPIO_CLK, ENABLE);
DEBUG_UART_APBClkENx(DEBUG_UART_CLK, ENABLE);
//UART TX RX ??
DEBUG_UART_AFTX;
DEBUG_UART_AFRX;
GPIO_InitStructure.Pins = DEBUG_UART_TX_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Init(DEBUG_UART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pins = DEBUG_UART_RX_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
GPIO_Init(DEBUG_UART_RX_GPIO_PORT, &GPIO_InitStructure);
UART_InitStructure.UART_BaudRate = DEBUG_UART_BaudRate;
UART_InitStructure.UART_Over = UART_Over_16;
UART_InitStructure.UART_Source = UART_Source_PCLK;
UART_InitStructure.UART_UclkFreq = DEBUG_UART_UclkFreq;
UART_InitStructure.UART_StartBit = UART_StartBit_FE;
UART_InitStructure.UART_StopBits = UART_StopBits_1;
UART_InitStructure.UART_Parity = UART_Parity_No ;
UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
UART_Init(DEBUG_UARTx, &UART_InitStructure);
}
/**
* @brief Retargets the C library printf function to the UART.
*
*/
PUTCHAR_PROTOTYPE
{
UART_SendData_8bit(DEBUG_UARTx, (uint8_t)ch);
while (UART_GetFlagStatus(DEBUG_UARTx, UART_FLAG_TXE) == RESET);
return ch;
}
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
size_t nChars = 0;
if (buffer == 0)
{
/*
* This means that we should flush internal buffers. Since we
* don't we just return. (Remember, "handle" == -1 means that all
* handles should be flushed.)
*/
return 0;
}
for (/* Empty */; size != 0; --size)
{
UART_SendData_8bit(DEBUG_UARTx, *buffer++);
while (UART_GetFlagStatus(DEBUG_UARTx, UART_FLAG_TXE) == RESET);
++nChars;
}
return nChars;
}
2.3、fun_uart.h
#ifndef __FUN_UART_H
#define __FUN_UART_H
#ifdef __cplusplus
extern "C"
{
#endif
//UARTx
#define DEBUG_UARTx CW_UART2
#define DEBUG_UART_CLK RCC_APB1_PERIPH_UART2
#define DEBUG_UART_APBClkENx RCC_APBPeriphClk_Enable1
#define DEBUG_UART_BaudRate 115200
#define DEBUG_UART_UclkFreq 48000000
//UARTx GPIO
#define DEBUG_UART_GPIO_CLK RCC_AHB_PERIPH_GPIOD|RCC_AHB_PERIPH_GPIOC
#define DEBUG_UART_TX_GPIO_PORT CW_GPIOC
#define DEBUG_UART_TX_GPIO_PIN GPIO_PIN_12
#define DEBUG_UART_RX_GPIO_PORT CW_GPIOD
#define DEBUG_UART_RX_GPIO_PIN GPIO_PIN_2
//GPIO AF
#define DEBUG_UART_AFTX PD02_AFx_UART2RXD()
#define DEBUG_UART_AFRX PC12_AFx_UART2TXD()
void init_uart(void);
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
/************************ (C) COPYRIGHT CW *****END OF FILE****/
2.4、main.c
int32_t main(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
rcc_config();
init_uart();
InitTick( 48000000 ); //初始化SysTick
__RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.IT = GPIO_IT_NONE;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pins = LED_GPIO_PINS;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
while (1)
{
GPIO_TogglePin(LED_GPIO_PORT, LED_GPIO_PINS);
// Delay(0xFFFF);
SysTickDelay(100);
printf("CW32L052 UART Printf Test!\r\n");
}
}
三、程序运行
下载程序,复位开发板,串口输出:
四、附件
|