48
Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by Kraingkrai Bumroungruksa

Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Embed Size (px)

Citation preview

Page 1: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Embedded Software Programming and Implementation Guidelines

Software Engineering for Embedded SystemCh.7

Robert Oshana and Mark Kraeling

Presented by Kraingkrai Bumroungruksa

Page 2: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Overview

• Principles of high-quality programming• Starting the embedded software project• Variable structure

Page 3: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Principles of High-Quality Programming

• Readability• Maintainability• Testability

Page 4: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Readability

• Clean code helps in reviewing and maintaining the software

Page 5: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Readability

• Poor readability code

Page 6: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Readability

• Better readability code

Page 7: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Maintainability

• Potential problems– Incorrect interpretation– Existing code breaks – Adding another “if” condition

Page 8: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Maintainability

• Solution– Descriptive comments

• Bad comment– “Set timer to 10 seconds”

• Good comment– “Reset timer because if we are here we have received

a properly formatted, CRC-checked, ping request message”

Page 9: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Testability

• Writing software with testability in mind– Unit testing– Code debugging

Page 10: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Testability

• Poor testability

• Good testability

Page 11: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Embedded Software Programmer

• Things to keep in mind– Resources– Hardware features– Performance

Page 12: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Starting the Embedded Software Project

• Hardware platform input• Project files/organization • Team programming guidelines • Syntax standard • Safety requirements in source code

Page 13: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Hardware Platform Input

• Make the connection with the hardware developers early– Hardware interrupt request lines– Memory size– On-chip & off-chip resources– Hardware I/O interfaces– Debugging interface

Page 14: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Project Files Organization

• Separate the following items– Source files written locally – Source files from company libraries – Libraries from third parties – Libraries from compiler/linker toolset

Page 15: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Team Programming Guidelines

• Software Guidelines Checklist – Conformance to syntax standard– Number of source lines per function / per file – Run through code formatter – No compiler warnings – Comment and design document understandability

Page 16: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Syntax Standard

• Code white space

Page 17: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Syntax Standard

• Tabs in source files – Tab characters should not be used • Tab character could be interpreted differently by

source editing tools

– Source code editors provide a way to substitute spaces with the tab character

Page 18: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Syntax Standard

• Alignment within source

int incubator = RED_MAX; /* Setup for Red Zone */char marker = ‘\0’; /* Marker code for zone */

–Improved versionint incubator = RED_MAX; /* Setup for Red Zone */char marker = ‘\0’; /* Marker code for zone */

Page 19: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Safety Requirements in Source Code

• Need to have fail-safe operations • Safety sections clearly marked to standard • Checks are in place to make sure safety-critical

code is executed on-time • Periodic flash and RAM checks are done to

check hardware correctness • Safety-critical data is protected by regular CRC

or data integrity checks

Page 20: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Variable Structure

• Variable declarations • Data types • Definitions

Page 21: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Variable Declarations

• Global variables– Declare the variable in a header file • ip.h

Page 22: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Variable Declarations

• Global variables– Always prefix the name with the “owner” of the

variable itself – Example• Input Processing IP

Page 23: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Variable Declarations

• Global variables– Only modify that variable by its source file– Other source files only have “read” access to the

variable

Page 24: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Variable Declarations

• File scope variables– Share data between multiple functions in a single

source file – Should make the variables “static”• To make it local and not being used by other files

Page 25: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Variable Declarations

• Local variables– No need to use prefix in the name– Use all lower case– Capitalize the first character for “static” variable

Page 26: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Data Types

• Keep an embedded system portable to other processors

• Add a suffix of “_t” to show it is a type definition

Page 27: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Definitions

• Conditional compilation – Allowing a compiler to dictate which code is

compiled and which code is skipped

Page 28: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Definitions

• Conditional compilation – Example: 2 processors (PROCA and PROCB)

Page 29: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Definitions

• #define – Allowing programmers to use a particular naming

convention for values

Page 30: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Ensuring the Integrityof Embedded Software

with Static Code Analysis

B. Chelf, C. Ebert.

Presented by Kraingkrai Bumroungruksa

Page 31: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Key Contribution of the Paper

• A technique to detect defects early

Page 32: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Problem

• Any software system always might have defects

• Software defect in an embedded device can have dramatic consequences

Page 33: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Proposed Solution

• Use static code analysis tool to detect defects

Page 34: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Static Code Analysis Tool

• Typical defect types – Overflows, unassigned variables

• Hard-to-find race conditions • Deadlocks in multithreaded applications

Page 35: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Six Defect Classes

• Division by Zero • Memory Leak • Null Pointer Dereference • Uninitialized Variable • Buffer overflow or Underflow • Inappropriate Cast

Page 36: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Division by Zero

• Runtime error

void divide_by_zero(int x, int y) { int z = x - y; // If x==y, z will be zero y = y / z; // Possible divide by zero error}

Page 37: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Memory Leak • Program should de-allocate the memory once it

has finished using it

int leak_example(int c) { void *p = malloc(10); if (c) return -1; // DEFECT: “p” is leaked /* ... */ free(p); return 0; }

Page 38: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Null Pointer Dereference • A pointer to the memory address 0

void bad_malloc() { // malloc returns NULL on error struct some_struct *x = (struct some_struct*) malloc(sizeof(*x)); // ERROR: memset dereferences possibly NULL pointer x memset(x, 0, sizeof(*x));}

• Function malloc can return NULL when it’s impossible to satisfy the memory allocation request

Page 39: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Uninitialized Variable • A variable is used without first setting it to a

defined value, thus creating unexpected results

int uninit_example(int c) {int x;

if(c)return c;

elsereturn x; // defect: “x” is not initialized

}

Page 40: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Buffer Overflow or Underflow • An attempt to write data into the buffer in a memory location

beyond that buffer’s scope

void overrun() {struct some_struct vmax_mtd[2]; // vmax_mtd has 2 elements, index 0 and 1if (!vmax_mtd[1] && !vmax_mtd[2]) {

// incorrectly accessing vmax_mtd[2]return;

}}

void overrun_pointer() {int buf[10]; // buff has 10 elements, index 0 to 9int *x = &buff[1]; // x now points to buff[1]x[9] = 0; // x[9] is equivalent to buff[10], which is out of bounds

}

Page 41: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Inappropriate Cast

• Inappropriate cast can alter the variable’s value in unexpected ways

int char_io() {char c;c = getchar(); // Returns an int value to c, which is a char

return c;}

Page 42: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Analysing and Improvingthe Performance of Software Code for Real Time Embedded Systems

P. Joshi, K. S. Gurumurthy

Presented by Kraingkrai Bumroungruksa

Page 43: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Key Contribution of the Paper

• Techniques to enhance the performance of the software in ARM processor

Page 44: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Problem

• Poor code design leads to low performance embedded system

Page 45: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Proposed Solution

• Loop transformation – Loop reversal – Loop fusion– Loop unswitching

Page 46: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Loop Reversal

• Reversing the order in which values are assigned to the index variable

• Counting down to zero with the decrement operator is faster than counting up to a number of iterations with the increment operator

• 30% increase in the speed of execution• 20% increase in the code density

Page 47: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Loop Fusion

• Replacing multiple loops with a single one

• 60% increase in the speed of execution• 65% increase in the code density

Page 48: Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by

Loop Unswitching

• Moving the conditional statement outside the loop

• 60% increase in the code execution speed

• 66% decrease in the code density