Robocon 2007 Electronics Quickstart!

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Advertisements

Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Virtual Memory Management G. Anuradha Ref:- Galvin.
Chapter 6 Limited Direct Execution
6-1 I/O Methods I/O – Transfer of data between memory of the system and the I/O device Most devices operate asynchronously from the CPU Most methods involve.
CSE Fall Introduction - 1 What is an Embedded Systems  Its not a desktop system  Fixed or semi-fixed functionality (not user programmable)
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
1 Last Class: Introduction Operating system = interface between user & architecture Importance of OS OS history: Change is only constant User-level Applications.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
INPUT/OUTPUT ORGANIZATION INTERRUPTS CS147 Summer 2001 Professor: Sin-Min Lee Presented by: Jing Chen.
1 OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
INTERRUPTS PROGRAMMING
Introduction to Embedded Systems
System Calls 1.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Runtime Environments Compiler Construction Chapter 7.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Device Drivers CPU I/O Interface Device Driver DEVICECONTROL OPERATIONSDATA TRANSFER OPERATIONS Disk Seek to Sector, Track, Cyl. Seek Home Position.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This training course provides an overview of the CPU architecture.
8051 Micro controller. Architecture of 8051 Features of 8051.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 9: Virtual Memory.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
6-1 Infineon 167 Interrupts The C167CS provides 56 separate interrupt sources that may be assigned to 16 priority levels. The C167CS uses a vectored interrupt.
Silberschatz, Galvin and Gagne  Applied Operating System Concepts Chapter 2: Computer-System Structures Computer System Architecture and Operation.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
7. IRQ and PIC ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  Interrupts ◦ Section
CSCI1600: Embedded and Real Time Software Lecture 16: Advanced Programming with I/O Steven Reiss, Fall 2015.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Robocon 2007, Hong Kong University of Science & Technology Robocon 2007 Electronics Quickstart! Session 1 Hello! Microcontroller. Prepared by KI Chi Keung.
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Overview: Using Hardware.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Processes and threads.
Operating Systems CMPSC 473
68HC11 Interrupts & Resets.
CS 6560: Operating Systems Design
Lesson Objectives Aims Key Words Interrupt, Buffer, Priority, Stack
Chapter 9: Virtual Memory
Computer Architecture
Day 08 Processes.
Day 09 Processes.
Intro to Processes CSSE 332 Operating Systems
Presented by: Chi Yan Hung
Chapter 8 I/O.
CSCI1600: Embedded and Real Time Software
Chapter 8 I/O.
Chapter 9: Virtual-Memory Management
Computer System Overview
Module 2: Computer-System Structures
Processor Fundamentals
Fundamentals of Computer Organisation & Architecture
Chapter 8 I/O.
BIC 10503: COMPUTER ARCHITECTURE
Module 2: Computer-System Structures
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Chapter 8 I/O.
CSCI1600: Embedded and Real Time Software
Chapter 6: Scheduling Algorithms Dr. Amjad Ali
Module 2: Computer-System Structures
COMP3221: Microprocessors and Embedded Systems
CS703 – Advanced Operating Systems
Lecture 12 Input/Output (programmer view)
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

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

Program Counter 0002 MOV A,0 ADD A,B JMP HEY SEI A,1 CLR C SUB B,C 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! 0002 MOV A,0 ADD A,B JMP HEY SEI A,1 CLR C SUB B,C CMP B OUT 20 XOR A,9 0006 MOV A,0 ADD A,B JMP HEY SEI A,1 CLR C SUB B,C CMP B OUT 20 XOR A,9 HEY 0000 MOV A,0 ADD A,B JMP HEY SEI A,1 CLR C SUB B,C CMP B OUT 20 XOR A,9 0000 0001 MOV A,0 ADD A,B JMP HEY SEI A,1 CLR C SUB B,C CMP B OUT 20 XOR A,9

Hardware Stack 0FF1 0FF6 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. 0FF1 0FFF int A 0FFE 0FFD int B 0FFC 0FFB char i 0FFA Rtn Addr 0FF9 0FF8 void* j 0FF7 0FF6 char i 0FF5 Rtn Addr 0FF4 int i[2] 0FF3 0FF2 0FF0 0FFF int A 0FFE 0FFD int B 0FFC 0FFB char i 0FFA Rtn Addr 0FF9 0FF8 void* j 0FF7 0FF6 char i 0FF5 0FF4 0FF3 0FF2 0FF1 0FF0 0FF6

Hardware Stack Another illustration from the Wikipedia

Memory Arrangement Global Variables Heap Dynamic Allocation Stack Local Variable Start End

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

Interrupt Tell the CPU that you want to be notified for an specific event. Enabling the specific Interrupt line in register 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 When ISR returns, execution resumes in main program

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.

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?

Buffering for Events A Queue! ISR Pop CPU Push

Buffering for Events Yeah! 勝った (我贏了)。 almost. The word Queue is just a concept. What are the implementations? Linked List Circular Buffer

Buffering for Events Linked list Advantage Disadvantage 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;

Buffering for Events Circular Buffer Advantage Disadvantage 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

Task Modify the echo program with output using circular buffer.

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 }

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 }

Multitasking And that is multitasking, with the help of OS. People have written mini-OS that suitable for MCU deployment, to solve that problem.

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)

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

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