Init error when combining CAN and XCP in same port

A free API for the communication with control units based on the Universal Measurement and Calibration Protocol (XCP) by ASAM for Windows®
Locked
CarSanMa
Posts: 4
Joined: Mon 21. Jun 2021, 09:20

Init error when combining CAN and XCP in same port

Post by CarSanMa » Mon 21. Jun 2021, 13:04

Hello,

I am a C# Software Developer and I am having some issues when trying to combine XCP and CAN features in the same port in PCAN-USB Pro FD device. I use the non-free PCAN-API 4 libraries which, theoretically, support this combination.

What I am doing is:

TXCPResult result = XCPApi.InitializeCanChannel(out _XcpChannel, PCANBasic.PCAN_USBBUS1, TPCANBaudrate.PCAN_BAUD_500K); --> Returns "TXCPResult.XCP_ERR_OK"

And then:

StatusErrors status = CanApi4.RegisterClient(PcanDevice.Usb, "Port" + 16, 0, out byte client); --> Returns "StatusErrors.Ok"
status = CanApi4.ConnectToHardware(PcanDevice.Usb, client, _comm_parameters, ref _nethandle); --> Returns "StatusErrors.HardwareInUse"

Where:
_comm_parameters = "hw_handle=16, f_clock=40000000, nom_brp=5, nom_tseg1=11, nom_tseg2=4, nom_sjw=1" --> which sould mean using USBBUS1 with 500K baudrate speed.

If I try to execute these instructions in reverse order, the CanApi4 instructions return "StatusErrors.Ok" but the XCPApi instruction returns "TXCPResult.XCP_ERROR_INVALID_HANDLE".

Maybe I am doing something wrong (or maybe I am not doing something :D ). I would appreciate a lot if someone can give me some support so I can solve this issue.

Thanks a lot in advance!

K.Wagner
Software Development
Software Development
Posts: 796
Joined: Wed 22. Sep 2010, 13:36

Re: Init error when combining CAN and XCP in same port

Post by K.Wagner » Mon 21. Jun 2021, 15:35

Hello,

Note that the bitrates used in XCP when initializing it thought the function

Code: Select all

XCPApi.InitializeCanChannel
are SJA1000 based values (BTR0BTR1 pair of values using a frequency of 8 MHz). Since 8 MHz is not supported by newer CAN transceivers, then you need to use the next supported frequency multiple of 8 to match the same bitrate.

Try using a frequency of 24 MHz for your "_comm_parameters " bitrate string, for instance:

Code: Select all

f_clock=24000000, nom_brp=3, nom_tseg1=13, nom_tseg2=2, nom_sjw=1
If you need help by finding other bitrate parameters, you can use our Bitrate Tool to find those. The tool is free available and can be downloaded using this link: Bit Rate Calculation Tool
Best regards,
Keneth

CarSanMa
Posts: 4
Joined: Mon 21. Jun 2021, 09:20

Re: Init error when combining CAN and XCP in same port

Post by CarSanMa » Tue 22. Jun 2021, 17:23

Hello, thank you for your fast answer!

Following your recommendations, I have used the Bit Rate Calculation Tool and I have used the following "_comm_parameters" combinations:
- "hw_handle=16, f_clock=24000000, nom_brp=3, nom_tseg1=13, nom_tseg2=2, nom_sjw=1".
- "hw_handle=16, f_clock=24000000, nom_brp=3, nom_tseg1=13, nom_tseg2=2, nom_sjw=2"
- "hw_handle=16, f_clock=80000000, nom_brp=10, nom_tseg1=13, nom_tseg2=2, nom_sjw=2"
- "hw_handle=16, f_clock=8000000, nom_brp=1, nom_tseg1=13, nom_tseg2=2, nom_sjw=1"

I have tried all these "_comm_parameters" combinations with the same results commented in the first post. This is:

TXCPResult result = XCPApi.InitializeCanChannel(out _XcpChannel, PCANBasic.PCAN_USBBUS1, TPCANBaudrate.PCAN_BAUD_500K); --> Returns: TXCPResult.XCP_ERR_OK
StatusErrors status = CanApi4.RegisterClient(PcanDevice.Usb, "Port" + 16, 0, out byte client); --> Returns: StatusErrors.Ok
status = CanApi4.ConnectToHardware(PcanDevice.Usb, client, _comm_parameters, ref _nethandle); --> Returns: StatusErrors.HardwareInUse

All _comm_parameters commented above have the same Nominal Sample Point (87,5%) and Nominal Bit Duration (16).

Thank you for your attention!

K.Wagner
Software Development
Software Development
Posts: 796
Joined: Wed 22. Sep 2010, 13:36

Re: Init error when combining CAN and XCP in same port

Post by K.Wagner » Wed 23. Jun 2021, 08:59

Hello,

