From 9a0c458948d81c2878435c5d55a1fd82589490d4 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 00:24:04 -0500 Subject: [PATCH 01/40] Work In Progess! SPICS to MCPCS MODE_NORMAL to mcpMode Support to change operational mode of MCP2515 (sleep, listen only, etc) Support to enable No Mask/Filter, Standard IDs Only, Extended IDs Only, or Both. Support for 20MHz (Need to add constants to [..]dfs.h file!) --- mcp_can.cpp | 310 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 213 insertions(+), 97 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index aa8e478..788fd65 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -1,8 +1,9 @@ /* mcp_can.cpp 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2014 Copyright (c) Cory J. Fowler All Rights Reserved. - Author:Loovee + Author: Loovee Contributor: Cory J. Fowler 2014-1-16 This library is free software; you can redistribute it and/or @@ -132,6 +133,17 @@ INT8U MCP_CAN::mcp2515_readStatus(void) return i; } +/********************************************************************************************************* +** Function name: setMode +** Descriptions: set control mode via sketch +*********************************************************************************************************/ +INT8U MCP_CAN::setMode(const INT8U newmode) +{ + mcpMode = newmode + mcp2515_setCANCTRL_Mode(mcpMode) + +} + /********************************************************************************************************* ** Function name: mcp2515_setCANCTRL_Mode ** Descriptions: set control mode @@ -158,84 +170,174 @@ INT8U MCP_CAN::mcp2515_setCANCTRL_Mode(const INT8U newmode) ** Function name: mcp2515_configRate ** Descriptions: set boadrate *********************************************************************************************************/ -INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed) +INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) { INT8U set, cfg1, cfg2, cfg3; set = 1; - switch (canSpeed) + switch (canClock) { - case (CAN_5KBPS): - cfg1 = MCP_16MHz_5kBPS_CFG1; - cfg2 = MCP_16MHz_5kBPS_CFG2; - cfg3 = MCP_16MHz_5kBPS_CFG3; - break; - - case (CAN_10KBPS): - cfg1 = MCP_16MHz_10kBPS_CFG1; - cfg2 = MCP_16MHz_10kBPS_CFG2; - cfg3 = MCP_16MHz_10kBPS_CFG3; - break; - - case (CAN_20KBPS): - cfg1 = MCP_16MHz_20kBPS_CFG1; - cfg2 = MCP_16MHz_20kBPS_CFG2; - cfg3 = MCP_16MHz_20kBPS_CFG3; - break; - - case (CAN_40KBPS): - cfg1 = MCP_16MHz_40kBPS_CFG1; - cfg2 = MCP_16MHz_40kBPS_CFG2; - cfg3 = MCP_16MHz_40kBPS_CFG3; - break; - - case (CAN_50KBPS): - cfg1 = MCP_16MHz_50kBPS_CFG1; - cfg2 = MCP_16MHz_50kBPS_CFG2; - cfg3 = MCP_16MHz_50kBPS_CFG3; - break; - - case (CAN_80KBPS): - cfg1 = MCP_16MHz_80kBPS_CFG1; - cfg2 = MCP_16MHz_80kBPS_CFG2; - cfg3 = MCP_16MHz_80kBPS_CFG3; - break; - - case (CAN_100KBPS): /* 100KBPS */ - cfg1 = MCP_16MHz_100kBPS_CFG1; - cfg2 = MCP_16MHz_100kBPS_CFG2; - cfg3 = MCP_16MHz_100kBPS_CFG3; - break; - - case (CAN_125KBPS): - cfg1 = MCP_16MHz_125kBPS_CFG1; - cfg2 = MCP_16MHz_125kBPS_CFG2; - cfg3 = MCP_16MHz_125kBPS_CFG3; - break; - - case (CAN_200KBPS): - cfg1 = MCP_16MHz_200kBPS_CFG1; - cfg2 = MCP_16MHz_200kBPS_CFG2; - cfg3 = MCP_16MHz_200kBPS_CFG3; - break; - - case (CAN_250KBPS): - cfg1 = MCP_16MHz_250kBPS_CFG1; - cfg2 = MCP_16MHz_250kBPS_CFG2; - cfg3 = MCP_16MHz_250kBPS_CFG3; + case (16MHZ): + switch (canSpeed) + { + case (CAN_5KBPS): /* 5KBPS */ + cfg1 = MCP_16MHz_5kBPS_CFG1; + cfg2 = MCP_16MHz_5kBPS_CFG2; + cfg3 = MCP_16MHz_5kBPS_CFG3; + break; + + case (CAN_10KBPS): /* 10KBPS */ + cfg1 = MCP_16MHz_10kBPS_CFG1; + cfg2 = MCP_16MHz_10kBPS_CFG2; + cfg3 = MCP_16MHz_10kBPS_CFG3; + break; + + case (CAN_20KBPS): /* 20KBPS */ + cfg1 = MCP_16MHz_20kBPS_CFG1; + cfg2 = MCP_16MHz_20kBPS_CFG2; + cfg3 = MCP_16MHz_20kBPS_CFG3; + break; + + case (CAN_40KBPS): /* 40KBPS */ + cfg1 = MCP_16MHz_40kBPS_CFG1; + cfg2 = MCP_16MHz_40kBPS_CFG2; + cfg3 = MCP_16MHz_40kBPS_CFG3; + break; + + case (CAN_50KBPS): /* 50KBPS */ + cfg1 = MCP_16MHz_50kBPS_CFG1; + cfg2 = MCP_16MHz_50kBPS_CFG2; + cfg3 = MCP_16MHz_50kBPS_CFG3; + break; + + case (CAN_80KBPS): /* 80KBPS */ + cfg1 = MCP_16MHz_80kBPS_CFG1; + cfg2 = MCP_16MHz_80kBPS_CFG2; + cfg3 = MCP_16MHz_80kBPS_CFG3; + break; + + case (CAN_100KBPS): /* 100KBPS */ + cfg1 = MCP_16MHz_100kBPS_CFG1; + cfg2 = MCP_16MHz_100kBPS_CFG2; + cfg3 = MCP_16MHz_100kBPS_CFG3; + break; + + case (CAN_125KBPS): /* 125KBPS */ + cfg1 = MCP_16MHz_125kBPS_CFG1; + cfg2 = MCP_16MHz_125kBPS_CFG2; + cfg3 = MCP_16MHz_125kBPS_CFG3; + break; + + case (CAN_200KBPS): /* 200KBPS */ + cfg1 = MCP_16MHz_200kBPS_CFG1; + cfg2 = MCP_16MHz_200kBPS_CFG2; + cfg3 = MCP_16MHz_200kBPS_CFG3; + break; + + case (CAN_250KBPS): /* 250KBPS */ + cfg1 = MCP_16MHz_250kBPS_CFG1; + cfg2 = MCP_16MHz_250kBPS_CFG2; + cfg3 = MCP_16MHz_250kBPS_CFG3; + break; + + case (CAN_500KBPS): /* 500KBPS */ + cfg1 = MCP_16MHz_500kBPS_CFG1; + cfg2 = MCP_16MHz_500kBPS_CFG2; + cfg3 = MCP_16MHz_500kBPS_CFG3; + break; + + case (CAN_1000KBPS): /* 1MBPS */ + cfg1 = MCP_16MHz_1000kBPS_CFG1; + cfg2 = MCP_16MHz_1000kBPS_CFG2; + cfg3 = MCP_16MHz_1000kBPS_CFG3; + break; + + default: + set = 0; + break; + } break; - - case (CAN_500KBPS): - cfg1 = MCP_16MHz_500kBPS_CFG1; - cfg2 = MCP_16MHz_500kBPS_CFG2; - cfg3 = MCP_16MHz_500kBPS_CFG3; + + case (20MHZ): + switch (canSpeed) + { + case (CAN_5KBPS): /* 5KBPS */ + cfg1 = MCP_20MHz_5kBPS_CFG1; + cfg2 = MCP_20MHz_5kBPS_CFG2; + cfg3 = MCP_20MHz_5kBPS_CFG3; + break; + + case (CAN_10KBPS): /* 10KBPS */ + cfg1 = MCP_20MHz_10kBPS_CFG1; + cfg2 = MCP_20MHz_10kBPS_CFG2; + cfg3 = MCP_20MHz_10kBPS_CFG3; + break; + + case (CAN_20KBPS): /* 20KBPS */ + cfg1 = MCP_20MHz_20kBPS_CFG1; + cfg2 = MCP_20MHz_20kBPS_CFG2; + cfg3 = MCP_20MHz_20kBPS_CFG3; + break; + + case (CAN_40KBPS): /* 40KBPS */ + cfg1 = MCP_20MHz_40kBPS_CFG1; + cfg2 = MCP_20MHz_40kBPS_CFG2; + cfg3 = MCP_20MHz_40kBPS_CFG3; + break; + + case (CAN_50KBPS): /* 50KBPS */ + cfg1 = MCP_20MHz_50kBPS_CFG1; + cfg2 = MCP_20MHz_50kBPS_CFG2; + cfg3 = MCP_20MHz_50kBPS_CFG3; + break; + + case (CAN_80KBPS): /* 80KBPS */ + cfg1 = MCP_20MHz_80kBPS_CFG1; + cfg2 = MCP_20MHz_80kBPS_CFG2; + cfg3 = MCP_20MHz_80kBPS_CFG3; + break; + + case (CAN_100KBPS): /* 100KBPS */ + cfg1 = MCP_20MHz_100kBPS_CFG1; + cfg2 = MCP_20MHz_100kBPS_CFG2; + cfg3 = MCP_20MHz_100kBPS_CFG3; + break; + + case (CAN_125KBPS): /* 125KBPS */ + cfg1 = MCP_20MHz_125kBPS_CFG1; + cfg2 = MCP_20MHz_125kBPS_CFG2; + cfg3 = MCP_20MHz_125kBPS_CFG3; + break; + + case (CAN_200KBPS): /* 200KBPS */ + cfg1 = MCP_20MHz_200kBPS_CFG1; + cfg2 = MCP_20MHz_200kBPS_CFG2; + cfg3 = MCP_20MHz_200kBPS_CFG3; + break; + + case (CAN_250KBPS): /* 250KBPS */ + cfg1 = MCP_20MHz_250kBPS_CFG1; + cfg2 = MCP_20MHz_250kBPS_CFG2; + cfg3 = MCP_20MHz_250kBPS_CFG3; + break; + + case (CAN_500KBPS): /* 500KBPS */ + cfg1 = MCP_20MHz_500kBPS_CFG1; + cfg2 = MCP_20MHz_500kBPS_CFG2; + cfg3 = MCP_20MHz_500kBPS_CFG3; + break; + + case (CAN_1000KBPS): /* 1MBPS */ + cfg1 = MCP_20MHz_1000kBPS_CFG1; + cfg2 = MCP_20MHz_1000kBPS_CFG2; + cfg3 = MCP_20MHz_1000kBPS_CFG3; + break; + + default: + set = 0; + break; + } break; - case (CAN_1000KBPS): - cfg1 = MCP_16MHz_1000kBPS_CFG1; - cfg2 = MCP_16MHz_1000kBPS_CFG2; - cfg3 = MCP_16MHz_1000kBPS_CFG3; - break; - default: set = 0; break; @@ -298,7 +400,7 @@ void MCP_CAN::mcp2515_initCANBuffers(void) ** Function name: mcp2515_init ** Descriptions: init the device *********************************************************************************************************/ -INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp2515init */ +INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const INT8U canClock) /* mcp2515init */ { INT8U res; @@ -309,24 +411,24 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp25 if(res > 0) { #if DEBUG_MODE - Serial.print("Enter setting mode fall\r\n"); + Serial.print("Entering Configuration Mode Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("Enter setting mode success \r\n"); + Serial.print("Entering Configuration Mode Successful!!!\r\n"); #endif /* set boadrate */ - if(mcp2515_configRate(canSpeed)) + if(mcp2515_configRate(canSpeed, canClock)) { #if DEBUG_MODE - Serial.print("set rate fall!!\r\n"); + Serial.print("Setting Baudrate Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("set rate success!!\r\n"); + Serial.print("Setting Baudrate Successful!!!\r\n"); #endif if ( res == MCP2515_OK ) { @@ -337,40 +439,54 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp25 /* interrupt mode */ mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); -#if (DEBUG_RXANY==1) - /* enable both receive-buffers */ - /* to receive any message */ - /* and enable rollover */ +switch(canIDMode); +{ + case (ANY): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_ANY); -#else - /* enable both receive-buffers */ - /* to receive messages */ - /* with std. and ext. identifie */ - /* rs */ - /* and enable rollover */ + break; + + case (STD): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STD); + break; + + case (EXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_EXT); + break; + + case (STDEXT): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_STDEXT); -#endif - /* enter normal mode */ - res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + break; +} + + + res = mcp2515_setCANCTRL_Mode(mcpMode); if(res) { #if DEBUG_MODE - Serial.print("Enter Normal Mode Fall!!\r\n"); + Serial.print("Returning to Previous Mode Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("Enter Normal Mode Success!!\r\n"); + Serial.print("Returning to Previous Mode Successful!!!\r\n"); #endif } @@ -518,8 +634,8 @@ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get N *********************************************************************************************************/ MCP_CAN::MCP_CAN(INT8U _CS) { - SPICS = _CS; - pinMode(SPICS, OUTPUT); + MCPCS = _CS; + pinMode(MCPCS, OUTPUT); MCP2515_UNSELECT(); } @@ -564,7 +680,7 @@ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) } else res = MCP2515_FAIL; - res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + res = mcp2515_setCANCTRL_Mode(mcpMode); if(res > 0){ #if DEBUG_MODE Serial.print("Enter normal mode fall\r\n"); @@ -626,7 +742,7 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) res = MCP2515_FAIL; } - res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + res = mcp2515_setCANCTRL_Mode(mcpMode); if(res > 0) { #if DEBUG_MODE From c2a6a988541b734e3873876a94193036d52a9437 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 02:17:05 -0500 Subject: [PATCH 02/40] Work In Progress!! SPICS to MCPCS MODE_NORMAL to mcpMode Support to change operational mode of MCP2515 (sleep, listen only, etc) Support to enable No Mask/Filter, Standard IDs Only, Extended IDs Only, or Both. Support for 20MHz. readMsgBuf returns ID. --- mcp_can.cpp | 106 ++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index 788fd65..4e910a5 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -137,9 +137,9 @@ INT8U MCP_CAN::mcp2515_readStatus(void) ** Function name: setMode ** Descriptions: set control mode via sketch *********************************************************************************************************/ -INT8U MCP_CAN::setMode(const INT8U newmode) +INT8U MCP_CAN::setMode(const INT8U opMode) { - mcpMode = newmode + mcpMode = opMode mcp2515_setCANCTRL_Mode(mcpMode) } @@ -260,24 +260,6 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) case (20MHZ): switch (canSpeed) { - case (CAN_5KBPS): /* 5KBPS */ - cfg1 = MCP_20MHz_5kBPS_CFG1; - cfg2 = MCP_20MHz_5kBPS_CFG2; - cfg3 = MCP_20MHz_5kBPS_CFG3; - break; - - case (CAN_10KBPS): /* 10KBPS */ - cfg1 = MCP_20MHz_10kBPS_CFG1; - cfg2 = MCP_20MHz_10kBPS_CFG2; - cfg3 = MCP_20MHz_10kBPS_CFG3; - break; - - case (CAN_20KBPS): /* 20KBPS */ - cfg1 = MCP_20MHz_20kBPS_CFG1; - cfg2 = MCP_20MHz_20kBPS_CFG2; - cfg3 = MCP_20MHz_20kBPS_CFG3; - break; - case (CAN_40KBPS): /* 40KBPS */ cfg1 = MCP_20MHz_40kBPS_CFG1; cfg2 = MCP_20MHz_40kBPS_CFG2; @@ -406,6 +388,8 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I INT8U res; mcp2515_reset(); + + mcpMode = MODE_LISTENONLY; res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0) @@ -439,39 +423,46 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I /* interrupt mode */ mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); -switch(canIDMode); -{ - case (ANY): - mcp2515_modifyRegister(MCP_RXB0CTRL, - MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); - mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, - MCP_RXB_RX_ANY); - break; - - case (STD): - mcp2515_modifyRegister(MCP_RXB0CTRL, - MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); - mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, - MCP_RXB_RX_STD); - break; - - case (EXT): - mcp2515_modifyRegister(MCP_RXB0CTRL, - MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); - mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, - MCP_RXB_RX_EXT); - break; - - case (STDEXT): - mcp2515_modifyRegister(MCP_RXB0CTRL, - MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); - mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, - MCP_RXB_RX_STDEXT); - break; + switch(canIDMode) + { + case (ANY): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_ANY); + break; + + case (STD): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STD); + break; + + case (EXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_EXT); + break; + + case (STDEXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STDEXT); + break; + + default: +#if DEBUG_MODE + Serial.print("`Setting ID Mode Failure...\r\n"); +#endif + return MCP2515_FAIL; + break; } @@ -643,12 +634,12 @@ MCP_CAN::MCP_CAN(INT8U _CS) ** Function name: init ** Descriptions: init can and set speed *********************************************************************************************************/ -INT8U MCP_CAN::begin(INT8U speedset) +INT8U MCP_CAN::begin(INT8U idmodeset, INT8U speedset, INT8U clockset) { INT8U res; SPI.begin(); - res = mcp2515_init(speedset); + res = mcp2515_init(idmodeset, speedset, clockset); if (res == MCP2515_OK) return CAN_OK; else return CAN_FAILINIT; } @@ -867,9 +858,10 @@ INT8U MCP_CAN::readMsg() ** Function name: readMsgBuf ** Descriptions: read message buf *********************************************************************************************************/ -INT8U MCP_CAN::readMsgBuf(INT8U *len, INT8U buf[]) +INT8U MCP_CAN::readMsgBuf(INT32U *id, INT8U *len, INT8U buf[]) { readMsg(); + *id = m_nID; *len = m_nDlc; for(int i = 0; i Date: Wed, 22 Jan 2014 02:19:04 -0500 Subject: [PATCH 03/40] Work In Progress! SPICS to MCPCS Support to change operational mode of MCP2515 (sleep, listen only, etc) Support to enable No Mask/Filter, Standard IDs Only, Extended IDs Only, or Both. Support for 20MHz. --- mcp_can.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/mcp_can.h b/mcp_can.h index ff2fd48..27717df 100644 --- a/mcp_can.h +++ b/mcp_can.h @@ -1,6 +1,7 @@ /* mcp_can.h 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2014 Copyright (c) Cory J. Fowler All Rights Reserved. Author:Loovee Contributor: Cory J. Fowler @@ -38,7 +39,9 @@ class MCP_CAN INT8U m_nDta[MAX_CHAR_IN_MESSAGE]; /* data */ INT8U m_nRtr; /* rtr */ INT8U m_nfilhit; - INT8U SPICS; + INT8U MCPCS; + INT8U mcpMode; + /* * mcp2515 driver function @@ -68,14 +71,18 @@ class MCP_CAN INT8U mcp2515_readStatus(void); /* read mcp2515's Status */ INT8U mcp2515_setCANCTRL_Mode(const INT8U newmode); /* set mode */ - INT8U mcp2515_configRate(const INT8U canSpeed); /* set boadrate */ - INT8U mcp2515_init(const INT8U canSpeed); /* mcp2515init */ + INT8U mcp2515_configRate(const INT8U canSpeed, /* set baudrate */ + const INT8U canClock); + + INT8U mcp2515_init(const INT8U canIDMode, /* mcp2515init */ + const INT8U canSpeed, + const INT8U canClock); void mcp2515_write_id( const INT8U mcp_addr, /* write can id */ const INT8U ext, const INT32U id ); - void mcp2515_read_id( const INT8U mcp_addr, /* read can id */ + void mcp2515_read_id( const INT8U mcp_addr, /* read can id */ INT8U* ext, INT32U* id ); @@ -95,14 +102,15 @@ class MCP_CAN public: MCP_CAN(INT8U _CS); - INT8U begin(INT8U speedset); /* init can */ + INT8U begin(INT8U idmodeset, INT8U speedset, INT8U clockset); /* init can */ INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); /* init Masks */ INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); /* init filters */ + INT8U setMode(INT8U opMode); /* Sets operational mode */ INT8U sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf); /* send buf */ - INT8U readMsgBuf(INT8U *len, INT8U *buf); /* read buf */ + INT8U readMsgBuf(INT32U *id, INT8U *len, INT8U *buf); /* read buf */ INT8U checkReceive(void); /* if something received */ INT8U checkError(void); /* if something error */ - INT32U getCanId(void); /* get can id when receive */ + INT32U getCanId(void); /* Will be depreciated soon... */ }; extern MCP_CAN CAN; From 5ab7ffc11b997a2c1d09d55de18951a5bdfd52ea Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 02:19:36 -0500 Subject: [PATCH 04/40] Work In Progress!! SPICS to MCPCS Support to change operational mode of MCP2515 (sleep, listen only, etc) Support to enable No Mask/Filter, Standard IDs Only, Extended IDs Only, or Both. Support for 20MHz. readMsgBuf returns ID. --- mcp_can_dfs.h | 62 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/mcp_can_dfs.h b/mcp_can_dfs.h index 4d6d848..4213f89 100644 --- a/mcp_can_dfs.h +++ b/mcp_can_dfs.h @@ -1,6 +1,7 @@ /* mcp_can_dfs.h 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2014 Copyright (c) Cory J. Fowler All Rights Reserved. Author:Loovee Contributor: Cory J. Fowler @@ -36,7 +37,7 @@ #endif // if print debug information -#define DEBUG_MODE 0 +#define DEBUG_MODE 1 /* * Begin mt @@ -51,6 +52,11 @@ #define MCP_DLC_MASK 0x0F /* 4 LSBits */ #define MCP_RTR_MASK 0x40 /* (1<<6) Bit 6 */ +#define STDEXT 0 /* Standard and Extended */ +#define STD 1 /* Standard IDs ONLY */ +#define EXT 2 /* Extended IDs ONLY */ +#define ANY 3 /* Disables Masks and Filters */ + #define MCP_RXB_RX_ANY 0x60 #define MCP_RXB_RX_EXT 0x40 #define MCP_RXB_RX_STD 0x20 @@ -91,7 +97,6 @@ /* * Define MCP2515 register addresses */ - #define MCP_RXF0SIDH 0x00 #define MCP_RXF0SIDL 0x01 #define MCP_RXF0EID8 0x02 @@ -154,7 +159,6 @@ /* * Define SPI Instruction Set */ - #define MCP_WRITE 0x02 #define MCP_READ 0x03 @@ -183,7 +187,6 @@ /* * CANCTRL Register Values */ - #define MODE_NORMAL 0x00 #define MODE_SLEEP 0x20 #define MODE_LOOPBACK 0x40 @@ -204,7 +207,6 @@ /* * CNF1 Register Values */ - #define SJW1 0x00 #define SJW2 0x40 #define SJW3 0x80 @@ -214,7 +216,6 @@ /* * CNF2 Register Values */ - #define BTLMODE 0x80 #define SAMPLE_1X 0x00 #define SAMPLE_3X 0x40 @@ -223,7 +224,6 @@ /* * CNF3 Register Values */ - #define SOF_ENABLE 0x80 #define SOF_DISABLE 0x00 #define WAKFIL_ENABLE 0x40 @@ -233,7 +233,6 @@ /* * CANINTF Register Bits */ - #define MCP_RX0IF 0x01 #define MCP_RX1IF 0x02 #define MCP_TX0IF 0x04 @@ -243,6 +242,10 @@ #define MCP_WAKIF 0x40 #define MCP_MERRF 0x80 + +#define 16MHZ 1 +#define 20MHZ 0 + /* * speed 16M */ @@ -294,6 +297,44 @@ #define MCP_16MHz_5kBPS_CFG2 (0xFF) #define MCP_16MHz_5kBPS_CFG3 (0x87) +/* + * speed 20M + */ +#define MCP_20MHz_1000kBPS_CFG1 (0x00) +#define MCP_20MHz_1000kBPS_CFG2 (0xD9) +#define MCP_20MHz_1000kBPS_CFG3 (0x82) + +#define MCP_20MHz_500kBPS_CFG1 (0x00) +#define MCP_20MHz_500kBPS_CFG2 (0xFA) +#define MCP_20MHz_500kBPS_CFG3 (0x87) + +#define MCP_20MHz_250kBPS_CFG1 (0x41) +#define MCP_20MHz_250kBPS_CFG2 (0xFB) +#define MCP_20MHz_250kBPS_CFG3 (0x86) + +#define MCP_20MHz_200kBPS_CFG1 (0x01) +#define MCP_20MHz_200kBPS_CFG2 (0xFF) +#define MCP_20MHz_200kBPS_CFG3 (0x87) + +#define MCP_20MHz_125kBPS_CFG1 (0x03) +#define MCP_20MHz_125kBPS_CFG2 (0xFA) +#define MCP_20MHz_125kBPS_CFG3 (0x87) + +#define MCP_20MHz_100kBPS_CFG1 (0x04) +#define MCP_20MHz_100kBPS_CFG2 (0xFA) +#define MCP_20MHz_100kBPS_CFG3 (0x87) + +#define MCP_20MHz_80kBPS_CFG1 (0x04) +#define MCP_20MHz_80kBPS_CFG2 (0xFF) +#define MCP_20MHz_80kBPS_CFG3 (0x87) + +#define MCP_20MHz_50kBPS_CFG1 (0x09) +#define MCP_20MHz_50kBPS_CFG2 (0xFA) +#define MCP_20MHz_50kBPS_CFG3 (0x87) + +#define MCP_20MHz_40kBPS_CFG1 (0x09) +#define MCP_20MHz_40kBPS_CFG2 (0xFF) +#define MCP_20MHz_40kBPS_CFG3 (0x87) #define MCPDEBUG (0) @@ -303,9 +344,8 @@ #define MCP_RXBUF_0 (MCP_RXB0SIDH) #define MCP_RXBUF_1 (MCP_RXB1SIDH) -//#define SPICS 10 -#define MCP2515_SELECT() digitalWrite(SPICS, LOW) -#define MCP2515_UNSELECT() digitalWrite(SPICS, HIGH) +#define MCP2515_SELECT() digitalWrite(MCPCS, LOW) +#define MCP2515_UNSELECT() digitalWrite(MCPCS, HIGH) #define MCP2515_OK (0) #define MCP2515_FAIL (1) From c5547a3b21ddc7547ab05655a7f2424ab92db79d Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 02:49:20 -0500 Subject: [PATCH 05/40] Update mcp_can_dfs.h --- mcp_can_dfs.h | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/mcp_can_dfs.h b/mcp_can_dfs.h index 4213f89..1448580 100644 --- a/mcp_can_dfs.h +++ b/mcp_can_dfs.h @@ -52,11 +52,6 @@ #define MCP_DLC_MASK 0x0F /* 4 LSBits */ #define MCP_RTR_MASK 0x40 /* (1<<6) Bit 6 */ -#define STDEXT 0 /* Standard and Extended */ -#define STD 1 /* Standard IDs ONLY */ -#define EXT 2 /* Extended IDs ONLY */ -#define ANY 3 /* Disables Masks and Filters */ - #define MCP_RXB_RX_ANY 0x60 #define MCP_RXB_RX_EXT 0x40 #define MCP_RXB_RX_STD 0x20 @@ -80,17 +75,17 @@ #define MCP_RXB_RTR_M 0x40 /* In RXBnDLC */ #define MCP_STAT_RXIF_MASK (0x03) -#define MCP_STAT_RX0IF (1<<0) -#define MCP_STAT_RX1IF (1<<1) - -#define MCP_EFLG_RX1OVR (1<<7) -#define MCP_EFLG_RX0OVR (1<<6) -#define MCP_EFLG_TXBO (1<<5) -#define MCP_EFLG_TXEP (1<<4) -#define MCP_EFLG_RXEP (1<<3) -#define MCP_EFLG_TXWAR (1<<2) -#define MCP_EFLG_RXWAR (1<<1) -#define MCP_EFLG_EWARN (1<<0) +#define MCP_STAT_RX0IF (1<<0) +#define MCP_STAT_RX1IF (1<<1) + +#define MCP_EFLG_RX1OVR (1<<7) +#define MCP_EFLG_RX0OVR (1<<6) +#define MCP_EFLG_TXBO (1<<5) +#define MCP_EFLG_TXEP (1<<4) +#define MCP_EFLG_RXEP (1<<3) +#define MCP_EFLG_TXWAR (1<<2) +#define MCP_EFLG_RXWAR (1<<1) +#define MCP_EFLG_EWARN (1<<0) #define MCP_EFLG_ERRORMASK (0xF8) /* 5 MS-Bits */ @@ -187,10 +182,10 @@ /* * CANCTRL Register Values */ -#define MODE_NORMAL 0x00 -#define MODE_SLEEP 0x20 -#define MODE_LOOPBACK 0x40 -#define MODE_LISTENONLY 0x60 +#define MCP_NORMAL 0x00 +#define MCP_SLEEP 0x20 +#define MCP_LOOPBACK 0x40 +#define MCP_LISTENONLY 0x60 #define MODE_CONFIG 0x80 #define MODE_POWERUP 0xE0 #define MODE_MASK 0xE0 @@ -243,9 +238,6 @@ #define MCP_MERRF 0x80 -#define 16MHZ 1 -#define 20MHZ 0 - /* * speed 16M */ @@ -370,6 +362,14 @@ #define CANDEFAULTIDENT (0x55CC) #define CANDEFAULTIDENTEXT (CAN_EXTID) +#define MCP_STDEXT 0 /* Standard and Extended */ +#define MCP_STD 1 /* Standard IDs ONLY */ +#define MCP_EXT 2 /* Extended IDs ONLY */ +#define MCP_ANY 3 /* Disables Masks and Filters */ + +#define MCP_20MHZ 0 +#define MCP_16MHZ 1 + #define CAN_5KBPS 1 #define CAN_10KBPS 2 #define CAN_20KBPS 3 @@ -383,12 +383,12 @@ #define CAN_500KBPS 11 #define CAN_1000KBPS 12 -#define CAN_OK (0) -#define CAN_FAILINIT (1) -#define CAN_FAILTX (2) -#define CAN_MSGAVAIL (3) -#define CAN_NOMSG (4) -#define CAN_CTRLERROR (5) +#define CAN_OK (0) +#define CAN_FAILINIT (1) +#define CAN_FAILTX (2) +#define CAN_MSGAVAIL (3) +#define CAN_NOMSG (4) +#define CAN_CTRLERROR (5) #define CAN_GETTXBFTIMEOUT (6) #define CAN_SENDMSGTIMEOUT (7) #define CAN_FAIL (0xff) From 4861aeec2c96a72e439f01a2af84e92e2f541120 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 02:58:38 -0500 Subject: [PATCH 06/40] Update README.md --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d2b7f6..9df3a74 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,17 @@ MCP2515 Library for Arduino ============== -MCP2515 library v1.1 +MCP2515 library v1.5 This library is compatible with any shield or CAN interface that uses the MCP2515 CAN protocol controller. -Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k are confirmed to work using a Peak-System PCAN-USB dongle as a reference. + +This version supports setting the ID mode of the MCP2515, the BAUD rate with clock speed with the begin() function. +Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. +Baudrates for a 20MHz crystal are yet to be confirmed but were calculated appropiately. + +The readMsgBuf() function now brings in the message ID, so the getCanId() function will be depreciated soon. + +Using the setMode() function you can now put the MCP2515 into sleep, loop-back, or listen-only modes as well as normal operation from your sketch. +Right now the code defaults to listen-only mode after the begin() function runs. + Installation ============== From e9aab99b793429f6cdaf82c1f0af5db90c2ae182 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 02:59:37 -0500 Subject: [PATCH 07/40] Update keywords.txt --- keywords.txt | 73 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/keywords.txt b/keywords.txt index e7128e8..dcfcf74 100644 --- a/keywords.txt +++ b/keywords.txt @@ -5,43 +5,54 @@ ####################################### # Datatypes (KEYWORD1) ####################################### -MCP_CAN KEYWORD1 -mcp_can_dfs KEYWORD1 -mcp_can KEYWORD1 +MCP_CAN KEYWORD1 +mcp_can_dfs KEYWORD1 +mcp_can KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### -begin KEYWORD2 -init_Mask KEYWORD2 -init_Filt KEYWORD2 -sendMsgBuf KEYWORD2 -readMsgBuf KEYWORD2 +begin KEYWORD2 +setMode KEYWORD2 +init_Mask KEYWORD2 +init_Filt KEYWORD2 +sendMsgBuf KEYWORD2 +readMsgBuf KEYWORD2 checkReceive KEYWORD2 -checkError KEYWORD2 -getCanId KEYWORD2 +checkError KEYWORD2 +getCanId KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### -CAN_5KBPS LITERAL1 -CAN_10KBPS LITERAL1 -CAN_20KBPS LITERAL1 -CAN_40KBPS LITERAL1 -CAN_50KBPS LITERAL1 -CAN_80KBPS LITERAL1 -CAN_100KBPS LITERAL1 -CAN_125KBPS LITERAL1 -CAN_200KBPS LITERAL1 -CAN_250KBPS LITERAL1 -CAN_500KBPS LITERAL1 -CAN_1000KBPS LITERAL1 -CAN_OK LITERAL1 -CAN_FAILINIT LITERAL1 -CAN_FAILTX LITERAL1 -CAN_MSGAVAIL LITERAL1 -CAN_NOMSG LITERAL1 -CAN_CTRLERROR LITERAL1 -CAN_GETTXBFTIMEOUT LITERAL1 -CAN_SENDMSGTIMEOUT LITERAL1 -CAN_FAIL LITERAL1 +MCP_16MHZ LITERAL1 +MCP_20MHZ LITERAL1 +CAN_5KBPS LITERAL1 +CAN_10KBPS LITERAL1 +CAN_20KBPS LITERAL1 +CAN_40KBPS LITERAL1 +CAN_50KBPS LITERAL1 +CAN_80KBPS LITERAL1 +CAN_100KBPS LITERAL1 +CAN_125KBPS LITERAL1 +CAN_200KBPS LITERAL1 +CAN_250KBPS LITERAL1 +CAN_500KBPS LITERAL1 +CAN_1000KBPS LITERAL1 +MCP_ANY LITERAL1 +MCP_STD LITERAL1 +MCP_EXT LITERAL1 +MCP_STDEXT LITERAL1 +CAN_OK LITERAL1 +CAN_FAILINIT LITERAL1 +CAN_FAILTX LITERAL1 +CAN_MSGAVAIL LITERAL1 +CAN_NOMSG LITERAL1 +CAN_CTRLERROR LITERAL1 +CAN_GETTXBFTIMEOUT LITERAL1 +CAN_SENDMSGTIMEOUT LITERAL1 +CAN_FAIL LITERAL1 +MCP_NORMAL LITERAL1 +MCP_SLEEP LITERAL1 +MCP_LOOPBACK LITERAL1 +MCP_LISTENONLY LITERAL1 From ad87ed06e51db9612f31eff5105c5629cc2274b1 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 03:06:42 -0500 Subject: [PATCH 08/40] Update receive.ino --- examples/receive/receive.ino | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/receive/receive.ino b/examples/receive/receive.ino index ee31b3e..525496b 100644 --- a/examples/receive/receive.ino +++ b/examples/receive/receive.ino @@ -12,7 +12,8 @@ MCP_CAN CAN0(10); // Set CS to pin 10 void setup() { Serial.begin(115200); - CAN0.begin(CAN_500KBPS); // init can bus : baudrate = 500k + CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ); // init CAN Bus with 500kb/s baudrate at 16MHz with Mask & Filters disabled + CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data. pinMode(2, INPUT); // Setting pin 2 for /INT input Serial.println("MCP2515 Library Receive Example..."); } @@ -21,10 +22,12 @@ void loop() { if(!digitalRead(2)) // If pin 2 is low, read receive buffer { - CAN0.readMsgBuf(&len, rxBuf); // Read data: len = data length, buf = data byte(s) - rxId = CAN0.getCanId(); // Get message ID + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) +// rxId = CAN0.getCanId(); // Function will be depreciated soon due to readMsgBuf now returning ID + Serial.print("ID: "); Serial.print(rxId, HEX); + Serial.print(" Data: "); for(int i = 0; i Date: Wed, 22 Jan 2014 03:09:35 -0500 Subject: [PATCH 09/40] Update send.ino --- examples/send/send.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/send/send.ino b/examples/send/send.ino index 4524792..ec4adc5 100644 --- a/examples/send/send.ino +++ b/examples/send/send.ino @@ -7,9 +7,10 @@ MCP_CAN CAN0(10); // Set CS to pin 10 void setup() { Serial.begin(115200); - // init can bus, baudrate: 500k - if(CAN0.begin(CAN_500KBPS) == CAN_OK) Serial.print("can init ok!!\r\n"); + // Init CAN bus with 500kb/s baudrate at 16MHz with Mask and Filters disabled. + if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 init ok!!\r\n"); else Serial.print("Can init fail!!\r\n"); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted } unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; From 16aca1eeacfc98cc2d4296c5d2120929d396ec89 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 03:38:35 -0500 Subject: [PATCH 10/40] Update mcp_can.cpp --- mcp_can.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index 4e910a5..4651fce 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -389,7 +389,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I mcp2515_reset(); - mcpMode = MODE_LISTENONLY; + mcpMode = MCP_LISTENONLY; res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0) @@ -652,12 +652,12 @@ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) { INT8U res = MCP2515_OK; #if DEBUG_MODE - Serial.print("Begin to set Mask!!\r\n"); + Serial.print("Starting to Set Mask!!!\r\n"); #endif res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0){ #if DEBUG_MODE - Serial.print("Enter setting mode fall\r\n"); + Serial.print("Entering Configuration Mode Failure...\r\n"); #endif return res; } @@ -674,12 +674,12 @@ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) res = mcp2515_setCANCTRL_Mode(mcpMode); if(res > 0){ #if DEBUG_MODE - Serial.print("Enter normal mode fall\r\n"); + Serial.print("Entering Previous Mode Failure...\r\nSetting Mask Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("set Mask success!!\r\n"); + Serial.print("Setting Mask Successful!!!\r\n"); #endif return res; } @@ -692,13 +692,13 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) { INT8U res = MCP2515_OK; #if DEBUG_MODE - Serial.print("Begin to set Filter!!\r\n"); + Serial.print("Starting to Set Filter!!!\r\n"); #endif res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0) { #if DEBUG_MODE - Serial.print("Enter setting mode fall\r\n"); + Serial.print("Enter Configuration Mode Failure...\r\n"); #endif return res; } @@ -737,12 +737,12 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) if(res > 0) { #if DEBUG_MODE - Serial.print("Enter normal mode fall\r\nSet filter fail!!\r\n"); + Serial.print("Entering Previous Mode Failure...\r\nSetting Filter Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("set Filter success!!\r\n"); + Serial.print("Setting Filter Successfull!!!\r\n"); #endif return res; From 2f570786b242849693f8e3632c277ed54e02bad8 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 03:39:51 -0500 Subject: [PATCH 11/40] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9df3a74..252b46a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +Work In Progress - Library UNTESTED +============== + MCP2515 Library for Arduino ============== MCP2515 library v1.5 From 997f12883e765e4f7b42830bf84f15c945280d2c Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 22 Jan 2014 13:57:32 -0500 Subject: [PATCH 12/40] Update mcp_can.cpp Should work now... --- mcp_can.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index 4651fce..c37db8a 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -139,8 +139,8 @@ INT8U MCP_CAN::mcp2515_readStatus(void) *********************************************************************************************************/ INT8U MCP_CAN::setMode(const INT8U opMode) { - mcpMode = opMode - mcp2515_setCANCTRL_Mode(mcpMode) + mcpMode = opMode; + mcp2515_setCANCTRL_Mode(mcpMode); } @@ -176,7 +176,7 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) set = 1; switch (canClock) { - case (16MHZ): + case (MCP_16MHZ): switch (canSpeed) { case (CAN_5KBPS): /* 5KBPS */ @@ -257,7 +257,7 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) } break; - case (20MHZ): + case (MCP_20MHZ): switch (canSpeed) { case (CAN_40KBPS): /* 40KBPS */ @@ -425,7 +425,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I switch(canIDMode) { - case (ANY): + case (MCP_ANY): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); @@ -433,7 +433,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I MCP_RXB_RX_ANY); break; - case (STD): + case (MCP_STD): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); @@ -441,7 +441,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I MCP_RXB_RX_STD); break; - case (EXT): + case (MCP_EXT): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); @@ -449,7 +449,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I MCP_RXB_RX_EXT); break; - case (STDEXT): + case (MCP_STDEXT): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); From ab38b31d5f1f62ddbe4510a0ab12fb74d4efaace Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 23 Jan 2014 00:17:33 -0500 Subject: [PATCH 13/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 252b46a..e7c555a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Work In Progress - Library UNTESTED +Work In Progress - Library Seems to Work... ============== MCP2515 Library for Arduino From 284d058ddd71737d8170c08d87663c6ffd725498 Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 28 Jan 2014 16:39:51 -0500 Subject: [PATCH 14/40] New Example! --- examples/CAN_to_Ethernet/CAN_to_Ethernet.ino | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 examples/CAN_to_Ethernet/CAN_to_Ethernet.ino diff --git a/examples/CAN_to_Ethernet/CAN_to_Ethernet.ino b/examples/CAN_to_Ethernet/CAN_to_Ethernet.ino new file mode 100644 index 0000000..157341d --- /dev/null +++ b/examples/CAN_to_Ethernet/CAN_to_Ethernet.ino @@ -0,0 +1,59 @@ +// CAN to Ethernet +// Jan 28th, 2014 +// Written by: Cory J. Fowler + +#include +#include +#include +#include + +// Change these for your network! +byte mac[] = {0x00, 0x55, 0x66, 0xEE, 0xFF, 0xFF}; +IPAddress ip(10, 100, 50, 233); +IPAddress gateway(10, 100, 50, 254); +IPAddress dest(10, 100, 50, 210); + +unsigned int localPort = 8888; +unsigned int remPort = 54321; + +unsigned long rxId; +byte len = 0; +byte rxBuf[8]; +char buffer[50]; + +MCP_CAN CAN0(9); // Set CS to pin 9 + +EthernetUDP UDP; +void setup() +{ + Serial.begin(115200); +// CAN0.begin(CAN_250KBPS); // init CAN Bus with 250kb/s baudrate + CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_16MHZ); // init CAN Bus with 250kb/s baudrate at 16MHz with Mask & Filters Disabled + CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data. + pinMode(2, INPUT); // Setting pin 2, MCP2515 /INT, to input mode + Ethernet.begin(mac,ip); // Initialize Ethernet + UDP.begin(localPort); // Initialize the UDP listen port that is currently unused! + + Serial.println("CAN to Ethernet..."); +} + +void loop() +{ + if(!digitalRead(2)) // If pin 2 is low, read receive buffer + { + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read Data: rxID = Message ID, len = Data Length, buf = Data Byte(s) +// CAN0.readMsgBuf(&len, rxBuf); // Read Data: len = Data Length, buf = Data Byte(s) +// rxId = CAN0.getCanId(); // Function will be depreciated soon due to readMsgBuf now returning ID + + sprintf(buffer, "ID: %.8lX Data: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n\r", + rxId, rxBuf[0], rxBuf[1], rxBuf[2], rxBuf[3], rxBuf[4], rxBuf[5], rxBuf[6], rxBuf[7]); + + UDP.beginPacket(dest, remPort); + UDP.write(buffer); + UDP.endPacket(); + } +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ From b4bd377564b739508dab2e9e0f37576b89699bd6 Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 28 Jan 2014 16:45:41 -0500 Subject: [PATCH 15/40] Companion script for example sketch. --- examples/CAN_to_Ethernet/udp_listen.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/CAN_to_Ethernet/udp_listen.pl diff --git a/examples/CAN_to_Ethernet/udp_listen.pl b/examples/CAN_to_Ethernet/udp_listen.pl new file mode 100644 index 0000000..413c803 --- /dev/null +++ b/examples/CAN_to_Ethernet/udp_listen.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w +#This works on Debian Linux +#I have not tested it on a Windows system running Perl, but it should work if all dependencies are met. +#Also, FIREWALL. <--- I gaurantee that will be the issue. + +use IO::Socket; + +### Create UDP Listen Socket +my $udpsocket = new IO::Socket::INET ( + LocalPort => '54321', + Proto => 'udp', + ); + die "Could not create socket: $!\n" unless $udpsocket; + +### Data Manipulation and Display +while(1) { + $udpsocket->recv(my $data,512); + print $data; +} From adc5881b54be70f009dee6421986c32839b083d3 Mon Sep 17 00:00:00 2001 From: Cory Date: Fri, 31 Jan 2014 10:34:24 -0500 Subject: [PATCH 16/40] Added Dual CAN Example Sketch This sketch demos the ability for this library to support more than one CAN interface on a single Arduino. --- examples/dual_CAN/dual_CAN.ino | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/dual_CAN/dual_CAN.ino diff --git a/examples/dual_CAN/dual_CAN.ino b/examples/dual_CAN/dual_CAN.ino new file mode 100644 index 0000000..860feb7 --- /dev/null +++ b/examples/dual_CAN/dual_CAN.ino @@ -0,0 +1,55 @@ +// Demo: Dual CAN-BUS Shields, Data Pass-through +// Written by: Cory J. Fowler +// January 31st 2014 +// This examples the ability of this library to support more than one MCP2515 based CAN interface. + + +#include +#include + +unsigned long rxId; +byte len; +byte rxBuf[8]; + +byte txBuf0[] = {AA,55,AA,55,AA,55,AA,55}; +byte txBuf1[] = {55,AA,55,AA,55,AA,55,AA}; + +MCP_CAN CAN0(10); // CAN0 interface usins CS on digital pin 10 +MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9 + +void setup() +{ + Serial.begin(115200); + + // init CAN0 bus, baudrate: 250k@16MHz + if(CAN0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){ + Serial.print("CAN0: Init OK!\r\n"); + CAN0.setMode(MCP_NORMAL); + } else Serial.print("CAN0: Init Fail!!!\r\n"); + + // init CAN1 bus, baudrate: 250k@16MHz + if(CAN1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){ + Serial.print("CAN1: Init OK!\r\n"); + CAN1.setMode(MCP_NORMAL); + } else Serial.print("CAN1: Init Fail!!!\r\n"); + + SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz) + + CAN0.sendMsgBuf(0x1000000, 1, 8, tx0Buf); + CAN1.sendMsgBuf(0x1000001, 1, 8, tx1Buf); +} + +void loop(){ + if(!digitalRead(2)){ // If pin 2 is low, read CAN0 receive buffer + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + CAN1.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN1 interface + } + if(!digitalRead(3)){ // If pin 3 is low, read CAN1 receive buffer + CAN1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + CAN0.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN0 interface + } +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ From 2a9e0bcbe63e2bf9d48af2f4e6a666f5e35bc392 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Jun 2014 00:24:05 -0400 Subject: [PATCH 17/40] Depreciated function, Testing for bug fix. Depreciated 'mcp2515_start_transmit' function call as it's sole purpose was to call another function and return. Per http://forum.arduino.cc/index.php?topic=152145.msg1654634#msg1654634 After review of the code, this appears to be correct but I need to test this before I apply it to the master branch. --- mcp_can.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index c37db8a..e73df2c 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -585,15 +585,6 @@ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read mcp2515_readRegisterS( mcp_addr+5, &(m_nDta[0]), m_nDlc ); } -/********************************************************************************************************* -** Function name: sendMsg -** Descriptions: send message -*********************************************************************************************************/ -void MCP_CAN::mcp2515_start_transmit(const INT8U mcp_addr) /* start transmit */ -{ - mcp2515_modifyRegister( mcp_addr-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M ); -} - /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message @@ -610,8 +601,8 @@ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get N for (i=0; i Date: Wed, 4 Jun 2014 00:28:08 -0400 Subject: [PATCH 18/40] Depreciated function Removed depreciated private function mcp2515_start_transmit --- mcp_can.h | 1 - 1 file changed, 1 deletion(-) diff --git a/mcp_can.h b/mcp_can.h index 27717df..a667170 100644 --- a/mcp_can.h +++ b/mcp_can.h @@ -88,7 +88,6 @@ class MCP_CAN void mcp2515_write_canMsg( const INT8U buffer_sidh_addr ); /* write can msg */ void mcp2515_read_canMsg( const INT8U buffer_sidh_addr); /* read can msg */ - void mcp2515_start_transmit(const INT8U mcp_addr); /* start transmit */ INT8U mcp2515_getNextFreeTXBuf(INT8U *txbuf_n); /* get Next free txbuf */ /* From e3b1de83857ad240c38c15d519c5dde163839f06 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 16 Jun 2014 11:18:34 -0400 Subject: [PATCH 19/40] Fixed errors... Trying to make an example sketch to demo masks and filters found and some errors... There appears to be a further issue that I have not resolved, standard ID filtering doesn't work for me at the moment. I have been successful with extended ID filtering before attempting to make the demo. I think the issue lies within 'mcp2515_write_id.' --- mcp_can.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index e73df2c..bfc9c52 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -436,7 +436,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I case (MCP_STD): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + MCP_RXB_RX_STD | MCP_RXB_BUKT_MASK ); mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_STD); break; @@ -444,7 +444,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I case (MCP_EXT): mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + MCP_RXB_RX_EXT | MCP_RXB_BUKT_MASK ); mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_EXT); break; @@ -791,7 +791,6 @@ INT8U MCP_CAN::sendMsg() } uiTimeOut = 0; mcp2515_write_canMsg( txbuf_n); -// mcp2515_start_transmit( txbuf_n ); // Depreciated this function call, the function calls another function and returns. mcp2515_modifyRegister( txbuf_n-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M ); do { From 93c5312d06743192a965851bd8953981573f65a5 Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 17 Jul 2014 02:43:25 -0400 Subject: [PATCH 20/40] Update! (Read Description) Added function to utilize mask and filter functionality of checking first two data bytes for Standard CAN ID frames. (A mask or filter entry for standard IDs will look similar Extended IDs. 0x07FF0000 The first nibble is unused, the last four nibbles are the two data bytes.) Cleaned up function descriptions. Removed 'getCanId' function. --- mcp_can.cpp | 290 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 200 insertions(+), 90 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index bfc9c52..abcc3e4 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -28,7 +28,7 @@ /********************************************************************************************************* ** Function name: mcp2515_reset -** Descriptions: reset the device +** Descriptions: Performs a software reset *********************************************************************************************************/ void MCP_CAN::mcp2515_reset(void) { @@ -40,7 +40,7 @@ void MCP_CAN::mcp2515_reset(void) /********************************************************************************************************* ** Function name: mcp2515_readRegister -** Descriptions: read register +** Descriptions: Read data register *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) { @@ -57,7 +57,7 @@ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) /********************************************************************************************************* ** Function name: mcp2515_readRegisterS -** Descriptions: read registerS +** Descriptions: Reads sucessive data registers *********************************************************************************************************/ void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const INT8U n) { @@ -75,7 +75,7 @@ void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const I /********************************************************************************************************* ** Function name: mcp2515_setRegister -** Descriptions: set register +** Descriptions: Sets data register *********************************************************************************************************/ void MCP_CAN::mcp2515_setRegister(const INT8U address, const INT8U value) { @@ -84,11 +84,12 @@ void MCP_CAN::mcp2515_setRegister(const INT8U address, const INT8U value) spi_readwrite(address); spi_readwrite(value); MCP2515_UNSELECT(); + delayMicroseconds(250); } /********************************************************************************************************* ** Function name: mcp2515_setRegisterS -** Descriptions: set registerS +** Descriptions: Sets sucessive data registers *********************************************************************************************************/ void MCP_CAN::mcp2515_setRegisterS(const INT8U address, const INT8U values[], const INT8U n) { @@ -102,11 +103,12 @@ void MCP_CAN::mcp2515_setRegisterS(const INT8U address, const INT8U values[], co spi_readwrite(values[i]); } MCP2515_UNSELECT(); + delayMicroseconds(250); } /********************************************************************************************************* ** Function name: mcp2515_modifyRegister -** Descriptions: set bit of one register +** Descriptions: Sets specific bits of a register *********************************************************************************************************/ void MCP_CAN::mcp2515_modifyRegister(const INT8U address, const INT8U mask, const INT8U data) { @@ -116,11 +118,12 @@ void MCP_CAN::mcp2515_modifyRegister(const INT8U address, const INT8U mask, cons spi_readwrite(mask); spi_readwrite(data); MCP2515_UNSELECT(); + delayMicroseconds(250); } /********************************************************************************************************* ** Function name: mcp2515_readStatus -** Descriptions: read mcp2515's Status +** Descriptions: Reads status register *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_readStatus(void) { @@ -129,24 +132,27 @@ INT8U MCP_CAN::mcp2515_readStatus(void) spi_readwrite(MCP_READ_STATUS); i = spi_read(); MCP2515_UNSELECT(); - return i; } /********************************************************************************************************* -** Function name: setMode -** Descriptions: set control mode via sketch +** Function name: setMode +** Descriptions: Sets control mode *********************************************************************************************************/ INT8U MCP_CAN::setMode(const INT8U opMode) { + INT8U i; + mcpMode = opMode; - mcp2515_setCANCTRL_Mode(mcpMode); + i = mcp2515_setCANCTRL_Mode(mcpMode); + + return i; } /********************************************************************************************************* ** Function name: mcp2515_setCANCTRL_Mode -** Descriptions: set control mode +** Descriptions: Set control mode *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_setCANCTRL_Mode(const INT8U newmode) { @@ -163,12 +169,11 @@ INT8U MCP_CAN::mcp2515_setCANCTRL_Mode(const INT8U newmode) } return MCP2515_FAIL; - } /********************************************************************************************************* ** Function name: mcp2515_configRate -** Descriptions: set boadrate +** Descriptions: Set baudrate *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) { @@ -176,76 +181,158 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) set = 1; switch (canClock) { + // Add support for boards operating at 8MHz + /* + case (MCP_8MHZ): + switch (canSpeed) + { + case (CAN_5KBPS): // 5KBPS + cfg1 = MCP_8MHz_5kBPS_CFG1; + cfg2 = MCP_8MHz_5kBPS_CFG2; + cfg3 = MCP_8MHz_5kBPS_CFG3; + break; + + case (CAN_10KBPS): // 10KBPS + cfg1 = MCP_8MHz_10kBPS_CFG1; + cfg2 = MCP_8MHz_10kBPS_CFG2; + cfg3 = MCP_8MHz_10kBPS_CFG3; + break; + + case (CAN_20KBPS): // 20KBPS + cfg1 = MCP_8MHz_20kBPS_CFG1; + cfg2 = MCP_8MHz_20kBPS_CFG2; + cfg3 = MCP_8MHz_20kBPS_CFG3; + break; + + case (CAN_40KBPS): // 40Kbps + cfg1 = MCP_8MHz_40kBPS_CFG1; + cfg2 = MCP_8MHz_40kBPS_CFG2; + cfg3 = MCP_8MHz_40kBPS_CFG3; + break; + + case (CAN_50KBPS): // 50Kbps + cfg1 = MCP_8MHz_50kBPS_CFG1; + cfg2 = MCP_8MHz_50kBPS_CFG2; + cfg3 = MCP_8MHz_50kBPS_CFG3; + break; + + case (CAN_80KBPS): // 80Kbps + cfg1 = MCP_8MHz_80kBPS_CFG1; + cfg2 = MCP_8MHz_80kBPS_CFG2; + cfg3 = MCP_8MHz_80kBPS_CFG3; + break; + + case (CAN_100KBPS): // 100Kbps + cfg1 = MCP_8MHz_100kBPS_CFG1; + cfg2 = MCP_8MHz_100kBPS_CFG2; + cfg3 = MCP_8MHz_100kBPS_CFG3; + break; + + case (CAN_125KBPS): // 125Kbps + cfg1 = MCP_8MHz_125kBPS_CFG1; + cfg2 = MCP_8MHz_125kBPS_CFG2; + cfg3 = MCP_8MHz_125kBPS_CFG3; + break; + + case (CAN_200KBPS): // 200Kbps + cfg1 = MCP_8MHz_200kBPS_CFG1; + cfg2 = MCP_8MHz_200kBPS_CFG2; + cfg3 = MCP_8MHz_200kBPS_CFG3; + break; + + case (CAN_250KBPS): // 250Kbps + cfg1 = MCP_8MHz_250kBPS_CFG1; + cfg2 = MCP_8MHz_250kBPS_CFG2; + cfg3 = MCP_8MHz_250kBPS_CFG3; + break; + + case (CAN_500KBPS): // 500Kbps + cfg1 = MCP_8MHz_500kBPS_CFG1; + cfg2 = MCP_8MHz_500kBPS_CFG2; + cfg3 = MCP_8MHz_500kBPS_CFG3; + break; + + case (CAN_1000KBPS): // 1Mbps + cfg1 = MCP_8MHz_1000kBPS_CFG1; + cfg2 = MCP_8MHz_1000kBPS_CFG2; + cfg3 = MCP_8MHz_1000kBPS_CFG3; + break; + + default: + set = 0; + break; + } + break; + */ case (MCP_16MHZ): switch (canSpeed) { - case (CAN_5KBPS): /* 5KBPS */ + case (CAN_5KBPS): // 5Kbps cfg1 = MCP_16MHz_5kBPS_CFG1; cfg2 = MCP_16MHz_5kBPS_CFG2; cfg3 = MCP_16MHz_5kBPS_CFG3; break; - case (CAN_10KBPS): /* 10KBPS */ + case (CAN_10KBPS): // 10Kbps cfg1 = MCP_16MHz_10kBPS_CFG1; cfg2 = MCP_16MHz_10kBPS_CFG2; cfg3 = MCP_16MHz_10kBPS_CFG3; break; - case (CAN_20KBPS): /* 20KBPS */ + case (CAN_20KBPS): // 20Kbps cfg1 = MCP_16MHz_20kBPS_CFG1; cfg2 = MCP_16MHz_20kBPS_CFG2; cfg3 = MCP_16MHz_20kBPS_CFG3; break; - case (CAN_40KBPS): /* 40KBPS */ + case (CAN_40KBPS): // 40Kbps cfg1 = MCP_16MHz_40kBPS_CFG1; cfg2 = MCP_16MHz_40kBPS_CFG2; cfg3 = MCP_16MHz_40kBPS_CFG3; break; - case (CAN_50KBPS): /* 50KBPS */ - cfg1 = MCP_16MHz_50kBPS_CFG1; + case (CAN_50KBPS): // 50Kbps cfg2 = MCP_16MHz_50kBPS_CFG2; cfg3 = MCP_16MHz_50kBPS_CFG3; break; - case (CAN_80KBPS): /* 80KBPS */ + case (CAN_80KBPS): // 80Kbps cfg1 = MCP_16MHz_80kBPS_CFG1; cfg2 = MCP_16MHz_80kBPS_CFG2; cfg3 = MCP_16MHz_80kBPS_CFG3; break; - case (CAN_100KBPS): /* 100KBPS */ + case (CAN_100KBPS): // 100Kbps cfg1 = MCP_16MHz_100kBPS_CFG1; cfg2 = MCP_16MHz_100kBPS_CFG2; cfg3 = MCP_16MHz_100kBPS_CFG3; break; - case (CAN_125KBPS): /* 125KBPS */ + case (CAN_125KBPS): // 125Kbps cfg1 = MCP_16MHz_125kBPS_CFG1; cfg2 = MCP_16MHz_125kBPS_CFG2; cfg3 = MCP_16MHz_125kBPS_CFG3; break; - case (CAN_200KBPS): /* 200KBPS */ + case (CAN_200KBPS): // 200Kbps cfg1 = MCP_16MHz_200kBPS_CFG1; cfg2 = MCP_16MHz_200kBPS_CFG2; cfg3 = MCP_16MHz_200kBPS_CFG3; break; - case (CAN_250KBPS): /* 250KBPS */ + case (CAN_250KBPS): // 250Kbps cfg1 = MCP_16MHz_250kBPS_CFG1; cfg2 = MCP_16MHz_250kBPS_CFG2; cfg3 = MCP_16MHz_250kBPS_CFG3; break; - case (CAN_500KBPS): /* 500KBPS */ + case (CAN_500KBPS): // 500Kbps cfg1 = MCP_16MHz_500kBPS_CFG1; cfg2 = MCP_16MHz_500kBPS_CFG2; cfg3 = MCP_16MHz_500kBPS_CFG3; break; - case (CAN_1000KBPS): /* 1MBPS */ + case (CAN_1000KBPS): // 1Mbps cfg1 = MCP_16MHz_1000kBPS_CFG1; cfg2 = MCP_16MHz_1000kBPS_CFG2; cfg3 = MCP_16MHz_1000kBPS_CFG3; @@ -260,55 +347,55 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) case (MCP_20MHZ): switch (canSpeed) { - case (CAN_40KBPS): /* 40KBPS */ + case (CAN_40KBPS): // 40Kbps cfg1 = MCP_20MHz_40kBPS_CFG1; cfg2 = MCP_20MHz_40kBPS_CFG2; cfg3 = MCP_20MHz_40kBPS_CFG3; break; - case (CAN_50KBPS): /* 50KBPS */ + case (CAN_50KBPS): // 50Kbps cfg1 = MCP_20MHz_50kBPS_CFG1; cfg2 = MCP_20MHz_50kBPS_CFG2; cfg3 = MCP_20MHz_50kBPS_CFG3; break; - case (CAN_80KBPS): /* 80KBPS */ + case (CAN_80KBPS): // 80Kbps cfg1 = MCP_20MHz_80kBPS_CFG1; cfg2 = MCP_20MHz_80kBPS_CFG2; cfg3 = MCP_20MHz_80kBPS_CFG3; break; - case (CAN_100KBPS): /* 100KBPS */ + case (CAN_100KBPS): // 100Kbps cfg1 = MCP_20MHz_100kBPS_CFG1; cfg2 = MCP_20MHz_100kBPS_CFG2; cfg3 = MCP_20MHz_100kBPS_CFG3; break; - case (CAN_125KBPS): /* 125KBPS */ + case (CAN_125KBPS): // 125Kbps cfg1 = MCP_20MHz_125kBPS_CFG1; cfg2 = MCP_20MHz_125kBPS_CFG2; cfg3 = MCP_20MHz_125kBPS_CFG3; break; - case (CAN_200KBPS): /* 200KBPS */ + case (CAN_200KBPS): // 200Kbps cfg1 = MCP_20MHz_200kBPS_CFG1; cfg2 = MCP_20MHz_200kBPS_CFG2; cfg3 = MCP_20MHz_200kBPS_CFG3; break; - case (CAN_250KBPS): /* 250KBPS */ + case (CAN_250KBPS): // 250Kbps cfg1 = MCP_20MHz_250kBPS_CFG1; cfg2 = MCP_20MHz_250kBPS_CFG2; cfg3 = MCP_20MHz_250kBPS_CFG3; break; - case (CAN_500KBPS): /* 500KBPS */ + case (CAN_500KBPS): // 500Kbps cfg1 = MCP_20MHz_500kBPS_CFG1; cfg2 = MCP_20MHz_500kBPS_CFG2; cfg3 = MCP_20MHz_500kBPS_CFG3; break; - case (CAN_1000KBPS): /* 1MBPS */ + case (CAN_1000KBPS): // 1Mbps cfg1 = MCP_20MHz_1000kBPS_CFG1; cfg2 = MCP_20MHz_1000kBPS_CFG2; cfg3 = MCP_20MHz_1000kBPS_CFG3; @@ -338,7 +425,7 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) /********************************************************************************************************* ** Function name: mcp2515_initCANBuffers -** Descriptions: init canbuffers +** Descriptions: Initialize Buffers, Masks, and Filters *********************************************************************************************************/ void MCP_CAN::mcp2515_initCANBuffers(void) { @@ -349,16 +436,16 @@ void MCP_CAN::mcp2515_initCANBuffers(void) INT32U ulMask = 0x00, ulFilt = 0x00; - mcp2515_write_id(MCP_RXM0SIDH, ext, ulMask); /*Set both masks to 0 */ - mcp2515_write_id(MCP_RXM1SIDH, ext, ulMask); /*Mask register ignores ext bit */ + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulMask); /*Set both masks to 0 */ + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulMask); /*Mask register ignores ext bit */ /* Set all filters to 0 */ - mcp2515_write_id(MCP_RXF0SIDH, ext, ulFilt); /* RXB0: extended */ - mcp2515_write_id(MCP_RXF1SIDH, std, ulFilt); /* RXB1: standard */ - mcp2515_write_id(MCP_RXF2SIDH, ext, ulFilt); /* RXB2: extended */ - mcp2515_write_id(MCP_RXF3SIDH, std, ulFilt); /* RXB3: standard */ - mcp2515_write_id(MCP_RXF4SIDH, ext, ulFilt); - mcp2515_write_id(MCP_RXF5SIDH, std, ulFilt); + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulFilt); /* RXB0: extended */ + mcp2515_write_mf(MCP_RXF1SIDH, std, ulFilt); /* RXB1: standard */ + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulFilt); /* RXB2: extended */ + mcp2515_write_mf(MCP_RXF3SIDH, std, ulFilt); /* RXB3: standard */ + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulFilt); + mcp2515_write_mf(MCP_RXF5SIDH, std, ulFilt); /* Clear, deactivate the three */ /* transmit buffers */ @@ -380,9 +467,9 @@ void MCP_CAN::mcp2515_initCANBuffers(void) /********************************************************************************************************* ** Function name: mcp2515_init -** Descriptions: init the device +** Descriptions: Initialize the controller *********************************************************************************************************/ -INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const INT8U canClock) /* mcp2515init */ +INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const INT8U canClock) { INT8U res; @@ -475,11 +562,6 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I return res; } - -#if DEBUG_MODE - Serial.print("Returning to Previous Mode Successful!!!\r\n"); -#endif - } return res; @@ -487,7 +569,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I /********************************************************************************************************* ** Function name: mcp2515_write_id -** Descriptions: write can id +** Descriptions: Write CAN ID *********************************************************************************************************/ void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id ) { @@ -513,12 +595,47 @@ void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT tbufdata[MCP_EID0] = 0; tbufdata[MCP_EID8] = 0; } + + mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); +} + +/********************************************************************************************************* +** Function name: mcp2515_write_mf +** Descriptions: Write Masks and Filters +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_mf( const INT8U mcp_addr, const INT8U ext, const INT32U id ) +{ + uint16_t canid; + INT8U tbufdata[4]; + + canid = (uint16_t)(id & 0x0FFFF); + + if ( ext == 1) + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); + tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3); + tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; + tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 ); + } + else + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); + tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x07) << 5); + tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); + } + mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); } /********************************************************************************************************* ** Function name: mcp2515_read_id -** Descriptions: read can id +** Descriptions: Read CAN ID *********************************************************************************************************/ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) { @@ -543,7 +660,7 @@ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) /********************************************************************************************************* ** Function name: mcp2515_write_canMsg -** Descriptions: write msg +** Descriptions: Write message *********************************************************************************************************/ void MCP_CAN::mcp2515_write_canMsg( const INT8U buffer_sidh_addr) { @@ -554,14 +671,14 @@ void MCP_CAN::mcp2515_write_canMsg( const INT8U buffer_sidh_addr) { m_nDlc |= MCP_RTR_MASK; } - mcp2515_setRegister((mcp_addr+4), m_nDlc ); /* write the RTR and DLC */ - mcp2515_write_id(mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */ + mcp2515_setRegister((mcp_addr+4), m_nDlc ); /* write the RTR and DLC */ + mcp2515_write_id(mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */ } /********************************************************************************************************* ** Function name: mcp2515_read_canMsg -** Descriptions: read message +** Descriptions: Read message *********************************************************************************************************/ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read can msg */ { @@ -586,8 +703,8 @@ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read } /********************************************************************************************************* -** Function name: sendMsg -** Descriptions: send message +** Function name: mcp2515_getNextFreeTXBuf +** Descriptions: Send message *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */ { @@ -611,8 +728,8 @@ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get N } /********************************************************************************************************* -** Function name: set CS -** Descriptions: init CS pin and set UNSELECTED +** Function name: MCP_CAN +** Descriptions: Public function to declare CAN class and the /CS pin. *********************************************************************************************************/ MCP_CAN::MCP_CAN(INT8U _CS) { @@ -622,8 +739,8 @@ MCP_CAN::MCP_CAN(INT8U _CS) } /********************************************************************************************************* -** Function name: init -** Descriptions: init can and set speed +** Function name: begin +** Descriptions: Public function to declare controller initialization parameters. *********************************************************************************************************/ INT8U MCP_CAN::begin(INT8U idmodeset, INT8U speedset, INT8U clockset) { @@ -637,7 +754,7 @@ INT8U MCP_CAN::begin(INT8U idmodeset, INT8U speedset, INT8U clockset) /********************************************************************************************************* ** Function name: init_Mask -** Descriptions: init canid Masks +** Descriptions: Public function to set mask(s). *********************************************************************************************************/ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) { @@ -654,11 +771,11 @@ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) } if (num == 0){ - mcp2515_write_id(MCP_RXM0SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData); } else if(num == 1){ - mcp2515_write_id(MCP_RXM1SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData); } else res = MCP2515_FAIL; @@ -677,7 +794,7 @@ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) /********************************************************************************************************* ** Function name: init_Filt -** Descriptions: init canid filters +** Descriptions: Public function to set filter(s). *********************************************************************************************************/ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) { @@ -697,27 +814,27 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) switch( num ) { case 0: - mcp2515_write_id(MCP_RXF0SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData); break; case 1: - mcp2515_write_id(MCP_RXF1SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData); break; case 2: - mcp2515_write_id(MCP_RXF2SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData); break; case 3: - mcp2515_write_id(MCP_RXF3SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData); break; case 4: - mcp2515_write_id(MCP_RXF4SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData); break; case 5: - mcp2515_write_id(MCP_RXF5SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData); break; default: @@ -741,7 +858,7 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) /********************************************************************************************************* ** Function name: setMsg -** Descriptions: set can message, such as dlc, id, dta[] and so on +** Descriptions: Set can message, such as dlc, id, dta[] and so on *********************************************************************************************************/ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) { @@ -756,7 +873,7 @@ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) /********************************************************************************************************* ** Function name: clearMsg -** Descriptions: set all message to zero +** Descriptions: Set all messages to zero *********************************************************************************************************/ INT8U MCP_CAN::clearMsg() { @@ -773,7 +890,7 @@ INT8U MCP_CAN::clearMsg() /********************************************************************************************************* ** Function name: sendMsg -** Descriptions: send message +** Descriptions: Send message *********************************************************************************************************/ INT8U MCP_CAN::sendMsg() { @@ -791,6 +908,7 @@ INT8U MCP_CAN::sendMsg() } uiTimeOut = 0; mcp2515_write_canMsg( txbuf_n); +// mcp2515_start_transmit( txbuf_n ); // Depreciated this function call, the function calls another function and returns. mcp2515_modifyRegister( txbuf_n-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M ); do { @@ -808,7 +926,7 @@ INT8U MCP_CAN::sendMsg() /********************************************************************************************************* ** Function name: sendMsgBuf -** Descriptions: send buf +** Descriptions: Send message to transmitt buffer *********************************************************************************************************/ INT8U MCP_CAN::sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf) { @@ -818,7 +936,7 @@ INT8U MCP_CAN::sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf) /********************************************************************************************************* ** Function name: readMsg -** Descriptions: read message +** Descriptions: Read message *********************************************************************************************************/ INT8U MCP_CAN::readMsg() { @@ -847,7 +965,7 @@ INT8U MCP_CAN::readMsg() /********************************************************************************************************* ** Function name: readMsgBuf -** Descriptions: read message buf +** Descriptions: Public function, Reads message from receive buffer. *********************************************************************************************************/ INT8U MCP_CAN::readMsgBuf(INT32U *id, INT8U *len, INT8U buf[]) { @@ -862,7 +980,7 @@ INT8U MCP_CAN::readMsgBuf(INT32U *id, INT8U *len, INT8U buf[]) /********************************************************************************************************* ** Function name: checkReceive -** Descriptions: check if got something +** Descriptions: Public function, Checks for received data. (Used if not using the interrupt output) *********************************************************************************************************/ INT8U MCP_CAN::checkReceive(void) { @@ -880,7 +998,7 @@ INT8U MCP_CAN::checkReceive(void) /********************************************************************************************************* ** Function name: checkError -** Descriptions: if something error +** Descriptions: Public function, Returns error register data. *********************************************************************************************************/ INT8U MCP_CAN::checkError(void) { @@ -896,14 +1014,6 @@ INT8U MCP_CAN::checkError(void) } } -/********************************************************************************************************* -** Function name: getCanId -** Descriptions: Will be depreciated soon... -*********************************************************************************************************/ -INT32U MCP_CAN::getCanId(void) -{ - return m_nID; -} /********************************************************************************************************* END FILE *********************************************************************************************************/ From a5c0e5a36ff465e988435d1098194fd056940a6d Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 17 Jul 2014 02:45:51 -0400 Subject: [PATCH 21/40] Update! (Read Description) Added function to utilize mask and filter functionality of checking first two data bytes for Standard CAN ID frames. (A mask or filter entry for standard IDs will look similar Extended IDs. 0x07FF0000 The first nibble is unused, the last four nibbles are the two data bytes.) Cleaned up function descriptions. Removed 'getCanId' function. --- mcp_can.h | 79 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/mcp_can.h b/mcp_can.h index a667170..81ce067 100644 --- a/mcp_can.h +++ b/mcp_can.h @@ -31,13 +31,13 @@ class MCP_CAN { private: - INT8U m_nExtFlg; /* identifier xxxID */ - /* either extended (the 29 LSB) */ - /* or standard (the 11 LSB) */ - INT32U m_nID; /* can id */ - INT8U m_nDlc; /* data length: */ - INT8U m_nDta[MAX_CHAR_IN_MESSAGE]; /* data */ - INT8U m_nRtr; /* rtr */ + INT8U m_nExtFlg; // identifier xxxID + // either extended (the 29 LSB) + // or standard (the 11 LSB) + INT32U m_nID; // CAN ID + INT8U m_nDlc; // data length: + INT8U m_nDta[MAX_CHAR_IN_MESSAGE]; // data + INT8U m_nRtr; // rtr INT8U m_nfilhit; INT8U MCPCS; INT8U mcpMode; @@ -49,70 +49,73 @@ class MCP_CAN // private: private: - void mcp2515_reset(void); /* reset mcp2515 */ + void mcp2515_reset(void); // Soft Reset MCP2515 - INT8U mcp2515_readRegister(const INT8U address); /* read mcp2515's register */ + INT8U mcp2515_readRegister(const INT8U address); // Read MCP2515 register - void mcp2515_readRegisterS(const INT8U address, + void mcp2515_readRegisterS(const INT8U address, // Read MCP2515 successive registers INT8U values[], const INT8U n); - void mcp2515_setRegister(const INT8U address, /* set mcp2515's register */ + + void mcp2515_setRegister(const INT8U address, // Set MCP2515 register const INT8U value); - void mcp2515_setRegisterS(const INT8U address, /* set mcp2515's registers */ + void mcp2515_setRegisterS(const INT8U address, // Set MCP2515 successive registers const INT8U values[], const INT8U n); void mcp2515_initCANBuffers(void); - void mcp2515_modifyRegister(const INT8U address, /* set bit of one register */ + void mcp2515_modifyRegister(const INT8U address, // Set specific bit(s) of a register const INT8U mask, const INT8U data); - INT8U mcp2515_readStatus(void); /* read mcp2515's Status */ - INT8U mcp2515_setCANCTRL_Mode(const INT8U newmode); /* set mode */ - INT8U mcp2515_configRate(const INT8U canSpeed, /* set baudrate */ + INT8U mcp2515_readStatus(void); // Read MCP2515 Status + INT8U mcp2515_setCANCTRL_Mode(const INT8U newmode); // Set mode + INT8U mcp2515_configRate(const INT8U canSpeed, // Set baudrate const INT8U canClock); - INT8U mcp2515_init(const INT8U canIDMode, /* mcp2515init */ + INT8U mcp2515_init(const INT8U canIDMode, // Initialize Controller const INT8U canSpeed, const INT8U canClock); - - void mcp2515_write_id( const INT8U mcp_addr, /* write can id */ + + void mcp2515_write_mf( const INT8U mcp_addr, // Write CAN Mask or Filter + const INT8U ext, + const INT32U id ); + + void mcp2515_write_id( const INT8U mcp_addr, // Write CAN ID const INT8U ext, const INT32U id ); - void mcp2515_read_id( const INT8U mcp_addr, /* read can id */ + void mcp2515_read_id( const INT8U mcp_addr, // Read CAN ID INT8U* ext, INT32U* id ); - void mcp2515_write_canMsg( const INT8U buffer_sidh_addr ); /* write can msg */ - void mcp2515_read_canMsg( const INT8U buffer_sidh_addr); /* read can msg */ - INT8U mcp2515_getNextFreeTXBuf(INT8U *txbuf_n); /* get Next free txbuf */ + void mcp2515_write_canMsg( const INT8U buffer_sidh_addr ); // Write CAN message + void mcp2515_read_canMsg( const INT8U buffer_sidh_addr); // Read CAN message + INT8U mcp2515_getNextFreeTXBuf(INT8U *txbuf_n); // Find empty transmit buffer /* -* can operator function +* CAN operator function */ - INT8U setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData); /* set message */ - INT8U clearMsg(); /* clear all message to zero */ - INT8U readMsg(); /* read message */ - INT8U sendMsg(); /* send message */ + INT8U setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData); // Set message + INT8U clearMsg(); // Clear all message to zero + INT8U readMsg(); // Read message + INT8U sendMsg(); // Send message public: MCP_CAN(INT8U _CS); - INT8U begin(INT8U idmodeset, INT8U speedset, INT8U clockset); /* init can */ - INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); /* init Masks */ - INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); /* init filters */ - INT8U setMode(INT8U opMode); /* Sets operational mode */ - INT8U sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf); /* send buf */ - INT8U readMsgBuf(INT32U *id, INT8U *len, INT8U *buf); /* read buf */ - INT8U checkReceive(void); /* if something received */ - INT8U checkError(void); /* if something error */ - INT32U getCanId(void); /* Will be depreciated soon... */ + INT8U begin(INT8U idmodeset, INT8U speedset, INT8U clockset); // Initilize controller prameters + INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); // Initilize Mask(s) + INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); // initilize Filter(s) + INT8U setMode(INT8U opMode); // Set operational mode + INT8U sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf); // Send message to transmit buffer + INT8U readMsgBuf(INT32U *id, INT8U *len, INT8U *buf); // Read message from receive buffer + INT8U checkReceive(void); // Check for received data + INT8U checkError(void); // Check for errors }; -extern MCP_CAN CAN; #endif /********************************************************************************************************* END FILE From 66eaf322569e608417b6fbc50977204ea6ccc76c Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 17 Jul 2014 05:19:34 -0400 Subject: [PATCH 22/40] Mask and Filter Demo for Standard IDs This demo only applies to standard IDs. --- .../Standard_MaskFilter.ino | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 examples/Standard_MaskFilter/Standard_MaskFilter.ino diff --git a/examples/Standard_MaskFilter/Standard_MaskFilter.ino b/examples/Standard_MaskFilter/Standard_MaskFilter.ino new file mode 100644 index 0000000..5220a78 --- /dev/null +++ b/examples/Standard_MaskFilter/Standard_MaskFilter.ino @@ -0,0 +1,94 @@ + +// MCP2515 Mask and Filter example for standard CAN message frames. +// Written by Cory J. Fowler (20140717) + +/*********************************************************************************** +If you send the following standard IDs below to an Arduino loaded with this sketch +you will find that 0x102 and 0x105 will not get in. + +ID in Hex - Two Data Bytes! - Filter/Mask in HEX + 0x100 + 0000 0000 0000 0000 = 0x01000000 + 0x101 + 0000 0000 0000 0000 = 0x01010000 + 0x102 + 0000 0000 0000 0000 = 0x01020000 This example will NOT be receiving this ID + 0x103 + 0000 0000 0000 0000 = 0x01030000 + 0x104 + 0000 0000 0000 0000 = 0x01040000 + 0x105 + 0000 0000 0000 0000 = 0x01050000 This example will NOT be receiving this ID + 0x106 + 0000 0000 0000 0000 = 0x01060000 + 0x107 + 0000 0000 0000 0000 = 0x01070000 + + This mask will check the filters against ID bit 8 and ID bits 3-0. + MASK + 0000 0000 0000 0000 = 0x010F0000 + + If there is an explicit filter match to those bits, the message will be passed to the + receive buffer and the interrupt pin will be set. + This example will NOT be exclusive to ONLY the above frame IDs, for that a mask such + as the below would be used: + MASK + 0000 0000 0000 0000 = 0x07FF0000 + + This mask will check the filters against all ID bits and the first data byte: + MASK + 1111 1111 0000 0000 = 0x07FFFF00 + If you use this mask and do not touch the filters below, you will find that your first + data byte must be 0x00 for the message to enter the receive buffer. + + At the moment, to disable a filter or mask, copy the value of a used filter or mask. + + Data bytes are ONLY checked when the MCP2515 is in 'MCP_STDEXT' mode via the begin + function, otherwise ('MCP_STD') only the ID is checked. +***********************************************************************************/ + + +#include +#include + +long unsigned int rxId; +unsigned char len = 0; +unsigned char rxBuf[8]; + +MCP_CAN CAN0(10); // Set CS to pin 10 + +void setup() +{ + Serial.begin(115200); + if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n"); + else Serial.print("MCP2515 Init Failed!!\r\n"); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted + pinMode(2, INPUT); // Setting pin 2 for /INT input + + + CAN0.init_Mask(0,0,0x010F0000); // Init first mask... + CAN0.init_Filt(0,0,0x01000000); // Init first filter... + CAN0.init_Filt(1,0,0x01010000); // Init second filter... + + CAN0.init_Mask(1,0,0x010F0000); // Init second mask... + CAN0.init_Filt(2,0,0x01030000); // Init third filter... + CAN0.init_Filt(3,0,0x01040000); // Init fouth filter... + CAN0.init_Filt(4,0,0x01060000); // Init fifth filter... + CAN0.init_Filt(5,0,0x01070000); // Init sixth filter... + + Serial.println("MCP2515 Library Mask & Filter Example..."); +} + +void loop() +{ + if(!digitalRead(2)) // If pin 2 is low, read receive buffer + { + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + Serial.print("ID: "); + Serial.print(rxId, HEX); + Serial.print(" Data: "); + for(int i = 0; i Date: Thu, 17 Jul 2014 05:38:18 -0400 Subject: [PATCH 23/40] Mask and Filter Demo for Extended IDs This demo only applies to extended IDs. --- .../Extended_MaskFilter.ino | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 examples/Extended_MaskFilter/Extended_MaskFilter.ino diff --git a/examples/Extended_MaskFilter/Extended_MaskFilter.ino b/examples/Extended_MaskFilter/Extended_MaskFilter.ino new file mode 100644 index 0000000..651970e --- /dev/null +++ b/examples/Extended_MaskFilter/Extended_MaskFilter.ino @@ -0,0 +1,88 @@ + +// MCP2515 Mask and Filter example for extended CAN message frames. +// Written by Cory J. Fowler (20140717) + +/*********************************************************************************** +If you send the following extended IDs below to an Arduino loaded with this sketch +you will find that 0x00FFCC00 and 0x00FF9900 will not get in. + + ID in Hex is the same as the Filter in Hex. + 0x00FFEE00 + 0x00FFDD00 + 0x00FFCC00 This example will NOT be receiving this ID + 0x00FFBB00 + 0x00FFAA00 + 0x00FF9900 This example will NOT be receiving this ID + 0x00FF8800 + 0x00FF7700 + + This mask will check the filters against ID bits 23 through 8. + (Those familiar with J1939 might see why I used this mask.) + MASK = 0x00FFFF00 + If there is an explicit filter match to those bits, the message will be passed to the + receive buffer and the interrupt pin will be set. + + This example will NOT be exclusive to ONLY the above message IDs, for that a mask such + as the below would be used: + MASK = 0x1FFFFFFF + + At the moment, to disable a filter or mask, copy the value of a used filter or mask. + +***********************************************************************************/ + + +#include +#include + +long unsigned int rxId; +unsigned char len = 0; +unsigned char rxBuf[8]; + +MCP_CAN CAN0(10); // Set CS to pin 10 + +void setup() +{ + Serial.begin(115200); + if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n"); + else Serial.print("MCP2515 Init Failed!!\r\n"); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted + pinMode(2, INPUT); // Setting pin 2 for /INT input + + + CAN0.init_Mask(0,1,0x00FFFF00); // Init first mask... + CAN0.init_Filt(0,1,0x00FFEE00); // Init first filter... + CAN0.init_Filt(1,1,0x00FFDD00); // Init second filter... + + CAN0.init_Mask(1,1,0x00FFFF00); // Init second mask... + CAN0.init_Filt(2,1,0x00FFBB00); // Init third filter... + CAN0.init_Filt(3,1,0x00FFAA00); // Init fouth filter... + CAN0.init_Filt(4,1,0x00FF8800); // Init fifth filter... + CAN0.init_Filt(5,1,0x00FF7700); // Init sixth filter... + + Serial.println("MCP2515 Library Mask & Filter Example..."); +} + +void loop() +{ + if(!digitalRead(2)) // If pin 2 is low, read receive buffer + { + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + Serial.print("ID: "); + Serial.print(rxId, HEX); + Serial.print(" Data: "); + for(int i = 0; i Date: Thu, 17 Jul 2014 05:39:29 -0400 Subject: [PATCH 24/40] Rename examples/dual_CAN/dual_CAN.ino to examples/Dual_CAN/Dual_CAN.ino --- examples/{dual_CAN/dual_CAN.ino => Dual_CAN/Dual_CAN.ino} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{dual_CAN/dual_CAN.ino => Dual_CAN/Dual_CAN.ino} (100%) diff --git a/examples/dual_CAN/dual_CAN.ino b/examples/Dual_CAN/Dual_CAN.ino similarity index 100% rename from examples/dual_CAN/dual_CAN.ino rename to examples/Dual_CAN/Dual_CAN.ino From 47505d817efaef423de0407e54890da18f9fae85 Mon Sep 17 00:00:00 2001 From: Cory Date: Fri, 18 Jul 2014 02:19:17 -0400 Subject: [PATCH 25/40] Update and rename examples/send/send.ino to examples/CAN_send/CAN_send.ino Added sent message status information. Cleaned up descriptions. --- examples/CAN_send/CAN_send.ino | 36 ++++++++++++++++++++++++++++++++++ examples/send/send.ino | 26 ------------------------ 2 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 examples/CAN_send/CAN_send.ino delete mode 100644 examples/send/send.ino diff --git a/examples/CAN_send/CAN_send.ino b/examples/CAN_send/CAN_send.ino new file mode 100644 index 0000000..4ee6f54 --- /dev/null +++ b/examples/CAN_send/CAN_send.ino @@ -0,0 +1,36 @@ +// CAN Send Example +// + +#include +#include + +MCP_CAN CAN0(10); // Set CS to pin 10 + +void setup() +{ + Serial.begin(115200); + + // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. + if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); + else Serial.println("Error Initializing MCP2515..."); + + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted +} + +byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; + +void loop() +{ + // send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send + byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data); + if(sndStat == CAN_OK){ + Serial.println("Message Sent Successfully!"); + } else { + Serial.println("Error Sending Message..."); + } + delay(100); // send data per 100ms +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/examples/send/send.ino b/examples/send/send.ino deleted file mode 100644 index ec4adc5..0000000 --- a/examples/send/send.ino +++ /dev/null @@ -1,26 +0,0 @@ -// demo: CAN-BUS Shield, send data -#include -#include - -MCP_CAN CAN0(10); // Set CS to pin 10 - -void setup() -{ - Serial.begin(115200); - // Init CAN bus with 500kb/s baudrate at 16MHz with Mask and Filters disabled. - if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 init ok!!\r\n"); - else Serial.print("Can init fail!!\r\n"); - CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted -} - -unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; -void loop() -{ - // send data: id = 0x00, standrad flame, data len = 8, stmp: data buf - CAN0.sendMsgBuf(0x00, 0, 8, stmp); - delay(100); // send data per 100ms -} - -/********************************************************************************************************* - END FILE -*********************************************************************************************************/ From b3c88aaaa72c755fac5a39d3ca16c81e3917bc8f Mon Sep 17 00:00:00 2001 From: Cory Date: Fri, 18 Jul 2014 02:26:01 -0400 Subject: [PATCH 26/40] Update and rename examples/receive/receive.ino to examples/CAN_receive/CAN_receive.ino Added initialization information to detail success or failure. --- .../CAN_receive.ino} | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) rename examples/{receive/receive.ino => CAN_receive/CAN_receive.ino} (74%) diff --git a/examples/receive/receive.ino b/examples/CAN_receive/CAN_receive.ino similarity index 74% rename from examples/receive/receive.ino rename to examples/CAN_receive/CAN_receive.ino index 525496b..06463de 100644 --- a/examples/receive/receive.ino +++ b/examples/CAN_receive/CAN_receive.ino @@ -1,4 +1,6 @@ -// demo: CAN-BUS Shield, receive data +// CAN Receive Example +// + #include #include @@ -12,9 +14,15 @@ MCP_CAN CAN0(10); // Set CS to pin 10 void setup() { Serial.begin(115200); - CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ); // init CAN Bus with 500kb/s baudrate at 16MHz with Mask & Filters disabled + + // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. + if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); + else Serial.println("Error Initializing MCP2515..."); + CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data. + pinMode(2, INPUT); // Setting pin 2 for /INT input + Serial.println("MCP2515 Library Receive Example..."); } @@ -23,15 +31,14 @@ void loop() if(!digitalRead(2)) // If pin 2 is low, read receive buffer { CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) -// rxId = CAN0.getCanId(); // Function will be depreciated soon due to readMsgBuf now returning ID - Serial.print("ID: "); + Serial.print("ID: "); // Print the message ID. Serial.print(rxId, HEX); Serial.print(" Data: "); - for(int i = 0; i Date: Tue, 16 Sep 2014 04:23:23 -0400 Subject: [PATCH 27/40] Updated, Read Description... During initialization, the MCP2515 is put in loopback mode. This seems to allow the masks and filters to set without undesired behavior when connected to an operating, utilized bus. Also uncommented the section for 8MHz crystal baud rate selection. --- mcp_can.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index abcc3e4..534ccff 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -5,7 +5,7 @@ Author: Loovee Contributor: Cory J. Fowler - 2014-1-16 + 2014-9-16 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -181,8 +181,6 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) set = 1; switch (canClock) { - // Add support for boards operating at 8MHz - /* case (MCP_8MHZ): switch (canSpeed) { @@ -263,7 +261,7 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) break; } break; - */ + case (MCP_16MHZ): switch (canSpeed) { @@ -476,7 +474,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const I mcp2515_reset(); - mcpMode = MCP_LISTENONLY; + mcpMode = MCP_LOOPBACK; res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0) @@ -625,7 +623,6 @@ void MCP_CAN::mcp2515_write_mf( const INT8U mcp_addr, const INT8U ext, const INT tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); tbufdata[MCP_EID8] = (INT8U) (canid >> 8); canid = (uint16_t)(id >> 16); - tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x07) << 5); tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); } From a9754fd60a0b565d15a488ea2cf0d29b4e2b4b1b Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 16 Sep 2014 04:25:42 -0400 Subject: [PATCH 28/40] Updated, Read Description... Added definitions for 8MHz crystal baud rate selection. --- mcp_can_dfs.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/mcp_can_dfs.h b/mcp_can_dfs.h index 1448580..995469f 100644 --- a/mcp_can_dfs.h +++ b/mcp_can_dfs.h @@ -238,6 +238,61 @@ #define MCP_MERRF 0x80 +/* + * Speed 8M + */ +#define MCP_8MHz_1000kBPS_CFG1 (0x00) +#define MCP_8MHz_1000kBPS_CFG2 (0x80) +#define MCP_8MHz_1000kBPS_CFG3 (0x80) + +#define MCP_8MHz_500kBPS_CFG1 (0x00) +#define MCP_8MHz_500kBPS_CFG2 (0x90) +#define MCP_8MHz_500kBPS_CFG3 (0x82) + +#define MCP_8MHz_250kBPS_CFG1 (0x00) +#define MCP_8MHz_250kBPS_CFG2 (0xB1) +#define MCP_8MHz_250kBPS_CFG3 (0x85) + +#define MCP_8MHz_200kBPS_CFG1 (0x00) +#define MCP_8MHz_200kBPS_CFG2 (0xB4) +#define MCP_8MHz_200kBPS_CFG3 (0x86) + +#define MCP_8MHz_125kBPS_CFG1 (0x01) +#define MCP_8MHz_125kBPS_CFG2 (0xB1) +#define MCP_8MHz_125kBPS_CFG3 (0x85) + +#define MCP_8MHz_100kBPS_CFG1 (0x01) +#define MCP_8MHz_100kBPS_CFG2 (0xB4) +#define MCP_8MHz_100kBPS_CFG3 (0x86) + +#define MCP_8MHz_80kBPS_CFG1 (0x01) +#define MCP_8MHz_80kBPS_CFG2 (0xBF) +#define MCP_8MHz_80kBPS_CFG3 (0x87) + +#define MCP_8MHz_50kBPS_CFG1 (0x03) +#define MCP_8MHz_50kBPS_CFG2 (0xB4) +#define MCP_8MHz_50kBPS_CFG3 (0x86) + +#define MCP_8MHz_40kBPS_CFG1 (0x03) +#define MCP_8MHz_40kBPS_CFG2 (0xBF) +#define MCP_8MHz_40kBPS_CFG3 (0x87) + +#define MCP_8MHz_31k25BPS_CFG1 (0x07) +#define MCP_8MHz_31k25BPS_CFG2 (0xA4) +#define MCP_8MHz_31k25BPS_CFG3 (0x84) + +#define MCP_8MHz_20kBPS_CFG1 (0x07) +#define MCP_8MHz_20kBPS_CFG2 (0xBF) +#define MCP_8MHz_20kBPS_CFG3 (0x87) + +#define MCP_8MHz_10kBPS_CFG1 (0x0F) +#define MCP_8MHz_10kBPS_CFG2 (0xBF) +#define MCP_8MHz_10kBPS_CFG3 (0x87) + +#define MCP_8MHz_5kBPS_CFG1 (0x1F) +#define MCP_8MHz_5kBPS_CFG2 (0xBF) +#define MCP_8MHz_5kBPS_CFG3 (0x87) + /* * speed 16M */ @@ -369,6 +424,7 @@ #define MCP_20MHZ 0 #define MCP_16MHZ 1 +#define MCP_8MHZ 2 #define CAN_5KBPS 1 #define CAN_10KBPS 2 From 43da2b48000e19275ee1fa90ead42979e8e7842d Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 16 Sep 2014 04:28:11 -0400 Subject: [PATCH 29/40] Updated, Read Description... Moved MCP2515 mode change to after all initialization commands. --- examples/Standard_MaskFilter/Standard_MaskFilter.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Standard_MaskFilter/Standard_MaskFilter.ino b/examples/Standard_MaskFilter/Standard_MaskFilter.ino index 5220a78..bfd693c 100644 --- a/examples/Standard_MaskFilter/Standard_MaskFilter.ino +++ b/examples/Standard_MaskFilter/Standard_MaskFilter.ino @@ -51,7 +51,6 @@ void setup() Serial.begin(115200); if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n"); else Serial.print("MCP2515 Init Failed!!\r\n"); - CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted pinMode(2, INPUT); // Setting pin 2 for /INT input @@ -66,6 +65,7 @@ void setup() CAN0.init_Filt(5,0,0x01070000); // Init sixth filter... Serial.println("MCP2515 Library Mask & Filter Example..."); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted } void loop() From 92b151a52d00a8b6b4f3b0b7d1a8c4a9cd826d7c Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 16 Sep 2014 04:29:02 -0400 Subject: [PATCH 30/40] Updated, Read Description... Moved MCP2515 mode change to after all initialization commands. --- examples/Extended_MaskFilter/Extended_MaskFilter.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/Extended_MaskFilter/Extended_MaskFilter.ino b/examples/Extended_MaskFilter/Extended_MaskFilter.ino index 651970e..e9dbd54 100644 --- a/examples/Extended_MaskFilter/Extended_MaskFilter.ino +++ b/examples/Extended_MaskFilter/Extended_MaskFilter.ino @@ -45,10 +45,8 @@ void setup() Serial.begin(115200); if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n"); else Serial.print("MCP2515 Init Failed!!\r\n"); - CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted pinMode(2, INPUT); // Setting pin 2 for /INT input - CAN0.init_Mask(0,1,0x00FFFF00); // Init first mask... CAN0.init_Filt(0,1,0x00FFEE00); // Init first filter... CAN0.init_Filt(1,1,0x00FFDD00); // Init second filter... @@ -60,6 +58,7 @@ void setup() CAN0.init_Filt(5,1,0x00FF7700); // Init sixth filter... Serial.println("MCP2515 Library Mask & Filter Example..."); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted } void loop() From 46bc9e07c79a226ce1bc091e1d9d0758fbb51f85 Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 16 Sep 2014 05:23:40 -0400 Subject: [PATCH 31/40] Updated, Read Description... Fixed a small bug I caused from the previous edit... --- mcp_can.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index 534ccff..0377da3 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -202,6 +202,12 @@ INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock) cfg3 = MCP_8MHz_20kBPS_CFG3; break; + case (CAN_31K25BPS): // 31.25KBPS + cfg1 = MCP_8MHz_31k25BPS_CFG1; + cfg2 = MCP_8MHz_31k25BPS_CFG2; + cfg3 = MCP_8MHz_31k25BPS_CFG3; + break; + case (CAN_40KBPS): // 40Kbps cfg1 = MCP_8MHz_40kBPS_CFG1; cfg2 = MCP_8MHz_40kBPS_CFG2; @@ -623,7 +629,7 @@ void MCP_CAN::mcp2515_write_mf( const INT8U mcp_addr, const INT8U ext, const INT tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); tbufdata[MCP_EID8] = (INT8U) (canid >> 8); canid = (uint16_t)(id >> 16); - tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x07) << 5); + tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07) << 5); tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); } @@ -905,7 +911,6 @@ INT8U MCP_CAN::sendMsg() } uiTimeOut = 0; mcp2515_write_canMsg( txbuf_n); -// mcp2515_start_transmit( txbuf_n ); // Depreciated this function call, the function calls another function and returns. mcp2515_modifyRegister( txbuf_n-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M ); do { From c9452a9a2e3782b86205d324bc5b3844ec8c0d52 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 22 Sep 2014 23:13:54 -0400 Subject: [PATCH 32/40] Updated Bit Rate Definitions --- mcp_can_dfs.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mcp_can_dfs.h b/mcp_can_dfs.h index 995469f..3e8bc14 100644 --- a/mcp_can_dfs.h +++ b/mcp_can_dfs.h @@ -426,18 +426,20 @@ #define MCP_16MHZ 1 #define MCP_8MHZ 2 +#define CAN_4K096BPS 0 #define CAN_5KBPS 1 #define CAN_10KBPS 2 #define CAN_20KBPS 3 -#define CAN_40KBPS 4 -#define CAN_50KBPS 5 -#define CAN_80KBPS 6 -#define CAN_100KBPS 7 -#define CAN_125KBPS 8 -#define CAN_200KBPS 9 -#define CAN_250KBPS 10 -#define CAN_500KBPS 11 -#define CAN_1000KBPS 12 +#define CAN_31K25BPS 4 +#define CAN_40KBPS 5 +#define CAN_50KBPS 6 +#define CAN_80KBPS 7 +#define CAN_100KBPS 8 +#define CAN_125KBPS 9 +#define CAN_200KBPS 10 +#define CAN_250KBPS 11 +#define CAN_500KBPS 12 +#define CAN_1000KBPS 13 #define CAN_OK (0) #define CAN_FAILINIT (1) From b227e2fe93f21e1862f4e24ff08e5a83e1b3704d Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 22 Sep 2014 23:18:11 -0400 Subject: [PATCH 33/40] Updated to reflect recent changes. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e7c555a..986ecca 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ This library is compatible with any shield or CAN interface that uses the MCP251 This version supports setting the ID mode of the MCP2515, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for a 20MHz crystal are yet to be confirmed but were calculated appropiately. +Baudrates for a 8MHz crystal are yet to be confirmed but were calculated appropiately. -The readMsgBuf() function now brings in the message ID, so the getCanId() function will be depreciated soon. +The readMsgBuf() function brings in the message ID. The getCanId() function is depreciated. -Using the setMode() function you can now put the MCP2515 into sleep, loop-back, or listen-only modes as well as normal operation from your sketch. -Right now the code defaults to listen-only mode after the begin() function runs. +Using the setMode() function you can now put the MCP2515 into sleep, loop-back, or listen-only modes as well as normal operation from your sketch. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus. Installation From 3dd9b4183f98f2e625c7d7409e7a1b28f1489cfd Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 22 Sep 2014 23:25:29 -0400 Subject: [PATCH 34/40] Updated to reflect recent changes. --- keywords.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index dcfcf74..2fec82e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -20,16 +20,18 @@ sendMsgBuf KEYWORD2 readMsgBuf KEYWORD2 checkReceive KEYWORD2 checkError KEYWORD2 -getCanId KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### +MCP_8MHz LITERAL1 MCP_16MHZ LITERAL1 MCP_20MHZ LITERAL1 +CAN_4K096BPS LITERAL1 CAN_5KBPS LITERAL1 CAN_10KBPS LITERAL1 CAN_20KBPS LITERAL1 +CAN_31K25BPS LITERAL1 CAN_40KBPS LITERAL1 CAN_50KBPS LITERAL1 CAN_80KBPS LITERAL1 From 8c71eacf9513de901140a96599cde01219b36ad6 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 22 Sep 2014 23:46:28 -0400 Subject: [PATCH 35/40] Removed references to second person. --- README.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 986ecca..b63cc43 100644 --- a/README.md +++ b/README.md @@ -6,22 +6,18 @@ MCP2515 Library for Arduino MCP2515 library v1.5 This library is compatible with any shield or CAN interface that uses the MCP2515 CAN protocol controller. -This version supports setting the ID mode of the MCP2515, the BAUD rate with clock speed with the begin() function. -Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. -Baudrates for a 20MHz crystal are yet to be confirmed but were calculated appropiately. -Baudrates for a 8MHz crystal are yet to be confirmed but were calculated appropiately. +This version supports setting the ID filter mode of the MCP2515, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for 8MHz and 20MHz crystals are yet to be confirmed but were calculated appropiately. The readMsgBuf() function brings in the message ID. The getCanId() function is depreciated. -Using the setMode() function you can now put the MCP2515 into sleep, loop-back, or listen-only modes as well as normal operation from your sketch. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus. +Using the setMode() function the sketch can now put the MCP2515 into sleep, loop-back, or listen-only modes as well as normal operation. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus. Installation ============== -Copy this into your "[...]/MySketches/libraries/" folder and restart the Arduino editor. +Copy this into the "[...]/MySketches/libraries/" folder and restart the Arduino editor. -NOTE: If you have an older version of the library (CAN_BUS_Shield) be sure to remove - it from the libraries folder or replace the files with those in this library to avoid conflicts. +NOTE: If an older version of the library exists (e.g. CAN_BUS_Shield) be sure to remove it from the libraries folder or replace the files with those in this library to avoid conflicts. *Happy Coding!* From 38e1af56085af727ddc32245447411257e40f839 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 28 Jan 2015 20:23:23 -0500 Subject: [PATCH 36/40] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b63cc43..0a02ff2 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ Work In Progress - Library Seems to Work... ============== -MCP2515 Library for Arduino +MCP_CAN Library for Arduino ============== -MCP2515 library v1.5 -This library is compatible with any shield or CAN interface that uses the MCP2515 CAN protocol controller. +MCP_CAN library v1.5 +This library is compatible with any shield or board that uses the MCP2515 or MCP25625 CAN protocol controller. -This version supports setting the ID filter mode of the MCP2515, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for 8MHz and 20MHz crystals are yet to be confirmed but were calculated appropiately. +This version supports setting the ID filter mode of the protocol controller, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for 8MHz and 20MHz crystals are yet to be confirmed but were calculated appropiately. The readMsgBuf() function brings in the message ID. The getCanId() function is depreciated. -Using the setMode() function the sketch can now put the MCP2515 into sleep, loop-back, or listen-only modes as well as normal operation. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus. +Using the setMode() function the sketch can now put the protocol controller into sleep, loop-back, or listen-only modes as well as normal operation. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus. Installation From 33666c3b878d339f66f92a0b6c180d2aaf0a4f06 Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 30 Jun 2016 14:55:41 -0400 Subject: [PATCH 37/40] Functions Added Added error functions and fixed return values on public functions as a well as added two other functions for differing user preferences. --- mcp_can.cpp | 267 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 222 insertions(+), 45 deletions(-) diff --git a/mcp_can.cpp b/mcp_can.cpp index 0377da3..9b97b8c 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -67,9 +67,8 @@ void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const I spi_readwrite(address); // mcp2515 has auto-increment of address-pointer for (i=0; i 0){ +#if DEBUG_MODE + Serial.print("Entering Configuration Mode Failure...\r\n"); +#endif + return res; +} + + if((num & 0x80000000) == 0x80000000) + ext = 1; + + if (num == 0){ + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData); + + } + else if(num == 1){ + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData); + } + else res = MCP2515_FAIL; + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0){ +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Mask Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Mask Successful!!!\r\n"); +#endif + return res; +} + /********************************************************************************************************* ** Function name: init_Filt ** Descriptions: Public function to set filter(s). @@ -859,6 +898,75 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) return res; } +/********************************************************************************************************* +** Function name: init_Filt +** Descriptions: Public function to set filter(s). +*********************************************************************************************************/ +INT8U MCP_CAN::init_Filt(INT8U num, INT32U ulData) +{ + INT8U res = MCP2515_OK; + INT8U ext = 0; + +#if DEBUG_MODE + Serial.print("Starting to Set Filter!!!\r\n"); +#endif + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Enter Configuration Mode Failure...\r\n"); +#endif + return res; + } + + if((num & 0x80000000) == 0x80000000) + ext = 1; + + switch( num ) + { + case 0: + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData); + break; + + case 1: + mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData); + break; + + case 2: + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData); + break; + + case 3: + mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData); + break; + + case 4: + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData); + break; + + case 5: + mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData); + break; + + default: + res = MCP2515_FAIL; + } + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Filter Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Filter Successfull!!!\r\n"); +#endif + + return res; +} + /********************************************************************************************************* ** Function name: setMsg ** Descriptions: Set can message, such as dlc, id, dta[] and so on @@ -871,6 +979,7 @@ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) m_nDlc = len; for(i = 0; i Date: Thu, 30 Jun 2016 14:56:16 -0400 Subject: [PATCH 38/40] Added functions --- mcp_can.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mcp_can.h b/mcp_can.h index 81ce067..80325ee 100644 --- a/mcp_can.h +++ b/mcp_can.h @@ -108,12 +108,19 @@ class MCP_CAN MCP_CAN(INT8U _CS); INT8U begin(INT8U idmodeset, INT8U speedset, INT8U clockset); // Initilize controller prameters INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); // Initilize Mask(s) - INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); // initilize Filter(s) + INT8U init_Mask(INT8U num, INT32U ulData); // Initilize Mask(s) + INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); // Initilize Filter(s) + INT8U init_Filt(INT8U num, INT32U ulData); // Initilize Filter(s) INT8U setMode(INT8U opMode); // Set operational mode INT8U sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf); // Send message to transmit buffer + INT8U sendMsgBuf(INT32U id, INT8U len, INT8U *buf); // Send message to transmit buffer + INT8U readMsgBuf(INT32U *id, INT8U *ext, INT8U *len, INT8U *buf); // Read message from receive buffer INT8U readMsgBuf(INT32U *id, INT8U *len, INT8U *buf); // Read message from receive buffer INT8U checkReceive(void); // Check for received data INT8U checkError(void); // Check for errors + INT8U getError(void); // Check for errors + INT8U errorCountRX(void); // Get error count + INT8U errorCountTX(void); // Get error count }; #endif From a00f46f41b2266357e1f8e29fd2c3e7aefae8974 Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 30 Jun 2016 21:42:52 -0400 Subject: [PATCH 39/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a02ff2..388ced5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Work In Progress - Library Seems to Work... +Sub-Development branch was copied to Master on June 30th, 2016. ============== MCP_CAN Library for Arduino From 48a40c5d2ba17186b8ea18fe897a5e239ed6a5e5 Mon Sep 17 00:00:00 2001 From: Cory Date: Fri, 24 Mar 2017 11:35:02 -0400 Subject: [PATCH 40/40] Sub-Development Branch NOT Maintained --- README.md | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/README.md b/README.md index 388ced5..5d02b98 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,5 @@ -Sub-Development branch was copied to Master on June 30th, 2016. +Sub-Development branch was copied to Master on June 30th, 2016 and is not being maintained and will be deleted soon. The master is current. ============== -MCP_CAN Library for Arduino -============== -MCP_CAN library v1.5 -This library is compatible with any shield or board that uses the MCP2515 or MCP25625 CAN protocol controller. - -This version supports setting the ID filter mode of the protocol controller, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for 8MHz and 20MHz crystals are yet to be confirmed but were calculated appropiately. - -The readMsgBuf() function brings in the message ID. The getCanId() function is depreciated. - -Using the setMode() function the sketch can now put the protocol controller into sleep, loop-back, or listen-only modes as well as normal operation. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus. - - -Installation -============== -Copy this into the "[...]/MySketches/libraries/" folder and restart the Arduino editor. - -NOTE: If an older version of the library exists (e.g. CAN_BUS_Shield) be sure to remove it from the libraries folder or replace the files with those in this library to avoid conflicts. - *Happy Coding!*