mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-09-14 10:33:49 +00:00
drop mcp_can_dfs.h
This commit is contained in:
+39
-40
@@ -21,10 +21,10 @@ void MCP_CAN::endSPI() {
|
||||
SPI.endTransaction();
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: reset
|
||||
** Descriptions: reset the device
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
void MCP_CAN::reset(void)
|
||||
{
|
||||
startSPI();
|
||||
@@ -33,10 +33,10 @@ void MCP_CAN::reset(void)
|
||||
delay(10);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: readRegister
|
||||
** Descriptions: read register
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
uint8_t MCP_CAN::readRegister(const REGISTER reg)
|
||||
{
|
||||
startSPI();
|
||||
@@ -48,10 +48,10 @@ uint8_t MCP_CAN::readRegister(const REGISTER reg)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: readRegisterS
|
||||
** Descriptions: read registerS
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
void MCP_CAN::readRegisterS(const REGISTER address, uint8_t values[], const uint8_t n)
|
||||
{
|
||||
startSPI();
|
||||
@@ -64,10 +64,10 @@ void MCP_CAN::readRegisterS(const REGISTER address, uint8_t values[], const uint
|
||||
endSPI();
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: setRegister
|
||||
** Descriptions: set register
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
void MCP_CAN::setRegister(const REGISTER reg, const uint8_t value)
|
||||
{
|
||||
startSPI();
|
||||
@@ -77,10 +77,10 @@ void MCP_CAN::setRegister(const REGISTER reg, const uint8_t value)
|
||||
endSPI();
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: setRegisterS
|
||||
** Descriptions: set registerS
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
void MCP_CAN::setRegisterS(const REGISTER reg, const uint8_t values[], const uint8_t n)
|
||||
{
|
||||
startSPI();
|
||||
@@ -92,24 +92,24 @@ void MCP_CAN::setRegisterS(const REGISTER reg, const uint8_t values[], const uin
|
||||
endSPI();
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: modifyRegister
|
||||
** Descriptions: set bit of one register
|
||||
*********************************************************************************************************/
|
||||
void MCP_CAN::modifyRegister(const REGISTER address, const uint8_t mask, const uint8_t data)
|
||||
******************************************************************************/
|
||||
void MCP_CAN::modifyRegister(const REGISTER reg, const uint8_t mask, const uint8_t data)
|
||||
{
|
||||
startSPI();
|
||||
SPI.transfer(INSTRUCTION_BITMOD);
|
||||
SPI.transfer(address);
|
||||
SPI.transfer(reg);
|
||||
SPI.transfer(mask);
|
||||
SPI.transfer(data);
|
||||
endSPI();
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: getStatus
|
||||
** Descriptions: read mcp2515's Status
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
uint8_t MCP_CAN::getStatus(void)
|
||||
{
|
||||
startSPI();
|
||||
@@ -120,10 +120,10 @@ uint8_t MCP_CAN::getStatus(void)
|
||||
return i;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: setCANCTRL_Mode
|
||||
** Descriptions: set control mode
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::ERROR MCP_CAN::setCANCTRL_Mode(const MODE newmode)
|
||||
{
|
||||
modifyRegister(MCP_CANCTRL, MODE_MASK, newmode);
|
||||
@@ -133,10 +133,10 @@ MCP_CAN::ERROR MCP_CAN::setCANCTRL_Mode(const MODE newmode)
|
||||
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: configRate
|
||||
** Descriptions: set boadrate
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::ERROR MCP_CAN::configRate(const CAN_SPEED canSpeed)
|
||||
{
|
||||
uint8_t set, cfg1, cfg2, cfg3;
|
||||
@@ -255,10 +255,10 @@ MCP_CAN::ERROR MCP_CAN::configRate(const CAN_SPEED canSpeed)
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: init
|
||||
** Descriptions: init the device
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::ERROR MCP_CAN::init(const CAN_SPEED canSpeed)
|
||||
{
|
||||
reset();
|
||||
@@ -287,10 +287,9 @@ MCP_CAN::ERROR MCP_CAN::init(const CAN_SPEED canSpeed)
|
||||
setRegister(MCP_CANINTE, CANINTF_RX0IF | CANINTF_RX1IF);
|
||||
|
||||
modifyRegister(MCP_RXB0CTRL,
|
||||
MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
|
||||
MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK );
|
||||
modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
|
||||
MCP_RXB_RX_STDEXT);
|
||||
RXBnCTRL_RXM_MASK | RXB0CTRL_BUKT,
|
||||
RXBnCTRL_RXM_STDEXT | RXB0CTRL_BUKT);
|
||||
modifyRegister(MCP_RXB1CTRL, RXBnCTRL_RXM_MASK, RXBnCTRL_RXM_STDEXT);
|
||||
|
||||
res = setCANCTRL_Mode(m_mode);
|
||||
delay(10);
|
||||
@@ -299,10 +298,10 @@ MCP_CAN::ERROR MCP_CAN::init(const CAN_SPEED canSpeed)
|
||||
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: write_id
|
||||
** Descriptions: write can id
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
void MCP_CAN::write_id(const REGISTER reg, const bool ext, const uint32_t id)
|
||||
{
|
||||
uint8_t tbufdata[4];
|
||||
@@ -332,10 +331,10 @@ void MCP_CAN::prepareId(uint8_t *buffer, const bool ext, const uint32_t id)
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: set CS
|
||||
** Descriptions: init CS pin and set UNSELECTED
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::MCP_CAN(const uint8_t _CS, const MODE mode)
|
||||
{
|
||||
m_mode = mode;
|
||||
@@ -344,20 +343,20 @@ MCP_CAN::MCP_CAN(const uint8_t _CS, const MODE mode)
|
||||
endSPI();
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: init
|
||||
** Descriptions: init can and set speed
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::ERROR MCP_CAN::begin(const CAN_SPEED speedset)
|
||||
{
|
||||
SPI.begin();
|
||||
return init(speedset);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: initMask
|
||||
** Descriptions: init canid Masks
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::ERROR MCP_CAN::initMask(const uint8_t num, const bool ext, const uint32_t ulData)
|
||||
{
|
||||
delay(10);
|
||||
@@ -381,10 +380,10 @@ MCP_CAN::ERROR MCP_CAN::initMask(const uint8_t num, const bool ext, const uint32
|
||||
return res;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/******************************************************************************
|
||||
** Function name: initFilt
|
||||
** Descriptions: init canid filters
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
MCP_CAN::ERROR MCP_CAN::initFilt(const RXF num, const bool ext, const uint32_t ulData)
|
||||
{
|
||||
delay(10);
|
||||
@@ -524,10 +523,10 @@ MCP_CAN::ERROR MCP_CAN::readMessage(uint32_t *id, uint8_t *dlc, uint8_t buf[], b
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: checkReceive
|
||||
** Descriptions: check if got something
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
bool MCP_CAN::checkReceive(void)
|
||||
{
|
||||
uint8_t res = getStatus();
|
||||
@@ -538,10 +537,10 @@ bool MCP_CAN::checkReceive(void)
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
/*****************************************************************************
|
||||
** Function name: checkError
|
||||
** Descriptions: if something error
|
||||
*********************************************************************************************************/
|
||||
******************************************************************************/
|
||||
bool MCP_CAN::checkError(void)
|
||||
{
|
||||
uint8_t eflg = readRegister(MCP_EFLG);
|
||||
|
||||
@@ -3,7 +3,78 @@
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
#include "mcp_can_dfs.h"
|
||||
/*
|
||||
* speed 16M
|
||||
*/
|
||||
#define MCP_16MHz_1000kBPS_CFG1 (0x00)
|
||||
#define MCP_16MHz_1000kBPS_CFG2 (0xD0)
|
||||
#define MCP_16MHz_1000kBPS_CFG3 (0x82)
|
||||
|
||||
#define MCP_16MHz_500kBPS_CFG1 (0x00)
|
||||
#define MCP_16MHz_500kBPS_CFG2 (0xF0)
|
||||
#define MCP_16MHz_500kBPS_CFG3 (0x86)
|
||||
|
||||
#define MCP_16MHz_250kBPS_CFG1 (0x41)
|
||||
#define MCP_16MHz_250kBPS_CFG2 (0xF1)
|
||||
#define MCP_16MHz_250kBPS_CFG3 (0x85)
|
||||
|
||||
#define MCP_16MHz_200kBPS_CFG1 (0x01)
|
||||
#define MCP_16MHz_200kBPS_CFG2 (0xFA)
|
||||
#define MCP_16MHz_200kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_125kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_125kBPS_CFG2 (0xF0)
|
||||
#define MCP_16MHz_125kBPS_CFG3 (0x86)
|
||||
|
||||
#define MCP_16MHz_100kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_100kBPS_CFG2 (0xFA)
|
||||
#define MCP_16MHz_100kBPS_CFG3 (0x87)
|
||||
|
||||
/*
|
||||
#define MCP_16MHz_100kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_100kBPS_CFG2 (0xBA)
|
||||
#define MCP_16MHz_100kBPS_CFG3 (0x07)
|
||||
*/
|
||||
|
||||
#define MCP_16MHz_95kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_95kBPS_CFG2 (0xAD)
|
||||
#define MCP_16MHz_95kBPS_CFG3 (0x07)
|
||||
|
||||
#define MCP_16MHz_83k3BPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_83k3BPS_CFG2 (0xBE)
|
||||
#define MCP_16MHz_83k3BPS_CFG3 (0x07)
|
||||
|
||||
#define MCP_16MHz_80kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_80kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_80kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_50kBPS_CFG1 (0x07)
|
||||
#define MCP_16MHz_50kBPS_CFG2 (0xFA)
|
||||
#define MCP_16MHz_50kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_40kBPS_CFG1 (0x07)
|
||||
#define MCP_16MHz_40kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_40kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_33kBPS_CFG1 (0x09)
|
||||
#define MCP_16MHz_33kBPS_CFG2 (0xBE)
|
||||
#define MCP_16MHz_33kBPS_CFG3 (0x07)
|
||||
|
||||
#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 (0x87)
|
||||
|
||||
#define MCP_16MHz_10kBPS_CFG1 (0x1F)
|
||||
#define MCP_16MHz_10kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_10kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_5kBPS_CFG1 (0x3F)
|
||||
#define MCP_16MHz_5kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_5kBPS_CFG3 (0x87)
|
||||
|
||||
enum CAN_SPEED {
|
||||
CAN_5KBPS,
|
||||
@@ -68,6 +139,19 @@ class MCP_CAN
|
||||
static const uint8_t DLC_MASK = 0x0F;
|
||||
static const uint8_t RTR_MASK = 0x40;
|
||||
|
||||
static const uint8_t RXBnCTRL_RXM_STD = 0x20;
|
||||
static const uint8_t RXBnCTRL_RXM_EXT = 0x40;
|
||||
static const uint8_t RXBnCTRL_RXM_STDEXT = 0x00;
|
||||
static const uint8_t RXBnCTRL_RXM_MASK = 0x60;
|
||||
static const uint8_t RXB0CTRL_BUKT = 0x40;
|
||||
|
||||
static const uint8_t MCP_SIDH = 0;
|
||||
static const uint8_t MCP_SIDL = 1;
|
||||
static const uint8_t MCP_EID8 = 2;
|
||||
static const uint8_t MCP_EID0 = 3;
|
||||
static const uint8_t MCP_DLC = 4;
|
||||
static const uint8_t MCP_DATA = 5;
|
||||
|
||||
enum RXF {
|
||||
RXF0 = 0,
|
||||
RXF1 = 1,
|
||||
@@ -232,27 +316,23 @@ class MCP_CAN
|
||||
private:
|
||||
|
||||
void reset(void);
|
||||
|
||||
uint8_t readRegister(const REGISTER reg);
|
||||
|
||||
void readRegisterS(const REGISTER reg, uint8_t values[], const uint8_t n);
|
||||
void setRegister(const REGISTER reg, const uint8_t value);
|
||||
|
||||
void setRegisterS(const REGISTER reg, const uint8_t values[], const uint8_t n);
|
||||
|
||||
void modifyRegister(const REGISTER reg, const uint8_t mask, const uint8_t data);
|
||||
|
||||
ERROR setCANCTRL_Mode(const MODE newmode);
|
||||
ERROR configRate(const CAN_SPEED canSpeed);
|
||||
ERROR init(const CAN_SPEED canSpeed);
|
||||
|
||||
void startSPI();
|
||||
void endSPI();
|
||||
|
||||
uint8_t readRegister(const REGISTER reg);
|
||||
void readRegisterS(const REGISTER reg, uint8_t values[], const uint8_t n);
|
||||
void setRegister(const REGISTER reg, const uint8_t value);
|
||||
void setRegisterS(const REGISTER reg, const uint8_t values[], const uint8_t n);
|
||||
void modifyRegister(const REGISTER reg, const uint8_t mask, const uint8_t data);
|
||||
|
||||
void write_id(const REGISTER reg, const bool ext, const uint32_t id);
|
||||
|
||||
void prepareId(uint8_t *buffer, const bool ext, const uint32_t id);
|
||||
|
||||
void startSPI();
|
||||
void endSPI();
|
||||
|
||||
public:
|
||||
MCP_CAN(const uint8_t _CS, const MODE mode);
|
||||
ERROR begin(const CAN_SPEED speedset);
|
||||
|
||||
@@ -4,91 +4,6 @@
|
||||
/*
|
||||
* Begin mt
|
||||
*/
|
||||
#define MCP_SIDH 0
|
||||
#define MCP_SIDL 1
|
||||
#define MCP_EID8 2
|
||||
#define MCP_EID0 3
|
||||
#define MCP_DLC 4
|
||||
#define MCP_DATA 5
|
||||
|
||||
#define MCP_RXB_RX_EXT 0x40
|
||||
#define MCP_RXB_RX_STD 0x20
|
||||
#define MCP_RXB_RX_STDEXT 0x00
|
||||
#define MCP_RXB_RX_MASK 0x60
|
||||
#define MCP_RXB_BUKT_MASK (1<<2)
|
||||
|
||||
/*
|
||||
* speed 16M
|
||||
*/
|
||||
#define MCP_16MHz_1000kBPS_CFG1 (0x00)
|
||||
#define MCP_16MHz_1000kBPS_CFG2 (0xD0)
|
||||
#define MCP_16MHz_1000kBPS_CFG3 (0x82)
|
||||
|
||||
#define MCP_16MHz_500kBPS_CFG1 (0x00)
|
||||
#define MCP_16MHz_500kBPS_CFG2 (0xF0)
|
||||
#define MCP_16MHz_500kBPS_CFG3 (0x86)
|
||||
|
||||
#define MCP_16MHz_250kBPS_CFG1 (0x41)
|
||||
#define MCP_16MHz_250kBPS_CFG2 (0xF1)
|
||||
#define MCP_16MHz_250kBPS_CFG3 (0x85)
|
||||
|
||||
#define MCP_16MHz_200kBPS_CFG1 (0x01)
|
||||
#define MCP_16MHz_200kBPS_CFG2 (0xFA)
|
||||
#define MCP_16MHz_200kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_125kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_125kBPS_CFG2 (0xF0)
|
||||
#define MCP_16MHz_125kBPS_CFG3 (0x86)
|
||||
|
||||
#define MCP_16MHz_100kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_100kBPS_CFG2 (0xFA)
|
||||
#define MCP_16MHz_100kBPS_CFG3 (0x87)
|
||||
|
||||
/*
|
||||
#define MCP_16MHz_100kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_100kBPS_CFG2 (0xBA)
|
||||
#define MCP_16MHz_100kBPS_CFG3 (0x07)
|
||||
*/
|
||||
|
||||
#define MCP_16MHz_95kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_95kBPS_CFG2 (0xAD)
|
||||
#define MCP_16MHz_95kBPS_CFG3 (0x07)
|
||||
|
||||
#define MCP_16MHz_83k3BPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_83k3BPS_CFG2 (0xBE)
|
||||
#define MCP_16MHz_83k3BPS_CFG3 (0x07)
|
||||
|
||||
#define MCP_16MHz_80kBPS_CFG1 (0x03)
|
||||
#define MCP_16MHz_80kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_80kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_50kBPS_CFG1 (0x07)
|
||||
#define MCP_16MHz_50kBPS_CFG2 (0xFA)
|
||||
#define MCP_16MHz_50kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_40kBPS_CFG1 (0x07)
|
||||
#define MCP_16MHz_40kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_40kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_33kBPS_CFG1 (0x09)
|
||||
#define MCP_16MHz_33kBPS_CFG2 (0xBE)
|
||||
#define MCP_16MHz_33kBPS_CFG3 (0x07)
|
||||
|
||||
#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 (0x87)
|
||||
|
||||
#define MCP_16MHz_10kBPS_CFG1 (0x1F)
|
||||
#define MCP_16MHz_10kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_10kBPS_CFG3 (0x87)
|
||||
|
||||
#define MCP_16MHz_5kBPS_CFG1 (0x3F)
|
||||
#define MCP_16MHz_5kBPS_CFG2 (0xFF)
|
||||
#define MCP_16MHz_5kBPS_CFG3 (0x87)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user