/* 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) #define CHECK_RXBID(rxbid) do { \ if (!((rxbid == 0) || (rxbid == 1))) { \ return CAN_FAIL; \ } \ } while(false) #define CHECK_LEN(len) do { \ if (len > 8) { \ return CAN_FAIL; \ } \ } while(false) #define CHECK_TXBID(txbid) do { \ if (!((txbid == 0) || (txbid == 1) || (txbid == 2))) { \ return CAN_FAIL; \ } \ } while(false) /********************************************************************************************************* ** Function name: mcp2515_reset ** Descriptions: reset the device *********************************************************************************************************/ void MCP_CAN::mcp2515_reset(void) { MCP2515_SELECT(); spi_readwrite(MCP_RESET); MCP2515_UNSELECT(); delay(10); } /********************************************************************************************************* ** Function name: mcp2515_readRegister ** Descriptions: read register *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) { INT8U ret; MCP2515_SELECT(); spi_readwrite(MCP_READ); spi_readwrite(address); ret = spi_read(); MCP2515_UNSELECT(); return ret; } /********************************************************************************************************* ** Function name: mcp2515_readRegisterS ** Descriptions: read registerS *********************************************************************************************************/ 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 TXBnD7 */ a1 = MCP_TXB0CTRL; a2 = MCP_TXB1CTRL; a3 = MCP_TXB2CTRL; for (i = 0; i < 14; i++) { /* in-buffer loop */ mcp2515_setRegister(a1, 0); mcp2515_setRegister(a2, 0); mcp2515_setRegister(a3, 0); a1++; a2++; a3++; } mcp2515_setRegister(MCP_RXB0CTRL, 0x00); mcp2515_setRegister(MCP_RXB1CTRL, 0xff); } /********************************************************************************************************* ** Function name: mcp2515_init ** Descriptions: init the device *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp2515init */ { INT8U res; mcp2515_reset(); res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0) { #if DEBUG_MODE Serial.print("Enter setting mode fall\r\n"); #endif return res; } #if DEBUG_MODE Serial.print("Enter setting mode success \r\n"); #endif if(mcp2515_configRate(canSpeed)) { #if DEBUG_MODE Serial.print("set rate fall!!\r\n"); #endif return res; } #if DEBUG_MODE Serial.print("set rate success!!\r\n"); #endif if ( res == MCP2515_OK ) { /* init canbuffers */ mcp2515_initCANBuffers(); /* interrupt mode */ mcp2515_setRegister(MCP_CANINTE, 0x00); /* enter normal mode */ res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); if(res) { #if DEBUG_MODE Serial.print("Enter Normal Mode Fall!!\r\n"); #endif return res; } #if DEBUG_MODE Serial.print("Enter Normal Mode Success!!\r\n"); #endif } return res; } /********************************************************************************************************* ** Function name: mcp2515_write_id ** Descriptions: write can id *********************************************************************************************************/ void MCP_CAN::mcp2515_write_id( 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_SIDH] = (INT8U) (canid >> 3 ); tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5); tbufdata[MCP_EID0] = 0; tbufdata[MCP_EID8] = 0; } mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); } /********************************************************************************************************* ** Function name: mcp2515_read_id ** Descriptions: read can id *********************************************************************************************************/ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) { INT8U tbufdata[4]; *ext = 0; *id = 0; mcp2515_readRegisterS( mcp_addr, tbufdata, 4 ); *id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5); if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M ) { /* extended id */ *id = (*id<<2) + (tbufdata[MCP_SIDL] & 0x03); *id = (*id<<8) + tbufdata[MCP_EID8]; *id = (*id<<8) + tbufdata[MCP_EID0]; *ext = 1; } } /********************************************************************************************************* ** Function name: mcp2515_write_canMsg ** Descriptions: write msg *********************************************************************************************************/ void MCP_CAN::mcp2515_write_canMsg( INT8U txbid, INT32U id, INT8U len, INT8U *pData, INT8U ext, INT8U rtr ) { INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL }; INT8U mcp_addr = ctrlregs[txbid] + 1; mcp2515_setRegisterS(mcp_addr+5, pData, len ); /* write data bytes */ if ( rtr == 1) /* if RTR set bit in byte */ { len |= MCP_RTR_MASK; } mcp2515_setRegister((mcp_addr+4), len ); /* write the RTR and DLC */ mcp2515_write_id(mcp_addr, ext, id ); /* write CAN id */ } /********************************************************************************************************* ** Function name: mcp2515_read_canMsg ** Descriptions: read message *********************************************************************************************************/ void MCP_CAN::mcp2515_read_canMsg( INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf, INT8U *p_ext, INT8U *p_rtr) /* read can msg */ { INT8U mcp_addr = (rxbid == 0)? MCP_RXBUF_0 : MCP_RXBUF_1; INT8U ctrl; mcp2515_read_id( mcp_addr, p_ext, p_id ); ctrl = mcp2515_readRegister( mcp_addr-1 ); *p_len = mcp2515_readRegister( mcp_addr+4 ); if ((ctrl & 0x08)) { *p_rtr = 1; } else { *p_rtr = 0; } *p_len &= MCP_DLC_MASK; mcp2515_readRegisterS( mcp_addr+5, p_buf, *p_len ); } /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message *********************************************************************************************************/ void MCP_CAN::mcp2515_start_transmit(INT8U txbid) /* start transmit */ { INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL }; INT8U mcp_addr = ctrlregs[txbid]; mcp2515_modifyRegister( mcp_addr, MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M ); } /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */ { INT8U res, i, ctrlval; INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL }; res = MCP_ALLTXBUSY; *txbuf_n = 0x00; /* check all 3 TX-Buffers */ for (i=0; i 0){ #if DEBUG_MODE Serial.print("Enter setting mode fall\r\n"); #endif return res; } if (num == 0){ mcp2515_write_id(MCP_RXM0SIDH, ext, ulData); } else if(num == 1){ mcp2515_write_id(MCP_RXM1SIDH, ext, ulData); } else res = MCP2515_FAIL; res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); if(res > 0){ #if DEBUG_MODE Serial.print("Enter normal mode fall\r\n"); #endif return res; } #if DEBUG_MODE Serial.print("set Mask success!!\r\n"); #endif return res; } /********************************************************************************************************* ** Function name: init_Filter ** Descriptions: init canid filters *********************************************************************************************************/ INT8U MCP_CAN::init_Filter(INT8U num, INT32U ulData, INT8U ext) { INT8U res = MCP2515_OK; #if DEBUG_MODE Serial.print("Begin 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"); #endif return res; } switch( num ) { case 0: mcp2515_write_id(MCP_RXF0SIDH, ext, ulData); break; case 1: mcp2515_write_id(MCP_RXF1SIDH, ext, ulData); break; case 2: mcp2515_write_id(MCP_RXF2SIDH, ext, ulData); break; case 3: mcp2515_write_id(MCP_RXF3SIDH, ext, ulData); break; case 4: mcp2515_write_id(MCP_RXF4SIDH, ext, ulData); break; case 5: mcp2515_write_id(MCP_RXF5SIDH, ext, ulData); break; default: res = MCP2515_FAIL; } res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); if(res > 0) { #if DEBUG_MODE Serial.print("Enter normal mode fall\r\nSet filter fail!!\r\n"); #else delay(10); #endif return res; } #if DEBUG_MODE Serial.print("set Filter success!!\r\n"); #else delay(10); #endif return res; } /********************************************************************************************************* ** Function name: startReceive ** Descriptions: start Receive *********************************************************************************************************/ INT8U MCP_CAN::startReceive(INT8U rxbid) { CHECK_RXBID(rxbid); #if (DEBUG_RXANY==1) /* enable both receive-buffers */ /* to receive any message */ /* and enable rollover */ if (rxbid == 0) { mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_ANY); }else{ 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 */ if (rxbid == 0) { mcp2515_modifyRegister(MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT); }else{ mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_STDEXT); } #endif return CAN_OK; } /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send message *********************************************************************************************************/ INT8U MCP_CAN::sendMsg(INT8U txbid, INT32U id, INT8U len, INT8U *p_buf, INT8U ext, INT8U rtr) { INT8U intf_tbl[MCP_N_TXBUFFERS] = { MCP_TX0IF, MCP_TX1IF, MCP_TX2IF }; INT8U intf_bit = intf_tbl[txbid]; CHECK_LEN(len); CHECK_TXBID(txbid); mcp2515_write_canMsg(txbid, id, len, p_buf, ext, rtr); mcp2515_modifyRegister(MCP_CANINTF, intf_bit, 0); mcp2515_start_transmit( txbid ); return CAN_OK; } /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send buf *********************************************************************************************************/ INT8U MCP_CAN::sendMsg(INT8U txbid, INT32U id, INT8U len, INT8U *p_buf, INT8U ext) { return sendMsg(txbid, id, len, p_buf, ext, 0); } /********************************************************************************************************* ** Function name: sendMsg ** Descriptions: send buf *********************************************************************************************************/ INT8U MCP_CAN::sendMsg(INT8U txbid, INT32U id, INT8U len, INT8U *p_buf) { return sendMsg(txbid, id, len, p_buf, 0, 0); } /********************************************************************************************************* ** Function name: readMsg ** Descriptions: read message *********************************************************************************************************/ INT8U MCP_CAN::readMsg(INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf, INT8U *p_ext, INT8U *p_rtr) { INT8U stat, res; CHECK_RXBID(rxbid); INT8U stat_bit = (rxbid == 0)? MCP_STAT_RX0IF : MCP_STAT_RX1IF; INT8U intf_bit = (rxbid == 0)? MCP_RX0IF : MCP_RX1IF; stat = mcp2515_readStatus(); if ( stat & stat_bit ) { mcp2515_read_canMsg(rxbid, p_id, p_len, p_buf, p_ext, p_rtr); mcp2515_modifyRegister(MCP_CANINTF, intf_bit, 0); res = CAN_OK; } else { res = CAN_NOMSG; } return res; } /********************************************************************************************************* ** Function name: readMsgBuf ** Descriptions: read message *********************************************************************************************************/ INT8U MCP_CAN::readMsg(INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf, INT8U *p_ext) { INT8U rtr; return readMsg(rxbid, p_id, p_len, p_buf, p_ext, &rtr); } /********************************************************************************************************* ** Function name: readMsgBuf ** Descriptions: read message *********************************************************************************************************/ INT8U MCP_CAN::readMsg(INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf) { INT8U ext; INT8U rtr; return readMsg(rxbid, p_id, p_len, p_buf, &ext, &rtr); } /********************************************************************************************************* ** Function name: readMsgBuf ** Descriptions: read message *********************************************************************************************************/ INT8U MCP_CAN::readMsg(INT8U rxbid, INT8U *p_len, INT8U *p_buf) { INT32U id; INT8U ext; INT8U rtr; return readMsg(rxbid, &id, p_len, p_buf, &ext, &rtr); } /********************************************************************************************************* ** Function name: checkReceive ** Descriptions: check if got something *********************************************************************************************************/ INT8U MCP_CAN::checkReceive(INT8U rxbid) { INT8U res; CHECK_RXBID(rxbid); res = mcp2515_readStatus(); /* RXnIF in Bit 1 and 0 */ if (rxbid == 0) { if ( (res & MCP_STAT_RX0IF) == MCP_STAT_RX0IF) { return CAN_MSGAVAIL; } } else if (rxbid == 1) { if ( (res & MCP_STAT_RX1IF) == MCP_STAT_RX1IF ) { return CAN_MSGAVAIL; } } return CAN_NOMSG; } /********************************************************************************************************* ** Function name: checkSend ** Descriptions: check if snt msg *********************************************************************************************************/ INT8U MCP_CAN::checkSend(INT8U txbid) { INT8U res; INT8U txbx_ctrl[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL }; CHECK_TXBID(txbid); res = mcp2515_readRegister(txbx_ctrl[txbid]); if ( (res & MCP_TXB_TXREQ_M) == MCP_TXB_TXREQ_M) { return CAN_SENDWAIT; } return CAN_NOSENDWAIT; } /********************************************************************************************************* ** Function name: checkError ** Descriptions: if something error *********************************************************************************************************/ INT8U MCP_CAN::checkError(void) { INT8U eflg = mcp2515_readRegister(MCP_EFLG); if ( eflg & MCP_EFLG_ERRORMASK ) { return CAN_CTRLERROR; } else { return CAN_OK; } } /********************************************************************************************************* END FILE *********************************************************************************************************/