Introduction to Embedded Systems Introduction to Embedded Systems - - PDF document

introduction to embedded systems introduction to embedded
SMART_READER_LITE
LIVE PREVIEW

Introduction to Embedded Systems Introduction to Embedded Systems - - PDF document

Introduction to Embedded Systems Introduction to Embedded Systems CS/ECE 6780/5780 CS/ECE 6780/5780 Al Davis Al Davis Todays topics: logistics - minor synopsis of last lecture software desig finite state machine based


slide-1
SLIDE 1

Page 1

1

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Introduction to Embedded Systems Introduction to Embedded Systems

CS/ECE 6780/5780 CS/ECE 6780/5780 Al Davis Al Davis

Today’s topics:

  • logistics - minor
  • synopsis of last lecture
  • software desig
  • finite state machine based control

2

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Logistics Logistics

  • Labs start Wed

Labs start Wed

  • make sure you attend your designated session

make sure you attend your designated session

» lab has to be signed off by relevant TA before you leave lab has to be signed off by relevant TA before you leave

  • Check web page lab section assignments

Check web page lab section assignments

  • I made one error – might be others

I made one error – might be others

» it’s a hectic term for me it’s a hectic term for me

  • Mistake last lecture

Mistake last lecture

  • #$AB – denotes a hex immediate value

#$AB – denotes a hex immediate value

  • #24 – denotes a decimal immediate value

#24 – denotes a decimal immediate value

  • color me “duh”

color me “duh”

slide-2
SLIDE 2

Page 2

3

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Last Time Last Time

  • Overview of 6812 assembly

Overview of 6812 assembly

  • reminder - read manual for full ISA

reminder - read manual for full ISA

  • assembler translates symbolic version into executable

assembler translates symbolic version into executable

  • bject code
  • bject code
  • Key things to remember

Key things to remember

  • addressing modes are key to read/write assembly code

addressing modes are key to read/write assembly code

  • CC’s

CC’s and subsequent branches are critical focus points and subsequent branches are critical focus points

» know which instruction set the know which instruction set the CC’s CC’s for the branch for the branch

  • HCS12 provides extensive math for 8-bits and wider

HCS12 provides extensive math for 8-bits and wider

» make sure you understand the relevant instructions and CC make sure you understand the relevant instructions and CC bits bits » otherwise math is what you’d expect

  • therwise math is what you’d expect
  • Assembly

Assembly

» allows full control of the HW allows full control of the HW » but permits very basic and very serious mistakes but permits very basic and very serious mistakes

  • e.g. save and restore registers on a function call

e.g. save and restore registers on a function call

  • mismatched stack frames, etc.

mismatched stack frames, etc.

4

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

ES Software Design ES Software Design

  • ES success depends on both HW and SW design

ES success depends on both HW and SW design

  • most are not large but can be quite complex

most are not large but can be quite complex

  • Requisite SW skills

Requisite SW skills

  • modular design, layered architecture, abstraction

modular design, layered architecture, abstraction

  • AND verification

AND verification

» ES’s ES’s held to a much higher “correctness” standard held to a much higher “correctness” standard

  • Writing good software is an art

Writing good software is an art

  • requires practice (e.g. this course is lab intensive)

requires practice (e.g. this course is lab intensive)

  • can not be an end of project add-on

can not be an end of project add-on

  • Good SW

Good SW w/ average HW will outperform good HW / average HW will outperform good HW w/ average SW average SW

  • why?

why?

slide-3
SLIDE 3

Page 3

5

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Good SW – what does it look like? Good SW – what does it look like?

  • Quantitative measures

Quantitative measures

  • Dynamic efficiency

Dynamic efficiency – number of CPU cycles and power – number of CPU cycles and power

  • Static efficiency

Static efficiency – RAM/ROM code/data footprint – RAM/ROM code/data footprint

  • Design constraints satisfied?

Design constraints satisfied?

  • Qualitative measures

Qualitative measures

  • Ease of debug

Ease of debug

  • Ease of verification – prove correct

Ease of verification – prove correct

  • Ease of maintenance – enhance features

Ease of maintenance – enhance features

  • Note

Note

  • sacrifice clarity to enhance speed is usually a bad choice

sacrifice clarity to enhance speed is usually a bad choice

» leads to bugs and leads to maintenance nightmares leads to bugs and leads to maintenance nightmares

  • You’re a good programmer when:

You’re a good programmer when:

  • you can understand your code a year later

you can understand your code a year later

  • others find it relatively easy to modify your code
  • thers find it relatively easy to modify your code

6

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

SW Maintenance SW Maintenance

  • Extremely important design phase

Extremely important design phase

  • may persist longer than other phases

may persist longer than other phases

» think think iTunes iTunes vs. a particular

  • vs. a particular iPod

iPod platform platform

  • Includes

Includes

  • initial bug fixes

initial bug fixes

  • add features

add features

  • optimization
  • ptimization
  • porting to new hardware

porting to new hardware

  • porting to new OS or run-time system

porting to new OS or run-time system

  • reconfigure to handle new requirements

reconfigure to handle new requirements

  • Documentation

Documentation

  • should assist maintenance

should assist maintenance

  • resides in and outside of the SW itself

resides in and outside of the SW itself

» caveat – programmer and external tech writer are seldom the caveat – programmer and external tech writer are seldom the same people same people

  • ever read the manual and then decided just to figure it out?

ever read the manual and then decided just to figure it out?

slide-4
SLIDE 4

Page 4

7

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Comments Comments

  • Internal to the SW documentation

Internal to the SW documentation

  • restating the operation doesn’t add to the content

restating the operation doesn’t add to the content

  • Variable definition – explain how it’s used

Variable definition – explain how it’s used

  • Constant definition – explain meaning

Constant definition – explain meaning

8

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Subroutine Comments Subroutine Comments

  • On definition – 2 types of comments needed

On definition – 2 types of comments needed

  • client comments (place in header or subroutine start)

client comments (place in header or subroutine start)

» explain how function is used explain how function is used » how parameters are passed how parameters are passed

  • input: call by value or reference

input: call by value or reference

– range & format (8/16 bit, signed/unsigned, etc.) range & format (8/16 bit, signed/unsigned, etc.) – examples if appropriate examples if appropriate

  • output: return by value or reference
  • utput: return by value or reference

– range & format range & format – examples if appropriate examples if appropriate

» describe errors and results that are returned describe errors and results that are returned » example calling sequence example calling sequence » local variables and their significance local variables and their significance

  • colleague comments

colleague comments

» explain how the function works within the function body explain how the function works within the function body

slide-5
SLIDE 5

Page 5

9

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Self-Documenting Code Self-Documenting Code

  • SW written in a way that both purpose and function are

SW written in a way that both purpose and function are self-apparent self-apparent

  • use descriptive names for variables, constants, & functions

use descriptive names for variables, constants, & functions

» remember that someone else be doing maintenance remember that someone else be doing maintenance

  • 5780: this doesn’t really apply except to your lab partner

5780: this doesn’t really apply except to your lab partner

  • but might as well develop good habits and stick to it

but might as well develop good habits and stick to it

» document your naming convention document your naming convention

  • formulate and organize your code

formulate and organize your code

» into a well defined hierarchy of sub-components into a well defined hierarchy of sub-components » ideal ideal

  • match problem decomposition with program structure

match problem decomposition with program structure

  • liberal use of #define or

liberal use of #define or equ equ statements helps statements helps

10

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Using #define Using #define

slide-6
SLIDE 6

Page 6

11

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Naming Conventions Naming Conventions

  • Names should have meaning

Names should have meaning

  • avoid ambiguities

avoid ambiguities

  • give hints about type

give hints about type

  • use same name to refer to the same type of object

use same name to refer to the same type of object

  • Some basic conventions

Some basic conventions

  • use prefix to identify public or global objects

use prefix to identify public or global objects

  • use upper and lower case to specify object scope

use upper and lower case to specify object scope

  • use capitalization to delimit words

