/*
*******************************************************************************
* Copyright (c) 2008,
* All rights reserved.
*
* File Name : canapp.c
* Project : CT101
* Author : jlong
* Version : 1.0.0
* Date : 12/11/2015
* Description :
*******************************************************************************
*/
/*
*************************************修改日志**********************************
*******************************************************************************
*/
#include "includes.h"
#include "can_j1939.h"
/*
*******************************************************************************
* 常量定义
*******************************************************************************
*/
#define STATUS_IDLE 0 //AT引擎处于等待状态//
#define STATUS_RUNNING 1 //AT引擎处于运行状态//
/*
*******************************************************************************
* 类型定义
*******************************************************************************
*/
#pragma pack(1)
typedef struct
{
void (*CanRecvHandle)(u32 CanID, u8 *CanBuf, u8 event); //回调函数//
s32 timeout; //超时时间//
u8 exe_status; //执行状态//
}sJ1939Engine;
#pragma pack()
/*
*******************************************************************************
* 变量定义
*******************************************************************************
*/
u8 Source_Address = 0XA5; //存储原地址
u8 Target_Address = 0x00; //发送请求的目标地址
u8 PDU_PF = 0x00; //存储PDU_PF域变量
u8 PDU_PS = 0x00; //存储PDU_PS域变量
u8 PDU_SA = 0x00; //存储PDU_PA域变量
u8 PDU_TA = 0X00; //存储上请求发送的目的地址
u8 PGN_Low = 0x00; //存储参数组pdu_pf
u8 PGN_Mid = 0x00; //存储参数组的PDU_PS
u8 PGN_High = 0x00; //存储目前正在进行数据交换的PGN
u8 CA_Name[8] = {0x00}; //存储CA名字
sJ1939Engine J1939Engine;
/*
*******************************************************************************
* 函数原型定义
*******************************************************************************
*/
/*=========================================================================
函数名称: Store_Ca_Name
功能描述: 存储CA的名字
输入参数:
输出参数:
=========================================================================*/
void J1939_StoreCaName(void)
{
CA_Name[0] = J1939_CA_Name0;
CA_Name[1] = J1939_CA_Name1;
CA_Name[2] = J1939_CA_Name2;
CA_Name[3] = J1939_CA_Name3;
CA_Name[4] = J1939_CA_Name4;
CA_Name[5] = J1939_CA_Name5;
CA_Name[6] = J1939_CA_Name6;
CA_Name[7] = J1939_CA_Name7;
}
/*=========================================================================
函数名称: Translate_IdExt
功能描述: 将标准的CAN 转换成J1939
输入参数: PDU_PF,PDU_PS,PDU_SA
输出参数: STM32报id_Exr
=========================================================================*/
u32 static J1939_TranslateIdExt (u8 Priority,u8 Pdu_PF,u8 Pdu_Ps,u8 Pdu_Sa)
{
u32 Id_Ext = 0x00000000;
Id_Ext = Id_Ext|Priority<<26;
Id_Ext = ((Id_Ext>>16)|Pdu_PF)<<16;
Id_Ext = ((Id_Ext>>8)|Pdu_Ps)<<8;
Id_Ext = Id_Ext|Pdu_Sa;
return Id_Ext;
}
/*=========================================================================
函数名称: CAN_SetFilterStd
功能描述: 设置标准帧过滤器
输入参数: 过滤器号码,过滤的ID和屏蔽器
输出参数: 无
=========================================================================*/
void J1939_SetFilterStd(u8 number,u16 ID,u16 Mask)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN 过滤器设置 */
CAN_FilterInitStructure.CAN_FilterNumber=number;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=ID;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=Mask;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
/*=========================================================================
函数名称: CAN_SetFilterExt
功能描述: 设置扩展帧过滤器
输入参数: 过滤器号码,过滤的ID和屏蔽器
输出参数: 无
=========================================================================*/
void J1939_SetFilterExt(u8 number,u16 ID,u16 Mask)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN 过滤器设置 */
CAN_FilterInitStructure.CAN_FilterNumber=number;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=ID;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=Mask;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
/*=========================================================================
函数名称: CAN_SetFilterExt_Full(u8 number,u32 ID)
功能描述: 设置扩展帧过滤器全滤波29位ID
输入参数: 过滤器号码,过滤的
输出参数: 无
=========================================================================*/
void J1939_SetFilterExtFull(u8 number,u32 ID)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN 过滤器设置 */
CAN_FilterInitStructure.CAN_FilterNumber=number;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=((ID<<3)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow=((ID<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0XFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x1fff;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xfff8;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
/*=========================================================================
函数名称: Cmd_Filter
功能描述: 滤波器激活
输入参数: 命令
输出参数: 无
=========================================================================*/
void J1939_CmdFilter(u8 Status_Filter0,u8 Status_Filter1)
{
if(Status_Filter0)
{
J1939_SetFilterExt(0,ID_REQUEST,Mask_Ack_Request_Tbdt);
}
else
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber=0x00;
CAN_FilterInitStructure.CAN_FilterActivation=DISABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
if(Status_Filter1)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber=0x01;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
else
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber=0x01;
CAN_FilterInitStructure.CAN_FilterActivation=DISABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
}
/*=========================================================================
函数名称: Set_ReadData_Filter(u8 Priority,u16 Pgn,u8 Target)
功能描述: 设置滤波器,只开一个滤波器并且对ID全滤波
输入参数: 优先级,PGN,目标地址
输出参数:
=========================================================================*/
void J1939_SetReadDataFilter(u8 Priority, u16 Pgn, u8 Target)
{
u32 Temp0 = 0x00000000;
J1939_CmdFilter(Disable_Filter0,Disable_Filter1); //禁止所有接收
CAN_FIFORelease(CAN1, CAN_FIFO0); //先释放已经接收的数据
Temp0 = (Temp0|Priority)<<18; //设置优先级
Temp0 = (Temp0|Pgn)<<8;
Temp0 = Temp0|Target;
J1939_SetFilterExtFull(1,Temp0); //开放设置好