CAN_UserRead

Universal Programmable Converter for CAN FD and CAN
Post Reply
EdwardKraft
Posts: 3
Joined: Fri 28. Apr 2023, 11:02

CAN_UserRead

Post by EdwardKraft » Fri 28. Apr 2023, 11:11

Hello,

i am trying to route a CAN Signal to CAN FD. My problem is that the example 10_CAN_FD does not work. I don't see anythin in PCAN-View. It seems like the if-statement

Code: Select all

if ( CAN_UserRead ( CAN_BUS1, &RxMsg) == CAN_ERR_OK)
is not true. If I put

Code: Select all

RxMsg.msgtype = CAN_MSGTYPE_FDF | CAN_MSGTYPE_BRS;
CAN_Write ( CAN_BUS2, &RxMsg);
before the if-statement I can see the message in PCAN-View. So I don't really understand why we need the if-statement. Might there be a problem if I delete it?

Here is my code:

Code: Select all

#include <stdint.h>
#include "can.h"
#include "can_user.h"
#include "hardware.h"


// abstract:
// Here we will convert a classical CAN message to a CAN-FD message.


// identifier is needed by PEAK-Flash.exe -> do not delete
const char Ident[] __attribute__ ((used)) = { "PCAN-Router_FD"};


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





// main_greeting()
// transmit a message at module start
static void  main_greeting ( void)
{
	CANTxMsg_t  Msg;
	
	
	Msg.bufftype = CAN_BUFFER_TX_MSG;
	Msg.dlc      = CAN_LEN8_DLC;
	Msg.msgtype  = CAN_MSGTYPE_STANDARD;
	Msg.id       = 0x123;
	
	Msg.data32[0] = 0x67452301;
	Msg.data32[1] = 0xEFCDAB89;
	
	// overwrite byte 0 with FPGA version
	Msg.data8[0] = FPGA_VERSION;
	
	// Send Msg
	CAN_Write ( CAN_BUS1, &Msg);
}





// main()
// entry point from startup
int  main ( void)
{
	// init hardware and timer 0. Timer 0 is free running
	// with 1 us resolution without any IRQ.
	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);
	
	
	// send the greeting message
	// main_greeting();
	
	// main loop
	while ( 1)
	{
		main_greeting();

		CANRxMsg_t  RxMsg;

		HW_SetLED ( HW_LED_CAN1, HW_LED_RED);

		RxMsg.msgtype = CAN_MSGTYPE_FDF | CAN_MSGTYPE_BRS;
		CAN_Write ( CAN_BUS2, &RxMsg);		
		
		// process messages from CAN1
		if ( CAN_UserRead ( CAN_BUS1, &RxMsg) == CAN_ERR_OK)
		{
			HW_SetLED ( HW_LED_CAN1, HW_LED_ORANGE);
			
			/*
			// 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 and convert it from classical CAN to CAN-FD
			// if ( (RxMsg.id == 0x123 || RxMsg.id == 0x347 || RxMsg.id == 0x346 ) &&  RxMsg.msgtype == CAN_MSGTYPE_STANDARD)
			if ( (RxMsg.id == 0x123) && (RxMsg.msgtype == CAN_MSGTYPE_STANDARD) )
			{				
				HW_SetLED ( HW_LED_CAN1, HW_LED_GREEN);
				// overwrite msgtype
				// RxMsg.msgtype = CAN_MSGTYPE_FDF | CAN_MSGTYPE_BRS;
				RxMsg.msgtype = CAN_MSGTYPE_FDF;
				
				// set dlc to 12 data bytes
				// RxMsg.dlc = CAN_LEN12_DLC;
				
				// add some other data
				// RxMsg.data8[8] = 0x11;
				// RxMsg.data8[9] = 0x22;
				// RxMsg.data8[10] = 0x33;
				// RxMsg.data8[11] = 0x44;

				// forward message to CAN2
			CAN_Write ( CAN_BUS2, &RxMsg);
			HW_SetLED ( HW_LED_CAN2, HW_LED_GREEN);
			}
						
		}
		
		
		// process messages from CAN2
		if ( CAN_UserRead ( CAN_BUS2, &RxMsg) == CAN_ERR_OK)
		{
			// message received from CAN2
			LED_toggleCAN2 ^= 1;

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

			// Test: CAN-FD Signal auf CAN2 erzeugen, in CAN übersetzen und an CAN1 senden
			// RxMsg.msgtype = CAN_MSGTYPE_STANDARD;
			
			// forward message to CAN1
			CAN_Write ( CAN_BUS1, &RxMsg);
		}
	}
}

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: CAN_UserRead

Post by PEAK-Support » Fri 28. Apr 2023, 11:45

You could not simply remove this part

Code: Select all

if ( CAN_UserRead ( CAN_BUS1, &RxMsg) == CAN_ERR_OK)
This check if the lates CAN_UserRead end up with a real CAN Frame. If you ignore it, it could be that a error is happened, or the the queue was simply empty!
And then you send only the latest correct received CAN Frame which is stored in the CAN Message Structure. But not a new received CAN - Frame - Take a closer look if you Network is well terminated, CAN / CAN-FD Bitrate is OK etc. If you do not get a CAN_ERR_OK back - you have NOT received a valid CAN Frame !
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

EdwardKraft
Posts: 3
Joined: Fri 28. Apr 2023, 11:02

Re: CAN_UserRead

Post by EdwardKraft » Fri 28. Apr 2023, 13:39

Hello,

thank you for you answer. Unfortunately I still don't know where the error is. I tried 01_ROUTING, but it still does not work.

I activated the internal termination for both sides. I have connected the CAN1 side to 12V power supply (pin 9) and ground (pin 3). On the CAN2 side I have a PCAN-USB IPEH-002022 connected to the PC. I also tried to add 120 Ohm between Pin 2 and Pin 7 of CAN1 side but that does not work either. Do you have other suggestions?

There is a Upgrade Folder inside Hardware\PCAN-Router_FD. Do I have to upgrade the PCAN-Router FD?

Best regards
Edward

EdwardKraft
Posts: 3
Joined: Fri 28. Apr 2023, 11:02

Re: CAN_UserRead

Post by EdwardKraft » Fri 28. Apr 2023, 15:10

I found that inside the can_user.c file

Code: Select all

if ( CAN_Read ( hBus, rx_buff) == CAN_ERR_OK)
{
	// buffer read from CANx. Check type of buffer.
	switch ( rx_buff->bufftype)
	{
		case CAN_BUFFER_STATUS:
that buffer type is CAN_BUFFER_STATUS.

Code: Select all

if ( /*!busOFF[hBus]  &&*/  rx_buff->status.bus_status)
is not true.

The buffer type should be CAN_BUFFER_RX_MSG. Any ideas why this is not the case?

M.Maidhof
Support
Support
Posts: 1753
Joined: Wed 22. Sep 2010, 14:00

Re: CAN_UserRead

Post by M.Maidhof » Tue 2. May 2023, 10:19

Hi,

when the buffer type is CAN_BUFFER_STATUS, it looks like the CAN controller runs into an error state. Check your bitrate settings again, and adapt them to the exact settings of your connected CAN FD system.

regards

Michael

Post Reply