[PSOC™] CY8CKIT-041S-MAX 如何使用printf?

[复制链接]
 楼主| IFX_Lingling 发表于 2024-4-2 17:42 | 显示全部楼层 |阅读模式
1:
通过retarget-io实现printf的功能:

可以参考历程:


2:如果不用retarget-io,可以参考下面基于GCC编译器的实现代码。

  1. /******************************************************************************
  2. * File Name:   main.c
  3. *
  4. * Description: This code avoids the usage of retarget-io and prints UART
  5. *              messages using printf() function.
  6. *
  7. ******************************************************************************/

  8. #include "cy_pdl.h"
  9. #include "cybsp.h"

  10. cy_stc_scb_uart_context_t CYBSP_DEBUG_UART_context;

  11. void handle_error(void)
  12. {
  13.      /* Disable all interrupts. */
  14.     __disable_irq();

  15.     CY_ASSERT(0);
  16. }

  17. int main(void)
  18. {
  19.     cy_rslt_t result;
  20.     cy_en_scb_uart_status_t initstatus;
  21.     uint8_t read_data; /* Variable to store the received character
  22.                         * through terminal */

  23.     /* Initialize the device and board peripherals */
  24.     result = cybsp_init();
  25.     if (result != CY_RSLT_SUCCESS)
  26.     {
  27.         handle_error();
  28.     }

  29.     /* Enable global interrupts */
  30.     __enable_irq();

  31.     /* Initialize the UART */
  32.     initstatus = Cy_SCB_UART_Init(CYBSP_DEBUG_UART_HW, &CYBSP_DEBUG_UART_config, &CYBSP_DEBUG_UART_context);

  33.         /* Initialization failed. Handle error */
  34.         if(initstatus!=CY_SCB_UART_SUCCESS)
  35.         {
  36.                 handle_error();
  37.         }

  38.         /* Enable UART */
  39.         Cy_SCB_UART_Enable(CYBSP_DEBUG_UART_HW);

  40.         /* Transmit Header to the Terminal */
  41.     printf("\x1b[2J\x1b[;H");
  42.     printf("***********************************************************\r\n");
  43.     printf("PSoC MCU UART Transmit and Receive\r\n");
  44.     printf("***********************************************************\r\n\n");
  45.     printf(">> Start typing to see the echo on the screen \r\n\n");

  46.     for (;;)
  47.     {

  48.             /* Check if there is a received character from user console */
  49.                 if (0UL != Cy_SCB_UART_GetNumInRxFifo(CYBSP_DEBUG_UART_HW))
  50.                 {
  51.                         /* Re-transmit whatever the user types on the console */
  52.                         read_data = Cy_SCB_UART_Get(CYBSP_DEBUG_UART_HW);
  53.                         printf("%c\r\n",read_data);

  54.                 }
  55.     }
  56. }

  57. /*******************************************************************************
  58. * When printf function is called it is redirected to the following functions
  59. * depending on compiler used.
  60. *******************************************************************************/
  61. #if defined (__GNUC__)
  62. /*******************************************************************************
  63. * Function Name: _write
  64. ********************************************************************************
  65. * Summary:
  66. * NewLib C library is used to retarget printf to _write. printf is redirected to
  67. * this function when GCC compiler is used to print data to terminal using UART.
  68. *
  69. * \param file
  70. * This variable is not used.
  71. * \param *ptr
  72. * Pointer to the data which will be transfered through UART.
  73. * \param len
  74. * Length of the data to be transfered through UART.
  75. *
  76. * \return
  77. * returns the number of characters transferred using UART.
  78. * \ref int
  79. *******************************************************************************/
  80.    int _write(int file, char *ptr, int len)
  81.     {
  82.         /* Suppress the compiler warning about an unused variable. */
  83.         if (0 != file)
  84.         {
  85.         }

  86.         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的功能很简单有效
hilahope 发表于 2024-5-1 20:53 | 显示全部楼层
在项目中,你需要配置UART(通用异步收发器)以实现串行通信。在PSoC Creator中,打开“设计工具箱”,找到“通信”部分,将UART组件拖到你的设计中。然后,将UART的TX(发送)引脚连接到开发板的TX引脚,将RX(接收)引脚连接到开发板的RX引脚。
iyoum 发表于 2024-5-2 15:58 | 显示全部楼层
可以通过修改应用程序的启动文件(如cy_boot.c)来实现这一点,将puts和puts_P函数指向UART的发送函数。
hearstnorman323 发表于 2024-5-4 12:49 | 显示全部楼层
开发板应该能够通过 UART 接口输出你编写的信息。你需要使用逻辑分析仪或串行通信工具来观察 UART 输出。
abotomson 发表于 2024-5-6 08:16 | 显示全部楼层
使用 printf 函数前,  UART 已经初始化并可以正常工作。
juliestephen 发表于 2024-5-6 11:21 | 显示全部楼层
首先包含了所需的头文件,然后定义了一个main函数。在main函数中,我们调用了UART_Start()函数来初始化UART,然后使用printf函数输出一条消息。
sanfuzi 发表于 2024-5-6 14:33 | 显示全部楼层
在配置完串口和重定向后,编译你的代码,并将固件烧录到 CY8CKIT-041S-MAX 开发板上。
bartonalfred 发表于 2024-5-6 18:03 | 显示全部楼层
需要配置一个 UART 组件,设置好相应的波特率、数据位、停止位和校验位。
timfordlare 发表于 2024-5-6 21:33 | 显示全部楼层
提供的标准 C 库,这些库中应该包含了 printf 函数的实现。
earlmax 发表于 2024-5-7 08:40 | 显示全部楼层
需要将标准输出重定向到UART接口。
febgxu 发表于 2024-5-7 12:21 | 显示全部楼层
重定向 stdout               
jackcat 发表于 2024-5-7 15:31 | 显示全部楼层
打开相应的串口。设置波特率、数据位、停止位和奇偶校验位等参数,然后打开串口。此时,你应该能看到“Hello, World!”字符串在串口上周期性地输出。
deliahouse887 发表于 2024-5-7 18:49 | 显示全部楼层
在嵌入式系统中没有标准的显示器连接
jkl21 发表于 2024-5-7 22:00 | 显示全部楼层
配置 UART 的波特率、数据位、停止位和校验位, 与你的调试工具或终端仿真器匹配。
10299823 发表于 2024-5-8 13:05 | 显示全部楼层
使用printf会增加程序的Flash/RAM空间消耗。
gygp 发表于 2024-5-8 16:45 | 显示全部楼层
使 printf 输出到 UART,你需要重定向 _write 函数
chenqianqian 发表于 2024-5-8 20:44 来自手机 | 显示全部楼层
可以用格式化字符串函数来实现
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

120

主题

208

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部