?? jadrclai.c
字號:
/******************************************************************************
*
* File Name: JAdrClai.c
*
* Description: Address Claiming functions
*
*
* Comments:
*
*
* Version Date Author Comments
* ------- ------------ -------------- ------------------------------
* 0.9 18.12.98 Ft Create
* 1.0 24.04.99 Ft C167CR and Tasking compiler
* 1.1 31.05.2001 Ma merge Stack with CANopen driver
* 2.0 18.06.2001 Ma Version 2.0 new buffer concept and
* documented for doxygen
* 2.1 15.08.2001 Ma added var to control the post online
* state
* 2.2 03.06.2003 Ma changed ADC handling
*
* Copyright (c) 1998 - 2003 Vector Informatik GmbH
*****************************************************************************/
#include "Includes.h"
#include "Portab.h"
#include "Types.h"
#include "J1939.h"
#include "J1939CFG.h"
#include "J1939PGN.h"
#include "J1939TRA.h"
#include "J1939EXT.h"
#include "JCancntrl.h"
#include "Jbuffer.h"
/*!
\brief Compares the 64 bit name of a device
Compare name with own device name and check priority.
\param -
\retval >0 if name has higher priority than own name
\retval =0 if name has lower priority than own name
\retval <0 if both names are equal
*/
signed char CompareJ1939Name(struct VJ1939DeviceName*name)
{
signed char i;
for (i=7; i>=0; i--)
{
if ( *(((unsigned char*)name)+i) < *(((unsigned char*)&gDeviceName)+i) )
return 1;
else if ( *(((unsigned char*)name)+i) > *(((unsigned char*)&gDeviceName)+i) )
return 0;
}
/* Both names are equal */
return -1;
}
/*!
\brief Sends the Address Claimed message
\param -
\retval -
*/
void SendAddressClaimed(void)
{
unsigned char i;
if (gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPGState == PG_TX_REQ)
{
/* If buffer is not free, throw away */
}
else
{
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPGState = PG_TX_REQ; /* Use buffer */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPDUF = 238; /* PDU Format */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPDUS = J1939GLOBALADDRESS; /* PDU Specific */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mSource = gDeviceAddress;
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPriority = 6; /* 3Bit Priority */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mTransType = TT_ASYNC; /* TT_CYCLIC, TT_ASYNC */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mTransRate = 0; /* Trans. rate in ms, only for cyclic */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mDataLength = 8; /* number of databytes */
#ifdef _TSK_C166_
for (i=0; i<8 ;i++) /* Copy data (name) */
#else
for (i=0; i<sizeof(struct VJ1939PGN_DATA_EE00) ;i++) /* Copy data (name) */
#endif /* _TSK_C166_ */
{
(((unsigned char*)(gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mData))[i]) = (((unsigned char*)&gDeviceName)[i]);
} /* for */
}
}
/*!
\brief Sends the Cannot Claim Address message
\param -
\retval -
*/
void SendCannotClaimAddress(void)
{
unsigned char i;
/* Use buffer */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPGState = PG_TX_REQ; /* Use buffer */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPDUF = 238; /* PDU Format */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPDUS = J1939GLOBALADDRESS; /* PDU Specific */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mSource = J1939NULLADDRESS;
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPriority = 6; /* 3Bit Priority */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mTransType = TT_ASYNC; /* TT_CYCLIC, TT_ASYNC */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mTransRate = 0; /* Trans. rate in ms, only for cyclic */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mDataLength = 8; /* number of databytes */
for (i=0; i<8 ;i++) /* Copy data (name) */
{
((unsigned char*)(gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mData))[i] = ((unsigned char*)&gDeviceName)[i];
} /* for */
}
/*!
\brief Checks the reception of an Address Claimed message
Function checks the name and sends the corresponding Address Claim
message
\param -
\retval -
*/
void CheckRxAddressClaimed(void)
{
if (gAddressClaimed)
{
if (gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mSource == J1939GLOBALADDRESS)
{
SendAddressClaimed();
}
else if (gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mSource == gDeviceAddress)
{
/* Get priority information */
if (CompareJ1939Name(gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mData) > 0)
{
/* Own name has lower priority */
SendCannotClaimAddress();
gAddressClaimed = 0; /* Reset address claimed */
}
else
{
/* Own name has higher priority */
SendAddressClaimed();
}
}
}
}
/*!
\brief The function executes the Address Claiming
After sending the 1. AddressClaimed message we wait 250 ms for other
AC messages. If there was no response with the same adress, we set our
address valid. Then we have successful claimed the address
\param -
\retval The current Device Address
*/
BOOLEAN J1939AddressClaiming(unsigned char *address)
{
static unsigned char step = 0;
static unsigned char nFlag = 0;
unsigned char i;
if(step == 0)
{
loop1:
gTxPGTable[RX_PGN_ADDRESS_CLAIMED].mSource = J1939AppGetAddress(); /* Get default address */
gTxPGTable[RX_PGN_ADDRESS_CLAIMED].mPDUS = J1939GLOBALADDRESS; /* Global receiver */
for (i=0; i<gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mDataLength; i++) /* Copy device name to data */
{
((unsigned char*)gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mData)[i] = ((unsigned char*)&gDeviceName)[i];
}
gCB_SendSysPG(&gTxPGTable[TX_PGN_ADDRESS_CLAIMED]); /* Transmit PGN */
step++;
} /* if state == 0 */
if ((step==1))
{
if(gCB_CheckSysPGSend()) /* Wait until transmitted */
{
step++;
} /* while */
else
{
if(gCan_GetCanStatus()== CAN_STS_BUS_OFF)
{
gCANBusOff = 1;
return(FALSE);
} /* if */
} /* else */
} /* if state == 1*/
if( step == 2)
{
gAddressClaimTimer = Tim_GetCurrentTime(); /* Load/start timer 250ms */
nFlag = 0;
step++;
} /* if step == 2 */
if(step == 3)
{
gCB_DequeueElm();
if ((gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mPGState == PG_RX_FULL) && (nFlag == 0))
{
if (gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mSource == J1939AppGetAddress())
{
if (CompareJ1939Name((struct VJ1939DeviceName*)gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mData)>0) /* check priority */
{
/* Other name has higher priority than own name. */
gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mPGState = PG_RX_FREE; /* Free buffer */
gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mSource = J1939NULLADDRESS;
/*gTxPGTable[TX_PGN_ADDRESS_CLAIMED].mPDUS = J1939GLOBALADDRESS;*/
gCB_SendSysPG(&gTxPGTable[TX_PGN_ADDRESS_CLAIMED]); /* Transmit PGN */
gAddressClaimed = 0; /* cannot claim address */
*address = J1939NULLADDRESS; /* return new address */
nFlag = 1;
return FALSE;
}
else
{
/* Other name has lower priority than own name */
gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mPGState = PG_RX_FREE; /* Free buffer */
step=0;
goto loop1; /* Restart address claiming */
}
}
gRxPGTable[RX_PGN_ADDRESS_CLAIMED].mPGState = PG_RX_FREE; /* Free buffer */
} /* if */
if(Tim_CheckTimeOut(gAddressClaimTimer,Tim_GetCurrentTime(),ADDRESSCLAIMTIMEOUT)) /* timer still running */
{
if(nFlag==0)
{
step++;
}
else
{
step = 0;
}
}
} /* if step == 3 */
if(step == 4)
{
/* Now the device address is valid */
*address = J1939AppGetAddress();
gAddressClaimed = 1; /* address claimed */
gOnlineMarker = 1; /* sign the first online state */
step = 0;
return TRUE; /* return the new address */
} /* if step == 4 */
return(FALSE);
}
/*!
\file
\brief Basic routines for the AddressClaim Handling
\date Last modification 04/07/2001
\author (c) 2001 by Vector Informatik GmbH. All rights reserved
This module uses the timer and buffer module independendly against the stack
module. It dequeues the system parameter group queue!
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -