05 Process and Design Handouts
05 Process and Design Handouts
05 Process and Design Handouts
Intro. to Engineering
Process and
Design Techniques
18-348 Embedded System Engineering
Philip Koopman
Wednesday, 27-Jan-2016
Electrical &Computer
ENGINEERING
© Copyright 2006-2016, Philip Koopman, All Rights Reserved
… but also …
Mechanical
Electrical
Fluids
Environmental control
Food Safety
Security
Lots of engineering considerations
• Exam #1
3
Preview
Engineering projects have phases
• Marketing, product definition, requirements, architecture, design,
implementation, test, V&V, support, evolution
Requirements
• Shall vs. should
• Keep an eye on these in projects
Design
• Flowcharts
• Statecharts
• Sequence Diagrams
Implementation
• Basic coding style
• Good & Bad practices
4
Embedded System Engineers Need Perspective
How does what I do fit into the bigger picture?
• What outside constraints do I have to meet (e.g., limited battery life)
• How can I exploit the non-computer aspects of the system?
Engineering phases:
General System Life Cycle
Marketing
Product definition
Requirements
Architecture
Design
Implementation
Test
Verification &
Validation
Support
Evolution
6
Marketing & Product Definition
What is the need?
• Personal communication device
• More selection in TV programs
• Better energy efficiency in a home
1. The program shall compute the hash value of the string in memory and store the
lowest byte of the value in memory.
2. When PB1 (on the project board) is pressed, the program shall display the stored
hash value on the bar graph LED.
3. When PB1 is not pressed, all elements of the bar graph LED shall be turned off.
4. Values written to the display shall use the following convention:
» * A "1" bit shall be indicated by the corresponding LED being lit.
» * A "0" bit shall be indicated by the corresponding LED being unlit.
8
Architecture – How Do The Pieces Fit Together?
Architectures are all about “boxes and arrows”
• Boxes are the pieces
• Arrows are how they fit together
Hardware architecture
A S A S S A S A S A
9
CALLS
CALLS CALLS CALLS CALLS
10
Design – Working Out High Level Details
(Is “High Level Details” an oxymoron? – Not in computer abstractions!)
The point of design is to hide messy details so you can do the hard stuff
• Circuit board routing doesn’t (usually) affect how registers are connected
• Software design shouldn’t worry about the name of a variable used as the index
in a switch statement
11
Software
• Source code
• Header files
• Etc.
12
Test – Executing Code To Check Its Operation
How many of you write perfect code all the time?
• If you play around with code a few minutes, was that perfect testing?
13
14
Version Control
What happens if you need to go back to an old version?
• The new version doesn’t work, and you don’t remember what you changed
• The new version doesn’t work, and you can’t figure out why
• Your computer crashed and you need to restore some recent version
• Someone messed up the code base before quitting
• There is a bug in an old deployed version and you need to test small fixes
• …
Lifecycle Support
What happens after you ship version 1.0?
• Version 1.1, Emergency Bug Patch 1.1.035, and Version 2.0 happen
• You get calls at 3 AM because there is a bug in version 1.7.3.2
– … but you can’t even remember what is in that particular version!
– … and you can’t find, or even recreate the source code for it!
• You get calls at your new job from desperate people at your old job
• …
17
Flowchart Basics
18
Flowchart Pro/Con
Pro:
• Good at describing a lot of classical software…
… especially if its job is to execute a sequence of steps in mostly linear order
• Everyone seems to know how to create one
• Better than nothing
Con:
• Easy to get caught in trap of one line of code per box – pretty much useless
– Each box should be a high level operation, not just a line of code
• Subroutine calls are the only way to manage complexity – but not enough
• Usually get out of date with software, because aren’t that useful for
maintenance
19
20
Statechart Basics
Statecharts are a software finite state machine diagram
• “Bubbles” are states of a state machine
– While in each box, perform some action
• “Arrows” between bubbles are guarded state transitions
– Take an arrow of the “guard” condition is true
• Has a “reset” or initialization state
• Can be implemented via switch statements in software
21
Example Statechart
This is a controller for a multi-speed motor or other similar application
Inputs: SPDBUTTON and ONOFF
Outputs: Speed = {Stop, Slow, Med, Fast}
State names (arbitrary labels): {OFF, SLOW, MEDIUM, FAST}
System Reset is to state s1
[Koopman10]
22
Example Statechart Implementation – 1
23
24
Statechart Pro/Con
Pro:
• If you had 18-240, you already know how to do these!
– They are the software version of FSM state diagrams
• Many embedded systems have a lot of modes; great for that
– What common embedded systems have modes?
• Forcing designers to look for states generally improves designs
– Lots of duplicative nested “if” statements usually means it should have been
designed as a state machine with a “switch” statement instead
Con:
• Not every system is reducible to states
• Not good at representing flows of control (long lists of steps)
Other considerations
• Use a switch statement to convert to code, with integer state numbers
25
26
On Clarity in Requirements
A wife asks her software engineer husband, “Could you
please go shopping for me and buy one carton of milk? And
if they have eggs, get six.”
www.ganssle.com/jokes.htm.
27
28
Example Sequence Diagram
Customer ButtonControl Button_Light VendControl Vend CoinOutControl
1. mButton[s](True)
3. mVend[s](True)
Vend[s](True)
2. Light[s](True)
COUNTER = 0 COUNTER = 0
3. Dispense Soda
3. mVend[s](False) Vend[s](False)
4. Light[s](False)
5. Button[s](False)
5. mButton[s](False)
29
Con:
• Only works if you have organized code into multiple objects
30
Exercise: Sequence Diagram For Getting Voice Mail
31
32
IMPLEMENTATION
How long does software last?
• Many people act as if their software will be thrown away next week
• But cars typically live 10-15 years after sale
• Houses last up to 100 years
• Example: SAGE air defense system
– Started 1954
– Deployed 1963 – vacuum tube hardware (500,000 lines of code)
– In operation until 1983 – (IBM PC came out in 1982)
[http://history.acusd.edu/
gen/20th/sage.html]
• Example: MS-DOS. Released 1981. Still runs as command line for Windows.
• 1AESS telephone switch: 1976 through 2008+ (maybe some still working)
33
34
Why Do You Need Coding Style Sheets?
So others can understand your software quickly and accurately
• Your design colleagues
• YOU – when you look at it years later
• People who have to maintain and expand it
• Your successors, when you move on but the software stays behind
35
36
Important Practices
Avoid “magic numbers”
• If a value is used repeatedly or could possibly change, use an EQU value
– or #define
– or C++ const
37
38
Avoid Global Variables In Real Code
• Global variables can be read/written from any module in the system
– In contrast, local variables can only be seen from a particular software module
• Excessive use of globals tends to compromise modularity
– Changes to code in one place affect other parts of code via the globals
– In other words: Global Variables are Considered Harmful
41
Source: http://betterembsw.blogspot.com/2011/11/embedded-system-code-review-checklist.html
(See the detailed checklist in these handouts)
Requirements
• Shall vs. should
• Keep an eye on these in projects
Design
• Flowcharts
• Statecharts
• Sequence Diagrams
Implementation
• Basic coding style
• Good & Bad practices
• Read the course coding style sheet before recitation!
• Read the style sheet attached to this lecture and understand what it is talking about
– Not required to follow this style sheet for course projects, but might be a good idea
43
Lab Skills
State chart
• Create a state chart for a moderately complex system
Implementation
• Assembly language implementation of a state machine
• Follow course coding style for all labs after this point
44
Embedded System Code Review Checklist
Gautam Khattak & Philip Koopman
July 2012 Version 1.01
Recommended Usage:
Assign each section below to a specific reviewer, giving two or three sections to each reviewer.
Ensure that each question has been considered for every piece of code.
Review 100-400 lines of code per 1-2 hour review session. Do the review in person.
FUNCTION
F-1. Does the code match the design and the system requirements?
F-2. Does the code do what it should be doing?
F-3. Does the code do anything it should not be doing?
F-4. Can the code be made simpler while still doing what it needs to do?
F-5. Are available building blocks used when appropriate? (algorithms, data structures, types,
templates, libraries, RTOS functions)
F-6. Does the code use good patterns and abstractions? (e.g., state charts, no copy-and paste)
F-7. Can this function be written with a single point of exit? (no returns in middle of function)
F-8. Are all variables initialized before use?
F-9. Are there unused variables?
F-10. Is each function doing only one thing? (Does it make sense to break it down into smaller
modules that each do something different?)
STYLE
S-1. Does the code follow the style guide for this project?
S-2. Is the header information for each file and each function descriptive enough?
S-3. Is there an appropriate amount of comments? (frequency, location, and level of detail)
S-4. Is the code well structured? (typographically and functionally)
S-5. Are the variable and function names descriptive and consistent in style?
S-6. Are "magic numbers" avoided? (use named constants rather than numbers)
S-7. Is there any “dead code” (commented out code or unreachable code) that should be
removed?
S-8. Is it possible to remove any of the assembly language code, if present?
S-9. Is the code too tricky? (Did you have to think hard to understand what it does?)
S-10. Did you have to ask the author what the code does? (code should be self-explanatory)
ARCHITECTURE
A-1. Is the function too long? (e.g., longer than fits on one printed page)
A-2. Can this code be reused? Should it be reusing something else?
A-3. Is there minimal use of global variables? Do all variables have minimum scope?
A-4. Are classes and functions that are doing related things grouped appropriately? (cohesion)
A-5. Is the code portable? (especially variable sizes, e.g., “int32” instead of “long”)
A-6. Are specific types used when possible? (e.g., “unsigned” and typedef, not just "int")
A-7. Are there any if/else structures nested more than two deep? (consecutive “else if” is OK)
A-8. Are there nested switch or case statements? (they should never be nested)
TIMING
T-1. Is the worst case timing bounded? (no unbounded loops, no recursion)
T-2. Are there any race conditions? (especially multi-byte variables modified by an interrupt)
T-3. Is appropriate code thread safe and reentrant?
T-4. Are there any long-running ISRs? (no loops inside ISRs; should be half-page of code)
T-5. Are interrupts masked for more than a few clocks?
T-6. Is priority inversion avoided or handled by the RTOS?
T-7. Is the watchdog timer turned on? Is the watchdog kicked only if every task is executing?
T-8. Has code readability been sacrificed for unnecessary optimization?
HARDWARE
H-1. Do I/O operations put the hardware in correct state?
H-2. Are min/max timing requirements met for the hardware interface?
H-3. Are you sure that multi-byte hardware registers can’t change during read/write?
H-4. Does the software ensure that the system resets to a well defined hardware system state?
H-5. Have brownout and power loss been handled?
H-6. Is the system correctly configured for entering/leaving sleep mode (e.g. timers)?
H-7. Have unused interrupt vectors been directed to an error handler?
H-8. Has care been taken to avoid EEPROM corruption? (e.g., power loss during write)
This document is placed in the public domain. Credit to the original authors is appreciated.