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

Optionally disable MCP2517FD workarounds #34

Open
wants to merge 1 commit 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
148 changes: 46 additions & 102 deletions src/ACAN2517FD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ static uint16_t u16FromBufferAtIndex (uint8_t ioBuffer [], const uint8_t inIndex

//----------------------------------------------------------------------------------------------------------------------

static inline void turnOffInterrupts () {
#ifndef DISABLEMCP2517FDCOMPAT
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
#endif
}

//----------------------------------------------------------------------------------------------------------------------

static inline void turnOnInterrupts() {
#ifndef DISABLEMCP2517FDCOMPAT
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
#endif
}

//----------------------------------------------------------------------------------------------------------------------

ACAN2517FD::ACAN2517FD (const uint8_t inCS, // CS input of MCP2517FD
SPIClass & inSPI, // Hardware SPI object
const uint8_t inINT) : // INT output of MCP2517FD
Expand Down Expand Up @@ -526,11 +550,7 @@ uint32_t ACAN2517FD::begin (const ACAN2517FDSettings & inSettings,

bool ACAN2517FD::end (void) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
//--- Detach interrupt pin
if (mINT != 255) { // 255 means interrupt is not used
const int8_t itPin = digitalPinToInterrupt (mINT) ;
Expand Down Expand Up @@ -565,11 +585,7 @@ bool ACAN2517FD::end (void) {
mDriverReceiveBuffer.initWithSize (0) ;
mDriverTransmitBuffer.initWithSize (0) ;
//---
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
//---
return ok ;
Expand All @@ -583,11 +599,7 @@ bool ACAN2517FD::tryToSend (const CANFDMessage & inMessage) {
bool ok = inMessage.isValid () ;
if (ok) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
if (inMessage.idx == 0) {
ok = inMessage.len <= mTransmitFIFOPayload ;
if (ok) {
Expand All @@ -599,11 +611,7 @@ bool ACAN2517FD::tryToSend (const CANFDMessage & inMessage) {
ok = sendViaTXQ (inMessage) ;
}
}
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts();
mSPI.endTransaction () ;
}
return ok ;
Expand Down Expand Up @@ -774,17 +782,9 @@ bool ACAN2517FD::sendViaTXQ (const CANFDMessage & inMessage) {

bool ACAN2517FD::available (void) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
const bool hasReceivedMessage = mDriverReceiveBuffer.count () > 0 ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts();
mSPI.endTransaction () ;
return hasReceivedMessage ;
}
Expand All @@ -793,11 +793,7 @@ bool ACAN2517FD::available (void) {

bool ACAN2517FD::receive (CANFDMessage & outMessage) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
const bool hasReceivedMessage = mDriverReceiveBuffer.remove (outMessage) ;
//--- If receive interrupt is disabled, enable it (added in release 2.17)
if (mINT == 255) { // No interrupt pin
Expand All @@ -809,11 +805,7 @@ bool ACAN2517FD::receive (CANFDMessage & outMessage) {
data8 |= (1 << 1) ; // Receive FIFO Interrupt Enable
writeRegister8Assume_SPI_transaction (INT_REGISTER + 2, data8) ;
}
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts();
mSPI.endTransaction () ;
return hasReceivedMessage ;
}
Expand Down Expand Up @@ -854,9 +846,9 @@ bool ACAN2517FD::dispatchReceivedMessage (const tFilterMatchCallBack inFilterMat

#ifndef ARDUINO_ARCH_ESP32
void ACAN2517FD::poll (void) {
noInterrupts () ;
turnOffInterrupts () ;
isr_poll_core () ;
interrupts () ;
turnOnInterrupts () ;
}
#endif

Expand Down Expand Up @@ -1102,35 +1094,19 @@ uint8_t ACAN2517FD::readRegister8Assume_SPI_transaction (const uint16_t inRegist

void ACAN2517FD::writeRegister8 (const uint16_t inRegisterAddress, const uint8_t inValue) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
writeRegister8Assume_SPI_transaction (inRegisterAddress, inValue) ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
}

//----------------------------------------------------------------------------------------------------------------------

uint8_t ACAN2517FD::readRegister8 (const uint16_t inRegisterAddress) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
const uint8_t result = readRegister8Assume_SPI_transaction (inRegisterAddress) ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
return result ;
}
Expand All @@ -1139,17 +1115,9 @@ uint8_t ACAN2517FD::readRegister8 (const uint16_t inRegisterAddress) {

uint16_t ACAN2517FD::readRegister16 (const uint16_t inRegisterAddress) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
const uint16_t result = readRegister16Assume_SPI_transaction (inRegisterAddress) ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
return result ;
}
Expand All @@ -1158,35 +1126,19 @@ uint16_t ACAN2517FD::readRegister16 (const uint16_t inRegisterAddress) {

void ACAN2517FD::writeRegister32 (const uint16_t inRegisterAddress, const uint32_t inValue) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
writeRegister32Assume_SPI_transaction (inRegisterAddress, inValue) ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
}

//----------------------------------------------------------------------------------------------------------------------

uint32_t ACAN2517FD::readRegister32 (const uint16_t inRegisterAddress) {
mSPI.beginTransaction (mSPISettings) ;
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
const uint32_t result = readRegister32Assume_SPI_transaction (inRegisterAddress) ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
return result ;
}
Expand Down Expand Up @@ -1228,19 +1180,11 @@ bool ACAN2517FD::recoverFromRestrictedOperationMode (void) {

void ACAN2517FD::reset2517FD (void) {
mSPI.beginTransaction (mSPISettings) ; // Check RESET is performed with 1 MHz clock
#ifdef ARDUINO_ARCH_ESP32
taskDISABLE_INTERRUPTS () ;
#else
noInterrupts () ;
#endif
turnOffInterrupts () ;
assertCS () ;
mSPI.transfer16 (0x00) ; // Reset instruction: 0x0000
deassertCS () ;
#ifdef ARDUINO_ARCH_ESP32
taskENABLE_INTERRUPTS () ;
#else
interrupts () ;
#endif
turnOnInterrupts () ;
mSPI.endTransaction () ;
}

Expand Down
10 changes: 10 additions & 0 deletions src/ACAN2517FD.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
#include <ACAN2517FDFilters.h>
#include <SPI.h>

//----------------------------------------------------------------------------------------------------------------------
// Settings
//----------------------------------------------------------------------------------------------------------------------
//
// Enable this if you want to disable the MCP2517FD compatability mode. This can slightly increase performance when
// running on the MCP2518FD but you risk hitting issues mentioned in the MCP2517FD errata-sheet when using this option
// on the MCP2517FD.
//
// #define DISABLEMCP2517FDCOMPAT

//----------------------------------------------------------------------------------------------------------------------
// ACAN2517FD class
//----------------------------------------------------------------------------------------------------------------------
Expand Down