From 9d839b680be1f1e89fc3ad398fc5783f9e6bd390 Mon Sep 17 00:00:00 2001 From: Michael Waiblinger Date: Mon, 30 May 2016 23:34:41 +0200 Subject: [PATCH] initial --- src/CANode.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/CANode.cpp diff --git a/src/CANode.cpp b/src/CANode.cpp new file mode 100644 index 0000000..3bcf6fb --- /dev/null +++ b/src/CANode.cpp @@ -0,0 +1,111 @@ +#include + +// ------------------------------------------------------------- +// CANtest for Teensy 3.1 +// by teachop +// +// This test is talking to a single other echo-node on the bus. +// 6 frames are transmitted and rx frames are counted. +// Tx and rx are done in a way to force some driver buffering. +// Serial is used to print the ongoing status. +// + +#include +#include + +#define CAN_RS_PIN 2 + +Metro sysTimer = Metro(1);// milliseconds + +int led = 13; +FlexCAN CANbus(125000); +static CAN_message_t msg,rxmsg; +static uint8_t hex[17] = "0123456789abcdef"; + +int txCount,rxCount; +unsigned int txTimer,rxTimer; + + +// ------------------------------------------------------------- +static void hexDump(uint8_t dumpLen, uint8_t *bytePtr) +{ + uint8_t working; + while( dumpLen-- ) { + working = *bytePtr++; + Serial.write( hex[ working>>4 ] ); + Serial.write( hex[ working&15 ] ); + } + Serial.write('\r'); + Serial.write('\n'); +} + + +// ------------------------------------------------------------- +void setup(void) +{ + pinMode(CAN_RS_PIN,OUTPUT); + digitalWrite(CAN_RS_PIN,0); + CANbus.begin(); + pinMode(led, OUTPUT); + digitalWrite(led, 1); + + delay(1000); + Serial.println(F("Hello Teensy 3.1 CAN Test.")); + + sysTimer.reset(); +} + + +// ------------------------------------------------------------- +void loop(void) +{ + // service software timers based on Metro tick + if ( sysTimer.check() ) { + if ( txTimer ) { + --txTimer; + } + if ( rxTimer ) { + --rxTimer; + } + } + + // if not time-delayed, read CAN messages and print 1st byte + if ( !rxTimer ) { + while ( CANbus.read(rxmsg) ) { + //hexDump( sizeof(rxmsg), (uint8_t *)&rxmsg ); + Serial.write(rxmsg.buf[0]); + rxCount++; + } + } + + // insert a time delay between transmissions + if ( !txTimer ) { + // if frames were received, print the count + if ( rxCount ) { + Serial.write('='); + Serial.print(rxCount); + Serial.print(",Hello="); + Serial.print(rxmsg.id,HEX); + rxCount = 0; + } + txTimer = 100;//milliseconds + msg.len = 8; + msg.id = 0x222; + for( int idx=0; idx<8; ++idx ) { + msg.buf[idx] = '0'+idx; + } + // send 6 at a time to force tx buffering + txCount = 16; + digitalWrite(led, 1); + Serial.println("."); + while ( txCount-- ) { + CANbus.write(msg); + msg.buf[0]++; + } + digitalWrite(led, 0); + // time delay to force some rx data queue use + rxTimer = 3;//milliseconds + } + +} +