21
bocon 2007, Hong Kong University of Science & Technology Robocon 2007 Electronics Quickstart! Session 5 Interrupt and OS Prepared by KI Chi Keung [chikeung ust.hk] WONG Long Sing (Sam) [sam hellosam.net]

Robocon 2007 Electronics Quickstart!

  • Upload
    jud

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

Robocon 2007 Electronics Quickstart!. Session 5 Interrupt and OS Prepared by KI Chi Keung [chikeung @ ust.hk] WONG Long Sing (Sam) [sam @ hellosam.net]. Today ’ s Timeline. Program Counter and Hardware Stack Interrupt Buffering for Events Multitasking. 0000. 0001. 0002. 0006. - PowerPoint PPT Presentation

Citation preview

Page 1: Robocon 2007 Electronics Quickstart!

Robocon 2007, Hong Kong University of Science & Technology

Robocon 2007Electronics Quickstart!

Robocon 2007Electronics Quickstart!

Session 5Interrupt and OS

Prepared by KI Chi Keung [chikeung @ ust.hk]

WONG Long Sing (Sam) [sam @ hellosam.net]

Page 2: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Today’s Timeline

1. Program Counter and Hardware Stack

2. Interrupt

3. Buffering for Events

4. Multitasking

Page 3: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Program Counter

• Program Counter is an Integer register– A.k.a. as PC.– Has the address of the

executing byte code– It advance by 1 step when

execution is finish– Or being rewritten to some

other value when CPU faces branching instruction, or calling a function

• or being interrupted!

00000000 MOV A,0ADD A,BJMP HEYSEI A,1CLR CSUB B,CCMP BOUT 20XOR A,9

0001 MOV A,0ADD A,BJMP HEYSEI A,1CLR CSUB B,CCMP BOUT 20XOR A,9

0002 MOV A,0ADD A,BJMP HEYSEI A,1CLR CSUB B,CCMP BOUT 20XOR A,9

0006 MOV A,0ADD A,BJMP HEYSEI A,1CLR CSUB B,CCMP BOUT 20XOR A,9

HEY

Page 4: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Hardware Stack• Hardware Stack is a piece

of storage– Top located by the register -

Stack Pointer, a.k.a. SP– Located at a specific position

of main memory– For temporary storage, grows

and shrink during execution• Local variable• Return address after calling a

function– Grows towards lower address– An entire record is pushed

when calling a function.– Usually, Only the top frame is

used, others are ignored.

0FF6

0FFF int A0FFE0FFD int B0FFC0FFB char i0FFA Rtn Addr0FF90FF8 void* j0FF7 0FF6 char i0FF50FF40FF30FF20FF10FF00FF1

0FFF int A0FFE0FFD int B0FFC0FFB char i0FFA Rtn Addr0FF90FF8 void* j0FF7 0FF6 char i0FF5 Rtn Addr0FF4 int i[2]0FF30FF20FF10FF0

Page 5: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Hardware Stack

Another illustration from the Wikipedia

Page 6: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Memory Arrangement

Start End

GlobalVariables

Stack

Local Variable

Heap

DynamicAllocation

Page 7: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Interrupt

• So far we use while loop to keep on eyes on event (Switch, UART…)– This is called polling

• It’s problematic– When we have a lot of events, it’s so hard to code– At the end, 99% of CPU time is used on busy waiting

• Wouldn’t it be good if you are notified about an event instead?– So that CPU time is spent wisely instead of busy waiting– And this is interrupt

Page 8: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Interrupt

1. Tell the CPU that you want to be notified for an specific event.

• Enabling the specific Interrupt line in register

2. Your interrupt service routine, a.k.a. ISR, will be called if such event occurs.

• PC and Stack are modified in the same way, compare to that of calling a function

3. When ISR returns, execution resumes in main program

Page 9: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Interrupt

• Interrupt is a hardware feature. All modern CPU have it.

• It could never detect an in interrupt has happened, unless you tell it.

• It could never prevent an interrupt from occurring, it can occur at any time

• Of course unless the interrupt is disabled

These characteristics will bring you a new set of problem.

Page 10: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Buffering for Events

By using the interrupt• Send

Handover all the data to be sent to the ISR• Receive

Need to store the data. Wait for CPU to process them

i.e. You need a buffer. A temporary storage that making the transition smooth.

So what is the data structure?

Page 11: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Buffering for Events

A Queue!

ISR Pop CPU Push

Page 12: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Buffering for Events

Yeah! 勝った ( 我贏了 ) 。 almost.

The word Queue is just a concept.

What are the implementations?

• Linked List

• Circular Buffer

Page 13: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Buffering for Events

Linked list

Advantage– Clean and clear concept! Has

been explained for 100 times– Dynamically expandable

Disadvantage– 2/3 memory overhead– No dynamic allocation, or

otherwise expensive, in MCU!

struct queue {

char data;

struct queue* next;

}

struct queue

*head, *tail;

Page 14: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Buffering for Events

Circular Buffer

Advantage– Compact, almost no

overhead– Fast to process

Disadvantage– Fixed size

char queue[128];

char head, tail;

Head points to a free cell

Tail points to the oldest occupied cell

Page 15: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Task

Modify the echo program with output using circular buffer.

Page 16: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Multitasking

We have a new problem. Imagine if we have many I/O queue like the UART.

int main() {

while (true) {

// Check UART Queue

// Response to computer command

// Check Playstation Queue

// Response to operator input

// Auto Machine check Sensor

// Auto Machine Calculation

// Auto Machine Motor Output

}

}

Page 17: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Multitasking

Won’t it be more logical if we…

void UART_handler() {

while (true) {

// Wait for Check UART Queue

// Response to computer command

}

} void manual_control() {

while (true) {

// Wait for Playstation Queue

// Response to operator input

}

} void auto_machine_the_brain() {

while (true) {

// Wait for Playstation Queue

// Response to operator input

}

}

Page 18: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Multitasking

And that is multitasking, with the help of OS.

People have written mini-OS that suitable for MCU deployment, to solve that problem.

Page 19: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Multitasking• There is only one processing unit.

It looks like everything is running at the same time but it is not.

• The CPU take care of different task from time to time.

• When it change its task, it’s called Context Switching. Overhead: backup and restore all registers.

• Context switching can be triggered at fixed interval (Preemptive) or task give up CPU time voluntary (Non-preemptive)

Page 20: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Thread Safety

Traps produced by Multitasking and Interrupt• I/O device is shared. Make sure it is either

shared or a task has exclusive right to use.• Interrupt/Context switch can happen at any

time. Variable update and Race condition!• New task access to the same function, is the function

reentrant?• Specific to AVR: you need to shutdown interrupt for accessing

16-bit SFR like Timer value. (Atomic operation problem)

See http://en.wikipedia.org/wiki/Thread-safe for more

Page 21: Robocon 2007 Electronics Quickstart!

Robocon 2007, HKUST

Thread Safety

Non-preemptive VS Preemptive• Less responsive• but higher throughput• Less to worry about reentrancy problem