12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO...

39
12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Transcript of 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO...

Page 1: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-1

EE 319KIntroduction to Embedded Systems

Lecture 12: Serial Communication (UART),

Lab 9, FIFO Queues

Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Page 2: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-2

AgendaRecap

ADC, fixed-pointAssembly-C mixLab 8

AgendaCommunicationSerial: UART, interruptsFIFO Queues used as buffers in communicationLab 9: Distributing Lab 8

o Transmitter uses ADC to read potentiometero Receiver uses LCD to display position.o FIFO serves as buffer at receiver

Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Page 3: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-3

Universal Asynchronous Receiver/Transmitter (UART)

UART (Serial Port) Interface

Send/receive a frame of (5-8) data bits with a single (start) bit prefix and a 1 or 2 (stop) bit suffix

Baud rate is total number of bits per unit timeo Baudrate = 1 / bit-time

Bandwidth is data per unit timeo Bandwidth = (data-bits / frame-bits) * baudrate

3.3V 0Vb0 b1 b2 b3 b4 b5 b6

Serial port b7Start Stop

One frame

Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Page 4: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-4

TM4C123 LaunchPad I/O PinsIO Ain 0 1 2 3 4 5 6 7 8 9 14

PA2 Port SSI0Clk PA3 Port SSI0Fss PA4 Port SSI0Rx PA5 Port SSI0Tx PA6 Port I2C1SCL M1PWM2 PA7 Port I2C1SDA M1PWM3 PB0 Port U1Rx T2CCP0 PB1 Port U1Tx T2CCP1 PB2 Port I2C0SCL T3CCP0 PB3 Port I2C0SDA T3CCP1 PB4 Ain10 Port SSI2Clk M0PWM2 T1CCP0 CAN0Rx PB5 Ain11 Port SSI2Fss M0PWM3 T1CCP1 CAN0Tx PB6 Port SSI2Rx M0PWM0 T0CCP0 PB7 Port SSI2Tx M0PWM1 T0CCP1 PC4 C1- Port U4Rx U1Rx M0PWM6 IDX1 WT0CCP0 U1RTS PC5 C1+ Port U4Tx U1Tx M0PWM7 PhA1 WT0CCP1 U1CTS PC6 C0+ Port U3Rx PhB1 WT1CCP0 USB0epen PC7 C0- Port U3Tx WT1CCP1 USB0pflt PD0 Ain7 Port SSI3Clk SSI1Clk I2C3SCL M0PWM6 M1PWM0 WT2CCP0 PD1 Ain6 Port SSI3Fss SSI1Fss I2C3SDA M0PWM7 M1PWM1 WT2CCP1 PD2 Ain5 Port SSI3Rx SSI1Rx M0Fault0 WT3CCP0 USB0epen PD3 Ain4 Port SSI3Tx SSI1Tx IDX0 WT3CCP1 USB0pflt PD6 Port U2Rx M0Fault0 PhA0 WT5CCP0 PD7 Port U2Tx PhB0 WT5CCP1 NMI PE0 Ain3 Port U7Rx PE1 Ain2 Port U7Tx PE2 Ain1 Port PE3 Ain0 Port PE4 Ain9 Port U5Rx I2C2SCL M0PWM4 M1PWM2 CAN0Rx PE5 Ain8 Port U5Tx I2C2SDA M0PWM5 M1PWM3 CAN0Tx PF0 Port U1RTS SSI1Rx CAN0Rx M1PWM4 PhA0 T0CCP0 NMI C0o PF1 Port U1CTS SSI1Tx M1PWM5 PhB0 T0CCP1 C1o TRD1 PF2 Port SSI1Clk M0Fault0 M1PWM6 T1CCP0 TRD0 PF3 Port SSI1Fss CAN0Tx M1PWM7 T1CCP1 TRCLK PF4 Port M1Fault0 IDX0 T2CCP0 USB0epen

Page 5: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-5Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

RS-232 Serial Port

DB25 Pin

RS232 Name

DB9 Pin

EIA-574 Name

Signal Description True DTE DCE

2 BA 3 103 TxD Transmit Data -5.5V out in 3 BB 2 104 RxD Receive Data -5.5V in out 7 AB 5 102 SG Signal Ground

// this U1Tx PD3 not connected// this U1Rx PD2 tied to U1Tx PD3 of other microcontroller

