mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-09-14 18:42:56 +00:00
Add mcp2518fd rxtxstatusget() function
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
Input a PID, then you will get reponse from vehicle, the input should be end with '\n'
|
||||
***************************************************************************************************/
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
#include "mcp2518fd_can.h"
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
@@ -26,9 +26,8 @@
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
const int SPI_CS_PIN = BCM8;
|
||||
mcp2518fd* controller; // Set CS pin
|
||||
|
||||
#define PID_ENGIN_PRM 0x0C
|
||||
#define PID_VEHICLE_SPEED 0x0D
|
||||
@@ -43,19 +42,19 @@ void set_mask_filt() {
|
||||
/*
|
||||
set mask, set both the mask to 0x3ff
|
||||
*/
|
||||
CAN.init_Mask(0, 0, 0x7FC);
|
||||
CAN.init_Mask(1, 0, 0x7FC);
|
||||
controller->init_Mask(0, 0, 0x7FC);
|
||||
controller->init_Mask(1, 0, 0x7FC);
|
||||
|
||||
/*
|
||||
set filter, we can receive id from 0x04 ~ 0x09
|
||||
*/
|
||||
CAN.init_Filt(0, 0, 0x7E8);
|
||||
CAN.init_Filt(1, 0, 0x7E8);
|
||||
controller->init_Filt(0, 0, 0x7E8);
|
||||
controller->init_Filt(1, 0, 0x7E8);
|
||||
|
||||
CAN.init_Filt(2, 0, 0x7E8);
|
||||
CAN.init_Filt(3, 0, 0x7E8);
|
||||
CAN.init_Filt(4, 0, 0x7E8);
|
||||
CAN.init_Filt(5, 0, 0x7E8);
|
||||
controller->init_Filt(2, 0, 0x7E8);
|
||||
controller->init_Filt(3, 0, 0x7E8);
|
||||
controller->init_Filt(4, 0, 0x7E8);
|
||||
controller->init_Filt(5, 0, 0x7E8);
|
||||
}
|
||||
|
||||
void sendPid(unsigned char __pid) {
|
||||
@@ -67,7 +66,10 @@ void sendPid(unsigned char __pid) {
|
||||
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
while(!Serial){};
|
||||
controller = new mcp2518fd();
|
||||
controller->mcp_canbus(SPI_CS_PIN);
|
||||
while (CAN_OK != controller->begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -92,12 +94,12 @@ void taskCanRecv() {
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
|
||||
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data
|
||||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
if (CAN_MSGAVAIL == controller->checkReceive()) { // check if get data
|
||||
controller->readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
|
||||
SERIAL.println("\r\n------------------------------------------------------------------");
|
||||
SERIAL.print("Get Data From id: 0x");
|
||||
SERIAL.println(CAN.getCanId(), HEX);
|
||||
SERIAL.println(controller->getCanId(), HEX);
|
||||
for (int i = 0; i < len; i++) { // print the data
|
||||
SERIAL.print("0x");
|
||||
SERIAL.print(buf[i], HEX);
|
||||
@@ -126,4 +128,4 @@ void taskDbg() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// END FILE
|
||||
// END FILE
|
||||
|
||||
+74
-64
@@ -2499,22 +2499,22 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) {
|
||||
// }
|
||||
|
||||
|
||||
// byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) {
|
||||
byte mcp2518fd::init_Mask(byte num, byte ext, unsigned long ulData) {
|
||||
|
||||
// int8_t err;
|
||||
// mcp2518fd_OperationModeSelect(CAN_CONFIGURATION_MODE);
|
||||
int8_t err;
|
||||
mcp2518fd_OperationModeSelect(CAN_CONFIGURATION_MODE);
|
||||
|
||||
|
||||
// // Setup RX Mask
|
||||
// mObj.word = 0;
|
||||
// mObj.bF.MSID = ulData;
|
||||
// mObj.bF.MIDE = ext; // Only allow standard IDs
|
||||
// mObj.bF.MEID = 0x0;
|
||||
// err = mcp2518fd_FilterMaskConfigure(num, &mObj.bF);
|
||||
// mcp2518fd_OperationModeSelect(mcpMode);
|
||||
// Setup RX Mask
|
||||
mObj.word = 0;
|
||||
mObj.bF.MSID = ulData;
|
||||
mObj.bF.MIDE = ext; // Only allow standard IDs
|
||||
mObj.bF.MEID = 0x0;
|
||||
err = mcp2518fd_FilterMaskConfigure((CAN_FILTER)num, &mObj.bF);
|
||||
mcp2518fd_OperationModeSelect(mcpMode);
|
||||
|
||||
// return err;
|
||||
// }
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// /*********************************************************************************************************
|
||||
@@ -2611,20 +2611,30 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) {
|
||||
// return mcp2518fd_OperationModeSelect(mcpMode);
|
||||
// }
|
||||
|
||||
// /*********************************************************************************************************
|
||||
// ** Function name: checkReceive
|
||||
// ** Descriptions: check if got something
|
||||
// *********************************************************************************************************/
|
||||
// byte mcp2518fd::checkReceive(void) {
|
||||
// CAN_RX_FIFO_STATUS* status;
|
||||
// // byte res;
|
||||
// // res = mcp2518_readStatus(); // RXnIF in Bit 1 and 0
|
||||
// // return ((res & MCP_STAT_RXIF_MASK) ? CAN_MSGAVAIL : CAN_NOMSG);
|
||||
// mcp2518fd_ReceiveChannelStatusGet(APP_RX_FIFO,status);
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readMsgBuf
|
||||
** Descriptions: read message buf
|
||||
*********************************************************************************************************/
|
||||
byte MCP_CAN::readMsgBuf(byte* len, byte buf[]) {
|
||||
return readMsgBufID(readRxTxStatus(), &can_id, &ext_flg, &rtr, len, buf);
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: checkReceive
|
||||
** Descriptions: check if got something
|
||||
*********************************************************************************************************/
|
||||
byte mcp2518fd::checkReceive(void) {
|
||||
CAN_RX_FIFO_STATUS status;
|
||||
// byte res;
|
||||
// res = mcp2518_readStatus(); // RXnIF in Bit 1 and 0
|
||||
// return ((res & MCP_STAT_RXIF_MASK) ? CAN_MSGAVAIL : CAN_NOMSG);
|
||||
mcp2518fd_ReceiveChannelStatusGet(APP_RX_FIFO,status);
|
||||
|
||||
// byte res = (byte)*status;
|
||||
// return res;
|
||||
// }
|
||||
byte res = (byte)(status & CAN_RX_FIFO_NOT_EMPTY_EVENT);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2648,34 +2658,34 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) {
|
||||
// ** Descriptions: Read message buf and can bus source ID according to status.
|
||||
// ** Status has to be read with readRxTxStatus.
|
||||
// *********************************************************************************************************/
|
||||
// byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtrBit,
|
||||
// volatile byte* len, volatile byte* buf) {
|
||||
// // byte rc = CAN_NOMSG;
|
||||
byte mcp2518fd::readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtrBit,
|
||||
volatile byte* len, volatile byte* buf) {
|
||||
// byte rc = CAN_NOMSG;
|
||||
|
||||
// // if (status & MCP_RX0IF) { // Msg in Buffer 0
|
||||
// // mcp2515_read_canMsg(MCP_READ_RX0, id, ext, rtrBit, len, buf);
|
||||
// // rc = CAN_OK;
|
||||
// // } else if (status & MCP_RX1IF) { // Msg in Buffer 1
|
||||
// // mcp2515_read_canMsg(MCP_READ_RX1, id, ext, rtrBit, len, buf);
|
||||
// // rc = CAN_OK;
|
||||
// // }
|
||||
// if (status & MCP_RX0IF) { // Msg in Buffer 0
|
||||
// mcp2515_read_canMsg(MCP_READ_RX0, id, ext, rtrBit, len, buf);
|
||||
// rc = CAN_OK;
|
||||
// } else if (status & MCP_RX1IF) { // Msg in Buffer 1
|
||||
// mcp2515_read_canMsg(MCP_READ_RX1, id, ext, rtrBit, len, buf);
|
||||
// rc = CAN_OK;
|
||||
// }
|
||||
|
||||
// // if (rc == CAN_OK) {
|
||||
// // rtr = *rtrBit;
|
||||
// // // dta_len=*len; // not used on any interface function
|
||||
// // ext_flg = *ext;
|
||||
// // can_id = *id;
|
||||
// // } else {
|
||||
// // *len = 0;
|
||||
// // }
|
||||
// if (status & CAN_RX_FIFO_NOT_EMPTY_EVENT) {
|
||||
// mcp2518fd_ReceiveMessageGet(APP_RX_FIFO, &rxObj, rxd, 8);
|
||||
// }
|
||||
// if (rc == CAN_OK) {
|
||||
// rtr = *rtrBit;
|
||||
// // dta_len=*len; // not used on any interface function
|
||||
// ext_flg = *ext;
|
||||
// can_id = *id;
|
||||
// } else {
|
||||
// *len = 0;
|
||||
// }
|
||||
if (status & CAN_RX_FIFO_NOT_EMPTY_EVENT) {
|
||||
mcp2518fd_ReceiveMessageGet(APP_RX_FIFO, &rxObj, rxd, 8);
|
||||
}
|
||||
|
||||
// can_id = rxObj->bF.id;
|
||||
can_id = rxObj->bF.id;
|
||||
|
||||
// return rc;
|
||||
// }
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2743,21 +2753,21 @@ uint8_t mcp2518fd::mcp2518fd_init(byte speedset) {
|
||||
// }
|
||||
|
||||
|
||||
// /*********************************************************************************************************
|
||||
// ** Function name: readRxTxStatus
|
||||
// ** Descriptions: Read RX and TX interrupt bits. Function uses status reading, but translates.
|
||||
// ** result to MCP_CANINTF. With this you can check status e.g. on interrupt sr
|
||||
// ** with one single call to save SPI calls. Then use checkClearRxStatus and
|
||||
// ** checkClearTxStatus for testing.
|
||||
// *********************************************************************************************************/
|
||||
// byte mcp2518fd::readRxTxStatus(void) {
|
||||
// // byte ret = (mcp2515_readStatus() & (MCP_STAT_TXIF_MASK | MCP_STAT_RXIF_MASK));
|
||||
// // ret = (ret & MCP_STAT_TX0IF ? MCP_TX0IF : 0) |
|
||||
// // (ret & MCP_STAT_TX1IF ? MCP_TX1IF : 0) |
|
||||
// // (ret & MCP_STAT_TX2IF ? MCP_TX2IF : 0) |
|
||||
// // (ret & MCP_STAT_RXIF_MASK); // Rx bits happend to be same on status and MCP_CANINTF
|
||||
// // return ret;
|
||||
// }
|
||||
/*********************************************************************************************************
|
||||
** Function name: readRxTxStatus
|
||||
** Descriptions: Read RX and TX interrupt bits. Function uses status reading, but translates.
|
||||
** result to MCP_CANINTF. With this you can check status e.g. on interrupt sr
|
||||
** with one single call to save SPI calls. Then use checkClearRxStatus and
|
||||
** checkClearTxStatus for testing.
|
||||
*********************************************************************************************************/
|
||||
byte mcp2518fd::readRxTxStatus(void) {
|
||||
// byte ret = (mcp2515_readStatus() & (MCP_STAT_TXIF_MASK | MCP_STAT_RXIF_MASK));
|
||||
// ret = (ret & MCP_STAT_TX0IF ? MCP_TX0IF : 0) |
|
||||
// (ret & MCP_STAT_TX1IF ? MCP_TX1IF : 0) |
|
||||
// (ret & MCP_STAT_TX2IF ? MCP_TX2IF : 0) |
|
||||
// (ret & MCP_STAT_RXIF_MASK); // Rx bits happend to be same on status and MCP_CANINTF
|
||||
// return ret;
|
||||
}
|
||||
|
||||
|
||||
// /*********************************************************************************************************
|
||||
|
||||
+6
-5
@@ -110,23 +110,24 @@ public:
|
||||
int8_t mcp2518fd_receiveMsg();
|
||||
|
||||
// void enableTxInterrupt(bool enable = true); // enable transmit interrupt
|
||||
// byte init_Mask(byte num, byte ext, unsigned long ulData);
|
||||
byte init_Mask(byte num, byte ext, unsigned long ulData);
|
||||
// byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters
|
||||
// void setSleepWakeup(const byte enable);
|
||||
// byte sleep();
|
||||
// byte wake();
|
||||
// byte setMode(byte opMode);
|
||||
// byte getMode();
|
||||
// byte checkReceive(void);
|
||||
byte readMsgBuf(byte* len, byte* buf);
|
||||
byte checkReceive(void);
|
||||
// byte checkError(void);
|
||||
// byte readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtr, volatile byte* len,
|
||||
// volatile byte* buf);
|
||||
byte readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtr, volatile byte* len,
|
||||
volatile byte* buf);
|
||||
// byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, byte iTxBuf);
|
||||
// byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte* buf);
|
||||
// byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, bool wait_sent = true); // send buf
|
||||
// byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte* buf, bool wait_sent = true); // send buf
|
||||
// void clearBufferTransmitIfFlags(byte flags);
|
||||
// byte readRxTxStatus(void);
|
||||
byte readRxTxStatus(void);
|
||||
// byte checkClearRxStatus(byte* status);
|
||||
// byte checkClearTxStatus(byte* status, byte iTxBuf);
|
||||
bool mcpPinMode(const byte pin, const byte mode);
|
||||
|
||||
Reference in New Issue
Block a user