Page 1 of 1

For beginners, how to route one ID to another ID

Posted: Fri 23. Aug 2019, 20:43
by liviu2004
Hi, please bear with me, I need a kick to get the wheel rolling. Yes, beginner in all this.

Problem: I bought a corner lighting module, which looks to the HSCAN of a car, and turns on and off individually each fog light, based on input data engine running and steering angle sensor data. Nice, however, it turned out that the message ID for steering angle data mismatches my car. Maker does not want to support, no reply, nothing.

So I went ahead, reversed engineer my SAS sensor and got the ID and data bytes necessary (thank you PCAN-USB pro for making this easy to me :)). I went to a supported car, reversed engineer the SAS there and got the ID.

It turns out, I only need to change the ID, from 180h to 1E4h, the rest of the message looks the same. I have a PCAN-Router I would like to use. Attached is the schematic of what I want to achieve. Yes, I know I have to have a module which acknowledges the ID messages, but that is a problem for later to think off.

Easy job to setup PCAN-Router, is it? :oops:

Manual reading, all resources of PCAN-router can be accessed by the .a library, which is explained by .h header files.
I will comply and read them by the letter.

However, how do I follow the right steps? I created a folder Steering angle sensor, copied there folders:
.dep
.lst
.obj
.out

Copied all .h files, Flash.Id, hex2bin.exe and makefile.

Is this for a start ok? Are there any folders / files I don't actually need there? What about crt0.S? What is this file used for? Do I always need it?

In terms of writing .c files, is there a specific order, specific file names, how would one start from scratch? I see in the ROUTING example a can_user.c and main.c. Why there are two files? What do I need to put in each?

Sorry for the long text, I have more questions then I can turn my head around at this time. Can anyone give me a kick in the right road?
SAS.png
SAS.png (10.12 KiB) Viewed 17394 times

Re: For beginners, how to route one ID to another ID

Posted: Fri 23. Aug 2019, 22:11
by liviu2004
I'm so far making a main.c file.

I still have to sort out if this is correct and how I set 500 kb/s. And what about the can_user.c

Code: Select all

#include "datatypes.h"
#include "can.h"
#include "can_user.h"
#include "hardware.h"
#include "crc_data.h"


// identifier is needed by PCANFlash.exe -> do not delete
const b8_t Ident[] __attribute__ ((used)) = { "PCAN-Router"};


// info data for PCANFlash.exe
const crc_array_t  C2F_Array __attribute__((section(".C2F_Info"), used)) = {

	.Str = CRC_IDENT_STRING,
	.Version = 0x21,
	.Day = 5,
	.Month = 5,
	.Year = 6,
	.Mode = 1,

	// crc infos are patched during link time by flash.ld
	// crc value is patched by PCANFlash.exe
};


// variables for LED toggle
static u8_t LED_toggleCAN1;
static u8_t LED_toggleCAN2;


// main()
// entry point from crt0.S
int  main (void)
{
	
	// init hardware
	HW_Init();
	
	
	// init CAN
	CAN_UserInit();
	
	
	// Set green LEDs for CAN1 and CAN2
	HW_SetLED ( HW_LED_CAN1, HW_LED_GREEN);
	HW_SetLED ( HW_LED_CAN2, HW_LED_GREEN);
	
		// main loop
	while (1)
	{
		CANMsg_t  RxMsg, TxMsg;
		
		
		// process messages from CAN1
		if ( CAN_UserRead ( CAN_BUS1, &RxMsg) != 0)
		{
			// message received from CAN1
			LED_toggleCAN1 ^= 1;
			
			if ( LED_toggleCAN1)
			{
				HW_SetLED ( HW_LED_CAN1, HW_LED_ORANGE);
			}
			
			else
			{
				HW_SetLED (HW_LED_CAN1, HW_LED_GREEN);
			}
			
			
			// catch ID 180h from CAN1 to modify the ID to 1E4h
			if ( RxMsg.Id == 0x180  &&  RxMsg.Type == CAN_MSG_STANDARD)
			{
				RxMsg.Id = 0x1E4;
				RxMsg.Type = CAN_MSG_STANDARD;
			}			

			// copy message
			TxMsg.Id   = RxMsg.Id;
			TxMsg.Type = RxMsg.Type;
			TxMsg.Len  = RxMsg.Len;

			// send
			CAN_UserWrite ( CAN_BUS2, &TxMsg);
		}
	}
}

Re: For beginners, how to route one ID to another ID

Posted: Sat 24. Aug 2019, 08:41
by liviu2004
Overnight thinking, I guess my chosen code not only changes the ID of the message, but will route also all other messages from can1 to can2, which I don't want. In order to solve my acknowledgment problem, I want now to connect both can1 and can2 to the same car CAN network, so taking messages on can1 and push them back on can2 would not be very smart of me. I've checked and ID 1E4h does not exist in my car, so no ID conflict possibilities there, which kind of make sense, otherwise I would of not need an PCAN-Router.
SAS.png
SAS.png (11.67 KiB) Viewed 17385 times

Re: For beginners, how to route one ID to another ID

Posted: Sat 24. Aug 2019, 09:53
by liviu2004
I hope this would be good, not sure how if syntax exactly works, I thought only one instruction if 1 but I might be wrong.

Code: Select all

// catch ID 180h from CAN1 to modify the ID to 1E5h
if ( RxMsg.Id == 0x180  &&  RxMsg.Type == CAN_MSG_STANDARD)
{
	RxMsg.Id = 0x1E5;
	RxMsg.Type = CAN_MSG_STANDARD;

	// copy message
	TxMsg.Id   = RxMsg.Id;
	TxMsg.Type = RxMsg.Type;
	TxMsg.Len  = RxMsg.Len;

	// send
	CAN_UserWrite ( CAN_BUS2, &TxMsg);
}

Re: For beginners, how to route one ID to another ID

Posted: Mon 26. Aug 2019, 11:39
by M.Maidhof
Hi,

all information on how to build own software for the PCAN-Router module can be found in the manual of the PCAN-Router and it looks like you were able to build your own code.

In your code you copy the RxMsg data to a TxMsg structure, but ID and type are still set to RxMsg...makes no sense IMHO, when you send TxMsg later on CAN2 :?

RxMsg.Id = 0x1E5;
RxMsg.Type = CAN_MSG_STANDARD;

// copy message
TxMsg.Id = RxMsg.Id;
TxMsg.Type = RxMsg.Type;
TxMsg.Len = RxMsg.Len;

// send
CAN_UserWrite ( CAN_BUS2, &TxMsg);

Re: For beginners, how to route one ID to another ID

Posted: Tue 27. Aug 2019, 20:48
by liviu2004
Thanks for your reply and push forward. However, I adapted the code taken from downloads examples:

Code: Select all

// catch ID 120h from CAN1 to modify the ID and message type
			if ( RxMsg.Id == 0x120  &&  RxMsg.Type == CAN_MSG_STANDARD)
			{
				RxMsg.Id = 0x4ED8;
				RxMsg.Type = CAN_MSG_EXTENDED;
			}

Code: Select all

// copy message
			TxMsg.Id   = RxMsg.Id;
			TxMsg.Type = RxMsg.Type;
			TxMsg.Len  = RxMsg.Len;
			
			TxMsg.Data32[0]	= RxMsg.Data32[0];
			TxMsg.Data32[1]	= RxMsg.Data32[1];
				
			// send
			CAN_UserWrite ( CAN_BUS2, &TxMsg);
Are the pcan examples wrong then, because I can't get my head around what have I done different? :oops:

Re: For beginners, how to route one ID to another ID

Posted: Wed 28. Aug 2019, 09:17
by M.Maidhof
Hi,

the example is correct:

// copy message
TxMsg.Id = RxMsg.Id;
TxMsg.Type = RxMsg.Type;
TxMsg.Len = RxMsg.Len;

TxMsg.Data32[0] = RxMsg.Data32[0];
TxMsg.Data32[1] = RxMsg.Data32[1];

// send
CAN_UserWrite ( CAN_BUS2, &TxMsg);

But your code makes no sense! You modify the RxMsg structure, but later you send the TxMsg structure!

regards

Michael

Re: For beginners, how to route one ID to another ID

Posted: Wed 28. Aug 2019, 12:03
by liviu2004
Thanks again, I am slow but I think I got it. Code prepared all for sending except there was nothing in the data part of the message. I believe it is now time for me to put things at work and see how it works.

Re: For beginners, how to route one ID to another ID

Posted: Wed 28. Aug 2019, 21:22
by liviu2004
After some puzzling, more reverse engineering and change in the functional specification of my wishes, I'm happy to announce that PCAN-Router has been successfully loaded with correct firmware, and user expectations are met and exceeded. ;)
There are still declarations and data formats a bit unclear to me, but it is up to me to put the brain to work.
Thanks Peak for making this product available.

Code: Select all

#include "datatypes.h"
#include "can.h"
#include "can_user.h"
#include "hardware.h"
#include "crc_data.h"


// identifier is needed by PCANFlash.exe -> do not delete
const b8_t Ident[] __attribute__ ((used)) = { "PCAN-Router"};


// info data for PCANFlash.exe
const crc_array_t  C2F_Array __attribute__((section(".C2F_Info"), used)) = {

	.Str = CRC_IDENT_STRING,
	.Version = 0x21,
	.Day = 5,
	.Month = 5,
	.Year = 6,
	.Mode = 1,

	// crc infos are patched during link time by flash.ld
	// crc value is patched by PCANFlash.exe
};


// variables for LED toggle
static u8_t LED_toggleCAN1;
static u8_t LED_toggleCAN2;


// main()
// entry point from crt0.S
int  main (void)
{
	
	// init hardware
	HW_Init();
	
	
	// init CAN
	CAN_UserInit();
	
	
	// Set green LEDs for CAN1 and CAN2
	HW_SetLED ( HW_LED_CAN1, HW_LED_GREEN);
	HW_SetLED ( HW_LED_CAN2, HW_LED_GREEN);
	
		// main loop
	while (1)
	{
		CANMsg_t  RxMsg, TxMsg;
		
		
		// process messages from CAN1
		if ( CAN_UserRead ( CAN_BUS1, &RxMsg) != 0)
		{
			//message received from CAN1;
			LED_toggleCAN1 ^= 1;
			
			if ( LED_toggleCAN1)
			{
				HW_SetLED ( HW_LED_CAN1, HW_LED_ORANGE);
			}
			
			else
			{
				HW_SetLED (HW_LED_CAN1, HW_LED_GREEN);
			}
			
			// catch ID 180h from CAN1 to process various changes
			if ( RxMsg.Id == 0x180  &&  RxMsg.Type == CAN_MSG_STANDARD)
			{
							
				//change data byte 00 to 03, 01 to 02, 04 to 01, 01 to 04 and add 2 fixed values
				RxMsg.Data8[3] = RxMsg.Data8[0];
				RxMsg.Data8[2] = RxMsg.Data8[1];
				RxMsg.Data8[0] = 0x84;
				RxMsg.Data8[1] = RxMsg.Data8[4];
				RxMsg.Data8[4] = RxMsg.Data8[1];
				RxMsg.Data8[5] = 0x00;
				
				// copy message and set data length to 6 bytes
				TxMsg.Id = RxMsg.Id;
				TxMsg.Type = RxMsg.Type;
				TxMsg.Len = 6;

				TxMsg.Data32[0] = RxMsg.Data32[0];
				TxMsg.Data32[1] = RxMsg.Data32[1];
				
				// send
				CAN_UserWrite ( CAN_BUS2, &TxMsg);	
				
				// message sent to CAN2
				LED_toggleCAN2 ^= 1;

				if ( LED_toggleCAN2)
				{
					HW_SetLED ( HW_LED_CAN2, HW_LED_ORANGE);
				}
			
				else
				{
					HW_SetLED ( HW_LED_CAN2, HW_LED_GREEN);
				}				
			}			
		}
	}
}