Presentation is loading. Please wait.

Presentation is loading. Please wait.

November 16, 2012 Gary Stringham

Similar presentations


Presentation on theme: "November 16, 2012 Gary Stringham"— Presentation transcript:

1 November 16, 2012 Gary Stringham gary@garystringham.com
Writing Reusable C Code for Embedded Systems Reusable Code Tests & Concluding Remarks November 16, 2012 Gary Stringham

2 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Review of Yesterday Flexible reusable code: Compile-, build-, and run-time switches Self-adapting code Eliminate the need Use comments liberally SysLevelTestDebugPt1.pptx

3 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Today’s Topics Testing reusable code Reusable insights Review SysLevelTestDebugPt1.pptx

4 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Testing Reusable Code The same thoroughness used to test code for one product applies in testing reusable code for many products Don’t add support for a switch setting without testing it Test all switch settings and boundary conditions Use good engineering judgment to determine an appropriate mix of combinations and permutations of switch settings SysLevelTestDebugPt1.pptx

5 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Test the CPP Checks Here is an example check from Tuesday Run a test with SCAN_SCALAR undefined Run a test with FEATURE_A defined and not FEATURE_B #ifndef SCAN_SCALAR # error SCAN_SCALAR left undefined #endif #if defined(FEATURE_A) && !defined(FEATURE_B) # error Feature B should be defined when A is SysLevelTestDebugPt1.pptx

6 Test the Switches On and Off
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Test the Switches On and Off Make sure the code is tested with switches turned on and with switches turned off Helps ensure that the correct pieces of code were compiled in or out as appropriate #if FEATURE_X == MY_TRUE // Some code #elif FEATURE_X == MY_FALSE // Other code #else # error Bad CPP coding #endif SysLevelTestDebugPt1.pptx

7 Testing Switch Combinations
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Testing Switch Combinations Features A and B are somehow related, so test all four combinations of their on and off settings But testing the FEATURE_A’s on and off in combination with each of many possible integers for SCAN_SCALAR is probably not necessary #ifndef SCAN_SCALAR # error SCAN_SCALAR left undefined #endif #if defined(FEATURE_A) && !defined(FEATURE_B) # error Feature B should be defined when A is SysLevelTestDebugPt1.pptx

8 Test Features Switches
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Test Features Switches Test features, not products (yet) Thoroughly test the feature Test features’ dependency switches Test feature A on hardware X Test feature A on hardware Y (supported?) These are similar to unit-level tests Feature-level tests apply for all products A feature modified for a product needs modified feature-level tests SysLevelTestDebugPt1.pptx

9 Test Setup Switching Logic
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Test Setup Switching Logic Product and hardware switches exist only at the “beginning” and turn on appropriate features Feature switches exist throughout code Test to ensure appropriate feature switch setup Use #warn or #warning if compilers support it SysLevelTestDebugPt1.pptx

10 Test Setup Switching Logic
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Test Setup Switching Logic #if defined (PROD_GOOFY) # warn Setting up product Goofy # define SCAN_SCALAR 3 #elif defined (PROD_SMILEY) # warn Setting up product Smiley # define SCAN_SCALAR 2 # define FEATURE_MAILBOX #endif #ifdef FEATURE_MAILBOX # warn FEATURE_MAILBOX turned on #else # warn FEATURE_MAILBOX turned off #error End of Switching Test – Remove when done SysLevelTestDebugPt1.pptx

11 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Testing Products After thoroughly testing features, then configure for a product and run tests These are system-level tests When a new product comes along, adjust the switches as necessary then conduct system-level tests for the new product If a new product requires a modified feature, the feature-level test should be modified and passing before product system-level tests are run SysLevelTestDebugPt1.pptx

12 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Reusable Insights Own the same code for at least three products Collaborate with hardware engineers SysLevelTestDebugPt1.pptx

13 Own the Same Code for Three Products
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Own the Same Code for Three Products You don’t know what you need the first time you own the (new or existing) code By the third product, you know where changes are required and are motivated to make it easier Work yourself out of the porting job There’s no motivation to improve the code if it is written once and thrown away Document the porting steps so the next owner can continue SysLevelTestDebugPt1.pptx

14 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 We’re Not Stupid “… a module isn’t really reusable till it has been reused three times. … That’s not because we’re stupid; it’s simply because domain analysis is hard.” Jack Ganssle, The Embedded Muse, #167 SysLevelTestDebugPt1.pptx

15 Collaborate with Hardware Engineers
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Collaborate with Hardware Engineers Work with the hardware team in their designs Minimize changes, reducing switches required Keep bits and registers in the same location Use version numbers at the chip and block level Practice these especially with FPGAs that change frequently Work on eliminating product-specific switches This effort takes time, years SysLevelTestDebugPt1.pptx

16 Book: Hardware/Firmware Interface Design
Introduction: What You Need to Know about System Testing & Debugging 3/19/2012 Book: Hardware/Firmware Interface Design garystringham.com/hwfwbook How to design hardware to ease firmware development Many concepts support reusable code SysLevelTestDebugPt1.pptx

17 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Conclusion Use switches to allow the same code to support multiple products Use what makes sense for your application and environment Note the pitfall areas and avoid them It is an evolutionary process that takes time But it is worth it Best-case examples of porting my code: If I do it, 1 hour If others do it, 4 hours SysLevelTestDebugPt1.pptx

18 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Further Reading The Embedded Muse, Jack Ganssle, Issues 167, 168, and 170 Lessons Learned on Five Large-Scale System Developments, section titled “Excessive Faith in Code Reuse,” Nat Ozarin, IEEE Instrumentation & Measurement Magazine, Feb, 2008, pgs 18-23 One at a Time, Please, Stephen C. Dewhurst, semantics.org/publications/01_08_oneatatime.pdf SysLevelTestDebugPt1.pptx

19 Remember… Proper application of common and flexible techniques produces effective reusable code

20 Introduction: What You Need to Know about System Testing & Debugging
3/19/2012 Questions? SysLevelTestDebugPt1.pptx


Download ppt "November 16, 2012 Gary Stringham"

Similar presentations


Ads by Google