User Tools

Site Tools


arduino:rcan-en

RCAN


img_0511.jpgcan-extframe.jpegrcan.jpg

Introduction

The current CAN-GC* hardware serie is based on MC PICs.
RCAN is firmware for CAN based solutions on the Arduino platform.
The firmware target is the Arduino Nano ATmega328PB.


Dependencies

The only not standard Arduino library used is

to be able to communicate with the MCP2515 chip.

Modifications

Two modifications in the library source code are to disable DEBUG to save memory, and increase the timeout value in the mcp_can_dfs.h: (line 40 and 45)

// if print debug information
//#define DEBUG_MODE 1
 
/*
 *   Begin mt
 */
#define TIMEOUTVALUE    100

The original TIMEOUTVALUE of 50 is too low and brings errors on sending frames in a CAN bus. (Depends on how much traffic is on the CAN bus.)
It is very important to disable DEBUG_MODE otherwise there is not enough memory available. The Arduino IDE will complain about this, and RCAN will reboot continuously.

GPIO Board

The difference between the standard PIC based and Arduino boards is the FLAT addressing scheme, and only support long events. (Long/Short events are detected in Rocrail automatically.)

The GPIO is compatible with the CANGC2. (Except of the addressing scheme.)

Nano's with the ATmega328P cannot use port A6 and A7 as digital I/O, and are only available as input with analogRead.


Input only pins on ATmega328P

Nano Pin GC2a ArduCAN-IO
A6 10 2
A7 9 1

Variables

Variable Nr Description
0 SYNCID
1 Node configuration byte
2…17 Port configuration bytes
18…19 Port state bytes (Read only)
20 CANID

Node configuration byte

Bit Value Function Remark
0 1 Save state Save output states in EEPROM on a power off command.
1 1 Short events Not used.
2,3 0,0 250ms pulse time
2,3 1,0 500ms pulse time
2,3 0,1 1000ms pulse time
2,3 1,1 2000ms pulse time
4 1 Report all on SoD Report also low input values instead of only high.

Port configuration byte

Bit Value Function Remark
0 1 Input Initialise the port as input with internal pull-up resistor.
The GC2a board will also provide 10kOhm resister arrays because the internal pull-up value is too high.
0 0 Output Initialise the port as output.
1 1 Input off delay Input 2 seconds off delay for occupancy detectors.
1 1 Output pulse Pulse length according the global node configuration.
2 1 Invert Invert the digital port value.
3 1 IR port Not used.
4 1 Sync Synchronise output also with the SYNCID


CBus

CBUS→RCAN Rocrail Range Remark
Event node number → CANID Bus 0…127 (default 127) CBUS uses the 4 MSBits for priority. (The standard CAN Frame ID is 11 bit.)
Event address → Port Address 1…16 (fixed) This is the Port Number.

Module node number and module CANID are the same and just one value in the firmware.
The CBUS learning mode is not supported and not needed.

The same flat addressing scheme can be used with the PIC based boards by setting the event node numbers to the modules node number and the event addresses to the corresponding port number, and disable the short event option.
In case the RCAN based modules are planned to be used in an existing CBUS network, it is recommended to convert the existing modules to the flat addressing scheme to make it transparent.


MBus

Support for Märklin CAN protocol.

  • Inputs are reported as s88 events. (0x11)
  • Outputs react on 0x0B with MM 0x3nnn. The dlc 8 is used as bus/address.
  • Read/Write config with 0x07 and 0x08.
  • Ping is also supported as query in the CBus Node Dialog.


ZBus

WIP

Support for Zimo CAN protocol. (Z21)

Frame

Ext. CAN Frame
ID F DLC 8 Data
F GRP CMD MODE SID CF DLC RID DATA ZCAN usage
1 4 6 2 16 4 4 16 48 Nr of bits
  • F = ZCAN Flag
  • GRP = Group
  • CMD = Command
  • MODE = request, command, event or ack
  • SID = Sender ID (Must be unique to be able to resolve collisions)
  • CF = CAN Flags
  • DLC = Number of data bytes
  • RID = Recipient ID
  • DATA = Optional data



Serial Monitor

500000bps

