|
|
View previous topic :: View next topic |
Author |
Message |
jmaudley
Joined: 01 Feb 2011 Posts: 32 Location: 53
|
MCP2515 - trying to set CLKOUT prescaler to clock/4 |
Posted: Thu Sep 11, 2014 2:34 am |
|
|
Hi,
I'm digging through the CCS code 'can_mcp2510' trying to find out where I can modify the CANCTRL register to adjust the CLKOUT freq.
Unfortunately, I'm struggling to find where this is set and I don't really understand the CCS code.
I'm assuming that can_set_mode (CAN_OP_CONFIG); is the function that sets this, but where is it and where is the definition for CAN_OP_CONFIG ?
It says in the comments that some of the defines are in the can_mcp2510.h file which confuses me even more.
I don't really get why can_mcp2510.h is included within can_mcp2510.c.
Also, should I be using can_mcp251X instead of 2510. The CCS CAN dev kit has an MCP2515 chip but uses the 2510 code.
I did a diff on the codes and there are some slight changes to CTR register I think.
Any guidance would be appreciated.
PS - It all works I just need to change the CLKOUT div which I use as the clock for my main PIC. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Sep 11, 2014 3:39 am |
|
|
It's included, since otherwise it wouldn't get read....
On CAN_OP_CONFIG, not quite.
CAN_OP_CONFIG, is the _command_ to put the chip into config mode, to accept the device configuration. It is defined in the .h file, which generates an enum, with values 4,3,2,1, for CAN_OP_CONFIG, CAN_OP_LISTEN, CAN_OP_LOOPBACK, and CAN_OP_NORMAL.
The actual configuration is sent after the chip is switched to this mode.
The key is the can_set_baud function. This is called immediately after this mode is selected.
If you look at the start of this, you see:
new_CNF1.brp=CAN_BRG_PRESCALAR;
which sets the prescaler (why they spell it with an A, don't ask me.....).
and if you look in the .h file, you see:
Code: |
#IFNDEF CAN_BRG_PRESCALAR
#define CAN_BRG_PRESCALAR 4 //baud rate generator prescalar (def: 4) ( Tq = (2 x (PRE + 1))/Fosc )
#ENDIF
|
So it is set to '4', _unless it is already defined_
So all you need to do, is define the value you want, before loading the can file.
So:
Code: |
#define CAN_BRG_PRESCALAR what_you_want
#include <can_mcp2510.c>
|
Putting the number you want, in 'what_you_want'.
The keyword to the chip, is 'compatible'. If you look at the header of the code, you see it says:
MCP2510 (and compatable) (again CCS spelling...). The 2515, is a direct update to the 2510. The 251x, and 2510 files seem to be almost duplicates of one another (no idea why...). |
|
|
jmaudley
Joined: 01 Feb 2011 Posts: 32 Location: 53
|
Still confused |
Posted: Thu Sep 11, 2014 5:51 am |
|
|
Sorry, still a bit confused.
Should it really be the baud rate generation prescaler.
If it is I don't understand how 4 equates to system clock/8.
Shouldn't it be the clkout pin prescaler?
clkpre:2
Also don't understand how 2 equates to divide by 8. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Sep 11, 2014 6:25 am |
|
|
Got you. You want to use this for something.
The driver doesn't actually use it. It is always 'reloaded' with whatever it is already defined as, which gives /8.
Code: |
void can_set_mode(CAN_OP_MODE mode) {
struct struct_CANCTRL old_CANCTRL;
struct struct_CANSTAT new_CANSTAT;
memset(&old_CANCTRL,mcp2510_read(CANCTRL),1);
old_CANCTRL.reqop=mode;
//add this line
old_canctrl.clkpre=2; // /4
mcp2510_write(CANCTRL, (unsigned int8)old_CANCTRL);
do {
memset(&new_CANSTAT,mcp2510_read(CANSTAT),1);
//old_CANCTRL=mcp2510_read(CANCTRL);
} while (new_CANSTAT.opmode != mode);
}
|
Replace the can_set_mode routine in can-mcp2510.c, with the above. |
|
|
jmaudley
Joined: 01 Feb 2011 Posts: 32 Location: 53
|
Working |
Posted: Thu Sep 11, 2014 7:27 am |
|
|
You are a star!!
Thank you. |
|
|
|
|
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
|