Search code examples
cembeddedrequirements

How to make your embedded C code immune to requirement changes without adding too much overhead and complexity?


In many embedded applications there is a tradeoff between making the code very efficient or isolating the code from the specific system configuration to be immune to changing requirements.

What kinds of C constructs do you usually employ to achieve the best of both worlds (flexibility and reconfigurabilty without losing efficiency)?

If you have the time, please read on to see exactly what I am talking about.

When I was developing embedded SW for airbag controllers, we had the problem that we had to change some parts of the code every time the customer changed their mind regarding the specific requirements. For example, the combination of conditions and events that would trigger the airbag deployment changed every couple weeks during development. We hated to change that piece of code so often.

At that time, I attended the Embedded Systems Conference and heard a brilliant presentation by Stephen Mellor called "Coping with changing requirements". You can read the paper here (they make you sign-up but it's free).

The main idea of this was to implement the core behavior in your code but configure the specific details in the form of data. The data is something you can change easily and it can even be programmable in EEPROM or a different section of flash.

This idea sounded great to solve our problem. I shared this with my colleague and we immediately started reworking some of the SW modules.

When trying to use this idea in our coding, we encountered some difficulty in the actual implementation. Our code constructs got terribly heavy and complex for a constrained embedded system.

To illustrate this I will elaborate on the example I mentioned above. Instead of having a a bunch of if-statements to decide if the combination of inputs was in a state that required an airbag deployment, we changed to a big table of tables. Some of the conditions were not trivial, so we used a lot of function pointers to be able to call lots of little helper functions which somehow resolved some of the conditions. We had several levels of indirection and everything became hard to understand. To make a long story short, we ended up using a lot of memory, runtime and code complexity. Debugging the thing was not straightforward either. The boss made us change some things back because the modules were getting too heavy (and he was maybe right!).

PS: There is a similar question in SO but it looks like the focus is different. Adapting to meet changing business requirements?


Solution

  • As another point of view on changing requirements ... requirements go into building the code. So why not take a meta-approach to this:

    1. Separate out parts of the program that are likely to change
    2. Create a script that will glue parts of source together

    This way you are maintaining compatible logic-building blocks in C ... and then sticking those compatible parts together at the end:

    /* {conditions_for_airbag_placeholder} */
    if( require_deployment)
         trigger_gas_release()
    

    Then maintain independent conditions:

    /* VAG Condition */
    if( poll_vag_collision_event() )
        require_deployment=1
    

    and another

    /* Ford Conditions */
    if( ford_interrupt( FRONT_NEARSIDE_COLLISION )) 
        require_deploymen=1
    

    Your build script could look like:

    BUILD airbag_deployment_logic.c WITH vag_events
    TEST airbag_deployment_blob WITH vag_event_emitter
    

    Thinking outloud really. This way you get a tight binary blob without reading in config. This is sort of like using overlays http://en.wikipedia.org/wiki/Overlay_(programming) but doing it at compile-time.