赞
踩
基于S32K312开发板进行配置CAN模块的收发功能。
需要的工具有:EB、S32DS、USB_CAN TooL
其中USB_CAN是进行CAN报文接收和发送的工具。
EB需要的模块有:
1.Port模块:配置can的接收和发送IO,拉高STB和EN脚
2.Can_43_FLEX模块:配置Can的收发邮箱和端口信息等
3.Canif模块:配置Can的驱动层
4.Ecuc和EcuM模块:配置相关所需配置
5.MCU和Platform模块:使能相关中断
下面是各个模块的相应的配置
使用的是S32K312,只有Can0才有收发器,下面是相关引脚图。
所以EB的Port配置如下图
找到相应的引脚,并按照下图配置
其中STB和EN是控制CAN收发器的模式,两者都高即为正常收发模式。
选择模式VariantPreCompile,方便初始化
这里只需要配置两个选项即可,分别为CanController和CanHardwareObject
相关can的波特率设置
在这里可以通过计算相应的系数得到相应的can通信波特率。
在这里可配置收发邮箱,如下图,
进入Can_0_RX,设置接收的信号段(每两组)
只需要添加这个模块即可
使能相应的FLEXCAN模块。
构造can的发送函数,下面是can.c和can.文件
#include "can.h" void can_get_rx_message(const PduInfoType* PduInfoPtr , uint8_t *pRxData) { uint8 lenth_cout = 0; for(lenth_cout = 0;lenth_cout < PduInfoPtr->SduLength;lenth_cout++) { pRxData[lenth_cout] = PduInfoPtr->SduDataPtr[lenth_cout]; } } void can_send_data(uint8 Controller,Can_HwHandleType Hth,Can_IdType id, PduIdType swPduHandle, uint8 length, uint8* sdu) { Can_PduType PduInfo; PduInfo.id = id; PduInfo.swPduHandle = swPduHandle; PduInfo.length = length; PduInfo.sdu = sdu; Can_43_FLEXCAN_SetControllerMode(Controller, CAN_CS_STARTED); Can_43_FLEXCAN_Write(Hth, &PduInfo); }
#ifndef CAN_H_
#define CAN_H_
//void CAN_Analysis(const Can_HwType* Mailbox);
#include "Can_43_FLEXCAN.h"
void can_get_rx_message(const PduInfoType* PduInfoPtr , uint8_t *pRxData);
void can_send_data(uint8 Controller,Can_HwHandleType Hth,Can_IdType id,
PduIdType swPduHandle, uint8 length, uint8* sdu);
#endif /* CAN_H_ */
在main函数调用函数即可,下面是示例
//这些是can所需要的配置,是S32调用的 uint8 CanIf_u8TxConfirmCnt = 0U; boolean CanIf_bTxFlag = FALSE; uint8 CanIf_u8RxIndicationCnt = 0U; boolean CanIf_bRxFlag = FALSE; void CanIf_ControllerBusOff(uint8 ControllerId) { (void)ControllerId; } void CanIf_ControllerModeIndication(uint8 ControllerId, Can_ControllerStateType ControllerMode) { (void)ControllerId; (void)ControllerMode; } void CanIf_TxConfirmation(PduIdType CanTxPduId) { CanIf_u8TxConfirmCnt++; CanIf_bTxFlag = TRUE; (void)CanTxPduId; } void CanIf_RxIndication(const Can_HwType* Mailbox, const PduInfoType* PduInfoPtr ) { CanIf_u8RxIndicationCnt++; CanIf_bRxFlag = TRUE; (void)Mailbox; (void)PduInfoPtr; } Can_PduType Can_CreatePduInfo(Can_IdType id, PduIdType swPduHandle, uint8 length, uint8* sdu) { Can_PduType PduInfo; PduInfo.id = id; PduInfo.swPduHandle = swPduHandle; PduInfo.length = length; PduInfo.sdu = sdu; return PduInfo; } int main(){ uint8 sdu[8U]= {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; can_send_data(Can_0,Can0_TX_001,0x001,0U,8U,sdu); }
上诉代码即可发送can数据,然后利用USB_CAN TOOL 查看发送的数据。
1.学习到can模块的配置,使用can的收发功能
2.K3的功能开发打下基础,基于Autosar8架构
3.加油!后续写一个can收发的上位机!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。