CY8CKIT-041S-MAX 如何使用printf?
1:通过retarget-io实现printf的功能:
可以参考历程:
2:如果不用retarget-io,可以参考下面基于GCC编译器的实现代码。
/******************************************************************************
* File Name: main.c
*
* Description: This code avoids the usage of retarget-io and prints UART
* messages using printf() function.
*
******************************************************************************/
#include "cy_pdl.h"
#include "cybsp.h"
cy_stc_scb_uart_context_t CYBSP_DEBUG_UART_context;
void handle_error(void)
{
/* Disable all interrupts. */
__disable_irq();
CY_ASSERT(0);
}
int main(void)
{
cy_rslt_t result;
cy_en_scb_uart_status_t initstatus;
uint8_t read_data; /* Variable to store the received character
* through terminal */
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
handle_error();
}
/* Enable global interrupts */
__enable_irq();
/* Initialize the UART */
initstatus = Cy_SCB_UART_Init(CYBSP_DEBUG_UART_HW, &CYBSP_DEBUG_UART_config, &CYBSP_DEBUG_UART_context);
/* Initialization failed. Handle error */
if(initstatus!=CY_SCB_UART_SUCCESS)
{
handle_error();
}
/* Enable UART */
Cy_SCB_UART_Enable(CYBSP_DEBUG_UART_HW);
/* Transmit Header to the Terminal */
printf("\x1b[2J\x1b[;H");
printf("***********************************************************\r\n");
printf("PSoC MCU UART Transmit and Receive\r\n");
printf("***********************************************************\r\n\n");
printf(">> Start typing to see the echo on the screen \r\n\n");
for (;;)
{
/* Check if there is a received character from user console */
if (0UL != Cy_SCB_UART_GetNumInRxFifo(CYBSP_DEBUG_UART_HW))
{
/* Re-transmit whatever the user types on the console */
read_data = Cy_SCB_UART_Get(CYBSP_DEBUG_UART_HW);
printf("%c\r\n",read_data);
}
}
}
/*******************************************************************************
* When printf function is called it is redirected to the following functions
* depending on compiler used.
*******************************************************************************/
#if defined (__GNUC__)
/*******************************************************************************
* Function Name: _write
********************************************************************************
* Summary:
* NewLib C library is used to retarget printf to _write. printf is redirected to
* this function when GCC compiler is used to print data to terminal using UART.
*
* \param file
* This variable is not used.
* \param *ptr
* Pointer to the data which will be transfered through UART.
* \param len
* Length of the data to be transfered through UART.
*
* \return
* returns the number of characters transferred using UART.
* \ref int
*******************************************************************************/
int _write(int file, char *ptr, int len)
{
/* Suppress the compiler warning about an unused variable. */
if (0 != file)
{
}
for(uint8_t i = 0; i
通过retarget-io的方法消耗的Flash/RAM空间要大一点,所以可以结合自己的需要选择适合的方法。
可以直接使用C语言标准库函数中的格式化字符串 应该也是重新定义的prift这个函数吧 retarget-io实现printf的功能很简单有效 在项目中,你需要配置UART(通用异步收发器)以实现串行通信。在PSoC Creator中,打开“设计工具箱”,找到“通信”部分,将UART组件拖到你的设计中。然后,将UART的TX(发送)引脚连接到开发板的TX引脚,将RX(接收)引脚连接到开发板的RX引脚。 可以通过修改应用程序的启动文件(如cy_boot.c)来实现这一点,将puts和puts_P函数指向UART的发送函数。 开发板应该能够通过 UART 接口输出你编写的信息。你需要使用逻辑分析仪或串行通信工具来观察 UART 输出。 使用 printf 函数前,UART 已经初始化并可以正常工作。 首先包含了所需的头文件,然后定义了一个main函数。在main函数中,我们调用了UART_Start()函数来初始化UART,然后使用printf函数输出一条消息。 在配置完串口和重定向后,编译你的代码,并将固件烧录到 CY8CKIT-041S-MAX 开发板上。 需要配置一个 UART 组件,设置好相应的波特率、数据位、停止位和校验位。 提供的标准 C 库,这些库中应该包含了 printf 函数的实现。 需要将标准输出重定向到UART接口。 重定向 stdout 打开相应的串口。设置波特率、数据位、停止位和奇偶校验位等参数,然后打开串口。此时,你应该能看到“Hello, World!”字符串在串口上周期性地输出。 在嵌入式系统中没有标准的显示器连接 配置 UART 的波特率、数据位、停止位和校验位, 与你的调试工具或终端仿真器匹配。
使用printf会增加程序的Flash/RAM空间消耗。 使 printf 输出到 UART,你需要重定向 _write 函数 可以用格式化字符串函数来实现
页:
[1]