mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-09-15 02:52:53 +00:00
+22
-18
@@ -106,7 +106,7 @@ void MCP_CAN::mcp2515_readRegisterS(const byte address, byte values[], const byt
|
||||
spi_readwrite(MCP_READ);
|
||||
spi_readwrite(address);
|
||||
// mcp2515 has auto-increment of address-pointer
|
||||
for(i=0; i<n && i<CAN_MAX_CHAR_IN_MESSAGE; i++)
|
||||
for(i=0; i<n && i<CAN_MAX_CHAR_IN_MESSAGE; i++)
|
||||
{
|
||||
values[i] = spi_read();
|
||||
}
|
||||
@@ -367,7 +367,7 @@ void MCP_CAN::mcp2515_initCANBuffers(void)
|
||||
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);
|
||||
@@ -529,12 +529,14 @@ void MCP_CAN::mcp2515_read_id(const byte mcp_addr, byte* ext, unsigned long* id)
|
||||
** Function name: mcp2515_write_canMsg
|
||||
** Descriptions: write msg
|
||||
*********************************************************************************************************/
|
||||
void MCP_CAN::mcp2515_write_canMsg(const byte buffer_sidh_addr)
|
||||
void MCP_CAN::mcp2515_write_canMsg(const byte buffer_sidh_addr, int rtrBit)
|
||||
{
|
||||
byte mcp_addr;
|
||||
mcp_addr = buffer_sidh_addr;
|
||||
mcp2515_setRegisterS(mcp_addr+5, dta, dta_len); // write data bytes
|
||||
if(rtr == 1) // if RTR set bit in byte
|
||||
// Serial.print("RTR: ");
|
||||
// Serial.println(rtrBit);
|
||||
if(rtrBit == 1) // if RTR set bit in byte
|
||||
{
|
||||
dta_len |= MCP_RTR_MASK;
|
||||
}
|
||||
@@ -584,7 +586,7 @@ byte MCP_CAN::mcp2515_getNextFreeTXBuf(byte *txbuf_n) // get Nex
|
||||
*txbuf_n = 0x00;
|
||||
|
||||
// check all 3 TX-Buffers
|
||||
for(i=0; i<MCP_N_TXBUFFERS; i++)
|
||||
for(i=0; i<MCP_N_TXBUFFERS; i++)
|
||||
{
|
||||
ctrlval = mcp2515_readRegister(ctrlregs[i]);
|
||||
if((ctrlval & MCP_TXB_TXREQ_M) == 0) {
|
||||
@@ -776,7 +778,7 @@ byte MCP_CAN::clearMsg()
|
||||
ext_flg = 0;
|
||||
rtr = 0;
|
||||
filhit = 0;
|
||||
|
||||
|
||||
for(int i = 0; i<dta_len; i++)
|
||||
{
|
||||
dta[i] = 0x00;
|
||||
@@ -789,7 +791,7 @@ byte MCP_CAN::clearMsg()
|
||||
** Function name: sendMsg
|
||||
** Descriptions: send message
|
||||
*********************************************************************************************************/
|
||||
byte MCP_CAN::sendMsg()
|
||||
byte MCP_CAN::sendMsg(int rtrBit)
|
||||
{
|
||||
byte res, res1, txbuf_n;
|
||||
uint16_t uiTimeOut = 0;
|
||||
@@ -803,23 +805,23 @@ byte MCP_CAN::sendMsg()
|
||||
{
|
||||
return CAN_GETTXBFTIMEOUT; // get tx buff time out
|
||||
}
|
||||
|
||||
|
||||
uiTimeOut = 0;
|
||||
mcp2515_write_canMsg(txbuf_n);
|
||||
mcp2515_write_canMsg(txbuf_n, rtrBit);
|
||||
mcp2515_start_transmit(txbuf_n);
|
||||
|
||||
|
||||
do {
|
||||
uiTimeOut++;
|
||||
res1= mcp2515_readRegister(txbuf_n-1 /* the ctrl reg is located at txbuf_n-1 */); // read send buff ctrl reg
|
||||
res1 = res1 & 0x08;
|
||||
}while(res1 && (uiTimeOut < TIMEOUTVALUE));
|
||||
|
||||
|
||||
if(uiTimeOut == TIMEOUTVALUE) // send msg timeout
|
||||
{
|
||||
return CAN_SENDMSGTIMEOUT;
|
||||
}
|
||||
return CAN_OK;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
@@ -829,7 +831,9 @@ byte MCP_CAN::sendMsg()
|
||||
byte MCP_CAN::sendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, byte *buf)
|
||||
{
|
||||
setMsg(id, ext, len, rtr, buf);
|
||||
return sendMsg();
|
||||
// Serial.print("RTR00: ");
|
||||
// Serial.println(rtr, HEX);
|
||||
return sendMsg(rtr);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
@@ -839,7 +843,7 @@ byte MCP_CAN::sendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, byte *b
|
||||
byte MCP_CAN::sendMsgBuf(unsigned long id, byte ext, byte len, byte *buf)
|
||||
{
|
||||
setMsg(id, ext, len, buf);
|
||||
return sendMsg();
|
||||
return sendMsg(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -884,7 +888,7 @@ byte MCP_CAN::readMsgBuf(byte *len, byte buf[])
|
||||
|
||||
if(rc == CAN_OK) {
|
||||
*len = dta_len;
|
||||
for(int i = 0; i<dta_len; i++)
|
||||
for(int i = 0; i<dta_len; i++)
|
||||
{
|
||||
buf[i] = dta[i];
|
||||
}
|
||||
@@ -898,7 +902,7 @@ byte MCP_CAN::readMsgBuf(byte *len, byte buf[])
|
||||
** Function name: readMsgBufID
|
||||
** Descriptions: read message buf and can bus source ID
|
||||
*********************************************************************************************************/
|
||||
byte MCP_CAN::readMsgBufID(unsigned long *ID, byte *len, byte buf[])
|
||||
byte MCP_CAN::readMsgBufID(byte *ID, byte *len, byte buf[])
|
||||
{
|
||||
byte rc;
|
||||
rc = readMsg();
|
||||
@@ -906,7 +910,7 @@ byte MCP_CAN::readMsgBufID(unsigned long *ID, byte *len, byte buf[])
|
||||
if(rc == CAN_OK) {
|
||||
*len = dta_len;
|
||||
*ID = can_id;
|
||||
for(int i = 0; i<dta_len && i < MAX_CHAR_IN_MESSAGE; i++)
|
||||
for(int i = 0; i<dta_len && i < MAX_CHAR_IN_MESSAGE; i++)
|
||||
{
|
||||
buf[i] = dta[i];
|
||||
}
|
||||
@@ -966,4 +970,4 @@ byte MCP_CAN::isExtendedFrame(void)
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
Author:Loovee (loovee@seeed.cc)
|
||||
2014-1-16
|
||||
|
||||
Contributor:
|
||||
|
||||
|
||||
Contributor:
|
||||
|
||||
Cory J. Fowler
|
||||
Latonita
|
||||
Woodward1
|
||||
@@ -20,7 +20,7 @@
|
||||
Adlerweb
|
||||
Btetz
|
||||
Hurvajs
|
||||
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Seeed Technology Inc.
|
||||
@@ -53,8 +53,8 @@
|
||||
class MCP_CAN
|
||||
{
|
||||
private:
|
||||
|
||||
byte ext_flg; // identifier xxxID
|
||||
|
||||
byte ext_flg; // identifier xxxID
|
||||
// either extended (the 29 LSB) or standard (the 11 LSB)
|
||||
unsigned long can_id; // can id
|
||||
byte dta_len; // data length
|
||||
@@ -64,73 +64,73 @@ class MCP_CAN
|
||||
byte SPICS;
|
||||
|
||||
/*
|
||||
* mcp2515 driver function
|
||||
* mcp2515 driver function
|
||||
*/
|
||||
|
||||
private:
|
||||
|
||||
void mcp2515_reset(void); // reset mcp2515
|
||||
void mcp2515_reset(void); // reset mcp2515
|
||||
|
||||
byte mcp2515_readRegister(const byte address); // read mcp2515's register
|
||||
|
||||
void mcp2515_readRegisterS(const byte address,
|
||||
byte values[],
|
||||
byte mcp2515_readRegister(const byte address); // read mcp2515's register
|
||||
|
||||
void mcp2515_readRegisterS(const byte address,
|
||||
byte values[],
|
||||
const byte n);
|
||||
void mcp2515_setRegister(const byte address, // set mcp2515's register
|
||||
void mcp2515_setRegister(const byte address, // set mcp2515's register
|
||||
const byte value);
|
||||
|
||||
void mcp2515_setRegisterS(const byte address, // set mcp2515's registers
|
||||
void mcp2515_setRegisterS(const byte address, // set mcp2515's registers
|
||||
const byte values[],
|
||||
const byte n);
|
||||
|
||||
|
||||
void mcp2515_initCANBuffers(void);
|
||||
|
||||
void mcp2515_modifyRegister(const byte address, // set bit of one register
|
||||
|
||||
void mcp2515_modifyRegister(const byte address, // set bit of one register
|
||||
const byte mask,
|
||||
const byte data);
|
||||
|
||||
byte mcp2515_readStatus(void); // read mcp2515's Status
|
||||
byte mcp2515_setCANCTRL_Mode(const byte newmode); // set mode
|
||||
byte mcp2515_configRate(const byte canSpeed); // set boadrate
|
||||
byte mcp2515_init(const byte canSpeed); // mcp2515init
|
||||
byte mcp2515_readStatus(void); // read mcp2515's Status
|
||||
byte mcp2515_setCANCTRL_Mode(const byte newmode); // set mode
|
||||
byte mcp2515_configRate(const byte canSpeed); // set boadrate
|
||||
byte mcp2515_init(const byte canSpeed); // mcp2515init
|
||||
|
||||
void mcp2515_write_id(const byte mcp_addr, // write can id
|
||||
const byte ext,
|
||||
const unsigned long id);
|
||||
void mcp2515_write_id( const byte mcp_addr, // write can id
|
||||
const byte ext,
|
||||
const unsigned long id );
|
||||
|
||||
void mcp2515_read_id(const byte mcp_addr, // read can id
|
||||
byte* ext,
|
||||
unsigned long* id);
|
||||
void mcp2515_read_id( const byte mcp_addr, // read can id
|
||||
byte* ext,
|
||||
unsigned long* id );
|
||||
|
||||
void mcp2515_write_canMsg(const byte buffer_sidh_addr); // write can msg
|
||||
void mcp2515_read_canMsg(const byte buffer_sidh_addr); // read can msg
|
||||
void mcp2515_start_transmit(const byte mcp_addr); // start transmit
|
||||
byte mcp2515_getNextFreeTXBuf(byte *txbuf_n); // get Next free txbuf
|
||||
void mcp2515_write_canMsg( const byte buffer_sidh_addr, int rtrBit ); // write can msg
|
||||
void mcp2515_read_canMsg( const byte buffer_sidh_addr); // read can msg
|
||||
void mcp2515_start_transmit(const byte mcp_addr); // start transmit
|
||||
byte mcp2515_getNextFreeTXBuf(byte *txbuf_n); // get Next free txbuf
|
||||
|
||||
/*
|
||||
* can operator function
|
||||
*/
|
||||
*/
|
||||
|
||||
byte setMsg(unsigned long id, byte ext, byte len, byte rtr, byte *pData); // set message
|
||||
byte setMsg(unsigned long id, byte ext, byte len, byte *pData); // set message
|
||||
byte clearMsg(); // clear all message to zero
|
||||
byte readMsg(); // read message
|
||||
byte sendMsg(); // send message
|
||||
byte setMsg(unsigned long id, byte ext, byte len, byte rtr, byte *pData); // set message
|
||||
byte setMsg(unsigned long id, byte ext, byte len, byte *pData); // set message
|
||||
byte clearMsg(); // clear all message to zero
|
||||
byte readMsg(); // read message
|
||||
byte sendMsg(int rtrBit); // send message
|
||||
|
||||
public:
|
||||
MCP_CAN(byte _CS);
|
||||
byte begin(byte speedset); // init can
|
||||
byte init_Mask(byte num, byte ext, unsigned long ulData); // init Masks
|
||||
byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters
|
||||
byte sendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, byte *buf); // send buf
|
||||
byte sendMsgBuf(unsigned long id, byte ext, byte len, byte *buf); // send buf
|
||||
byte readMsgBuf(byte *len, byte *buf); // read buf
|
||||
byte readMsgBufID(unsigned long *ID, byte *len, byte *buf); // read buf with object ID
|
||||
byte checkReceive(void); // if something received
|
||||
byte checkError(void); // if something error
|
||||
unsigned long getCanId(void); // get can id when receive
|
||||
byte isRemoteRequest(void); // get RR flag when receive
|
||||
byte isExtendedFrame(void); // did we recieve 29bit frame?
|
||||
byte begin(byte speedset); // init can
|
||||
byte init_Mask(byte num, byte ext, unsigned long ulData); // init Masks
|
||||
byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters
|
||||
byte sendMsgBuf(unsigned long id, byte ext, byte rtr, byte len, byte *buf); // send buf
|
||||
byte sendMsgBuf(unsigned long id, byte ext, byte len, byte *buf); // send buf
|
||||
byte readMsgBuf(byte *len, byte *buf); // read buf
|
||||
byte readMsgBufID(byte *ID, byte *len, byte *buf); // read buf with object ID
|
||||
byte checkReceive(void); // if something received
|
||||
byte checkError(void); // if something error
|
||||
unsigned long getCanId(void); // get can id when receive
|
||||
byte isRemoteRequest(void); // get RR flag when receive
|
||||
byte isExtendedFrame(void); // did we recieve 29bit frame?
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user