📄 tep113.txt
字号:
============================
Serial Communication
============================
:TEP: 113
:Group: Core Working Group
:Type: Documentary
:Status: Final
:TinyOS-Version: 2.x
:Author: Ben Greenstein and Philip Levis
.. Note::
This memo documents a part of TinyOS for the TinyOS Community, and
requests discussion and suggestions for improvements. Distribution
of this memo is unlimited. This memo is in full compliance with
TEP 1.
Abstract
====================================================================
This memo describes the structure and standard implementation of the
TinyOS 2.x serial communication system for mote-to-PC data
exchange. The system is broken into three levels (encoding, framing,
and dispatch) to allow easy experimentation and replacement. It can
also handle multiple packet formats: unlike 1.x, 2.x serial packets
are not bound to the mote's radio packet format. Additionally, one of
the supported packet formats is platform independent, so PC-side
applications can communicate with arbitrary motes.
1. Introduction
====================================================================
Users need to read data out of a TinyOS network. The most common
approach is to attach a mote to a PC or laptop with a wired
connection. While the interface on the PC side can vary from a serial
cable to a USB device to IP, the mote generally talks to a serial port
(UART). In TinyOS 1.x, the UART packet format is platform-specific,
pushing a good deal of complexity into the protocol and PC-side tools
in order to discover and properly handle platform diversity. TinyOS
2.0 introduces the notion of packet format dispatch, so a mote can
support multiple UART packet formats simultaneously. This allows
transparent bridging (e.g., an 802.15.4 base station) to exist in
parallel with platform-independent communication, which simplifies the
PC toolchain. This memo documents the protocols and structure of the
TinyOS 2.x serial communication stack.
2. Serial Stack Structure
====================================================================
The TinyOS 2.x serial communication stack is broken up into four
functional components. From bottom to top, they are
o the raw UART,
o the encoder/framer,
o the protocol,
o and the dispatcher.
Structurally, they look like this:
::
_____________________
| |
| Dispatcher | Packet formatting.
|_____________________|
_____________________
| |
| Protocol | Acknowledgements, CRC computation,
|_____________________| windowing.
_____________________
| |
| Encoder/Framer | Translating raw bytes into frame
|_____________________| delimiters, escape bytes.
_____________________
| |
| Raw UART | Platform code for reading/writing
|_____________________| bytes over the serial connection.
The bottom three provide a byte-level interface: only the Dispatcher
provides a packet-level interface. The top three are all
platform-independent: only the UART is platform-specific code.
The lowest level of the stack is the raw UART. This HIL component
provides functionality for configuring the UART (speed, stop bytes,
etc.), sending/receiving bytes, and flushing the UART.
The Encoder/Framer sits above the raw UART. This component translates
raw data bytes into packet bytes using a serial protocol's
encoding. The Encoder/Framer assumes that a protocol's encoding has
two kinds of bytes: delimiters and data bytes, and signals each in
separate events to the component above.
The Protocol component handles data and delimiter byte events. It is
responsible for reading in and sending all protocol control
packets. If the Protocol component starts receiving a data packet, it
signals to the Dispatcher that a packet has started and signals the
data bytes. When the data packet completes, the Protocol signals to
the Dispatcher that the packet is complete and whether it passed the
protocol-level CRC.
The Dispatcher component handles data packet bytes and delimiters. It
is responsible for reading data bytes into a message_t and signaling
packet reception to components above it. The dispatcher can support
multiple packet formats. Based on how message_t works (see TEP
111[tep111_]), this boils down to knowing where in a message_t a
particular packet format begins (based on its header size). Section
3.4 describes how the default TinyOS 2.x implementation,
``SerialDispatcherC`` does this.
3. The 2.x Serial Stack Implementation
====================================================================
Section 2 describes the basic structure of the TinyOS 2.x serial
stack. This section describes its actual implementation,
including SerialActiveMessageC, which sits on top of the Dispatcher.
All of the components except for UartC are part of the serial
library that lives in ``tos/lib/serial``.
3.1 Raw UART: UartC
--------------------------------------------------------------------
The UART HIL[TEP2_] is ``UartC``, which provides a byte-level
interface to the underlying serial communication. It provides the
``SerialByteComm`` interface:
::
interface SerialByteComm {
async command error_t put(uint8_t data);
async event void get(uint8_t data);
async event void putDone();
}
Alternatively, ``UartC`` may provide the UartStream multi-byte level
interface. See the Low-Level I/O TEP [TEP117_] for details.
Additionally, UartC provides a split-phase interface to signal when
the UART is idle. There are situations (such as when powering down the
usart, when switching from TX to RX on a radio with a UART data line,
etc.) when we need explicit information that the data sent over the
UART has actually been transmitted in full. The problem is that on
MCUs that double-buffer UART communication (such as the msp430), a
putDone event signifies that the UART is ready to accept another byte,
but NOT that the UART is idle.
::
interface SerialFlush {
command void flush();
event void flushDone();
}
It may provide additional interfaces for configuring the serial
port. This TEP does not consider this topic.
3.2 Encoder/Framer: HdlcTranslateC
--------------------------------------------------------------------
HdlcTranslateC is the serial encoder/framer. It uses the
``SerialByteComm`` interface and provides the ``SerialFrameComm``
interface:
::
interface SerialFrameComm {
async command error_t putDelimiter();
async command error_t putData(uint8_t data);
async command void resetSend();
async command void resetReceive();
async event void delimiterReceived();
async event void dataReceived(uint8_t data);
async event void putDone();
}
As its name suggests, it uses the same encoding as the HDLC[HDLC_]
protocol. ``0x7e`` is reserved as a frame delimiter byte, and ``0x7d``
is reserved as an escape byte. HdlcTranslateC maintains ten bits of
state. The receive and send paths each have one bit to store whether
they are using an escape byte, and the transmit path has a byte for
when it sends an escaped byte.
When HdlcTranslateC receives a delimiter byte, it signals
delimiterReceived(). When HdlcTranslateC receives an escape byte, it
sets the receiveEscape flag to true. When it receives any other byte,
it tests to see if the receiveEscape flag is set; if so, it XORs the
data byte with ``0x20`` and clears the flag. It signals dataReceived()
with the byte. The most common use of escape byte is to transmit data
bytes corresponding to the delimiter byte or escape byte. For example,
``0x7e`` becomes ``0x7d 0x5e``.
HdlcTranslateC performs similar actions on the transmit side. When
told to transmit the delimiter or escape byte as a data byte, it sets
the transmitEscape flag to true, stores the data byte XOR ``0x20``,
and sends an escape byte. When the escape byte is sent, it sends the
stored data byte.
3.3 Protocol: SerialP
--------------------------------------------------------------------
The SerialP component implements the serial protocol using PPP/HDLC-
like framing (See RFC 1662[RFC1662_]). Type dispatch and buffer
management are left to higher layers in the serial stack. The protocol
is currently stop-and-wait in the host-to-mote direction and best
effort in the mote-to-host direction.
SerialP provides two byte-level interfaces to the upper layer for
sending and receiving packets, respectively called SendBytePacket and
ReceiveBytePacket.
On the sending side, SerialP is responsible for encapsulation of upper
layer packets. An upper layer component such as SerialDispatcherC
initiates the sending of a packet by calling startSend(), passing the
first byte to send. SerialP collects subsequent bytes by signalling
nextByte(). Within the nextByte handler or between calls to nextByte(),
the upper layer should indicate the end-of-packet by calling
completeSend(). If completeSend is called from within a nextByte()
handler, SerialP will ignore the return of the call to nextByte().
::
interface SendBytePacket {
async command error_t startSend(uint8_t first_byte);
async command error_t completeSend();
async event uint8_t nextByte();
async event void sendCompleted(error_t error);
}
SerialP maintains a small window of bytes that have been received by
the upper layer and not yet sent to the UART. Depending on the timing
requirements of the underlying UART, the size of this window can be
changed. SerialP uses repeated calls to nextByte() to keep this window
filled.
SerialP uses SerialFrameComm to send a delimiter between frames, a
serial-level type field, the bytes of the packet, and a two-byte frame
CRC. For mote-to-host gap detection and link reliability, a sequence
number may also be sent (not activated in the default implementation).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -