-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.1.0 : fixed extended frame reception
- Loading branch information
1 parent
68aa8b8
commit 977c3eb
Showing
7 changed files
with
216 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
examples/LoopBackDemoRaspberryPiPicoSPI1/LoopBackDemoRaspberryPiPicoSPI1.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
//—————————————————————————————————————————————————————————————————————————————— | ||
// ACAN2515 Demo in loopback mode, for the Raspberry Pi Pico | ||
// Demo sketch that uses SPI1 | ||
//—————————————————————————————————————————————————————————————————————————————— | ||
|
||
#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 so they must be set explicitly. | ||
// Testing was done with Earle Philhower's arduino-pico core: | ||
// https://github.com/earlephilhower/arduino-pico | ||
//—————————————————————————————————————————————————————————————————————————————— | ||
|
||
static const byte MCP2515_SCK = 14 ; // SCK input of MCP2515 (adapt to your design) | ||
static const byte MCP2515_MOSI = 15 ; // SDI input of MCP2515 (adapt to your design) | ||
static const byte MCP2515_MISO = 12 ; // SDO output of MCP2515 (adapt to your design) | ||
|
||
static const byte MCP2515_CS = 13 ; // CS input of MCP2515 (adapt to your design) | ||
static const byte MCP2515_INT = 11 ; // INT output of MCP2515 (adapt to your design) | ||
|
||
//—————————————————————————————————————————————————————————————————————————————— | ||
// MCP2515 Driver object | ||
//—————————————————————————————————————————————————————————————————————————————— | ||
|
||
ACAN2515 can (MCP2515_CS, SPI1, 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 SPI1 pins so they must be explicitly assigned | ||
SPI1.setSCK (MCP2515_SCK); | ||
SPI1.setTX (MCP2515_MOSI); | ||
SPI1.setRX (MCP2515_MISO); | ||
SPI1.setCS (MCP2515_CS); | ||
//--- Begin SPI1 | ||
SPI1.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)) ; | ||
frame.ext = true ; | ||
frame.id = 0x1FFFFFFF ; | ||
frame.len = 8 ; | ||
frame.data [0] = 0x11 ; | ||
frame.data [1] = 0x22 ; | ||
frame.data [2] = 0x33 ; | ||
frame.data [3] = 0x44 ; | ||
frame.data [4] = 0x55 ; | ||
frame.data [5] = 0x66 ; | ||
frame.data [6] = 0x77 ; | ||
frame.data [7] = 0x88 ; | ||
const bool ok = can.tryToSend (frame) ; | ||
if (ok) { | ||
gSentFrameCount += 1 ; | ||
Serial.print ("Sent: ") ; | ||
Serial.println (gSentFrameCount) ; | ||
}else{ | ||
Serial.println ("Send failure") ; | ||
} | ||
} | ||
if (can.receive (frame)) { | ||
gReceivedFrameCount ++ ; | ||
Serial.print (" id: ");Serial.println (frame.id,HEX); | ||
Serial.print (" ext: ");Serial.println (frame.ext); | ||
Serial.print (" rtr: ");Serial.println (frame.rtr); | ||
Serial.print (" len: ");Serial.println (frame.len); | ||
Serial.print (" data: "); | ||
for(int x=0;x<frame.len;x++) { | ||
Serial.print (frame.data[x],HEX); Serial.print(":"); | ||
} | ||
Serial.println (""); | ||
Serial.print ("Received: ") ; | ||
Serial.println (gReceivedFrameCount) ; | ||
} | ||
} | ||
|
||
|
||
//—————————————————————————————————————————————————————————————————————————————— |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=ACAN2515 | ||
version=2.0.9 | ||
version=2.1.0 | ||
author=Pierre Molinaro | ||
maintainer=Pierre Molinaro <[email protected]> | ||
sentence=Driver for MCP2515 CAN Controller | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.