diff --git a/src/mcp2515_can.cpp b/src/mcp2515_can.cpp index 7c82e1b..d75d7d3 100644 --- a/src/mcp2515_can.cpp +++ b/src/mcp2515_can.cpp @@ -1162,31 +1162,6 @@ byte mcp2515_can::sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, return sendMsg(id, ext, rtrBit, len, buf, wait_sent); } -/********************************************************************************************************* -** Function name: sendMsgBuf -** Descriptions: send buf -*********************************************************************************************************/ -byte mcp2515_can::sendMsgBuf(unsigned long id, byte ext, byte len, const byte* buf, bool wait_sent) { - return sendMsg(id, ext, 0, len, buf, wait_sent); -} - - -/********************************************************************************************************* -** Function name: readMsgBuf -** Descriptions: read message buf -*********************************************************************************************************/ -byte mcp2515_can::readMsgBuf(byte* len, byte buf[]) { - return readMsgBufID(readRxTxStatus(), &can_id, &ext_flg, &rtr, len, buf); -} - -/********************************************************************************************************* -** Function name: readMsgBufID -** Descriptions: read message buf and can bus source ID -*********************************************************************************************************/ -byte mcp2515_can::readMsgBufID(unsigned long* ID, byte* len, byte buf[]) { - return readMsgBufID(readRxTxStatus(), ID, &ext_flg, &rtr, len, buf); -} - /********************************************************************************************************* ** Function name: readMsgBufID ** Descriptions: Read message buf and can bus source ID according to status. @@ -1312,30 +1287,6 @@ byte mcp2515_can::checkError(uint8_t* err_ptr) { return ((eflg & MCP_EFLG_ERRORMASK) ? CAN_CTRLERROR : CAN_OK); } -/********************************************************************************************************* -** Function name: getCanId -** Descriptions: when receive something, you can get the can id!! -*********************************************************************************************************/ -unsigned long mcp2515_can::getCanId(void) { - return can_id; -} - -/********************************************************************************************************* -** Function name: isRemoteRequest -** Descriptions: when receive something, you can check if it was a request -*********************************************************************************************************/ -byte mcp2515_can::isRemoteRequest(void) { - return rtr; -} - -/********************************************************************************************************* -** Function name: isExtendedFrame -** Descriptions: did we just receive standard 11bit frame or extended 29bit? 0 = std, 1 = ext -*********************************************************************************************************/ -byte mcp2515_can::isExtendedFrame(void) { - return ext_flg; -} - /********************************************************************************************************* ** Function name: mcpPinMode ** Descriptions: switch supported pins between HiZ, interrupt, output or input diff --git a/src/mcp2515_can.h b/src/mcp2515_can.h index 81c1516..fa07dbd 100644 --- a/src/mcp2515_can.h +++ b/src/mcp2515_can.h @@ -77,18 +77,26 @@ public: virtual byte wake(); // Wake MCP2515 manually from sleep virtual byte setMode(byte opMode); // Set operational mode virtual byte getMode(); // Get operational mode - virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent = true); // send buf - virtual byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf, bool wait_sent = true); // send buf - virtual byte readMsgBuf(byte *len, byte *buf); // read buf - virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf); // read buf with object ID - virtual byte checkReceive(void); // if something received virtual byte checkError(uint8_t* err_ptr = NULL); // if something error - virtual unsigned long getCanId(void); // get can id when receive - virtual byte isRemoteRequest(void); // get RR flag when receive - virtual byte isExtendedFrame(void); // did we recieve 29bit frame? + + virtual byte checkReceive(void); // if something received virtual byte readMsgBufID(byte status, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf); // read buf with object ID + /* wrapper */ + byte readMsgBufID(unsigned long *ID, byte *len, byte *buf) { + return readMsgBufID(readRxTxStatus(), ID, &ext_flg, &rtr, len, buf); + } + byte readMsgBuf(byte *len, byte *buf) { + return readMsgBufID(readRxTxStatus(), &can_id, &ext_flg, &rtr, len, buf); + } + virtual byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, byte iTxBuf = 0xff); // as sendMsgBuf, but does not have any wait for free buffer virtual byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte *buf); // send message buf by using parsed buffer status + virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent = true); // send buf + /* wrapper */ + inline byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf) { + return sendMsgBuf(id, ext, 0, len, buf, true); + } + virtual void clearBufferTransmitIfFlags(byte flags = 0); // Clear transmit flags according to status virtual byte readRxTxStatus(void); // read has something send or received virtual byte checkClearRxStatus(byte *status); // read and clear and return first found rx status bit @@ -146,7 +154,6 @@ private: byte sendMsg(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent = true); // send message private: - byte rtr; // rtr byte nReservedTx; // Count of tx buffers for reserved send }; diff --git a/src/mcp2518fd_can.cpp b/src/mcp2518fd_can.cpp index 6e94183..a856f93 100644 --- a/src/mcp2518fd_can.cpp +++ b/src/mcp2518fd_can.cpp @@ -2452,26 +2452,6 @@ byte mcp2518fd::setMode(const byte opMode) { return mcp2518fd_OperationModeSelect(mcpMode); } -/********************************************************************************************************* -** Function name: getCanId -** Descriptions: when receive something, you can get the can id!! -*********************************************************************************************************/ -unsigned long mcp2518fd::getCanId(void) { return can_id; } - -/********************************************************************************************************* -** Function name: isRemoteRequest -** Descriptions: when receive something, you can check if it was a -*request -*********************************************************************************************************/ -byte mcp2518fd::isRemoteRequest(void) { return rtr; } - -/********************************************************************************************************* -** Function name: isExtendedFrame -** Descriptions: did we just receive standard 11bit frame or extended -*29bit? 0 = std, 1 = ext -*********************************************************************************************************/ -byte mcp2518fd::isExtendedFrame(void) { return ext_flg; } - /********************************************************************************************************* ** Function name: readMsgBufID ** Descriptions: Read message buf and can bus source ID according to @@ -2492,22 +2472,6 @@ byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long *id, return r; } -/********************************************************************************************************* -** Function name: readMsgBuf -** Descriptions: read message buf -*********************************************************************************************************/ -byte mcp2518fd::readMsgBuf(byte *len, byte buf[]) { - return readMsgBufID(readRxTxStatus(), NULL, &ext_flg, &rtr, len, buf); -} - -/********************************************************************************************************* -** Function name: readMsgBufID -** Descriptions: read message buf and can bus source ID -*********************************************************************************************************/ -byte mcp2518fd::readMsgBufID(unsigned long *ID, byte *len, byte buf[]) { - return readMsgBufID(readRxTxStatus(), ID, &ext_flg, &rtr, len, buf); -} - /********************************************************************************************************* ** Function name: checkReceive ** Descriptions: check if got something @@ -2603,15 +2567,6 @@ byte mcp2518fd::sendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, return mcp2518fd_sendMsg(buf, len, id, ext, rtr, wait_sent); } -/********************************************************************************************************* -** Function name: sendMsgBuf -** Descriptions: send buf -*********************************************************************************************************/ -byte mcp2518fd::sendMsgBuf(unsigned long id, byte ext, byte len, - const byte *buf, bool wait_sent) { - return mcp2518fd_sendMsg(buf, len, id, ext, 0, wait_sent); -} - /********************************************************************************************************* ** Function name: readRxTxStatus ** Descriptions: Read RX and TX interrupt bits. Function uses status diff --git a/src/mcp2518fd_can.h b/src/mcp2518fd_can.h index 453506a..871e0d0 100644 --- a/src/mcp2518fd_can.h +++ b/src/mcp2518fd_can.h @@ -108,7 +108,7 @@ public: return 3 - 1; // read index of last tx buffer } virtual byte begin(byte speedset, - const byte clockset = CAN_SYSCLK_40M); // init can + const byte clockset = MCP2518FD_40MHz); // init can virtual byte init_Mask(byte num, byte ext, unsigned long ulData); virtual byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters @@ -121,15 +121,17 @@ public: /* ---- receiving ---- */ virtual byte checkReceive(void); - virtual unsigned long getCanId(void); - virtual byte isRemoteRequest(void); - virtual byte isExtendedFrame(void); virtual byte readMsgBufID(byte status, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf); // read buf with object ID - virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf); - virtual byte readMsgBuf(byte *len, byte *buf); + /* wrapper */ + byte readMsgBufID(unsigned long *ID, byte *len, byte *buf) { + return readMsgBufID(readRxTxStatus(), ID, &ext_flg, &rtr, len, buf); + } + byte readMsgBuf(byte *len, byte *buf) { + return readMsgBufID(readRxTxStatus(), &can_id, &ext_flg, &rtr, len, buf); + } /* ---- sending ---- */ /* dlc = CAN_DLC_0..CAN_DLC_64(0..15) @@ -148,8 +150,10 @@ public: byte dlc, volatile const byte *buf); virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtr, byte dlc, const byte *buf, bool wait_sent = true); - virtual byte sendMsgBuf(unsigned long id, byte ext, byte dlc, const byte *buf, - bool wait_sent = true); + /* wrapper */ + inline byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf) { + return sendMsgBuf(id, ext, 0, len, buf, true); + } virtual void clearBufferTransmitIfFlags(byte flags = 0); virtual byte readRxTxStatus(void); @@ -256,7 +260,6 @@ private: int8_t mcp2518fd_BitTimeConfigureData10MHz(MCP2518FD_BITTIME_SETUP bitTime, CAN_SSP_MODE sspMode); - byte rtr; // rtr byte nReservedTx; // Count of tx buffers for reserved send CAN_OPERATION_MODE mcpMode = CAN_CLASSIC_MODE; // Current controller mode }; diff --git a/src/mcp_can.cpp b/src/mcp_can.cpp index b913055..ef5e176 100644 --- a/src/mcp_can.cpp +++ b/src/mcp_can.cpp @@ -29,11 +29,3 @@ void MCP_CAN::setSPI(SPIClass *_pSPI) { pSPI = _pSPI; // define SPI port to use before begin() } -inline byte MCP_CAN::trySendExtMsgBuf(unsigned long id, byte len, const byte *buf, byte iTxBuf) -{ // as trySendMsgBuf, but set ext=1 and rtr=0 - return trySendMsgBuf(id, 1, 0, len, buf, iTxBuf); -} -inline byte MCP_CAN::sendExtMsgBuf(byte status, unsigned long id, byte len, volatile const byte *buf) -{ // as sendMsgBuf, but set ext=1 and rtr=0 - return sendMsgBuf(status, id, 1, 0, len, buf); -} diff --git a/src/mcp_can.h b/src/mcp_can.h index 82f0b27..845d15e 100644 --- a/src/mcp_can.h +++ b/src/mcp_can.h @@ -5,14 +5,10 @@ #include #include - - #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) @@ -57,54 +53,67 @@ typedef enum { class MCP_CAN { public: - virtual void enableTxInterrupt(bool enable = true) = 0; // enable transmit interrupt + virtual void enableTxInterrupt(bool enable = true) = 0; // enable transmit interrupt virtual void reserveTxBuffers(byte nTxBuf = 0) = 0; virtual byte getLastTxBuffer() = 0; /* * speedset be in MCP_BITTIME_SETUP * clockset be in MCP_CLOCK_T */ - virtual byte begin(byte speedset, const byte clockset) = 0; // init can - virtual byte init_Mask(byte num, byte ext, unsigned long ulData) = 0; // init Masks - virtual byte init_Filt(byte num, byte ext, unsigned long ulData) = 0; // init filters - virtual void setSleepWakeup(byte enable) = 0; // Enable or disable the wake up interrupt (If disabled the MCP2515 will not be woken up by CAN bus activity, making it send only) - virtual byte sleep() = 0; // Put the MCP2515 in sleep mode - virtual byte wake() = 0; // Wake MCP2515 manually from sleep - virtual byte setMode(byte opMode) = 0; // Set operational mode - virtual byte getMode() = 0; // Get operational mode - virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent = true) = 0; // send buf - virtual byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf, bool wait_sent = true) = 0; // send buf - virtual byte readMsgBuf(byte *len, byte *buf) = 0; // read buf - virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf) = 0; // read buf with object ID - virtual byte checkReceive(void) = 0; // if something received - virtual byte checkError(uint8_t* err_ptr = NULL) = 0; // if something error - virtual unsigned long getCanId(void) = 0; // get can id when receive - virtual byte isRemoteRequest(void) = 0; // get RR flag when receive - virtual byte isExtendedFrame(void) = 0; // did we recieve 29bit frame? - virtual byte readMsgBufID(byte status, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf) = 0; // read buf with object ID - virtual byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, byte iTxBuf = 0xff) = 0; // as sendMsgBuf, but does not have any wait for free buffer - virtual byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte *buf) = 0; // send message buf by using parsed buffer status - virtual void clearBufferTransmitIfFlags(byte flags = 0) = 0; // Clear transmit flags according to status - virtual byte readRxTxStatus(void) = 0; // read has something send or received - virtual byte checkClearRxStatus(byte *status) = 0; // read and clear and return first found rx status bit - virtual byte checkClearTxStatus(byte *status, byte iTxBuf = 0xff) = 0; // read and clear and return first found or buffer specified tx status bit - virtual bool mcpPinMode(const byte pin, const byte mode) = 0; // switch supported pins between HiZ, interrupt, output or input - virtual bool mcpDigitalWrite(const byte pin, const byte mode) = 0; // write HIGH or LOW to RX0BF/RX1BF - virtual byte mcpDigitalRead(const byte pin) = 0; // read HIGH or LOW from supported pins + virtual byte begin(byte speedset, const byte clockset) = 0; // init can + virtual byte init_Mask(byte num, byte ext, unsigned long ulData) = 0; // init Masks + virtual byte init_Filt(byte num, byte ext, unsigned long ulData) = 0; // init filters + virtual void setSleepWakeup(byte enable) = 0; // Enable or disable the wake up interrupt + // (If disabled the MCP2515 will not be woken up by CAN bus activity, making it send only) + virtual byte sleep() = 0; // Put the MCP2515 in sleep mode + virtual byte wake() = 0; // Wake MCP2515 manually from sleep + virtual byte setMode(byte opMode) = 0; // Set operational mode + virtual byte getMode() = 0; // Get operational mode + virtual byte checkError(uint8_t* err_ptr = NULL) = 0; // if something error + + /* ---- receiving ---- */ + virtual byte checkReceive(void) = 0; // if something received + virtual byte readMsgBufID(byte status, + volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, + volatile byte *len, volatile byte *buf) = 0; // read buf with object ID + /* wrapper */ + virtual byte readMsgBufID(unsigned long *ID, byte *len, byte *buf) = 0; + virtual byte readMsgBuf(byte *len, byte *buf) = 0; + + /* could be called after a successful readMsgBufID() */ + unsigned long getCanId(void) { return can_id; } + byte isRemoteRequest(void) { return rtr; } + byte isExtendedFrame(void) { return ext_flg;} + + /* ---- sending ---- */ + virtual byte trySendMsgBuf(unsigned long id, byte ext, byte rtr, + byte len, const byte *buf, byte iTxBuf = 0xff) = 0; // as sendMsgBuf, but does not have any wait for free buffer + virtual byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtr, + byte len, volatile const byte *buf) = 0; // send message buf by using parsed buffer status + virtual byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, + byte len, const byte *buf, bool wait_sent = true) = 0; // send message with wait + + virtual void clearBufferTransmitIfFlags(byte flags = 0) = 0; // Clear transmit flags according to status + virtual byte readRxTxStatus(void) = 0; // read has something send or received + virtual byte checkClearRxStatus(byte *status) = 0; // read and clear and return first found rx status bit + virtual byte checkClearTxStatus(byte *status, byte iTxBuf = 0xff) = 0; // read and clear and return first found or buffer specified tx status bit + virtual bool mcpPinMode(const byte pin, const byte mode) = 0; // switch supported pins between HiZ, interrupt, output or input + virtual bool mcpDigitalWrite(const byte pin, const byte mode) = 0; // write HIGH or LOW to RX0BF/RX1BF + virtual byte mcpDigitalRead(const byte pin) = 0; // read HIGH or LOW from supported pins + public: MCP_CAN(byte _CS); void init_CS(byte _CS); // define CS after construction before begin() void setSPI(SPIClass *_pSPI); - inline byte trySendExtMsgBuf(unsigned long id, byte len, const byte *buf, byte iTxBuf = 0xff); - inline byte sendExtMsgBuf(byte status, unsigned long id, byte len, volatile const byte *buf); protected: byte ext_flg; // identifier xxxID // either extended (the 29 LSB) or standard (the 11 LSB) unsigned long can_id; // can id + byte rtr; // is remote frame byte SPICS; SPIClass *pSPI; byte mcpMode; // Current controller mode }; -#endif \ No newline at end of file +#endif