Pretty printed the Arduino code with astyle

This commit is contained in:
Baozhu Zuo
2020-02-03 15:00:22 +08:00
parent 7d41ceb28a
commit 7d30ccccc8
17 changed files with 1268 additions and 1407 deletions
+41 -59
View File
@@ -1,27 +1,27 @@
/************************************************************************************************* /*************************************************************************************************
OBD-II_PIDs TEST CODE OBD-II_PIDs TEST CODE
LOOVEE @ JUN24, 2017 LOOVEE @ JUN24, 2017
Query Query
send id: 0x7df send id: 0x7df
dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0
Response Response
From id: 0x7E9 or 0x7EA or 0x7EB From id: 0x7E9 or 0x7EA or 0x7EB
dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option)
https://en.wikipedia.org/wiki/OBD-II_PIDs https://en.wikipedia.org/wiki/OBD-II_PIDs
Input a PID, then you will get reponse from vehicle, the input should be end with '\n' Input a PID, then you will get reponse from vehicle, the input should be end with '\n'
***************************************************************************************************/ ***************************************************************************************************/
#include <SPI.h> #include <SPI.h>
#include "mcp_can.h" #include "mcp_can.h"
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -39,39 +39,35 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
unsigned char PID_INPUT; unsigned char PID_INPUT;
unsigned char getPid = 0; unsigned char getPid = 0;
void set_mask_filt() void set_mask_filt() {
{
/* /*
* set mask, set both the mask to 0x3ff set mask, set both the mask to 0x3ff
*/ */
CAN.init_Mask(0, 0, 0x7FC); CAN.init_Mask(0, 0, 0x7FC);
CAN.init_Mask(1, 0, 0x7FC); CAN.init_Mask(1, 0, 0x7FC);
/* /*
* set filter, we can receive id from 0x04 ~ 0x09 set filter, we can receive id from 0x04 ~ 0x09
*/ */
CAN.init_Filt(0, 0, 0x7E8); CAN.init_Filt(0, 0, 0x7E8);
CAN.init_Filt(1, 0, 0x7E8); CAN.init_Filt(1, 0, 0x7E8);
CAN.init_Filt(2, 0, 0x7E8); CAN.init_Filt(2, 0, 0x7E8);
CAN.init_Filt(3, 0, 0x7E8); CAN.init_Filt(3, 0, 0x7E8);
CAN.init_Filt(4, 0, 0x7E8); CAN.init_Filt(4, 0, 0x7E8);
CAN.init_Filt(5, 0, 0x7E8); CAN.init_Filt(5, 0, 0x7E8);
} }
void sendPid(unsigned char __pid) void sendPid(unsigned char __pid) {
{
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0};
SERIAL.print("SEND PID: 0x"); SERIAL.print("SEND PID: 0x");
SERIAL.println(__pid, HEX); SERIAL.println(__pid, HEX);
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp);
} }
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -81,33 +77,28 @@ void setup()
} }
void loop() void loop() {
{
taskCanRecv(); taskCanRecv();
taskDbg(); taskDbg();
if(getPid) // GET A PID if (getPid) { // GET A PID
{
getPid = 0; getPid = 0;
sendPid(PID_INPUT); sendPid(PID_INPUT);
PID_INPUT = 0; PID_INPUT = 0;
} }
} }
void taskCanRecv() void taskCanRecv() {
{
unsigned char len = 0; unsigned char len = 0;
unsigned char buf[8]; unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
SERIAL.println("\r\n------------------------------------------------------------------"); SERIAL.println("\r\n------------------------------------------------------------------");
SERIAL.print("Get Data From id: 0x"); SERIAL.print("Get Data From id: 0x");
SERIAL.println(CAN.getCanId(), HEX); SERIAL.println(CAN.getCanId(), HEX);
for(int i = 0; i<len; i++) // print the data for (int i = 0; i < len; i++) { // print the data
{
SERIAL.print("0x"); SERIAL.print("0x");
SERIAL.print(buf[i], HEX); SERIAL.print(buf[i], HEX);
SERIAL.print("\t"); SERIAL.print("\t");
@@ -116,30 +107,21 @@ void taskCanRecv()
} }
} }
void taskDbg() void taskDbg() {
{ while (SERIAL.available()) {
while(SERIAL.available())
{
char c = SERIAL.read(); char c = SERIAL.read();
if(c>='0' && c<='9') if (c >= '0' && c <= '9') {
{
PID_INPUT *= 0x10; PID_INPUT *= 0x10;
PID_INPUT += c-'0'; PID_INPUT += c - '0';
} } else if (c >= 'A' && c <= 'F') {
else if(c>='A' && c<='F')
{
PID_INPUT *= 0x10; PID_INPUT *= 0x10;
PID_INPUT += 10+c-'A'; PID_INPUT += 10 + c - 'A';
} } else if (c >= 'a' && c <= 'f') {
else if(c>='a' && c<='f')
{
PID_INPUT *= 0x10; PID_INPUT *= 0x10;
PID_INPUT += 10+c-'a'; PID_INPUT += 10 + c - 'a';
} } else if (c == '\n') { // END
else if(c == '\n') // END
{
getPid = 1; getPid = 1;
} }
} }
+8 -14
View File
@@ -5,9 +5,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
#define SPI_CS_PIN 10 #define SPI_CS_PIN 10
@@ -15,34 +15,28 @@
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k 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); delay(100);
} }
SERIAL.println("CAN init ok"); SERIAL.println("CAN init ok");
if(CAN.mcpPinMode(MCP_TX2RTS, MCP_PIN_IN)) if (CAN.mcpPinMode(MCP_TX2RTS, MCP_PIN_IN)) {
{
SERIAL.println("TX2RTS is now an input"); SERIAL.println("TX2RTS is now an input");
} } else {
else
{
SERIAL.println("Could not switch TX2RTS"); SERIAL.println("Could not switch TX2RTS");
} }
} }
void loop() void loop() {
{
SERIAL.print("TX2RTS is currently "); SERIAL.print("TX2RTS is currently ");
SERIAL.println(CAN.mcpDigitalRead(MCP_TX2RTS)); SERIAL.println(CAN.mcpDigitalRead(MCP_TX2RTS));
delay(500); delay(500);
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+10 -19
View File
@@ -5,9 +5,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
#define SPI_CS_PIN 10 #define SPI_CS_PIN 10
@@ -15,38 +15,29 @@
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k 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); delay(100);
} }
SERIAL.println("CAN init ok"); SERIAL.println("CAN init ok");
if(CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT)) if (CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT)) {
{
SERIAL.println("RX0BF is now an output"); SERIAL.println("RX0BF is now an output");
} } else {
else
{
SERIAL.println("Could not switch RX0BF"); SERIAL.println("Could not switch RX0BF");
} }
if(CAN.mcpPinMode(MCP_RX1BF, MCP_PIN_OUT)) if (CAN.mcpPinMode(MCP_RX1BF, MCP_PIN_OUT)) {
{
SERIAL.println("RX1BF is now an output"); SERIAL.println("RX1BF is now an output");
} } else {
else
{
SERIAL.println("Could not switch RX1BF"); SERIAL.println("Could not switch RX1BF");
} }
} }
void loop() void loop() {
{
SERIAL.println("10"); SERIAL.println("10");
CAN.mcpDigitalWrite(MCP_RX0BF, HIGH); CAN.mcpDigitalWrite(MCP_RX0BF, HIGH);
CAN.mcpDigitalWrite(MCP_RX1BF, LOW); CAN.mcpDigitalWrite(MCP_RX1BF, LOW);
@@ -58,5 +49,5 @@ void loop()
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+10 -18
View File
@@ -7,9 +7,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -20,13 +20,11 @@ boolean ledON = 1;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
pinMode(LED,OUTPUT); pinMode(LED, OUTPUT);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println("Init CAN BUS Shield again"); SERIAL.println("Init CAN BUS Shield again");
delay(100); delay(100);
@@ -35,13 +33,11 @@ void setup()
} }
void loop() void loop() {
{
unsigned char len = 0; unsigned char len = 0;
unsigned char buf[8]; unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId(); unsigned long canId = CAN.getCanId();
@@ -50,19 +46,15 @@ void loop()
SERIAL.println("get data from ID: 0x"); SERIAL.println("get data from ID: 0x");
SERIAL.println(canId, HEX); SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data for (int i = 0; i < len; i++) { // print the data
{
SERIAL.print(buf[i]); SERIAL.print(buf[i]);
SERIAL.print("\t"); SERIAL.print("\t");
if(ledON && i==0) if (ledON && i == 0) {
{
digitalWrite(LED, buf[i]); digitalWrite(LED, buf[i]);
ledON = 0; ledON = 0;
delay(500); delay(500);
} } else if ((!(ledON)) && i == 4) {
else if((!(ledON)) && i==4)
{
digitalWrite(LED, buf[i]); digitalWrite(LED, buf[i]);
ledON = 1; ledON = 1;
+9 -14
View File
@@ -8,9 +8,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -19,12 +19,10 @@ const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -33,23 +31,20 @@ void setup()
} }
void loop() void loop() {
{
unsigned char len = 0; unsigned char len = 0;
unsigned char buf[8]; unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId(); unsigned long canId = CAN.getCanId();
SERIAL.println("-----------------------------"); SERIAL.println("-----------------------------");
SERIAL.print("Get data from ID: 0x"); SERIAL.print("Get data from ID: 0x");
SERIAL.println(canId, HEX); SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data for (int i = 0; i < len; i++) { // print the data
{
SERIAL.print(buf[i], HEX); SERIAL.print(buf[i], HEX);
SERIAL.print("\t"); SERIAL.print("\t");
} }
@@ -58,5 +53,5 @@ void loop()
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
@@ -7,9 +7,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -25,14 +25,12 @@ unsigned char len = 0;
unsigned char buf[8]; unsigned char buf[8];
char str[20]; char str[20];
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (!SERIAL) { while (!SERIAL) {
; // wait for serial port to connect. Needed for native USB port only ; // wait for serial port to connect. Needed for native USB port only
} }
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -42,15 +40,13 @@ void setup()
attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt
} }
void MCP2515_ISR() void MCP2515_ISR() {
{
flagRecv = 1; flagRecv = 1;
} }
void loop() void loop() {
{ if (flagRecv) {
if(flagRecv) // check if get data
{ // check if get data
flagRecv = 0; // clear flag flagRecv = 0; // clear flag
@@ -58,15 +54,13 @@ void loop()
// If either the bus is saturated or the MCU is busy, // If either the bus is saturated or the MCU is busy,
// both RX buffers may be in use and reading a single // both RX buffers may be in use and reading a single
// message does not clear the IRQ conditon. // message does not clear the IRQ conditon.
while (CAN_MSGAVAIL == CAN.checkReceive()) while (CAN_MSGAVAIL == CAN.checkReceive()) {
{
// read data, len: data length, buf: data buf // read data, len: data length, buf: data buf
CAN.readMsgBuf(&len, buf); CAN.readMsgBuf(&len, buf);
// print the data // print the data
for(int i = 0; i<len; i++) 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();
} }
@@ -74,5 +68,5 @@ void loop()
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+70 -77
View File
@@ -1,18 +1,18 @@
// demo: CAN Sleep Example - receive // demo: CAN Sleep Example - receive
// by Kai, based on the receive_interrupt example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files) // by Kai, based on the receive_interrupt example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files)
// //
// By setting the MCU, the CAN controller (MCP2515) and the transceiver (MCP2551) into sleep mode, you can reduce // By setting the MCU, the CAN controller (MCP2515) and the transceiver (MCP2551) into sleep mode, you can reduce
// the power consumption of the whole setup from around 50mA down to 240uA (Arduino directly connected to 5V, regulator and // the power consumption of the whole setup from around 50mA down to 240uA (Arduino directly connected to 5V, regulator and
// power LED removed). The node will wake up when a new message arrives, process the message and go back to sleep // power LED removed). The node will wake up when a new message arrives, process the message and go back to sleep
// afterwards. // afterwards.
// //
// Known issues: // Known issues:
// - Because it takes some time for the controller to wake up, the first message is usually lost. Look at the // - Because it takes some time for the controller to wake up, the first message is usually lost. Look at the
// send_sleep example on how to avoid this by sending a special wakeup message before the normal message. // send_sleep example on how to avoid this by sending a special wakeup message before the normal message.
// - If you only have 2 devices on the CAN bus (the device running this sketch and some other device sending // - If you only have 2 devices on the CAN bus (the device running this sketch and some other device sending
// messages), you may find that duplicate messages are received when waking up. This is because when // messages), you may find that duplicate messages are received when waking up. This is because when
// the MCP2515 wakes up it enters LISTENONLY mode where it does not send ACKs to messages, so the transmitter // the MCP2515 wakes up it enters LISTENONLY mode where it does not send ACKs to messages, so the transmitter
// will retransmit the same message a few times. See below for a simple solution to filter duplicate messages out. // will retransmit the same message a few times. See below for a simple solution to filter duplicate messages out.
#include <SPI.h> #include <SPI.h>
#include "mcp_can.h" #include "mcp_can.h"
@@ -20,9 +20,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -30,13 +30,13 @@
const int SPI_CS_PIN = 9; const int SPI_CS_PIN = 9;
#define CAN_INT 2 // Set INT to pin 2 #define CAN_INT 2 // Set INT to pin 2
// To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or // To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or
// any free Arduino output. // any free Arduino output.
#define RS_TO_MCP2515 true // Set this to false if Rs is connected to your Arduino #define RS_TO_MCP2515 true // Set this to false if Rs is connected to your Arduino
#define RS_OUTPUT MCP_RX0BF // RX0BF is a pin of the MCP2515. You can also define an Arduino pin here #define RS_OUTPUT MCP_RX0BF // RX0BF is a pin of the MCP2515. You can also define an Arduino pin here
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
#define KEEP_AWAKE_TIME 200 // time the controller will stay awake after the last activity on the bus (in ms) #define KEEP_AWAKE_TIME 200 // time the controller will stay awake after the last activity on the bus (in ms)
unsigned long lastBusActivity = millis(); unsigned long lastBusActivity = millis();
@@ -51,12 +51,10 @@ unsigned long lastMsgTime = 0;
char str[20]; char str[20];
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -70,25 +68,22 @@ void setup()
CAN.setSleepWakeup(1); // this tells the MCP2515 to wake up on incoming messages CAN.setSleepWakeup(1); // this tells the MCP2515 to wake up on incoming messages
// Pull the Rs pin of the MCP2551 transceiver low to enable it: // Pull the Rs pin of the MCP2551 transceiver low to enable it:
if(RS_TO_MCP2515) if (RS_TO_MCP2515) {
{ CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT);
CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT); CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
} else { } else {
pinMode(RS_OUTPUT, OUTPUT); pinMode(RS_OUTPUT, OUTPUT);
digitalWrite(RS_OUTPUT, LOW); digitalWrite(RS_OUTPUT, LOW);
} }
} }
void MCP2515_ISR() void MCP2515_ISR() {
{
flagRecv = 1; flagRecv = 1;
} }
void loop() void loop() {
{ if (flagRecv) {
if(flagRecv) // check if get data
{ // check if get data
flagRecv = 0; // clear flag flagRecv = 0; // clear flag
lastBusActivity = millis(); lastBusActivity = millis();
@@ -97,69 +92,67 @@ void loop()
// If either the bus is saturated or the MCU is busy, // If either the bus is saturated or the MCU is busy,
// both RX buffers may be in use and reading a single // both RX buffers may be in use and reading a single
// message does not clear the IRQ conditon. // message does not clear the IRQ conditon.
while (CAN_MSGAVAIL == CAN.checkReceive()) while (CAN_MSGAVAIL == CAN.checkReceive()) {
{
// read data, len: data length, buf: data buf // read data, len: data length, buf: data buf
CAN.readMsgBuf(&len, buf); CAN.readMsgBuf(&len, buf);
// check if this is a duplicate message (including a timeout, so that the same message is accepted again after a while) // check if this is a duplicate message (including a timeout, so that the same message is accepted again after a while)
if((len != lastLen) || (millis() > lastMsgTime + DUPLICATE_TIMEOUT) || (memcmp((const void *)lastBuf, (const void *)buf, sizeof(buf)) != 0)) if ((len != lastLen) || (millis() > lastMsgTime + DUPLICATE_TIMEOUT)
{ || (memcmp((const void*)lastBuf, (const void*)buf, sizeof(buf)) != 0)) {
lastLen = len; lastLen = len;
memcpy(lastBuf, buf, sizeof(buf)); memcpy(lastBuf, buf, sizeof(buf));
lastMsgTime = millis(); lastMsgTime = millis();
// print the data // print the data
for(int i = 0; i<len; i++) 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) } else if (millis() > lastBusActivity + KEEP_AWAKE_TIME) {
{ // Put MCP2515 into sleep mode
// Put MCP2515 into sleep mode SERIAL.println(F("CAN sleep"));
SERIAL.println(F("CAN sleep")); CAN.sleep();
CAN.sleep();
// Put the transceiver into standby (by pulling Rs high):
if(RS_TO_MCP2515)
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
else
digitalWrite(RS_OUTPUT, HIGH);
// Put the MCU to sleep
SERIAL.println(F("MCU sleep"));
// Clear serial buffers before sleeping // Put the transceiver into standby (by pulling Rs high):
SERIAL.flush(); if (RS_TO_MCP2515) {
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
} else {
digitalWrite(RS_OUTPUT, HIGH);
}
cli(); // Disable interrupts // Put the MCU to sleep
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 SERIAL.println(F("MCU sleep"));
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Clear serial buffers before sleeping
sleep_enable(); SERIAL.flush();
sleep_bod_disable();
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
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
// Now the Arduino sleeps until the next message arrives...
sleep_disable();
}
sei(); sei();
sleep_cpu();
// Now the Arduino sleeps until the next message arrives...
sleep_disable();
}
sei();
CAN.wake(); // When the MCP2515 wakes up it will be in LISTENONLY mode, here we put it into the mode it was before sleeping CAN.wake(); // When the MCP2515 wakes up it will be in LISTENONLY mode, here we put it into the mode it was before sleeping
// Wake up the transceiver: // Wake up the transceiver:
if(RS_TO_MCP2515) if (RS_TO_MCP2515) {
CAN.mcpDigitalWrite(RS_OUTPUT, LOW); CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
else } else {
digitalWrite(RS_OUTPUT, LOW); digitalWrite(RS_OUTPUT, LOW);
}
SERIAL.println(F("Woke up")); SERIAL.println(F("Woke up"));
} }
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+18 -24
View File
@@ -9,9 +9,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
File myFile; File myFile;
@@ -28,12 +28,10 @@ unsigned char len = 0;
unsigned char buf[8]; unsigned char buf[8];
char str[20]; char str[20];
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println("Init CAN BUS Shield again"); SERIAL.println("Init CAN BUS Shield again");
delay(100); delay(100);
@@ -41,55 +39,51 @@ void setup()
SERIAL.println("CAN BUS Shield init ok!"); SERIAL.println("CAN BUS Shield init ok!");
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
if (!SD.begin(4)) { if (!SD.begin(4)) {
SERIAL.println("SD initialization failed!"); SERIAL.println("SD initialization failed!");
while(1); while (1);
} }
SERIAL.println("SD initialization done."); SERIAL.println("SD initialization done.");
} }
void MCP2515_ISR() void MCP2515_ISR() {
{
flagRecv = 1; flagRecv = 1;
} }
void loop() void loop() {
{ if (flagRecv) {
if(flagRecv) // check if get data
{ // check if get data
flagRecv = 0; // clear flag flagRecv = 0; // clear flag
unsigned long id= 0; unsigned long id = 0;
myFile = SD.open("can.csv", FILE_WRITE); myFile = SD.open("can.csv", FILE_WRITE);
// iterate over all pending messages // iterate over all pending messages
// If either the bus is saturated or the MCU is busy, // If either the bus is saturated or the MCU is busy,
// both RX buffers may be in use and reading a single // both RX buffers may be in use and reading a single
// message does not clear the IRQ conditon. // message does not clear the IRQ conditon.
while (CAN_MSGAVAIL == CAN.checkReceive()) while (CAN_MSGAVAIL == CAN.checkReceive()) {
{
// read data, len: data length, buf: data buf // read data, len: data length, buf: data buf
CAN.readMsgBufID(&id, &len, buf); CAN.readMsgBufID(&id, &len, buf);
SERIAL.print(id); SERIAL.print(id);
SERIAL.print(","); SERIAL.print(",");
myFile.print(id); myFile.print(id);
myFile.print(","); myFile.print(",");
for(int i = 0; i<len; i++) for (int i = 0; i < len; i++) {
{
SERIAL.print(buf[i]); SERIAL.print(buf[i]);
SERIAL.print(","); SERIAL.print(",");
myFile.print(buf[i]); myFile.print(buf[i]);
myFile.print(","); myFile.print(",");
} }
SERIAL.println(); SERIAL.println();
myFile.println(); myFile.println();
} }
myFile.close(); myFile.close();
} }
} }
+10 -15
View File
@@ -6,9 +6,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -17,12 +17,10 @@ const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -31,22 +29,19 @@ void setup()
} }
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0}; unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop() void loop() {
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf // send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
stmp[7] = stmp[7]+1; stmp[7] = stmp[7] + 1;
if(stmp[7] == 100) if (stmp[7] == 100) {
{
stmp[7] = 0; stmp[7] = 0;
stmp[6] = stmp[6] + 1; stmp[6] = stmp[6] + 1;
if(stmp[6] == 100) if (stmp[6] == 100) {
{
stmp[6] = 0; stmp[6] = 0;
stmp[5] = stmp[6] + 1; stmp[5] = stmp[6] + 1;
} }
} }
CAN.sendMsgBuf(0x00, 0, 8, stmp); CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(100); // send data per 100ms delay(100); // send data per 100ms
} }
+7 -9
View File
@@ -4,9 +4,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -17,12 +17,10 @@ const int ledLOW = 0;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -32,13 +30,13 @@ void setup()
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7}; unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
void loop() void loop() {
{ SERIAL.println("In loop"); SERIAL.println("In loop");
// send data: id = 0x70, standard frame, data len = 8, stmp: data buf // send data: id = 0x70, standard frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x70, 0, 8, stmp); CAN.sendMsgBuf(0x70, 0, 8, stmp);
delay(1000); // send data once per second delay(1000); // send data once per second
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+15 -18
View File
@@ -18,42 +18,40 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
ros::NodeHandle nh; ros::NodeHandle nh;
const int SPI_CS_PIN = 9; const int SPI_CS_PIN = 9;
const int ledHIGH=1; const int ledHIGH = 1;
const int ledLOW=0; const int ledLOW = 0;
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7}; unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void messageCb( const std_msgs::Empty& toggle_msg){ void messageCb(const std_msgs::Empty& toggle_msg) {
//digitalWrite(13, HIGH-digitalRead(13)); // blink the led //digitalWrite(13, HIGH-digitalRead(13)); // blink the led
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf // send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x70,0, 8, stmp); CAN.sendMsgBuf(0x70, 0, 8, stmp);
delay(1000); // send data per 100ms delay(1000); // send data per 100ms
} }
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb ); ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb);
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10 // v0.9b and v1.0 is default D10
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
nh.initNode(); nh.initNode();
nh.subscribe(sub); nh.subscribe(sub);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -62,13 +60,12 @@ void setup()
} }
void loop() void loop() {
{
nh.spinOnce(); nh.spinOnce();
delay(1); delay(1);
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+67 -72
View File
@@ -1,6 +1,6 @@
// demo: CAN Sleep Example - send // demo: CAN Sleep Example - send
// by Kai, based on the send example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files) // by Kai, based on the send example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files)
// //
// See receive_sleep example for additional notes. // See receive_sleep example for additional notes.
#include <mcp_can.h> #include <mcp_can.h>
@@ -10,64 +10,61 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10 // v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9; const int SPI_CS_PIN = 9;
// To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or // To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or
// any free Arduino output. // any free Arduino output.
#define RS_TO_MCP2515 true // Set this to false if Rs is connected to your Arduino #define RS_TO_MCP2515 true // Set this to false if Rs is connected to your Arduino
#define RS_OUTPUT MCP_RX0BF // RX0BF is a pin of the MCP2515. You can also define an Arduino pin here #define RS_OUTPUT MCP_RX0BF // RX0BF is a pin of the MCP2515. You can also define an Arduino pin here
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
// Watchdog interrupt, used to wake the MCU periodically. // Watchdog interrupt, used to wake the MCU periodically.
ISR (WDT_vect) ISR(WDT_vect) {
{ wdt_disable(); // disable watchdog
wdt_disable(); // disable watchdog
}
void sleepMCU()
// Sleep the MCU for one second.
// See http://www.gammon.com.au/power for details.
// (You can make your life a lot easyer by using one of the numeral low power libraries for Arduino.)
{
// disable ADC
ADCSRA = 0;
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and an interval
WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1); // set WDIE, and 1 second delay
wdt_reset(); // pat the dog
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
noInterrupts (); // timed sequence follows
sleep_enable();
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
interrupts (); // guarantees next instruction executed
sleep_cpu ();
// cancel sleep as a precaution
sleep_disable();
} }
void setup() void sleepMCU()
// Sleep the MCU for one second.
// See http://www.gammon.com.au/power for details.
// (You can make your life a lot easyer by using one of the numeral low power libraries for Arduino.)
{ {
// disable ADC
ADCSRA = 0;
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit(WDCE) | bit(WDE);
// set interrupt mode and an interval
WDTCSR = bit(WDIE) | bit(WDP2) | bit(WDP1); // set WDIE, and 1 second delay
wdt_reset(); // pat the dog
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
noInterrupts(); // timed sequence follows
sleep_enable();
// turn off brown-out enable in software
MCUCR = bit(BODS) | bit(BODSE);
MCUCR = bit(BODS);
interrupts(); // guarantees next instruction executed
sleep_cpu();
// cancel sleep as a precaution
sleep_disable();
}
void setup() {
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -75,69 +72,67 @@ void setup()
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, CAN.setSleepWakeup(0); // the MCP2515 will NOT wake up on incoming messages,
// making it a 'send only' node // making it a 'send only' node
// Pull the Rs pin of the MCP2551 transceiver low to enable it: // Pull the Rs pin of the MCP2551 transceiver low to enable it:
if(RS_TO_MCP2515) if (RS_TO_MCP2515) {
{ CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT);
CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT); CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
} else { } else {
pinMode(RS_OUTPUT, OUTPUT); pinMode(RS_OUTPUT, OUTPUT);
digitalWrite(RS_OUTPUT, LOW); digitalWrite(RS_OUTPUT, LOW);
} }
} }
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0}; unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop() void loop() {
{
SERIAL.println("Sending message"); SERIAL.println("Sending message");
CAN.sendMsgBuf(0x00, 0, 0, NULL); // Send empty wakeup message CAN.sendMsgBuf(0x00, 0, 0, NULL); // Send empty wakeup message
delay(100); // give the receiving node some time to wake up delay(100); // give the receiving node some time to wake up
// send data: id = 0x00, standard frame, data len = 8, stmp: data buf // send data: id = 0x00, standard frame, data len = 8, stmp: data buf
stmp[7] = stmp[7]+1; stmp[7] = stmp[7] + 1;
if(stmp[7] == 100) if (stmp[7] == 100) {
{
stmp[7] = 0; stmp[7] = 0;
stmp[6] = stmp[6] + 1; stmp[6] = stmp[6] + 1;
if(stmp[6] == 100) if (stmp[6] == 100) {
{
stmp[6] = 0; stmp[6] = 0;
stmp[5] = stmp[6] + 1; stmp[5] = stmp[6] + 1;
} }
} }
CAN.sendMsgBuf(0x00, 0, 8, stmp); CAN.sendMsgBuf(0x00, 0, 8, stmp);
// sleep // sleep
SERIAL.println("Sleep"); SERIAL.println("Sleep");
SERIAL.flush(); SERIAL.flush();
// Put MCP2515 into sleep mode // Put MCP2515 into sleep mode
CAN.sleep(); CAN.sleep();
// Put the transceiver into standby (by pulling Rs high): // Put the transceiver into standby (by pulling Rs high):
if(RS_TO_MCP2515) if (RS_TO_MCP2515) {
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH); CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
else } else {
digitalWrite(RS_OUTPUT, HIGH); digitalWrite(RS_OUTPUT, HIGH);
}
// Put the MCU to sleep for one second // Put the MCU to sleep for one second
sleepMCU(); sleepMCU();
// wake MCP2515 and transceiver // wake MCP2515 and transceiver
CAN.wake(); CAN.wake();
if(RS_TO_MCP2515) if (RS_TO_MCP2515) {
CAN.mcpDigitalWrite(RS_OUTPUT, LOW); CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
else } else {
digitalWrite(RS_OUTPUT, LOW); digitalWrite(RS_OUTPUT, LOW);
}
} }
// END FILE // END FILE
@@ -8,9 +8,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -25,12 +25,10 @@ unsigned char len = 0;
unsigned char buf[8]; unsigned char buf[8];
char str[20]; char str[20];
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -41,15 +39,15 @@ void setup()
/* /*
* set mask, set both the mask to 0x3ff set mask, set both the mask to 0x3ff
*/ */
CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
CAN.init_Mask(1, 0, 0x3ff); CAN.init_Mask(1, 0, 0x3ff);
/* /*
* set filter, we can receive id from 0x04 ~ 0x09 set filter, we can receive id from 0x04 ~ 0x09
*/ */
CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515 CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515
CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515 CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515
@@ -60,15 +58,12 @@ void setup()
} }
void MCP2515_ISR() void MCP2515_ISR() {
{
flagRecv = 1; flagRecv = 1;
} }
void loop() void loop() {
{ if (flagRecv) { // check if get data
if(flagRecv) // check if get data
{
flagRecv = 0; // clear flag flagRecv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
@@ -76,8 +71,7 @@ void loop()
SERIAL.println("\r\n------------------------------------------------------------------"); SERIAL.println("\r\n------------------------------------------------------------------");
SERIAL.print("Get Data From id: "); SERIAL.print("Get Data From id: ");
SERIAL.println(CAN.getCanId()); SERIAL.println(CAN.getCanId());
for(int i = 0; i<len; i++) // print the data for (int i = 0; i < len; i++) { // print the data
{
SERIAL.print("0x"); SERIAL.print("0x");
SERIAL.print(buf[i], HEX); SERIAL.print(buf[i], HEX);
SERIAL.print("\t"); SERIAL.print("\t");
@@ -88,5 +82,5 @@ void loop()
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
@@ -6,9 +6,9 @@
/*SAMD core*/ /*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB #define SERIAL SerialUSB
#else #else
#define SERIAL Serial #define SERIAL Serial
#endif #endif
// the cs pin of the version after v1.1 is default to D9 // the cs pin of the version after v1.1 is default to D9
@@ -17,12 +17,10 @@ const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() void setup() {
{
SERIAL.begin(115200); SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail"); SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again"); SERIAL.println(" Init CAN BUS Shield again");
delay(100); delay(100);
@@ -32,10 +30,8 @@ void setup()
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop() void loop() {
{ for (int id = 0; id < 10; id++) {
for(int id=0; id<10; id++)
{
memset(stmp, id, sizeof(stmp)); // set id to send data buff memset(stmp, id, sizeof(stmp)); // set id to send data buff
CAN.sendMsgBuf(id, 0, sizeof(stmp), stmp); CAN.sendMsgBuf(id, 0, sizeof(stmp), stmp);
delay(100); delay(100);
@@ -43,5 +39,5 @@ void loop()
} }
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+826 -890
View File
File diff suppressed because it is too large Load Diff
+105 -90
View File
@@ -1,48 +1,48 @@
/* /*
mcp_can.h mcp_can.h
2012 Copyright (c) Seeed Technology Inc. All right reserved. 2012 Copyright (c) Seeed Technology Inc. All right reserved.
Author:Loovee (loovee@seeed.cc) Author:Loovee (loovee@seeed.cc)
2014-1-16 2014-1-16
Contributor: Contributor:
Cory J. Fowler Cory J. Fowler
Latonita Latonita
Woodward1 Woodward1
Mehtajaghvi Mehtajaghvi
BykeBlast BykeBlast
TheRo0T TheRo0T
Tsipizic Tsipizic
ralfEdmund ralfEdmund
Nathancheek Nathancheek
BlueAndi BlueAndi
Adlerweb Adlerweb
Btetz Btetz
Hurvajs Hurvajs
ttlappalainen ttlappalainen
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013 Seeed Technology Inc. Copyright (c) 2013 Seeed Technology Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
#ifndef _MCP2515_H_ #ifndef _MCP2515_H_
#define _MCP2515_H_ #define _MCP2515_H_
@@ -51,31 +51,30 @@
#define MAX_CHAR_IN_MESSAGE 8 #define MAX_CHAR_IN_MESSAGE 8
class MCP_CAN class MCP_CAN {
{ private:
private:
byte ext_flg; // identifier xxxID byte ext_flg; // identifier xxxID
// either extended (the 29 LSB) or standard (the 11 LSB) // either extended (the 29 LSB) or standard (the 11 LSB)
unsigned long can_id; // can id unsigned long can_id; // can id
byte rtr; // rtr byte rtr; // rtr
byte SPICS; byte SPICS;
SPIClass *pSPI; SPIClass* pSPI;
byte nReservedTx; // Count of tx buffers for reserved send byte nReservedTx; // Count of tx buffers for reserved send
byte mcpMode; // Current controller mode byte mcpMode; // Current controller mode
/* /*
* mcp2515 driver function mcp2515 driver function
*/ */
private: private:
void mcp2515_reset(void); // reset mcp2515 void mcp2515_reset(void); // reset mcp2515
byte mcp2515_readRegister(const byte address); // read mcp2515's register byte mcp2515_readRegister(const byte address); // read mcp2515's register
void mcp2515_readRegisterS(const byte address, void mcp2515_readRegisterS(const byte address,
byte values[], byte values[],
const byte n); const byte n);
void mcp2515_setRegister(const byte address, // set mcp2515's register void mcp2515_setRegister(const byte address, // set mcp2515's register
const byte value); const byte value);
@@ -92,71 +91,87 @@ private:
byte mcp2515_readStatus(void); // read mcp2515's Status byte mcp2515_readStatus(void); // read mcp2515's Status
byte mcp2515_setCANCTRL_Mode(const byte newmode); // set mode byte mcp2515_setCANCTRL_Mode(const byte newmode); // set mode
byte mcp2515_requestNewMode(const byte newmode); // Set mode byte mcp2515_requestNewMode(const byte newmode); // Set mode
byte mcp2515_configRate(const byte canSpeed, const byte clock); // set baudrate byte mcp2515_configRate(const byte canSpeed, const byte clock); // set baudrate
byte mcp2515_init(const byte canSpeed, const byte clock); // mcp2515init byte mcp2515_init(const byte canSpeed, const byte clock); // mcp2515init
void mcp2515_write_id( const byte mcp_addr, // write can id void mcp2515_write_id(const byte mcp_addr, // write can id
const byte ext, const byte ext,
const unsigned long id ); const unsigned long id);
void mcp2515_read_id( const byte mcp_addr, // read can id void mcp2515_read_id(const byte mcp_addr, // read can id
byte* ext, byte* ext,
unsigned long* id ); unsigned long* id);
void mcp2515_write_canMsg( const byte buffer_sidh_addr, unsigned long id, byte ext, byte rtr, byte len, volatile const byte *buf); // read can msg void mcp2515_write_canMsg(const byte buffer_sidh_addr, unsigned long id, byte ext, byte rtr, byte len,
void mcp2515_read_canMsg( const byte buffer_load_addr, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf); // write can msg volatile const byte* buf); // read can msg
void mcp2515_read_canMsg(const byte buffer_load_addr, volatile unsigned long* id, volatile byte* ext,
volatile byte* rtr, volatile byte* len, volatile byte* buf); // write can msg
void mcp2515_start_transmit(const byte mcp_addr); // start transmit void mcp2515_start_transmit(const byte mcp_addr); // start transmit
byte mcp2515_getNextFreeTXBuf(byte *txbuf_n); // get Next free txbuf byte mcp2515_getNextFreeTXBuf(byte* txbuf_n); // get Next free txbuf
byte mcp2515_isTXBufFree(byte *txbuf_n, byte iBuf); // is buffer by index free byte mcp2515_isTXBufFree(byte* txbuf_n, byte iBuf); // is buffer by index free
/* /*
* can operator function can operator function
*/ */
byte sendMsg(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent=true); // send message byte sendMsg(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, bool wait_sent = true); // send message
public: public:
MCP_CAN(byte _CS=0); MCP_CAN(byte _CS = 0);
void init_CS(byte _CS); // define CS after construction before begin() void init_CS(byte _CS); // define CS after construction before begin()
void setSPI(SPIClass *_pSPI) { pSPI=_pSPI; } // define SPI port to use before begin() void setSPI(SPIClass* _pSPI) {
void enableTxInterrupt(bool enable=true); // enable transmit interrupt pSPI = _pSPI; // define SPI port to use before begin()
void reserveTxBuffers(byte nTxBuf=0) { nReservedTx=(nTxBuf<MCP_N_TXBUFFERS?nTxBuf:MCP_N_TXBUFFERS-1); } }
byte getLastTxBuffer() { return MCP_N_TXBUFFERS-1; } // read index of last tx buffer void enableTxInterrupt(bool enable = true); // enable transmit interrupt
void reserveTxBuffers(byte nTxBuf = 0) {
nReservedTx = (nTxBuf < MCP_N_TXBUFFERS ? nTxBuf : MCP_N_TXBUFFERS - 1);
}
byte getLastTxBuffer() {
return MCP_N_TXBUFFERS - 1; // read index of last tx buffer
}
byte begin(byte speedset, const byte clockset = MCP_16MHz); // init can byte begin(byte speedset, const byte clockset = MCP_16MHz); // init can
byte init_Mask(byte num, byte ext, unsigned long ulData); // init Masks byte init_Mask(byte num, byte ext, unsigned long ulData); // init Masks
byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters
void setSleepWakeup(byte enable); // Enable or disable the wake up interrupt (If disabled the MCP2515 will not be woken up by CAN bus activity, making it send only) void setSleepWakeup(byte
byte sleep(); // Put the MCP2515 in sleep mode enable); // Enable or disable the wake up interrupt (If disabled the MCP2515 will not be woken up by CAN bus activity, making it send only)
byte wake(); // Wake MCP2515 manually from sleep byte sleep(); // Put the MCP2515 in sleep mode
byte setMode(byte opMode); // Set operational mode byte wake(); // Wake MCP2515 manually from sleep
byte getMode(); // Get operational mode byte setMode(byte opMode); // Set operational mode
byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, bool wait_sent=true); // send buf byte getMode(); // Get operational mode
byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte *buf, bool wait_sent=true); // send buf byte sendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf, bool wait_sent = true); // send buf
byte readMsgBuf(byte *len, byte *buf); // read buf byte sendMsgBuf(unsigned long id, byte ext, byte len, const byte* buf, bool wait_sent = true); // send buf
byte readMsgBufID(unsigned long *ID, byte *len, byte *buf); // read buf with object ID byte readMsgBuf(byte* len, byte* buf); // read buf
byte readMsgBufID(unsigned long* ID, byte* len, byte* buf); // read buf with object ID
byte checkReceive(void); // if something received byte checkReceive(void); // if something received
byte checkError(void); // if something error byte checkError(void); // if something error
unsigned long getCanId(void); // get can id when receive unsigned long getCanId(void); // get can id when receive
byte isRemoteRequest(void); // get RR flag when receive byte isRemoteRequest(void); // get RR flag when receive
byte isExtendedFrame(void); // did we recieve 29bit frame? byte isExtendedFrame(void); // did we recieve 29bit frame?
byte readMsgBufID(byte status, volatile unsigned long *id, volatile byte *ext, volatile byte *rtr, volatile byte *len, volatile byte *buf); // read buf with object ID byte readMsgBufID(byte status, volatile unsigned long* id, volatile byte* ext, volatile byte* rtr, volatile byte* len,
byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf, byte iTxBuf=0xff); // as sendMsgBuf, but does not have any wait for free buffer volatile byte* buf); // read buf with object ID
byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len, volatile const byte *buf); // send message buf by using parsed buffer status byte trySendMsgBuf(unsigned long id, byte ext, byte rtrBit, byte len, const byte* buf,
inline byte trySendExtMsgBuf(unsigned long id, byte len, const byte *buf, byte iTxBuf=0xff) { // as trySendMsgBuf, but set ext=1 and rtr=0 byte iTxBuf = 0xff); // as sendMsgBuf, but does not have any wait for free buffer
return trySendMsgBuf(id,1,0,len,buf,iTxBuf); byte sendMsgBuf(byte status, unsigned long id, byte ext, byte rtrBit, byte len,
volatile const byte* buf); // send message buf by using parsed buffer status
inline byte trySendExtMsgBuf(unsigned long id, byte len, const byte* buf,
byte iTxBuf = 0xff) { // as trySendMsgBuf, but set ext=1 and rtr=0
return trySendMsgBuf(id, 1, 0, len, buf, iTxBuf);
} }
inline byte sendExtMsgBuf(byte status, unsigned long id, byte len, volatile const byte *buf) { // as sendMsgBuf, but set ext=1 and rtr=0 inline byte sendExtMsgBuf(byte status, unsigned long id, byte len,
return sendMsgBuf(status,id,1,0,len,buf); volatile const byte* buf) { // as sendMsgBuf, but set ext=1 and rtr=0
return sendMsgBuf(status, id, 1, 0, len, buf);
} }
void clearBufferTransmitIfFlags(byte flags=0); // Clear transmit flags according to status void clearBufferTransmitIfFlags(byte flags = 0); // Clear transmit flags according to status
byte readRxTxStatus(void); // read has something send or received byte readRxTxStatus(void); // read has something send or received
byte checkClearRxStatus(byte *status); // read and clear and return first found rx status bit byte checkClearRxStatus(byte* status); // read and clear and return first found rx status bit
byte checkClearTxStatus(byte *status, byte iTxBuf=0xff); // read and clear and return first found or buffer specified tx status bit byte checkClearTxStatus(byte* status,
byte iTxBuf = 0xff); // read and clear and return first found or buffer specified tx status bit
bool mcpPinMode(const byte pin, const byte mode); // switch supported pins between HiZ, interrupt, output or input
bool mcpPinMode(const byte pin, const byte
mode); // switch supported pins between HiZ, interrupt, output or input
bool mcpDigitalWrite(const byte pin, const byte mode); // write HIGH or LOW to RX0BF/RX1BF bool mcpDigitalWrite(const byte pin, const byte mode); // write HIGH or LOW to RX0BF/RX1BF
byte mcpDigitalRead(const byte pin); // read HIGH or LOW from supported pins byte mcpDigitalRead(const byte pin); // read HIGH or LOW from supported pins
@@ -164,5 +179,5 @@ public:
#endif #endif
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/
+39 -39
View File
@@ -1,49 +1,49 @@
/* /*
mcp_can_dfs.h mcp_can_dfs.h
2012 Copyright (c) Seeed Technology Inc. All right reserved. 2012 Copyright (c) Seeed Technology Inc. All right reserved.
Author:Loovee (loovee@seeed.cc) Author:Loovee (loovee@seeed.cc)
2014-1-16 2014-1-16
Contributor: Contributor:
Cory J. Fowler Cory J. Fowler
Latonita Latonita
Woodward1 Woodward1
Mehtajaghvi Mehtajaghvi
BykeBlast BykeBlast
TheRo0T TheRo0T
Tsipizic Tsipizic
ralfEdmund ralfEdmund
Nathancheek Nathancheek
BlueAndi BlueAndi
Adlerweb Adlerweb
Btetz Btetz
Hurvajs Hurvajs
xboxpro1 xboxpro1
ttlappalainen ttlappalainen
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013 Seeed Technology Inc. Copyright (c) 2013 Seeed Technology Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
#ifndef _MCP2515DFS_H_ #ifndef _MCP2515DFS_H_
#define _MCP2515DFS_H_ #define _MCP2515DFS_H_
@@ -55,7 +55,7 @@
// if print debug information // if print debug information
#ifndef DEBUG_EN #ifndef DEBUG_EN
#define DEBUG_EN 1 #define DEBUG_EN 1
#endif #endif
// Begin mt // Begin mt
@@ -480,5 +480,5 @@
#endif #endif
/********************************************************************************************************* /*********************************************************************************************************
END FILE END FILE
*********************************************************************************************************/ *********************************************************************************************************/