Docsity

Input Output Programming-Embedded Systems-Lecture 11 Slides-Electrical and Computer Engineering, Slides of Embedded Systems

Toggle sidebar
bg1
1
Embedded Systems
Input/Output Programming
Dr. Jeff Jackson Lecture 11-1Electrical & Computer Engineering – Embedded Systems
Outline
•External I/O devices
•I/O software
Plld iti l
–
P
o
ll
e
d
wa
iti
ng
l
oops
–Interrupt-driven I/O
–Direct memory access (DMA)
•Synchronization, transfer rate, and latency
•Interrupt Systems
Dr. Jeff Jackson Lecture 11-2Electrical & Computer Engineering – Embedded Systems
•Buffers and Queues
•Interrupt Service Routines in C
•NIOS II Interrupt System
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Input Output Programming-Embedded Systems-Lecture 11 Slides-Electrical and Computer Engineering and more Slides Embedded Systems in PDF only on Docsity!

Embedded Systems

Input/Output Programming

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

Outline

• External I/O devices

• I/O software

– Polled waiting loopsP ll d iti l

– Interrupt-driven I/O

– Direct memory access (DMA)

• Synchronization, transfer rate, and latency

• Interrupt Systems

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

• Buffers and Queues

• Interrupt Service Routines in C

• NIOS II Interrupt System

External I/O Devices

• External I/O devices in embedded systems are often

mechanical in nature

– Solenoids, relays, etc.

– Their response time is often characterized by the time requiredTheir response time is often characterized by the time required

for some physical movement

  • Orders of magnitude slower than that of a typical CPU

• Other I/O devices are analog-to-digital (AD) or digital-to-

analog (DA) converters

– Commonly translate a voltage to/from a digital value

– Still slower than the typical CPU

P i t t b d

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

• Point to be made:

– The typical I/O data transfer rate is limited by the speed of the

external device, not the CPU

– A programmer must be certain a CPU does not attempt to

transfer data at a rate faster than the device can process it

I/O Device Behavior

• All I/O devices operate asynchronously with respect

to the CPU

• Most I/O programming req ires someMost I/O programming requires some

“handshaking” mechanism between the device and

the CPU to coordinate reliable data transfer

• The time behavior of I/O data transfer rates vary

significantly from one device to another

