From 677adc5211251b840bbde1de0d53a69fa484886d Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 12 Mar 2019 12:01:56 +0100 Subject: [PATCH 1/4] Initial import of the MCP_CAN library. --- .gitmodules | 3 +++ Sming/Libraries/MCP_CAN_lib | 1 + 2 files changed, 4 insertions(+) create mode 160000 Sming/Libraries/MCP_CAN_lib diff --git a/.gitmodules b/.gitmodules index 1133a9521e..953c3c5eb9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -112,4 +112,7 @@ [submodule "Libraries.ArduinoJson5"] path = Sming/Libraries/ArduinoJson5/ArduinoJson url = https://github.com/bblanchon/ArduinoJson.git +[submodule "Libraries.MCP_CAN_lib"] + path = Sming/Libraries/MCP_CAN_lib + url = https://github.com/coryjfowler/MCP_CAN_lib.git ignore = dirty diff --git a/Sming/Libraries/MCP_CAN_lib b/Sming/Libraries/MCP_CAN_lib new file mode 160000 index 0000000000..280d6bec2f --- /dev/null +++ b/Sming/Libraries/MCP_CAN_lib @@ -0,0 +1 @@ +Subproject commit 280d6bec2f424f39955e62f2bc45efc01577f783 From 0af3b5c28ad08c7c0c08b949db7288483ff70f5e Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 12 Mar 2019 16:11:20 +0100 Subject: [PATCH 2/4] Adding initial CanBus sample. --- samples/CanBus/.cproject | 151 +++++++++++++++++++++++++++++ samples/CanBus/.project | 28 ++++++ samples/CanBus/Makefile | 20 ++++ samples/CanBus/Makefile-user.mk | 39 ++++++++ samples/CanBus/app/application.cpp | 41 ++++++++ 5 files changed, 279 insertions(+) create mode 100644 samples/CanBus/.cproject create mode 100644 samples/CanBus/.project create mode 100644 samples/CanBus/Makefile create mode 100644 samples/CanBus/Makefile-user.mk create mode 100644 samples/CanBus/app/application.cpp diff --git a/samples/CanBus/.cproject b/samples/CanBus/.cproject new file mode 100644 index 0000000000..67c056d24e --- /dev/null +++ b/samples/CanBus/.cproject @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make + -f ${ProjDirPath}/Makefile + all + true + true + true + + + make + -f ${ProjDirPath}/Makefile + clean + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flash + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashonefile + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashinit + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashboot + true + true + true + + + make + -f ${ProjDirPath}/Makefile + rebuild + true + true + true + + + + + + + + + + + + + + + + + + + + diff --git a/samples/CanBus/.project b/samples/CanBus/.project new file mode 100644 index 0000000000..4d1952da65 --- /dev/null +++ b/samples/CanBus/.project @@ -0,0 +1,28 @@ + + + CanBus + + + SmingFramework + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/samples/CanBus/Makefile b/samples/CanBus/Makefile new file mode 100644 index 0000000000..77fee85347 --- /dev/null +++ b/samples/CanBus/Makefile @@ -0,0 +1,20 @@ +##################################################################### +#### Please don't change this file. Use Makefile-user.mk instead #### +##################################################################### +# Including user Makefile. +# Should be used to set project-specific parameters +include ./Makefile-user.mk + +# Important parameters check. +# We need to make sure SMING_HOME and ESP_HOME variables are set. +# You can use Makefile-user.mk in each project or use enviromental variables to set it globally. + +ifndef SMING_HOME +$(error SMING_HOME is not set. Please configure it in Makefile-user.mk) +endif +ifndef ESP_HOME +$(error ESP_HOME is not set. Please configure it in Makefile-user.mk) +endif + +# Include application Makefile +include $(SMING_HOME)/Makefile-rboot.mk \ No newline at end of file diff --git a/samples/CanBus/Makefile-user.mk b/samples/CanBus/Makefile-user.mk new file mode 100644 index 0000000000..bd088ca354 --- /dev/null +++ b/samples/CanBus/Makefile-user.mk @@ -0,0 +1,39 @@ +## Local build configuration +## Parameters configured here will override default and ENV values. +## Uncomment and change examples: + +## Add your source directories here separated by space +# MODULES = app +# EXTRA_INCDIR = include + +## ESP_HOME sets the path where ESP tools and SDK are located. +## Windows: +# ESP_HOME = c:/Espressif + +## MacOS / Linux: +# ESP_HOME = /opt/esp-open-sdk + +## SMING_HOME sets the path where Sming framework is located. +## Windows: +# SMING_HOME = c:/tools/sming/Sming + +## MacOS / Linux +# SMING_HOME = /opt/sming/Sming + +## COM port parameter is reqruied to flash firmware correctly. +## Windows: +# COM_PORT = COM3 + +## MacOS / Linux: +# COM_PORT = /dev/tty.usbserial + +## Com port speed +# COM_SPEED = 115200 + +## Configure flash parameters (for ESP12-E and other new boards): +# SPI_MODE = dio + +## SPIFFS options +DISABLE_SPIFFS = 1 +# SPIFF_FILES = files + diff --git a/samples/CanBus/app/application.cpp b/samples/CanBus/app/application.cpp new file mode 100644 index 0000000000..e102069edf --- /dev/null +++ b/samples/CanBus/app/application.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include + +unsigned long rxId; +byte len; +byte rxBuf[8]; + +byte txBuf0[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; +byte txBuf1[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA}; + +MCP_CAN CAN0(10); // CAN0 interface using CS on digital pin 10 +MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9 + +void init() +{ + Serial.begin(115200); + + // init CAN0 bus, baudrate: 250k@16MHz + if(CAN0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK) { + Serial.print("CAN0: Init OK!\r\n"); + CAN0.setMode(MCP_NORMAL); + } else { + Serial.print("CAN0: Init Fail!!!\r\n"); + } + + // init CAN1 bus, baudrate: 250k@16MHz + if(CAN1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK) { + Serial.print("CAN1: Init OK!\r\n"); + CAN1.setMode(MCP_NORMAL); + } else { + Serial.print("CAN1: Init Fail!!!\r\n"); + } + + // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz) + SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE3)); + + CAN0.sendMsgBuf(0x1000000, 1, 8, txBuf0); + CAN1.sendMsgBuf(0x1000001, 1, 8, txBuf1); +} From 325e95af13053ddf5af1b550f4d091b6caaeae41 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Fri, 12 Jul 2019 15:00:57 +0200 Subject: [PATCH 3/4] Updated to take advantage of the modular build system. --- .gitmodules | 1 + samples/CanBus/Makefile | 17 +++----------- samples/CanBus/Makefile-user.mk | 39 --------------------------------- samples/CanBus/component.mk | 3 +++ 4 files changed, 7 insertions(+), 53 deletions(-) delete mode 100644 samples/CanBus/Makefile-user.mk create mode 100644 samples/CanBus/component.mk diff --git a/.gitmodules b/.gitmodules index 953c3c5eb9..f910ca0dea 100644 --- a/.gitmodules +++ b/.gitmodules @@ -112,6 +112,7 @@ [submodule "Libraries.ArduinoJson5"] path = Sming/Libraries/ArduinoJson5/ArduinoJson url = https://github.com/bblanchon/ArduinoJson.git + ignore = dirty [submodule "Libraries.MCP_CAN_lib"] path = Sming/Libraries/MCP_CAN_lib url = https://github.com/coryjfowler/MCP_CAN_lib.git diff --git a/samples/CanBus/Makefile b/samples/CanBus/Makefile index 77fee85347..ff51b6c3a7 100644 --- a/samples/CanBus/Makefile +++ b/samples/CanBus/Makefile @@ -1,20 +1,9 @@ ##################################################################### -#### Please don't change this file. Use Makefile-user.mk instead #### +#### Please don't change this file. Use component.mk instead #### ##################################################################### -# Including user Makefile. -# Should be used to set project-specific parameters -include ./Makefile-user.mk -# Important parameters check. -# We need to make sure SMING_HOME and ESP_HOME variables are set. -# You can use Makefile-user.mk in each project or use enviromental variables to set it globally. - ifndef SMING_HOME -$(error SMING_HOME is not set. Please configure it in Makefile-user.mk) -endif -ifndef ESP_HOME -$(error ESP_HOME is not set. Please configure it in Makefile-user.mk) +$(error SMING_HOME is not set: please configure it as an environment variable) endif -# Include application Makefile -include $(SMING_HOME)/Makefile-rboot.mk \ No newline at end of file +include $(SMING_HOME)/project.mk diff --git a/samples/CanBus/Makefile-user.mk b/samples/CanBus/Makefile-user.mk deleted file mode 100644 index bd088ca354..0000000000 --- a/samples/CanBus/Makefile-user.mk +++ /dev/null @@ -1,39 +0,0 @@ -## Local build configuration -## Parameters configured here will override default and ENV values. -## Uncomment and change examples: - -## Add your source directories here separated by space -# MODULES = app -# EXTRA_INCDIR = include - -## ESP_HOME sets the path where ESP tools and SDK are located. -## Windows: -# ESP_HOME = c:/Espressif - -## MacOS / Linux: -# ESP_HOME = /opt/esp-open-sdk - -## SMING_HOME sets the path where Sming framework is located. -## Windows: -# SMING_HOME = c:/tools/sming/Sming - -## MacOS / Linux -# SMING_HOME = /opt/sming/Sming - -## COM port parameter is reqruied to flash firmware correctly. -## Windows: -# COM_PORT = COM3 - -## MacOS / Linux: -# COM_PORT = /dev/tty.usbserial - -## Com port speed -# COM_SPEED = 115200 - -## Configure flash parameters (for ESP12-E and other new boards): -# SPI_MODE = dio - -## SPIFFS options -DISABLE_SPIFFS = 1 -# SPIFF_FILES = files - diff --git a/samples/CanBus/component.mk b/samples/CanBus/component.mk new file mode 100644 index 0000000000..5db38ef1f8 --- /dev/null +++ b/samples/CanBus/component.mk @@ -0,0 +1,3 @@ +ARDUINO_LIBRARIES := MCP_CAN_lib + +DISABLE_SPIFFS = 1 From a29f005369f4274868ba1d67dc15ec2152ccb59a Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Fri, 12 Jul 2019 15:59:58 +0200 Subject: [PATCH 4/4] Updated sample. --- samples/CanBus/app/application.cpp | 44 ++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/samples/CanBus/app/application.cpp b/samples/CanBus/app/application.cpp index e102069edf..2bca550606 100644 --- a/samples/CanBus/app/application.cpp +++ b/samples/CanBus/app/application.cpp @@ -3,32 +3,46 @@ #include #include -unsigned long rxId; -byte len; -byte rxBuf[8]; +unsigned long rxId = 0; +uint8_t len = 0; +uint8_t rxBuf[8] = {0}; -byte txBuf0[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; -byte txBuf1[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA}; +uint8_t txBuf0[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; +uint8_t txBuf1[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA}; -MCP_CAN CAN0(10); // CAN0 interface using CS on digital pin 10 -MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9 +MCP_CAN canBus0(10); // CAN0 interface using CS on digital pin 10 +MCP_CAN canBus1(9); // CAN1 interface using CS on digital pin 9 + +Timer loopTimer; + +void loop() +{ + if(!digitalRead(2)) { // If pin 2 is low, read CAN0 receive buffer + canBus0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + canBus1.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN1 interface + } + if(!digitalRead(3)) { // If pin 3 is low, read CAN1 receive buffer + canBus1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + canBus0.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN0 interface + } +} void init() { - Serial.begin(115200); + Serial.begin(SERIAL_BAUD_RATE); // init CAN0 bus, baudrate: 250k@16MHz - if(CAN0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK) { + if(canBus0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK) { Serial.print("CAN0: Init OK!\r\n"); - CAN0.setMode(MCP_NORMAL); + canBus0.setMode(MCP_NORMAL); } else { Serial.print("CAN0: Init Fail!!!\r\n"); } // init CAN1 bus, baudrate: 250k@16MHz - if(CAN1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK) { + if(canBus1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK) { Serial.print("CAN1: Init OK!\r\n"); - CAN1.setMode(MCP_NORMAL); + canBus1.setMode(MCP_NORMAL); } else { Serial.print("CAN1: Init Fail!!!\r\n"); } @@ -36,6 +50,8 @@ void init() // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz) SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE3)); - CAN0.sendMsgBuf(0x1000000, 1, 8, txBuf0); - CAN1.sendMsgBuf(0x1000001, 1, 8, txBuf1); + canBus0.sendMsgBuf(0x1000000, 1, 8, txBuf0); + canBus1.sendMsgBuf(0x1000001, 1, 8, txBuf1); + + loopTimer.initializeMs(1000, loop).start(); }