15

Vss5

4

3

1

2

9

8

7

6

DB9 female

Sin

Sout

89

710

6

0.1F

2

0.1F

0.1F 5

4

0.1F 3

1

0.1F16

+3.3V

MAX3232

+5.5V

-5.5V

TxD

RxD

TM4C123

PA0/U1RxPA1/U0Tx

PB0/U1RxPB1/U1Tx

PD6/U2RxPD7/U2Tx

0

Page 6: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-6Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Serial I/O

Serial communicationTransmit Data (TxD), Receive Data (RxD),

and Signal Ground (SG) implement duplex communication link

Both communicating devices must operate at the same bit rate

Least significant bit sent first

Full duplex

Half duplex

Simplex

Page 7: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-7Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART - Transmitter

UART0_DR_RWrite data

1 0

Stop StartShiftclock

Data

7 6 5 4 3 2 1 0

U0Tx

Transmit data register

16-elementFIFO TXEF Fifo empty flag

TXFF Fifo full flag

Transmit shift register

Page 8: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-8Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART - Transmitter

Tx OperationData written to UART0_DR_R

o passes through 16-element FIFOo permits small amount of data rate matching

between processor and UART

Shift clock is generated from 16x clocko permits differences in Tx and Rx clocks to be

reconciled

3.3V 0Vb0 b1 b2 b3 b4 b5 b6

Serial port b7Start Stop

One frame

Page 9: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-9Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART - Receiver

UART0_DR_RRead data

1 0

Stop Start

Shiftclock

Data

7 6 5 4 3 2 1 0

U0Rx

Receive data register

12-bit, 16-elementFIFO

RXFE Fifo empty flag

OE BE PE FE

RXFF Fifo full flag

Receive shift register

Page 10: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-10Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART - ReceiverRx Operation

RXFE is 0 when data are availableRXFF is 1 when FIFO is fullFIFO entries have four control bits

o BE set when Tx signal held low for more than one frame (break)

o OE set when FIFO is full and new frame has arrivedo PE set if frame parity erroro FE set if stop bit timing error

3.3V 0Vb0 b1 b2 b3 b4 b5 b6

Serial port b7Start Stop

One frame

Page 11: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-11Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART – Overrun Error

17 frames transmitted and none read=> overrun error

"B"=$42

s 0 1 2 3 4 5 6 7 s s 0 1 2 3 4 5 6 7 s

"Q"=$51"A"=$41

RXFE=0 OE=1RXFF=1

"P"=$50

s 0 1 2 3 4 5 6 7 s s 0 1 2 3 4 5 6 7 s

CDEFGHIJKLMNO

Page 12: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-12Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

TM4C UART0 – Registers 31–12 11 10 9 8 7–0 Name $4000.C000 OE BE PE FE DATA UART0_DR_R 31–3 3 2 1 0 $4000.C004 OE BE PE FE UART0_RSR_R 31–8 7 6 5 4 3 2–0 $4000.C018 TXFE RXFF TXFF RXFE BUSY UART0_FR_R 31–16 15–0 $4000.C024 DIVINT UART0_IBRD_R 31–6 5–0 $4000.C028 DIVFRAC UART0_FBRD_R 31–8 7 6 – 5 4 3 2 1 0 $4000.C02C SPS WPEN FEN STP2 EPS PEN BRK UART0_LCRH_R 31–10 9 8 7 6–3 2 1 0 $4000.C030 RXE TXE LBE SIRLP SIREN UARTEN UART0_CTL_R 31–6 5-3 2-0 $4000.C034 RXIFLSEL TXIFLSEL UART0_IFLS_R 31-11 10 9 8 7 6 5 4 $4000.C038 OEIM BEIM PEIM FEIM RTIM TXIM RXIM UART0_IM_R $4000.C03C OERIS BERIS PERIS FERIS RTRIS TXRIS RXRIS UART0_RIS_R $4000.C040 OEMIS BEMIS PEMIS FEMIS RTMIS TXMIS RXMIS UART0_MIS_R $4000.C044 OEIC BEIC PEIC FEIC RTIC TXIC RXIC UART0_IC_R

Page 13: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-13Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

TM4C UART Setup

UART0 operation UART clock started in SYSCTL_RCGCUART_R Digital port clock started in SYSCTL_RCGCGPIO_R UART0_CTL_R contains UART enable (UARTEN), Tx (TXE), and Rx enable

