Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Portable and Robust Firmware in C

Similar presentations


Presentation on theme: "Writing Portable and Robust Firmware in C"— Presentation transcript:

1 Writing Portable and Robust Firmware in C
Embedded System Design Techniques™ Writing Portable and Robust Firmware in C Class 4: Assertions and printf September 3, 2015 Jacob Beningo, CSDP

2 Course Overview Portable C Concepts Doxygen and PC-Lint
Uart Driver and Stm32CubeMx Assertions and printf Robust Firmware Concepts

3 Session Overview printf Overview Problems with printf "Fixing" printf
Assertions Examples

4 Overview of printf printf provides a method for formatting and outputing serial data. #include <stdio.h> printf("Hello World!"); printf("The value of x is %d", x); printf("The value of x is %d, y is %d", x, y); printf("The value of pi is %3.2f", 3.14);

5 printf Capabilities %[flags][width][.precision][length][specifier]
Output Example c Character k d or i Signed decimal integer 392 e Scientific notation (mantis/exponent) using e character 3,93E+002 E Scientific notation (mantis/exponent) using E character f Decimal floating point 392,65 g Use the shorter of %e or %f 392,65 G Use the shorter of %E or %f 392,65 o Signed octal 610 610 s String of characters sample sample u Unsigned decimal integer 7235 7235 x Unsigned hexadecimal integer 7fa 7fa X Unsigned hexadecimal integer (capital letters) 7FA 7FA p Pointer address B800:0000 B800:0000 n Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored. % A % followed by another % character will produce a % character

6 Examples Requires: stdarg.h stdio.h string.h

7 Issues with printf Have to pull in standard library
ROM and RAM usage will increase Simple printf statements can affect the real-time performance of the system Limited number of printf's that can be added to a system Requires changing code to get useful data out during a debug session

8 Timing Measurements of printf
printf("Hello World!"); 12.5 ms printf("The System state is %d", State); 21 ms

9 "Fixing" printf

10 "Fixing" printf – nonblocking setup
Minimum Time Maximum Time

11 "Fixing" printf – nonblocking ISR
Interrupt Frequency ~ 1 ms ISR Execution Time 35 us

12 What is the difference between a bug and an error condition?
Assertions An assertion is a Boolean expression at a specific point in a program that will be true unless there is a bug in the program. Pros of ASSERT() Cons of ASSERT() improve testing bugs are easier to detect execution stops at them can serve as executable comments improve code quality can be turned on and off slow down code execution commonly misunderstood used improperly for error handling use string that require RAM/ROM require printf and a terminal can be turned on and off What is the difference between a bug and an error condition?

13 Assertion Use If the expression is true, execution continues normally
If the expression is false, whatever happens is "undefined" Proper Use: void CalculateDistance(uint8_t Velocity) { ASSERT(Velocity < 150); } Improper Use: int result = Open("MyFile.txt", 'r'); ASSERT(result != NULL); Mistakes: ASSERT(result = 14); ASSERT(VelocitySet(50) < VELOCITY_MAX);

14 Assert.h __DO_NOT_LINK_PROMISE_WITH_ASSERT, __ASSERT_MSG

15 Assertion Setup

16 Assertion Setup Uart_printf is interrupt driven implementation
May want to map standard printf

17 Testing the assertion

18 Testing the assertion

19 Additional Resources Download Course Material for
Updated C Doxygen Templates (May 2015) Example source code Templates Microcontroller API Standard EDN Embedded Basics Articles Embedded Bytes Newsletter From under - Blog and Articles > Software Techniques > CEC Writing Portable and Robust Firmware in C

20 Jacob Beningo Newsletters P.O. Box 400 Embedded Bytes
Linden, Michigan 48451 Newsletters Embedded Bytes Training MicroPython Bootloaders Low Power Design Real-time Software C/C++ Embedded : : : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant


Download ppt "Writing Portable and Robust Firmware in C"

Similar presentations


Ads by Google