本文摘要:本文章介绍如何使用NXP官方软件S32KDS中的flexcan组件实现can fd收发
开发平台:S32 Design Studio for ARM Version 2.2
SDK版本:S32_SDK_S32K1xx_RTM_3.0.0
使用芯片:S32K148
组件例程免费下载方式在文末!!!
1. 配置CAN所使用的引脚
2. 添加flexcan组件到工程(使用多个CAN就添加多个)
3. 打开对应的flexcan配置页面![](https://i-blog.csdnimg.cn/blog_migrate/24a58d00eeb41729c2175f1ac249e6d7.png)
4. 进行配置
5. 时钟配置
6. 配置好参数后点击如下图按钮或选项即可将配置数据更新至代码
7. 在代码中加入CAN FD初始化的代码即可使用CAN FD
#include "main.h"
// CAN接收数据结构体
CANDataStruct CANrecMsg1;
// CAN发送数据结构体
CANDataStruct CANsendMsg1;
// 系统
static flexcan_msgbuff_t recvMsg1;
// CAN接收到数据标志
AllCANFlagStruct AllCANFlag;
// CAN发送时要使用到的 意为 使能FD 标准帧 64byte 数据帧
flexcan_data_info_t can1_data_std_info = {
.msg_id_type = FLEXCAN_MSG_ID_STD,
.data_length = 64U,
.fd_enable = true,
.fd_padding = 0xCC,
.enable_brs = false, // 不开启数据域波特率切换
.is_remote = false
};
// can接收发送回调函数
void canRxCallback(uint8_t instance, flexcan_event_type_t eventType,
uint32_t buffIdx, flexcan_state_t *flexcanState)
{
(void)flexcanState;
(void)buffIdx;
// Rx完成
if(eventType == FLEXCAN_EVENT_RX_COMPLETE) {
if(instance == INST_CANCOM1)
{
// 只有当上次报文被处理后才更新新的报文
if(!AllCANFlag.CANRecData_flag1) {
CANrecMsg1.CAN_ID = recvMsg1.msgId;
memcpy(CANrecMsg1.CAN_DATA, recvMsg1.data, 64);
AllCANFlag.CANRecData_flag1 = 1;
#ifdef DEBUG_printf
SEGGER_RTT_printf(0,"CAN0 received ID:0x%x DATA:", CANrecMsg1.CAN_ID);
for(uint8_t i = 0; i < 64; i++) SEGGER_RTT_printf(0,"0x%02x ", CANrecMsg1.CAN_DATA[i]);
SEGGER_RTT_printf(0,"\n");
#endif
}
FLEXCAN_DRV_Receive(INST_CANCOM1, 0, &recvMsg1); // 接收成功后再配置接收 实现不间断接收
}
}
}
// can发送
void set_CANTransmitData(CANDataStruct *CANStruct, uint8_t MAILBOX)
{
while(FLEXCAN_DRV_Send(INST_CANCOM1, MAILBOX, &can1_data_std_info, CANStruct->CAN_ID, CANStruct->CAN_DATA) == STATUS_BUSY);
}
// CAN初始化
void CAN1_Init(void)
{
FLEXCAN_DRV_Init(INST_CANCOM1, &canCom1_State, &canCom1_InitConfig0);
FLEXCAN_DRV_SetRxMbGlobalMask(INST_CANCOM1, FLEXCAN_MSG_ID_STD, 0); // 使用MB0接收
FLEXCAN_DRV_InstallEventCallback(INST_CANCOM1, canRxCallback, NULL);
FLEXCAN_DRV_ConfigRxMb(INST_CANCOM1, 0, &can1_data_std_info, 0); // 0不过滤ID !0为接收到的ID
FLEXCAN_DRV_Receive(INST_CANCOM1, 0, &recvMsg1); // 配置接收
#ifdef DEBUG_printf
SEGGER_RTT_printf(0,"CAN0 init success\n");
#endif
}
8. 发送和接收示例如下
/**
* @brief 系统初始化
* @param None
* @retval None
*/
void SYSTEM_Init(void)
{
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
POWER_SYS_Init(&powerConfigsArr, POWER_MANAGER_CONFIG_CNT, &powerStaticCallbacksConfigsArr, POWER_MANAGER_CALLBACK_CNT);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
CAN1_Init();
lptmr_Init();
CANsendMsg1.CAN_ID = 0x777;
for(uint8_t count = 0; count < 64; count++)
{
CANsendMsg1.CAN_DATA[count] = count;
}
}
/**
* @brief 任务处理
* @param None
* @retval None
*/
void TASK_Schedule(void)
{
// 周期发送
if(TIM_1s_flag)
{
set_CANTransmitData(&CANsendMsg1, MAILBOX_6);
TIM_1s_flag = 0;
#ifdef DEBUG_printf
SEGGER_RTT_printf(0,"send success\n");
#endif
}
// 收到数据回显
if(AllCANFlag.CANRecData_flag1)
{
set_CANTransmitData(&CANrecMsg1, MAILBOX_6);
AllCANFlag.CANRecData_flag1 = 0;
#ifdef DEBUG_printf
SEGGER_RTT_printf(0,"return success\n");
#endif
}
}
组件例程下载地址:
https://download.csdn.net/download/m0_50669075/85423212
S32K148更多例程和源码以及最新内容下载地址:
END