use capitalization to delimit words

  • Companies often have their own conventions

Companies often have their own conventions

  • since original code monkey and maintenance may involve

since original code monkey and maintenance may involve different people different people

  • often have a specific documentation trail
  • ften have a specific documentation trail

» often interned in the code repository

  • ften interned in the code repository
  • svn

svn, , rcs rcs, …, there are many , …, there are many

» releases often include a “what’s changed & why” log releases often include a “what’s changed & why” log

12

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Examples Examples

Given this table what are the conventions?

slide-7
SLIDE 7

Page 7

13

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Abstraction Abstraction

  • A SW abstraction factors common functionality out of

A SW abstraction factors common functionality out of diverse examples diverse examples

  • advantages

advantages

» faster to develop because some building blocks already exist faster to develop because some building blocks already exist » easier to debug easier to debug

  • due to separation of concept and implementation

due to separation of concept and implementation

» easier to understand easier to understand

  • understand the abstraction and then see how it is implemented

understand the abstraction and then see how it is implemented

» easier to change easier to change

  • if you understand it you know how to change it

if you understand it you know how to change it

  • Finite state machine (FSM)

Finite state machine (FSM)

  • simple concept consisting of:

simple concept consisting of:

» states, inputs, outputs, and state transitions states, inputs, outputs, and state transitions

  • FSM software is easy to understand, debug, & modify

FSM software is easy to understand, debug, & modify

  • works equally well in HW or SW

works equally well in HW or SW

14

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

FSM Concepts FSM Concepts

  • When are outputs assigned?

When are outputs assigned?

  • by state ::= Moore machine

by state ::= Moore machine

  • by transition ::= Mealy machine

by transition ::= Mealy machine

  • equally powerful

equally powerful

» so use the version that is most intuitive for the problem at so use the version that is most intuitive for the problem at hand hand » turn the crank procedure exists to convert Mealy turn the crank procedure exists to convert Mealy   Moore Moore

  • When are the inputs observed

When are the inputs observed

  • when they happen

when they happen  asynchronous FSM asynchronous FSM

  • based on some periodic time step

based on some periodic time step  synchronous FSM synchronous FSM

» we’ll focus on this one since we’ll focus on this one since asynch asynch FSM’s FSM’s have some have some additional complexity additional complexity » and most microcontrollers are synchronous and most microcontrollers are synchronous

  • which makes synch

which makes synch FSM’s FSM’s an easy and natural choice an easy and natural choice

  • Anybody use this for their lab1 abstraction?

Anybody use this for their lab1 abstraction?

  • when? ==

when? == button_push button_push

slide-8
SLIDE 8

Page 8

15

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

2-bit Gray Code FSM 2-bit Gray Code FSM

00 01 11 01 reset Moore or Mealy? Where are the inputs? What is λ ? ? λ λ λ λ

16

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Mealy 2-bit Gray Counter Mealy 2-bit Gray Counter

reset λ/00 λ/01 λ/11 λ/10 In the case where there are no inputs – Mealy vs. Moore distinction is moot. reset – ALWAYS needs to be there since it helps to know which state is in play when you start. Looks like we need a timer.

slide-9
SLIDE 9

Page 9

17

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

FSM’s FSM’s in HW in HW

CL S R G CL inputs NS Out CLK CL S R G O R G inputs Out NS Out Moore Mealy

18

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

6812 Timer Details 6812 Timer Details

  • TCNT is a 16-bit unsigned counter

TCNT is a 16-bit unsigned counter

  • increments at a rate defined by 3

increments at a rate defined by 3 prescale prescale bits in TSCR2 bits in TSCR2

» bit 7 in TSCR1 enables use of the TCNT timer bit 7 in TSCR1 enables use of the TCNT timer

  • assuming a 4 MHz E clock

assuming a 4 MHz E clock

  • When TCNT overflows, TOF flag in TFLG2 register is set

When TCNT overflows, TOF flag in TFLG2 register is set

» causes an interrupt if the TOI bit in TSCR2 is set. causes an interrupt if the TOI bit in TSCR2 is set.

/2PR-bits

slide-10
SLIDE 10

Page 10

19

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Implementing a Time Delay Implementing a Time Delay

Note modular (maybe taken too far)

20

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Traffic Light Traffic Light

  • 2 one-way roads

2 one-way roads

  • ther ways to do it?
slide-11
SLIDE 11

Page 11

21

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Moore FSM & State Table Moore FSM & State Table

For every input next_state must be spec’d

22

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Implementing the FSM in SW Implementing the FSM in SW

  • Required

Required

  • initialize timer (in this case)

initialize timer (in this case)

  • initial state specified

initial state specified

» entry point or go there on some reset signal entry point or go there on some reset signal

  • build FSM controller

build FSM controller

» implementation options implementation options

  • mess of jumps (OK for simple state machine – bad for complex)

mess of jumps (OK for simple state machine – bad for complex)

– bigger code footprint bigger code footprint – con: complete rewrite if FSM changes con: complete rewrite if FSM changes

  • table based

table based

– bigger data footprint bigger data footprint

  • pointer based

pointer based

– even bigger data footprint but will run faster even bigger data footprint but will run faster – in this case you don’t really care in this case you don’t really care

  • maybe some others?

maybe some others?

slide-12
SLIDE 12

Page 12

23

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Goto Goto Pseudocode Pseudocode

goN:

  • utput = 0x21;

wait(30); if (E==0) goto goN; waitN:

  • utput = 0x22;

wait(5) goE:

  • utput = 0x0C;

wait(30); if (N==0) goto goE; waitE:

  • utput = 0x14;

wait(5); goto goN;

24

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Table vs. Pointer Implementation in C Table vs. Pointer Implementation in C

slide-13
SLIDE 13

Page 13

25

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Assembly: Setting up constants Assembly: Setting up constants

26

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Assembly Main Control Loop Assembly Main Control Loop

slide-14
SLIDE 14

Page 14

27

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Partial Memory Map Partial Memory Map

28

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Execution Execution

slide-15
SLIDE 15

Page 15

29

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Execution Execution

30

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Execution Execution

slide-16
SLIDE 16

Page 16

31

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Execution Execution

32

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Execution Execution

slide-17
SLIDE 17

Page 17

33

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Execution Execution

34

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Exectution Exectution

slide-18
SLIDE 18

Page 18

35

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Executioon Executioon

36

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Weary Robot Interface Weary Robot Interface

slide-19
SLIDE 19

Page 19

37

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Mealy FSM for a Robot Controller Mealy FSM for a Robot Controller

Difference – output depends on transition so 2 tables: next_state & output go through the example in your text

38

CS 5780 CS 5780

Sc School of hool of Computing Computing Univ Univer ersity of sity of Utah Utah

Concluding Remarks Concluding Remarks

  • FSM’s

FSM’s are your friend are your friend

  • nice abstraction mechanism

nice abstraction mechanism

» works well for both HW & SW control design works well for both HW & SW control design » context aware transfer function model of the world context aware transfer function model of the world

  • context = state

context = state

  • transfer function = CL model = output response to input stimulus

transfer function = CL model = output response to input stimulus

  • context + transfer function models

context + transfer function models  FSM FSM

  • multiple implementation styles

multiple implementation styles

» choice depends on problem/environment constraints choice depends on problem/environment constraints

  • code or data footprint

code or data footprint

  • execution speed

execution speed

» if no critical constraints if no critical constraints

  • then pick the implementation strategy that max’s clarity

then pick the implementation strategy that max’s clarity

  • usually transparency between abstract FSM (state graph) and code

usually transparency between abstract FSM (state graph) and code

  • Other FSM benefits

Other FSM benefits

  • turn the crank minimization procedures

turn the crank minimization procedures

» developed for HW designs also apply to SW – NICE! developed for HW designs also apply to SW – NICE!

  • any intro book on digital design will show you how

any intro book on digital design will show you how

  • What what should you do if your FSM gets huge?

What what should you do if your FSM gets huge?