From low data rate random occurrence (a keyboard for

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

– From low data rate, random occurrence (a keyboard for

example)

– To high data rate, periodic occurrence (DMA device)

I/O Device Synchronization

• CPU and I/O devices should be considered

physically and logically independent

– Events that occur within an I/O device that determineEvents that occur within an I/O device that determine

when data is available for transfer are usually not under

control of the CPU

• Synchronization is therefore necessary

• Synchronization steps include:

– Checking the status of the device

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

Checking the status of the device

– Wait until it is ready to transfer data

Transfer Rate

• Transfer rate is a measure of the number of

bytes/second transferred between the CPU and an

external device

• Maximum transfer rate give a measure of the

bandwidth capability of a particular method of

performing I/O

– DMA usually provides the fastest transfer rate, at the

expense of additional hardware complexity

– Polled waiting loops are somewhat slower

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

– Polled waiting loops are somewhat slower

– Interrupt-based I/O can degrade data rates even further

unless more than one byte can be transferred per

interrupt

  • Due to the overhead of saving/restoring machine state

Latency

• Latency is a measure of the delay from the time a

devices is ready until the first data byte is

transferredtransferred

– Essentially equivalent to “response time”

• Latency is:

– High with polled waiting loops

  • CPU may not be checking the device for data when it is available

– Average with interrupt-based I/O

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

Average with interrupt based I/O

  • Still some software overhead

– Low with DMA

Polled Waiting Loop Pseudocode

IO_input(void) { while((inport(IO_DEVICE_STATUS_PORT) & DEVICE_READY) == 0) { // Do nothing (wait for data to arrive) } return (inport(IO_DEVICE_DATA_PORT); }

IO_output(unsigned char ch) { while((inport(IO_DEVICE_STATUS_PORT) & DEVICE_READY) == 0) { // Do nothing (wait for device to have data) } outport(IO_DEVICE_DATA_PORT, ch);

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

  • Memory bandwidth may be a limiting factor for this code
  • Spend some time optimizing these types of functions
    • Run from on-chip memory, store results in on-chip memory, etc.

Example Queue Operation

• A queue can decouple time of data arrival from time

of data processing

• Note the aperiodic arrival rate and the periodic

removal rate

Insert ↓ ↓ ↓ ↓ ↓ ↓ Count 0 1 0 1 1 2 1 1 1 2 1 2 3 3 2 2 2 2 1 1 1 1 0

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

Remove ↓ ↓ ↓ ↓ ↓ ↓

C struct for Implementing a Queue

typedef struct {

unsigned nq; // insertion index

unsigned dq; //// removal index

unsigned count; // #items in queue

unsigned size; // buffer capacity

BYTE bfr[1]; // buffer array

} QUEUE;

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

} QUEUE;

• BYTE is assumed to be an unsigned char

C Code for Creating a Queue

QUEUE *CreateQueue(unsigned bfrsize) {

unsigned bytes;

QUEUE *q;QUEUE q;

bytes = (sizeof(QUEUE)-1)+bfrsize;

q = (QUEUE *) malloc(bytes);

if (q != NULL) {

q->nq = q->dq = q->count = 0;

q- >size = bfrsize;

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

return q;

C Code to Queue Data

BOOL Enqueue(QUEUE *q, BYTE data) {

BOOL full;

disable(); // Function to disable interrupts

fullfull == qq->count >count ==== qq->size; >size;

if (!full) {

q->bfr[q->nq] = data;

if (++q->nq == q->size) {

q->nq = 0;

q->count++;

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

enable(); // Function to enable interrupts

return !full;

NIOS II Interrupt System

• The HAL provides an enhanced application program

interface (API) for writing, registering and managing

ISRsISRs

– This API is compatible with both internal and external hardware

interrupt controllers.

• Altera also supports a legacy hardware interrupt API

– This API supports only the internal interrupt controller (IIC)

• Both APIs include the following types of routines:

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

– Routines to be called to register an ISR

– Routines to be called by an ISR to manage its environment

– Routines to be called to control ISR behavior

The Enhanced HAL Interrupt API

• Enhanced HAL Interrupt API Functions

– alt_ic_isr_register() – Register a specific ISR function

– alt_ic_irq_enable() – Enable a specific interruptalt ic irq enable() Enable a specific interrupt

– alt_ic_irq_disable() – Disable a specific interrupt

– alt_ic_irq_enabled() – Determine if a specific interrupt is

enabled

– alt_irq_disable_all() – Disable all maskable interrupts

– alt irq enable all() – Enable all maskable interrupts

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

_ q_ _ () p

Implementing an ISR

• Using the enhanced HAL API to implement ISRs

requires the following steps:

– Write the ISR that handles hardware interrupts for aWrite the ISR that handles hardware interrupts for a

specific device

  • We will write these in C

• Register the ISR with the HAL by calling the

alt_ic_isr_register() function

– alt_ic_isr_register() enables hardware interrupts

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

_ _ _ g () p

alt_ic_isr_register()

• The prototype for alt_ic_isr_register() is:

int alt_ic_isr_register(alt_u32 ic_id, alt_u32 irq, alt isr funcalt_isr_func isr,isr, void isr_context, void flags)

• The function has the following parameters:

– ic_id is the interrupt controller identifier (ID) as defined in

system.h

– irq is the hardware interrupt number for the device

– isr is a pointer to the ISR function that is called in response to

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

p p

IRQ number irq

– isr_context points to a data structure associated with the

device driver instance. Passed as the input argument to the

ISR function to pass context-specific information to the ISR

– flags is reserved

Code to Register the ISR with the HAL

• The following execution flow is possible:

– Button is pressed, generating an IRQ

– The ISR gains controlThe ISR gains control

  • The processor branches to the address in the vector table, which

transfers control to the handle_button_interrupts() ISR

– handle_button_interrupts() services the hardware

interrupt and returns

– Normal program operation continues with an updated

value of edge capture

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

value of edge_capture

Code to Register the ISR with the HAL

#include "sys/alt_irq.h" #include "system.h" ... // Declare a global variable to hold the edge capture value volatile int edge_capture;_ ... // Initialize the button_pio static void init_button_pio() { // Recast the edge_capture pointer to match the // alt_irq_register() function prototype void edge_capture_ptr = (void) &edge_capture; // Enable all 4 button interrupts IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf); // Reset the edge capture register**

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 11-

IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0); // Register the ISR alt_ic_isr_register( BUTTON_PIO_IRQ_INTERRUPT_CONTROLLER_ID, BUTTON_PIO_IRQ, handle_button_interrupts, edge_capture_ptr, 0x0); }