C++ for 8051 microcontroller?

10,643

Solution 1

I would question whether this is really a good idea in the first place. I understand the reasoning behind wanting to use c++ over C in the general case but in the case of an 8-bit Harvard architecture microcontroller I would warn against this.

Things to bear in mind include:

  • Source-level debugging support will be somewhere between poor and impossible.
  • Runtime overhead of OOP on an 8-bit machine. I would strongly recommend doing some serious benchmarking before committing to a tool.
  • Memory isn't cheap in embedded systems and you will no doubt have some address space limitations.

Also, if you really are going to be doing some serious string handling I would recommend using the C standard library rather than a string object library simply because you have better control over in-place substitution and so string copies become glaringly obvious in the code.

Please post a little about the microcontroller you plan to use (Data Memory, Program Memory) and whether there are any performance requirements that must be met so we can help a little more concretely.

Solution 2

IAR Systems have a 8051 compiler which can compile C++ natively (no translation to C), and source level debugging shouldn't be a problem either.

Solution 3

There is a commercial compiler from ceibo.

However if you can use c++ (especially STL string) depends on how much resources (both ROM and RAM you will have.

There's a 8051 site with forums, tutorials and downloads where you may get some more resources for programming the 8051.

Solution 4

You might want to consider providing additional detail about the sort of program that you intend to run on that microcontroller:

You mentioned C++, as well as C# in your posting, both of which are surely not ideally used for heavy string processing on a microcontroller, not to mention that you are probably considering heavy use of the STL, which would furthermore increase the size of the executable?

So what exactly are your primary constraints (RAM, CPU, ROM etc)?

If you really think that you need to do this string processing in an OO fashion, you might want to consider running a lightweight embedded scripting interpreter on the controller, so that you can then provide your string processing routines using the scripting language, while the interpreter itself would be ANSI C compiled to a HEX file (e.g. lua or nasal would both seem like suitable candidates).

However, take into account that a scripting language such as lua will usually impose approximately 100kb+ of overhead in space, Nasal is somewhat more lightweight and may compile down to 50-70 kb if you disable certain extensions.

Also, there are other scripting interpreters available that are specifically meant to be used on embedded platforms.

Solution 5

IAR appears to offer a C/C++ compiler for 8051'sa C/C++ compiler for 8051's. -- But in full disclosure, I have only used Keil's C compilers for 8051 development.

As for your header-file concerns: The header files are often distributed by either the IDE vendor or the hardware manufacturer and often provide a symbolic representation of your register mappings. A modest amount of massaging may be required to incorporate a C-based header file into a C++ project. -- If you're about to switch IDE's / compilers you can often expect some massaging of your source code to accommodate the new compiler. (Read: accessing C code from a C++ code-base often causes me to stop for a day to do it right.)

Share:
10,643

Related videos on Youtube

CodeConfused
Author by

CodeConfused

Updated on April 16, 2022

Comments

  • CodeConfused
    CodeConfused about 2 years

    Could someone please tell me if it's possible to burn an 8051 microcontroller with a C++ program? I've tried searching about it online but can't seem to find out for sure if it's possible or not. Keil uses C, but the program I need to write is very string-intensive and C is quite string-unfriendly as compared to C# which is what I'm used to using. At the moment, I'm trying my hand at writing the code in C but it's getting really messy, so I'd be extremely relieved if I could write it in C++ instead.

    I would need a C++ compiler that creates a Hex output file that can then be burnt onto the microcontroller. Anyone heard of something I could use? And also, C uses a header file that lets you refer to ports, but when I tried to find out if this header file is used in C++ as well I couldn't find any information on it.

    Addition: The microcontroller I'm using is an Atmel AT89C51 with 4K Bytes of Reprogrammable Flash Memory, and 128 x 8-bit Internal RAM. This is actually for a Robot for a project at university and the coding does not actually require OOP. It just has a lot of look up tables that are in 2D string array format. The only reason I wanted to consider C++ was because of how messy manipulating strings seemed to be getting (due to MY lack of expertise in C).

    And does anyone know about the header file? C uses #include reg51.h but I tried to find out if this works for C++ and couldn't find anything on it.

    • Venedictos
      Venedictos almost 15 years
      I don't know if there is a native way, but you could always use the LLVM c++ to C translater.
    • Nathaniel Sharp
      Nathaniel Sharp almost 15 years
      Well how much memory do you have (both ROM and RAM)?
    • Turbo J
      Turbo J about 13 years
      Forget it! You will probably need every single bit of your 128 Byte RAM and 4096 Byte Flash. Using assembly language is strongly recommended...
  • Nathaniel Sharp
    Nathaniel Sharp almost 15 years
    "Source-level debugging support will be somewhere between poor and impossible". IMHO it won't be different from debugging C. If the target has JTAG it should be fairly strait forward if the debugger has support for it.
  • hieubk
    hieubk almost 15 years
    From what I understand, all of the 8051 C++ tools translate to C and use a popular platform such as Keil tools or IAR systems. It is these tools that then perform the IDE debugging support. Of course I could be completely mistaken :-)
  • Steve Melnikoff
    Steve Melnikoff almost 15 years
    For a processor like this, running a scripting engine would be massive overkill. Far better to write some efficient string routines in C or C++.
  • CodeConfused
    CodeConfused almost 15 years
    @none - I'm a she by the way! :-)
  • CodeConfused
    CodeConfused almost 15 years
    I'm beginning to think you're right, Stephen. :( There just isn't anything easily available.
  • Nate
    Nate almost 15 years
    I've used Keil before too. IMHO, their IDE is the best option for 8051 C development. -- But my scans of the website lead me to conclude they do not offer C++ compilers for 8051's. Am I missing something?
  • Nate
    Nate almost 15 years
    Here's a link for IAR's C/C++ 8051 compiler: iar.com/website1/1.0.1.0/244/1
  • JesperE
    JesperE almost 15 years
    Thanks, post updated. I was just too lazy to dig up the link.
  • NotMe
    NotMe almost 15 years
    I just spoke with one of their dev's, and you're right. It is C only. So, my answer doesn't really meet the OP's requirements. ;
  • rlbond
    rlbond almost 15 years
    Most 8051 controllers i've seen have around 4kb of memory.
  • Johann Gerell
    Johann Gerell over 14 years
    Just a clarification. As you know, std::string is just a typedef for basic_string, with char and an allocator. Instead of that bulk allocator, a custom allocator can be implemented, where no heap memory is used.
  • bk1e
    bk1e over 14 years
    @Johann Gerell: I suspect that writing a custom STL allocator that manages strings stored in ROM would be significantly more difficult (if even possible) than rewriting the string processing in C. Keep in mind that the OP's target system has only 128 bytes of RAM and 4 KB of flash. That means that the lookup tables must be stored in flash.
  • Kuba hasn't forgotten Monica
    Kuba hasn't forgotten Monica almost 11 years
    Remember that C++ does compile-time computation/code generation so it can in principle generate better code than any C compiler ever would. I'm working on an LLVM port to an 8 bit architecture (sorry, can't say any more than that), and the C++ front-end generates beautiful numerical code that's impossible to generate from pure C! I've basically got a hacked-up version of Eigen 3 generating fixed-point numerical code for an 8 bit architecture with hardware multiplier. Code that's on par with hand-written assembly, by the way.
  • hieubk
    hieubk almost 11 years
    Interesting. Come back and let me know the details when your work is declassified/commercialized. Sounds like lots of fun!