(RXE)o set each to 1 to enableo UART disabled during initialization

UART0_IBRD_R and UART_FBRD_R specify baud rateo bit rate = (bus clock frequency)/(16*divider)o ex: want 19.2 kb/s and bus clock is 8 MHz

o 8 MHz/(16*19.2 k) = 26.04167 = 11010.0000112

o Tx and Rx clo ck rates must be within 5% to avoid errors GPIO_PORTA_AFSEL_R to choose alternate function Write appropriate values to GPIO_PORTA_PCTL_R (See slide 4) GPIO_PORTA_DEN_R Enable digital I/O on pins 1-0 GPIO_PORTA_AMSEL_R no Analog I/O on pins 1-0 write to UART0_LCRH_R to activate

Page 14: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-14Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART Setup

// Assumes a 50 MHz bus clock, creates 115200 baud ratevoid UART_Init(void){ SYSCTL_RCGCUART_R |= 0x0001; // activate UART0 SYSCTL_RCGCGPIO_R |= 0x0001; // activate port A UART0_CTL_R &= ~0x0001; // disable UART UART0_IBRD_R = 27; // IBRD=int(50000000/(16*115,200)) = int(27.1267) UART0_FBRD_R = 8; // FBRD = round(0.1267 * 64) = 8 UART0_LCRH_R = 0x0070; // 8-bit length, enable FIFO UART0_CTL_R = 0x0301; // enable RXE, TXE and UART GPIO_PORTA_AFSEL_R |= 0x03; // alt funct on PA1-0 GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011; GPIO_PORTA_DEN_R |= 0x03; // digital I/O on PA1-0 GPIO_PORTA_AMSEL_R &= ~0x03; // No analog on PA1-0}

Page 15: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-15Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART Synchronization

Busy-wait operation

RXFE

Read data

Empty

Not empty

return

InChar

Write data

TXFFFull

Not full

return

OutChar

1 1

00

Page 16: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-16Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART Busy-Wait Send/Recv

// Wait for new input,// then return ASCII codeuint8_t UART_InChar(void) { while((UART0_FR_R&0x0010) != 0); // wait until RXFE is 0 return((uint8_t)(UART0_DR_R&0xFF));}

// Wait for buffer to be not full,// then outputvoid UART_OutChar(uint8_t data) { while((UART0_FR_R&0x0020) != 0); // wait until TXFF is 0 UART0_DR_R = data;}

Page 17: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-17Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

UART Interrupts

UART0_IFLS_R register (bits 5,4,3)

RXIFLSEL RX FIFO Set RXRIS interrupt trigger when0x0 ≥ ⅛ full Receive FIFO goes from 1 to 2 characters0x1 ≥ ¼ full Receive FIFO goes from 3 to 4 characters0x2 ≥ ½ full Receive FIFO goes from 7 to 8 characters 0x3 ≥ ¾ full Receive FIFO goes from 11 to 12 characters0x4 ≥ ⅞ full Receive FIFO goes from 13 to 14 characters

TXIFLSEL TX FIFO Set TXRIS interrupt trigger when0x0 ≤ ⅞ empty Transmit FIFO goes from 15 to 14 characters0x1 ≤ ¾ empty Transmit FIFO goes from 13 to 12 characters0x2 ≤ ½ empty Transmit FIFO goes from 9 to 8 characters0x3 ≤ ¼ empty Transmit FIFO goes from 5 to 4 characters0x4 ≤ ⅛ empty Transmit FIFO goes from 3 to 2 characters

Page 18: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-18Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Lab 9 – Distributed Measurement

PositionSensor

Voltage0 to +3.3V

ADChardware

ADCdriver

Sample0 to 4095

SysTickISR

Sample0 to 4095

SysTickhardware

LCDdisplay

LCDdriver

Fixed-point0 to 3.000

Position0 to 3 cm

UARTdriver

UART1hardware

main UART1ISR

UART1hardware

Computer 1

Computer 2

Message

Message

FIFO

MessageMessage

Message

MessageSTX d1 . d2 d3 d4 CR ETX

Message

Page 19: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-19Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Lab9: Transmitter SysTick ISR

Toggle heartbeat Sample ADC Toggle heartbeat Convert to integer part of fixed point Send message, 8 calls to UART_OutChar

STX Ones digit Decimal point Tenths digit Hundreds digit Thousandth digit CR ETX

Toggle heartbeat

Interrupt

Perform I/O

return from interrupt

Busy-wait version

Busy-wait version

Page 20: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-20Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Lab9: UART Rx InterruptInterrupt Trigger, sets RXRIS

Receive FIFO has gone from 7 to 8 elements (1/2 full)

Initialization (add these)Arm RXRIS UART1_IM_R |= 0x10;Set UART1_IFLS_R bits 5,4,3 to 010 (1/2 full)NVIC_PRI1_R // bits 21-23NVIC_EN0_R // enable interrupt 6 in NVIC

Interrupt vector in startup.sName ISR UART1_Handler

Acknowledge (in ISR)UART1_ICR_R = 0x10;

Page 21: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-21Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Lab9: Interrupt+Mailbox?

RXRIS ISRRead UART1_DR_RStore in RXmailSet RXstatus

Main loopWait for RXstatusRead RXmailClear RXstatusConvert to distanceDisplay on LCD

Background thread Foreground thread

Other calculationsRead datafrom input

Full

Empty

Mainprogram

ISR

Process MailStatus = Empty

Status Mail = dataStatus = Full

a

bc

d

Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

What can go wrong?

Page 22: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-22Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

First-In/First-Out (FIFO) Queues

Source processProducer

FIFO Sink processConsumer

Fifo_Put Fifo_Get

Order preservingProducer(s) put (on tail end)Consumer(s) get (from head end)Buffer decouples producer & consumer

Even out temporary mismatch in rates

Page 23: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-23Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Operation

I/O bound input interface

Inputdevice

Mainprogram

busy done busy

Interruptserviceroutine

a

done

b

c d a

b

c d a

Elements in FIFO

empty 1 1empty empty

Page 24: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-24Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Operation

High bandwidth input burst

Inputdevice

Mainprogram

busy done busy

Interruptserviceroutine

a

done

b

c

b

c d

Elements in FIFO

empty 1 3

d

busy busydone done

b b

busy done busy

b

1empty 2 32

Page 25: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-25Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Queue Synchronization

Arm output

Write datato output

Disarmoutput

FullTxFifo

Not empty

Empty

TxFifo_Put

Not fullTxFifo_Get

TxFifoRead datafrom input

ERROR

Not empty

Empty

Input

RxFifo_Get RxFifo_Put

Not full

FullRxFifoRxFifo

Input ISR

Output

return

Output ISR

return

Lab 9

Page 26: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-26Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

Lab 9 - RXRIS ISR

1. toggle PF2 (change from 0 to 1, or from 1 to 0), heartbeat2. toggle PF2 (change from 0 to 1, or from 1 to 0), heartbeat3. as long as the RXFE bit in the UART1_FR_R is zero

o Read bytes from UART1_DR_R o Put all bytes into your software FIFO, RxFifo_Put o Should be exactly 8 bytes, but could be more possiblyo If your software FIFO is full (data lost)

increment a global error count (but don’t loop back)o The message will be interpreted in the main program

4. Increment a Counter, o debugging monitor of the number of UART messages received

5. acknowledge the interrupt by clearing the flag which requested ito UART1_ICR_R = 0x10; // clears bit 4 (RXRIS) in RIS register

6. toggle PF2 (change from 0 to 1, or from 1 to 0), heartbeat7. return from interrupt

Page 27: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-27Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Queue Implementation

How is memory allocated?FIFO implies that we write new data at the

head of the queue and we read data from the tail of the queue

What problem does this cause?

To address that problem the queue is operated in a circular mannerAn array of locations is processed so that the

FIRST element of array appears to follow the LAST element of the array

Page 28: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-28Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Queue Implementation

Two parameters are needed to specify the FIFO buffer (array)FIRST (usually index 0)

o points to the first location of the buffer

LIMIT (usually index SIZE)o points to last location+1 of the buffero since LIMIT is outside the buffer, data must not

be written there

FIRST and LIMIT are constantso they are NOT modified by either the source or sink

processing routines

Page 29: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-29Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Queue Implementation

PutPt: Points to the location where the next element to be added goes

GetPt: Points to the location of the oldest valid element, hence the element to be removed first

GetPt

RxFifo

