📄 klinecomm.c
字号:
/******************************************************************************
Copyright (c) Jiang Kui 2005
File Name : Klinecommunication.c
Engineer : manda
Location : Source
Date Created : 12/12/2005
Current Revision : Revision: 1.0
Notes : none
************************* Project Include Files *****************************/
#include <libdefs.h> //ANSI-C library file: library configuration definition
#include <hidef.h> //ANSI-C library, machine dependent stuff Warning:Dont modify
#include "mcs912dp256.h" //I/O device mapping for MCS12DJ256B
#include "commondefine.h" //common global parameters and functions define
#include "configuration.h" //common global parameters and functions define
#include "Diagnose.h"
#include "eeprom.h" //EEPROM read and write functions define
#include "KlineComm.h" //the K_Line communication functions define
#include "KldataConf.h"
#include "os.h"
#pragma CODE_SEG DEFAULT
/************************* External Variables ********************************/
/******************************************************************************/
/******************************************************************************
Function Name : KlineCommInit
Engineer :
Date : 12/12/2005
Arguments : channel:
baudrate:
Return : none
Notes : This function initialize SCI 0.
******************************************************************************/
void KlineCommInit(UINT8 channel,UINT16 baudrate)
{
if(channel==1)
{
SCI1BDL=(unsigned char)((4000000UL /* OSC freq */ / 2) / baudrate /* baud rate */ / 16 /*factor*/);
SCI1CR2=0x2c;//Transmit Enable & Receive Enable & Receive Interrupts Enable
// DDRM_DDRM2=1; //set the PortM6 as output
// PTM_PTM2=1; //enable the MC33199
}
else if(channel==0)
{
SCI0BDL=(unsigned char)((4000000UL /* OSC freq */ / 2) / baudrate/* baud rate */ / 16 /*factor*/);
SCI0CR2=0x2c;//Transmit Enable & Receive Enable & Receive Interrupts Enable
// DDRM_DDRM2=1; //set the PortM6 as output
// PTM_PTM2=1; //enable the MC33199
}
}
/******************************************************************************
Function Name : KlineInit
Engineer :
Date : 12/12/2005
Arguments : none
Return : none
Notes : This function initialize the parameters of K line communication.
******************************************************************************/
void KlineParaInit(void)
{
CommunicationFlag0=0;
CommunicationFlag1=0;
CommunicationFlag2=0;
CommunicationFlag3=0;
G_un16ReceivedataNum=0;
G_uc8RequstMLength=0;
G_un16ResponseMLength=0;
G_n16CommDelay=0;
G_ServiceID=0;
G_bSendEnble=0;
G_bSendReady=0;
G_bSendComplete=0;
G_bReceiveComplete=0;
G_un8P2TimerMin=25;//G_un8P2TimerMin=100 for RTI 250us period/G_un8P2TimerMin=25 for RTI 1MS
G_un8P2TimerMax=48;//G_un8P2TimerMax=192 for RTI 250us period/G_un8P2TimerMax=48 for RTI 1MS
G_un16P3TimerMin=55;//G_un16P3TimerMin=220 for RTI 250us period/G_un16P3TimerMin=55 for RTI 1MS
G_un16P3TimerMax=5000;//G_un16P3TimerMax=20000 for RTI 250us period/G_un16P3TimerMax=5000 for RTI 1MS
G_un16SendNum=0;
G_n8ByteInter=0;
G_uc8ResponseMessage[1]=0xf1;
G_uc8ResponseMessage[2]=0x11;
}
/******************************************************************************
Function Name : KLineSend
Engineer :
Date : 03/01/2005
Arguments : none
Return : none
Notes : This function send data from SCI 0.
******************************************************************************/
void KLineSend(unsigned char L_byte)
{
//SCI0CR2=0x08;
while(SCI1SR1&0x80 !=0)// !SCI1SR1_TDRE){} //Poll the TDRE flag by reading the SCI0SR1 until the TDRE flag is set
SCI1DRL=L_byte; //If the TDRE flag is set, write the data to be transmitted to SCI0DRL
ucReadBuffer=SCI1SR1;//_TDRE;
ucReadBuffer=SCI1DRL;
//SCI0CR2=0x2c;
}
/******************************************************************************
Function Name : KLineReceive
Engineer :
Date : 12/12/2005
Arguments : none
Return : none
Notes : This function receive data from SCI 1 and analyse message data.
******************************************************************************/
void KLineReceive()
{
ucReadBuffer=SCI1SR1;
ucReadBuffer=SCI1DRL;
G_uc8Receivedata[G_un16ReceivedataNum++]=ucReadBuffer;
if(G_un16ReceivedataNum>=4)
{
if(G_uc8Receivedata[G_un16ReceivedataNum-2]==0xf1&&
G_uc8Receivedata[G_un16ReceivedataNum-3]==0x33)
{
G_StartLocation=G_un16ReceivedataNum-4;//Format Byte 报文头字节在G_uc8Receivedata[]中的位置
if(G_uc8Receivedata[G_un16ReceivedataNum-4]>0xc0)
{
G_uc8RequstMLength=(G_uc8Receivedata[G_StartLocation]&0x0f)+0x04;//Length of Request Message
G_SIDLocation=G_StartLocation+3;
}
else if(G_uc8Receivedata[G_un16ReceivedataNum-4]==0xc0)
{
G_uc8RequstMLength=G_uc8Receivedata[G_StartLocation+3]+0x05;//Length of Request Message
G_SIDLocation=G_StartLocation+4;
}/**/
}
}
if((G_un16ReceivedataNum-G_StartLocation)==G_uc8RequstMLength)
{
unsigned int i,data=0;
unsigned char un8CheckSum=0;
i=G_StartLocation;
while(i<=G_un16ReceivedataNum-2)
{
data+=G_uc8Receivedata[i];
un8CheckSum=(unsigned char)(data&0x00ff);// checksum of uc8ReadResponse[].
i++;
}
if(G_uc8Receivedata[G_un16ReceivedataNum-1]==un8CheckSum)//检错字节相符,接收报文正确
{
G_un16ReceivedataNum=0;
G_bSendReady=0;
G_bSendEnble=0;
G_bSendComplete=0;
bReadDataLIDFlag=0;
G_ServiceID=G_uc8Receivedata[G_SIDLocation];
switch(G_ServiceID)
{
case 0x81:bStartFlag=1;break;
case 0x82:bStopFlag=1;break;
case 0x3E:
{
unsigned int i;
for(i=3;i<260;i++)
{
G_uc8ResponseMessage[i]=0;
}
G_un16ResponseMLength=0;
}
CommunicationFlag=0;
CommunicationFlag0=0;
CommunicationFlag1=0;
CommunicationFlag2=0;
CommunicationFlag3=0;
bHandshakeFlag=1;
break;
case 0x21:
bReadDataLIDFlag=1;
bReadDTCBySFlag=0;
G_ReadDataPID=G_uc8Receivedata[G_SIDLocation+1];
break;
case 0x14:bClearFlag=1;break;
case 0x18:
bReadDataLIDFlag=0;
bReadDTCBySFlag=1;
break;
case 0x23:
bReadDataLIDFlag=0;
bHandshakeFlag=0;
bReadMemoryByAFlag=1;
G_uc8MemoryAddressH=G_uc8Receivedata[G_SIDLocation+1];
G_uc8MemoryAddressM=G_uc8Receivedata[G_SIDLocation+2];
G_uc8MemoryAddressL=G_uc8Receivedata[G_SIDLocation+3];
G_uc8MemorySize=G_uc8Receivedata[G_SIDLocation+4];
break;
case 0x3B:
bReadDataLIDFlag=0;
bWriteDataByLIdFlag=1;
G_WriteDataPID=G_uc8Receivedata[G_SIDLocation+1];
break;
case 0x3D:
bWriteMemoryByAFlag=1;
G_uc8MemoryAddressH=G_uc8Receivedata[G_SIDLocation+1];
G_uc8MemoryAddressM=G_uc8Receivedata[G_SIDLocation+2];
G_uc8MemoryAddressL=G_uc8Receivedata[G_SIDLocation+3];
G_uc8MemorySize=G_uc8Receivedata[G_SIDLocation+4];
break;
case 0x38:
bStartRoutineByAFlag=1;
G_uc8MemoryAddressH=G_uc8Receivedata[G_SIDLocation+1];
G_uc8MemoryAddressM=G_uc8Receivedata[G_SIDLocation+2];
G_uc8MemoryAddressL=G_uc8Receivedata[G_SIDLocation+3];
break;
case 0x83:bAccessTimingFlag=1;break;
case 0x10:bStartSessionFlag=1;break;
/*case 0x27:bSecurityAccessFlag=1;break;
case 0x11:bECUResetFlag=1;break;
case 0x1A:bReadEcuIdFlag=1;break;
case 0x22:bReadDataCIDFlag=1;break;
case 0x2C:bDefineLocalIdFlag=1;break;
case 0x2E:bWriteDataByCIdFlag=1;break;
case 0x17:bReadStatusofDTCFlag=1;break;
case 0x12:bReadFreezeDataFlag=1;break;
case 0x30:bIOControlByLIdFlag=1;break;
case 0x2F:bIOControlByCIdFlag=1;break;
case 0x31:bStartRoutineByLIdFlag=1;break;
case 0x32:bStopRoutineByLIdFlag=1;break;
case 0x39:bStopRoutineByAFlag=1;break;
case 0x33:bRoutineResultByLIdFlag=1;break;
case 0x3A:bRoutineResultByAFlag=1;break;
case 0x34:bDownloadFlag=1;break;
case 0x35:bUploadFlag=1;break;
case 0x36:bTransferDataFlag=1;break;
case 0x37:bTransferExitFlag=1;break;*/
default:
bNotSurpport=1;
break;
}
G_bReceiveComplete=1;
ActivateTask(CreatRespMesKWP2000Task);
// bBasicTask27Ready=1;//触发执行相应操作并生成发送报文函数
}
}
}
/******************************************************************************
Function Name : CheckSum
Engineer : Manda
Date : 12/12/2005
Arguments : none
Return : none
Notes : This function calculates the byte of check sum of a response message.
******************************************************************************/
void KLineTimer()
{
if(SCI1SR2&0x01 !=0)// SCI1SR2_RAF)
G_n16WakeUpDelay++;
if(G_n16WakeUpDelay>=6)
{
G_bWakeUpFlag=1;
G_n16WakeUpDelay=0;
}
if(G_bReceiveComplete)//||G_bSendComplete
{
G_n16CommDelay++; //timer for SCI0 communication
if(G_n16CommDelay>=G_un8P2TimerMin&&G_n16CommDelay<G_un8P2TimerMax&&G_bSendReady)/*G_un8P2TimerMin*/
{
G_bSendEnble=1;
G_n16CommDelay=0;
G_bReceiveComplete=0;
G_bSendReady=0;
SCI1CR2=0x08;
}
else if(G_n16CommDelay>=G_un8P2TimerMax&&!G_bSendReady)
{
bResponsePending=1; //requestCorrectlyReceived-ResponsePending
SCI1CR2=0x08;
}
else if(G_n16CommDelay<G_un16P3TimerMax&&!G_bSendEnble&&G_bSendReady)//在P2扩展时间内执行完毕
{
G_bSendEnble=1;
G_n16CommDelay=0;
G_bReceiveComplete=0;
G_bSendReady=0;
SCI1CR2=0x08;
}
else if(G_n16CommDelay>=G_un16P3TimerMax&&!G_bSendEnble&&!G_bSendReady)
{
G_n16CommDelay=0;
G_un16ResponseMLength=0;
G_bReceiveComplete=0;
CommunicationFlag=0;
CommunicationFlag0=0;
CommunicationFlag1=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -