From 925f97c2c9789d398bdf238e87737bb557629911 Mon Sep 17 00:00:00 2001 From: loovee Date: Tue, 8 Jul 2014 13:55:49 +0800 Subject: [PATCH] version 1.2 --- .../receive_check/receive_check.ino | 4 +- .../receive_interrupt/receive_interrupt.ino | 5 +- {example => examples}/send/send.ino | 16 +- keywords.txt | 2 +- mcp_can.cpp | 307 +++++++----------- mcp_can.h | 50 +-- mcp_can_dfs.h | 89 ++--- 7 files changed, 209 insertions(+), 264 deletions(-) rename {example => examples}/receive_check/receive_check.ino (92%) rename {example => examples}/receive_interrupt/receive_interrupt.ino (93%) rename {example => examples}/send/send.ino (65%) diff --git a/example/receive_check/receive_check.ino b/examples/receive_check/receive_check.ino similarity index 92% rename from example/receive_check/receive_check.ino rename to examples/receive_check/receive_check.ino index a56688d..f8ba0f9 100644 --- a/example/receive_check/receive_check.ino +++ b/examples/receive_check/receive_check.ino @@ -1,6 +1,6 @@ // demo: CAN-BUS Shield, receive data with check mode // send data coming to fast, such as less than 10ms, you can use this way -// loovee, 2013-11-27 +// loovee, 2014-6-13 #include @@ -13,6 +13,8 @@ unsigned char buf[8]; char str[20]; +MCP_CAN CAN(10); // Set CS to pin 10 + void setup() { Serial.begin(115200); diff --git a/example/receive_interrupt/receive_interrupt.ino b/examples/receive_interrupt/receive_interrupt.ino similarity index 93% rename from example/receive_interrupt/receive_interrupt.ino rename to examples/receive_interrupt/receive_interrupt.ino index 58490e1..269b4b8 100644 --- a/example/receive_interrupt/receive_interrupt.ino +++ b/examples/receive_interrupt/receive_interrupt.ino @@ -1,10 +1,13 @@ // demo: CAN-BUS Shield, receive data with interrupt mode // when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode -// loovee, 2013-11-27 +// loovee, 2014-6-13 #include #include "mcp_can.h" +MCP_CAN CAN(10); // Set CS to pin 10 + + unsigned char Flag_Recv = 0; unsigned char len = 0; unsigned char buf[8]; diff --git a/example/send/send.ino b/examples/send/send.ino similarity index 65% rename from example/send/send.ino rename to examples/send/send.ino index 9cf3ca0..50b17cb 100644 --- a/example/send/send.ino +++ b/examples/send/send.ino @@ -1,13 +1,13 @@ // demo: CAN-BUS Shield, send data -// loovee, 2013-11-27 - #include #include +MCP_CAN CAN(10); // Set CS to pin 10 + void setup() { Serial.begin(115200); - + START_INIT: if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k @@ -23,14 +23,14 @@ START_INIT: } } +unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; void loop() { - unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; - - CAN.sendMsgBuf(0x00, 0, 8, stmp); // send data: id = 0x00, standrad flame, data len = 8, stmp: data buf - delay(10); // when the delay less than 20ms, you shield use receive_interrupt + // send data: id = 0x00, standrad flame, data len = 8, stmp: data buf + CAN.sendMsgBuf(0x00, 0, 8, stmp); + delay(100); // send data per 100ms } /********************************************************************************************************* END FILE -*********************************************************************************************************/ \ No newline at end of file +*********************************************************************************************************/ diff --git a/keywords.txt b/keywords.txt index 071cc06..e7128e8 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,7 +6,6 @@ # Datatypes (KEYWORD1) ####################################### MCP_CAN KEYWORD1 -CAN KEYWORD1 mcp_can_dfs KEYWORD1 mcp_can KEYWORD1 @@ -36,6 +35,7 @@ CAN_125KBPS LITERAL1 CAN_200KBPS LITERAL1 CAN_250KBPS LITERAL1 CAN_500KBPS LITERAL1 +CAN_1000KBPS LITERAL1 CAN_OK LITERAL1 CAN_FAILINIT LITERAL1 CAN_FAILTX LITERAL1 diff --git a/mcp_can.cpp b/mcp_can.cpp index edccca6..e7074f3 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -1,39 +1,34 @@ -/* - * mcp_can.cpp - * Library for SeeedStudio CANBUS SHIELD - * - * Copyright (c) Seeedstudio - * Spread by SeeedStudio - * Author : Loovee - * Create Time: 2012-4-24 - * Change Log : - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +/* + mcp_can.cpp + 2012 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + Contributor: Cory J. Fowler + 2014-1-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 + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ #include "mcp_can.h" #define spi_readwrite SPI.transfer #define spi_read() spi_readwrite(0x00) -MCP_CAN CAN; /********************************************************************************************************* ** Function name: mcp2515_reset ** Descriptions: reset the device -** input parameters: NONE -** Output parameters: NONE -** Returned value: NONE *********************************************************************************************************/ - void MCP_CAN::mcp2515_reset(void) { MCP2515_SELECT(); @@ -45,9 +40,6 @@ void MCP_CAN::mcp2515_reset(void) /********************************************************************************************************* ** Function name: mcp2515_readRegister ** Descriptions: read register -** input parameters: address : register address -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) { @@ -65,20 +57,16 @@ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) /********************************************************************************************************* ** Function name: mcp2515_readRegisterS ** Descriptions: read registerS -** input parameters: address : register address -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ -void MCP_CAN::mcp2515_readRegisterS(const INT8U address, - INT8U values[], - const INT8U n) +void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const INT8U n) { INT8U i; MCP2515_SELECT(); spi_readwrite(MCP_READ); spi_readwrite(address); // mcp2515 has auto-increment of address-pointer - for (i=0; i> 8); + canid = (uint16_t)(id >> 16); tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); - tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C )*8); + tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3); tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; - tbufdata[MCP_SIDH] = (INT8U) (canid / 32 ); + tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 ); } - else { - tbufdata[MCP_SIDH] = (INT8U) (canid / 8 ); - tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 )<<5); + else + { + tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); + tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5); tbufdata[MCP_EID0] = 0; tbufdata[MCP_EID8] = 0; } @@ -452,13 +418,8 @@ void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, /********************************************************************************************************* ** Function name: mcp2515_read_id ** Descriptions: read can id -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ -void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, - INT8U* ext, - INT32U* id ) +void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) { INT8U tbufdata[4]; @@ -469,11 +430,12 @@ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, *id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5); - if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M ) { + if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M ) + { /* extended id */ *id = (*id<<2) + (tbufdata[MCP_SIDL] & 0x03); - *id <<= 16; - *id = *id + (unsigned)(tbufdata[MCP_EID8]<<8) + tbufdata[MCP_EID0]; + *id = (*id<<8) + tbufdata[MCP_EID8]; + *id = (*id<<8) + tbufdata[MCP_EID0]; *ext = 1; } } @@ -481,27 +443,24 @@ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, /********************************************************************************************************* ** Function name: mcp2515_write_canMsg ** Descriptions: write msg -** input parameters: NONE -** Output parameters: NONE -** Returned value: NONE *********************************************************************************************************/ void MCP_CAN::mcp2515_write_canMsg( const INT8U buffer_sidh_addr) { INT8U mcp_addr; mcp_addr = buffer_sidh_addr; mcp2515_setRegisterS(mcp_addr+5, m_nDta, m_nDlc ); /* write data bytes */ - if ( m_nRtr == 1) m_nDlc |= MCP_RTR_MASK; /* if RTR set bit in byte */ - mcp2515_setRegister( (mcp_addr+4), m_nDlc ); /* write the RTR and DLC */ - mcp2515_write_id( mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */ + if ( m_nRtr == 1) /* if RTR set bit in byte */ + { + 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 */ } /********************************************************************************************************* ** Function name: mcp2515_read_canMsg ** Descriptions: read message -** input parameters: NONE -** Output parameters: NONE -** Returned value: NONE *********************************************************************************************************/ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read can msg */ { @@ -528,9 +487,6 @@ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ void MCP_CAN::mcp2515_start_transmit(const INT8U mcp_addr) /* start transmit */ { @@ -540,9 +496,6 @@ void MCP_CAN::mcp2515_start_transmit(const INT8U mcp_addr) /* start /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */ { @@ -565,20 +518,25 @@ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get N return res; } +/********************************************************************************************************* +** Function name: set CS +** Descriptions: init CS pin and set UNSELECTED +*********************************************************************************************************/ +MCP_CAN::MCP_CAN(INT8U _CS) +{ + SPICS = _CS; + pinMode(SPICS, OUTPUT); + MCP2515_UNSELECT(); +} + /********************************************************************************************************* ** Function name: init ** Descriptions: init can and set speed -** input parameters: speedset : can boadrate -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::begin(INT8U speedset) { INT8U res; - MCP2515_UNSELECT(); /* disable chip select to begin with */ - pinMode(SPICS, OUTPUT); /* make chip select pin an output */ - SPI.begin(); res = mcp2515_init(speedset); if (res == MCP2515_OK) return CAN_OK; @@ -588,11 +546,6 @@ INT8U MCP_CAN::begin(INT8U speedset) /********************************************************************************************************* ** Function name: init_Mask ** Descriptions: init canid Masks -** input parameters: num : Mask number - ext : if ext id - ulData: data -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) { @@ -633,11 +586,6 @@ INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData) /********************************************************************************************************* ** Function name: init_Filt ** Descriptions: init canid filters -** input parameters: num : Filters number - ext : if ext id - ulData: data -** Output parameters: NONE -** Returned value: ifsuccess *********************************************************************************************************/ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) { @@ -702,9 +650,6 @@ 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 -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) { @@ -720,9 +665,6 @@ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) /********************************************************************************************************* ** Function name: clearMsg ** Descriptions: set all message to zero -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::clearMsg() { @@ -740,9 +682,6 @@ INT8U MCP_CAN::clearMsg() /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::sendMsg() { @@ -754,17 +693,23 @@ INT8U MCP_CAN::sendMsg() uiTimeOut++; } while (res == MCP_ALLTXBUSY && (uiTimeOut < TIMEOUTVALUE)); - if(uiTimeOut == TIMEOUTVALUE) return CAN_GETTXBFTIMEOUT; /* get tx buff time out */ + if(uiTimeOut == TIMEOUTVALUE) + { + return CAN_GETTXBFTIMEOUT; /* get tx buff time out */ + } uiTimeOut = 0; mcp2515_write_canMsg( txbuf_n); mcp2515_start_transmit( txbuf_n ); do { uiTimeOut++; - res1= CAN.mcp2515_readRegister(txbuf_n); /* read send buff ctrl reg */ + res1= mcp2515_readRegister(txbuf_n); /* read send buff ctrl reg */ res1 = res1 & 0x08; }while(res1 && (uiTimeOut < TIMEOUTVALUE)); - if(uiTimeOut == TIMEOUTVALUE) return CAN_SENDMSGTIMEOUT; /* send msg timeout */ + if(uiTimeOut == TIMEOUTVALUE) /* send msg timeout */ + { + return CAN_SENDMSGTIMEOUT; + } return CAN_OK; } @@ -772,12 +717,6 @@ INT8U MCP_CAN::sendMsg() /********************************************************************************************************* ** Function name: sendMsgBuf ** Descriptions: send buf -** input parameters: id : can id - ext : if ext - len : buf length - *buf: data buf -** Output parameters: NONE -** Returned value: NONE *********************************************************************************************************/ INT8U MCP_CAN::sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf) { @@ -788,9 +727,6 @@ INT8U MCP_CAN::sendMsgBuf(INT32U id, INT8U ext, INT8U len, INT8U *buf) /********************************************************************************************************* ** Function name: readMsg ** Descriptions: read message -** input parameters: NONE -** Output parameters: NONE -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::readMsg() { @@ -798,17 +734,20 @@ INT8U MCP_CAN::readMsg() stat = mcp2515_readStatus(); - if ( stat & MCP_STAT_RX0IF ) { /* Msg in Buffer 0 */ + if ( stat & MCP_STAT_RX0IF ) /* Msg in Buffer 0 */ + { mcp2515_read_canMsg( MCP_RXBUF_0); mcp2515_modifyRegister(MCP_CANINTF, MCP_RX0IF, 0); res = CAN_OK; } - else if ( stat & MCP_STAT_RX1IF ) { /* Msg in Buffer 1 */ + else if ( stat & MCP_STAT_RX1IF ) /* Msg in Buffer 1 */ + { mcp2515_read_canMsg( MCP_RXBUF_1); mcp2515_modifyRegister(MCP_CANINTF, MCP_RX1IF, 0); res = CAN_OK; } - else { + else + { res = CAN_NOMSG; } return res; @@ -817,9 +756,6 @@ INT8U MCP_CAN::readMsg() /********************************************************************************************************* ** Function name: readMsgBuf ** Descriptions: read message buf -** input parameters: len: data length -** Output parameters: buf: data buf -** Returned value: if success *********************************************************************************************************/ INT8U MCP_CAN::readMsgBuf(INT8U *len, INT8U buf[]) { @@ -834,18 +770,17 @@ INT8U MCP_CAN::readMsgBuf(INT8U *len, INT8U buf[]) /********************************************************************************************************* ** Function name: checkReceive ** Descriptions: check if got something -** input parameters: NONE -** Output parameters: NONE -** Returned value: if something *********************************************************************************************************/ INT8U MCP_CAN::checkReceive(void) { INT8U res; res = mcp2515_readStatus(); /* RXnIF in Bit 1 and 0 */ - if ( res & MCP_STAT_RXIF_MASK ) { + if ( res & MCP_STAT_RXIF_MASK ) + { return CAN_MSGAVAIL; } - else { + else + { return CAN_NOMSG; } } @@ -853,18 +788,17 @@ INT8U MCP_CAN::checkReceive(void) /********************************************************************************************************* ** Function name: checkError ** Descriptions: if something error -** input parameters: NONE -** Output parameters: NONE -** Returned value: NONE *********************************************************************************************************/ INT8U MCP_CAN::checkError(void) { INT8U eflg = mcp2515_readRegister(MCP_EFLG); - if ( eflg & MCP_EFLG_ERRORMASK ) { + if ( eflg & MCP_EFLG_ERRORMASK ) + { return CAN_CTRLERROR; } - else { + else + { return CAN_OK; } } @@ -872,14 +806,11 @@ INT8U MCP_CAN::checkError(void) /********************************************************************************************************* ** Function name: getCanId ** Descriptions: when receive something ,u can get the can id!! -** input parameters: NONE -** Output parameters: NONE -** Returned value: NONE *********************************************************************************************************/ INT32U MCP_CAN::getCanId(void) { - return m_nID; + return m_nID; } /********************************************************************************************************* END FILE -*********************************************************************************************************/ +*********************************************************************************************************/ \ No newline at end of file diff --git a/mcp_can.h b/mcp_can.h index 1dc90db..e4144c5 100644 --- a/mcp_can.h +++ b/mcp_can.h @@ -1,25 +1,25 @@ -/* - * mcp_can.h - * Library for SeeedStudio CANBUS SHIELD - * - * Copyright (c) Seeedstudio - * Spread by SeeedStudio - * Author : Loovee - * Create Time: 2012-4-24 - * Change Log : - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +/* + mcp_can.h + 2012 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + Contributor: Cory J. Fowler + 2014-1-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 + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ #ifndef _MCP2515_H_ #define _MCP2515_H_ @@ -38,6 +38,7 @@ class MCP_CAN INT8U m_nDta[MAX_CHAR_IN_MESSAGE]; /* data */ INT8U m_nRtr; /* rtr */ INT8U m_nfilhit; + INT8U SPICS; /* * mcp2515 driver function @@ -74,7 +75,7 @@ class MCP_CAN 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 ); @@ -91,7 +92,9 @@ class MCP_CAN INT8U clearMsg(); /* clear all message to zero */ INT8U readMsg(); /* read message */ INT8U sendMsg(); /* send message */ + public: + MCP_CAN(INT8U _CS); INT8U begin(INT8U speedset); /* init can */ INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData); /* init Masks */ INT8U init_Filt(INT8U num, INT8U ext, INT32U ulData); /* init filters */ @@ -102,7 +105,6 @@ public: INT32U getCanId(void); /* get can id when receive */ }; -extern MCP_CAN CAN; #endif /********************************************************************************************************* END FILE diff --git a/mcp_can_dfs.h b/mcp_can_dfs.h index 84cddc3..fa20378 100644 --- a/mcp_can_dfs.h +++ b/mcp_can_dfs.h @@ -3,7 +3,8 @@ 2012 Copyright (c) Seeed Technology Inc. All right reserved. Author:Loovee - 2012-4-24 + Contributor: Cory J. Fowler + 2014-1-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 @@ -22,7 +23,7 @@ #ifndef _MCP2515DFS_H_ #define _MCP2515DFS_H_ -#include +#include #include #include @@ -31,11 +32,12 @@ #endif #ifndef INT8U -#define INT8U unsigned char +#define INT8U byte #endif // if print debug information -#define DEBUG_MODE 1 +#define DEBUG_MODE 0 + /* * Begin mt */ @@ -116,8 +118,8 @@ #define MCP_RXF5SIDL 0x19 #define MCP_RXF5EID8 0x1A #define MCP_RXF5EID0 0x1B -#define MCP_TEC 0x1C -#define MCP_REC 0x1D +#define MCP_TEC 0x1C +#define MCP_REC 0x1D #define MCP_RXM0SIDH 0x20 #define MCP_RXM0SIDL 0x21 #define MCP_RXM0EID8 0x22 @@ -129,8 +131,8 @@ #define MCP_CNF3 0x28 #define MCP_CNF2 0x29 #define MCP_CNF1 0x2A -#define MCP_CANINTE 0x2B -#define MCP_CANINTF 0x2C +#define MCP_CANINTE 0x2B +#define MCP_CANINTF 0x2C #define MCP_EFLG 0x2D #define MCP_TXB0CTRL 0x30 #define MCP_TXB1CTRL 0x40 @@ -147,7 +149,7 @@ #define MCP_NO_INT 0x00 // Disable all interrupts #define MCP_TX01_MASK 0x14 -#define MCP_TX_MASK 0x54 +#define MCP_TX_MASK 0x54 /* * Define SPI Instruction Set @@ -246,51 +248,55 @@ */ #define MCP_16MHz_1000kBPS_CFG1 (0x00) #define MCP_16MHz_1000kBPS_CFG2 (0xD0) -#define MCP_16MHz_1000kBPS_CFG3 (0x02) +#define MCP_16MHz_1000kBPS_CFG3 (0x82) #define MCP_16MHz_500kBPS_CFG1 (0x00) #define MCP_16MHz_500kBPS_CFG2 (0xF0) -#define MCP_16MHz_500kBPS_CFG3 (0x06) +#define MCP_16MHz_500kBPS_CFG3 (0x86) #define MCP_16MHz_250kBPS_CFG1 (0x41) #define MCP_16MHz_250kBPS_CFG2 (0xF1) -#define MCP_16MHz_250kBPS_CFG3 (0x05) +#define MCP_16MHz_250kBPS_CFG3 (0x85) #define MCP_16MHz_200kBPS_CFG1 (0x01) #define MCP_16MHz_200kBPS_CFG2 (0xFA) -#define MCP_16MHz_200kBPS_CFG3 (0x07) +#define MCP_16MHz_200kBPS_CFG3 (0x87) #define MCP_16MHz_125kBPS_CFG1 (0x03) #define MCP_16MHz_125kBPS_CFG2 (0xF0) -#define MCP_16MHz_125kBPS_CFG3 (0x06) +#define MCP_16MHz_125kBPS_CFG3 (0x86) #define MCP_16MHz_100kBPS_CFG1 (0x03) #define MCP_16MHz_100kBPS_CFG2 (0xFA) -#define MCP_16MHz_100kBPS_CFG3 (0x07) +#define MCP_16MHz_100kBPS_CFG3 (0x87) #define MCP_16MHz_80kBPS_CFG1 (0x03) #define MCP_16MHz_80kBPS_CFG2 (0xFF) -#define MCP_16MHz_80kBPS_CFG3 (0x07) +#define MCP_16MHz_80kBPS_CFG3 (0x87) #define MCP_16MHz_50kBPS_CFG1 (0x07) #define MCP_16MHz_50kBPS_CFG2 (0xFA) -#define MCP_16MHz_50kBPS_CFG3 (0x07) +#define MCP_16MHz_50kBPS_CFG3 (0x87) #define MCP_16MHz_40kBPS_CFG1 (0x07) #define MCP_16MHz_40kBPS_CFG2 (0xFF) -#define MCP_16MHz_40kBPS_CFG3 (0x07) +#define MCP_16MHz_40kBPS_CFG3 (0x87) + +#define MCP_16MHz_31k25BPS_CFG1 (0x0F) +#define MCP_16MHz_31k25BPS_CFG2 (0xF1) +#define MCP_16MHz_31k25BPS_CFG3 (0x85) #define MCP_16MHz_20kBPS_CFG1 (0x0F) #define MCP_16MHz_20kBPS_CFG2 (0xFF) -#define MCP_16MHz_20kBPS_CFG3 (0x07) +#define MCP_16MHz_20kBPS_CFG3 (0x87) #define MCP_16MHz_10kBPS_CFG1 (0x1F) #define MCP_16MHz_10kBPS_CFG2 (0xFF) -#define MCP_16MHz_10kBPS_CFG3 (0x07) +#define MCP_16MHz_10kBPS_CFG3 (0x87) #define MCP_16MHz_5kBPS_CFG1 (0x3F) #define MCP_16MHz_5kBPS_CFG2 (0xFF) -#define MCP_16MHz_5kBPS_CFG3 (0x07) +#define MCP_16MHz_5kBPS_CFG3 (0x87) @@ -301,7 +307,7 @@ #define MCP_RXBUF_0 (MCP_RXB0SIDH) #define MCP_RXBUF_1 (MCP_RXB1SIDH) -#define SPICS 10 +//#define SPICS 10 #define MCP2515_SELECT() digitalWrite(SPICS, LOW) #define MCP2515_UNSELECT() digitalWrite(SPICS, HIGH) @@ -331,29 +337,30 @@ #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) -#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) +#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) #define CAN_MAX_CHAR_IN_MESSAGE (8) #endif /********************************************************************************************************* END FILE -*********************************************************************************************************/ \ No newline at end of file +*********************************************************************************************************/