赞
踩
本文摘要:本文章介绍如何使用NXP官方软件S32KDS中的flexcan组件(RxFIFO+DMA)
开发平台:S32 Design Studio for ARM Version 2.2
SDK版本:S32_SDK_S32K1xx_RTM_3.0.0
使用芯片:S32K148
注:本文讲述CAN使用Rx FIFO功能DMA方式实现无过滤CAN报文接收,中断方式见这篇文章!
S32K的flexcan组件使用(RxFIFO+中断)_我是阿衰的博客-CSDN博客
组件例程免费下载方式在文末!!!
- // 自定义数据缓存结构体
- typedef struct {
- uint16_t CAN_ID;
- uint8_t CAN_DATA[8];
- }CANDataStruct;
- // CAN接收数据结构体
- CANDataStruct CANrecMsg1;
- CANDataStruct CANrecMsg2;
- // 配置RxFIFO接收必须使用该结构体声明的变量
- static flexcan_msgbuff_t recvMsg1;
- static flexcan_msgbuff_t recvMsg2;
- // DMA初始化 放在CAN初始化之前
- void DMA_Init(void)
- {
- EDMA_DRV_Init(&dmaController1_State, &dmaController1_InitConfig0, edmaChnStateArray, edmaChnConfigArray, EDMA_CONFIGURED_CHANNELS_COUNT);
- }
- // CAN0初始化
- void CAN0_Init(void)
- {
- FLEXCAN_DRV_Init(INST_CANCOM1, &canCom1_State, &canCom1_InitConfig0);
- FLEXCAN_DRV_SetRxFifoGlobalMask(INST_CANCOM1, FLEXCAN_RX_FIFO_ID_FORMAT_A, 0);
- FLEXCAN_DRV_InstallEventCallback(INST_CANCOM1, canRxCallback, NULL);
- // 开启RxFIFO接收 之后在接收中断函数中加入此函数实现不间断接收
- FLEXCAN_DRV_RxFifo(INST_CANCOM1, &recvMsg1);
- }
- // CAN1初始化
- void CAN1_Init(void)
- {
- FLEXCAN_DRV_Init(INST_CANCOM2, &canCom2_State, &canCom2_InitConfig0);
- FLEXCAN_DRV_SetRxFifoGlobalMask(INST_CANCOM2, FLEXCAN_RX_FIFO_ID_FORMAT_A, 0);
- FLEXCAN_DRV_InstallEventCallback(INST_CANCOM2, canRxCallback, NULL);
- FLEXCAN_DRV_RxFifo(INST_CANCOM2, &recvMsg2);
- }
- // CAN接收中断回调函数
- void canRxCallback(uint8_t instance, flexcan_event_type_t eventType,
- uint32_t buffIdx, flexcan_state_t *flexcanState)
- {
- // DMA接收完成
- if(eventType == FLEXCAN_EVENT_DMA_COMPLETE) {
- // CAN0接收
- if(instance == INST_CANCOM1)
- {
- CANrecMsg1.CAN_ID = recvMsg1.msgId;
- memcpy(CANrecMsg1.CAN_DATA, recvMsg1.data, 8);
- // 每次接收完成需要再设置接收,所以将此函数放在这里就可以不间断接收
- FLEXCAN_DRV_RxFifo(INST_CANCOM1, &recvMsg1);
- } // CAN1接收
- else if(instance == INST_CANCOM2)
- {
- CANrecMsg2.CAN_ID = recvMsg2.msgId;
- memcpy(CANrecMsg2.CAN_DATA, recvMsg2.data, 8);
- FLEXCAN_DRV_RxFifo(INST_CANCOM2, &recvMsg2);
- }
- }
- }
- // CAN发送时要使用到的 意为 标准帧 8byte 数据帧
- flexcan_data_info_t can1_data_std_info = {
- .msg_id_type = FLEXCAN_MSG_ID_STD,
- .data_length = 8U,
- .is_remote = false
- };
-
- uint8_t Tx[8] = {0,0x1,0x2,0x3,0x4,0x5,0x6,0x7};
-
- // 通过 CAN0 邮箱10 以默认配置 发送 ID是0x111 数据为0,0x1,0x2,0x3,0x4,0x5,0x6,0x7的报文
- FLEXCAN_DRV_Send(INST_CANCOM1, 10, &can1_data_std_info, 0x111, Tx);
组件例程下载地址:
https://download.csdn.net/download/m0_50669075/85034823
S32K148更多例程和源码以及最新内容下载地址:
END
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。