NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
NMEA2000_CAN.h
Go to the documentation of this file.
1/*
2 * NMEA2000_CAN.h
3 *
4 * Copyright (c) 2015-2024 Timo Lappalainen, Kave Oy, www.kave.fi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22*/
23
24/***********************************************************************/
118#ifndef _NMEA2000_CAN_H_
119#define _NMEA2000_CAN_H_
120
121#include "N2kMsg.h"
122#include "NMEA2000.h"
123
124
125/*********************************************************************/
135#define USE_N2K_MCP_CAN 1
136
137/*********************************************************************/
142#define USE_N2K_DUE_CAN 2
143
144/*********************************************************************/
150#define USE_N2K_TEENSY_CAN 3
151
152/*********************************************************************/
157#define USE_N2K_AVR_CAN 4
158
159/*********************************************************************/
165#define USE_N2K_SOCKET_CAN 5
166
167/*********************************************************************/
172#define USE_N2K_MBED_CAN 6
173
174/*********************************************************************/
178#define USE_N2K_ESP32_CAN 7
179
180/*********************************************************************/
185#define USE_N2K_TEENSYX_CAN 8
186
187/*********************************************************************/
191#define USE_N2K_ARDUINO_CAN 9
192
193
194/***********************************************************************/
199// Select right CAN according to processor
200#if !defined(USE_N2K_CAN)
201#if defined(__MBED__) // Placing mbed 1st in tree, as the following CPUs can also be used in MBED IDE
202#define USE_N2K_CAN USE_N2K_MBED_CAN
203#elif defined(__SAM3X8E__)
204#define USE_N2K_CAN USE_N2K_DUE_CAN
205#elif defined(__ATMEGA32U4__)
206#define USE_N2K_CAN USE_N2K_TEENSY_CAN
207#elif defined(__MK20DX256__) || defined(__MK64FX512__) || defined (__MK66FX1M0__)
208#ifndef USE_NMEA2000_TEENSYX_FOR_TEENSY_3X
209 #define USE_N2K_CAN USE_N2K_TEENSY_CAN
210#else
211 #define USE_N2K_CAN USE_N2K_TEENSYX_CAN
212#endif
213#elif defined(__AVR_AT90CAN32__)||defined(__AVR_AT90CAN64__)||defined(__AVR_AT90CAN128__)|| \
214 defined(__AVR_ATmega32C1__)||defined(__AVR_ATmega64C1__)||defined(__AVR_ATmega16M1__)||defined(__AVR_ATmega32M1__)|| defined(__AVR_ATmega64M1__)
215#define USE_N2K_CAN USE_N2K_AVR_CAN
216#elif defined(__linux__)||defined(__linux)||defined(linux)
217#define USE_N2K_CAN USE_N2K_SOCKET_CAN
218#elif defined(ARDUINO_ARCH_ESP32) || defined(ESP32)
219#define USE_N2K_CAN USE_N2K_ESP32_CAN
220#elif defined(__IMXRT1062__)
221#define USE_N2K_CAN USE_N2K_TEENSYX_CAN
222#elif defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_MINIMA)
223#define USE_N2K_CAN USE_N2K_ARDUINO_CAN
224#else
225#define USE_N2K_CAN USE_N2K_MCP_CAN
226#endif
227#endif
228
229#if USE_N2K_CAN == USE_N2K_DUE_CAN
230// Use Arduino Due internal CAN with due_can library
231#include <NMEA2000_due.h>
232#ifndef NMEA2000_ARDUINO_DUE_CAN_BUS
233 #define NMEA2000_ARDUINO_DUE_CAN_BUS
234#endif
235tNMEA2000 &NMEA2000=*(new tNMEA2000_due(NMEA2000_ARDUINO_DUE_CAN_BUS));
236
237#elif USE_N2K_CAN == USE_N2K_TEENSY_CAN
238// Use Teensy 3.1/3.2/3.5/3.6 boards internal CAN FlexCAN library
239#include <NMEA2000_teensy.h> // https://github.com/ttlappalainen/NMEA2000_teensy
240#ifndef NMEA2000_TEENSY_VER
241#error Update NMEA2000_Teensy for the latest version!
242#endif
243#ifndef NMEA2000_TEENSY_DEF_TIMEOUT
244#define NMEA2000_TEENSY_DEF_TIMEOUT 4
245#endif
246#ifndef NMEA2000_TEENSY_CAN_BUS
247#define NMEA2000_TEENSY_CAN_BUS 0
248#endif
249tNMEA2000 &NMEA2000=*(new tNMEA2000_teensy(NMEA2000_TEENSY_DEF_TIMEOUT,NMEA2000_TEENSY_CAN_BUS));
250
251#elif USE_N2K_CAN == USE_N2K_TEENSYX_CAN
252// Use Teensy 3.1/3.2/3.5/3.6/4.0/4.1 boards internal CAN
253#include <NMEA2000_Teensyx.h> // https://github.com/ttlappalainen/NMEA2000_Teensyx
254tNMEA2000 &NMEA2000=*(new tNMEA2000_Teensyx(NMEA2000_TEENSYX_CAN_BUS,NMEA2000_TEENSYX_TX_PIN,NMEA2000_TEENSYX_RX_PIN));
255
256#elif USE_N2K_CAN == USE_N2K_AVR_CAN
257// Use Atmel AVR internal CAN controller with avr_can library
258#include <avr_can.h> // https://github.com/thomasonw/avr_can
259#include <NMEA2000_avr.h> // https://github.com/thomasonw/NMEA2000_avr
260tNMEA2000 &NMEA2000=*(new tNMEA2000_avr());
261
262#elif USE_N2K_CAN == USE_N2K_SOCKET_CAN
263// Use socketCAN devices
264#ifndef SOCKET_CAN_PORT
265 #define SOCKET_CAN_PORT NULL
266 #endif
267#include <NMEA2000_SocketCAN.h> // https://github.com/thomasonw/NMEA2000_socketCAN
268tNMEA2000 &NMEA2000=*(new tNMEA2000_SocketCAN(SOCKET_CAN_PORT));
269
270#elif USE_N2K_CAN == USE_N2K_MBED_CAN
271// Use MBED devices
272#include <NMEA2000_mbed.h> // https://github.com/thomasonw/NMEA2000_mbed
273tNMEA2000 &NMEA2000=*(new tNMEA2000_mbed());
274tmbedStream serStream;
275
276#elif USE_N2K_CAN == USE_N2K_ESP32_CAN
277#include <NMEA2000_esp32.h> // https://github.com/ttlappalainen/NMEA2000_esp32
278tNMEA2000 &NMEA2000=*(new tNMEA2000_esp32());
279
280#elif USE_N2K_CAN == USE_N2K_ARDUINO_CAN
281#include <NMEA2000_ArduinoCAN.h>
282tNMEA2000 &NMEA2000=*(new tNMEA2000_ArduinoCAN());
283
284#else // Use USE_N2K_MCP_CAN
285// Use mcp_can library e.g. with Arduino Mega and external MCP2551 CAN bus chip
286// CAN_BUS_shield libraries will be originally found on https://github.com/Seeed-Studio/CAN_BUS_Shield
287// There is improved library, which branch can be found on https://github.com/peppeve/CAN_BUS_Shield
288// That works also with Maple mini and 8 MHz clock. Hopefully these improvements will be applied to
289// original library
290
291#if defined(__STM32F1__) // Maple
292#include <MapleIntCompatibility.h>
293#endif
294
295#include <SPI.h>
296#include <mcp_can.h> // https://github.com/ttlappalainen/CAN_BUS_Shield
297
298#if !defined(N2k_CAN_INT_PIN)
299
300/*********************************************************************/
305#define N2k_CAN_INT_PIN 0xff // No interrupt.
306
307/*********************************************************************/
312#ifndef MCP_CAN_RX_BUFFER_SIZE
313#define MCP_CAN_RX_BUFFER_SIZE 1 // Just small buffer to save memory
314#endif
315#endif
316
317#include <NMEA2000_mcp.h>
318#if !defined(N2k_SPI_CS_PIN)
319
320/*********************************************************************/
325#define N2k_SPI_CS_PIN 53 // Pin for SPI Can Select
326#endif
327
328#if !defined(USE_MCP_CAN_CLOCK_SET)
329
330/*********************************************************************/
333#define USE_MCP_CAN_CLOCK_SET 16
334#endif
335
336#if USE_MCP_CAN_CLOCK_SET == 8
337
338/*********************************************************************/
341#define MCP_CAN_CLOCK_SET MCP_8MHz
342#else
343
344/*********************************************************************/
347#define MCP_CAN_CLOCK_SET MCP_16MHz
348#endif
349
351
352#endif
353
354
355#endif
356
This File contains the class tN2kMsg and all necessary functions to handle a NMEA2000 Message.
This file contains the class tNMEA2000, which consists the main functionality of the library.
#define N2k_CAN_INT_PIN
Definition of the CAN Interrupt Pin This defines the number of the CAN Interrupt PIN....
Definition: NMEA2000_CAN.h:305
#define N2k_SPI_CS_PIN
Definition of the SPI ChipSelectPin for the CAN Transceiver This defines the the I/O Pin uses for the...
Definition: NMEA2000_CAN.h:325
#define MCP_CAN_CLOCK_SET
The Clock frequency for the MCP CAN is 16MHz.
Definition: NMEA2000_CAN.h:347
#define MCP_CAN_RX_BUFFER_SIZE
Definition of the MCP RX Buffer Size Value of 1 is a small buffer size to save memory.
Definition: NMEA2000_CAN.h:313
tNMEA2000 & NMEA2000
Definition: NMEA2000_CAN.h:350
tNMEA2000 device class definition.
Definition: NMEA2000.h:138