Page 1 of 3

SPEED CAN-BUS inernal external

Posted: Wed Oct 11, 2017 9:00 pm
by Barmaley_GMN
Hello!
I have a PIC18F2580 with internal (only MCP2551) and one external (MCP2515 and MCP2551) CAN-Buses.
I have a simple program (CAN-retranslator). From the first channel to the second. It loses some frames. Like not enough speed. I increased Osc. from 16 to 40 MHz? but almost nothing was changed.
Was the speed of SPI channel (to external CAN) been increased with Osc?
How can I increase speed of traffic between PIC and CAN transceivers?

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 6:30 am
by QMESAR
Hi.

As CAN is time based communication it is simply not just a question of speeding up the SPI there are a few important information regarding your CAN translator missing.

1 What is the bus load you are trying to connect too(how many messages are on this bus per second)
2 What is the cycle time of the fastest message on the bus
3 are you setting the MASK and filters to mask out the messages of interest

we need a bit more information to help you will some sensible advise :D

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 7:10 am
by Barmaley_GMN
Thanks for your answer!
1. It is very different. I'll put an example of track.
2.Sometime it less then 1 ms (see str. 7-8)
3.Yes and no. I use mask 0 and filter 0 because I don't know the protocol fully, and I have to exclude some frames with exact IDs.
But the speed of CAN-bus isn't so big (95kbps) and I'm sure there are enough resources of this PIC for this task.
And I have one additional question: how I can enable

Code: Select all

RXB0DBEN: Receive Buffer 0 Double-Buffer Enable bit
1 = Receive Buffer 0 overflow will write to Receive Buffer 1
for both CAN channels?

Code: Select all

Time  	          Frame	                               ID     DLC          Data
16:14:48:007	Data frame	Standard frame	000004e8	7	46 0f 0b a4 00 00 01	
16:14:48:007	Data frame	Standard frame	00000188	6	46 02 00 00 00 00	
16:14:48:022	Data frame	Standard frame	00000506	8	16 00 42 16 84 00 00 03	
16:14:48:038	Data frame	Standard frame	000004e8	7	46 0f 0b bf 00 00 01	
16:14:48:085	Data frame	Standard frame	000004e8	7	46 0f 0b f6 00 00 01	
16:14:48:116	Data frame	Standard frame	00000188	6	46 02 00 00 00 00	
16:14:48:132	Data frame	Standard frame	00000501	8	61 00 42 16 01 00 40 01	
16:14:48:132	Data frame	Standard frame	000004e8	7	46 0f 0c 06 00 00 01
16:14:48:178	Data frame	Standard frame	000004e8	7	46 0f 0b b5 00 00 01	
16:14:48:210	Data frame	Standard frame	00000188	6	46 02 00 00 00 00	
16:14:48:225	Data frame	Standard frame	00000506	8	16 00 42 16 81 00 02 00	
16:14:48:241	Data frame	Standard frame	000004e8	7	46 0f 0b 34 00 00 01	
16:14:48:288	Data frame	Standard frame	000004e8	7	46 0f 0b 8c 00 00 01	
16:14:48:303	Data frame	Standard frame	00000501	8	61 00 42 16 01 00 40 01	

Code: Select all