PutPt

Validdata

Page 30: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-30Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Full/Empty Conditions

FIFO Parameter RelationsBuffer is EMPTY

o PutPt equals GetPt

Buffer is FULLo PutPt + 1 equals GetPt

note that there is no data stored at PutPt as a result, if N locations are allocated for a buffer,

only N-1 data elements will fill the buffer

Page 31: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-31Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Wrapping

GPt

Put

PPt

oldest

newestGPt

PutPPt

oldest

newest

GPt

Put

PPt

oldest

newest

GPt

PPt

oldest

newest

GPt

Put

PPt

Get Get Get

GPt

PPt

oldest

newest

Get

GPt

PPt

oldest

newest

PPt

newest

GPt oldest

PPt

newestGPt oldest

PPtGPt

Pointer wrapon 2nd put

Pointer wrapon 4th get

FIRST

LIMIT

FIRST

LIMIT

Page 32: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-32Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Queue

FIFO ImplementationsFIFO_Put

o stores a single value on the FIFO queue operates with interrupts disabled updates PutPt

detects buffer full condition handles transition from LIMIT-1 to FIRST

FIFO_Geto reads a single value from the FIFO queue

operates with interrupts disabled updates GetPt

detects buffer empty condition handles transition from LIMIT-1 to FIRST

Page 33: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-33Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO in C

#define FIFO_SIZE 10

int32_t static *PutPt;

int32_t static *GetPt;

int32_t static Fifo[FIFO_SIZE];

void Fifo_Init(void){

PutPt = GetPt = &Fifo[0];

}

static means private to this file

Page 34: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-34Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Routines in C

int Fifo_Put(int32_t data){int32_t *tempPt; tempPt = PutPt+1; // see if there is room if(tempPt==&Fifo[FIFO_SIZE]){ tempPt = &Fifo[0]; } if(tempPt == GetPt){ return(0); // full! } else{ *(PutPt) = data; // save PutPt = tempPt; // OK return(1); }}

Actually plus 4 bytes

Page 35: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-35Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Routines in C

int Fifo_Get(int32_t *datapt){

if(PutPt == GetPt){

return(0); // Empty

}

else{

*datapt = *(GetPt++);

if(GetPt==&Fifo[FIFO_SIZE]){

GetPt = &Fifo[0];

}

return(1);

}

}

Actually plus 4 bytes

Page 36: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-36Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Queue – Index Implementation

FIFO ImplementationsFIFO_Put

o stores a single value on the FIFO queue operates with interrupts disabled updates PutI

detects buffer full condition handles transition from LIMIT-1 to FIRST

FIFO_Geto reads a single value from the FIFO queue

operates with interrupts disabled updates GetI

detects buffer empty condition handles transition from LIMIT-1 to FIRST

Page 37: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-37Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO in C – Index Implementation

#define FIFO_SIZE 10

int32_t static PutI; //Index in FIFO to

// put new item in

int32_t static GetI; //Index of oldest

// item in FIFO

int32_t static Fifo[FIFO_SIZE];

void Fifo_Init(void){

PutI = GetI = 0;

}

static means private to this file

Page 38: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-38Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO in C – Index Implementationint Fifo_Put(int32_t data){ if ( (PutI+1)% FIFO_SIZE) == GetI) { return(0); } FIFO[PutI] = data; PutI = (PutI+1)%FIFO_SIZE; return(1);}int Fifo_Get(int32_t *datapt){ if (GetI == PutI) { return(0); } *datapt = FIFO[GetI]; GetI = (GetI+1)%FIFO_SIZE; return(1);}

Empty FIFO check

Full FIFO check

Page 39: 12-1 EE 319K Introduction to Embedded Systems Lecture 12: Serial Communication (UART), Lab 9, FIFO Queues Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh.

12-39Bill Bard, Andreas Gerstlauer, Jon Valvano, Ramesh Yerraballi

FIFO Full Errors

Average producer rate exceeds the average consumer rateSample ADC every 50 msAverage time to process the sample is 51 msSolution: decrease producer rate or increase consumer rate

o Lower sampling rateo Faster computero More efficient compilero Rewrite time-critical code

in assemblyo More computers

(distributed processing)

Producer rate temporarily exceeds the consumer rateSample ADC every 50 msEvery 100th sample it takes 1 sec to processSolution: increase FIFO queue size