[AIROC™ 蓝牙] 【英飞凌CYW20829测评】+ Hello 21ic, Hello Infineon CYW20829!"

[复制链接]
602|0
 楼主| lishuihua 发表于 2024-7-21 17:41 | 显示全部楼层 |阅读模式
任务1:建立开发调试环境。选择自己熟悉的编译器,完成代码的编译,下载与调试功能。成果展示:以视频或截图的方式,通过调试串口打印下列字符串“Hello 21ic, Hello Infineon CYW20829!"


参考例程:Hello World


实物效果:



核心代码:
  1. /******************************************************************************
  2. * File Name:   main.c
  3. *
  4. * Description: This is the source code for Hello World Example using HAL APIs.
  5. *
  6. * Related Document: See README.md
  7. *
  8. *
  9. *******************************************************************************
  10. * Copyright 2022-2024, Cypress Semiconductor Corporation (an Infineon company) or
  11. * an affiliate of Cypress Semiconductor Corporation.  All rights reserved.
  12. *
  13. * This software, including source code, documentation and related
  14. * materials ("Software") is owned by Cypress Semiconductor Corporation
  15. * or one of its affiliates ("Cypress") and is protected by and subject to
  16. * worldwide patent protection (United States and foreign),
  17. * United States copyright laws and international treaty provisions.
  18. * Therefore, you may use this Software only as provided in the license
  19. * agreement accompanying the software package from which you
  20. * obtained this Software ("EULA").
  21. * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
  22. * non-transferable license to copy, modify, and compile the Software
  23. * source code solely for use in connection with Cypress's
  24. * integrated circuit products.  Any reproduction, modification, translation,
  25. * compilation, or representation of this Software except as specified
  26. * above is prohibited without the express written permission of Cypress.
  27. *
  28. * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
  31. * reserves the right to make changes to the Software without notice. Cypress
  32. * does not assume any liability arising out of the application or use of the
  33. * Software or any product or circuit described in the Software. Cypress does
  34. * not authorize its products for use in any products where a malfunction or
  35. * failure of the Cypress product may reasonably be expected to result in
  36. * significant property damage, injury or death ("High Risk Product"). By
  37. * including Cypress's product in a High Risk Product, the manufacturer
  38. * of such system or application assumes all risk of such use and in doing
  39. * so agrees to indemnify Cypress against all liability.
  40. *******************************************************************************/

  41. #include "cyhal.h"
  42. #include "cybsp.h"
  43. #include "cy_retarget_io.h"


  44. /*******************************************************************************
  45. * Macros
  46. *******************************************************************************/
  47. /* LED blink timer clock value in Hz  */
  48. #define LED_BLINK_TIMER_CLOCK_HZ          (10000)

  49. /* LED blink timer period value */
  50. #define LED_BLINK_TIMER_PERIOD            (9999)


  51. /*******************************************************************************
  52. * Global Variables
  53. *******************************************************************************/
  54. bool timer_interrupt_flag = false;
  55. bool led_blink_active_flag = true;

  56. /* Variable for storing character read from terminal */
  57. uint8_t uart_read_value;

  58. /* Timer object used for blinking the LED */
  59. cyhal_timer_t led_blink_timer;


  60. /*******************************************************************************
  61. * Function Prototypes
  62. *******************************************************************************/
  63. void timer_init(void);
  64. static void isr_timer(void *callback_arg, cyhal_timer_event_t event);

  65. /*******************************************************************************
  66. * Function Name: main
  67. ********************************************************************************
  68. * Summary:
  69. * This is the main function. It sets up a timer to trigger a periodic interrupt.
  70. * The main while loop checks for the status of a flag set by the interrupt and
  71. * toggles an LED at 1Hz to create an LED blinky. Will be achieving the 1Hz Blink
  72. * rate based on the The LED_BLINK_TIMER_CLOCK_HZ and LED_BLINK_TIMER_PERIOD
  73. * Macros,i.e. (LED_BLINK_TIMER_PERIOD + 1) / LED_BLINK_TIMER_CLOCK_HZ = X ,Here,
  74. * X denotes the desired blink rate. The while loop also checks whether the
  75. * 'Enter' key was pressed and stops/restarts LED blinking.
  76. *
  77. * Parameters:
  78. *  none
  79. *
  80. * Return:
  81. *  int
  82. *
  83. *******************************************************************************/
  84. int main(void)
  85. {
  86.     cy_rslt_t result;

  87. #if defined (CY_DEVICE_SECURE)
  88.     cyhal_wdt_t wdt_obj;

  89.     /* Clear watchdog timer so that it doesn't trigger a reset */
  90.     result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
  91.     CY_ASSERT(CY_RSLT_SUCCESS == result);
  92.     cyhal_wdt_free(&wdt_obj);
  93. #endif /* #if defined (CY_DEVICE_SECURE) */

  94.     /* Initialize the device and board peripherals */
  95.     result = cybsp_init();

  96.     /* Board init failed. Stop program execution */
  97.     if (result != CY_RSLT_SUCCESS)
  98.     {
  99.         CY_ASSERT(0);
  100.     }

  101.     /* Enable global interrupts */
  102.     __enable_irq();

  103.     /* Initialize retarget-io to use the debug UART port */
  104.     result = cy_retarget_io_init_fc(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
  105.             CYBSP_DEBUG_UART_CTS,CYBSP_DEBUG_UART_RTS,CY_RETARGET_IO_BAUDRATE);

  106.     /* retarget-io init failed. Stop program execution */
  107.     if (result != CY_RSLT_SUCCESS)
  108.     {
  109.         CY_ASSERT(0);
  110.     }

  111.     /* Initialize the User LED */
  112.     result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,
  113.                              CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);

  114.     /* GPIO init failed. Stop program execution */
  115.     if (result != CY_RSLT_SUCCESS)
  116.     {
  117.         CY_ASSERT(0);
  118.     }

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

  121.     printf("****************** "
  122.            "HAL: Hello World! Example "
  123.            "****************** \r\n\n");

  124.     printf("Hello World!!!\r\n\n");
  125.     printf("For more projects, "
  126.            "visit our code examples repositories:\r\n\n");

  127.     printf("https://github.com/Infineon/"
  128.            "Code-Examples-for-ModusToolbox-Software\r\n\n");

  129.     /* Initialize timer to toggle the LED */
  130.     timer_init();

  131.     printf("Press 'Enter' key to pause or "
  132.            "resume blinking the user LED \r\n\r\n");

  133.     for (;;)
  134.     {
  135.         /* Check if 'Enter' key was pressed */
  136.         if (cyhal_uart_getc(&cy_retarget_io_uart_obj, &uart_read_value, 1)
  137.              == CY_RSLT_SUCCESS)
  138.         {
  139.             if (uart_read_value == '\r')
  140.             {
  141.                 /* Pause LED blinking by stopping the timer */
  142.                 if (led_blink_active_flag)
  143.                 {
  144.                     cyhal_timer_stop(&led_blink_timer);

  145.                     printf("LED blinking paused \r\n");
  146.                 }
  147.                 else /* Resume LED blinking by starting the timer */
  148.                 {
  149.                     cyhal_timer_start(&led_blink_timer);

  150.                     printf("LED blinking resumed\r\n");
  151.                 }

  152.                 /* Move cursor to previous line */
  153.                 printf("\x1b[1F");

  154.                 led_blink_active_flag ^= 1;
  155.             }else  if(uart_read_value == ' '){
  156.                     printf("Hello 21ic, Hello Infineon CYW20829!\r\n");
  157.             }
  158.         }
  159.         /* Check if timer elapsed (interrupt fired) and toggle the LED */
  160.         if (timer_interrupt_flag)
  161.         {
  162.             /* Clear the flag */
  163.             timer_interrupt_flag = false;

  164.             /* Invert the USER LED state */
  165.             cyhal_gpio_toggle(CYBSP_USER_LED);
  166.         }
  167.     }
  168. }


  169. /*******************************************************************************
  170. * Function Name: timer_init
  171. ********************************************************************************
  172. * Summary:
  173. * This function creates and configures a Timer object. The timer ticks
  174. * continuously and produces a periodic interrupt on every terminal count
  175. * event. The period is defined by the 'period' and 'compare_value' of the
  176. * timer configuration structure 'led_blink_timer_cfg'. Without any changes,
  177. * this application is designed to produce an interrupt every 1 second.
  178. *
  179. * Parameters:
  180. *  none
  181. *
  182. * Return :
  183. *  void
  184. *
  185. *******************************************************************************/
  186. void timer_init(void)
  187. {
  188.     cy_rslt_t result;

  189.     const cyhal_timer_cfg_t led_blink_timer_cfg =
  190.     {
  191.         .compare_value = 0,                 /* Timer compare value, not used */
  192.         .period = LED_BLINK_TIMER_PERIOD,   /* Defines the timer period */
  193.         .direction = CYHAL_TIMER_DIR_UP,    /* Timer counts up */
  194.         .is_compare = false,                /* Don't use compare mode */
  195.         .is_continuous = true,              /* Run timer indefinitely */
  196.         .value = 0                          /* Initial value of counter */
  197.     };

  198.     /* Initialize the timer object. Does not use input pin ('pin' is NC) and
  199.      * does not use a pre-configured clock source ('clk' is NULL). */
  200.     result = cyhal_timer_init(&led_blink_timer, NC, NULL);

  201.     /* timer init failed. Stop program execution */
  202.     if (result != CY_RSLT_SUCCESS)
  203.     {
  204.         CY_ASSERT(0);
  205.     }

  206.     /* Configure timer period and operation mode such as count direction,
  207.        duration */
  208.     cyhal_timer_configure(&led_blink_timer, &led_blink_timer_cfg);

  209.     /* Set the frequency of timer's clock source */
  210.     cyhal_timer_set_frequency(&led_blink_timer, LED_BLINK_TIMER_CLOCK_HZ);

  211.     /* Assign the ISR to execute on timer interrupt */
  212.     cyhal_timer_register_callback(&led_blink_timer, isr_timer, NULL);

  213.     /* Set the event on which timer interrupt occurs and enable it */
  214.     cyhal_timer_enable_event(&led_blink_timer, CYHAL_TIMER_IRQ_TERMINAL_COUNT,
  215.                               7, true);

  216.     /* Start the timer with the configured settings */
  217.     cyhal_timer_start(&led_blink_timer);
  218. }


  219. /*******************************************************************************
  220. * Function Name: isr_timer
  221. ********************************************************************************
  222. * Summary:
  223. * This is the interrupt handler function for the timer interrupt.
  224. *
  225. * Parameters:
  226. *    callback_arg    Arguments passed to the interrupt callback
  227. *    event            Timer/counter interrupt triggers
  228. *
  229. * Return:
  230. *  void
  231. *******************************************************************************/
  232. static void isr_timer(void *callback_arg, cyhal_timer_event_t event)
  233. {
  234.     (void) callback_arg;
  235.     (void) event;

  236.     /* Set the interrupt flag and process it from the main while(1) loop */
  237.     timer_interrupt_flag = true;
  238. }

  239. /* [] END OF FILE */


本帖子中包含更多资源

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

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

本版积分规则

9

主题

31

帖子

0

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