1*43da2f07SMax Staudt.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2*43da2f07SMax Staudt 3*43da2f07SMax Staudtcan327: ELM327 driver for Linux SocketCAN 4*43da2f07SMax Staudt========================================== 5*43da2f07SMax Staudt 6*43da2f07SMax StaudtAuthors 7*43da2f07SMax Staudt-------- 8*43da2f07SMax Staudt 9*43da2f07SMax StaudtMax Staudt <max@enpas.org> 10*43da2f07SMax Staudt 11*43da2f07SMax Staudt 12*43da2f07SMax Staudt 13*43da2f07SMax StaudtMotivation 14*43da2f07SMax Staudt----------- 15*43da2f07SMax Staudt 16*43da2f07SMax StaudtThis driver aims to lower the initial cost for hackers interested in 17*43da2f07SMax Staudtworking with CAN buses. 18*43da2f07SMax Staudt 19*43da2f07SMax StaudtCAN adapters are expensive, few, and far between. 20*43da2f07SMax StaudtELM327 interfaces are cheap and plentiful. 21*43da2f07SMax StaudtLet's use ELM327s as CAN adapters. 22*43da2f07SMax Staudt 23*43da2f07SMax Staudt 24*43da2f07SMax Staudt 25*43da2f07SMax StaudtIntroduction 26*43da2f07SMax Staudt------------- 27*43da2f07SMax Staudt 28*43da2f07SMax StaudtThis driver is an effort to turn abundant ELM327 based OBD interfaces 29*43da2f07SMax Staudtinto full fledged (as far as possible) CAN interfaces. 30*43da2f07SMax Staudt 31*43da2f07SMax StaudtSince the ELM327 was never meant to be a stand alone CAN controller, 32*43da2f07SMax Staudtthe driver has to switch between its modes as quickly as possible in 33*43da2f07SMax Staudtorder to fake full-duplex operation. 34*43da2f07SMax Staudt 35*43da2f07SMax StaudtAs such, can327 is a best effort driver. However, this is more than 36*43da2f07SMax Staudtenough to implement simple request-response protocols (such as OBD II), 37*43da2f07SMax Staudtand to monitor broadcast messages on a bus (such as in a vehicle). 38*43da2f07SMax Staudt 39*43da2f07SMax StaudtMost ELM327s come as nondescript serial devices, attached via USB or 40*43da2f07SMax StaudtBluetooth. The driver cannot recognize them by itself, and as such it 41*43da2f07SMax Staudtis up to the user to attach it in form of a TTY line discipline 42*43da2f07SMax Staudt(similar to PPP, SLIP, slcan, ...). 43*43da2f07SMax Staudt 44*43da2f07SMax StaudtThis driver is meant for ELM327 versions 1.4b and up, see below for 45*43da2f07SMax Staudtknown limitations in older controllers and clones. 46*43da2f07SMax Staudt 47*43da2f07SMax Staudt 48*43da2f07SMax Staudt 49*43da2f07SMax StaudtData sheet 50*43da2f07SMax Staudt----------- 51*43da2f07SMax Staudt 52*43da2f07SMax StaudtThe official data sheets can be found at ELM electronics' home page: 53*43da2f07SMax Staudt 54*43da2f07SMax Staudt https://www.elmelectronics.com/ 55*43da2f07SMax Staudt 56*43da2f07SMax Staudt 57*43da2f07SMax Staudt 58*43da2f07SMax StaudtHow to attach the line discipline 59*43da2f07SMax Staudt---------------------------------- 60*43da2f07SMax Staudt 61*43da2f07SMax StaudtEvery ELM327 chip is factory programmed to operate at a serial setting 62*43da2f07SMax Staudtof 38400 baud/s, 8 data bits, no parity, 1 stopbit. 63*43da2f07SMax Staudt 64*43da2f07SMax StaudtIf you have kept this default configuration, the line discipline can 65*43da2f07SMax Staudtbe attached on a command prompt as follows:: 66*43da2f07SMax Staudt 67*43da2f07SMax Staudt sudo ldattach \ 68*43da2f07SMax Staudt --debug \ 69*43da2f07SMax Staudt --speed 38400 \ 70*43da2f07SMax Staudt --eightbits \ 71*43da2f07SMax Staudt --noparity \ 72*43da2f07SMax Staudt --onestopbit \ 73*43da2f07SMax Staudt --iflag -ICRNL,INLCR,-IXOFF \ 74*43da2f07SMax Staudt 30 \ 75*43da2f07SMax Staudt /dev/ttyUSB0 76*43da2f07SMax Staudt 77*43da2f07SMax StaudtTo change the ELM327's serial settings, please refer to its data 78*43da2f07SMax Staudtsheet. This needs to be done before attaching the line discipline. 79*43da2f07SMax Staudt 80*43da2f07SMax StaudtOnce the ldisc is attached, the CAN interface starts out unconfigured. 81*43da2f07SMax StaudtSet the speed before starting it:: 82*43da2f07SMax Staudt 83*43da2f07SMax Staudt # The interface needs to be down to change parameters 84*43da2f07SMax Staudt sudo ip link set can0 down 85*43da2f07SMax Staudt sudo ip link set can0 type can bitrate 500000 86*43da2f07SMax Staudt sudo ip link set can0 up 87*43da2f07SMax Staudt 88*43da2f07SMax Staudt500000 bit/s is a common rate for OBD-II diagnostics. 89*43da2f07SMax StaudtIf you're connecting straight to a car's OBD port, this is the speed 90*43da2f07SMax Staudtthat most cars (but not all!) expect. 91*43da2f07SMax Staudt 92*43da2f07SMax StaudtAfter this, you can set out as usual with candump, cansniffer, etc. 93*43da2f07SMax Staudt 94*43da2f07SMax Staudt 95*43da2f07SMax Staudt 96*43da2f07SMax StaudtHow to check the controller version 97*43da2f07SMax Staudt------------------------------------ 98*43da2f07SMax Staudt 99*43da2f07SMax StaudtUse a terminal program to attach to the controller. 100*43da2f07SMax Staudt 101*43da2f07SMax StaudtAfter issuing the "``AT WS``" command, the controller will respond with 102*43da2f07SMax Staudtits version:: 103*43da2f07SMax Staudt 104*43da2f07SMax Staudt >AT WS 105*43da2f07SMax Staudt 106*43da2f07SMax Staudt 107*43da2f07SMax Staudt ELM327 v1.4b 108*43da2f07SMax Staudt 109*43da2f07SMax Staudt > 110*43da2f07SMax Staudt 111*43da2f07SMax StaudtNote that clones may claim to be any version they like. 112*43da2f07SMax StaudtIt is not indicative of their actual feature set. 113*43da2f07SMax Staudt 114*43da2f07SMax Staudt 115*43da2f07SMax Staudt 116*43da2f07SMax Staudt 117*43da2f07SMax StaudtCommunication example 118*43da2f07SMax Staudt---------------------- 119*43da2f07SMax Staudt 120*43da2f07SMax StaudtThis is a short and incomplete introduction on how to talk to an ELM327. 121*43da2f07SMax StaudtIt is here to guide understanding of the controller's and the driver's 122*43da2f07SMax Staudtlimitation (listed below) as well as manual testing. 123*43da2f07SMax Staudt 124*43da2f07SMax Staudt 125*43da2f07SMax StaudtThe ELM327 has two modes: 126*43da2f07SMax Staudt 127*43da2f07SMax Staudt- Command mode 128*43da2f07SMax Staudt- Reception mode 129*43da2f07SMax Staudt 130*43da2f07SMax StaudtIn command mode, it expects one command per line, terminated by CR. 131*43da2f07SMax StaudtBy default, the prompt is a "``>``", after which a command can be 132*43da2f07SMax Staudtentered:: 133*43da2f07SMax Staudt 134*43da2f07SMax Staudt >ATE1 135*43da2f07SMax Staudt OK 136*43da2f07SMax Staudt > 137*43da2f07SMax Staudt 138*43da2f07SMax StaudtThe init script in the driver switches off several configuration options 139*43da2f07SMax Staudtthat are only meaningful in the original OBD scenario the chip is meant 140*43da2f07SMax Staudtfor, and are actually a hindrance for can327. 141*43da2f07SMax Staudt 142*43da2f07SMax Staudt 143*43da2f07SMax StaudtWhen a command is not recognized, such as by an older version of the 144*43da2f07SMax StaudtELM327, a question mark is printed as a response instead of OK:: 145*43da2f07SMax Staudt 146*43da2f07SMax Staudt >ATUNKNOWN 147*43da2f07SMax Staudt ? 148*43da2f07SMax Staudt > 149*43da2f07SMax Staudt 150*43da2f07SMax StaudtAt present, can327 does not evaluate this response. See the section 151*43da2f07SMax Staudtbelow on known limitations for details. 152*43da2f07SMax Staudt 153*43da2f07SMax Staudt 154*43da2f07SMax StaudtWhen a CAN frame is to be sent, the target address is configured, after 155*43da2f07SMax Staudtwhich the frame is sent as a command that consists of the data's hex 156*43da2f07SMax Staudtdump:: 157*43da2f07SMax Staudt 158*43da2f07SMax Staudt >ATSH123 159*43da2f07SMax Staudt OK 160*43da2f07SMax Staudt >DEADBEEF12345678 161*43da2f07SMax Staudt OK 162*43da2f07SMax Staudt > 163*43da2f07SMax Staudt 164*43da2f07SMax StaudtThe above interaction sends the SFF frame "``DE AD BE EF 12 34 56 78``" 165*43da2f07SMax Staudtwith (11 bit) CAN ID ``0x123``. 166*43da2f07SMax StaudtFor this to function, the controller must be configured for SFF sending 167*43da2f07SMax Staudtmode (using "``AT PB``", see code or datasheet). 168*43da2f07SMax Staudt 169*43da2f07SMax Staudt 170*43da2f07SMax StaudtOnce a frame has been sent and wait-for-reply mode is on (``ATR1``, 171*43da2f07SMax Staudtconfigured on ``listen-only=off``), or when the reply timeout expires 172*43da2f07SMax Staudtand the driver sets the controller into monitoring mode (``ATMA``), 173*43da2f07SMax Staudtthe ELM327 will send one line for each received CAN frame, consisting 174*43da2f07SMax Staudtof CAN ID, DLC, and data:: 175*43da2f07SMax Staudt 176*43da2f07SMax Staudt 123 8 DEADBEEF12345678 177*43da2f07SMax Staudt 178*43da2f07SMax StaudtFor EFF (29 bit) CAN frames, the address format is slightly different, 179*43da2f07SMax Staudtwhich can327 uses to tell the two apart:: 180*43da2f07SMax Staudt 181*43da2f07SMax Staudt 12 34 56 78 8 DEADBEEF12345678 182*43da2f07SMax Staudt 183*43da2f07SMax StaudtThe ELM327 will receive both SFF and EFF frames - the current CAN 184*43da2f07SMax Staudtconfig (``ATPB``) does not matter. 185*43da2f07SMax Staudt 186*43da2f07SMax Staudt 187*43da2f07SMax StaudtIf the ELM327's internal UART sending buffer runs full, it will abort 188*43da2f07SMax Staudtthe monitoring mode, print "BUFFER FULL" and drop back into command 189*43da2f07SMax Staudtmode. Note that in this case, unlike with other error messages, the 190*43da2f07SMax Staudterror message may appear on the same line as the last (usually 191*43da2f07SMax Staudtincomplete) data frame:: 192*43da2f07SMax Staudt 193*43da2f07SMax Staudt 12 34 56 78 8 DEADBEEF123 BUFFER FULL 194*43da2f07SMax Staudt 195*43da2f07SMax Staudt 196*43da2f07SMax Staudt 197*43da2f07SMax StaudtKnown limitations of the controller 198*43da2f07SMax Staudt------------------------------------ 199*43da2f07SMax Staudt 200*43da2f07SMax Staudt- Clone devices ("v1.5" and others) 201*43da2f07SMax Staudt 202*43da2f07SMax Staudt Sending RTR frames is not supported and will be dropped silently. 203*43da2f07SMax Staudt 204*43da2f07SMax Staudt Receiving RTR with DLC 8 will appear to be a regular frame with 205*43da2f07SMax Staudt the last received frame's DLC and payload. 206*43da2f07SMax Staudt 207*43da2f07SMax Staudt "``AT CSM``" (CAN Silent Monitoring, i.e. don't send CAN ACKs) is 208*43da2f07SMax Staudt not supported, and is hard coded to ON. Thus, frames are not ACKed 209*43da2f07SMax Staudt while listening: "``AT MA``" (Monitor All) will always be "silent". 210*43da2f07SMax Staudt However, immediately after sending a frame, the ELM327 will be in 211*43da2f07SMax Staudt "receive reply" mode, in which it *does* ACK any received frames. 212*43da2f07SMax Staudt Once the bus goes silent, or an error occurs (such as BUFFER FULL), 213*43da2f07SMax Staudt or the receive reply timeout runs out, the ELM327 will end reply 214*43da2f07SMax Staudt reception mode on its own and can327 will fall back to "``AT MA``" 215*43da2f07SMax Staudt in order to keep monitoring the bus. 216*43da2f07SMax Staudt 217*43da2f07SMax Staudt Other limitations may apply, depending on the clone and the quality 218*43da2f07SMax Staudt of its firmware. 219*43da2f07SMax Staudt 220*43da2f07SMax Staudt 221*43da2f07SMax Staudt- All versions 222*43da2f07SMax Staudt 223*43da2f07SMax Staudt No full duplex operation is supported. The driver will switch 224*43da2f07SMax Staudt between input/output mode as quickly as possible. 225*43da2f07SMax Staudt 226*43da2f07SMax Staudt The length of outgoing RTR frames cannot be set. In fact, some 227*43da2f07SMax Staudt clones (tested with one identifying as "``v1.5``") are unable to 228*43da2f07SMax Staudt send RTR frames at all. 229*43da2f07SMax Staudt 230*43da2f07SMax Staudt We don't have a way to get real-time notifications on CAN errors. 231*43da2f07SMax Staudt While there is a command (``AT CS``) to retrieve some basic stats, 232*43da2f07SMax Staudt we don't poll it as it would force us to interrupt reception mode. 233*43da2f07SMax Staudt 234*43da2f07SMax Staudt 235*43da2f07SMax Staudt- Versions prior to 1.4b 236*43da2f07SMax Staudt 237*43da2f07SMax Staudt These versions do not send CAN ACKs when in monitoring mode (AT MA). 238*43da2f07SMax Staudt However, they do send ACKs while waiting for a reply immediately 239*43da2f07SMax Staudt after sending a frame. The driver maximizes this time to make the 240*43da2f07SMax Staudt controller as useful as possible. 241*43da2f07SMax Staudt 242*43da2f07SMax Staudt Starting with version 1.4b, the ELM327 supports the "``AT CSM``" 243*43da2f07SMax Staudt command, and the "listen-only" CAN option will take effect. 244*43da2f07SMax Staudt 245*43da2f07SMax Staudt 246*43da2f07SMax Staudt- Versions prior to 1.4 247*43da2f07SMax Staudt 248*43da2f07SMax Staudt These chips do not support the "``AT PB``" command, and thus cannot 249*43da2f07SMax Staudt change bitrate or SFF/EFF mode on-the-fly. This will have to be 250*43da2f07SMax Staudt programmed by the user before attaching the line discipline. See the 251*43da2f07SMax Staudt data sheet for details. 252*43da2f07SMax Staudt 253*43da2f07SMax Staudt 254*43da2f07SMax Staudt- Versions prior to 1.3 255*43da2f07SMax Staudt 256*43da2f07SMax Staudt These chips cannot be used at all with can327. They do not support 257*43da2f07SMax Staudt the "``AT D1``" command, which is necessary to avoid parsing conflicts 258*43da2f07SMax Staudt on incoming data, as well as distinction of RTR frame lengths. 259*43da2f07SMax Staudt 260*43da2f07SMax Staudt Specifically, this allows for easy distinction of SFF and EFF 261*43da2f07SMax Staudt frames, and to check whether frames are complete. While it is possible 262*43da2f07SMax Staudt to deduce the type and length from the length of the line the ELM327 263*43da2f07SMax Staudt sends us, this method fails when the ELM327's UART output buffer 264*43da2f07SMax Staudt overruns. It may abort sending in the middle of the line, which will 265*43da2f07SMax Staudt then be mistaken for something else. 266*43da2f07SMax Staudt 267*43da2f07SMax Staudt 268*43da2f07SMax Staudt 269*43da2f07SMax StaudtKnown limitations of the driver 270*43da2f07SMax Staudt-------------------------------- 271*43da2f07SMax Staudt 272*43da2f07SMax Staudt- No 8/7 timing. 273*43da2f07SMax Staudt 274*43da2f07SMax Staudt ELM327 can only set CAN bitrates that are of the form 500000/n, where 275*43da2f07SMax Staudt n is an integer divisor. 276*43da2f07SMax Staudt However there is an exception: With a separate flag, it may set the 277*43da2f07SMax Staudt speed to be 8/7 of the speed indicated by the divisor. 278*43da2f07SMax Staudt This mode is not currently implemented. 279*43da2f07SMax Staudt 280*43da2f07SMax Staudt- No evaluation of command responses. 281*43da2f07SMax Staudt 282*43da2f07SMax Staudt The ELM327 will reply with OK when a command is understood, and with ? 283*43da2f07SMax Staudt when it is not. The driver does not currently check this, and simply 284*43da2f07SMax Staudt assumes that the chip understands every command. 285*43da2f07SMax Staudt The driver is built such that functionality degrades gracefully 286*43da2f07SMax Staudt nevertheless. See the section on known limitations of the controller. 287*43da2f07SMax Staudt 288*43da2f07SMax Staudt- No use of hardware CAN ID filtering 289*43da2f07SMax Staudt 290*43da2f07SMax Staudt An ELM327's UART sending buffer will easily overflow on heavy CAN bus 291*43da2f07SMax Staudt load, resulting in the "``BUFFER FULL``" message. Using the hardware 292*43da2f07SMax Staudt filters available through "``AT CF xxx``" and "``AT CM xxx``" would be 293*43da2f07SMax Staudt helpful here, however SocketCAN does not currently provide a facility 294*43da2f07SMax Staudt to make use of such hardware features. 295*43da2f07SMax Staudt 296*43da2f07SMax Staudt 297*43da2f07SMax Staudt 298*43da2f07SMax StaudtRationale behind the chosen configuration 299*43da2f07SMax Staudt------------------------------------------ 300*43da2f07SMax Staudt 301*43da2f07SMax Staudt``AT E1`` 302*43da2f07SMax Staudt Echo on 303*43da2f07SMax Staudt 304*43da2f07SMax Staudt We need this to be able to get a prompt reliably. 305*43da2f07SMax Staudt 306*43da2f07SMax Staudt``AT S1`` 307*43da2f07SMax Staudt Spaces on 308*43da2f07SMax Staudt 309*43da2f07SMax Staudt We need this to distinguish 11/29 bit CAN addresses received. 310*43da2f07SMax Staudt 311*43da2f07SMax Staudt Note: 312*43da2f07SMax Staudt We can usually do this using the line length (odd/even), 313*43da2f07SMax Staudt but this fails if the line is not transmitted fully to 314*43da2f07SMax Staudt the host (BUFFER FULL). 315*43da2f07SMax Staudt 316*43da2f07SMax Staudt``AT D1`` 317*43da2f07SMax Staudt DLC on 318*43da2f07SMax Staudt 319*43da2f07SMax Staudt We need this to tell the "length" of RTR frames. 320*43da2f07SMax Staudt 321*43da2f07SMax Staudt 322*43da2f07SMax Staudt 323*43da2f07SMax StaudtA note on CAN bus termination 324*43da2f07SMax Staudt------------------------------ 325*43da2f07SMax Staudt 326*43da2f07SMax StaudtYour adapter may have resistors soldered in which are meant to terminate 327*43da2f07SMax Staudtthe bus. This is correct when it is plugged into a OBD-II socket, but 328*43da2f07SMax Staudtnot helpful when trying to tap into the middle of an existing CAN bus. 329*43da2f07SMax Staudt 330*43da2f07SMax StaudtIf communications don't work with the adapter connected, check for the 331*43da2f07SMax Staudttermination resistors on its PCB and try removing them. 332