mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-07-28 04:06:45 +00:00
- Use Hardware Serial in CAN-monitor example
This commit is contained in:
@@ -15,32 +15,6 @@
|
|||||||
#include "mcp_can.h"
|
#include "mcp_can.h"
|
||||||
#include "can-serial.h"
|
#include "can-serial.h"
|
||||||
|
|
||||||
#define LOGGING_ENABLED
|
|
||||||
|
|
||||||
#ifdef LOGGING_ENABLED
|
|
||||||
#define dbg_begin(x) debug.begin(x)
|
|
||||||
#define dbg0(x) debug.print(x)
|
|
||||||
#define dbg1(x) debug.println(x)
|
|
||||||
#define dbg2(x,y) debug.print(x); debug.println(y)
|
|
||||||
#define dbgH(x) debug.print(x,HEX)
|
|
||||||
#define DEBUG_RX_PIN 8
|
|
||||||
#define DEBUG_TX_PIN 9
|
|
||||||
#else
|
|
||||||
#define dbg_begin(x)
|
|
||||||
#define dbg0(x)
|
|
||||||
#define dbg1(x)
|
|
||||||
#define dbg2(x,y)
|
|
||||||
#define dbgH(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef LOGGING_ENABLED
|
|
||||||
// software serial #2: TX = digital pin 8, RX = digital pin 9
|
|
||||||
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
|
|
||||||
|
|
||||||
SoftwareSerial debug(DEBUG_RX_PIN, DEBUG_TX_PIN);
|
|
||||||
//#define debug Serial
|
|
||||||
#endif
|
|
||||||
|
|
||||||
CanSerial* CanSerial::_instance = 0;
|
CanSerial* CanSerial::_instance = 0;
|
||||||
|
|
||||||
CanSerial* CanSerial::instance() {
|
CanSerial* CanSerial::instance() {
|
||||||
@@ -50,8 +24,8 @@ CanSerial* CanSerial::instance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CanSerial::init(INT8U defaultCanSpeed, const INT8U clock) {
|
void CanSerial::init(INT8U defaultCanSpeed, const INT8U clock) {
|
||||||
dbg_begin(LWUART_DEFAULT_BAUD_RATE); // logging through software serial
|
Serial.begin(LWUART_DEFAULT_BAUD_RATE); // logging through software serial
|
||||||
dbg1("CAN ASCII. Welcome to debug");
|
Serial.println("CAN ASCII. Welcome to debug");
|
||||||
|
|
||||||
instance()->LWUARTCanSpeedSelection = defaultCanSpeed;
|
instance()->LWUARTCanSpeedSelection = defaultCanSpeed;
|
||||||
instance()->LWUARTMcpModuleClock = clock;
|
instance()->LWUARTMcpModuleClock = clock;
|
||||||
@@ -79,7 +53,7 @@ void CanSerial::serialEvent() {
|
|||||||
|
|
||||||
void CanSerial::initFunc() {
|
void CanSerial::initFunc() {
|
||||||
if (!inputString.reserve(LWUART_INPUT_STRING_BUFFER_SIZE)) {
|
if (!inputString.reserve(LWUART_INPUT_STRING_BUFFER_SIZE)) {
|
||||||
dbg0("inputString.reserve failed in initFunc. less optimal String work is expected");
|
Serial.print("inputString.reserve failed in initFunc. less optimal String work is expected");
|
||||||
}
|
}
|
||||||
// LWUARTAutoStart = true; //todo: read from eeprom
|
// LWUARTAutoStart = true; //todo: read from eeprom
|
||||||
// LWUARTAutoPoll = false; //todo: read from eeprom
|
// LWUARTAutoPoll = false; //todo: read from eeprom
|
||||||
@@ -98,7 +72,7 @@ void CanSerial::setFilterFunc(INT8U (*userFunc)(INT32U)) {
|
|||||||
|
|
||||||
void CanSerial::loopFunc() {
|
void CanSerial::loopFunc() {
|
||||||
if (stringComplete) {
|
if (stringComplete) {
|
||||||
int len = inputString.length();
|
unsigned int len = inputString.length();
|
||||||
if (len > 0 && len < LWUART_FRAME_MAX_SIZE) {
|
if (len > 0 && len < LWUART_FRAME_MAX_SIZE) {
|
||||||
strcpy((char*)LWUARTMessage, inputString.c_str());
|
strcpy((char*)LWUARTMessage, inputString.c_str());
|
||||||
exec();
|
exec();
|
||||||
@@ -110,7 +84,7 @@ void CanSerial::loopFunc() {
|
|||||||
if (LWUARTCanChannelMode != LWUART_STATUS_CAN_CLOSED) {
|
if (LWUARTCanChannelMode != LWUART_STATUS_CAN_CLOSED) {
|
||||||
int recv = 0;
|
int recv = 0;
|
||||||
while (CAN_MSGAVAIL == checkReceive() && recv++<5) {
|
while (CAN_MSGAVAIL == checkReceive() && recv++<5) {
|
||||||
dbg0('+');
|
Serial.print('+');
|
||||||
if (CAN_OK == receiveSingleFrame()) {
|
if (CAN_OK == receiveSingleFrame()) {
|
||||||
Serial.write(LWUART_CR);
|
Serial.write(LWUART_CR);
|
||||||
}
|
}
|
||||||
@@ -129,7 +103,8 @@ void CanSerial::serialEventFunc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
INT8U CanSerial::exec() {
|
INT8U CanSerial::exec() {
|
||||||
dbg2("Command received:", inputString);
|
Serial.print("Command received:");
|
||||||
|
Serial.println(inputString);
|
||||||
LWUARTLastErr = parseAndRunCommand();
|
LWUARTLastErr = parseAndRunCommand();
|
||||||
switch (LWUARTLastErr) {
|
switch (LWUARTLastErr) {
|
||||||
case LWUART_OK:
|
case LWUART_OK:
|
||||||
@@ -526,15 +501,12 @@ void CanSerial::parseCanExtId() {
|
|||||||
void HexHelper::printFullByte(INT8U b) {
|
void HexHelper::printFullByte(INT8U b) {
|
||||||
if (b < 0x10) {
|
if (b < 0x10) {
|
||||||
Serial.print('0');
|
Serial.print('0');
|
||||||
// dbg0('0');
|
|
||||||
}
|
}
|
||||||
Serial.print(b, HEX);
|
Serial.print(b, HEX);
|
||||||
//dbgH(b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HexHelper::printNibble(INT8U b) {
|
void HexHelper::printNibble(INT8U b) {
|
||||||
Serial.print(b & 0x0F, HEX);
|
Serial.print(b & 0x0F, HEX);
|
||||||
//dbgH(b & 0x0F);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,33 +17,12 @@
|
|||||||
|
|
||||||
#include "mcp_can.h"
|
#include "mcp_can.h"
|
||||||
|
|
||||||
|
|
||||||
#define LOGGING_ENABLED
|
#define LOGGING_ENABLED
|
||||||
|
|
||||||
#ifdef LOGGING_ENABLED
|
|
||||||
#include "SoftwareSerial.h"
|
|
||||||
#define dbg_begin(x) debug.begin(x)
|
|
||||||
#define dbg0(x) debug.print(x)
|
|
||||||
#define dbg1(x) debug.println(x)
|
|
||||||
#define dbg2(x,y) debug.print(x); debug.println(y)
|
|
||||||
#define dbgH(x) debug.print(x,HEX)
|
|
||||||
#define DEBUG_RX_PIN 8
|
|
||||||
#define DEBUG_TX_PIN 9
|
|
||||||
#else
|
|
||||||
#define dbg_begin(x)
|
|
||||||
#define dbg0(x)
|
|
||||||
#define dbg1(x)
|
|
||||||
#define dbg2(x,y)
|
|
||||||
#define dbgH(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define LWUART_LAWICEL_VERSION_STR "V1013"
|
#define LWUART_LAWICEL_VERSION_STR "V1013"
|
||||||
#define LWUART_LAWICEL_SERIAL_NUM "NA123"
|
#define LWUART_LAWICEL_SERIAL_NUM "NA123"
|
||||||
#define LWUART_CAN_BUS_SHIELD_CS_PIN 10
|
#define LWUART_CAN_BUS_SHIELD_CS_PIN 10
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// Commands not supported/not implemented:
|
// Commands not supported/not implemented:
|
||||||
@@ -92,7 +71,7 @@
|
|||||||
#define HIGH_WORD(x) ((unsigned short)(((x)>>16)&0xFFFF))
|
#define HIGH_WORD(x) ((unsigned short)(((x)>>16)&0xFFFF))
|
||||||
|
|
||||||
#ifndef INT32U
|
#ifndef INT32U
|
||||||
#define INT32U unsigned long
|
#define INT32U unsigned long
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef INT16U
|
#ifndef INT16U
|
||||||
|
|||||||
@@ -15,20 +15,21 @@
|
|||||||
#include "mcp_can.h"
|
#include "mcp_can.h"
|
||||||
#include "can-serial.h"
|
#include "can-serial.h"
|
||||||
|
|
||||||
|
//#define CAN_2518FD
|
||||||
#define CAN_2515
|
#define CAN_2515
|
||||||
|
|
||||||
#ifdef CAN_2518FD
|
#ifdef CAN_2518FD
|
||||||
#include "mcp2518fd_can.h"
|
#include "mcp2518fd_can.h"
|
||||||
const int SPI_CS_PIN = BCM8;
|
const int SPI_CS_PIN = BCM8;
|
||||||
const int CAN_INT_PIN = BCM25;
|
const int CAN_INT_PIN = BCM25;
|
||||||
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
|
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CAN_2515
|
#ifdef CAN_2515
|
||||||
#include "mcp2515_can.h"
|
#include "mcp2515_can.h"
|
||||||
const int SPI_CS_PIN = 9;
|
const int SPI_CS_PIN = 9;
|
||||||
const int CAN_INT_PIN = 2;
|
const int CAN_INT_PIN = 2;
|
||||||
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
|
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
|
||||||
#endif // Set CS pin
|
#endif // Set CS pin
|
||||||
|
|
||||||
|
|
||||||
@@ -83,4 +84,3 @@ void loop() {
|
|||||||
void serialEvent() {
|
void serialEvent() {
|
||||||
CanSerial::serialEvent();
|
CanSerial::serialEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user