|
| 1 | +//—————————————————————————————————————————————————————————————————————————————— |
| 2 | +// ACAN2517FD Demo in loopback mode, using hardware SPI1, with an external interrupt |
| 3 | +//—————————————————————————————————————————————————————————————————————————————— |
| 4 | +// Every 1 000 messages, this sketch calls the end method, deallocates the driver, |
| 5 | +// creates a new one, and configures it by calling the begin method. |
| 6 | +//—————————————————————————————————————————————————————————————————————————————— |
| 7 | + |
| 8 | +#include <ACAN2517FD.h> |
| 9 | + |
| 10 | +//—————————————————————————————————————————————————————————————————————————————— |
| 11 | +// MCP2517 connections: adapt theses settings to your design |
| 12 | +// As hardware SPI is used, you should select pins that support SPI functions. |
| 13 | +// This sketch is designed for a Teensy 3.5, using SPI1 |
| 14 | +// But standard Teensy 3.5 SPI1 pins are not used |
| 15 | +// SCK input of MCP2517FD is connected to pin #32 |
| 16 | +// SDI input of MCP2517FD is connected to pin #0 |
| 17 | +// SDO output of MCP2517FD is connected to pin #1 |
| 18 | +// CS input of MCP2517FD should be connected to a digital output port |
| 19 | +// INT output of MCP2517FD should be connected to a digital input port, with interrupt capability |
| 20 | +//—————————————————————————————————————————————————————————————————————————————— |
| 21 | + |
| 22 | +static const byte MCP2517_SCK = 32 ; // SCK input of MCP2517 |
| 23 | +static const byte MCP2517_SDI = 0 ; // SDI input of MCP2517 |
| 24 | +static const byte MCP2517_SDO = 1 ; // SDO output of MCP2517 |
| 25 | + |
| 26 | +static const byte MCP2517_CS = 31 ; // CS input of MCP2517 |
| 27 | +static const byte MCP2517_INT = 38 ; // INT output of MCP2517 |
| 28 | + |
| 29 | +//—————————————————————————————————————————————————————————————————————————————— |
| 30 | +// ACAN2517FD Driver object |
| 31 | +//—————————————————————————————————————————————————————————————————————————————— |
| 32 | + |
| 33 | +ACAN2517FD * can = NULL ; |
| 34 | + |
| 35 | +//—————————————————————————————————————————————————————————————————————————————— |
| 36 | +// SETUP |
| 37 | +//—————————————————————————————————————————————————————————————————————————————— |
| 38 | + |
| 39 | +void setup () { |
| 40 | +//--- Switch on builtin led |
| 41 | + pinMode (LED_BUILTIN, OUTPUT) ; |
| 42 | + digitalWrite (LED_BUILTIN, HIGH) ; |
| 43 | +//--- Start serial |
| 44 | + Serial.begin (38400) ; |
| 45 | +//--- Wait for serial (blink led at 10 Hz during waiting) |
| 46 | + while (!Serial) { |
| 47 | + delay (50) ; |
| 48 | + digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ; |
| 49 | + } |
| 50 | +//--- Define alternate pins for SPI1 (see https://www.pjrc.com/teensy/td_libs_SPI.html) |
| 51 | + Serial.print ("Using pin #") ; |
| 52 | + Serial.print (MCP2517_SDI) ; |
| 53 | + Serial.print (" for MOSI: ") ; |
| 54 | + Serial.println (SPI1.pinIsMOSI (MCP2517_SDI) ? "yes" : "NO!!!") ; |
| 55 | + Serial.print ("Using pin #") ; |
| 56 | + Serial.print (MCP2517_SDO) ; |
| 57 | + Serial.print (" for MISO: ") ; |
| 58 | + Serial.println (SPI1.pinIsMISO (MCP2517_SDO) ? "yes" : "NO!!!") ; |
| 59 | + Serial.print ("Using pin #") ; |
| 60 | + Serial.print (MCP2517_SCK) ; |
| 61 | + Serial.print (" for SCK: ") ; |
| 62 | + Serial.println (SPI1.pinIsSCK (MCP2517_SCK) ? "yes" : "NO!!!") ; |
| 63 | + SPI1.setMOSI (MCP2517_SDI) ; |
| 64 | + SPI1.setMISO (MCP2517_SDO) ; |
| 65 | + SPI1.setSCK (MCP2517_SCK) ; |
| 66 | +//----------------------------------- Begin SPI1 |
| 67 | + SPI1.begin () ; |
| 68 | +//--- Configure ACAN2517FD |
| 69 | + Serial.println ("Configure ACAN2517FD") ; |
| 70 | +//--- Settings |
| 71 | + ACAN2517FDSettings settings (ACAN2517FDSettings::OSC_4MHz10xPLL, 125 * 1000, DataBitRateFactor::x1) ; |
| 72 | +//--- Select loopback mode |
| 73 | + settings.mRequestedMode = ACAN2517FDSettings::InternalLoopBack ; |
| 74 | +//--- RAM Usage |
| 75 | + Serial.print ("MCP2517FD RAM Usage: ") ; |
| 76 | + Serial.print (settings.ramUsage ()) ; |
| 77 | + Serial.println (" bytes") ; |
| 78 | +//--- Begin |
| 79 | + can = new ACAN2517FD (MCP2517_CS, SPI1, MCP2517_INT) ; |
| 80 | + const uint32_t errorCode = can->begin (settings, [] { can->isr () ; }) ; |
| 81 | + if (errorCode == 0) { |
| 82 | + Serial.print ("Bit Rate prescaler: ") ; |
| 83 | + Serial.println (settings.mBitRatePrescaler) ; |
| 84 | + Serial.print ("Arbitration Phase segment 1: ") ; |
| 85 | + Serial.println (settings.mArbitrationPhaseSegment1) ; |
| 86 | + Serial.print ("Arbitration Phase segment 2: ") ; |
| 87 | + Serial.println (settings.mArbitrationPhaseSegment2) ; |
| 88 | + Serial.print ("Arbitration SJW:") ; |
| 89 | + Serial.println (settings.mArbitrationSJW) ; |
| 90 | + Serial.print ("Actual Arbitration Bit Rate: ") ; |
| 91 | + Serial.print (settings.actualArbitrationBitRate ()) ; |
| 92 | + Serial.println (" bit/s") ; |
| 93 | + Serial.print ("Exact Arbitration Bit Rate ? ") ; |
| 94 | + Serial.println (settings.exactArbitrationBitRate () ? "yes" : "no") ; |
| 95 | + Serial.print ("Arbitration Sample point: ") ; |
| 96 | + Serial.print (settings.arbitrationSamplePointFromBitStart ()) ; |
| 97 | + Serial.println ("%") ; |
| 98 | + Serial.print ("Data Phase segment 1: ") ; |
| 99 | + Serial.println (settings.mDataPhaseSegment1) ; |
| 100 | + Serial.print ("Data Phase segment 2: ") ; |
| 101 | + Serial.println (settings.mDataPhaseSegment2) ; |
| 102 | + Serial.print ("Data SJW:") ; |
| 103 | + Serial.println (settings.mDataSJW) ; |
| 104 | + Serial.print ("Actual Data Bit Rate: ") ; |
| 105 | + Serial.print (settings.actualDataBitRate ()) ; |
| 106 | + Serial.println (" bit/s") ; |
| 107 | + }else{ |
| 108 | + Serial.print ("Configuration error 0x") ; |
| 109 | + Serial.println (errorCode, HEX) ; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +//---------------------------------------------------------------------------------------------------------------------- |
| 114 | + |
| 115 | +static uint32_t gBlinkLedDate = 2000 ; |
| 116 | +static uint32_t gFrameCount = 0 ; |
| 117 | +static uint32_t gDebit = 0 ; |
| 118 | +static uint32_t gDebitMax = 0 ; |
| 119 | +static CANFDMessage gCurrentFrame ; |
| 120 | +static bool gWaitingForReception = false ; |
| 121 | + |
| 122 | +//—————————————————————————————————————————————————————————————————————————————— |
| 123 | + |
| 124 | +void loop () { |
| 125 | + if (gBlinkLedDate < millis ()) { |
| 126 | + gBlinkLedDate += 2000 ; |
| 127 | + digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ; |
| 128 | + if (gDebitMax < gDebit) { |
| 129 | + gDebitMax = gDebit ; |
| 130 | + } |
| 131 | + Serial.print ("Sent: ") ; |
| 132 | + Serial.print (gFrameCount) ; |
| 133 | + Serial.print (", ") ; |
| 134 | + Serial.print (gDebit) ; |
| 135 | + Serial.print (" bytes/s, max ") ; |
| 136 | + Serial.print (gDebitMax) ; |
| 137 | + Serial.println (" bytes/s") ; |
| 138 | + gDebit = 0 ; |
| 139 | + } |
| 140 | +//--- Send frame ? |
| 141 | + if (!gWaitingForReception) { |
| 142 | + if ((gFrameCount % 1000) == 0) { |
| 143 | + //--- Stop CAN |
| 144 | + const bool ok = can->end () ; |
| 145 | + delete can ; can = NULL ; |
| 146 | + Serial.print ("Reset MCP2517FD, ") ; |
| 147 | + Serial.println (ok ? "ok" : "error") ; |
| 148 | + //--- Settings |
| 149 | + ACAN2517FDSettings settings (ACAN2517FDSettings::OSC_4MHz10xPLL, 125 * 1000, DataBitRateFactor::x1) ; |
| 150 | + //--- Select loopback mode |
| 151 | + settings.mRequestedMode = ACAN2517FDSettings::InternalLoopBack ; |
| 152 | + //--- Begin |
| 153 | + can = new ACAN2517FD (MCP2517_CS, SPI1, MCP2517_INT) ; |
| 154 | + const uint32_t errorCode = can->begin (settings, [] { can->isr () ; }) ; |
| 155 | + Serial.print ("Reconfigure MCP2517FD, error ") ; |
| 156 | + Serial.println (errorCode) ; |
| 157 | + } |
| 158 | + gCurrentFrame.ext = (random () & 1) == 0 ; |
| 159 | + gCurrentFrame.id = (uint32_t) random () ; |
| 160 | + if (gCurrentFrame.ext) { |
| 161 | + gCurrentFrame.id &= 0x1FFFFFFF ; |
| 162 | + }else{ |
| 163 | + gCurrentFrame.id &= 0x7FF ; |
| 164 | + } |
| 165 | + gCurrentFrame.len = (uint8_t) (((uint32_t) random ()) % 65) ; |
| 166 | + for (uint8_t i=0 ; i<gCurrentFrame.len ; i++) { |
| 167 | + gCurrentFrame.data [i] = (uint8_t) random () ; |
| 168 | + } |
| 169 | + gCurrentFrame.pad () ; |
| 170 | + gDebit += gCurrentFrame.len ; |
| 171 | + const bool ok = can->tryToSend (gCurrentFrame) ; |
| 172 | + if (ok) { |
| 173 | + gFrameCount += 1 ; |
| 174 | + gWaitingForReception = true ; |
| 175 | + }else{ |
| 176 | + Serial.println ("Send failure") ; |
| 177 | + } |
| 178 | + } |
| 179 | +//--- Receive message ? |
| 180 | + if (gWaitingForReception && can->available ()) { |
| 181 | + gWaitingForReception = false ; |
| 182 | + CANFDMessage frame ; |
| 183 | + can->receive (frame) ; |
| 184 | + //--- Check receive frame is identical to sent frame |
| 185 | + if (frame.ext != gCurrentFrame.ext) { |
| 186 | + Serial.println ("ext error") ; |
| 187 | + } |
| 188 | + if (frame.id != gCurrentFrame.id) { |
| 189 | + Serial.println ("id error") ; |
| 190 | + } |
| 191 | + if (frame.len != gCurrentFrame.len) { |
| 192 | + Serial.println ("length error") ; |
| 193 | + }else{ |
| 194 | + bool ok = true ; |
| 195 | + for (uint8_t i=0 ; (i<frame.len) && ok ; i++) { |
| 196 | + ok = frame.data [i] == gCurrentFrame.data [i] ; |
| 197 | + } |
| 198 | + if (!ok) { |
| 199 | + Serial.println ("data error") ; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +//—————————————————————————————————————————————————————————————————————————————— |
0 commit comments