eln5622 embedded systems class 3 spring, 2003

42
ELN5622 Embedded Systems Class 3 Spring, 2003 Kent Orthner [email protected]

Upload: lorant

Post on 13-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

ELN5622 Embedded Systems Class 3 Spring, 2003. Kent Orthner [email protected]. Assembly Language Programming. Programming Conventions. Conventions: Rules that a designer follows to make his/her program easier to understand, communicate to other, debug, and less prone to mistakes. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ELN5622 Embedded Systems Class 3 Spring, 2003

ELN5622Embedded Systems

Class 3

Spring, 2003

Kent [email protected]

Page 2: ELN5622 Embedded Systems Class 3 Spring, 2003

Assembly Language Programming

Page 3: ELN5622 Embedded Systems Class 3 Spring, 2003

Conventions:– Rules that a designer follows to make

his/her program easier to understand, communicate to other, debug, and less prone to mistakes.

Includes:– Comment conventions– Naming conventions– Drawing Conventions

Programming Conventions

Page 4: ELN5622 Embedded Systems Class 3 Spring, 2003

Program Header– What the program does– Author’s Name– File Name– Date– Version– History

Function Header– What the function does/How it works– Author’s Name– Date– History

Assembly Language Style

Page 5: ELN5622 Embedded Systems Class 3 Spring, 2003

Equates vs. In-line constants– Use constants wherever possible

– Easier to understand,

– Easier to fix

– Easier to maintain

Location of Equate Statements– All together

– Point-of-use

Assembly Language Style

Page 6: ELN5622 Embedded Systems Class 3 Spring, 2003

Types of Equates – System Equates

• System Functions

• Location of I/O Registers

• Port Addresses

– Constant Equates• CR/LF, End of String, NIL

– Memory Map Equates• Program Location

• Flash Memory Location

• Stack Pointer Location

Assembly Language Style

Page 7: ELN5622 Embedded Systems Class 3 Spring, 2003

Constant Data Definitions– Tables, Strings, Etc.– Located in ROM– Often best to have at the end of the program

to lessen the chance of them executed

Variable Data Definitions– System-wide Variables– Located in RAM

Non-volatile ROM Data– Database for user settings– Maintained when power is shut off

Assembly Language Style

Page 8: ELN5622 Embedded Systems Class 3 Spring, 2003

Indenting– Not commonly used for assembly programs

– Can ease understanding by making program flow more obvious.

– Up to the individual designer.

Use ‘Boilerplate’ files– All files use the same flow

– Saves Typing

– Less potential Mistakes

Assembly Language Style

Page 9: ELN5622 Embedded Systems Class 3 Spring, 2003

Commenting Style– Headers for functional blocks

– Headers per line

– Goal: To make it so the person maintaining the code can understand the program.

Assembly Language Style

Page 10: ELN5622 Embedded Systems Class 3 Spring, 2003

Naming Conventions– Very important to prevent errors and make code

easy to understand.

– Examples:

LDX SP

LDB X,p_operand Stack variable

LDA g_timer Global variable

JSR s_timercheck Subroutine

LDB X,Operand Stack variable

LDA G_TimerVar Global variable

JSR TIMERCHECK Subroutine

Assembly Language Style

Page 11: ELN5622 Embedded Systems Class 3 Spring, 2003

Pseudocode– Self-commenting, less prone to error

– Makes it natural to

1. design first with pseudo code

2. Implement second with assembly language

* Get Temp

* If Temp > MaxAllowed

* Turn valve off

* Else

* turn the valve on

* End if

Assembly Language Style

Page 12: ELN5622 Embedded Systems Class 3 Spring, 2003

Pseudocode

* Get Temp LDAA TEMP_PORT* If Temp > MaxAllowedIF_TEMPMAX: CMPA MaxAllowed A-MaxAllowed BLE EL_TEMPMAX Branch if <= 0* Turn valve off LDAA ValveOff STAA CONTROL_PORT BRA EI_TEMPMAX* Else turn the valve onEL_TEMPMAX: LDAA ValveOn STAA CONTROL_PORT* End ifEI_TEMPMAX: <Next Inst>

Assembly Language Style

Page 13: ELN5622 Embedded Systems Class 3 Spring, 2003

While-Do Loop

* While Temperatue > MaxAllowed

WH_MAXALL: CMPA MaxAllowed A-MaxAllowed

BLS EW_MAXALL Branch if < 0

* Do

* End While

BRA WH_MAXALL

EW_MAXALL: <Next Inst>

Assembly Language Style

Page 14: ELN5622 Embedded Systems Class 3 Spring, 2003

Repeat-Until Loop

* RepeatRP_SWSTATE:

* Until SwitchState = END_STATE LDAA SwitchState CMPA END_STATE BNE RP_SWSTATE <Next Inst>

Assembly Language Style

Page 15: ELN5622 Embedded Systems Class 3 Spring, 2003

For Loop

* For (I=COUNT, I--, I==0)

LDAA COUNT

* Begin

FOR_LOOP: PUSH A

. . .

* Next

PULA

DECA

BNE FOR_LOOP

<Next Inst>

Assembly Language Style

Page 16: ELN5622 Embedded Systems Class 3 Spring, 2003

Anatomy of an Embedded Program

Page 17: ELN5622 Embedded Systems Class 3 Spring, 2003

Embedded System Characteristics A computing system embedded within a

device.

A system intended for a single purpose, which includes a general purpose processor.

Often used for– providing user control over a product– to observe or control something in the “real

world” (i.e. analog)

Page 18: ELN5622 Embedded Systems Class 3 Spring, 2003

Embedded System Startup

What is the first thing the user expects when an embedded system starts up?– Begin application execution.

When should the software in an embedded system finish?– It shouldn't. It should (normally) run until the

power is turned off.

Page 19: ELN5622 Embedded Systems Class 3 Spring, 2003

Environment setup– System Equates

– Constant Equates

– Memory Map Equates

Initialization– Set stack pointer

– Special Register Setup

– Initialize Tasks

– Enable Interrupts

Main Loop– Execute Applications

Embedded Program Flow

Page 20: ELN5622 Embedded Systems Class 3 Spring, 2003

Embedded Program FlowInitialize Stack PointerSpecial Register Setup

Initialize Task 1Initialize Task 2

Enable Interrupts. ( Go! )

Initialize Task 3

Execute Task 1Execute Task 2Execute Task 3

Page 21: ELN5622 Embedded Systems Class 3 Spring, 2003

Environment Setup Types of Equates

– System Equates• System Functions

• Location of I/O Registers

• Port Addresses

– Constant Equates• CR/LF, End of String, NIL

– Memory Map Equates• Program Location

• Flash Memory Location

• Stack Pointer Location

Page 22: ELN5622 Embedded Systems Class 3 Spring, 2003

68HC11 jumps to the 'Reset Vector' located at 0xFFFE, 0xFFFF

PC <- M(0xFFFE, 0xFFFF)

Execution Start

Page 23: ELN5622 Embedded Systems Class 3 Spring, 2003

Initialize Stack Pointer– Interrupts

– Functions

– Temporary Storage

Special Register Setup– Memory Mapping registers

• Determine the location of RAM & the register block within the memory map.

• Can only be written at Startup

Initialization

Page 24: ELN5622 Embedded Systems Class 3 Spring, 2003

Special Register Setup (Continued)– System Configuration Registers

• A/D Powerup

• Clock Select

• IRQE Edge-Sensitive Select

• Clock Monitor Enable

• COP Timer Rate

– I/O Control Registers

– Timer Registers

– Interrupt Mask Registers

– SCI & SPI Registers

– ADC/DAC Control Registers

Initialization

Page 25: ELN5622 Embedded Systems Class 3 Spring, 2003

Task Initializatoin– Set initial state for state machines

– Set initial values for task variables

– Pre-compute tables where necessary

– Clear / pre-set buffers

Initialization

Page 26: ELN5622 Embedded Systems Class 3 Spring, 2003

Main Loop Architecture 3 buttons need to be checked at least 10 times a second:

repeat

for (i=2; i--; i==0)

Checkbutton(i);

end for

until ( false )

Page 27: ELN5622 Embedded Systems Class 3 Spring, 2003

Main Loop Architecture 3 buttons need to be checked at least 10 times a second,

and 3 pins must be set 5 times a second.

repeat for (i=2; i--; i==0) CheckButton(i); end for

if (FifthSecondIsUp)

for (i=2; i<3; i--)

SetPin(i);

