Skip to content

malachi-iot/j1939

Repository files navigation

embr::J1939 C++ Library

j1939 logo

Welcome!

If you're new to CAN bus or J1939, check this out for some background

Why?

J1939 engenders compatibility and avoids re-inventing the wheel when using CAN bus.

What makes this particular library interesting? In a word: specialization.

We've gone all-in with C++ specialization to bring you a robust, intuitive & lean implementation. For example, setting up and sending a cab message 1 PDU is as easy as:

pdu<pgns::cm1> p(sa, da, null_t{});

p.requested_percent_fan_speed(speed);

transport_traits::send(t, p);

DBC files are useful, but don't always play well in constrained devices. With embr::j1939, compile-time PGN and SPN traits are available for interrogation, or to ignore completely - "only pay for what you use"

Features include:

  • J939-21 Network support:
    • Network address negotiation
    • Transport Protocol (1785b data field size)
  • Compile-time optimized units such as percentages, volts, kilometers, etc. by way of embr lib
  • Compile-time traits (metadata) for SPNs, PGNs including name, type, ownership, more
  • Accessors and mutators for SPN-defined payload fields
  • Lean and highly portable. No dynamic allocation.
  • c++11 compliant

Quick Start

Prerequisites:

  1. Initialize estd and embr via git submodule update --init --recursive
  2. Naturally you'll need a Can Transceiver

Quick Start: ESP-IDF

Example lighting command CA has the following features:

  • GPIO control of brake light and turn signals
  • automatic network address acquisition
  • Responds to OEL, CCVS commands and emits LCMD commands

Use idf.py menuconfig to specify CAN speed and TX/RX pins via embr config menu item. Full docs here

Quick Start: CMake

Stock standard CMake add_subdirectory works here for j1939 lib as well as depended-on can library.

For new targets, the can library needs transport and frame_traits. See new target guide

Quick Start: PlatformIO

I have yet to crack the nut to make platformio work smoothly with local libraries. Therefore, usage in this context is complicated. See test Arduino LCMD

Other Quick Snippets

Find more code snippets here

Infrastructure

Primary Targets

Supported:

Platform MCU Board
Arduino AVR Promicro (via external MCP2515)
Arduino M4 SAM Adafruit Feather CAN M4
ESP-IDF ESP32 Many

Secondary Targets

Although this library is tuned for embedded use, it compiles under GCC and Clang environments. Secondary targets include:

  • Linux (see catch unit testing area)
  • Qt/QML

Extras

Rough memory usage numbers

SLCAN firmware (USB-CAN bridge)

This implements the SLCAN (LAWICEL) protocol for ESP-IDF. Linux slcand happily speaks to this firmware. From there any SocketCAN tool is theoretically usable. Works with all CAN, not just J1939

  • Full read/write capability
  • Auto-poll (default) as well as legacy polled mode
  • Tested OK with Wireshark and Qt QCanBusDevice
  • Tested OK with ESP32C6 and ESP32S3

Find this under ESP32 SLCAN

Interesting Links & Special Thanks


Document v0.1