1014|1

226

帖子

2

TA的资源

纯净的硅(中级)

 

【CW32L052测评】 printf输出测试 [复制链接]

  本帖最后由 TL-LED 于 2023-7-18 23:30 编辑

这篇来测试下printf输出。

 

一、硬件电路

测试串口输出使用到开发板上的USB转串口。

 

1.1、电路图部分

005.png

 

1.2、引脚功能

使用到PD02和PC12端口,看下手册中这两个引脚的定义,对应的串口2。

006.png

 

1.3、时钟配置

时钟框图

UART2使用PCLK时钟。

007.png

 

二、程序部分

 

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");
    }
}

 

三、程序运行

下载程序,复位开发板,串口输出:

008.png

四、附件

源程序: cw32l052_prj_20230718.rar (659.94 KB, 下载次数: 0)

最新回复

下载程序,复位开发板,串口输出整的 串口助手测试正确ok     详情 回复 发表于 2023-7-19 23:03
 
 
 

回复

1524

帖子

0

TA的资源

五彩晶圆(初级)

 

下载程序,复位开发板,串口输出整的

串口助手测试正确ok

 

 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2023 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表