32
6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

Embed Size (px)

Citation preview

Page 1: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-1

EE 319KIntroduction to Embedded Systems

Lecture 6: Stack and Local Variables, Fixed-Point Numbers,

LCD and Recursion

Page 2: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-2Bard, Gerstlauer, Valvano, Yerraballi

Page 3: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-3

Agenda

Local VariablesStack and Activation RecordsFixed-point numbersLCD InterfacingRecursion

Page 4: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-4

Local Variables

Scope => from where can it be accessedlocal means restricted to current program

segmentglobal means any software can access it

Allocation/Lifetime => when is it created & destroyeddynamic allocation using registers or stackpermanent allocation assigned a block of

memory

Page 5: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-5

Local variables => local scope, dynamic allocationtemporary informationused only by one software moduleallocated, used, then deallocatednot permanentimplement using the stack or registers

o R0-R3 and R12 temporary datao R4-R11 more permanent data

Local Variables

Page 6: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-6

In C

Global Variables Public: global scope,

permanent allocation

// accessible by all modules int16_t myGlobalVariable; void MyFunction(void){…}

Private: global scope(only to the file), permanent allocation

//accessible in this file onlystatic int16_t myPrivateGlobalVariable;

// callable by other // routines in this file only void static MyPrivateFunction(void){…}

Local variables Public: local scope,

dynamic allocation

void MyFunction(void){ int16_t myLocalVariable; }

Private: local scope, permanent allocation

void MyFunction(void){ static int16_t count; count++; }

Page 7: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-7

Dynamic allocation/release allows for reuse of memory

Limited scope of access provides for data protection

Only the program that created the local can access it

The code is reentrant.The code is relocatableThe number of variables is more than

available registers

Why use Stack for Locals?

Page 8: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-8

Recursion Example

uint16_t foo(uint16_t a) { uint16_t b;

b = a / 2; if (b > 0) { b = foo(b); } else { b = 0; } return (a + b);}

Bard, Gerstlauer, Valvano, Yerraballi

Page 9: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-9

Recall Stack RulesProgram segments should have an equal

number of pushes and pullsPush with multiple registers will always

put the lower numbered register’s contents in the lower address.

Pop with multiple registers will always get the lower numbered register’s contents from the lower address.Push1. SP=SP-42. Store 32 bits at SPPop1. Read 32 bits at SP2. SP=SP+4

Empty Stack

SP

Stack with 3 elements

SP top

next

Page 10: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-10

Recursion using the stack

; Input R0 is n; Output R0 is return valuen EQU 0 ;input parameterFact PUSH {R0,LR} CMP R0,#1 BLS base SUB R0,#1 ;n-1 BL Fact ;Fact(n-1) LDR R1,[SP,#n] MUL R0,R0,R1 ;n*Fact(n-1) B donebase MOV R0,#1done ADD SP,#4 ;deallocate POP {PC}

uint32_t Fact(uint32_t n) { if(n<=1) return 1; return n*fact(n-1);}

Recursion requires putting parameters and locals on the stack

SP n

return address

32 bits

[SP,#0]

Bard, Gerstlauer, Valvano, Yerraballi

Page 11: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-11

Variables on the stack

Many inputsVoid play_note(uint16_b pitch, uint16_b duration, uint16_b loud_left, uint16_b loud_right, uint16_b timber_idx, uint16_b attack_rate, uint16_b attack_type, uint16_b decay_rate, uint16_b decay_type);Bard, Gerstlauer, Valvano, Yerraballi

Page 12: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-12

Stack frame using SP

; *****binding phase***************sum EQU 0 ;32-bit unsigned numbern EQU 4 ;32-bit unsigned number; 1)*****allocation phase *********calc PUSH {R4,LR} SUB SP,#8 ;allocate n,sum; 2)******access phase ************ MOV R0,#0 STR R0,[SP,#sum] ;sum=0 MOV R1,#1000 STR R1,[SP,#n] ;n=1000loop LDR R1,[SP,#n] ;R1=n LDR R0,[SP,#sum] ;R0=sum ADD R0,R1 ;R0=sum+n STR R0,[SP,sum] ;sum=sum+n LDR R1,[SP,#n] ;R1=n SUBS R1,#1 ;n-1 STR R1,[SP,#n] ;n=n-1 BNE loop; 3)******deallocation phase ***** ADD SP,#8 ;deallocation POP {R4,PC} ;R0=sum

uint32_t calc(void) { uint32_t sum, n; sum = 0; for(n=1000; n>0; n--) { sum=sum+n; } return sum;}

Program 7.3. Stack pointer implementation of a function with two local 32-bit variables.

SP sum

n

return address

32 bits

[SP,#0]

[SP,#4]

R4

Page 13: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-13

Push parameters on stack

; Inputs R0 is x; R1 is y; R2 is z; Output R0 is return valuesum EQU 0 ;32-bit signed numberx EQU 4 ;32-bit signed numbery EQU 8 ;32-bit signed numberz EQU 12 ;32-bit signed numberAdd3 PUSH {R0,R1,R2,LR} SUB SP,#4 ;allocate sum; body of the function LDR R0,[SP,#x] ADD R0,R0,[SP,#y] ADD R0,R0,[SP,#z] STR R0,[SP,#sum] ADD SP,#16 ;deallocate POP {PC}

int32_t Add3(int32_t x, int32_t y, int32_t z) { int32_t sum; sum = x+y+z; return sum;}

Pushing parameters on stack makes them similar to local variables

SP sum

x

return address

32 bits

[SP,#0]

[SP,#4]

y [SP,#8][SP,#12]z

Page 14: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-14

Fixed-Point Numbers

Why? (wish to represent non-integer values)

Next lab measures distance from 0 to 3 cmE.g., 1.234 cm

When? (range is known, range is small)

Range is 0 to 3cmResolution is 0.001 cm

How? (value = I*) I (Variable Integer) is a

16-bit unsigned integer. It is stored and manipulated in memory.

(Fixed Constant) that represents the resolution. It is not stored but is usually written in comments ; implicit.

(What about negative numbers?)

Page 15: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-15

Fixed-Point Numbers: Decimal

Decimal Fixed-Point (Value = I*10m)

I is a 16-bit unsigned integer (variable integer) = 10m decimal fixed-point (fixed constant)

For example with m=-3 (resolution of 0.001 or milli) the value range is 0.000 to 65.535 (with 16-bit)

What is represented as, in Decimal Fixed-Point? (3.14159…) = I*10-3 => I = Integral approximation of (3.14159…*103)

I = Integral approximation of (3141.59) I = 3142

Decimal Fixed-Point numbers are human-friendly-easy to input/output to humans

Page 16: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-16

Fixed-Point Numbers: Binary

Binary Fixed-Point(Value = I*2m)

I is a 16-bit unsigned integer (variable integer) = 2m binary fixed-point (fixed constant)

For example with m=-8 (resolution of 1/256)

What is represented as, in binary Fixed Point? (3.14159…)= I*2-8

=> I = Integral approximation of(3.14159…*28) I = Integral approximation of(804.2477)

I = 804

Binary Fixed-Point numbers are computer-friendly

-runs very fast because shifting is fast

Page 17: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-17

OutputOutput an integer. Assume integer, n, is between 0

and 9999. 1. LCD_OutChar(0x30+n/1000)

;thousand’s digit2. n = n%1000

LCD_OutChar(0x30+n/100);hundred’s digit

3. n = n%100LCD_OutChar(0x30+n/10);ten’s digit

4. LCD_OutChar(0x30+n%10);one’s digit

Output a fixed-point decimal number.

Assume the integer part of the fixed point number, n, is between 0 and 9999, and resolution is 0.001.

1. LCD_OutChar(0x30+n/1000)//thousand’s digit

2. n = n%1000LCD_OutChar(0x2E)//decimal point

3. LCD_OutChar(0x30+n/100)//hundred’s digit

4. n = n%100LCD_OutChar(0x30+n/10)//ten’s digit

5. LCD_OutChar (0x30+n%10)//one’s digit

Page 18: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-18

Input/Output Synchronization

Processor-Peripheral Timing MismatchPeripherals, e.g., displays, sensors, switches,

generally operate MUCH slower than processor instruction times

o Processor ~ MHzo Peripheral ~ kHz or Hz

MANY instructions can be executed while peripheral processes information

Page 19: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-19

Input/Output Sync. (cont.)Peripheral primitive states

READYo Peripheral is ready to initiate an I/O transfer

NOT READYo Peripheral is unable to perform I/O transfer

BUSYo READY peripheral becomes BUSY when I/O transfer

initiatedo Peripheral remains BUSY for duration of I/O

transfero Another transfer can NOT be initiated

NOT BUSYo READY peripheral is able to initiate another I/O

operation

Page 20: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-20

Input/Output Sync. (cont.)

INPUT

OUTPUT

Inputdevice

Software

Busy

Time

Wait

Read

Wait

Busy

Ready Ready

Busy

Wait

Process Read Process

Outputdevice

Software

Busy

Time

Wait

Write

Wait

Busy

Ready

Busy

Wait

GenerateWrite

Ready

WriteGenerate Generate Generate

Ready

Page 21: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-21

Write data

BlindCycle

return

Output

Wait a fixed time

BlindCycle

Wait a fixed time

Read data

return

Input

What to do while the peripheral is BUSY? 1. BLIND CYCLE TRANSFER

o Suppose that a BUSY control signal is not availableo Perform I/O operationo Wait for a period of time that is guaranteed to be

sufficient for operation to completeo Initiate next operation

I/O Sync Options (1)

Page 22: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-22

What to do while the peripheral is BUSY?2. BUSY-WAIT (e.g., ready-busy, test-transfer)

o Poll peripheral status – wait for READY/NOT BUSYo Perform other tasks between pollso Unless timed correctly, under/over run possible

One solution: POLL CONTINUOUSLY

I/O Sync Options (2)

Busy-Wait

Write data

StatusBusy

Ready

return

OutputBusy-Wait

Status

Read data

Busy

Ready

return

Input

Page 23: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-23

What to do while the peripheral is BUSY?3. INTERRUPT/TRANSFER

o Hardware INTERRUPTS processor on condition of READY/NOT BUSY

o Facilitates performing other – background - processing between I/O transfers Processor changes context when current transfer

complete Requires program structure to process context change

I/O Sync Options (3)

Write data

Get data from Fifo

FifoEmpty

Interrupt

FifoFull

Output

Put data in Fifo

return from interruptreturn

Not full

Not empty

Interrupt

Fifo

Get data from Fifo

Empty

Input

Read data

Put data in Fifo

return from interruptreturn

Some

Page 24: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-24

I/O Sync Options (4)

What to do while the peripheral is BUSY?4. DIRECT MEMORY ACCESS TRANSFER

o Special purpose hardware logic monitors status of BUSY signal and maintains addresses of data to be communicated Requires address and block size initialization

o On the condition of NOT BUSY logic communicates next data element and increments address

o When transfer is complete, logic provides COMPLETE INTERRUPT

Our LM4F120/TM4C123 supports DMA (but EE319K doesn’t use it)

Page 25: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-25

Latency

Software latency or interface latencyTime from when new input is ready until time

software reads data.Time from when output is idle until time

software writes new data.Execute tasks at periodic intervals, latency is

delay from when it should run until it does runInterrupts guarantee an upper bound on

the software response timeCount maximum time running with I=1, plusTime to process the interrupt.

Page 26: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-26

Real-Time System

Real-time systema system that can guarantee a worst case

latencyThroughput/bandwidth

maximum data flow (bytes/s) that can be processed by the system

Prioritydetermines the order of service among two

or more requests

Page 27: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-27

Sitronix ST7735 LCD

Resolution: WxD of 128x160 pixels 1.8” TFT LCD display with 18-bits per pixel On-chip Display Data RAM 128x160x18bits Device driver library ST7735.c provided to you – Implements

the SPI protocol Interfaced using the SPI protocol with 4 or 5 wires Built-in micro-SD card for storage

Page 28: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-28

Interface to LaunchpadSitronix ST7735 LaunchPad Pin

10 – Backlight Power (3.3V)

9 – MISO Not Connected

8 – SCK PA2 (SSI0Clk)

7 – MOSI PA5 (SSI0Tx)

6 – TFT_CS PA3 (SSI0Fss)

5 – CARD_CS Not Connected

4 – Data/Command PA6 (GPIO: High for Data Low for Command)

3 – RESET PA7

2 – Vcc Power (3.3v)

1 – Gnd Ground

SPI pins

Page 29: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-29

Module Call Graph

ST7735

LCDLow-level

main

Print IO

writedatawritecommand

LCD_OutDecLCD_OutFix

ST7735_OutCharST7735_OutString

Lab7Main.s

Print.sST7735.c

LCD.s

IO.sIO_InitIO_TouchIO_Heartbeat

Page 30: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-30

LCD Programming

writecommand: Involves 6 steps performed to send 8-bit Commands to the LCD

1. Read SSI0_SR_R and check bit 4, 2. If bit 4 is high, loop back to step 1 (wait for BUSY bit to be low)3. Clear D/C=PA6 to zero (D/C pin configured for COMMAND)4.Write the command to SSI0_DR_R 5.Read SSI0_SR_R and check bit 4, 6.If bit 4 is high loop back to step 5 (wait for BUSY bit to be low)

Page 31: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-31

LCD Programming

writedata: Involves 4 steps performed to send 8-bit Commands to the LCD:

1.Read SSI0_SR_R and check bit 1, 2.If bit 1 is low, loop back to step 1 (wait for TNF bit to be one)3.Set D/C=PA6 to one (D/C pin configured for DATA)4.Write the 8-bit data to SSI0_DR_R

Page 32: 6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion

6-32

LCD – Lab7

Lab assignmentInterface LCD to TI boardDevelop device driver to serve as interface

between TM4C123 and ST7735 displayWrite the modules you are responsible forTest on Simulator firstBuild circuit and test on real board