Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init CAN with custom SPI pins #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion mcp_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ byte MCP_CAN::begin(byte speedset)
return ((res == MCP2515_OK) ? CAN_OK : CAN_FAILINIT);
}

/*********************************************************************************************************
** Function name: init
** Descriptions: init can and set SPI pins and CAN speed
*********************************************************************************************************/
byte MCP_CAN::begin(int8_t sclk, int8_t miso, int8_t mosi, int8_t ss, byte speedset)
{

pinMode(SPICS, OUTPUT);
MCP2515_UNSELECT();
SPI.begin(sclk, miso, mosi, ss);
byte res = mcp2515_init(speedset);
return ((res == MCP2515_OK) ? CAN_OK : CAN_FAILINIT);
}

/*********************************************************************************************************
** Function name: init_Mask
** Descriptions: init canid Masks
Expand Down Expand Up @@ -746,9 +760,10 @@ byte MCP_CAN::init_Filt(byte num, byte ext, unsigned long ulData)
*********************************************************************************************************/
byte MCP_CAN::setMsg(unsigned long id, byte ext, byte len, byte rtr, byte *pData)
{
byte tmp = MAX_CHAR_IN_MESSAGE;
ext_flg = ext;
can_id = id;
dta_len = min(len, MAX_CHAR_IN_MESSAGE);
dta_len = min(len, tmp);
rtr = rtr;
for(int i = 0; i<dta_len; i++)
{
Expand Down Expand Up @@ -968,6 +983,56 @@ byte MCP_CAN::isExtendedFrame(void)
return ext_flg;
}

/*********************************************************************************************************
** Function name: getConfiguredMask
** Descriptions: get the configured mask
***********************************************************************************************************/
unsigned long MCP_CAN::getConfiguredMask(byte num)
{
unsigned long mask;
byte ext;
byte mcp_addr = MCP_RXM0SIDH;
if (num == 1)
{
mcp_addr = MCP_RXM1SIDH;
}
mcp2515_read_id(mcp_addr, &ext, &mask);
return mask;
}

/*********************************************************************************************************
** Function name: getConfiguredFilter
** Descriptions: get the configured filter
***********************************************************************************************************/
unsigned long MCP_CAN::getConfiguredFilter(byte num)
{
unsigned long filter;
byte ext;
byte mcp_addr = MCP_RXF0SIDH;
if (num == 1)
{
mcp_addr = MCP_RXF1SIDH;
}
else if (num == 2)
{
mcp_addr = MCP_RXF2SIDH;
}
else if (num == 3)
{
mcp_addr = MCP_RXF3SIDH;
}
else if (num == 4)
{
mcp_addr = MCP_RXF4SIDH;
}
else if (num == 5)
{
mcp_addr = MCP_RXF5SIDH;
}
mcp2515_read_id(mcp_addr, &ext, &filter);
return filter;
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
3 changes: 3 additions & 0 deletions mcp_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class MCP_CAN
public:
MCP_CAN(byte _CS);
byte begin(byte speedset); // init can
byte begin(int8_t sclk, int8_t miso, int8_t mosi, int8_t ss, byte speedset);// init can with SPI pins
byte init_Mask(byte num, byte ext, unsigned long ulData); // init Masks
byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters
byte sendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, byte *buf); // send buf
Expand All @@ -109,6 +110,8 @@ class MCP_CAN
unsigned long getCanId(void); // get can id when receive
byte isRemoteRequest(void); // get RR flag when receive
byte isExtendedFrame(void); // did we recieve 29bit frame?
unsigned long getConfiguredMask(byte num); // get configured mask
unsigned long getConfiguredFilter(byte num); // get configured filter
};

#endif
Expand Down