[AIROC™ 蓝牙] 【英飞凌CYW20829测评】第3篇 串口

[复制链接]
 楼主| 小宏121 发表于 2024-7-16 23:56 | 显示全部楼层 |阅读模式
<
本帖最后由 小宏121 于 2024-7-17 23:39 编辑

复制官方的例程“UART_Transmit_and_Receive”到自己的工程目录下,并且修改为my_uart_test


点击添加新的工程,选择工程所在的目录


等待一段时间,让MTB构建好工程


修改代码,让MCU接收到字符串 "Hello, CYW920829." 后会返回 "Hi, my friend!"

  1. #include "cyhal.h"
  2. #include "cybsp.h"
  3. #include "cy_retarget_io.h"
  4. #include <string.h>

  5. /*******************************************************************************
  6. * Function Name: handle_error
  7. ********************************************************************************
  8. * Summary:
  9. * User defined error handling function.
  10. *
  11. * Parameters:
  12. *  void
  13. *
  14. * Return:
  15. *  void
  16. *
  17. *******************************************************************************/
  18. void handle_error(void)
  19. {
  20.      /* Disable all interrupts. */
  21.     __disable_irq();

  22.     CY_ASSERT(0);
  23. }

  24. /*******************************************************************************
  25. * Function Name: main
  26. ********************************************************************************
  27. * Summary:
  28. * This is the main function.
  29. * Reads one byte from the serial terminal and echoes back the read byte.
  30. *
  31. * Parameters:
  32. *  void
  33. *
  34. * Return:
  35. *  int
  36. *
  37. *******************************************************************************/
  38. int main(void)
  39. {
  40.     cy_rslt_t result;
  41.     #if defined(CY_DEVICE_SECURE)
  42.         cyhal_wdt_t wdt_obj;
  43.         /* Clear watchdog timer so that it doesn't trigger a reset */
  44.         result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
  45.         CY_ASSERT(CY_RSLT_SUCCESS == result);
  46.         cyhal_wdt_free(&wdt_obj);
  47.     #endif

  48.     uint8_t read_data; /* Variable to store the received character
  49.                         * through terminal */
  50.     uint8_t str_buff[50] = {0};
  51.     char * cmp_str = "Hello, CYW920829.";
  52.     char * my_ack = "Hi, my friend!";
  53.     uint8_t index = 0;
  54.     uint8_t start_recv = 1;

  55.     /* Initialize the device and board peripherals */
  56.     result = cybsp_init();
  57.     if (result != CY_RSLT_SUCCESS)
  58.     {
  59.         handle_error();
  60.     }

  61.     /* Initialize retarget-io to use the debug UART port */

  62.     result = cy_retarget_io_init_fc(CYBSP_DEBUG_UART_TX,
  63.                                     CYBSP_DEBUG_UART_RX,
  64.                                     CYBSP_DEBUG_UART_CTS,
  65.                                     CYBSP_DEBUG_UART_RTS,
  66.                                     CY_RETARGET_IO_BAUDRATE);

  67.     if (result != CY_RSLT_SUCCESS)
  68.     {
  69.         handle_error();
  70.     }

  71.     /* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
  72.     printf("\x1b[2J\x1b[;H");

  73.     printf("***********************************************************\r\n");
  74.     printf("HAL: UART Transmit and Receive\r\n");
  75.     printf("***********************************************************\r\n\n");
  76.     printf(">> Start typing to see the echo on the screen \r\n\n");

  77.     __enable_irq();

  78.     for (;;)
  79.     {
  80.         if (CY_RSLT_SUCCESS == cyhal_uart_getc(&cy_retarget_io_uart_obj,
  81.                                                &read_data, 0))
  82.         {
  83.             if (read_data == cmp_str[0])
  84.             {
  85.                 index = 0;
  86.                 start_recv = 0;
  87.                 memset(str_buff, 0, sizeof(str_buff));
  88.             }


  89.             if (start_recv == 0)
  90.             {

  91.                 str_buff[index++] = read_data;
  92.                 if (index == strlen(cmp_str))
  93.                 {
  94.                     start_recv = 1;
  95.                     if(0 == strcmp((char *)str_buff, cmp_str))
  96.                     {
  97.                         for (uint8_t i=0; i < (strlen(my_ack) - 1); ++i)
  98.                         {
  99.                             cyhal_uart_putc(&cy_retarget_io_uart_obj, my_ack[i]);
  100.                         }
  101.                         printf("\r\n");
  102.                     }
  103.                     else
  104.                     {
  105.                         printf("request err\r\n");
  106.                     }
  107.                 }
  108.             }

  109.         }
  110.         else
  111.         {
  112.             handle_error();
  113.         }
  114.     }
  115. }

测试效果如下:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
suncat0504 发表于 2024-7-29 21:25 | 显示全部楼层
MTB是英飞凌的开发工具吗?操作方便不?
shenxiaolin 发表于 2024-7-29 21:48 | 显示全部楼层
感谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

21

帖子

0

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

6

主题

21

帖子

0

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