Skip to content

Commit

Permalink
Arduino Uno compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pierremolinaro committed Feb 8, 2019
1 parent 8126182 commit 59fb4ac
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 139 deletions.
103 changes: 103 additions & 0 deletions examples/LoopBackDemoArduinoUno/LoopBackDemoArduinoUno.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//——————————————————————————————————————————————————————————————————————————————
// ACAN2517FD Demo in loopback mode, for Arduino Uno
//——————————————————————————————————————————————————————————————————————————————

#include <ACAN2517FD.h>
#include <SPI.h>

//——————————————————————————————————————————————————————————————————————————————
// Very very important: put a 10kΩ resistor between CS and VDD of MCP2517FD

static const byte MCP2517_CS = 10 ; // CS input of MCP2517
static const byte MCP2517_INT = 3 ; // INT output of MCP2517

//——————————————————————————————————————————————————————————————————————————————
// ACAN2517FD Driver object
//——————————————————————————————————————————————————————————————————————————————

ACAN2517FD can (MCP2517_CS, SPI, MCP2517_INT) ;

//——————————————————————————————————————————————————————————————————————————————
// SETUP
//——————————————————————————————————————————————————————————————————————————————

void setup () {
//--- Start serial
Serial.begin (115200) ;
//--- Wait for serial (blink led at 10 Hz during waiting)
while (!Serial) {
delay (50) ;
}
//----------------------------------- Begin SPI
SPI.begin () ;
//--- Configure ACAN2517FD
Serial.print ("sizeof (ACAN2517FDSettings): ") ;
Serial.print (sizeof (ACAN2517FDSettings)) ;
Serial.println (" bytes") ;
Serial.println ("Configure ACAN2517FD") ;
ACAN2517FDSettings settings (ACAN2517FDSettings::OSC_40MHz, 125UL * 1000UL, ACAN2517FDSettings::DATA_BITRATE_x1) ;
settings.mRequestedMode = ACAN2517FDSettings::InternalLoopBack ; // Select loopback mode
//--- Default values are too high for an Arduino Uno that contains 2048 bytes of RAM: reduce them
settings.mDriverTransmitFIFOSize = 1 ;
settings.mDriverReceiveFIFOSize = 1 ;
//--- RAM Usage
Serial.print ("MCP2517FD RAM Usage: ") ;
Serial.print (settings.ramUsage ()) ;
Serial.println (" bytes") ;
//--- Begin
const uint32_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
if (errorCode == 0) {
Serial.print ("Bit Rate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Serial.print ("Arbitration Phase segment 1: ") ;
Serial.println (settings.mArbitrationPhaseSegment1) ;
Serial.print ("Arbitration Phase segment 2: ") ;
Serial.println (settings.mArbitrationPhaseSegment2) ;
Serial.print ("Arbitration SJW:") ;
Serial.println (settings.mArbitrationSJW) ;
Serial.print ("Actual Arbitration Bit Rate: ") ;
Serial.print (settings.actualArbitrationBitRate ()) ;
Serial.println (" bit/s") ;
Serial.print ("Exact Arbitration Bit Rate ? ") ;
Serial.println (settings.exactArbitrationBitRate () ? "yes" : "no") ;
Serial.print ("Arbitration Sample point: ") ;
Serial.print (settings.arbitrationSamplePointFromBitStart ()) ;
Serial.println ("%") ;
}else{
Serial.print ("Configuration error 0x") ;
Serial.println (errorCode, HEX) ;
}
}

//——————————————————————————————————————————————————————————————————————————————
// LOOP
//——————————————————————————————————————————————————————————————————————————————

static uint32_t gSendDate = 0 ;
static uint32_t gReceivedFrameCount = 0 ;
static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

void loop () {
CANFDMessage frame ;
if (gSendDate < millis ()) {
gSendDate += 2000 ;
const bool ok = can.tryToSend (frame) ;
if (ok) {
gSentFrameCount += 1 ;
Serial.print ("Sent: ") ;
Serial.println (gSentFrameCount) ;
}else{
Serial.println ("Send failure") ;
}
}
if (can.available ()) {
can.receive (frame) ;
gReceivedFrameCount ++ ;
Serial.print ("Received: ") ;
Serial.println (gReceivedFrameCount) ;
}
}

//——————————————————————————————————————————————————————————————————————————————
18 changes: 9 additions & 9 deletions examples/LoopBackDemoESP32/LoopBackDemoESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//——————————————————————————————————————————————————————————————————————————————

#ifndef ARDUINO_ARCH_ESP32
#error "Select an ESP32 board"
#error "Select an ESP32 board"
#endif

//——————————————————————————————————————————————————————————————————————————————
Expand All @@ -23,16 +23,16 @@
// Notes:
// - GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-ups or
// pull-down resistors. They can’t be used as outputs.
// - some pins do not support INPUT_PULLUP (see https://www.esp32.com/viewtopic.php?t=439)
// - some pins do not support INPUT_PULLUP (see https://www.esp32.com/viewtopic.php?t=439)
// - All GPIOs can be configured as interrupts
// See https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 26 ; // SCK input of MCP2517
static const byte MCP2517_MOSI = 19 ; // SDI input of MCP2517
static const byte MCP2517_MISO = 18 ; // SDO output of MCP2517
static const byte MCP2517_SCK = 26 ; // SCK input of MCP2517
static const byte MCP2517_MOSI = 19 ; // SDI input of MCP2517
static const byte MCP2517_MISO = 18 ; // SDO output of MCP2517

static const byte MCP2517_CS = 16 ; // CS input of MCP2517
static const byte MCP2517_CS = 16 ; // CS input of MCP2517
static const byte MCP2517_INT = 32 ; // INT output of MCP2517

//——————————————————————————————————————————————————————————————————————————————
Expand Down Expand Up @@ -98,9 +98,9 @@ void setup () {
// LOOP
//——————————————————————————————————————————————————————————————————————————————

static unsigned gBlinkLedDate = 0 ;
static unsigned gReceivedFrameCount = 0 ;
static unsigned gSentFrameCount = 0 ;
static uint32_t gBlinkLedDate = 0 ;
static uint32_t gReceivedFrameCount = 0 ;
static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

Expand Down
20 changes: 10 additions & 10 deletions examples/LoopBackDemoESP32NoInt/LoopBackDemoESP32NoInt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//——————————————————————————————————————————————————————————————————————————————

#ifndef ARDUINO_ARCH_ESP32
#error "Select an ESP32 board"
#error "Select an ESP32 board"
#endif

//——————————————————————————————————————————————————————————————————————————————
Expand All @@ -22,15 +22,15 @@
// Notes:
// - GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-ups or
// pull-down resistors. They can’t be used as outputs.
// - some pins do not support INPUT_PULLUP (see https://www.esp32.com/viewtopic.php?t=439)
// - some pins do not support INPUT_PULLUP (see https://www.esp32.com/viewtopic.php?t=439)
// See https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 26 ; // SCK input of MCP2517
static const byte MCP2517_MOSI = 19 ; // SDI input of MCP2517
static const byte MCP2517_MISO = 18 ; // SDO output of MCP2517
static const byte MCP2517_SCK = 26 ; // SCK input of MCP2517
static const byte MCP2517_MOSI = 19 ; // SDI input of MCP2517
static const byte MCP2517_MISO = 18 ; // SDO output of MCP2517

static const byte MCP2517_CS = 16 ; // CS input of MCP2517
static const byte MCP2517_CS = 16 ; // CS input of MCP2517

//——————————————————————————————————————————————————————————————————————————————
// ACAN2517FD Driver object
Expand Down Expand Up @@ -67,7 +67,7 @@ void setup () {
Serial.print (settings.ramUsage ()) ;
Serial.println (" bytes") ;
//--- Begin
const uint32_t errorCode = can.begin (settings, NULL) ; // Second argument is NULL -> no interrupt service routine
const uint32_t errorCode = can.begin (settings, NULL) ; // Second argument is NULL -> no interrupt service routine
if (errorCode == 0) {
Serial.print ("Bit Rate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Expand Down Expand Up @@ -95,9 +95,9 @@ void setup () {
// LOOP
//——————————————————————————————————————————————————————————————————————————————

static unsigned gBlinkLedDate = 0 ;
static unsigned gReceivedFrameCount = 0 ;
static unsigned gSentFrameCount = 0 ;
static uint32_t gBlinkLedDate = 0 ;
static uint32_t gReceivedFrameCount = 0 ;
static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

Expand Down
14 changes: 7 additions & 7 deletions examples/LoopBackDemoTeensy3x/LoopBackDemoTeensy3x.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
// INT output of MCP2517 should be connected to a digital input port, with interrupt capability
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517
static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517

static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_INT = 38 ; // INT output of MCP2517

//——————————————————————————————————————————————————————————————————————————————
Expand Down Expand Up @@ -97,9 +97,9 @@ void setup () {

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

static unsigned gBlinkLedDate = 0 ;
static unsigned gReceivedFrameCount = 0 ;
static unsigned gSentFrameCount = 0 ;
static uint32_t gBlinkLedDate = 0 ;
static uint32_t gReceivedFrameCount = 0 ;
static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
// INT output of MCP2517 should be connected to a digital input port, with interrupt capability
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517
static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517

static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_INT = 38 ; // INT output of MCP2517

//——————————————————————————————————————————————————————————————————————————————
Expand Down Expand Up @@ -71,9 +71,9 @@ void setup () {

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

static unsigned gBlinkLedDate = 0 ;
static uint32_t gBlinkLedDate = 0 ;
static uint32_t gPhase = 0 ;
static uint32_t gReceiveFlags = (1 << 29) - 1 ;
static uint32_t gReceiveFlags = (1UL << 29) - 1 ;

//——————————————————————————————————————————————————————————————————————————————

Expand Down
14 changes: 7 additions & 7 deletions examples/LoopBackDemoTeensy3xNoInt/LoopBackDemoTeensy3xNoInt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// CS input of MCP2517 should be connected to a digital output port
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517
static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517

static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_CS = 31 ; // CS input of MCP2517

//——————————————————————————————————————————————————————————————————————————————
// ACAN2517FD Driver object
Expand Down Expand Up @@ -95,9 +95,9 @@ void setup () {

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

static unsigned gBlinkLedDate = 0 ;
static unsigned gReceivedFrameCount = 0 ;
static unsigned gSentFrameCount = 0 ;
static uint32_t gBlinkLedDate = 0 ;
static uint32_t gReceivedFrameCount = 0 ;
static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
// INT output of MCP2517 should be connected to a digital input port, with interrupt capability
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517
static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517

static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_CS = 31 ; // CS input of MCP2517
static const byte MCP2517_INT = 38 ; // INT output of MCP2517

//——————————————————————————————————————————————————————————————————————————————
Expand Down Expand Up @@ -71,7 +71,7 @@ void setup () {

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

static unsigned gBlinkLedDate = 0 ;
static uint32_t gBlinkLedDate = 0 ;
static uint32_t gPhase = 0 ;
static uint32_t gReceiveFlags = (1 << 11) - 1 ;

Expand Down
Loading

0 comments on commit 59fb4ac

Please sign in to comment.