FC_CAN_SPI_CAN_Write_2(FCV_0e702_can2__CAN_RXB0CTRL, 0x24);
FC_CAN_SPI_CAN_Write_1(FCV_0e701_can1__CAN_RXB0CTRL, 0x24
?

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 9:25 am
by LeighM
Hi,
Would it be possible to attach your Flowcode project (.fcfx) file?
This would help answer a few questions about your setup.

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 10:18 am
by Barmaley_GMN
Sure!
Thanks!
111 Fast 2 Buffers 10MHzx4.fcfx
(38.18 KiB) Downloaded 247 times

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 11:11 am
by LeighM
Hi,
Not sure if this is the complete answer, but I noticed a missing check of CAN2 Buffer 1 ...
can2buffer1.jpg
can2buffer1.jpg (31.8 KiB) Viewed 12287 times
edit: To set the rollover
CAN1...

Code: Select all

// Request config mode.
cancon = MX_CAN_REQOP_CONFIG;
while( (canstat & MX_CAN_OP_MODE_BITS) != MX_CAN_REQOP_CONFIG );
rxb0con = 0x24;
CAN2...

Code: Select all

//setup CNF and control registers
FC_CAN_SPI_CAN_Config_2(FCV_0e702_can2__CAN_REQOP_CONFIG);
FC_CAN_SPI_CAN_Write_2(FCV_0e702_can2__CAN_RXB0CTRL, 0x24);

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 11:49 am
by Barmaley_GMN
Yes!
I see it. I rebuilt it many times and made this mistake.
But it doesn't resolve the main problem. I have tried correct variant.
I suppose the problem of missing frames can be in slow speed in communications of PIC and MCP2515. And it can be solved by increasing speed or use the RX(1) buffers when the RX0 is full and new message is receiving.

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 11:51 am
by LeighM
OK. I've just done an edit to my previous post, with code for setting the rollover mode, see if that helps.
btw, the code is to add the rxb0con line into the existing initialization code.

Does the problem happen in any particular direction, CAN1->CAN2, CAN2->CAN1?

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 11:52 am
by Barmaley_GMN
Does the problem happen in any particular direction, CAN1->CAN2, CAN2->CAN1?
Yes. I tried to remove any excludes for any IDs and just resend them. I sow many mistakes. And they was different (added - it was protocol crashing, I think) . But I didn't catch exact differences. And there was a few changes on the hardware board (resistance terminators and some other) because of including scheme differences.
Thanks I'll try your code soon.

Re: SPEED CAN-BUS inernal external

Posted: Thu Oct 12, 2017 9:44 pm
by Barmaley_GMN
Your code works good, thanks!
My program steel have problem. I checked it works without mistakes from Can2(ext) to Can1(int) and in reverse (1 to 2) it looses frames, and
I didn't see any frame from RX buffer(1) (I marked them).
I swapped all MCPs, resolder all wires, but it didn't help.
It looses about 10% Every time quantity of lost frames is different.
I made variable. It counts received frames and writes sum to eeprom. I see it receive all of them. So, I think, the problem is in sending from Can2 (external).
Any ideas?

Re: SPEED CAN-BUS inernal external

Posted: Fri Oct 13, 2017 9:32 am
by LeighM
They were good tests.
Maybe something to try is changing the CAN2 SPI speed and seeing what difference, if any, that makes.

Just after the can2::Initialise(), add a C icon with the following code
Add the following line for faster (x4) SPI

Code: Select all

sspcon = sspcon & 0xFC;
Or add the following lines for slower (/4) SPI

Code: Select all

sspcon = sspcon & 0xFC;
sspcon = sspcon | 2;

Re: SPEED CAN-BUS inernal external

Posted: Fri Oct 13, 2017 9:44 am
by Barmaley_GMN
Thanks!
I will try it.
Did you mean SSPCON1?

Re: SPEED CAN-BUS inernal external

Posted: Fri Oct 13, 2017 10:19 am
by LeighM
Yes, on this device it is sspcon1, but our generic CAL code redefines sspcon as sspcon1, so either will work

Re: SPEED CAN-BUS inernal external

Posted: Fri Oct 13, 2017 9:16 pm
by Barmaley_GMN
Thanks again!
Your code works, but doesn't help.
I made some new experiments and catch regularity. If I make simple program with cycle for 3000 and macro:
CAN2 Set TX ID simple
CAN2 Set TX data
CAN2 sendBuffer
It didn't send half of the all frames.
It sends all of them if I put delay to cycle more then 1200us and it is veeeery bad for my program, but decreases mistakes to 0,075%
This delay can be less more then 2 times if I use only one macros (SendBuffer).
If I choose software mode for CAN2 - it works slowly but the same way.
And CAN1 send everything without any delays. It works perfect!
So I think the main problem in external CAN component. Maybe it doesn't have some checks if the registers ready. And of course maybe I am again on a wrong way)))

Re: SPEED CAN-BUS inernal external

Posted: Fri Oct 13, 2017 11:42 pm
by Barmaley_GMN
mcp2515.c
(12.41 KiB) Downloaded 193 times
main.c
(3.88 KiB) Downloaded 195 times
mcp2515.h
(4.25 KiB) Downloaded 192 times
maybe it will help. It works fine

Re: SPEED CAN-BUS inernal external

Posted: Sat Oct 14, 2017 5:52 pm
by LeighM
Maybe it doesn't have some checks if the registers ready.
You could be right. I'll check on Monday.
Thanks,

Re: SPEED CAN-BUS inernal external

Posted: Mon Oct 16, 2017 9:33 am
by LeighM
Hi,
I've done a quick macro CheckTx, attached, that you can import into your project.
This can be used to check if the Tx buffer is busy, maybe test buffer 0 and if busy use 1 or 2?
can2busy.jpg
can2busy.jpg (33.17 KiB) Viewed 12211 times

Re: SPEED CAN-BUS inernal external

Posted: Mon Oct 16, 2017 11:36 am
by Barmaley_GMN
Thanks!
I'll be able to check it close to next weekend.
I tried to change TX buffers in cycle (0-1-0-1...) it didn't help.
And now, there are rollover RX0->RX1 works in my program. It uses TX0 and TX1 and has delays after every sending buffer 1200us. It isn't enough. And I see near 4 lost messages from 5450 sample from real CAN-BUS. If I remove or just make delays shorter I see more mistakes. I can try to use var. and use 3 TX buffers, but I think there is should be more smart solution.

Re: SPEED CAN-BUS inernal external

Posted: Mon Oct 16, 2017 11:54 am
by LeighM
And I try to change TX buffers in cycle (0-1-0-1...) it didn't help.
Oh, that sounds like we might be barking up the wrong tree then.
Are both CAN buses running at the same speed?
On the CAN2 transmit, are there any gaps between transmissions?
Are messages being acknowledged by a receiver?
I just wonder if there are auto re-transmissions going on?

Re: SPEED CAN-BUS inernal external

Posted: Mon Oct 16, 2017 1:32 pm
by Barmaley_GMN
There can be some language barriers.
If I understand You:
1. Both CAN-BUSes work on the same speed (95,238kbps). They work with different resonators (40MHz CAN1 and 20Mhz CAN2), but it doesn't matter. Microchip CAN Bit Timing Calculator got data for CNFn/BRGCONn registers.
2. I didn't catch gaps. But if delay not enough program skips some of messages.
3. I 'm not sure about acknowledging but both CAN have the same components since MCP2551 and their's surroundings, and as I said before CAN1 sent everything without delays and losses (skips).

Re: SPEED CAN-BUS inernal external

Posted: Wed Oct 18, 2017 12:05 pm
by LeighM
I noticed that in the MCP code there is a very small delay between setting up the data and sending.
Might be worth trying adding a very short delay between can2::SetTxData() and can2::SendBuffer()

Also might be worth setting an output port at the start of the can2 setup, then clear just after the send so that you can see (on an oscilloscope) how long the processes takes relative to the incoming CAN messages.

Re: SPEED CAN-BUS inernal external

Posted: Wed Oct 18, 2017 12:44 pm
by Barmaley_GMN
LeighM wrote:I noticed that in the MCP code there is a very small delay between setting up the data and sending.
Might be worth trying adding a very short delay between can2::SetTxData() and can2::SendBuffer()
I did it. And didn't catch the difference. If sum of delays less then 1200us - I see lost frames.
LeighM wrote:Also might be worth setting an output port at the start of the can2 setup, then clear just after the send so that you can see (on an oscilloscope) how long the processes takes relative to the incoming CAN messages.
I have one port for LED. I can connect my logic analyzer and catch the period. But I don't understand you: which exact process I need to check: setup (macro initialisation or set TX_Data... buffer). Or I can check it by ChipSelect pin...

Re: SPEED CAN-BUS inernal external

Posted: Wed Oct 18, 2017 12:53 pm
by Barmaley_GMN
I need to make special board for debugging code )))))

Re: SPEED CAN-BUS inernal external

Posted: Mon Oct 23, 2017 6:23 pm
by Barmaley_GMN
Some new experiments
The first image for internal CAN1
Internal
Internal
Int_Can!.jpg (142.85 KiB) Viewed 12134 times
Next for external CAN2 - used 3 TX buffers 1->2->0->1...
Ext_Can2(123).jpg
Ext_Can2(123).jpg (146.47 KiB) Viewed 12134 times
111 CAN1 send 10MHzx4.fcfx
(18.84 KiB) Downloaded 201 times

Re: SPEED CAN-BUS inernal external

Posted: Mon Oct 23, 2017 6:28 pm
by Barmaley_GMN
Next with one TX buffer (1-1-1)
Ext_Can2(1-1-1).jpg
Ext_Can2(1-1-1).jpg (147.3 KiB) Viewed 12134 times
Next one like previous but added faster SPI (x4)
Ext_Can2(1-1-1)+faster_SPIx4.jpg
Ext_Can2(1-1-1)+faster_SPIx4.jpg (98.07 KiB) Viewed 12134 times
If I add 2us delays after every CAN macros - it doesn't matter the result on the screen is the same.
I made some tests for every variant I have seen different periods but the same Count of received frames.