Command Description
dump Shows the EEPROM first 64 bytes
sod Start of Day
init Re-init all I/O
stat Shows the I/O state
pub Publish board identification and type
loop Set the loopback mode.
norm Set the normal mode. (default)
cbus Switch to CBUS protocol. (125k) (default)
mbus Switch to Märklin CS2 protocol. (250k)
zbus Switch to Zimo CAN protocol. (125k)
8m Set MCP2515 clock speed to 8MHz. (default)
16m Set MCP2515 clock speed to 16MHz.
l0 Set pin layout to fit the CANGC2a. (default) → Push the reset button to make changes active.
l1 Set pin layout to fit the ArduCAN-I/O. → Push the reset button to make changes active.
=<id> Set the CANID. Replace <id> with a number between 1 and 127.
#<port><config> Configure a port. Replace <port> with a character between 0…9 and a…f.
The <config> maybe: 0 for output, 1 for input, 2 for input with off delay, 3 for pulse output.
Example: "#a1" will configure port 10 as input.
:SxxxxNxx…xx; ASCII frame which will be send on the CAN bus and evaluated by the board. (Gridconnect scheme as used by MERG CBUS.)
See chapter 10 of the MERG "Developer’s Guide for CBUS".

Also all CAN traffic and supported OPCs are traced in the serial monitor.

Loopback Mode

The original text is found in the Microchip MCP2515 manual in section 10.4.
Loopback mode will allow internal transmission of messages from the transmit buffers to the receive buffers without actually transmitting messages on the CAN bus.
This mode can be used in system development and testing.
In this mode, the ACK bit is ignored and the device will allow incoming messages from itself, just as if they were coming from another node.
The Loopback mode is a silent mode, meaning no messages will be trans- mitted while in this state (including error flags or Acknowledge signals). The TXCAN pin will be in a recessive state.

Monitor output example

GPIO board
MBus 0x3017
__AVR_ATmega328P__
pinlayout=0
port 00(02) set INPUT_PULLUP
port 01(03) set INPUT_PULLUP
port 02(04) set INPUT_PULLUP
port 03(05) set INPUT_PULLUP
port 04(06) set OUTPUT
port 05(07) set OUTPUT
port 06(08) set OUTPUT
port 07(09) set OUTPUT
port 08(15) set INPUT_PULLUP
port 09(14) set INPUT_PULLUP
port 10(13) set INPUT_PULLUP
port 11(12) set INPUT_PULLUP
port 12(11) set INPUT_PULLUP
port 13(10) set INPUT_PULLUP
port 14(0F) set INPUT_PULLUP
port 15(0E) set INPUT_PULLUP
CAN bps=250, MCP2515 8MHz
:X00310327N000030170300460C;
change of port 12 0->1 conf=1 timer=0
:X00230327N0017000C00010000


Setup


The CBUS-IO will report itself as GC2a, module type 12.
The firmware version is only visible with the SocketCAN and GC2a interface.


CBUS Protocol

The CBUS protocol dictates the number of data bytes for all OPC ranges.
This makes no sense for CAN, because the DLC field is responsible for the number of data bytes used in a message.
The CBUS protocol seems to reflect the LocoNet scheme…
The OPC_PNN, a response on a query modules OPC_QNN, will not set the DLC to 6 but to 8. The last two bytes will contain the firmware version.
Officially the OPC_PNN is in the range of 6 data bytes.

Incompatibilities

  • SLiM is not supported
  • FLiM is not supported
  • Learn mode is not supported
  • The ‘Device Addressed’ model; This is fixed: NN → CANID, DN → Port 1…16
  • The push button will not request a node number by OPC_RQNN, but will publish itself by OPC_PNN

Drawbacks

With the FLAT addressing it is not possible to activate outputs with one command, NN+DN, on multiple boards(nodes).
This option is not or only hardly used, and makes the setup of boards needlessly very complex.
Tip: With the SYNCID the outputs can be synchronised with other IDs.


Push Button

The push button on the Arduino keeps its original function: Reboot.
After every reboot an OPC_PNN will be send to popup the CBusNode dialog in Rocview with selected node.
This feature can be used to identify a node.


GC2a as USB Interface

The GC2a can be used parallel as CBus/MBus interface.
It uses the same ASCII framing as the CANGC1(e) interface. (See chapter 10 of the MERG “Developer’s Guide for CBUS”.)

Line setting must be set to:

  • USB
  • 500000bps (This is set automatically in case of USB type.)
  • No hardware handshake (RTS/CTS)
  • Linefeed

With this option, the GC2a can also be used standalone; The minimal CBUS setup.

Note: Because the ACK slot bit will not be set by other CAN modules, a send error will be reported, but can be ignored in standalone mode.


SyncID

The SyncID option is added to be able to synchronise input event from board X with output states on board Y.
The port mapping is 1:1.
It can also be use to switch multiple outputs with a single Rocrail command.

Example

On board with ID 33 is input port switched to ON.
Another board with ID 22 hast set SyncID to 33, and will check if the corresponding port is an output and will synchronise it with the input port of board 33.
This situation is often used for switch panels.


Source Code

Documentation

Schedule and PCB

arduino/rcan-en.txt · Last modified: 2018/11/12 08:56 (external edit)