1 \documentclass[12pt,
a4paper,titlepage
]{article
}
9 \includegraphics[width=
1\textwidth]{./logos/freeems
}\\
[1cm
] \vfill
12 { \huge \bfseries Serial Protocol -- Core
}\\
[0.4cm
]
15 \Large \emph{Author:
}\\
16 \textsc{Fred Cooke
} \vfill
18 {\large Version
0.7.0 --- Saturday
1\textsuperscript{st
} January,
2011}
27 The purpose of the protocol is to provide reliable two-way serial communication
28 with FreeEMS and associated devices over multiple physical protocols. This will
29 commonly be used for live tuning, configuration changes, firmware updates and
30 data acquisition of various types.
32 This
document covers the basic data transfer protocol, which all
33 implementations should adhere to. It describes the methods through which
34 various functions are achieved, right down to the hardware, at all but the
37 Each implementation will provide documentation on the device-specific
38 functionality offered and the payload data formats used. Not all devices
39 will support all functions, either because they have limited functionality
40 (a display device for example), or because the software versions may differ.
41 Where logging/debug facilities are available, the payload type of unrecognised
42 packets should be noted before discarding them.
47 \section {Main Attributes
}
49 \item Compact binary data format to maximize speed and bandwidth.
50 \item Data is packetized with an escaping scheme.
51 \item Each packet consists of a header, payload and checksum.
52 \item Compulsory integrated low-cost checksum.
53 \item Compulsory acknowledgment of received packets.
54 \item Optional sequence numbers
55 \item Big endian multi byte number format
56 \item Compulsory core functionality across implementing devices
62 \section{Packet Description
}
64 \subsection{Packet Data Format
}
66 \begin{tabular
}{|c|c|c|
}
67 \hline Packer header:
3 --
6 bytes & Packet payload:
0 -- max bytes & CKS \\
72 A packet consists of a three to six byte header, a payload of variable length
73 from zero to implementation maximum payload size and a one byte checksum. This
74 format will be encapsulated in various lower level constructs depending on the
75 medium of transmission.
77 \subsection{Packet Header Layout
}
79 \begin{tabular
}{|c|c|c|c|
}
80 \hline Flags & Payload ID & Seq No. & Payload Length \\
85 \subsection{Packet Header Specification
}
88 \begin{tabular
}{|l|l|l|
}
89 \hline Byte sequence & Length & Data \\
90 \hline 1 &
1 & Header flags \\
91 \multirow{8}{*
}{} &
\multirow{8}{*
}{} & Bit
0 - Has Length \\
92 && Bit
1 - Is Negative Acknowledgment \\
93 && Bit
2 - Has Sequence \\
94 && Bit
3 - Reserved \\
95 && Bit
4 - Reserved \\
96 && Bit
5 - Reserved \\
97 && Bit
6 - Reserved \\
98 && Bit
7 - Reserved \\
99 \hline 2,
3 &
2 & Payload ID \\
100 \hline 4 &
1 (opt) & Sequence number \\
101 \hline 4,
5 or
5,
6 &
2 (opt)* & Length of payload (excluding header/checksum) \\
105 * Required for some packet types
109 Packets are just containers and the payload data is specific to the payload
110 type being sent. See Payload ID.
112 \subsection{Checksum
}
114 The checksum is a simple
8 bit type as the EMS deals with bytes during uart
115 transmission. It is compulsory to include the checksum in each packet. It is
116 located at the end of the payload to ease load on the EMS during send and
117 receive operations and keep the code simple and compact. The checksum covers
118 all bytes in the message before any escaping and packetising occurs, including
119 the header but not including the start and stop delimiter bytes.
124 \section{Header Details
}
126 \subsection{Protocol Packets
}
128 Payload IDs of less than
\texttt{256}/
\texttt{0x0100} (
\texttt{0x0000} -
129 \texttt{0x00FF} /
\texttt{0} -
\texttt{255}) are of type ``protocol'' and
130 defined payload IDs should be implemented in all devices using this protocol in
131 their application. (See appendix~
\ref{app:payload
})
133 All payload IDs of
256/
0x0100 or more when the payload type bit of the header
134 flags is clear (
\texttt{0}) the payload type is of the type ``firmware''. The firmware
135 payload IDs are covered in the FreeEMS Vanilla Specific protocol specification,
138 \subsection{Addressing Behaviour
}
140 Addressing of packets will be handled by wrapping them in other protocols of
141 a different layer at a higher level. UART is a point to point protocol and
142 thus it does not make sense to use addressing at this layer of the protocol.
143 Wrapping could occur on CAN, SPI, Ethernet and other addressable bus type
146 \subsection{Acknowledgments
}
148 All packets received are responded to with an acknowledgement. The
149 acknowledgement payload ID is one greater than whatever was received. In the
150 header of the acknowledgement packet is a bit that specifies whether it is a
151 positive or negative acknowledgement. In the case of success, the payload will
152 either be empty (default) or contain the requested data in the format specified
153 for that payload ID. In the case of failure the payload contains a
16 bit error
156 \subsection{Payload ID
}
158 The payload ID is a unique identifier number that indicates the functionality
159 to be delivered by a request packet. All payload request IDs are to be even
160 and all response packets are the ID of the request plus one. See Apendix A for
161 protocol payload ID definitions.
163 \subsection{Sequence Numbers
}
165 The protocol provides a facility for packets to be identified by sequence
166 number as well as just type and timing. If a sequence number is supplied and
167 flagged it will be returned just the same. Asynchronous packets will not carry
168 sequence numbers, and it is perfectly acceptable to not use them at all. They
169 are provided as an option for tuning authors to use.
171 \subsection{Length Of Payload
}
173 Length from the end of the header to the end of the payload NOT including the
174 checksum, header or packet delimiters, and before escaping occurs.
179 \section{General Information
}
181 \subsection{Flow Control
}
183 Upon receiving a packet that requires a response the EMS will be responsible
184 for disabling receipt of further requests by disabling the receive register
185 full interrupt. By this mechanism it can ignore subsequent requests until the
186 current packet is dealt with and the receive buffer free again. The external
187 device should expect messages to be ignored up until the first byte of the
188 response packet comes back. If datalog streaming is enabled it will not be
189 possible to determine whether or not the packet is the response until it is
190 completely received. Instead, it is probably better to wait for some short
191 period after a packet is transmitted and delay sending further packets until
192 after that period is over. No harm can be done by sending packets while the
193 device is not listening, they will just be ignored. RS232 hardware flow control
194 is not used as it is rarely implemented correctly or even at all in common
195 physical implementations.
197 \subsection{Reliable Transmission
}
199 In all cases the external software will be responsible for ensuring
200 unacknowledged messages sent to the EMS are resent. The EMS will work on a
201 ``best effort'' basis to handle all requests promptly, however that can not be
204 Several generic methods of recognising bad data exist:
207 \item Checksum does not match that supplied.
208 \item Packet length in the header (if present) does not match the actual length received.
209 \item Packet is larger than the maximum allowed size.
210 \item The payload length does not match that of the payload ID (where fixed and defined)
213 In addition, the specifics of how each different physical layer operates will
214 provide further means of recognising bad packet data as will the different
217 \subsection{Packet Size
}
219 Packet size is only limited by the size of the payload size field and the
220 specific implementations hardware software setup. Currently FreeEMS Vanilla
221 only requires ~
2k packet size to be handled, however up to
64k is supported by
222 the
16 bit size field. This will ensure it stays useful for a longer time and
223 will therefore allow tool re-use too. If larger data blocks need to be sent in
224 some future FreeEMS implementation then the data can be packetised down inside
225 this format without much difficulty. Each implementation should provide the
226 facility to request the maximum permissible packet size.
228 \subsection{Protocol Layers
}
231 \item Physical layers: UART over RS232 or USB, CAN, SPI, I$^
2$C, etc
232 \item Data layer (UART): escaping/start/stop/bit format/etc
233 \item Data layer (CAN): yet to be determined
234 \item Packet layer: generic across all mediums
235 \item Application layer: device/firmware specific
238 \subsection{Response Times
}
240 To be defined once implemented and tested. Thanks EdG!
245 \section{UART Specifc Information
}
247 \subsection{Packet Data Format
}
249 \begin{tabular
}{|c|c|c|
}
250 \hline \texttt{STX
} & Data Packet (Header Payload and Checksum):
4 -- max bytes &
\texttt{ETX
} \\
255 This is a diagram of a single packet of data, in a serial stream.
\texttt{STX
} and
256 \texttt{ETX
} are single bytes that mark the start and end of the packet (the escaping
257 scheme). Should the data flow be interrupted, the data flow can be picked back
258 up again, by looking for the
\texttt{STX
} byte. This escaped and delimited packet scheme
259 affords a number of opportunities to catch corrupt data as seen in the Reliable
260 transmission section.
262 \subsection{Bitwise Byte Format
}
264 \begin{tabular
}{|c|c|c|c|
}
265 \hline Start &
8 data bits & Parity & Stop \\
270 Low level
9 bit data format consisting of
11 bits total. One start bit (low),
271 8 data bits, one hardware generated odd parity bit, one stop bit (high). See
272 MC9S12XDP512V2.pdf section
11.4.3 page
495
274 \subsection{Escaping Scheme
}
277 \begin{tabular
}{|c|c|c|
}
278 \hline Description & Byte value & Escaped pair \\
279 \hline Start byte (
\texttt{STX
}) &
\texttt{0xAA} &
\texttt{0xBB} \texttt{0x55} \\
280 \hline Escape byte (
\texttt{ESC
}) &
\texttt{0xBB} &
\texttt{0xBB} \texttt{0x44} \\
281 \hline End byte (
\texttt{ETX
}) &
\texttt{0xCC} &
\texttt{0xBB} \texttt{0x33} \\
287 All packets start with
\texttt{STX
}, and end with
\texttt{ETX
}. Should any of
288 \texttt{STX
},
\texttt{ETX
} or
\texttt{ESC
} occur within the packet contents
289 (including the header and checksum), it must be ``escaped'', with a preceding
290 \texttt{ESC
} byte and the byte itself XOR'ed with the value
0xFF. This will
291 yield the following easy to recognise pairs in the stream:
293 If the character
\texttt{0xBB} is found in the raw stream without
294 \texttt{0x55},
\texttt{0x44} or
\texttt{0x33} following it, the stream and
295 therefore packet is corrupt and invalid and should be discarded.
297 \subsection{UART Specific Error Detection
}
299 This section is supplementary to the things mentioned in the reliable delivery
300 section above. The following things should be checked for when interacting over
301 a UART connection of any sort:
304 \item Start byte found before stop byte while inside a packet.
305 \item Escape byte not followed by an escaped special byte.
306 \item Parity bit errors at the byte level.
312 Note, not all of these signals will be available on all devices, but whatever
313 is available should be used to ensure a highly robust serial solution is
314 implemented. After all, the EMS code can only confirm that the received data is
315 in good condition, it can not be responsible for the integrity of the data it
316 sends out as it could easily be corrupted in transit.
318 Note: The checksum doesn't cover the start, stop, escape or escaped bytes, only
319 the original packet data.
324 \section{CAN Specific Information
}
326 Yet to be determined.
331 \section{Payload IDs
}
334 \begin{tabular
}{|l|p
{2cm
}|l|l|p
{7cm
}|
}
335 \hline ID & Name & Type & Size & Description and format \\
336 \hline 0 & Interface Version & Request &
0 & The unique number that identifies the firmware specific interface definition. If this is the same, there should be absolutely no difference in communications. Useful to allow tuning tools to support interface version ranges based on common functionality and different firmwares with the same interface type.\\
337 \hline 1 & Interface Version & Response &
16 -
256 & Details on the semantics of handling and using this field are not yet defined. For now it can simply be used to identify whether the API and data layout are AT ALL changed from previous versions. That is not good enough in the long term, though.\\
338 \hline 2 & Firmware Version & Request &
0 & The unique string that identifies the firmware version running on the device. Useful for associating behaviours, issues and bugs with specific code versions.\\
339 \hline 3 & Firmware Version & Response &
16 -
256 & An arbitrary string
16 to
256 bytes long uniquely describing the firmware version running on the device.\\
340 \hline 4 & Max Packet Size & Request &
0 & Ask the controller to tell us how large in bytes the maximum packet it can handle is. The size is measured from the end of start byte to beginning of stop byte including the header, payload and checksum regions.\\
341 \hline 5 & Max Packet Size & Response &
2 &
16 bit unsigned integer packet size.\\
342 \hline 6 & Echo Packet & Request & Any & Wrap and return the entire packet as the payload.\\
343 \hline 7 & Echoed Packet & Response & Any & The wrapped packet returned.\\
344 \hline 8 & Soft Reset * & Command &
0 & Instructs the device to software reset itself.\\
345 \hline 9 & $<$special case$>$ & Response &
2 & Only if the request is malformed you will get an error back.\\
346 \hline 10 & Hard Reset * & Command &
0 & Instructs the device to hardware reset itself. (complete)\\
347 \hline 11 & $<$special case$>$ & Response &
2 & Only if the request is malformed you will get an error back.\\
348 \hline 13 & Error/Exception & Assertion &
2 &
16 bit unsigned integer error number.\\
349 \hline 15 & Debug Data & Assertion & Any & Free form data of any type for debug purposes.\\
352 * Note, soft and hard reset behaviour is implementation
353 dependent and both may behave the same way as each other.
358 Payload ID number which uniquely identifies the purpose and format of the data
359 contained in the payload.
361 \subsubsection*
{Type Key:
}
363 \item Request - A message sent from a tuning device or similar to an embedded
364 device or similar. Typically PC $
\rightarrow$ EMS
365 \item Response - The reply to a request from an embedded device or similar to a
366 tuning device or similar. Typically EMS $
\rightarrow$ PC
367 \item Command - A message sent from a tuning device or similar to an embedded
368 device or similar. Typically PC $
\rightarrow$ EMS
369 \item Assertion - A message sent from an embedded device or similar to a tuning
370 device or similar. Typically EMS $
\rightarrow$ PC
373 \subsubsection*
{Size
}
374 Payload size is in bytes from end of header to beginning of checksum.
376 \subsubsection*
{Description And Format
}
377 The purpose of this particular payload type and how the data is laid out.
379 Note: This table is liable to be extended in subsequent
387 Copyright
2008--
2011 Fred Cooke. All rights reserved.