打印
[PSoC™]

CY8CKIT-041S-MAX 如何使用printf?

[复制链接]
742|19
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
IFX_Lingling|  楼主 | 2024-4-2 17:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1:
通过retarget-io实现printf的功能:

可以参考历程:
mtb-example-psoc4-safety-uart-loopback-test-master.zip (29.45 KB)

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空间要大一点,所以可以结合自己的需要选择适合的方法。


使用特权

评论回复
沙发
caigang13| | 2024-4-2 20:25 | 只看该作者
可以直接使用C语言标准库函数中的格式化字符串

使用特权

评论回复
板凳
小夏天的大西瓜| | 2024-4-22 21:56 | 只看该作者
应该也是重新定义的prift这个函数吧

使用特权

评论回复
地板
中国龙芯CDX| | 2024-4-23 15:31 | 只看该作者
retarget-io实现printf的功能很简单有效

使用特权

评论回复
5
hilahope| | 2024-5-1 20:53 | 只看该作者
在项目中,你需要配置UART(通用异步收发器)以实现串行通信。在PSoC Creator中,打开“设计工具箱”,找到“通信”部分,将UART组件拖到你的设计中。然后,将UART的TX(发送)引脚连接到开发板的TX引脚,将RX(接收)引脚连接到开发板的RX引脚。

使用特权

评论回复
6
iyoum| | 2024-5-2 15:58 | 只看该作者
可以通过修改应用程序的启动文件(如cy_boot.c)来实现这一点,将puts和puts_P函数指向UART的发送函数。

使用特权

评论回复
7
hearstnorman323| | 2024-5-4 12:49 | 只看该作者
开发板应该能够通过 UART 接口输出你编写的信息。你需要使用逻辑分析仪或串行通信工具来观察 UART 输出。

使用特权

评论回复
8
abotomson| | 2024-5-6 08:16 | 只看该作者
使用 printf 函数前,  UART 已经初始化并可以正常工作。

使用特权

评论回复
9
juliestephen| | 2024-5-6 11:21 | 只看该作者
首先包含了所需的头文件,然后定义了一个main函数。在main函数中,我们调用了UART_Start()函数来初始化UART,然后使用printf函数输出一条消息。

使用特权

评论回复
10
sanfuzi| | 2024-5-6 14:33 | 只看该作者
在配置完串口和重定向后,编译你的代码,并将固件烧录到 CY8CKIT-041S-MAX 开发板上。

使用特权

评论回复
11
bartonalfred| | 2024-5-6 18:03 | 只看该作者
需要配置一个 UART 组件,设置好相应的波特率、数据位、停止位和校验位。

使用特权

评论回复
12
timfordlare| | 2024-5-6 21:33 | 只看该作者
提供的标准 C 库,这些库中应该包含了 printf 函数的实现。

使用特权

评论回复
13
earlmax| | 2024-5-7 08:40 | 只看该作者
需要将标准输出重定向到UART接口。

使用特权

评论回复
14
febgxu| | 2024-5-7 12:21 | 只看该作者
重定向 stdout               

使用特权

评论回复
15
jackcat| | 2024-5-7 15:31 | 只看该作者
打开相应的串口。设置波特率、数据位、停止位和奇偶校验位等参数,然后打开串口。此时,你应该能看到“Hello, World!”字符串在串口上周期性地输出。

使用特权

评论回复
16
deliahouse887| | 2024-5-7 18:49 | 只看该作者
在嵌入式系统中没有标准的显示器连接

使用特权

评论回复
17
jkl21| | 2024-5-7 22:00 | 只看该作者
配置 UART 的波特率、数据位、停止位和校验位, 与你的调试工具或终端仿真器匹配。

使用特权

评论回复
18
10299823| | 2024-5-8 13:05 | 只看该作者
使用printf会增加程序的Flash/RAM空间消耗。

使用特权

评论回复
19
gygp| | 2024-5-8 16:45 | 只看该作者
使 printf 输出到 UART,你需要重定向 _write 函数

使用特权

评论回复
20
chenqianqian| | 2024-5-8 20:44 | 只看该作者
可以用格式化字符串函数来实现

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:
简介:关于英飞凌——我们致力于打造一个更加便利、安全和环保的世界,在赢得自身成功发展的同时,积极践行企业社会责任。

77

主题

155

帖子

4

粉丝