end for

end if

until ( false )

Page 28: ELN5622 Embedded Systems Class 3 Spring, 2003

Main Loop Architecture 3 buttons need to be checked at least 10 times a second,

and 3 pins must be set 5 times a second.

repeat for (i=2; i--; i==0)

CheckButton(i);

end for

if (FifthSecondIsUp)

for (i=2; i<3; i--)

SetPin(i);

end for

end if

until ( false )

Page 29: ELN5622 Embedded Systems Class 3 Spring, 2003

Main Loop Architecture And if there are a couple more things to be done, all at

different times …

repeat

CheckButtons()

SetPins()

ControlMotors()

SetDisplay()

until ( false )

Page 30: ELN5622 Embedded Systems Class 3 Spring, 2003

Cyclical Executive Architecture

Repeat

Task1 ()

Task2 ()

Task3 ()

Until ( false )

Page 31: ELN5622 Embedded Systems Class 3 Spring, 2003

Cyclical Executive Architecture

* Repeat

MAINLOOP:

* Task1 ()

JSR TASK1

* Task2 ()

JSR TASK2

* Task3 ()

JSR TASK3

* Until ( false )

JMP MAINLOOP

Page 32: ELN5622 Embedded Systems Class 3 Spring, 2003

Cyclical Executive Adequate for simple applications.

Other Names:– Round Robin Executive– Round Robin Kernel– Super Loop

Rules:– Tasks may not employ busy waiting– Tasks must do their work quickly and return

to the main loop so that other tasks can run– Tasks must save their place by using a state

variable

Page 33: ELN5622 Embedded Systems Class 3 Spring, 2003

Cyclical Executive Advantages:

– Small– Compact– Easy to use– Easy to understand

Disadvantages– No priorities– Polls for events– Timing responsibility is put on the programmer

Page 34: ELN5622 Embedded Systems Class 3 Spring, 2003

68HC11 On-Chip Peripherals:Parallel I/O

Page 35: ELN5622 Embedded Systems Class 3 Spring, 2003

Parallel I/O Three Functions per pins:

– Output (1 or 0)– Wired-or output (1 or 0)– Input

Up to 40 I/O pins. Each pin can be used as I/O or another

function. Some pins are fixed-direction, some are

bidirectional.

Page 36: ELN5622 Embedded Systems Class 3 Spring, 2003

Parallel I/O Port A

– Shared with Timer & Pulse Accumulator– PA7: Bidir– PA6-PA3: Output Only– PA2-PA0 : Input Only

Port B– Shared as Expanded bus Address pins– Output Only

Port C– Shared as Expanded bus Data / Multiplexed

Address pin.– Bidirectional

Page 37: ELN5622 Embedded Systems Class 3 Spring, 2003

Parallel I/O Registers PORTn

– 8-bit register for each port– Reads return the level of the pin itself for

input/bidirectional pins, or the state of the logic inside the output buffer at the pin output.

– Writes cause the data to be latched, so it can be used for output operation. (not input pins.)

PORTCL– Special Port C register for handshake writes.

Page 38: ELN5622 Embedded Systems Class 3 Spring, 2003

Parallel I/O Registers DDRn

– Register for each bidirectional pin.– Controls the direction of data flow.

• 0 = Input• 1 = Output

– Subsystem function overrides this pin function• Ie. SCI Tx/Rx

Control Registers– Determine if the pin is going to be used as General

Purpose I/O, or as a subsystem pin.– Determines if an output will be a wired-or (open

collector) output.

Page 39: ELN5622 Embedded Systems Class 3 Spring, 2003

Handshaking Ports B&C, with STRA input & STRB output

3 modes:– Simple Strobe (default)

• Port B = Simple Strobe Output• Port C = Simple Strobe Input

– Full-input Handshake– Full-output Handshake

Configured with PIOC register

Page 40: ELN5622 Embedded Systems Class 3 Spring, 2003

Handshaking - Simple Mode Port B: Output with STAB Port C: Input with STAA

Page 41: ELN5622 Embedded Systems Class 3 Spring, 2003

Handshaking - Full-Input Handshake Port C: Input with both STAA & STAB

Page 42: ELN5622 Embedded Systems Class 3 Spring, 2003

Handshaking - Full-Output Handshake Port C: Output with both STAA & STAB