Skip to content

Commit 6d00a04

Browse files
committed
Add CAN transport layer (#1488)
1 parent 6e4206b commit 6d00a04

28 files changed

+4426
-6
lines changed

.ci/doxygen.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def call(config) {
66
Documentation/doxygen.sh"""
77
warnings canComputeNew: false, canResolveRelativePaths: false,
88
defaultEncoding: '',
9-
excludePattern: '''.*/hal/architecture/Linux/drivers/.*,.*/hal/transport/PJON/driver/.*,.*/hal/architecture/AVR/drivers/.*,.*/drivers/TinyGSM/.*''',
9+
excludePattern: '''.*/hal/architecture/Linux/drivers/.*,.*/hal/transport/PJON/driver/.*,.*/hal/transport/CAN/driver/.*,.*/hal/architecture/AVR/drivers/.*,.*/drivers/TinyGSM/.*''',
1010
failedTotalAll: '', healthy: '', includePattern: '', messagesPattern: '',
1111
parserConfigurations: [[parserName: 'Doxygen', pattern: config.repository_root+'doxygen.log']],
1212
unHealthy: '', unstableTotalAll: '0'

.mystools/cppcheck/config/suppressions.cfg

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
// 3rd party
55
*:hal/architecture/Linux/*
66
*:drivers/*
7-
*:hal/transport/PJON/driver/*
7+
*:hal/transport/PJON/driver/*
8+
*:hal/transport/CAN/driver/*

MyConfig.h

+50-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,52 @@
267267
*/
268268
//#define MY_RS485
269269

270+
/**
271+
* @def MY_CAN
272+
* @brief Define this to use the CAN wired transport for sensor network communication.
273+
*/
274+
//#define MY_CAN
275+
/**
276+
* @def MY_DEBUG_VERBOSE_CAN
277+
* @brief Define this for verbose debug prints related to the %CAN driver.
278+
*/
279+
//#define MY_DEBUG_VERBOSE_CAN
280+
/**
281+
* @def CAN_INT
282+
* @brief Message arrived interrupt pin.
283+
*/
284+
#ifndef CAN_INT
285+
#define CAN_INT (2u)
286+
#endif
287+
/**
288+
* @def CAN_CS
289+
* @brief Chip select pin.
290+
*/
291+
#ifndef CAN_CS
292+
#define CAN_CS (10u)
293+
#endif
294+
/**
295+
* @def CAN_SPEED
296+
* @brief Baud rate. Allowed values can be found in mcp_can_dfs.h
297+
*/
298+
#ifndef CAN_SPEED
299+
#define CAN_SPEED CAN_250KBPS
300+
#endif
301+
/**
302+
* @def CAN_CLOCK
303+
* @brief can clock. Allowed values can be found in mcp_can_dfs.h
304+
*/
305+
#ifndef CAN_CLOCK
306+
#define CAN_CLOCK MCP_8MHZ
307+
#endif
308+
/**
309+
* @def CAN_BUF_SIZE
310+
* @brief assemble buffer size. Since long messages can be sliced and arrive mixed with other messages, assemble buffer is required.
311+
*/
312+
#ifndef CAN_BUF_SIZE
313+
#define CAN_BUF_SIZE (8u)
314+
#endif
315+
270316
/**
271317
* @def MY_RS485_BAUD_RATE
272318
* @brief The RS485 BAUD rate.
@@ -2328,7 +2374,7 @@
23282374
#endif
23292375

23302376
// Enable sensor network "feature" if one of the transport types was enabled
2331-
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined(MY_PJON)
2377+
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined(MY_PJON) || defined(MY_CAN)
23322378
#define MY_SENSOR_NETWORK
23332379
#endif
23342380

@@ -2499,6 +2545,9 @@
24992545
// PJON
25002546
#define MY_PJON
25012547
#define MY_DEBUG_VERBOSE_PJON
2548+
// CAN
2549+
#define MY_CAN
2550+
#define MY_DEBUG_VERBOSE_CAN
25022551
// RF24
25032552
#define MY_RADIO_RF24
25042553
#define MY_RADIO_NRF24 //deprecated

MySensors.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,13 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
290290
#else
291291
#define _PJONCNT 0 //!< _PJONCNT
292292
#endif
293+
#if defined(MY_CAN)
294+
#define __CANCNT 1 //!< __CANCNT
295+
#else
296+
#define __CANCNT 0 //!< __CANCNT
297+
#endif
293298

294-
#if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT + _PJONCNT > 1)
299+
#if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT + _PJONCNT + __CANCNT> 1)
295300
#error Only one forward link driver can be activated
296301
#endif
297302
#endif //DOXYGEN
@@ -302,7 +307,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
302307
#endif
303308

304309
// TRANSPORT INCLUDES
305-
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined (MY_PJON)
310+
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined (MY_PJON) || defined(MY_CAN)
306311
#include "hal/transport/MyTransportHAL.h"
307312
#include "core/MyTransport.h"
308313

@@ -386,6 +391,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
386391
#elif defined(MY_RADIO_RFM95)
387392
#include "hal/transport/RFM95/driver/RFM95.cpp"
388393
#include "hal/transport/RFM95/MyTransportRFM95.cpp"
394+
#elif defined(MY_CAN)
395+
#include "hal/transport/CAN/MyTransportCAN.cpp"
389396
#elif defined(MY_PJON)
390397
#include "hal/transport/PJON/driver/PJON.h"
391398
#include "hal/transport/PJON/driver/PJONSoftwareBitBang.h"

configure

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ MySensors options:
6262
MQTT publish topic prefix.
6363
--my-mqtt-subscribe-topic-prefix=<PREFIX>
6464
MQTT subscribe topic prefix.
65-
--my-transport=[none|rf24|rfm69|rfm95|rs485]
65+
--my-transport=[none|rf24|rfm69|rfm95|rs485|can]
6666
Set the transport to be used to communicate with other nodes. [rf24]
6767
--my-rf24-channel=<0-125> RF channel for the sensor net. [76]
6868
--my-rf24-pa-level=[RF24_PA_MAX|RF24_PA_HIGH|RF24_PA_LOW|RF24_PA_MIN]
@@ -660,6 +660,8 @@ elif [[ ${transport_type} == "rfm95" ]]; then
660660
CPPFLAGS="-DMY_RADIO_RFM95 $CPPFLAGS"
661661
elif [[ ${transport_type} == "rs485" ]]; then
662662
CPPFLAGS="-DMY_RS485 $CPPFLAGS"
663+
elif [[ ${transport_type} == "can" ]]; then
664+
CPPFLAGS="-DMY_CAN $CPPFLAGS"
663665
else
664666
die "Invalid transport type ${transport_type}." 3
665667
fi

examples/CANSwitch/CANSwitch.ino

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2019 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*
19+
*******************************
20+
*
21+
* DESCRIPTION
22+
*
23+
* Interrupt driven binary switch example with dual interrupts
24+
* Author: Patrick 'Anticimex' Fallberg
25+
* Connect one button or door/window reed switch between
26+
* digital I/O pin 3 (BUTTON_PIN below) and GND and the other
27+
* one in similar fashion on digital I/O pin 2.
28+
* This example is designed to fit Arduino Nano/Pro Mini
29+
*
30+
*/
31+
32+
33+
// Enable debug prints to serial monitor
34+
#define MY_DEBUG
35+
//#define MY_DEBUG_VERBOSE_CAN
36+
37+
// Enable and select radio type attached
38+
#define MY_CAN
39+
//#define MY_RADIO_NRF5_ESB
40+
//#define MY_RADIO_RFM69
41+
//#define MY_RADIO_RFM95
42+
43+
#include <MySensors.h>
44+
45+
#define SKETCH_NAME "Binary Sensor"
46+
#define SKETCH_MAJOR_VER "1"
47+
#define SKETCH_MINOR_VER "0"
48+
49+
#define SECONDARY_CHILD_ID 4
50+
51+
#define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
52+
53+
#if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
54+
#error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
55+
#endif
56+
57+
// Change to V_LIGHT if you use S_LIGHT in presentation below
58+
//MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
59+
MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
60+
61+
void setup()
62+
{
63+
// Setup the buttons
64+
pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
65+
}
66+
67+
void presentation()
68+
{
69+
// Send the sketch version information to the gateway and Controller
70+
sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
71+
72+
// Register binary input sensor to sensor_node (they will be created as child devices)
73+
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
74+
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
75+
present(SECONDARY_CHILD_ID, S_DOOR);
76+
}
77+
78+
// Loop will iterate on changes on the BUTTON_PINs
79+
void loop()
80+
{
81+
uint8_t value;
82+
static uint8_t sentValue2=2;
83+
84+
// Short delay to allow buttons to properly settle
85+
sleep(5);
86+
87+
value = digitalRead(SECONDARY_BUTTON_PIN);
88+
89+
if (value != sentValue2) {
90+
// Value has changed from last transmission, send the updated value
91+
send(msg2.set(value==HIGH));
92+
sentValue2 = value;
93+
}
94+
95+
// Sleep until something happens with the sensor
96+
sleep(SECONDARY_BUTTON_PIN-2, CHANGE, 0);
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2019 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*
19+
*******************************
20+
*
21+
* DESCRIPTION
22+
* The ArduinoGateway prints data received from sensors on the serial link.
23+
* The gateway accepts input on serial which will be sent out on radio network.
24+
*
25+
* The GW code is designed for Arduino Nano 328p / 16MHz
26+
*
27+
* Wire connections (OPTIONAL):
28+
* - Inclusion button should be connected between digital pin 3 and GND
29+
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
30+
*
31+
* LEDs (OPTIONAL):
32+
* - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
33+
* - RX (green) - blink fast on radio message received. In inclusion mode will blink fast only on presentation received
34+
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
35+
* - ERR (red) - fast blink on error during transmission error or receive crc error
36+
*
37+
*/
38+
39+
// Enable debug prints to serial monitor
40+
#define MY_DEBUG
41+
//#define MY_DEBUG_VERBOSE_CAN
42+
43+
44+
// Enable and select radio type attached
45+
#define MY_CAN
46+
//#define MY_RADIO_NRF5_ESB
47+
//#define MY_RADIO_RFM69
48+
//#define MY_RADIO_RFM95
49+
50+
// Set LOW transmit power level as default, if you have an amplified NRF-module and
51+
// power your radio separately with a good regulator you can turn up PA level.
52+
//#define MY_RF24_PA_LEVEL RF24_PA_LOW/
53+
54+
// Enable serial gateway
55+
#define MY_GATEWAY_SERIAL
56+
57+
// Define a lower baud rate for Arduinos running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
58+
//#if F_CPU == 8000000L/
59+
//#define MY_BAUD_RATE 38400/
60+
//#endif/
61+
62+
// Enable inclusion mode
63+
#define MY_INCLUSION_MODE_FEATURE
64+
// Enable Inclusion mode button on gateway
65+
//#define MY_INCLUSION_BUTTON_FEATURE
66+
67+
// Inverses behavior of inclusion button (if using external pullup)
68+
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
69+
70+
// Set inclusion mode duration (in seconds)
71+
//#define MY_INCLUSION_MODE_DURATION 60
72+
// Digital pin used for inclusion mode button
73+
//#define MY_INCLUSION_MODE_BUTTON_PIN 3
74+
75+
// Set blinking period
76+
//#define MY_DEFAULT_LED_BLINK_PERIOD 300
77+
78+
// Inverses the behavior of leds
79+
//#define MY_WITH_LEDS_BLINKING_INVERSE
80+
81+
// Flash leds on rx/tx/err
82+
// Uncomment to override default HW configurations
83+
//#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
84+
//#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
85+
//#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
86+
87+
#include <MySensors.h>
88+
89+
void setup()
90+
{
91+
// Setup locally attached sensors
92+
}
93+
94+
void presentation()
95+
{
96+
// Present locally attached sensors
97+
}
98+
99+
void loop()
100+
{
101+
// Send locally attached sensor data here
102+
}

0 commit comments

Comments
 (0)