- Use Hardware Serial in CAN-monitor example

This commit is contained in:
Gudni
2021-01-06 12:35:20 +00:00
parent 3a35904217
commit 1fc462f2b2
3 changed files with 17 additions and 66 deletions
+7 -35
View File
@@ -15,32 +15,6 @@
#include "mcp_can.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() {
@@ -50,8 +24,8 @@ CanSerial* CanSerial::instance() {
}
void CanSerial::init(INT8U defaultCanSpeed, const INT8U clock) {
dbg_begin(LWUART_DEFAULT_BAUD_RATE); // logging through software serial
dbg1("CAN ASCII. Welcome to debug");
Serial.begin(LWUART_DEFAULT_BAUD_RATE); // logging through software serial
Serial.println("CAN ASCII. Welcome to debug");
instance()->LWUARTCanSpeedSelection = defaultCanSpeed;
instance()->LWUARTMcpModuleClock = clock;
@@ -79,7 +53,7 @@ void CanSerial::serialEvent() {
void CanSerial::initFunc() {
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
// LWUARTAutoPoll = false; //todo: read from eeprom
@@ -98,7 +72,7 @@ void CanSerial::setFilterFunc(INT8U (*userFunc)(INT32U)) {
void CanSerial::loopFunc() {
if (stringComplete) {
int len = inputString.length();
unsigned int len = inputString.length();
if (len > 0 && len < LWUART_FRAME_MAX_SIZE) {
strcpy((char*)LWUARTMessage, inputString.c_str());
exec();
@@ -110,7 +84,7 @@ void CanSerial::loopFunc() {
if (LWUARTCanChannelMode != LWUART_STATUS_CAN_CLOSED) {
int recv = 0;
while (CAN_MSGAVAIL == checkReceive() && recv++<5) {
dbg0('+');
Serial.print('+');
if (CAN_OK == receiveSingleFrame()) {
Serial.write(LWUART_CR);
}
@@ -129,7 +103,8 @@ void CanSerial::serialEventFunc() {
}
INT8U CanSerial::exec() {
dbg2("Command received:", inputString);
Serial.print("Command received:");
Serial.println(inputString);
LWUARTLastErr = parseAndRunCommand();
switch (LWUARTLastErr) {
case LWUART_OK:
@@ -526,15 +501,12 @@ void CanSerial::parseCanExtId() {
void HexHelper::printFullByte(INT8U b) {
if (b < 0x10) {
Serial.print('0');
// dbg0('0');
}
Serial.print(b, HEX);
//dbgH(b);
}
void HexHelper::printNibble(INT8U b) {
Serial.print(b & 0x0F, HEX);
//dbgH(b & 0x0F);
}
+1 -22
View File
@@ -17,33 +17,12 @@
#include "mcp_can.h"
#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_SERIAL_NUM "NA123"
#define LWUART_CAN_BUS_SHIELD_CS_PIN 10
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Commands not supported/not implemented:
@@ -92,7 +71,7 @@
#define HIGH_WORD(x) ((unsigned short)(((x)>>16)&0xFFFF))
#ifndef INT32U
#define INT32U unsigned long
#define INT32U unsigned long
#endif
#ifndef INT16U
+9 -9
View File
@@ -15,20 +15,21 @@
#include "mcp_can.h"
#include "can-serial.h"
//#define CAN_2518FD
#define CAN_2515
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#include "mcp2518fd_can.h"
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif // Set CS pin
@@ -83,4 +84,3 @@ void loop() {
void serialEvent() {
CanSerial::serialEvent();
}