Summary

TL;DR 10 PCBs BPi CANBus (Thickness -> 1.6mm)

Using a Linux SBC with CAN interfaces to run model railroad. Idea: Gleisbox as Command Station (sorry, only in german)



This combination is a cheap starter for controlling model railroad. You can also running trains with mfx+, which isn't quite common on all command stations.
The used BananaPi is quite cheap and provides a CAN interface. All in all a perfect combination: Rocrail server and interface to Gleisbox/MS2. Many people are using the great RPi, but it's hard to setup a stable, no changing or loosing CAN packets interface. The combination of SPI and CAN has some drawbacks due to latency. The BPi doesn't have these problems with CAN.

I've tried to describe every step as easy as possible. But you should have some basic Linux knowledge before you start building this.

Used parts

- Gleisbox (60112, 60113 or 60116) / the MS2 is not necessary but can be used as well
- Banana Pi M1 (the first one)
- SDHC card (2GByte is enough)
- Netzteil Micro USB
- MCP2562 + 2x 100nF
or ISO1050 for galvanic isolation
- USB2Serial board (the cheap ones for two bucks)
- maybe connector from CdB Projekt

CAN-Bus interface

The CAN IP on BPi's SoC (Allwinner A20) includes a CAN controller - only a CAN transceiver is missing. Simply use a MCP2562 and connect CANH, CANL and GND with the Gleisbox:

The actual ready to use PCB - available at DirtyPCBs (please choose 1.6mm thickness and color you like) (10 +/-2 boards):


The PCB provides also some additional peripherals like S88, I2C and UART.

There are 4 boards in the batch, the Banana Pi daughter board (upper left, the large board), the trackbox board (the one with J5 in the upper right), and the two lower ones: bottom left= Distributor board: 3 x MiniDIN10 and 2x RJ45; bottom right = S88 adapter board.

PCB minimal assembly with MCP2562



BOM for CAN-Bus interface w/o galvanic isolation(w/o USB):
U1              MCP2562
R1              120 Ohm
R21,R22,R23     jump wires
C1,C2           100nF
P1              2x13 socket
P3              1x4 socket
P4              1x6 socket
P5              1x2 pin header
P12             1x5 3.5mm
P21             1x3 pin header
Minimal CAN connection:
CANH
CANL
GND

Alternate assembly with ISO1050 (preferred)



BOM for prefered CAN interface with galvanic isolation:
U12             ISO1050
R20             120 Ohm SMD 1205
C18,C19         100nF SMD 0805
P1              2x13 socket
P3              1x4 socket
P4              1x6 socket
P12             1x5 socket 3.5mm
P19             1x2 pin header with jumper
P21             1x3 pin header
# for external power supply from Gleisbox
C3,C4           100nF
U2              7805
# power indicator
D5              3mm LED
R12             330 - 470 Ohm (for 2mA LED 1.5 kOhm)
# alternate assembly
D4              LED SMD 0805
R11             330 - 470 Ohm SMD 0805
Don't assembly R21, R22 und R23 !

CAN connection needed:
CANH
CANL
GND
VCC

Option: Integrated USB-Serial adapter



USB to Serial BOM:
U13             FT230XS
D2, D3          LED red SMD 0805
R16, R17        27 Ohm SMD 0805
R18, R19        220 - 330 Ohm SMD 0805
C23, C24        47pF SMD 0805
C20, C25        100nF SMD 0805
C22             10nF SMD 0805
C26             4,7uF SMD 0805
F1              MI0805K601R-10
P13             USB-B Socket

Hint

Pin 10 and Pin 11 are shortened ;-) Solderbridges could be removed with solder wick.

Option: S88 with galvanic isolation



S88(N) BOM:
U3,U4,U5,U6     6N137
R2,R3,R4        270 Ohm
R5              390 Ohm
R6              330 Ohm
R7,R8,R9        1k5 Ohm
C5,C6,C7,C8     100nF
P6              1x6 screw socket 3,5 mm
You need to supply 5V externally.

OpenWRT Image

I've created a ready-to-use binary image: OpenWRT (hint for Firefox user: use the right mouse buton to download)
The images include all necessary parts to start:

- Init script for CAN interface
- can2lan
- Rocrail (not automatically started - could be changed if you want to)

For more advanced users there is also an Armbian Image. This image contains the necessary DTB files.

Prepare SDHC card

