S32K的flexcan组件使⽤(RxFIFO+中断)
本⽂摘要:本⽂章介绍如何使⽤NXP官⽅软件S32KDS中的flexcan组件(RxFIFO+中断)
开发平台:S32 Design Studio for ARM Version 2.2
SDK版本:S32_SDK_S32K1xx_RTM_3.0.0
使⽤芯⽚:S32K148
注:本⽂讲述CAN使⽤Rx FIFO功能中断⽅式实现⽆过滤CAN报⽂接收,DMA⽅式见这篇⽂章!
1. 配置CAN所使⽤的引脚
2. 添加flexcan组件到⼯程(使⽤多个CAN就添加多个)
3. 打开对应的flexcan配置页⾯
4. 根据所需进⾏配置(这⾥介绍使⽤RxFIFO功能中断⽅式)
5. 配置好参数后点击如下图按钮或选项即可将配置数据更新⾄代码
6. 在代码中加⼊CAN初始化的代码即可使⽤CAN
// ⾃定义数据缓存结构体
typedef struct {
flex软件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;
// CAN0初始化
void CAN0_Init(void)
{
FLEXCAN_DRV_Init(INST_CANCOM1, &canCom1_State, &canCom1_InitConfig0);
FLEXCAN_DRV_SetRxMaskType(INST_CANCOM1, FLEXCAN_RX_MASK_INDIVIDUAL);
FLEXCAN_DRV_ConfigRxFifo(INST_CANCOM1,FLEXCAN_RX_FIFO_ID_FORMAT_A,NULL);
FLEXCAN_DRV_SetRxIndividualMask(INST_CANCOM1, FLEXCAN_MSG_ID_STD, MAILBOX_1, 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_SetRxMaskType(INST_CANCOM2, FLEXCAN_RX_MASK_INDIVIDUAL);
FLEXCAN_DRV_ConfigRxFifo(INST_CANCOM2,FLEXCAN_RX_FIFO_ID_FORMAT_A,NULL);
FLEXCAN_DRV_SetRxIndividualMask(INST_CANCOM2, FLEXCAN_MSG_ID_STD, MAILBOX_1, 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)
{
// RxFIFO接收完成
if(eventType == FLEXCAN_EVENT_RXFIFO_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);
}
}
}
7. CAN发送使⽤该函数
// 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);
END
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论