Skip to content

Commit 74e5b29

Browse files
Version 2.1.16
1 parent 314dc5a commit 74e5b29

File tree

9 files changed

+416
-342
lines changed

9 files changed

+416
-342
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//——————————————————————————————————————————————————————————————————————————————
2+
// ACAN2517FD Demo in loopback mode, for Adafruit M0
3+
//——————————————————————————————————————————————————————————————————————————————
4+
5+
#ifndef ARDUINO_SAMD_ZERO
6+
#error "Select 'Adafruit Feather M0' board"
7+
#endif
8+
9+
//——————————————————————————————————————————————————————————————————————————————
10+
11+
#include <ACAN2517FD.h>
12+
#include <SPI.h>
13+
#include <wiring_private.h>
14+
15+
//——————————————————————————————————————————————————————————————————————————————
16+
// Very very important: put a 10kΩ resistor between CS and VDD of MCP2517FD
17+
18+
static const byte MCP2517_CS = 6 ; // CS input of MCP2517
19+
static const byte MCP2517_INT = 5 ; // INT output of MCP2517
20+
static const byte MCP2517_SO = 10 ; // SO output of MCP2517
21+
static const byte MCP2517_SI = 11 ; // SI input of MCP2517
22+
static const byte MCP2517_CLK = 12 ; // CLK input of MCP2517
23+
24+
//——————————————————————————————————————————————————————————————————————————————
25+
// ACAN2517FD Driver object
26+
//——————————————————————————————————————————————————————————————————————————————
27+
28+
SPIClass mySPI (&sercom1, MCP2517_SO, MCP2517_CLK, MCP2517_SI, SPI_PAD_0_SCK_3, SERCOM_RX_PAD_2);
29+
30+
ACAN2517FD can (MCP2517_CS, mySPI, MCP2517_INT) ;
31+
32+
//——————————————————————————————————————————————————————————————————————————————
33+
// SETUP
34+
//——————————————————————————————————————————————————————————————————————————————
35+
36+
void setup () {
37+
//--- Start serial
38+
Serial.begin (115200) ;
39+
//--- Wait for serial (blink led at 10 Hz during waiting)
40+
while (!Serial) {
41+
delay (50) ;
42+
}
43+
//----------------------------------- Begin SPI
44+
mySPI.begin () ;
45+
pinPeripheral (MCP2517_SI, PIO_SERCOM);
46+
pinPeripheral (MCP2517_CLK, PIO_SERCOM);
47+
pinPeripheral (MCP2517_SO, PIO_SERCOM);
48+
//--- Configure ACAN2517FD
49+
Serial.print ("sizeof (ACAN2517FDSettings): ") ;
50+
Serial.print (sizeof (ACAN2517FDSettings)) ;
51+
Serial.println (" bytes") ;
52+
Serial.println ("Configure ACAN2517FD") ;
53+
//--- For version >= 2.1.0
54+
ACAN2517FDSettings settings (ACAN2517FDSettings::OSC_20MHz, 500UL * 1000UL, DataBitRateFactor::x1) ;
55+
settings.mRequestedMode = ACAN2517FDSettings::InternalLoopBack ; // Select loopback mode
56+
//--- Default values are too high for an Arduino Uno that contains 2048 bytes of RAM: reduce them
57+
settings.mDriverTransmitFIFOSize = 1 ;
58+
settings.mDriverReceiveFIFOSize = 1 ;
59+
//--- RAM Usage
60+
Serial.print ("MCP2517FD RAM Usage: ") ;
61+
Serial.print (settings.ramUsage ()) ;
62+
Serial.println (" bytes") ;
63+
//--- Begin
64+
const uint32_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
65+
if (errorCode == 0) {
66+
Serial.print ("Bit Rate prescaler: ") ;
67+
Serial.println (settings.mBitRatePrescaler) ;
68+
Serial.print ("Arbitration Phase segment 1: ") ;
69+
Serial.println (settings.mArbitrationPhaseSegment1) ;
70+
Serial.print ("Arbitration Phase segment 2: ") ;
71+
Serial.println (settings.mArbitrationPhaseSegment2) ;
72+
Serial.print ("Arbitration SJW:") ;
73+
Serial.println (settings.mArbitrationSJW) ;
74+
Serial.print ("Actual Arbitration Bit Rate: ") ;
75+
Serial.print (settings.actualArbitrationBitRate ()) ;
76+
Serial.println (" bit/s") ;
77+
Serial.print ("Exact Arbitration Bit Rate ? ") ;
78+
Serial.println (settings.exactArbitrationBitRate () ? "yes" : "no") ;
79+
Serial.print ("Arbitration Sample point: ") ;
80+
Serial.print (settings.arbitrationSamplePointFromBitStart ()) ;
81+
Serial.println ("%") ;
82+
}else{
83+
Serial.print ("Configuration error 0x") ;
84+
Serial.println (errorCode, HEX) ;
85+
}
86+
}
87+
88+
//——————————————————————————————————————————————————————————————————————————————
89+
// LOOP
90+
//——————————————————————————————————————————————————————————————————————————————
91+
92+
static uint32_t gSendDate = 0 ;
93+
static uint32_t gReceiveDate = 0 ;
94+
static uint32_t gReceivedFrameCount = 0 ;
95+
static uint32_t gSentFrameCount = 0 ;
96+
97+
//——————————————————————————————————————————————————————————————————————————————
98+
99+
void loop () {
100+
CANFDMessage frame ;
101+
if (gSendDate < millis ()) {
102+
frame.type = CANFDMessage::CANFD_WITH_BIT_RATE_SWITCH ;
103+
gSendDate += 1000 ;
104+
const bool ok = can.tryToSend (frame) ;
105+
if (ok) {
106+
gSentFrameCount += 1 ;
107+
Serial.print ("Sent: ") ;
108+
Serial.print (gSentFrameCount) ;
109+
}else{
110+
Serial.print ("Send failure") ;
111+
}
112+
Serial.print (", receive overflows: ") ;
113+
Serial.println (can.hardwareReceiveBufferOverflowCount ()) ;
114+
}
115+
if (gReceiveDate < millis ()) {
116+
gReceiveDate += 4567 ;
117+
while (can.available ()) {
118+
can.receive (frame) ;
119+
gReceivedFrameCount +=1 ;
120+
Serial.print ("Received: ") ;
121+
Serial.println (gReceivedFrameCount) ;
122+
}
123+
}
124+
}
125+
126+
//——————————————————————————————————————————————————————————————————————————————

extras/acan2517FD.pdf

392 Bytes
Binary file not shown.

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ACAN2517FD
2-
version=2.1.15
2+
version=2.1.16
33
author=Pierre Molinaro
44
maintainer=Pierre Molinaro <Pierre.Molinaro@pcmolinaro.name>
55
sentence=Driver for MCP2517FD and MCP2518FD CAN Controller (CAN FD mode)

src/ACAN2517FD.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ mINT (inINT),
184184
mUsesTXQ (false),
185185
mHardwareTxFIFOFull (false),
186186
mRxInterruptEnabled (true),
187-
mHasDataBitRate (false),
188187
mTransmitFIFOPayload (0),
189188
mTXQBufferPayload (0),
190189
mReceiveFIFOPayload (0),
@@ -478,17 +477,14 @@ uint32_t ACAN2517FD::begin (const ACAN2517FDSettings & inSettings,
478477
// bits 11-8: TSEG2 - 1
479478
// bits 7-4: unused
480479
// bits 3-0: SJW - 1
481-
mHasDataBitRate = inSettings.mDataBitRateFactor != ::DataBitRateFactor::x1 ;
482-
if (mHasDataBitRate) {
483-
data = inSettings.mBitRatePrescaler - 1 ;
484-
data <<= 8 ;
485-
data |= inSettings.mDataPhaseSegment1 - 1 ;
486-
data <<= 8 ;
487-
data |= inSettings.mDataPhaseSegment2 - 1 ;
488-
data <<= 8 ;
489-
data |= inSettings.mDataSJW - 1 ;
490-
writeRegister32 (DBTCFG_REGISTER, data) ;
491-
}
480+
data = inSettings.mBitRatePrescaler - 1 ;
481+
data <<= 8 ;
482+
data |= inSettings.mDataPhaseSegment1 - 1 ;
483+
data <<= 8 ;
484+
data |= inSettings.mDataPhaseSegment2 - 1 ;
485+
data <<= 8 ;
486+
data |= inSettings.mDataSJW - 1 ;
487+
writeRegister32 (DBTCFG_REGISTER, data) ;
492488
//----------------------------------- Request mode (CON_REGISTER + 3, DS20005688B, page 24)
493489
// bits 7-4: Transmit Bandwith Sharing Bits ---> 0
494490
// bit 3: Abort All Pending Transmissions bit --> 0
@@ -677,9 +673,7 @@ void ACAN2517FD::appendInControllerTxFIFO (const CANFDMessage & inMessage) {
677673
break ;
678674
case CANFDMessage::CANFD_WITH_BIT_RATE_SWITCH :
679675
flags |= 1 << 7 ; // Set FDF bit
680-
if (mHasDataBitRate) {
681-
flags |= 1 << 6 ; // Set BRS bit
682-
}
676+
flags |= 1 << 6 ; // Set BRS bit
683677
break ;
684678
}
685679
//--- Word count
@@ -742,9 +736,7 @@ bool ACAN2517FD::sendViaTXQ (const CANFDMessage & inMessage) {
742736
break ;
743737
case CANFDMessage::CANFD_WITH_BIT_RATE_SWITCH :
744738
flags |= 1 << 7 ; // Set FDF bit
745-
if (mHasDataBitRate) {
746-
flags |= 1 << 6 ; // Set BRS bit
747-
}
739+
flags |= 1 << 6 ; // Set BRS bit
748740
break ;
749741
}
750742
//--- Word count

0 commit comments

Comments
 (0)