Docsity

Comunicaciones Seriales: Paralelas vs. Seriales, Diapositivas de Microprocesadores

Vista previa parcial del texto

¡Descarga Comunicaciones Seriales: Paralelas vs. Seriales y más Diapositivas en PDF de Microprocesadores solo en Docsity!

Serial Communications

Julio Enrique Fajardo

Overview

▪ Serial communications

▪ Concepts

▪ Tools

▪ Software: polling, interrupts and buffering

▪ Protocols:

▪ UART

▪ SPI

▪ I2C

Example System: Voyager Spacecraft

▪ Launched in 1977

▪ Constraints: Reliability, power, size, weight, reliability, reliability, etc.

▪ “Uplink communications is via S-band (16-bits/sec command rate) while an X-band transmitter provides

downlink telemetry at 160 bits/sec normally and 1.4 kbps for playback of high-rate plasma wave data. All

data are transmitted from and received at the spacecraft via the 3.7 meter high-gain antenna (HGA).”

http://voyager.jpl.nasa.gov/spacecraft/index.html

▪ Uplink – to spacecraft

▪ Downlink – from spacecraft

Example System

▪ Dedicated point-to-point connections

▪ Parallel data lines, read and write lines between MCU and each peripheral

▪ Fast, allows simultaneous transfers

▪ Requires many connections, PCB area, scales badly

MCU

Peripheral

WrRd Data

Peripheral

Data Rd Wr

Peripheral

Data

Rd Wr

Peripheral

Data

Rd Wr

Synchronous Serial Data Transmission

▪ Use shift registers and a clock signal to convert between serial and parallel formats

▪ Synchronous: an explicit clock signal is along with the data signal

Clk

D Q

D

D Q

D

D Q

D

D Q

D

Serial

Data Out

Parallel Data In

Clk

D Q

D

D Q

D

D Q

D

D Q

D

Serial

Data In

Parallel Data Out

Transmitting Device

Receiving Device

Clock

Serial Data

Data Sampling Time at Receiver

Synchronous Full-Duplex Serial Data Bus

▪ Now can use two serial data lines - one for reading, one for writing.

▪ Allows simultaneous send and receive full-duplex communication

MCU

Peripheral

DOut

Select

ClkDIn

Peripheral

DOut

Select

DIn

Peripheral

DOut

Select

DIn

Peripheral

DOut

Select

DIn

Asynchronous Serial Communication

▪ Eliminate the clock line!

▪ Transmitter and receiver must generate clock locally

▪ Transmitter must add start bit (always same value) to indicate start of each data frame

▪ Receiver detects leading edge of start bit, then uses it as a timing reference for sampling

data line to extract each data bit N at time Tbit*(N+1.5)

▪ Stop bit is also used to detect some timing errors

T

bit

*1.

Data bits

Data Sampling

Time at Receiver

T

bit

*2.

T

bit

*3.

T

bit

*4.

T

bit

*5.

T

bit

*6.

T

bit

*7.

T

bit

*8.

T

bit

*9.

Time Zero

Start bit

Stop bit

Serial Communication Specifics

▪ Data frame fields

▪ Start bit (one bit)

▪ Data (LSB first or MSB, and

size – 7, 8, 9 bits)

▪ Optional parity bit is used to make total number of ones in data even or odd

▪ Stop bit (one or two bits)

▪ All devices must use the same communications parameters

▪ E.g. communication speed (300 baud, 600, 1200, 2400, 9600, 14400, 19200, etc.)

▪ Sophisticated network protocols have more information in each data frame

▪ Medium access control – when multiple nodes are on bus, they must arbitrate for permission to transmit

▪ Addressing information – for which node is this message intended?

▪ Larger data payload

▪ Stronger error detection or error correction information

▪ Request for immediate response (“in-frame”)

Message

Data bits Start bit Stop bit

Tools for Serial Communications Development

▪ Tedious and slow to debug serial

protocols with just an oscilloscope

▪ Instead use a logic analyzer to decode

bus traffic

▪ Worth its weight in gold!

▪ Saelae 8 - Channel Logic Analyzer

▪ $150 (www.saelae.com)

▪ Plugs into PC’s USB port

▪ Decodes SPI, asynchronous serial, I2C, 1-

Wire, CAN, etc.

▪ Build your own: with Logic Sniffer or

related open-source project

SOFTWARE STRUCTURE – HANDLING

ASYNCHRONOUS COMMUNICATION

Serial Communications and Interrupts

▪ Want to provide multiple threads of control in the

program

▪ Main program (and subroutines it calls)

▪ Transmit ISR – executes when serial interface is ready to

send another character

▪ Receive ISR – executes when serial interface receives a

character

▪ Error ISR(s) – execute if an error occurs

▪ Need a way of buffering information between threads

▪ Solution: circular queue with head and tail pointers

▪ One for tx, one for rx

Serial

Interface

tx_isr

rx_isr

send_string get_string

Main Program or

other threads

▪ Enqueue at tail: tail is the index of the next free entry

▪ Dequeue from head: head is the index of the item to remove

▪ Queue size is initialised and stored in size

▪ One queue per direction

▪ tx ISR unloads tx_q

▪ rx ISR loads rx_q

▪ Other threads (e.g. main) load tx_q and unload rx_q

▪ Need to wrap pointer at end of buffer to make it circular,

▪ Use % (modulus, remainder) operator if queue size is not power of two

▪ Use & (bitwise and) if queue size is a power of two

▪ Queue is empty if tail == head

▪ Queue is full if (tail + 1) % size == head

Code to Implement Queues

write data

to tail

read data

from head

older

data

newer

data

Serial

Interface

tx_isr

rx_isr

get_string

send_string

int queue_init(Queue *queue, uint32_t size) {

queue->data = (uint8_t*)malloc(sizeof(uint8_t) * size);

queue->head = 0;

queue->tail = 0;

queue->size = size;

// If malloc returns NULL (0) the allocation has failed.

return queue->data != 0;

}

int queue_is_full(Queue *queue) {

return ((queue->tail + 1) % queue->size) == queue->head;

}

int queue_is_empty(Queue *queue) {

return queue->tail == queue->head;

}

Initialization and Status Inquiries

int queue_enqueue(Queue *queue, uint8_t item) {

if (!queue_is_full(queue)) {

queue->data[queue->tail++] = item;

queue->tail %= queue->size;

return 1;

} else {

return 0;

}

}

int queue_dequeue(Queue *queue, uint8_t *item) {

if (!queue_is_empty(queue)) {

*item = queue->data[queue->head++];

queue->head %= queue->size;

return 1;

} else {

return 0;

}

}

Enqueue and Dequeue