Writing an Embedded Controller

Slides:



Advertisements
Similar presentations
Catch up on previous topics Summary and putting together first four classes.
Advertisements

CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,
CDA 3100 Recitation Week 15. What does the function f1 do:.data A:.word 10,21,45,8,100,15,29,12,3,19 B:.word 2,5,33,5,20,1,53,52,5,5 C:.word 6,8,5,4,5,22,53,12,33,89.text.globl.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
The University of Adelaide, School of Computer Science
SPIM and MIPS programming
Some Samples of MIPS Assembly Language Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University.
MIPS Function Continued
MIPS Assembly Language Programming
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
.text fact: addiu $sp, $sp, -32 sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28 sw $a0, 0($fp) bgtz $a0, L2 li $v0, 1 b suffix L2: addi $a0, $a0,
Writing an Embedded Controller. It usually consists of two parts – Initialization – A main loop Experience: – The main loop usually has to deal with many.
ECE 0142 Recitation #5.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Input and Output How things get into and out of the CPU.
Writing an Embedded Controller. It usually consists of two parts – Initialization – A main loop More challenging than HW3 because HW3 is stateless, the.
MIPS I/O and Interrupt. SPIM I/O and MIPS Interrupts The materials of this lecture can be found in A7-A8 (3 rd Edition) and B7-B8 (4 th Edition).
COMP201 Computer Systems Exceptions and Interrupts.
Input and Output Computer Organization and Assembly Language: Module 9.
Writing an Embedded Controller. It usually consists of two parts – Initialization – A main loop More challenging than HW3 because HW3 is stateless, the.
HW #1 Problems & Industrial Strength Assembly CSCI 136.
MIPS I/O and Interrupt. .data val1:.float 0.6 val2:.float 0.8 msg_done:.asciiz "done\n".text.globl main main: li.s $f0, mfc1 $a0, $f0 jal calsqrt.
Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
MIPS I/O and Interrupt.
HW4. A TV controller Let’s use this lecture to start to develop a simple TV controller together.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Exceptions and Interrupts ◆ Breakpoints, arithmetic overflow, traps, and interrupts are all classified as exceptions. ◆ An exception is an event that requires.
Intro Assembly Computer Organization I 1 June 2010 © McQuain, Feng & Ribbens MIPS Hello World # Program: Hello, World!.data # data declaration.
Computer Organization CS224
MIPS Instruction Set Advantages
Introduction to Lab #1 José Nelson Amaral.
Computer System Structures
MIPS I/O and Interrupt.
the Operating System (OS)
MIPS Procedures.
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Input/Output Interface
MIPS instructions.
Pseudo instructions.
MIPS I/O and Interrupt.
MIPS coding.
MIPS I/O and Interrupt.
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
MIPS Procedures.
Solutions Chapter 2.
MIPS Functions.
MIPS coding.
MIPS Functions.
MIPS Functions.
MIPS coding.
MIPS Procedures.
Review.
Computer Organization and Design Assembly & Compilation
MIPS function continued
Компьютерийн зохион байгуулалт, ассемблер CS201
Interrupts and Exception Handling
MIPS Functions.
MIPS I/O and Interrupt.
Writing an Embedded Controller
MIPS coding.
MIPS Functions.
HW4.
Interrupts and Exception Handling
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
MIPS function continued
MIPS Functions.
Presentation transcript:

Writing an Embedded Controller

Embedded Controller A ‘computer’ with a dedicated function within a larger system Usually has ‘real time computing’ constraints Is expected to process requests at a certain speed Example, TV controllers are expected to respond to and typically fully process each user input within at most a few hundred milliseconds Examples: Digital watches, MP3 players, traffic lights, cars, TVs, Washing machines, game consoles, printers, routers, etc.

Writing an Embedded Controller It usually consists of two parts Initialization A main loop More challenging than HW3 because HW3 is stateless, the microcontroller has states. A state based program has to keep track of various global variables and program execution can produce different results each time it is run Stateless programs produce the same output during each invocation and at expected to not have any global variables Experience: The main loop usually has to deal with many things. It is important NOT to stay in any job for too long. You should process an event and almost immediately return to the main loop.

A Simple Program to Get Started Write a program which Prints out “TV is working” every 3 seconds Print out the ASCII of any key you have pressed immediately.

Two Jobs The simple program has two jobs: A periodical job executed every 3 seconds A job to process the input Note: Cannot sleep for 3 seconds and then print out the sentence because cannot process the input while sleeping Must make sure that each iteration of the main loop is short, such that you can check at a fine time granularity if need to print status Has new keyboard input

The code should look like loop: if key pressed print ascii value if 3 sec timer expires print msg goto loop

kdata # kernel data s1:. word 10 # storage for register $v0 s2: .kdata # kernel data s1: .word 10 # storage for register $v0 s2: .word 11 # storage for register $a0 new_line: .asciiz "\n" msg_tvworking: .asciiz "TV is working\n" .ktext 0x80000180 # kernel interupt code starts here sw $v0, s1 # Save registers sw $a0, s2 mfc0 $k0, $13 # Cause register srl $a0, $k0, 2 # Extract ExcCode Field andi $a0, $a0, 0x1f bne $a0, $zero, kdone # Exception Code 0 is I/O. Only processing I/O here lui $v0, 0xFFFF # $t0 = 0xFFFF0000; lw $s6, 4($v0) # get the input key, using $s6 as destination kdone: lw $v0, s1 # Restore registers lw $a0, s2 mtc0 $0, $13 # Clear Cause register mfc0 $k0, $12 # Set Status register andi $k0, 0xfffd # clear EXL bit ori $k0, 0x11 # Interrupts enabled mtc0 $k0, $12 # write back to status eret # return to EPC

.text .globl main main: mfc0 $a0, $12 # read from the status register ori $a0, 0xff11 # enable all interrupts mtc0 $a0, $12 # write back to the status register lui $t0, 0xFFFF # $t0 = 0xFFFF0000; ori $a0, $0, 2 # enable keyboard interrupt sw $a0, 0($t0) # write back to 0xFFFF0000; li $s0, 300 # 3 sec counter li $s6, 10000 # $s6 used to pass the ascii code li $s7, 10000 # a number that can’t be in ascii code loop: beq $s6, $s7, mainloopnext1 ori $a0, $s6, 0 li $s6, 10000 # reset $s6 li $v0,1 # print it here. syscall li $v0,4 # print the new line la $a0, new_line mainloopnext1: addi $s0, $s0, -1 bne $s0, $0, mainloopnext4 li $s0, 300 la $a0, msg_tvworking li $v0, 4 syscall mainloopnext4: jal delay_10ms j loop li $v0, 10 # exit,if it ever comes here delay_10ms: li $t0, 6000 # arbitrary value, attempts to busy delay_10ms_loop: # loop for 10ms; may need addi $t0, $t0, -1 # to change $t0 for your computer bne $t0, $0, delay_10ms_loop jr $ra

A Slightly More Advanced Version Write a process_input function that responds to `m’, `h’, `q’ (ascii code 109, 104, 112, respectively). Basically, The TV is initially not in the ``menu state.’’ When the user presses `m’ while the TV is not in the menu state, the TV should show a very simple menu, and enters the menu state: “`h' to print hello, `q' to quit.” In the menu state, if the user presses `h’, print out “Hello!” if the user presses `q’, print out “quit” and quits the menu state. If not in the menu state, the TV does not respond to `h’ and `q’.

The Challenge How do you know whether to respond to ‘h’ or ‘q’ or not? Should not respond in the normal state Should respond under menu A naïve way is to write a process_input function that Called when ‘m’ is pressed then waits there for ‘h’ and ‘q’ Problem? Problem is cannot print the “TV okay” message while waiting

The solution Maintain a global variable to remember if we are in the menu state Write the process_input function by checking the variable first

.kdata s1: .word 10 s2: .word 11 .data menuLevel: .word 0 msg_tvworking: .asciiz "tv is working\n" msg_menu: .asciiz "`h' to print hello, `q' to quit.\n" msg_hello: .asciiz "hello!\n" msg_quit: .asciiz "quit.\n“

.ktext 0x80000180 # kernel code starts here sw $v0, s1 # Save registers mfc0 $k0, $13 # Cause register srl $a0, $k0, 2 # Extract ExcCode Field andi $a0, $a0, 0x1f bne $a0, $zero, kdone # Exception Code 0 is I/O. Only processing I/O here lui $v0, 0xFFFF # $t0 = 0xFFFF0000; lw $s6, 4($v0) # get the input key kdone: lw $v0, s1 # Restore registers lw $a0, s2 mtc0 $0, $13 # Clear Cause register mfc0 $k0, $12 # Set Status register andi $k0, 0xfffd # clear EXL bit ori $k0, 0x11 # Interrupts enabled mtc0 $k0, $12 # write back to status eret # return to EPC

.text .globl main main: mfc0 $a0, $12 # read from the status register ori $a0, 0xff11 # enable all interrupts mtc0 $a0, $12 # write back to the status register lui $t0, 0xFFFF # $t0 = 0xFFFF0000; ori $a0, $0, 2 # enable keyboard interrupt sw $a0, 0($t0) # write back to 0xFFFF0000 li $s0, 300 # 3 secs li $s6, 10000 # $s6 used to pass the ascii code li $s7, 10000 # a large number impossible to be an ascii code mainloop: # 1. read keyboard input, and process it beq $s6, $s7, mainloopnext1 ori $a0, $s6, 0 li $s6, 10000 # $s0 used to pass the ascii code jal process_input mainloopnext1: addi $s0, $s0, -1 bne $s0, $0, mainloopnext2 li $v0, 4 la $a0, msg_tvworking syscall addi $s0, $0, 300 mainloopnext2: jal delay_10ms j mainloop li $v0,10 # exit

delay_10ms: li $t0, 6000 delay_10ms_loop: addi $t0, $t0, -1 beq $t0, $0, delay_10ms_done j delay_10ms_loop delay_10ms_done: jr $ra

process_input: la $t0, menuLevel lw $t1, 0($t0) bne $t1, $0, pi_menu_L_1 li $t0, 109 # comparing with the ascii of `m' bne $a0, $t0, process_input_done li $t1, 1 sw $t1, 0($t0) la $a0, msg_menu li $v0, 4 syscall j process_input_done pi_menu_L_1: li $t0, 104 # comparing with the ascii of `h' bne $a0, $t0, pi_menu_L_1_comp_q la $a0, msg_hello pi_menu_L_1_comp_q: li $t0, 113 # comparing with the ascii of `q' la $a0, msg_quit sw $0, 0($t0) process_input_done: jr $ra