-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCP-CAN.py
308 lines (260 loc) · 9.59 KB
/
TCP-CAN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# TCP - CAN bus hub
# Copyright (c) 2025 George Helffrich
# Released under the MIT License (MIT) - see LICENSE file
# CPU: Raspberry Pi pico W
# CAN device: RB-P-CAN-RS485 board by Joy-IT (www.joy-it.net)
# Runs with:
# MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico W with RP2040
# 16 Jan. 2025
# last revision 24 Jan. 2025
VER = 'AN245' # version ID
SSID = "****"
PASS = "****"
CS2_PORT = 15731 # Marklin diktat
CS2_SIZE = 13 # Fixed by protocol definition
QSIZE = 25 # Size of various I/O queues (overkill)
import uasyncio as asyncio
import network
import sys
from time import sleep
from micropython import alloc_emergency_exception_buf as AEEB
AEEB(100) # boilerplate for IRQ-level exception reporting
from machine import Pin
pico_led = Pin("LED", Pin.OUT)
class iCAN: # interrupt driven CAN message sniffer
from canbus import Can, CanError
from canbus.internal import CAN_SPEED
from machine import Pin, SPI
prep = SPI(0, # configure SPI to use correct pins
sck=Pin(18), mosi=Pin(19), miso=Pin(16)
)
def __init__(self):
from machine import Pin
from canbus import Can, CanError
from canbus.internal import CAN_SPEED
self.flag = asyncio.ThreadSafeFlag()
self.pin = Pin(15,Pin.IN,Pin.PULL_UP)
self.pin.irq( # this defines the interrupt handler (lambda)
trigger=Pin.IRQ_FALLING,
handler=lambda pin: self.flag.set(),
hard=True)
self.can = Can()
# Initialize the CAN interface. Reference says 250 kbps speed.
ret = self.can.begin(bitrate=CAN_SPEED.CAN_250KBPS)
if ret != CanError.ERROR_OK:
raise RuntimeError('Error initializing CAN bus')
print("CAN initialized successfully, waiting for traffic.")
def intf(self): # hardware interface level access if needed
return self.can
def run(self, proc):
# Method never returns; calls proc upon every interrupt.
# Usage:
# proc(msg, err)
# with
# msg - Can message
# err - True if read error; False if OK
# no return value
task = asyncio.create_task(self.intr(proc))
Loop.run_until_complete(task)
async def intr(self, proc):
# Handle CAN card interrupt.
# Response to any interrupt is implemented via a callback method
# rather than as a generator (using e.g. async for x in iCan(): ...)
# because the interrupt response appears to be faster. This is
# due to the checkReceive() loop in the handler itself.
from canbus import CanError
await asyncio.sleep(0)
while True:
await self.flag.wait()
intr = self.can.getInterrupts()
if intr & 0x03 == 0:
# ignore interrupts except for reading
self.can.clearInterrupts()
continue
while self.can.checkReceive():
# CAN error code and message
error, msg = self.can.recv()
proc(msg, error != CanError.ERROR_OK)
await asyncio.sleep(0)
self.can.clearInterrupts()
def send(self, ID=None, data=None, EFF=False, RTR=False):
# Send a CAN message.
# Called with:
# ID - (integer) CAN id
# data - (bytearray) data payload
# EFF - set EFF flag in frame for long IDs
# RTR - set RTR flag in frame
# returns
# True if send error, False if sent OK
from canbus import CanMsg, CanMsgFlag, CanError
cflg = CanMsgFlag.EFF if EFF else 0
cflg |= CanMsgFlag.RTR if RTR else 0
msg = CanMsg(can_id=ID, data=data, flags=cflg)
return self.can.send(msg) != CanError.ERROR_OK
def stop(self):
# Turns off CAN interrupt handling, disabling board for reading.
self.pin.irq(handler=None)
from threadsafe import ThreadSafeQueue
CANtoTCP = ThreadSafeQueue(QSIZE*[bytearray(CS2_SIZE)])
TCPtoCAN = ThreadSafeQueue(QSIZE*[bytearray(CS2_SIZE)])
debugQUE = ThreadSafeQueue(QSIZE*[bytearray(CS2_SIZE)])
TCP_R, TCP_W = None, None
TCP_RERR, TCP_WERR = False, False
rrhash = 0
async def TCP_SERVER(R, W):
# Callback when RocRail client connects to us
global TCP_R, TCP_W, TCP_RERR, TCP_WERR
print('TCP connection made, waiting for traffic.')
TCP_R, TCP_W = R, W
while True:
await asyncio.sleep(15)
if TCP_RERR and TCP_WERR:
# Must be a disconnect; finish and wait for a new connection
print('*** TCP connection lost, waiting for reconnect.')
break
async def TCP_READER():
# packet layout:
# xx xx xx xx xx xx xx xx xx xx xx xx xx - 13 bytes total = CS2_SIZE
# ----------- -- -----------------------
# CAN ID len data (left justified)
global TCP_R, TCP_RERR, rrhash
while True: # Wait for connection
if TCP_R is None:
await asyncio.sleep_ms(10)
continue
try: # Serve it
pkt = await TCP_R.readexactly(CS2_SIZE)
assert len(pkt) == CS2_SIZE
rrhash = int.from_bytes(pkt[2:4])
await TCPtoCAN.put(pkt)
await debugQUE.put(pkt)
except EOFError: # Connection lost/client disconnect
TCP_RERR, TCP_R = True, None
continue
async def CAN_READER():
global can
can = iCAN()
can.run(CAN_IN)
def CAN_IN(msg, err, buf=bytearray(CS2_SIZE)):
if err:
stat = can.intf().getStatus() # Order matters: status first ...
intr = can.intf().getInterrupts() # ...then interrupt reg
errf = can.intf().getErrorFlags()
print(' >>>CAN read error<<< stat %02x intr %02x err %02x' %
(stat,intr,errf)
)
return
buf[0] = msg.can_id >> 24 & 0xff
buf[1] = msg.can_id >> 16 & 0xff
buf[2] = msg.can_id >> 8 & 0xff
buf[3] = msg.can_id & 0xff
buf[4] = msg.dlc
buf[5:14] = 8*b'0'
for i in range(msg.dlc): buf[5 + i] = msg.data[i]
try:
CANtoTCP.put_sync(buf) # put_sync because no await used
except:
print('***CANtoTCP q full')
try:
debugQUE.put_sync(buf) # pub_sync because no await used
except:
print('***debug q full')
async def TCP_WRITER():
global TCP_W, TCP_WERR
while True: # Wait for connection
if TCP_W is None:
await asyncio.sleep_ms(10)
continue
try: # Serve it
async for pkt in CANtoTCP:
assert len(pkt) == CS2_SIZE
TCP_W.write(pkt)
TCP_W.drain()
except OSError: # Connection lost/client disconnect
TCP_WERR, TCP_W = True, None
async def CAN_WRITER(MERR=5, MCNT=500):
global can
async for pkt in TCPtoCAN:
assert len(pkt) == CS2_SIZE
cnt = 0
while can.send(
ID=int.from_bytes(pkt[0:4]),
data=pkt[5:5+int(pkt[4])],
EFF=True
):
cnt += 1
errf = can.intf().getErrorFlags() # Error Flag register
if cnt <= MERR:
print(' >>>CAN write error<<< (err %02x)%s' %
(errf, ' - quelling further reports' if cnt >= MERR else '')
)
if errf & 0x30: # TXBO/Bus-Off or TXEP/TX-Passive
can.intf().clearErrorFlags(MERR=True)
asyncio.sleep_ms(10)
if cnt > MCNT: # Abandon packet after this many tries
break
async def DEBUG_OUT():
global rrhash, avail
async for buf in debugQUE:
assert len(buf) == CS2_SIZE
data = ' '.join( # put space between every octet
map(''.join, zip(*[iter(buf.hex())]*2))
)
cid = int.from_bytes(buf[2:4])
if cid == rrhash:
print('TCP -> CAN %s' % data)
else:
print('CAN -> TCP %s' % data)
if avail:
print(' %s' % decode(
int.from_bytes(buf[0:4]), buf[5:5+int(buf[4])], detail=True
)
)
if TCPtoCAN.qsize() > 0 and TCPtoCAN.qsize() % 5 == 0:
print('TCPtoCAN queue congestion: %d waiting' % TCPtoCAN.qsize())
if CANtoTCP.qsize() > 0 and CANtoTCP.qsize() % 5 == 0:
print('CANtoTCP queue congestion: %d waiting' % CANtoTCP.qsize())
async def HEARTBEAT():
# Slow LED flash heartbeat while running; boot button restarts.
global pico_led
while True:
if rp2.bootsel_button() == 1: sys.exit()
pico_led.on()
await asyncio.sleep(1)
pico_led.off()
await asyncio.sleep(1)
from asyncio import Loop
print('TCP <-> CAN packet hub (%s)' % VER)
try:
from marklin import decode # Marklin CS2 CAN packet decoder
avail = True
except:
avail = False # don't use it if not available
print('No Märklin packet decoding, only logging raw data.')
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
wlan.active(True)
mac = wlan.config('mac')
host = 'CS2hub-' + ''.join('{:02x}'.format(b) for b in mac[3:])
wlan.config(hostname = host)
wlan.connect(SSID, PASS)
else:
host = wlan.config('hostname')
while not wlan.isconnected():
# Fast flash while waiting for wifi connection; boot button restarts.
if rp2.bootsel_button() == 1: sys.exit()
pico_led.on()
sleep(0.25)
pico_led.off()
sleep(0.25)
pico_led.on()
ip = wlan.ifconfig()[0]
print('Available at {} as {}, port {:d}.'.format(ip,host,CS2_PORT))
canr = asyncio.create_task(CAN_READER())
tcpr = asyncio.create_task(TCP_READER())
tcpw = asyncio.create_task(TCP_WRITER())
canw = asyncio.create_task(CAN_WRITER())
dbug = asyncio.create_task(DEBUG_OUT())
beat = asyncio.create_task(HEARTBEAT())
Loop.run_until_complete(asyncio.start_server(TCP_SERVER,ip,CS2_PORT))