[ModusToolbox™] 【英飞凌CYW20829测评】篇四 蓝牙之CANFD测试

[复制链接]
1876|0
 楼主| abner_ma 发表于 2024-7-22 20:42 | 显示全部楼层 |阅读模式
一、概述
CANFD简介:
  • CANFD是CAN 2.0的升级版,主要解决了传统CAN总线在传输速率和数据长度上的限制。
  • 它通过增加数据域的传输速率和长度,满足了现代汽车和工业应用中对高速、大数据量通信的需求。
CYW20829与CANFD:
  • CYW20829是英飞凌(Infineon)推出的一款高度集成的蓝牙LE 5.4 MCU,专为需要长距离和良好噪声免疫力的应用而设计。该MCU集成了CAN FD功能,使其能够支持高速、大数据量的通信需求。CYW20829支持一个CAN FD通道的CAN FD控制器。所有CAN FD控制器均符合ISO 11898-1:2015标准;同时提供ISO 16845:2015证书。此外,它还完全在硬件中实现了ISO 11898-4中指定的时间触发CAN(TTCAN)协议(TTCAN协议级别1和2)。与消息处理相关的所有功能均由RX(接收)和TX(发送)处理程序实现。RX处理程序管理消息的接受过滤,将从CAN核心接收到的消息传输到消息RAM,并提供接收消息的状态。TX处理程序负责将发送消息从消息RAM传输到CAN核心,并提供发送消息的状态。
  •    CYW20829还包含一个LIN通道。每个通道都支持根据ISO标准17987的LIN协议进行数据的发送/接收。每个LIN通道都通过一个3引脚接口(包括使能功能)连接到外部收发器,并支持主从功能。每个模块还支持经典和增强的校验和,以及在消息接收过程中的中断检测和唤醒信号。中断检测、同步字段、校验和计算以及错误中断都在硬件中处理。
    CYW20829中的CANFD(Controller Area Network with Flexible Data-Rate,可变速率的CAN)是该微控制器单元(MCU)的一个重要特性,它提供了比传统CAN总线更高的传输速度和数据长度。


二、技术特点
传输速率:
  • CAN FD的传输速率可达到8Mbps(数据比特率),远高于传统CAN总线的1Mbps(仲裁比特率)。
  • 这意味着CYW20829在数据传输速度上具有显著优势。
数据长度:
  • CAN FD支持的数据域长度最长可达64字节,而传统CAN总线的数据域长度最长为8字节。
  • 这使得CYW20829能够传输更多的数据,满足更复杂的应用需求。
帧格式:
  • CAN FD采用了可变速率的帧格式,即在控制场中的BRS(Bit Rate Switch)位之后,数据段的传输速率可以高于仲裁段的传输速率。
  • 这种设计使得CAN FD在保持仲裁段低速率以保证通信稳定性的同时,提高了数据段的传输速率。
三、应用场景
由于CYW20829集成了CAN FD功能,因此它非常适合于需要高速、大数据量通信的应用场景,如:
  • 智能家居:通过CAN FD实现设备间的快速、可靠通信。
  • 医疗健康:在医疗设备中传输大量医疗数据。
  • 工业自动化:在工业自动化系统中实现设备间的实时通信和数据交换。
  • 汽车电子:满足汽车内部各种元件之间的高速通信需求。
四、开发测试       Using Eclipse IDE for ModusToolbox™ software     引脚分配

  • code
    1. #include <stdio.h>
    2. #include <string.h>
    3. #include "cyhal.h"
    4. #include "cy_pdl.h"
    5. #include "cybsp.h"
    6. #include "cy_retarget_io.h"

    7. /*******************************************************************************
    8. * Macros
    9. *******************************************************************************/
    10. /* CAN-FD message identifier 1*/
    11. #define CANFD_NODE_1            1
    12. /* CAN-FD message identifier 2 (use different for 2nd device) */
    13. #define CANFD_NODE_2            2
    14. /* message Identifier used for this code */
    15. #define USE_CANFD_NODE          CANFD_NODE_1
    16. /* CAN-FD channel number used */
    17. #define CANFD_HW_CHANNEL        0
    18. /* CAN-FD data buffer index to send data from */
    19. #define CANFD_BUFFER_INDEX      0
    20. /* Maximum incoming data length supported */
    21. #define CANFD_DLC               8

    22. #define CANFD_INTERRUPT         canfd_0_interrupts0_0_IRQn

    23. #define GPIO_INTERRUPT_PRIORITY (7u)

    24. /*******************************************************************************
    25. * Global Variables
    26. *******************************************************************************/
    27. /* This is a shared context structure, unique for each can-fd channel */
    28. static cy_stc_canfd_context_t canfd_context;

    29. /* Variable which holds the button pressed status */
    30. volatile bool gpio_intr_flag = false;

    31. cyhal_gpio_callback_data_t gpio_btn_callback_data;

    32. /* Populate the configuration structure for CAN-FD Interrupt */
    33. cy_stc_sysint_t canfd_irq_cfg =
    34. {
    35.     /* Source of interrupt signal */
    36.     .intrSrc = CANFD_INTERRUPT,
    37.     /* Interrupt priority */
    38.     .intrPriority = 1U,
    39. };

    40. /*******************************************************************************
    41. * Function Prototypes
    42. *******************************************************************************/

    43. /* can-fd interrupt handler */
    44. static void isr_canfd (void);

    45. /* button press interrupt handler */
    46. static void gpio_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event);

    47. /* handler for general errors */
    48. void handle_error(uint32_t status);

    49. /*******************************************************************************
    50. * Function Definitions
    51. *******************************************************************************/

    52. /*******************************************************************************
    53. * Function Name: main
    54. ********************************************************************************
    55. * Summary:
    56. * This is the main function. It initializes the CAN-FD channel and interrupt.
    57. * User button and User LED are also initialized. The main loop checks for the
    58. * button pressed interrupt flag and when it is set, a CAN-FD frame is sent.
    59. * Whenever a CAN-FD frame is received from other nodes, the user LED toggles and
    60. * the received data is logged over serial terminal.
    61. *
    62. * Parameters:
    63. *  none
    64. *
    65. * Return:
    66. *  int
    67. *
    68. *******************************************************************************/
    69. int main(void)
    70. {
    71.     cy_rslt_t result;

    72.     cy_en_canfd_status_t status;
    73.     /* Initialize the device and board peripherals */
    74.     result = cybsp_init();
    75.     /* Board init failed. Stop program execution */
    76.     handle_error(result);
    77.     /* Initialize retarget-io for uart logging */
    78.     result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
    79.                                 CY_RETARGET_IO_BAUDRATE);
    80.     /* Retarget-io init failed. Stop program execution */
    81.     handle_error(result);

    82.     printf("===========================================================\r\n");
    83.     printf("Welcome to CAN-FD example\r\n");
    84.     printf("===========================================================\r\n\n");

    85.     printf("===========================================================\r\n");
    86.     printf("CAN-FD Node-%d (message id)\r\n", USE_CANFD_NODE);
    87.     printf("===========================================================\r\n\n");

    88.     /* Hook the interrupt service routine */
    89.     (void) Cy_SysInt_Init(&canfd_irq_cfg, &isr_canfd);
    90.     /* enable the CAN-FD interrupt */
    91.     NVIC_EnableIRQ(CANFD_INTERRUPT);

    92.     /* Initialize the user LED */
    93.     result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,
    94.                     CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
    95.     /* User LED init failed. Stop program execution */
    96.     handle_error(result);

    97.     /* Initialize the user button */
    98.     result = cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT,
    99.                     CYBSP_USER_BTN_DRIVE, CYBSP_BTN_OFF);
    100.     /* User button init failed. Stop program execution */
    101.     handle_error(result);

    102.     /* Configure GPIO interrupt */
    103.     gpio_btn_callback_data.callback = gpio_interrupt_handler;
    104.     cyhal_gpio_register_callback(CYBSP_USER_BTN,
    105.                                  &gpio_btn_callback_data);
    106.     cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL,
    107.                                      GPIO_INTERRUPT_PRIORITY, true);

    108.     /* Enable global interrupts */
    109.     __enable_irq();

    110.     /* Initialize CAN-FD Channel */
    111.     status = Cy_CANFD_Init(CANFD_HW, CANFD_HW_CHANNEL, &CANFD_config,
    112.                            &canfd_context);

    113.     handle_error(status);

    114.     /* Setting Node(message) Identifier to global setting of "USE_CANFD_NODE" */
    115.     CANFD_T0RegisterBuffer_0.id = USE_CANFD_NODE;

    116.     for(;;)
    117.     {
    118.         if (true == gpio_intr_flag)
    119.         {
    120.             /* Sending CAN-FD frame to other node */
    121.             status = Cy_CANFD_UpdateAndTransmitMsgBuffer(CANFD_HW,
    122.                                                     CANFD_HW_CHANNEL,
    123.                                                     &CANFD_txBuffer_0,
    124.                                                     CANFD_BUFFER_INDEX,
    125.                                                     &canfd_context);
    126.             if(CY_CANFD_SUCCESS == status)
    127.             {
    128.                 printf("CAN-FD Frame sent with message ID-%d\r\n\r\n",
    129.                         USE_CANFD_NODE);
    130.             }
    131.             else
    132.             {
    133.                 printf("Error sending CAN-FD Frame with message ID-%d\r\n\r\n",
    134.                         USE_CANFD_NODE);
    135.             }

    136.             gpio_intr_flag = false;
    137.         }
    138.     }
    139. }
    140. void canfd_rx_callback (bool  msg_valid, uint8_t msg_buf_fifo_num,
    141.                         cy_stc_canfd_rx_buffer_t* canfd_rx_buf)
    142. {
    143.     /* Array to hold the data bytes of the CAN-FD frame */
    144.     uint8_t canfd_data_buffer[CANFD_DLC];
    145.     /* Variable to hold the data length code of the CAN-FD frame */
    146.     uint32_t canfd_dlc;
    147.     /* Variable to hold the Identifier of the CAN-FD frame */
    148.     uint32_t canfd_id;

    149.     if (true == msg_valid)
    150.     {
    151.         /* Checking whether the frame received is a data frame */
    152.         if(CY_CANFD_RTR_DATA_FRAME == canfd_rx_buf->r0_f->rtr)
    153.         {

    154.             cyhal_gpio_toggle(CYBSP_USER_LED);

    155.             canfd_dlc = canfd_rx_buf->r1_f->dlc;
    156.             canfd_id  = canfd_rx_buf->r0_f->id;

    157.             printf("%d bytes received with message identifier %d\r\n\r\n",
    158.                                                         (int)canfd_dlc,
    159.                                                         (int)canfd_id);

    160.             memcpy(canfd_data_buffer,canfd_rx_buf->data_area_f,canfd_dlc);

    161.             printf("Rx Data : ");

    162.             for (uint8_t msg_idx = 0U; msg_idx < canfd_dlc ; msg_idx++)
    163.             {
    164.                 printf(" %d ", canfd_data_buffer[msg_idx]);
    165.             }

    166.             printf("\r\n\r\n");
    167.         }
    168.     }
    169. }





本帖子中包含更多资源

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

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

本版积分规则

认证:项目经理
简介:资深嵌入式开发工程师

104

主题

191

帖子

3

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