Download presentation
Presentation is loading. Please wait.
Published byMeghan Diana Ferguson Modified over 9 years ago
1
Embedded Software Programming and Implementation Guidelines Software Engineering for Embedded System Ch.7 Robert Oshana and Mark Kraeling Presented by Kraingkrai Bumroungruksa
2
Overview Principles of high-quality programming Starting the embedded software project Variable structure
3
Principles of High-Quality Programming Readability Maintainability Testability
4
Readability Clean code helps in reviewing and maintaining the software
5
Readability Poor readability code
6
Readability Better readability code
7
Maintainability Potential problems – Incorrect interpretation – Existing code breaks – Adding another “if” condition
8
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”
9
Testability Writing software with testability in mind – Unit testing – Code debugging
10
Testability Poor testability Good testability
11
Embedded Software Programmer Things to keep in mind – Resources – Hardware features – Performance
12
Starting the Embedded Software Project Hardware platform input Project files/organization Team programming guidelines Syntax standard Safety requirements in source code
13
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
14
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
15
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
16
Syntax Standard Code white space
17
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
18
Syntax Standard Alignment within source int incubator = RED_MAX; /* Setup for Red Zone */ char marker = ‘\0’; /* Marker code for zone */ – Improved version int incubator= RED_MAX;/* Setup for Red Zone */ char marker= ‘\0’; /* Marker code for zone */
19
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
20
Variable Structure Variable declarations Data types Definitions
21
Variable Declarations Global variables – Declare the variable in a header file ip.h
22
Variable Declarations Global variables – Always prefix the name with the “owner” of the variable itself – Example Input Processing IP
23
Variable Declarations Global variables – Only modify that variable by its source file – Other source files only have “read” access to the variable
24
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
25
Variable Declarations Local variables – No need to use prefix in the name – Use all lower case – Capitalize the first character for “static” variable
26
Data Types Keep an embedded system portable to other processors Add a suffix of “_t” to show it is a type definition
27
Definitions Conditional compilation – Allowing a compiler to dictate which code is compiled and which code is skipped
28
Definitions Conditional compilation – Example: 2 processors (PROCA and PROCB)
29
Definitions #define – Allowing programmers to use a particular naming convention for values
30
Ensuring the Integrity of Embedded Software with Static Code Analysis B. Chelf, C. Ebert. Presented by Kraingkrai Bumroungruksa
31
Key Contribution of the Paper A technique to detect defects early
32
Problem Any software system always might have defects Software defect in an embedded device can have dramatic consequences
33
Proposed Solution Use static code analysis tool to detect defects
34
Static Code Analysis Tool Typical defect types – Overflows, unassigned variables Hard-to-find race conditions Deadlocks in multithreaded applications
35
Six Defect Classes Division by Zero Memory Leak Null Pointer Dereference Uninitialized Variable Buffer overflow or Underflow Inappropriate Cast
36
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 }
37
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; }
38
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
39
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; else return x; // defect: “x” is not initialized }
40
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 1 if (!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 9 int *x = &buff[1]; // x now points to buff[1] x[9] = 0; // x[9] is equivalent to buff[10], which is out of bounds }
41
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; }
42
Analysing and Improving the Performance of Software Code for Real Time Embedded Systems P. Joshi, K. S. Gurumurthy Presented by Kraingkrai Bumroungruksa
43
Key Contribution of the Paper Techniques to enhance the performance of the software in ARM processor
44
Problem Poor code design leads to low performance embedded system
45
Proposed Solution Loop transformation – Loop reversal – Loop fusion – Loop unswitching
46
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
47
Loop Fusion Replacing multiple loops with a single one 60% increase in the speed of execution 65% increase in the code density
48
Loop Unswitching Moving the conditional statement outside the loop 60% increase in the code execution speed 66% decrease in the code density
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.