Skip to content

Commit

Permalink
Release 2.0.7, added Raspberry Pi Pico doc and sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
pierremolinaro committed Apr 21, 2021
1 parent a57f10b commit d6ff156
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/LoopBackDemo/LoopBackDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
// use B connections for MISO, MOSI, SCK, #9 or #10 for CS (as you want),
// #2 or #3 for INT (as you want).
//——————————————————————————————————————————————————————————————————————————————
// Error codes and possible causes:
// In case you see "Configuration error 0x1", the Arduino doesn't communicate
// with the 2515. You will get this error if there is no CAN shield or if
// the CS pin is incorrect.
// In case you see succes up to "Sent: 17" and from then on "Send failure":
// There is a problem with the interrupt. Check if correct pin is configured
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2515_CS = 10 ; // CS input of MCP2515 (adapt to your design)
static const byte MCP2515_INT = 3 ; // INT output of MCP2515 (adapt to your design)
Expand Down
126 changes: 126 additions & 0 deletions examples/LoopBackDemoRaspberryPiPico/LoopBackDemoRaspberryPiPico.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//——————————————————————————————————————————————————————————————————————————————
// ACAN2515 Demo in loopback mode, for the Raspberry Pi Pico
// Thanks to Duncan Greenwood for providing this sample sketch
//——————————————————————————————————————————————————————————————————————————————

#ifndef ARDUINO_ARCH_RP2040
#error "Select a Raspberry Pi Pico board"
#endif

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

#include <ACAN2515.h>

//——————————————————————————————————————————————————————————————————————————————
// The Pico has two SPI peripherals, SPI and SPI1. Either (or both) can be used.
// The are no default pin assignments to these must be set explicitly.
// At the time of writing (Apr 2021) there is no official Arduino core for the Pico
// Testing was done with Earle Philhower's arduino-pico core:
// https://github.com/earlephilhower/arduino-pico
// There is a small bug in release 1.0.3 so you will require at least 1.0.4
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2515_SCK = 2 ; // SCK input of MCP2515
static const byte MCP2515_MOSI = 3 ; // SDI input of MCP2515
static const byte MCP2515_MISO = 4 ; // SDO output of MCP2517

static const byte MCP2515_CS = 5 ; // CS input of MCP2515 (adapt to your design)
static const byte MCP2515_INT = 1 ; // INT output of MCP2515 (adapt to your design)

//——————————————————————————————————————————————————————————————————————————————
// MCP2515 Driver object
//——————————————————————————————————————————————————————————————————————————————

ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;

//——————————————————————————————————————————————————————————————————————————————
// MCP2515 Quartz: adapt to your design
//——————————————————————————————————————————————————————————————————————————————

static const uint32_t QUARTZ_FREQUENCY = 20UL * 1000UL * 1000UL ; // 20 MHz

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

void setup () {
//--- Switch on builtin led
pinMode (LED_BUILTIN, OUTPUT) ;
digitalWrite (LED_BUILTIN, HIGH) ;
//--- Start serial
Serial.begin (115200) ;
//--- Wait for serial (blink led at 10 Hz during waiting)
while (!Serial) {
delay (50) ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
//--- There are no default SPI pins so they must be explicitly assigned
SPI.setSCK(MCP2515_SCK);
SPI.setTX(MCP2515_MOSI);
SPI.setRX(MCP2515_MISO);
SPI.setCS(MCP2515_CS);
//--- Begin SPI
SPI.begin () ;
//--- Configure ACAN2515
Serial.println ("Configure ACAN2515") ;
ACAN2515Settings settings (QUARTZ_FREQUENCY, 125UL * 1000UL) ; // CAN bit rate 125 kb/s
settings.mRequestedMode = ACAN2515Settings::LoopBackMode ; // Select loopback mode
const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
if (errorCode == 0) {
Serial.print ("Bit Rate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Serial.print ("Propagation Segment: ") ;
Serial.println (settings.mPropagationSegment) ;
Serial.print ("Phase segment 1: ") ;
Serial.println (settings.mPhaseSegment1) ;
Serial.print ("Phase segment 2: ") ;
Serial.println (settings.mPhaseSegment2) ;
Serial.print ("SJW: ") ;
Serial.println (settings.mSJW) ;
Serial.print ("Triple Sampling: ") ;
Serial.println (settings.mTripleSampling ? "yes" : "no") ;
Serial.print ("Actual bit rate: ") ;
Serial.print (settings.actualBitRate ()) ;
Serial.println (" bit/s") ;
Serial.print ("Exact bit rate ? ") ;
Serial.println (settings.exactBitRate () ? "yes" : "no") ;
Serial.print ("Sample point: ") ;
Serial.print (settings.samplePointFromBitStart ()) ;
Serial.println ("%") ;
} else {
Serial.print ("Configuration error 0x") ;
Serial.println (errorCode, HEX) ;
}
}

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

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

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

void loop () {
CANMessage frame ;
if (gBlinkLedDate < millis ()) {
gBlinkLedDate += 2000 ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
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) ;
}
}

//——————————————————————————————————————————————————————————————————————————————
Binary file modified extras/acan2515.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=ACAN2515
version=2.0.6
version=2.0.7
author=Pierre Molinaro
maintainer=Pierre Molinaro <[email protected]>
sentence=Driver for MCP2515 CAN Controller
paragraph=Arduino CAN network driver for the MCP2515 CAN Controller. Compatible with ACAN, ACAN2517, ACAN2517FD libraries. The default configuration enables to receive all the frames. User can easily defines reception filters. Runs on ESP32 from version 1.1.2.
paragraph=Arduino CAN network driver for the MCP2515 CAN Controller. Compatible with ACAN, ACAN2515Tiny, ACAN2517, ACAN2517FD libraries. The default configuration enables to receive all the frames. User can easily defines reception filters. Runs on ESP32 from version 1.1.2, on Raspberry Pi Pico.
category=Communication
url=https://github.com/pierremolinaro/acan2515
architectures=*

0 comments on commit d6ff156

Please sign in to comment.