Please format your SDHC card if you have already used it. SDFormatter is a good tool for this task.
Decompress the image and copy it onto your memory card:
cd /tmp
wget http://lnxpps.de/bpi/bin/openwrt-sunxi-cortexa7-sun7i-a20-bananapi-ext4-sdcard.img.zip
unzip openwrt-sunxi-cortexa7-sun7i-a20-bananapi-ext4-sdcard.img.zip
sudo dd if=openwrt-sunxi-cortexa7-sun7i-a20-bananapi-ext4-sdcard.img of=/dev/sdX bs=1M # change X here
If you are MacOS user try ApplePi-Baker
Windows user may use Win32DiskImager.

Configure your BPi

Put the memory card into the BPi only in power off mode. Otherwise your the BPi could be damaged !
Use your 3V3 USB2Serial board to connet to your BPi:

                           GND TX RX
             J12   o  o  o  o  o  o
{SD slot}          o  o  o  o
You could do the first setup using network, but this is not recommend. If something goes wrong - and this might happen if you are going to change the network settings - you must reflash your memory card.

Using a WiFI adapter

Please connect your BPi with the client image to your existing network and change the WiFi configuration on http://<bpi-ip-address> to your needs. With the DHCP server image you can create a seperated acceess point. Connect a laptop or PC with BPIs LAN an do the WiFi setup point your internet browser to http://<router address>.
Don't connect your BPi with the DHCP server image to your local network, because the BPi might serve IP adresses faster than your existing router ! New connecting clients might get wrong IP address settings from the BPi.

Usage

Rocrail server is part of the image. If you want to use it you have to enter following commands once:
cp /root/rocrail /etc/init.d/
cp /root/rocnetnode /etc/init.d/
/etc/init.d/rocnetnode enable
/etc/init.d/rocnetnode start # don't wait for next boot - start now
/etc/init.d/rocrail enable
/etc/init.d/rocrail start # don't wait for next boot - start now

Extension(s)

LinkS88

Little bin for M*rklins Link S88: wake-up-links88
Usage: wake-up-links88 -i <can interface>
   Version 1.21

         -c <config_string>  config string "B1=1,T1=10,B2=3"
                             means: B1=1  -> bus 1 length one module
                                    T1=10 -> bus 1 cycle time 10ms
                                    B2=3  -> bus 2 length three modules
         -i <can int>        can interface - default can0
         -d                  daemonize
         -e #no_of_links88   exit after no of LinkS88 responded - default 1

Debugging

Some useful debugging commands:
# show CAN frames on console
candump -tA -xe can1,0:0,#FFFFFFFF
# or use alias
c0

# CAN interfaces statistics
# cs is the alias
ip -s -d link show can0
3: can0:  mtu 16 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen 10
    link/can  promiscuity 0 
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 
	  bitrate 250000 sample-point 0.875 
	  tq 250 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
	  sun4i_can: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
	  clock 24000000
	  re-started bus-errors arbit-lost error-warn error-pass bus-off
	  0          0          0          0          0          0         
    RX: bytes  packets  errors  dropped overrun mcast   
    34314      4399     0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    25259      4284     0       0       0       0       
# ERROR-ACTIVE is OK here

Background

Some basic infos - not only for the wizards

CON3 GPIOs


UARTS

      uart0: serial@01c28000
      uart1: serial@01c28400
      uart2: serial@01c28800
      uart3: serial@01c28c00
      uart4: serial@01c29000
      uart5: serial@01c29400
      uart6: serial@01c29800
      uart7: serial@01c29c00
Default:
ttyS0(UART0) -> Console
ttyS1(UART3)
ttyS2(UART7)
changed:
ttyS0(UART0) -> Console
ttyS1(UART2) - mit RTS/CTS
ttyS2(UART3)

Making your own image

Some infos about making your own OpenWRT image. Normally you don't need to follow these steps.
Base is OpenWRT, a Linux distribution optimized for routers. One big advantage: you could power off the BPi at any time without corrupting the file system.

Prepare OpenWRT

OpenWRT needs lots of disk space (>10 GByte). Installation:
sudo apt-get update
sudo apt-get install git build-essential
git clone git://git.openwrt.org/openwrt.git
cd openwrt
make menuconfig
make -j<number of cores>
The first compilation run needs hours - be patient ;-) The next run will be much faster.

OpenWRT Feeds init

Use the feed with
# change into OpenWRT dir
echo "src-git openwrtmisc https://github.com/GBert/openwrt-misc" >> feeds.conf.default
echo "src-git railroad https://github.com/GBert/railroad" >> feeds.conf.default
scripts/feeds update -a
scripts/feeds install kmod-sunxi-can
scripts/feeds install can-utils
scripts/feeds install can2udp
scripts/feeds install rocrail

Reduced to the max

Impressum: