Week-8_Serial-Communication_v1.3.pdf
-
Upload
anonymous-g0ozqa -
Category
Documents
-
view
214 -
download
0
Transcript of Week-8_Serial-Communication_v1.3.pdf
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
1/41
CENG-336Introduction to Embedded
Systems Development
Spring 2015
Serial Communications
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
2/41
CENG-336 Introduction to Embedded Systems Development 2
Early Serial Communications
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
3/41
CENG-336 Introduction to Embedded Systems Development 3
“Later” Serial Communications
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
4/41
CENG-336 Introduction to Embedded Systems Development 4
Serial Communications Serial communications means that information is transmitted from source
to destination over a single pathway.
Serial Data Links
A simplex serial data link transmits data in only one direction.
Usually, two serial data links are between a computer and terminal: one for
transmission in each direction. This is called full-duplex operation. Thecomputer echoes the character back to the terminal.
screen
keyboard
cpu
outputport
inputport
echo
Serial Link
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
5/41
CENG-336 Introduction to Embedded Systems Development 5
Serial Communications
With half-duplex operation, there is only one line fortransmission and the characters are printed locally.
We can transmit in either direction, but it takes time toturn the line around.
screen
keyboard
cpu
outputport
inputport
Serial Link
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
6/41
CENG-336 Introduction to Embedded Systems Development 6
Serial Communications
Since digital data are transmitted as a “stream of bits”,synchronization is required between the source anddestination of the data.
Asynchronous communication transmits one character (byte)at a time by adding start and stop bits to the character code.
Synchronous transmission transmits a block of data and aclock signal or known bit pattern that can be used forsynchronization.
A block number and checksum are also transmitted.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
7/41
CENG-336 Introduction to Embedded Systems Development 7
ASCII Character Encoding
The asynchronously transmitted character is composed of
7-8 data bits,
an optional parity bit, and
either 1, 1.5, or 2 stop bits.
The most used form of character encoding is the American Standard Codefor Information Interchange (ASCII).
This is a seven bit code which allows for 128 characters.
Codes that are less than 20H are not alpha-numeric characters but control
codes such as carriage return or backspace.
numbers 0-9 are encoded as 30H-39H.
capital letters A-Z have codes 41H-5AH , and
lower case letters a-z are 61H-7FH.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
8/41
CENG-336 Introduction to Embedded Systems Development 8
ASCII
When start and stop bits and a parity bit are added tothe ASCII code, 9 to 11 bits are transmitted.
The rate of transmission of a data bit stream in bits per
second (bps or b/s) is called the bit rate.
Standard bit rates are
110 ( the old teletype or TTY rate),
300 (early PC modems),
1200, 2400, 9600, 14.4K, 19.2K, 28.8K, 38.4K, 57.6K, and115.2 bps.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
9/41
CENG-336 Introduction to Embedded Systems Development 9
Character Transmission
...mark = 1
space = 0
start
bit
data
bit 0
data
bit 1
parity
bit
data
bit n-1
stop
bit
stop
bit
start
bit
time
framebit time
General bit format for asynchronous serial data.
start 1 0 1 0 1 1 0 parity stop stop
b0 b1 b2 b3 b4 b5 b6 b7
[ b7 b6 b5 b4 b3 b2 b1 b0 ] = [ 1 0 1 1 0 1 0 1 ]
odd
parity bit (set to produce an odd number of 1’s.)
Typical transmission - 7 character bits plus parity. The charactercode is 35H which is the code for the number 5.
idle
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
10/41
CENG-336 Introduction to Embedded Systems Development 10
BAUD Rate
Data transmission rates are also specified in terms ofbaud rates.
A baud is a unit of signaling speed that is equal to thenumber of discrete events occurring per second.
The key idea here is that an event contains more than one bitof information.
Therefore, the baud rate can be less than the bit rate.
With only two levels of logic the baud rate equals the bit rate.
However, inside a modem, the logic levels are converted totones.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
11/41
CENG-336 Introduction to Embedded Systems Development 11
BAUD Rate A typical implementation is the transmit sine waves with different phases,
e.g., sin(bt + 0o), sin(bt + 90o), sin(bt + 180o), and sin(bt + 270o).
The transmission can switch to any phase and since there are fourpossible phases, it takes two bits to code this information, i.e. 0o = 00, 90o= 01, etc.
Detecting the phase at a particular time produces 2 bits of information. Therefore, the bit rate is twice as fast as the baud rate in this case.
A 9600 bps modem typically has a baud rate of 2400 baud.
Transmission rates are sometimes referred to in units ofcharacters/second.
For example, if we have 10 bits/ character transmitted at 30 characters/sec.,
then the transmission rate is (10 b/ch)(30ch/s) = 300 b/s.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
12/41
CENG-336 Introduction to Embedded Systems Development 12
RS-232C
The TTL logic signals are not what is transmittedbetween equipment such as modems computers andterminals.
One standard that describes the signals forcommunicating is the RS-232C standard.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
13/41
CENG-336 Introduction to Embedded Systems Development 13
RS-232
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
14/41
CENG-336 Introduction to Embedded Systems Development 14
DCE/DTE
This standard causes as much trouble as any standard you can name
because it deals with two types of equipment,
Data Communications Equipment (DCE) and
Data Terminal Equipment (DTE).
Typically, a modem is DCE and a terminal or computer is DTE.
The problem is that the cable to connect DCE to DTE is different
from the cable to connect DTE to DTE.
Also, nonstandard use of the control signals is a problem.
Murphy’s law says that if you need to connect to a serial port, you
always have the wrong cable.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
15/41
CENG-336 Introduction to Embedded Systems Development 15
Mechanical/Electrical/Procedural
RS-232C specifies 25 signal pins with a
male DTE connector and
a female DCE connector.
Most of these signals are not used in most applications so a 9 pin
subset is used.
Actually in some cases, only three wires are used.
The mark and space signals are inverted by the standard to the
following levels:
A logic high or “mark” is between -3 V and -15 V under load
A logic low or “space” is between +3 V and +15 V under load.
Typically, +12 and -12 volts are used.
The voltage swing is > the TTL 5 volts for noise immunity.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
16/41
CENG-336 Introduction to Embedded Systems Development 16
RS-232C
interfaceTTL levels RS-232C levels
MC1489A line receiver
MC1488 line driver
Signal Conversion to and from TTL and RS-232C levels.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
17/41
CENG-336 Introduction to Embedded Systems Development 17
RS-232 Signals
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
18/41
CENG-336 Introduction to Embedded Systems Development 18
Serial Ports - RS-232 Standard
DTR Data terminal ready
TD Transmit data
RD Receive data
SG Signal ground
DSR Data set ready
RTS Ready to send
TS lear to send
D arrier detect
Signals for a DTE (Data Terminal Equipment – modem is aDCE (Data Communication Equipment)
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
19/41
CENG-336 Introduction to Embedded Systems Development 19
Signal meanings – RS232 Transmit data group
TD – transmit data – data from DTE to DCE
RTS – ready to send – DTE asserts before sending data and waits for CTSbefore sending
CTS – clear to send – DCE sends in response to RTS
Receive data group TD – transmit data – data from DCE to DTE
DSR – data set ready – DCE asserts before sending data and waits for DTR
before sending
DTR – data terminal ready – DTE sends in response to DSR
CD – carrier detect – modem is receiving a carrier from a modem at otherend
SG – signal ground – common return for all lines
Note: Not all manufacturers use the control lines
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
20/41
CENG-336 Introduction to Embedded Systems Development 20
DTE-to-DCE
• Connecting DTE to DCE equipment uses a straightthrough cable with wires connected to the proper pins.A DTE to DTE connection requires a null modem cable.
1 shield ground2 transmit3 receive4 request to send5 clear to send6 data set ready7 signal ground
20 data terminal ready
123456
720
Null modem connection.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
21/41
CENG-336 Introduction to Embedded Systems Development 21
Variants
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
22/41
CENG-336 Introduction to Embedded Systems Development 22
BAUD / bps
Baud: bits per second Baud Application
110 ASR-33 Teletype
300 Early acoustic modems
1200 Direct-coupled modems c. 1980 2400 Modems c. 1990
9600 Serial terminals
19200
38400 Typical maximum 115200 ...
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
23/41
CENG-336 Introduction to Embedded Systems Development 23
Physical Variants
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
24/41
CENG-336 Introduction to Embedded Systems Development 24
PIC Port
USART serial port Universal Synchronous/Asynchronous Receiver/Transmitter
Supports RS-232 or RS-485
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
25/41
CENG-336 Introduction to Embedded Systems Development 25
PIC Universal Synchronous Asynchronous
Receiver Transmitter (USART)
USART is a serial I/O module that can be configured as a full duplex
asynchronous system or a half duplex synchronous system
The USART has the following modes:
Asynchronous (full duplex)
Synchronous - Master (half duplex)
Synchronous - Slave (half duplex)
The USART can communicate using a number of different
protocols:
RS232
SPI Microwire
I2C
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
26/41
CENG-336 Introduction to Embedded Systems Development 26
PIC Peripherals: USART: UART
Serial Communications Peripheral: UniversalSynchronous/Asynchronous Receiver/Transmitter
Only available in 14bit and 16bit cores
Interrupt on TX buffer empty and RX buffer full
Asynchronous communication: UART (RS-232C serial)
Can do 300bps - 115kbps
8 or 9 bits, parity, start and stop bits, etc.
Outputs 5V so you need a RS232 level converter (e.g.,MAX232)
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
27/41
CENG-336 Introduction to Embedded Systems Development 27
PIC Peripherals: USART: USRT
Synchronous communication: i.e., with clock signal
SPI = Serial Peripheral Interface
3 wire: Data in, Data out, Clock
Master/Slave (can have multiple masters)
Very high speed (1.6Mbps)
Full speed simultaneous send and receive (Full duplex)
I2C = Inter IC
2 wire: Data and Clock Master/Slave (Single master only; multiple masters clumsy)
Lots of cheap I2C chips available; typically < 100kbps(For example, 8pin EEPROM chips, ADC, DACs, etc.)
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
28/41
CENG-336 Introduction to Embedded Systems Development 28
PIC18F8722 EUSART
Enhanced Universal Synchronous Asynchronous ReceiverTransmitter
Includes two serial modules EUSART1 and EUSART2
Can operate in both asynchronous (RS232) and synchronousmodes (I2C or SPI)
Usage
First, select BAUD rate
Configure receive and transmit interrupts
Then, select synchronous/asynchronous mode Enable serial port (SPEN=1)
Read data from RCREGx when RCxIF is set
Write data to TXREGx to send data
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
29/41
CENG-336 Introduction to Embedded Systems Development 29
PIC18F8722 BAUD Rate Selection
BRG16: 8-bit or 16-bit BRG mode
BRGH: High (1) or low(0) baud rate (only for asynchronous)
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
30/41
CENG-336 Introduction to Embedded Systems Development 30
PIC18F8722 BAUD Rate Selection
PIC18F8722 datasheet gives necessary tables for baud rate selection
An important criteria is the "error" in the baud rate
Asynchronous communication relies on matching baud rate fortransmitter and receiver
Otherwise, errors might occur during transmission
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
31/41
CENG-336 Introduction to Embedded Systems Development 31
Important Details
Port pins corresponding to the RX and TX functionsmust be configured properly
Consult the datasheet for details
Its is often a good idea to properly initialize ALL
configuration bits and registers explicitly 8-bit vs 9-bit transmission
Synchronous or asynchronous mode
Enable bits: SPEN, TXEN, RXEN
If all else fails, you may need to use a logic analyzer oroscilloscope connected to the port pins to figure outwhat might be going on.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
32/41
CENG-336 Introduction to Embedded Systems Development 32
Serial Communications Software
Asynchronous communications
Full-duplex structure requires independent components fortransmission and reception.
This is best implemented with receive and transmit interrupts
A robust and reliable "protocol" must be designed
Synchronous communications
Half-duplex nature enables a simpler structure. Transmission
and reception are interleaved Receive and transmit interrupts are still useful
Protocol design is less critical but still important
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
33/41
CENG-336 Introduction to Embedded Systems Development 33
Asynchronous Communications
Independent receive and transmit ISRs should be implemented
ISRs use ring buffers for incoming and outgoing data
Main program handles generation and processing of data, using
buffers Starting transmission may be tricky. The first byte can be initiated
by main. ISR handles remaining transmissions.
receivetask
receivebuffer
transmitbuffer
receiveISR
transmitISR
RCREGx
TXREGx
main
transmittask
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
34/41
CENG-336 Introduction to Embedded Systems Development 34
Asynchronous Example
System to listen for commands on a full-duplex RS232connection and respond accordingly
First step: "Communication Protocol"
Imposes a common structure on the exchange of data
Agreed upon and known by both the sender and the receiver Often structured in the form of "packets". For example:
bits 7 6 5 4 3 2 1 0
byte 1 type size
byte 2-n command / response
byte n+1 checksum
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
35/41
CENG-336 Introduction to Embedded Systems Development 35
Asynchronous Example
Program structure
void main() {init_interrupts();init_serial();while(1) {
receive_task(); // Handles reception of new packet from bufferprocess_task(); // Handles reception of new packet from buffersend_task(); // Builds and buffers response packet
}}void init_serial() {
TRISC = 0xDF; // Pin 7 input, Pin 8 outputBAUDCON1 = 0x48; // 16 bit baud rate generation, receive idleSPBRGH1 = 0x00;SPBRG1 = 0x56; // Baud rate for 115200 (See Table 20-3)TXSTA1 = 0x04; // Asynchronous, high speed, 8-bit mode, disable transmitter for nowRXSTA1 = 0x90; // Serial and receive enabled, 8-bit mode
}void init_interrupts() {
PIF1 = 0x00; // RC1IE = 1, TX1IE = 1PIE1 = 0x30; // RC1IE = 1, TX1IE = 1INTCON = 0xC0; // GIE = 1, PEIE = 1
}
warning: untested code, double check
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
36/41
CENG-336 Introduction to Embedded Systems Development 36
Asynchronous Example
Interrupt service routines
void interrupt high_priority high_isr() {if (PIF1 & 0x20 != 0) receive_isr();if (PIF1 & 0x10 != 0) transmit_isr();
}
void receive_isr() {receive_buf_push( RXREG1 ); // Receive and buffer data
PIF1 &= ~0x20; // Clear receive interrupt flag}
void transmit_isr() {if (transmit_buf_isempty()) { // Disable further interrupts if transmit buffer empty
TXSTA1 &= ~0x20; // This should be set when new data is placed into buffer} else {
TXREG1 = transmit_buf_pop();}
PIF1 &= ~0x10; // Clear transmit interrupt flag}
warning: untested code, double check
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
37/41
CENG-336 Introduction to Embedded Systems Development 37
Asynchronous Example
Receive task: Decodes incoming packet
void receive_task() {while (! receive_buf_isempty() ) { // Consume all available data
data = receive_buf_pop();switch (state ) {
case WAIT_HEADER:packet_type = data >> 6; packet_size = data & 0x3f;state = RECEIVE_DATA; i = 0;break;
case RECEIVE_DATA:command_data[i++] = data;if (i == packet_size) state = VERIFY_CHECKSUM;
break;case VERIFY_CHECKSUM:
if (verify_checksum() == 0) discard_command();else alert_process_task();state = WAIT_HEADER;break;
} }}
wait for size receive packet
verify checksum
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
38/41
CENG-336 Introduction to Embedded Systems Development 38
Asynchronous Example
Send task: Builds and buffers outgoing data
Also initiates sending of the first byte by enabling thetransmitter
void send_task() {
if (new_outgoing_packet) {
header = (response_type
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
39/41
CENG-336 Introduction to Embedded Systems Development 39
A Serious Problem: Scenario
Serial connections are unreliable. Bytes may be receivedincorrectly, lost, or extra bytes may be received.
Example: What happens if the receiver misses a start bitand fails to receive one of the packet bytes?
The receiver will process the remaining bytes of the packet,but they will be one less than what they should be
The first byte of the next packet will be interpreted as thechecksum byte
The following byte, which should have been the first data byteof the next packet, will be interpreted as the size
The rest of the communications will all be garbage.
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
40/41
CENG-336 Introduction to Embedded Systems Development 40
A Serious Problem: Solution
Distinct identifier to indicate the start of a byte
Could be a unique sequence of values
You could use a 9-bit protocol and use the 9th bit to identifythe header
If only 8 bits are available, use the most significant bit toidentify the header, and pack data into 7 bit segments placedinto the packet body
If a header is received in any state, go bck to theWAIT_HEADER state
Implement timeouts
If the entire packet is not received after a certain timeout, goback to the WAIT_HEADER state
-
8/20/2019 Week-8_Serial-Communication_v1.3.pdf
41/41
CENG 336 I d E b dd d S D l 41
Conclusion
Asynchronous communications may be difficult
Properly initialize hardware, select correct baud rates and portconfigurations
Use receive and transmit interrupts, together with ring buffers
Design and implement a well-defined communication protocol
Use state machines whenever you can, especially for reception
Handle ALL errors, including buffer overruns, underruns,checksum errors, timeouts, incorrect headers, invalid contentetc.