// -------------------------------------------------------------
// based on CANtest for Teensy 3.1 example by teachop
//
#include <Metro.h>
#include <FlexCAN.h>
#include <SPI.h>
#include <SD.h> /* Library from Adafruit.com */
Metro pauseLed = Metro(1000);// milliseconds
unsigned long time; //used for time stamp
boolean toggleP = LOW;
FlexCAN CANbus(500000);
static CAN_message_t msg,rxmsg;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
#define SCK_PIN 13 //Clock pin
#define MISO_PIN 12 //Mater in Slave output
#define MOSI_PIN 11 //Master out Slave input
#define SD_PIN 10 //pin for SD card control
//#define led 13
// -------------------------------------------------------------
void setup(void)
{
Serial.begin(57600);
Serial1.begin(57600);
CANbus.begin();
//pinMode(led, OUTPUT);
pinMode(10, OUTPUT);
//digitalWrite(led, 1);
delay(10000);
Serial.println(F("Hello Teensy 3.1 CAN Test."));
Serial1.println(F("Serial1 - Hello Teensy 3.1 CAN Test."));
//************************* SD CARD SETUP *******************
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!card.init(SPI_FULL_SPEED,SD_PIN)) {
Serial1.println("ERROR: card.init failed");
}
// initialize a FAT volume
if (!volume.init(&card)) {
Serial.println("ERROR: volume.init failed");
}
// open the root directory
if (!root.openRoot(&volume)) {
Serial.println("ERROR: openRoot failed");
}
}
// -------------------------------------------------------------
void loop()
{
if (!CANbus.available()) {
Serial1.println("Can bus not available. Check connections");
}
while(!CANbus.available()) {
if (pauseLed.check() ==1){
//digitalWrite(led, toggleP);
toggleP = !toggleP;
Serial.println("CAN not available");
Serial1.println("Serial1 - CAN not available");
}
} //waiting for Canbus to be connected
Serial1.println("Can Bus Connected and available"); //only comes out of the above loop if CAN is available
// create a new file
char name[] = "CANlog00.CSV";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) {
Serial.println("file.created");
}
Serial1.print("Writing to: ");
Serial1.println(name);
file.println("seconds,milliSec, ID, B0, B1, B2, B3, B4, B5, B6, B7"); //, B0(DEC), B1(DEC), B2(DEC), B3(DEC), B4(DEC), B5(DEC), B6(DEC), B7(DEC)");
//file.println(buffer1);
//file.println();
Serial1.println("Header written to SD Card");
CAN_Capture();
}//end loop
void CAN_Capture(){
Serial1.println("CAN_Capture loop");
while(1){ //stay within this loop unless forced out
//if (CANbus.available()) { //is the CAN available? assume it is or we wouldn't have got this far.
if (CANbus.read(rxmsg)){ //while messages are available perform the following
String CANStr("");
time = millis(); //capture time when message was recieved
CANStr +=String(int(time/1000));
CANStr += (",");
CANStr +=String(time);
CANStr += (",");
CANStr += String(rxmsg.id,HEX);
for (int i=0; i < 8; i++) {
CANStr += (",") ;
CANStr += String(rxmsg.buf[i],HEX);
}
file.println(CANStr); //print the CAN message to the file that should already be open
}
if (rxmsg.id == 0x100){
if (rxmsg.buf[2] == 0x8){ //look for an OFF on the start switch and then stop the logging
file.close();
//Serial1.print(name);
Serial1.println("file closed");
delay(2000);
while(1){ //loop until you see the bike turned ON again, then force out of this loop
//Serial1.println("Waiting for bike to be turned on again");
if (CANbus.read(rxmsg)){
if (rxmsg.id == 0x100 and rxmsg.buf[2] ==0x28){ //look for an ON on the start switch and start logging if it isn't already
Serial1.print("id: ");
Serial1.print(rxmsg.id);
Serial1.print(", byte2: ");
Serial1.print(rxmsg.buf[2]);
Serial1.println(" detected, about to restart caputure");
return;
}
}
}//end while after capture finishes
}
}
//}
}
}