int DiagnosticSessionControl_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle,
NCTYPE_OBJH rxHandle, ERRORINFO *errorInfo)
/* Description : Open a diagnostic session in supplier mode */
/*===========================================================================*/
{
int error = 0, defaut = 0;
long seed, key;
tabDataCAN txData, rxData;
char txtErr [100];
errorInfo->wCode = 0;
sprintf (errorInfo->description, "");
// open a diagnostic session
txData[0] = 0x03; // Frame length
txData[1] = 0x02; // Nb following bytes
txData[2] = 0x10; // Opening session code
txData[3] = 0x7A; // Supplier mode
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Opening session : the receiving frame is bad\n%s\nExpected frame = 50 FA", txtErr);
defaut = -10;
goto Error;
}
// security access
txData[0] = 0x03; // Frame length
txData[1] = 0x02; // Nb following bytes
txData[2] = 0x27; // SecurityAccess service code
txData[3] = 0x6F; // Supplier access mode
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Security access : the receiving frame is bad\n%s\nExpected frame = 67 EF XX XX XX XX", txtErr);
defaut = -10;
goto Error;
}
// calcul the key
seed = (rxData[5]) + (rxData[4] <<8) + (rxData[3] <<16);
if (seed == 0)
goto Error; // Product is always unlocked
key = seed + 0xB1234C;
// Send Seed + Key
txData[0] = 0x06; // Frame length
txData[1] = 0x05; // following bytes
txData[2] = 0x27; // SecurityAccess service code
txData[3] = 0x70; // key for Supplier mode
txData[4] = (NCTYPE_UINT8) ((key & 0xFF0000)>>16); /* */
txData[5] = (NCTYPE_UINT8) ((key & 0xFF00)>>8); /* */
txData[6] = (NCTYPE_UINT8) (key & 0xFF); /* */
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Seed & Key sending : the receiving frame is bad\n%s\nExpected frame = 67 F0", txtErr);
defaut = -10;
goto Error;
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int GetInformation_RC5_MAZDA_J64( NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, int codeInfo, tabDataCAN *varRxData,
int withBSI)
/* Description : Do a GetInformation action to read a product information /
/* valeur attendu. */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtValeur[100]={0};
txData[0] = 0x04; // Frame length
txData[1] = 0x03; // Following bytes
txData[2] = 0x22; // ReadDataByLocalIdentifier service code
txData[3] = 0xFD; // test erased local service code (high byte)
txData[4] = (NCTYPE_UINT8)codeInfo;
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo,
withBSI, 1, 1));
if (rxData[1] != (txData[2] + 0x40)) {
defaut = 1;
CAN_sprintf (txtValeur, rxData, 3, 1);
}
else if (rxData[2] != txData[3]) {
defaut = 2;
CAN_sprintf (txtValeur, rxData, 3, 1);
}
else if (rxData[3] != txData[4]) {
defaut = 3;
CAN_sprintf (txtValeur, rxData, 3, 1);
}
else {
memcpy (*varRxData, rxData, sizeof (tabDataCAN));
}
if (defaut != 0) {
sprintf (errorInfo->description, "%s", txtValeur);
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int HexSwitchMode_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, int mode)
/* Description : Send a hexswitchMode command to the product ` */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100];
txData[0] = 0x05; /* Frame length */
txData[1] = 0x04; /* Following bytes */
txData[2] = 0x2E; // WriteDataByLocalIdentifier service code/
txData[3] = 0xFD; // HexSwitchMode local service code
txData[4] = 0x12; // HexSwitchMode local service code
txData[5] = (char)mode; // Test mode
defaut = CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1, 1);
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "HexSwitch mode : the receiving frame is bad\n%s", txtErr);
defaut = -10;
goto Error;
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int TestMode_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, int mode)
/* Description : Send a TestMode command to the product */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100];
int withLoopError = 1;
txData[0] = 0x05; /* Frame length */
txData[1] = 0x04; /* Following bytes */
txData[2] = 0x2E; // WriteDataByLocalIdentifier service code/
txData[3] = 0xFD; // TestMode local service code
txData[4] = 0x13; // TestMode local service code
txData[5] = (char)mode; // Test mode
if ((mode == 1) || (mode == 2))
withLoopError = 0; // for mode 0 or 1 , ECU reset
defaut = CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1,
withLoopError);
switch (mode) {
case 1 : // external watchdog
case 2 : // internal watchdog
if (defaut != 0) defaut = 0;
break;
default :
if (defaut != 0) {
goto Error;
}
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Test mode : the receiving frame is bad\n%s", txtErr);
defaut = -10;
goto Error;
}
break;
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int EraseError_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo)
/* Description : Send the buffer error erase command */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100];
txData[0] = 0x05; /* Frame length */
txData[1] = 0x04; /* Following bytes */
txData[2] = 0x2E; // WriteDataByLocalIdentifier service code/
txData[3] = 0xFD; // EraseError local service code
txData[4] = 0x10; // EraseError local service code
txData[5] = 0xAA; // Dummy data
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Erased error : the receiving frame is bad\n%s", txtErr);
defaut = -10;
}
Error:
if (error != 0) {
errorInfo->wCode = error;
defaut = error;
}
return defaut;
}
/*===========================================================================*/
int TestErased_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, tabDataCAN *varRxData)
/* Description : Send the command to test if the buffer error is erased */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100];
txData[0] = 0x04; // Frame length
txData[1] = 0x03; // Following bytes
txData[2] = 0x22; // ReadDataByLocalIdentifier service code
txData[3] = 0xFD; // test erased local service code (high byte)
txData[4] = 0x00; // test erased local service code (low byte)
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, 0, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Test Erased : the receiving frame is bad\n%s", txtErr);
defaut = -10;
goto Error;
}
memcpy (*varRxData, rxData, sizeof (tabDataCAN));
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int ReadMemoryByAddress_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, unsigned long address,
int nbData, NCTYPE_UINT8 *varRxData, int withBSI, int withFlush)
/* Description : Read a value in the product memory and return the value
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100]={0};
txData[0] = 0x09; /* Frame length */
txData[1] = 0x08; /* Following bytes */
txData[2] = 0x23; // ReadMemoryByAddress service code
txData[3] = 0x24; // AddressAndLenghtFormatIdentifier
txData[4] = (NCTYPE_UINT8) ((address & 0xFF000000)>>24); // MSB of the ECU's address - byte 3
txData[5] = (NCTYPE_UINT8) ((address & 0xFF0000)>>16); // ECU's address - byte 2
txData[6] = (NCTYPE_UINT8) ((address & 0xFF00)>>8); // ECU's address - byte 1
txData[7] = (NCTYPE_UINT8) (address & 0xFF); // ECU's address - byte 0
txData[8] = (NCTYPE_UINT8) ((nbData&0xFF00)>>8); // Nb bytes to read - MSB
txData[9] = (NCTYPE_UINT8) (nbData&0xFF); // Nb bytes to read - LSB
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo,
withBSI, withFlush, 1));
if (rxData[1] != (txData[2] + 0x40)) {
defaut = 1;
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Read memory : the receiving frame is bad\n%s", txtErr);
}
else {
memcpy (varRxData, rxData, 255 * (sizeof (NCTYPE_UINT8)));
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int WriteMemoryByAddress_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, unsigned long address,
int nbData, NCTYPE_UINT8 *varTxData, int withBSI, int withFlush)
/* Description : Write data in ECU memory for RC5 RN product */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100]={0};
int count;
txData[0] = 0x09+nbData; /* Frame length */
txData[1] = 0x08+nbData; /* Following bytes */
txData[2] = 0x3D; // WriteMemoryByAddress service code
txData[3] = 0x24; // AddressAndLenghtFormatIdentifier
txData[4] = (NCTYPE_UINT8) ((address & 0xFF000000)>>24); // MSB of the ECU's address - byte 3
txData[5] = (NCTYPE_UINT8) ((address & 0xFF0000)>>16); // ECU's address - byte 2
txData[6] = (NCTYPE_UINT8) ((address & 0xFF00)>>8); // ECU's address - byte 1
txData[7] = (NCTYPE_UINT8) (address & 0xFF); // ECU's address - byte 0
txData[8] = (NCTYPE_UINT8) ((nbData&0xFF00)>>8); // Nb bytes to read - MSB
txData[9] = (NCTYPE_UINT8) (nbData&0xFF); // Nb bytes to read - LSB
for (count = 0; count < nbData; count++)
txData[10+count] = varTxData [count];
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo,
withBSI, withFlush, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4]) ||
(rxData[4] != txData[5]) ||
(rxData[5] != txData[6]) ||
(rxData[6] != txData[7]) ||
(rxData[7] != txData[8]) ||
(rxData[8] != txData[9]) ){
defaut = 1;
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Write memory : the receiving frame is bad\n%s", txtErr);
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int Unlock_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, int withBSI)
/* Description : Unlock the ACU4 product */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100];
// set shutdown mode
// DefCAN (TestMode_RC5_MAZDA_J64 (txHandle, rxHandle, errorInfo, 3));
// Send unlock command
txData[0] = 0x15; /* Frame length */
txData[1] = 0x14; /* Following bytes */
txData[2] = 0x2E; // WriteDataByLocalIdentifier service code/
txData[3] = 0xFD; // FactoryUnlocking local service code
txData[4] = 0x11; // FactoryUnlocking local service code
txData[5] = 0x55; // Unlocking
txData[6] = 0x4D; // key
txData[7] = 0x44;
txData[8] = 0x48;
txData[9] = 0x4C;
txData[10] = 0x46;
txData[11] = 0x56;
txData[12] = 0x4C;
txData[13] = 0x42;
txData[14] = 0x43;
txData[15] = 0x4D;
txData[16] = 0x41;
txData[17] = 0x41;
txData[18] = 0x44;
txData[19] = 0x47;
txData[20] = 0x4C;
txData[21] = 0x46;
// txData[5] = 0x45; // key
// txData[6] = 0x52;
// txData[7] = 0x46;
// txData[8] = 0x43;
// txData[9] = 0x40;
// txData[10] = 0x40;
// txData[11] = 0x53;
// txData[12] = 0x42;
// txData[13] = 0x41;
// txData[14] = 0x52;
// txData[15] = 0x62;
// txData[16] = 0x45;
// txData[17] = 0x52;
// txData[18] = 0x48;
// txData[19] = 0x43;
// txData[20] = 0x53;
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, withBSI, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Unlock : the receiving frame is bad\n%s", txtErr);
defaut = -10;
goto Error;
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int Lock_RC5_MAZDA_J64 (NCTYPE_OBJH txHandle, NCTYPE_OBJH rxHandle,
ERRORINFO *errorInfo, int withBSI)
/* Description : Lock the ACU4 product */
/*===========================================================================*/
{
int error = 0, defaut = 0;
tabDataCAN txData, rxData;
char txtErr [100];
// set shutdown mode
// DefCAN (TestMode_RC5_MAZDA_J64 (txHandle, rxHandle, errorInfo, 3));
// send lock command
txData[0] = 0x15; /* Frame length */
txData[1] = 0x14; /* Following bytes */
txData[2] = 0x2E; // WriteDataByLocalIdentifier service code/
txData[3] = 0xFD; // FactoryUnlocking local service code
txData[4] = 0x11; // FactoryUnlocking local service code
txData[5] = 0xAA; // Locking
txData[6] = 0x45; // key
txData[7] = 0x52;
txData[8] = 0x46;
txData[9] = 0x43;
txData[10] = 0x40;
txData[11] = 0x40;
txData[12] = 0x53;
txData[13] = 0x42;
txData[14] = 0x41;
txData[15] = 0x52;
txData[16] = 0x62;
txData[17] = 0x45;
txData[18] = 0x52;
txData[19] = 0x48;
txData[20] = 0x43;
txData[21] = 0x53;
DefCAN (CANexchange (txHandle, txData, rxHandle, &rxData, errorInfo, withBSI, 1, 1));
if ((rxData[1] != (txData[2] + 0x40)) ||
(rxData[2] != txData[3]) ||
(rxData[3] != txData[4])) {
CAN_sprintf (txtErr, rxData, 1, 1);
sprintf (errorInfo->description, "Lock : the receiving frame is bad\n%s", txtErr);
defaut = -10;
goto Error;
}
Error:
if (error != 0) {
errorInfo->wCode = error;
}
return defaut;
}
/*===========================================================================*/
int ReadEEpromData_RC5_MAZDA_J64 (
NCTYPE_OBJH txHandle, // CAN Handle to send data to ECU
NCTYPE_OBJH rxHandle, // CAN Handle to receive data from ECU
long address, // start address
long size, // number of byte to read
tabDataS19 *rdData, // data read. Warning : memory must be free after !!!
ERRORINFO *errorInfo, int withBSI)
/* Description : Return data read in EEPROM memory
/*===========================================================================*/
{
int sizeRC5BlocMemory = 248;
int defaut = 0;
long sizeBlock, count = 0;
int withFlush = 1;
while (count < size) {
if ((count + sizeRC5BlocMemory) > size)
sizeBlock = size - count;
else
sizeBlock = sizeRC5BlocMemory;
DefCAN (ReadMemoryByAddress_RC5_MAZDA_J64 (txHandle, rxHandle,
errorInfo, address + count, sizeBlock,
&((*rdData)[count]), withBSI, withFlush));
count += sizeBlock;
withFlush = 0;
}
Error :
return defaut;
}
/*===========================================================================*/
int WriteEEpromData_RC5_MAZDA_J64 (
NCTYPE_OBJH txHandle, // CAN Handle to send data to ECU
NCTYPE_OBJH rxHandle, // CAN Handle to receive data from ECU
tabAddrS19 *tabAddress, // address to write
tabDataS19 *tabData, // data to write
ERRORINFO *errorInfo, int withBSI)
/* Description : Write data in EEPROM memory
/*===========================================================================*/
{
int sizeRC5BlocMemory = 248;
int defaut = 0;
int count;
long indexAdr, sizeBlock;
int withFlush = 1; // for the first dialogue
indexAdr = 1;
sizeBlock = sizeRC5BlocMemory;
while (indexAdr <= (*tabAddress)[0]) {
if (indexAdr+sizeBlock-1 <= (*tabAddress)[0]) { // consecutif data
// printf ("adresse = %x, taille bloc = %d \n", tabAddress[indexAdr], sizeBlock);
if ((tabAddress[indexAdr+sizeBlock-1]-
tabAddress[indexAdr]) == (sizeBlock-1)) {
DefCAN (WriteMemoryByAddress_RC5_MAZDA_J64 (txHandle, rxHandle,
errorInfo, (*tabAddress)[indexAdr], sizeBlock,
&((*tabData)[indexAdr]), withBSI, withFlush));
indexAdr += sizeBlock; // update indexAdr
withFlush = 0; // disable flushing for the next dialogue
sizeBlock = sizeRC5BlocMemory; // for next loop
}
else { // look for a consecutif data block
for (count = 1; count < sizeBlock; count++)
if ((tabAddress[indexAdr+count]-tabAddress[indexAdr]) != count) {
// printf ("delta adresse = %d, cpt %d\n", tabAddress[indexAdr+cpt]-tabAddress[indexAdr], cpt);
sizeBlock = count;
break;
}
}
}
else {
sizeBlock = ((long)tabAddress[0])-indexAdr+1;
}
}
Error :
return defaut;
}
/*****************************************************************************/
/*======= Local functions ========*/
/*****************************************************************************/
// none
/*****************************************************************************/
/*=============================== END =====================================*/
/***************************************************************************