Search code examples
windowsembeddedkeil

Feasibility of using the same code on both embedded and Windows platforms


We have a program written in VBA that is running on Windows machines.

We have a very similar program written in ANSI C, using a Keil IDE and compiler that is running on an STR9x uP.

Our plans were to rewrite the VBA code in .NET using C#.

What is the feasibility of writing the shared code in C++ to be used on both systems? Obviously, the .NET framework would be off limits, but that isn't much of a concern. I'm wondering, specifically, about how labor intensive you think the compilation process might be.

This is kind of a theoretical question, I know, but thanks for any thoughts.


Solution

  • I do this a as general practice. I think a better question than "is it possible" is "how should I structure my code to be able to run on both an embedded system and also a PC".

    I prefer to write the code in C and structure each file as a c++ class using static variables to make global variables private to the module. Create getter and setter functions to access the private variables. Also use function pointers which I set at initialization of the module for the methods the module need to call outside of the module.

    It is also easy to refactor from the above structured c code to a class in c# or c++.

    You can also use C++ directly but using it incorrectly on an embedded system can cause problems.

    You will need a hardware abstraction layer if you are accessing any hardware. I separate my code into two types the first being code that has no reference to what it is running on and other code which I refer to as drivers.

    I use this code for reusing modules for things like communication protocols. But more importantly I use it for testing. I like to use gtest to unit test the modules. I can also rewrite the drivers and simulate the hardware on a PC to be able to run it on the PC.