|
| 1 | +// Spam messages out CAN0 as fast as the Due will allow! |
| 2 | +// |
| 3 | +// Use together with the can_spammer_test.py script to verify peak Canalyst-II |
| 4 | +// receive performance. |
| 5 | +// |
| 6 | +// NOTE: Requires due_can library. Library v2.0.1 sometimes sends CAN frames out |
| 7 | +// of order (or drops them, possibly?), so will give incorrect results. The |
| 8 | +// master branch library version doesn't have this issue. |
| 9 | +// |
| 10 | +#include "variant.h" |
| 11 | +#include <due_can.h> |
| 12 | + |
| 13 | +// Leave defined if you use native port, comment if using programming port |
| 14 | +//#define Serial SerialUSB |
| 15 | + |
| 16 | +void setup() |
| 17 | +{ |
| 18 | + Serial.begin(115200); |
| 19 | + Can0.begin(CAN_BPS_1000K); |
| 20 | + Serial.println("Starting..."); |
| 21 | +} |
| 22 | + |
| 23 | +void loop() |
| 24 | +{ |
| 25 | + CAN_FRAME outgoing; |
| 26 | + outgoing.id = 0x400; |
| 27 | + outgoing.length = 8; |
| 28 | + outgoing.data.value = 0xDEADBEEF00000000ULL; |
| 29 | + |
| 30 | + while(1) { |
| 31 | + // Sending will fail unless a device on the bus ACKs the frame |
| 32 | + if(Can0.sendFrame(outgoing)) { |
| 33 | + outgoing.data.value++; |
| 34 | + } |
| 35 | + |
| 36 | + // Every second, print an estimate of the sending rate |
| 37 | + static uint32_t last_seconds; |
| 38 | + uint32_t seconds = millis() / 1000; |
| 39 | + if (seconds != last_seconds) { |
| 40 | + uint32_t counter = outgoing.data.value; // Log the lower word only, Arduino doesn't format 64-bit integers |
| 41 | + static uint32_t last_counter; |
| 42 | + Serial.print(seconds); |
| 43 | + Serial.print("s "); |
| 44 | + Serial.print((uint32_t)counter); |
| 45 | + Serial.print(" msgs "); |
| 46 | + Serial.print(((uint32_t)counter - last_counter) / (seconds - last_seconds)); |
| 47 | + Serial.println("msg/s"); |
| 48 | + last_counter = counter; |
| 49 | + last_seconds = seconds; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments