add samd compatibility

This commit is contained in:
zhengxing Lu
2019-08-08 13:48:23 +08:00
committed by Baozhu Zuo
parent ad54af425f
commit 81a975210b
14 changed files with 210 additions and 113 deletions
+22 -15
View File
@@ -17,6 +17,13 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -55,21 +62,21 @@ void set_mask_filt()
void sendPid(unsigned char __pid)
{
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0};
Serial.print("SEND PID: 0x");
Serial.println(__pid, HEX);
SERIAL.print("SEND PID: 0x");
SERIAL.println(__pid, HEX);
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp);
}
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
set_mask_filt();
}
@@ -96,24 +103,24 @@ void taskCanRecv()
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println("\r\n------------------------------------------------------------------");
Serial.print("Get Data From id: 0x");
Serial.println(CAN.getCanId(), HEX);
SERIAL.println("\r\n------------------------------------------------------------------");
SERIAL.print("Get Data From id: 0x");
SERIAL.println(CAN.getCanId(), HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print("0x");
Serial.print(buf[i], HEX);
Serial.print("\t");
SERIAL.print("0x");
SERIAL.print(buf[i], HEX);
SERIAL.print("\t");
}
Serial.println();
SERIAL.println();
}
}
void taskDbg()
{
while(Serial.available())
while(SERIAL.available())
{
char c = Serial.read();
char c = SERIAL.read();
if(c>='0' && c<='9')
{
+14 -7
View File
@@ -3,6 +3,13 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
#define SPI_CS_PIN 10
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
@@ -10,29 +17,29 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN init failed, retry");
SERIAL.println("CAN init failed, retry");
delay(100);
}
Serial.println("CAN init ok");
SERIAL.println("CAN init ok");
if(CAN.mcpPinMode(MCP_TX2RTS, MCP_PIN_IN))
{
Serial.println("TX2RTS is now an input");
SERIAL.println("TX2RTS is now an input");
}
else
{
Serial.println("Could not switch TX2RTS");
SERIAL.println("Could not switch TX2RTS");
}
}
void loop()
{
Serial.print("TX2RTS is currently ");
Serial.println(CAN.mcpDigitalRead(MCP_TX2RTS));
SERIAL.print("TX2RTS is currently ");
SERIAL.println(CAN.mcpDigitalRead(MCP_TX2RTS));
delay(500);
}
+16 -9
View File
@@ -3,6 +3,13 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
#define SPI_CS_PIN 10
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
@@ -10,41 +17,41 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN init failed, retry");
SERIAL.println("CAN init failed, retry");
delay(100);
}
Serial.println("CAN init ok");
SERIAL.println("CAN init ok");
if(CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT))
{
Serial.println("RX0BF is now an output");
SERIAL.println("RX0BF is now an output");
}
else
{
Serial.println("Could not switch RX0BF");
SERIAL.println("Could not switch RX0BF");
}
if(CAN.mcpPinMode(MCP_RX1BF, MCP_PIN_OUT))
{
Serial.println("RX1BF is now an output");
SERIAL.println("RX1BF is now an output");
}
else
{
Serial.println("Could not switch RX1BF");
SERIAL.println("Could not switch RX1BF");
}
}
void loop()
{
Serial.println("10");
SERIAL.println("10");
CAN.mcpDigitalWrite(MCP_RX0BF, HIGH);
CAN.mcpDigitalWrite(MCP_RX1BF, LOW);
delay(500);
Serial.println("01");
SERIAL.println("01");
CAN.mcpDigitalWrite(MCP_RX0BF, LOW);
CAN.mcpDigitalWrite(MCP_RX1BF, HIGH);
delay(500);
+17 -10
View File
@@ -5,6 +5,13 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -15,16 +22,16 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
pinMode(LED,OUTPUT);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println("Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
}
@@ -39,14 +46,14 @@ void loop()
unsigned long canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: 0x");
Serial.println(canId, HEX);
SERIAL.println("-----------------------------");
SERIAL.println("get data from ID: 0x");
SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
SERIAL.print(buf[i]);
SERIAL.print("\t");
if(ledON && i==0)
{
@@ -61,7 +68,7 @@ void loop()
ledON = 1;
}
}
Serial.println();
SERIAL.println();
}
}
+16 -10
View File
@@ -6,6 +6,12 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
@@ -15,15 +21,15 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
}
@@ -38,16 +44,16 @@ void loop()
unsigned long canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.print("Get data from ID: 0x");
Serial.println(canId, HEX);
SERIAL.println("-----------------------------");
SERIAL.print("Get data from ID: 0x");
SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i], HEX);
Serial.print("\t");
SERIAL.print(buf[i], HEX);
SERIAL.print("\t");
}
Serial.println();
SERIAL.println();
}
}
@@ -5,6 +5,13 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -19,15 +26,15 @@ char str[20];
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
}
@@ -56,9 +63,9 @@ void loop()
// print the data
for(int i = 0; i<len; i++)
{
Serial.print(buf[i]);Serial.print("\t");
SERIAL.print(buf[i]);SERIAL.print("\t");
}
Serial.println();
SERIAL.println();
}
}
}
+17 -10
View File
@@ -18,6 +18,13 @@
#include "mcp_can.h"
#include <avr/sleep.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -46,15 +53,15 @@ char str[20];
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
// attach interrupt
pinMode(CAN_INT, INPUT);
@@ -105,15 +112,15 @@ void loop()
// print the data
for(int i = 0; i<len; i++)
{
Serial.print(buf[i]);Serial.print("\t");
SERIAL.print(buf[i]);SERIAL.print("\t");
}
Serial.println();
SERIAL.println();
}
}
} else if(millis() > lastBusActivity + KEEP_AWAKE_TIME)
{
// Put MCP2515 into sleep mode
Serial.println(F("CAN sleep"));
SERIAL.println(F("CAN sleep"));
CAN.sleep();
// Put the transceiver into standby (by pulling Rs high):
@@ -123,10 +130,10 @@ void loop()
digitalWrite(RS_OUTPUT, HIGH);
// Put the MCU to sleep
Serial.println(F("MCU sleep"));
SERIAL.println(F("MCU sleep"));
// Clear serial buffers before sleeping
Serial.flush();
SERIAL.flush();
cli(); // Disable interrupts
if(!flagRecv) // Make sure we havn't missed an interrupt between the check above and now. If an interrupt happens between now and sei()/sleep_cpu() then sleep_cpu() will immediately wake up again
@@ -149,7 +156,7 @@ void loop()
else
digitalWrite(RS_OUTPUT, LOW);
Serial.println(F("Woke up"));
SERIAL.println(F("Woke up"));
}
}
+18 -11
View File
@@ -7,6 +7,13 @@
#include "mcp_can.h"
#include <SD.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
File myFile;
// the cs pin of the version after v1.1 is default to D9
@@ -23,23 +30,23 @@ char str[20];
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println("Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
if (!SD.begin(4)) {
Serial.println("SD initialization failed!");
SERIAL.println("SD initialization failed!");
while(1);
}
Serial.println("SD initialization done.");
SERIAL.println("SD initialization done.");
}
void MCP2515_ISR()
@@ -66,20 +73,20 @@ void loop()
// read data, len: data length, buf: data buf
CAN.readMsgBufID(&id, &len, buf);
Serial.print(id);
Serial.print(",");
SERIAL.print(id);
SERIAL.print(",");
myFile.print(id);
myFile.print(",");
for(int i = 0; i<len; i++)
{
Serial.print(buf[i]);
Serial.print(",");
SERIAL.print(buf[i]);
SERIAL.print(",");
myFile.print(buf[i]);
myFile.print(",");
}
Serial.println();
SERIAL.println();
myFile.println();
}
+11 -4
View File
@@ -4,6 +4,13 @@
#include <mcp_can.h>
#include <SPI.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -12,15 +19,15 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
}
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+12 -5
View File
@@ -2,6 +2,13 @@
#include <mcp_can.h>
#include <SPI.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -12,21 +19,21 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
}
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
void loop()
{ Serial.println("In loop");
{ SERIAL.println("In loop");
// send data: id = 0x70, standard frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x70, 0, 8, stmp);
delay(1000); // send data once per second
+11 -4
View File
@@ -16,6 +16,13 @@
#include <ros.h>
#include <std_msgs/Empty.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
ros::NodeHandle nh;
const int SPI_CS_PIN = 9;
@@ -41,17 +48,17 @@ ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
nh.initNode();
nh.subscribe(sub);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
}
+14 -7
View File
@@ -8,6 +8,13 @@
#include <avr/sleep.h>
#include <avr/wdt.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -57,15 +64,15 @@ void sleepMCU()
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
CAN.setSleepWakeup(0); // the MCP2515 will NOT wake up on incoming messages,
// making it a 'send only' node
@@ -85,7 +92,7 @@ unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop()
{
Serial.println("Sending message");
SERIAL.println("Sending message");
CAN.sendMsgBuf(0x00, 0, 0, NULL); // Send empty wakeup message
@@ -110,8 +117,8 @@ void loop()
// sleep
Serial.println("Sleep");
Serial.flush();
SERIAL.println("Sleep");
SERIAL.flush();
// Put MCP2515 into sleep mode
CAN.sleep();
@@ -6,6 +6,13 @@
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -20,15 +27,15 @@ char str[20];
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
@@ -66,16 +73,16 @@ void loop()
flagRecv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println("\r\n------------------------------------------------------------------");
Serial.print("Get Data From id: ");
Serial.println(CAN.getCanId());
SERIAL.println("\r\n------------------------------------------------------------------");
SERIAL.print("Get Data From id: ");
SERIAL.println(CAN.getCanId());
for(int i = 0; i<len; i++) // print the data
{
Serial.print("0x");
Serial.print(buf[i], HEX);
Serial.print("\t");
SERIAL.print("0x");
SERIAL.print(buf[i], HEX);
SERIAL.print("\t");
}
Serial.println();
SERIAL.println();
}
}
@@ -4,6 +4,13 @@
#include <mcp_can.h>
#include <SPI.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
@@ -12,15 +19,15 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
SERIAL.println("CAN BUS Shield init ok!");
}
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};