I have to admit, I concentrated on the bitrate and missed to say something important regarding the function CanApi4.ConnectToHardware. This function always tries to create a net for the conenction so that, indifferently from the bitrate used, it will fail if the hardware is already connected by any other application.
ConnectToHardware_Remarks
ConnectToHardware_Remarks
CanApi4.PNG (34.18 KiB) Viewed 799 times
Trying to connect first your application with a call to CanApi4.ConnectToHardware as you posted will also not work, because the created net is not compatible with those created by PCAN-Basic (API used for CAN communication in XCP). For this reason, the call to XCPApi.InitializeCanChannel will also fail, as you already noted.

I see two ways to achieve what you want:
  1. Connect first the XCP application, and then use CanApi4.ConnectToNet instead of CanApi4.ConnectToHardware, to connect to the net created by the XCP application. You can see the name by using the PCAN Status Display Tool, or
  2. connect first your CanApi4 application, but give as parameter the name for the net that will be created. Be sure the net name is the same as that used by the XCP connection. You can also pass the bitrate as BTR0BTR1 for simplicity, for instance: "name=NetName,hw_handle=16, btr0btr1=0x1C" (500 kBit/s, samp. 87,5%, as used by PCAN-Basic).
Best regards,
Keneth

CarSanMa
Posts: 4
Joined: Mon 21. Jun 2021, 09:20

Re: Init error when combining CAN and XCP in same port

Post by CarSanMa » Wed 23. Jun 2021, 16:56

Hello,

Wether if I use option a) or b) I need PCAN Status Tool software to obtain XCP's created netname. I have tried several random netnames with no success (I also tried "NetName" :D ).

Maybe I am wrong, but I am not able to download this software in the web site. I can find several references to this program, but I have not seen any download link.

I tried to retrieve this netname created by XCP using PCAN-View but it is only possible to see that the channel is already used without additional info.

Thanks a lot for your fast and useful answers.

Best regards,
Carlos.

K.Wagner
Software Development
Software Development
Posts: 796
Joined: Wed 22. Sep 2010, 13:36

Re: Init error when combining CAN and XCP in same port

Post by K.Wagner » Thu 24. Jun 2021, 07:57

Dear,

You are using CanApi4.dll which is part of the professional API, PCAN-Developer 4, that is tied to a license (not for free). This package contains the mentioned tool. Please send us an email to support[at]peak-system.com with your license number and making mention to this topic. Otherwise we will not be able to help on this anymore.

Thanks for your understanding.
Best regards,
Keneth

CarSanMa
Posts: 4
Joined: Mon 21. Jun 2021, 09:20

Re: Init error when combining CAN and XCP in same port

Post by CarSanMa » Tue 29. Jun 2021, 17:26

Hello,

At the moment I wrote the original post, I haven’t installed all the package included in the flash memory stick brought with the non-free license. I had only downloaded the API from this website. That’s why I wasn’t able to find PCAN Status Tool on my computer.

With PCAN Status Tool I was able to see netNames and its naming rules which are related to the USB port involved.

Just to let you know, related to baudrates, I found out that communication parameters (prescalers, tseg1, tseg2, sjw...) of the baudrates included by default in PCAN-View were the ones that worked when trying to combine CAN (non-FD) with XCP. For non-FD communication, I used them instead of the ones shown in Bit Rate Calculation Tool.

These communication parameters, along with the netname knowledge, allowed me to solve my initialization issues.

Thanks a lot for your information and guidelines!

Best regards,
Carlos.

K.Wagner
Software Development
Software Development
Posts: 796
Joined: Wed 22. Sep 2010, 13:36

Re: Init error when combining CAN and XCP in same port

Post by K.Wagner » Wed 30. Jun 2021, 09:04

Hello,

glad to read that the problem is solved.

Regarding the Bitrate values in PCAN-View: yes, you are right. PCAN-View has default bitrates that pass for all devices, also those not being FD capable (SJA1000 register). This is mostly for backward compatibility. I missed to tell you this.

Regarding the Bitrate Tool: this tool does calculate the bitrate in 3 different modes, "CAN Bitrate", "CAN-FD Bitrate" and "SJA1000 Bitrate". The calculation algorithms are different, so in several cases the SJW value doesn't fit when calculating a SJA1000 value with other frequency than 8MHz. For example, here are the values for a 500 kBits as SJA1000 Bitrate (left) and as CAN bitrate at 24 Mhz (right):
Difference between bitrate values
Difference between bitrate values
Bitrates.png (58.84 KiB) Viewed 739 times
For this reason the bitrate returned by the tool is not working in your case. Changing the SJW to 1 solves the problem. A workaround is to calculate the SJA1000 values and multiply the SJA1000 Prescaler by 3 (frequency of 24MHz / 8). For example, the prescaler of a SJA1000 250 kBit bitrate (011Ch) is 2. When using 24 MHz, it would be needed to change it to 6.
SJA1000 vs CAN Bitrate - Prescaler value
SJA1000 vs CAN Bitrate - Prescaler value
Bitrates2.png (47.22 KiB) Viewed 739 times
But I admit, the tool should include these standard values too to avoid misunderstandings. We will check the tool for this. Thanks for bringing this to our attention.

This topic will be closed. Feel free to open another one if you need additional help.
Best regards,
Keneth

Locked