Michael Waiblinger
9 years ago
commit
9d839b680b
1 changed files with 111 additions and 0 deletions
@ -0,0 +1,111 @@ |
|||
#include <Arduino.h> |
|||
|
|||
// -------------------------------------------------------------
|
|||
// 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 <Metro.h> |
|||
#include <FlexCAN.h> |
|||
|
|||
#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
|
|||
} |
|||
|
|||
} |
|||
|
Loading…
Reference in new issue