|
| 1 | +//—————————————————————————————————————————————————————————————————————————————— |
| 2 | +// ACAN2517FD Demo in loopback mode, for Arduino Uno, without INT pin |
| 3 | +//—————————————————————————————————————————————————————————————————————————————— |
| 4 | + |
| 5 | +#include <ACAN2517FD.h> |
| 6 | +#include <SPI.h> |
| 7 | + |
| 8 | +//—————————————————————————————————————————————————————————————————————————————— |
| 9 | +// Very very important: put a 10kΩ resistor between CS and VDD of MCP2517FD |
| 10 | + |
| 11 | +static const byte MCP2517_CS = 10 ; // CS input of MCP2517 |
| 12 | + |
| 13 | +//—————————————————————————————————————————————————————————————————————————————— |
| 14 | +// ACAN2517FD Driver object |
| 15 | +//—————————————————————————————————————————————————————————————————————————————— |
| 16 | + |
| 17 | +ACAN2517FD can (MCP2517_CS, SPI, 255) ; // Last argument is 255 -> no interrupt pin |
| 18 | + |
| 19 | +//—————————————————————————————————————————————————————————————————————————————— |
| 20 | +// SETUP |
| 21 | +//—————————————————————————————————————————————————————————————————————————————— |
| 22 | + |
| 23 | +void setup () { |
| 24 | +//--- Start serial |
| 25 | + Serial.begin (115200) ; |
| 26 | +//--- Wait for serial (blink led at 10 Hz during waiting) |
| 27 | + while (!Serial) { |
| 28 | + delay (50) ; |
| 29 | + } |
| 30 | +//----------------------------------- Begin SPI |
| 31 | + SPI.begin () ; |
| 32 | +//--- Configure ACAN2517FD |
| 33 | + Serial.print ("sizeof (ACAN2517FDSettings): ") ; |
| 34 | + Serial.print (sizeof (ACAN2517FDSettings)) ; |
| 35 | + Serial.println (" bytes") ; |
| 36 | + Serial.println ("Configure ACAN2517FD") ; |
| 37 | +//--- For version >= 2.1.0 |
| 38 | + ACAN2517FDSettings settings (ACAN2517FDSettings::OSC_4MHz10xPLL, 125UL * 1000UL, DataBitRateFactor::x1) ; |
| 39 | +//--- For version < 2.1.0 |
| 40 | +// ACAN2517FDSettings settings (ACAN2517FDSettings::OSC_4MHz10xPLL, 125UL * 1000UL, ACAN2517FDSettings::DATA_BITRATE_x1) ; |
| 41 | + settings.mRequestedMode = ACAN2517FDSettings::InternalLoopBack ; // Select loopback mode |
| 42 | +//--- Default values are too high for an Arduino Uno that contains 2048 bytes of RAM: reduce them |
| 43 | + settings.mDriverTransmitFIFOSize = 1 ; |
| 44 | + settings.mDriverReceiveFIFOSize = 1 ; |
| 45 | +//--- RAM Usage |
| 46 | + Serial.print ("MCP2517FD RAM Usage: ") ; |
| 47 | + Serial.print (settings.ramUsage ()) ; |
| 48 | + Serial.println (" bytes") ; |
| 49 | +//--- Begin |
| 50 | + const uint32_t errorCode = can.begin (settings, NULL) ; // Second argument is NULL -> no interrupt service routine |
| 51 | + if (errorCode == 0) { |
| 52 | + Serial.print ("Bit Rate prescaler: ") ; |
| 53 | + Serial.println (settings.mBitRatePrescaler) ; |
| 54 | + Serial.print ("Arbitration Phase segment 1: ") ; |
| 55 | + Serial.println (settings.mArbitrationPhaseSegment1) ; |
| 56 | + Serial.print ("Arbitration Phase segment 2: ") ; |
| 57 | + Serial.println (settings.mArbitrationPhaseSegment2) ; |
| 58 | + Serial.print ("Arbitration SJW:") ; |
| 59 | + Serial.println (settings.mArbitrationSJW) ; |
| 60 | + Serial.print ("Actual Arbitration Bit Rate: ") ; |
| 61 | + Serial.print (settings.actualArbitrationBitRate ()) ; |
| 62 | + Serial.println (" bit/s") ; |
| 63 | + Serial.print ("Exact Arbitration Bit Rate ? ") ; |
| 64 | + Serial.println (settings.exactArbitrationBitRate () ? "yes" : "no") ; |
| 65 | + Serial.print ("Arbitration Sample point: ") ; |
| 66 | + Serial.print (settings.arbitrationSamplePointFromBitStart ()) ; |
| 67 | + Serial.println ("%") ; |
| 68 | + }else{ |
| 69 | + Serial.print ("Configuration error 0x") ; |
| 70 | + Serial.println (errorCode, HEX) ; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +//—————————————————————————————————————————————————————————————————————————————— |
| 75 | +// LOOP |
| 76 | +//—————————————————————————————————————————————————————————————————————————————— |
| 77 | + |
| 78 | +static uint32_t gSendDate = 0 ; |
| 79 | +static uint32_t gReceiveDate = 0 ; |
| 80 | +static uint32_t gReceivedFrameCount = 0 ; |
| 81 | +static uint32_t gSentFrameCount = 0 ; |
| 82 | + |
| 83 | +//—————————————————————————————————————————————————————————————————————————————— |
| 84 | + |
| 85 | +void loop () { |
| 86 | + can.poll () ; // No interrupt: call can.poll as often as possible |
| 87 | + CANFDMessage frame ; |
| 88 | + if (gSendDate < millis ()) { |
| 89 | + gSendDate += 1000 ; |
| 90 | + const bool ok = can.tryToSend (frame) ; |
| 91 | + if (ok) { |
| 92 | + gSentFrameCount += 1 ; |
| 93 | + Serial.print ("Sent: ") ; |
| 94 | + Serial.print (gSentFrameCount) ; |
| 95 | + }else{ |
| 96 | + Serial.print ("Send failure") ; |
| 97 | + } |
| 98 | + Serial.print (", receive overflows: ") ; |
| 99 | + Serial.println (can.hardwareReceiveBufferOverflowCount ()) ; |
| 100 | + } |
| 101 | + if (gReceiveDate < millis ()) { |
| 102 | + gReceiveDate += 4567 ; |
| 103 | + while (can.available ()) { |
| 104 | + can.receive (frame) ; |
| 105 | + gReceivedFrameCount ++ ; |
| 106 | + Serial.print ("Received: ") ; |
| 107 | + Serial.println (gReceivedFrameCount) ; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +//—————————————————————————————————————————————————————————————————————————————— |
0 commit comments