|
|
View previous topic :: View next topic |
Author |
Message |
WalkOver
Joined: 14 May 2021 Posts: 24
|
How to reach 1Mbaud in CAN with dsPIC33EP |
Posted: Tue Jul 06, 2021 5:06 am |
|
|
Hello,
I use can-pic24_dspic33.c driver with a dsPIC33EP64GP502 MCU.
I was able to operate the bus at 125 250 and 500 kbps. But it is impossible to achieve 1Mbaud.
I tried at 50 100 and 140 Mhz using the internal RC oscillator with PLL.
The bus speed remains to 125 kbps with the error "CAN_EC_BAUD_INVALID".
How can I reach 1 Mbaud ? Is there a bug in the driver or do I need to use an external oscillator ?
Thank you for your help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Jul 06, 2021 7:32 am |
|
|
The faster you clock the bus, the lower the oscillator tolerance that is
allowed. At 1MB/sec, you are only allowed a tolerance of 0.49%. The
internal FRC oscillator only guarantees 0.9%....
Just try rebuilding, telling the compiler that you have a crystal oscillator.
If it accepts it, you have your answer. |
|
|
WalkOver
Joined: 14 May 2021 Posts: 24
|
|
Posted: Tue Jul 06, 2021 9:36 am |
|
|
Hello Ttelmah,
Thank you for your help.
I'm not sure I made myself clear.
The error I have is not from the compiler, it is handled by the driver and returned by the function can_set_baud()
I will try this tomorrow on real hardware. There may be a way to test this in simulation but I don't know enough CCS compiler yet for that. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Jul 06, 2021 12:25 pm |
|
|
While I don't have that PIC or use CAN , anymore, I did look at the CCS example program code and saw this.....
Code: | #define CAN_BRG_PRESCALAR 4 //Set CAN Baud Rate to 125K
|
In the comments it says refers to some modules running at 20MHz...
My 'gut' is telling me that in addition to changing PIC clock speed, you also have to 'adjust' the CAN_BRG_PRESCALAR, and perhaps other settings to get higher speeds.
Those that use CAN, hopefully will be able to point out the correct registers and the proper values.
As well, as pointed out by Mr. T. , you'll probably need a real xtal/caps to get a stable, accurate clock.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Jul 07, 2021 12:20 am |
|
|
OK.
The first point is that even if you did set this rate up, it would probably
give issues using the Frc oscillator. It just is not accurate enough. See
at the end though.
The second though it to look at how can_set_baud works.
It counts through BRP values of 1 and upwards, calculating the bit time
in clocks using tBits = (Clock / (BRP * 2)) / Rate
It has to be able to get a division of the master clock that gives a result
for this that is between 8 and 25, or you will get the return BAUD_INVALID.
Now this also has to be met with the specified sample time (default 0.875
of the bit time).
What is happening is that with the specified sample point, and the clock
rates you are trying, it cannot find a divisor that works. Now the 0.875
value implies that there has to be a multiple of 8 clocks per bit. It may
simply be that the clock rate you are specifying does not give a divisor
meeting the parameters. So (for example), if I specify a clock of 100MHz,
this can't give a division by 8*1M, that will work. 100/8 = 12.5. Not an
integer division. 140MHz gives the same problem 140/8 = 17.5.
However selecting a CPU clock of 120MHz gives a legal division. 120/8
= 15.
So change your CPU speed to 120MHz, and the fault should disappear.
However be then aware that your accuracy will not be good enough
unless you add a crystal. Unless you have a very short bus. AN1798
from NXP gives the formulae for calculating the clock accuracies required
at different CAN rates. The actual accuracy needed, get tighter with
higher baud rates, and also with longer busses. The internal oscillator
is comfortably good enough for a 125K, 250K & 500K bus at reasonable
distances, but at 1Mbps, takes you out of tolerance for anything over a
very few meters... |
|
|
WalkOver
Joined: 14 May 2021 Posts: 24
|
|
Posted: Wed Jul 07, 2021 2:47 am |
|
|
Ttelmah wrote: | OK.
The first point is that even if you did set this rate up, it would probably
give issues using the Frc oscillator. It just is not accurate enough. See
at the end though.
The second though it to look at how can_set_baud works.
It counts through BRP values of 1 and upwards, calculating the bit time
in clocks using tBits = (Clock / (BRP * 2)) / Rate
It has to be able to get a division of the master clock that gives a result
for this that is between 8 and 25, or you will get the return BAUD_INVALID.
Now this also has to be met with the specified sample time (default 0.875
of the bit time).
What is happening is that with the specified sample point, and the clock
rates you are trying, it cannot find a divisor that works. Now the 0.875
value implies that there has to be a multiple of 8 clocks per bit. It may
simply be that the clock rate you are specifying does not give a divisor
meeting the parameters. So (for example), if I specify a clock of 100MHz,
this can't give a division by 8*1M, that will work. 100/8 = 12.5. Not an
integer division. 140MHz gives the same problem 140/8 = 17.5.
However selecting a CPU clock of 120MHz gives a legal division. 120/8
= 15.
So change your CPU speed to 120MHz, and the fault should disappear.
However be then aware that your accuracy will not be good enough
unless you add a crystal. Unless you have a very short bus. AN1798
from NXP gives the formulae for calculating the clock accuracies required
at different CAN rates. The actual accuracy needed, get tighter with
higher baud rates, and also with longer busses. The internal oscillator
is comfortably good enough for a 125K, 250K & 500K bus at reasonable
distances, but at 1Mbps, takes you out of tolerance for anything over a
very few meters... |
Hello Ttelmah and thank you very much.
I tried this morning with a 25 Mhz high performance TXO but it obviously didn't work.
I didn't understand the CAN sample time thing so it's much clearer now with your explanation.
At 120Mhz, it works both with internal RC and external oscillator.
I will keep this TXO on my next PCB build
Thank you again ! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Jul 07, 2021 4:24 am |
|
|
just a comment...
hmm, while it works 'on the bench' at 120MHz using the internal, it might fail in the 'real world'. The oscillator frequency will shift due to temperature variations, how much ?... you'll have to read the datasheet. Early PICs were bad, newer ones much better BUT at high baud rates, it doesn't take much of a shift to get 'out of spec'. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Jul 07, 2021 6:27 am |
|
|
Also the allowed tolerance drops with bus length. If you are working on
the bench with (say) a 2m bus, and then go to a real site with a 20m bus,
you may well find problems appear...
25/8 = 3.125
No wonder that didn't work.
Your allowable frequencies are
8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136MHz
Or you have to change the settings for the clock sampling point.
Jay, the tolerance on this Frc is very good indeed. Just +/- 0.9%. This is
good enough for reliable Canbus operation at the lower baud rates and
even over the full temperature range would probably work for the 1Mbps
bus for short busses. However it would fail for something like a full length
car bus at the higher rate for the temperatures these need.... |
|
|
WalkOver
Joined: 14 May 2021 Posts: 24
|
|
Posted: Wed Jul 07, 2021 6:43 am |
|
|
Hello again,
I'm working on very short CAN bus and yes, this MCU has a more stable oscillator than other PIC family....but I bought a 2.5ppm TXO following your remarks and I have already made the modification on my PCB
I made a test in real wold with a 25 Mhz TXO but I bought a 16Mhz one to reach 120Mhz easily with the PLL. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Jul 07, 2021 7:07 am |
|
|
Good.
Nice when things move forwards. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|