Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI fix, new begin command and allow oneshotmode #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Example of initialization

```C++
MCP2515 mcp2515(10);
mcp2515.reset();
mcp2515.begin();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setLoopbackMode();
```
Expand Down
3 changes: 1 addition & 2 deletions examples/CAN_SpeedTest/CAN_SpeedTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ unsigned long oldTime = 0;

void setup() {
Serial.begin(115200);
SPI.begin();

mcp2515.reset();
mcp2515.begin();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();

Expand Down
3 changes: 1 addition & 2 deletions examples/CAN_read/CAN_read.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ MCP2515 mcp2515(10);

void setup() {
Serial.begin(115200);
SPI.begin();

mcp2515.reset();
mcp2515.begin();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();

Expand Down
3 changes: 1 addition & 2 deletions examples/CAN_write/CAN_write.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ void setup() {

while (!Serial);
Serial.begin(115200);
SPI.begin();

mcp2515.reset();
mcp2515.begin();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();

Expand Down
15 changes: 12 additions & 3 deletions mcp2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ const struct MCP2515::RXBn_REGS MCP2515::RXB[N_RXBUFFERS] = {

MCP2515::MCP2515(const uint8_t _CS)
{
SPI.begin();

SPICS = _CS;
pinMode(SPICS, OUTPUT);
endSPI();
digitalWrite(SPICS, HIGH);
}

void MCP2515::startSPI() {
Expand All @@ -30,6 +28,12 @@ void MCP2515::endSPI() {
SPI.endTransaction();
}

MCP2515::ERROR MCP2515::begin(void) {
SPI.begin();
reset();
return ERROR_OK;
}

MCP2515::ERROR MCP2515::reset(void)
{
startSPI();
Expand Down Expand Up @@ -162,6 +166,11 @@ MCP2515::ERROR MCP2515::setNormalMode()
return setMode(CANCTRL_REQOP_NORMAL);
}

MCP2515::ERROR MCP2515::setOneShotMode()
{
return setMode(CANCTRL_REQOP_ONESHOT);
}

MCP2515::ERROR MCP2515::setMode(const CANCTRL_REQOP_MODE mode)
{
modifyRegister(MCP_CANCTRL, CANCTRL_REQOP, mode);
Expand Down
4 changes: 3 additions & 1 deletion mcp2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class MCP2515
CANCTRL_REQOP_LOOPBACK = 0x40,
CANCTRL_REQOP_LISTENONLY = 0x60,
CANCTRL_REQOP_CONFIG = 0x80,
CANCTRL_REQOP_POWERUP = 0xE0
CANCTRL_REQOP_ONESHOT = 0x08
};

static const uint8_t CANSTAT_OPMOD = 0xE0;
Expand Down Expand Up @@ -458,11 +458,13 @@ class MCP2515
public:
MCP2515(const uint8_t _CS);
ERROR reset(void);
ERROR begin(void);
ERROR setConfigMode();
ERROR setListenOnlyMode();
ERROR setSleepMode();
ERROR setLoopbackMode();
ERROR setNormalMode();
ERROR setOneShotMode();
ERROR setClkOut(const CAN_CLKOUT divisor);
ERROR setBitrate(const CAN_SPEED canSpeed);
ERROR setBitrate(const CAN_SPEED canSpeed, const CAN_CLOCK canClock);
Expand Down