+ All Categories
Home > Documents > EE 319K Introduction to Embedded Systems

EE 319K Introduction to Embedded Systems

Date post: 31-Dec-2015
Category:
Upload: fritz-calhoun
View: 21 times
Download: 1 times
Share this document with a friend
Description:
EE 319K Introduction to Embedded Systems. Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion. Agenda. Local Variables Stack and Activation Records Fixed-point numbers LCD Interfacing Recursion. Local Variables. Scope => from where can it be accessed - PowerPoint PPT Presentation
30
6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion
Transcript
Page 1: EE 319K Introduction to Embedded Systems

6-1

EE 319KIntroduction to Embedded Systems

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

LCD and Recursion

Page 2: EE 319K Introduction to Embedded Systems

6-2

Agenda

Local VariablesStack and Activation RecordsFixed-point numbersLCD InterfacingRecursion

Page 3: EE 319K Introduction to Embedded Systems

6-3

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 4: EE 319K Introduction to Embedded Systems

6-4

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 5: EE 319K Introduction to Embedded Systems

6-5

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 6: EE 319K Introduction to Embedded Systems

6-6

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 7: EE 319K Introduction to Embedded Systems

6-7

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 8: EE 319K Introduction to Embedded Systems

6-8

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 9: EE 319K Introduction to Embedded Systems

6-9

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 10: EE 319K Introduction to Embedded Systems

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]

Page 11: EE 319K Introduction to Embedded Systems

6-11

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 12: EE 319K Introduction to Embedded Systems

6-12

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 13: EE 319K Introduction to Embedded Systems

6-13

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 14: EE 319K Introduction to Embedded Systems

6-14

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. 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 15: EE 319K Introduction to Embedded Systems

6-15

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 16: EE 319K Introduction to Embedded Systems

6-16

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 17: EE 319K Introduction to Embedded Systems

6-17

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 18: EE 319K Introduction to Embedded Systems

6-18

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 19: EE 319K Introduction to Embedded Systems

6-19

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 20: EE 319K Introduction to Embedded Systems

6-20

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 21: EE 319K Introduction to Embedded Systems

6-21

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 22: EE 319K Introduction to Embedded Systems

6-22

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 23: EE 319K Introduction to Embedded Systems

6-23

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 24: EE 319K Introduction to Embedded Systems

6-24

NOKIA 5110 LCD

A low power CMOS LCD moduleHas a Phillips PCD8544 controller/driverResolution: WxD of 48x84 pixels Dot Matrix display with 1-bit per pixelHas a DDRAM (48 × 84 bit static RAM) which

stores the display data.Device driver library Nokia5110.c provided to

you – Implements the SPI protocolMax Rate 4 Mbps

Page 25: EE 319K Introduction to Embedded Systems

6-25

LCD – Operation

Possible Interface to Launchpad

Nokia 5110 -> LaunchPad pin6-VCC -> power (3.3V)8-GND -> ground2-CE -> to PA31-RST -> to PA73-DC -> to PA64-DIN -> to PA55-CLK -> to PA27-LIGHT-> not connected

Show Datasheet for LCD in LCD.pdf

Page 26: EE 319K Introduction to Embedded Systems

6-26

Module Call Graph

Nokia

LCDLow-level

main

Print IO

Page 27: EE 319K Introduction to Embedded Systems

6-27

LCD Programming

LCD_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 set loop back to step 1 (wait for BUSY bit to be zero)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 set loop back to step 5 (wait for BUSY bit to be zero)

Page 28: EE 319K Introduction to Embedded Systems

6-28

LCD Programming

LCD_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 clear 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 data to SSI0_DR_R

Page 29: EE 319K Introduction to Embedded Systems

6-29

LCD – Lab6

Lab assignment (Lab6.zip)Interface LCD to TI boardDevelop device driver to serve as interface

between TM4C123 and Nokisa displayUse main program (in lab6.c)Write the modules you are responsible forTest on Simulator firstBuild circuit and test on real board

Page 30: EE 319K Introduction to Embedded Systems

6-30

Lab6 – Your responsibility

IO.c – Switch and Heartbeat moduleIO_InitIO_HeartBeatIO_Touch

Lcd.c – LCD Device driver moduleLCD_WriteDataLCD_WriteCommandWait10ms

Print.c – Print moduleLCD_OutFixLCD_OutDec

Do not use SysTick for this wait, because we will add SysTick interrupts in Labs 7, 8, 9.


Recommended