• AVR Freaks

Hot!Harmony in C++??

Author
billreinhold
Super Member
  • Total Posts : 186
  • Reward points : 0
  • Joined: 2011/02/10 16:35:50
  • Location: Atlanta, GA
  • Status: offline
2019/03/17 03:31:50 (permalink)
0

Harmony in C++??

Any idea if there are future plans for Harmony to generate C++ classes instead of (or in addition to) C?  
 
Anybody else interested? 
post edited by billreinhold - 2019/03/17 03:39:25
#1

10 Replies Related Threads

    jdeguire
    Super Member
    • Total Posts : 457
    • Reward points : 0
    • Joined: 2012/01/13 07:48:44
    • Location: United States
    • Status: offline
    Re: Harmony in C++?? 2019/03/18 10:51:17 (permalink)
    0
    I would guess that Harmony will remain C only for a very long time, probably forever, because it in theory** will work with both C and C++ code.  There are also customers who are rather averse to C++, based on what I've seen on these forms and as MASTERs, so sticking with C would be the "safer" option.  Note that Harmony does use a fair bit of dynamic dispatch through structs of function pointers, so you could say that it is at least OO-like. 
     
    Our solution was to wrap the Harmony low-level peripheral library into C++ classes; some of our C++ code actually predates Harmony, so they actually used to access device registers directly.  The low-level library does basically the same thing, but we can more easily handle different devices by letting Harmony figure it out.
     
    **In practice, this isn't really true.  Harmony does a LOT of implicit conversions, which is a no-no in C++, and so you'll get a whole bunch of errors until you clean those up.  There's also other things like "extern C" declarations being in the wrong spots and forward declarations of enums that break with C++11 (the latter happens in the new graphics library).  Microchip did clean up a good bit of this long ago in the Harmony v1.xx days, but has made little progress here in Harmony 2.xx.
     
    I have not yet tried out Harmony 3, so maybe more progress was made there.
    #2
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: Harmony in C++?? 2019/03/19 16:11:27 (permalink)
    0
    We've been developing in C++ for a few years now and have a nice library of C++ classes wrapping the harmony calls as you described.  That's made development and maintenance so much easier and the code so much cleaner, which in turn makes it much quicker to get new developers up to speed.  But we're still on Harmony 1.06 and I had to do a lot of work on it to get it compiling and running with C++.  Totally worth it though.  
     
    I can't imagine anyone being averse to C++.  Other than problems with Microchip library code incompatibility I can't think of a single disadvantage.  What are they afraid of?  
    #3
    JPortici
    Super Member
    • Total Posts : 679
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: online
    Re: Harmony in C++?? 2019/03/20 00:52:48 (permalink)
    0
    I don't do C++.
    that's because 90% of the code i write is for PIC18 and dsPIC (so C), 9% for UI stuff (B4A/B4i for apps, C# and VB6 for PC) and 1% for PIC32... Why bother with C++ then? maybe if there was g++ for dsPIC..
     
    and all the books and tutorials i've been able to find on C++ spent like a couple of chapters on the language but then everything was focused on databases. databases databases and databases.
     
    Having a good reference on how to write GOOD C++ tagetting embedded devices (including peripherals and peripheral drivers) would be lovely, but i wasn't able to find something like that.
    If you can suggest a book i'm all ears :)
     
    #4
    crosland
    Super Member
    • Total Posts : 1576
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: Harmony in C++?? 2019/03/20 02:27:02 (permalink)
    5 (1)
    Most of the aversion to C++ seems to be based on fake news about code bloat.
     
    When you compare *like for like* C++ is no less efficient than C, it's just that C++ does a lot more.
    #5
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: Harmony in C++?? 2019/03/20 04:56:39 (permalink)
    0
    Jack_M
    1% for PIC32... Why bother with C++ then? 

    Good point, but that also means you probably don't use Harmony very much either.  In my case it's reversed -- we're mostly PIC32, which has both Harmony and C++, the two of which are essentially incompatible.  To me that just makes no sense.  
     
    I can't recommend a book, but for peripherals all we do is wrap the ones we use in C++ classes.  That way only one developer has to struggle through the Harmony documentation and from then on everyone can use the working code by just declaring a new object and calling our well-named methods, which takes advantage of the code completion features in NetBeans.  So much easier.  
     
    Regarding code bloat -- I was certainly worried about that when we got started and spent some time doing trials to see if that would be a problem.  It didn't seem to be and I pulled the trigger.  Three-plus years down the road the code size turned out considerably smaller than I expected.  
     
    I get that Microchip has huge code bases in C but I really do not want to go back to C for application development.
    #6
    Paul PortSol
    Super Member
    • Total Posts : 439
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: Harmony in C++?? 2019/03/20 05:11:12 (permalink)
    5 (2)
    I would switch to C++ in a heartbeat just for the extra checking (Type checks, etc.), even if I only wrote in C in cpp files.
    The agony of my short attempt of Harmony with C++, as a lone developer, quickly killed the attempt.
     
    Paul
    #7
    jdeguire
    Super Member
    • Total Posts : 457
    • Reward points : 0
    • Joined: 2012/01/13 07:48:44
    • Location: United States
    • Status: offline
    Re: Harmony in C++?? 2019/03/20 09:58:48 (permalink)
    0
    breinhold
    Good point, but that also means you probably don't use Harmony very much either.  In my case it's reversed -- we're mostly PIC32, which has both Harmony and C++, the two of which are essentially incompatible.  To me that just makes no sense.  
     
    I can't recommend a book, but for peripherals all we do is wrap the ones we use in C++ classes.  That way only one developer has to struggle through the Harmony documentation and from then on everyone can use the working code by just declaring a new object and calling our well-named methods, which takes advantage of the code completion features in NetBeans.  So much easier.  
     
    Regarding code bloat -- I was certainly worried about that when we got started and spent some time doing trials to see if that would be a problem.  It didn't seem to be and I pulled the trigger.  Three-plus years down the road the code size turned out considerably smaller than I expected.  
     
    I get that Microchip has huge code bases in C but I really do not want to go back to C for application development.

    Our group is in the same boar as you are in that we now mainly focus on PIC32s (we started with PIC24s long ago) and will probably end up moving to SAM parts in the future.  For our needs C++ has generally worked well despite our having to fight Harmony, MPLAB X, and the fact that XC32 only half-supports C++11.
     
    I'm not sure if the issue is with NetBeans C/C++ module or with MPLAB X, but I can say that we've had difficulties with it.  It does not recognize C++11 idioms like ranged-for, constexpr, final, override, and sized/scoped enums, which causes its parse to freak out and show red lines everywhere.  This also means that the code completion more or less stops working.  Even in the absence of those features, I've had MPLAB X get lost and not be able to find class methods unless they're explicitly qualified with the class name.  I haven't tried the absolute latest MPLAB X yet (I'm using 3.51 because I haven't had a chance to try a newer one), so maybe there have been improvements there.
     
    The only real bloat I've seen was when trying to use iostreams instead of C stdio functions (cout vs printf).  The iostream stuff seemed to pull in a lot of crud when I tried it, adding about 150kB to the flash usage.  This was long ago, so maybe things were fixed since then. Then again, I don't particularly like the iostream stuff anyway, so we've stuck with printf and friends.  I'd actually like to try out fmtlib at some point, which looks like a great alternative to both iostreams and stdio.  There very well may be a code size difference in C vs C++ code, but it is not simple to quantify because XC32 uses a completely different set of libraries for C vs C++ (HI-TECH/legacy vs Dinkumware) and so the difference might lie in there.
    #8
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: Harmony in C++?? 2019/03/28 07:05:21 (permalink)
    0
    As I'm getting more into the use of Harmony and specifically the configurator with a C++ project it's going reasonably smoothly.  I'm having to make changes to the Harmony library code so it will compile with C++ but so far they're pretty straightforward.  All of my changes are flagged during the Configurator code generation and with appropriate commenting it's easy to scan through them.
     
    But each time I re-generate I have to remember to remove the main.c and app.c files from the project so they don't conflict with main.cpp and app.cpp.
     
    How about just a Configurator option to suppress generation of those two files?
     
     
    #9
    jdeguire
    Super Member
    • Total Posts : 457
    • Reward points : 0
    • Joined: 2012/01/13 07:48:44
    • Location: United States
    • Status: offline
    Re: Harmony in C++?? 2019/03/28 10:23:22 (permalink)
    5 (1)
    Open up <harmony path>/utilities/mhc/config/harmony.hconfig and add the following just above where it says "config APP_INSTANCES":
     
     
    config INCLUDE_MAIN_C_TEMPLATE
     bool "Include a C main file template?"
     default y
     ---help---
     If checked, a simple main.c file will be generated that you can fill in.
     Otherwise, one will not be generated and you will need to provide your own
     main file. Uncheck this if you plan to use a C++ main file.
     ---endhelp---

     
    NOTE:  If you already created a project, you'll need to copy that same snippet above into the .hconfig file in your project's "firmware/" directory.
     
    Now, open up <harmony path>/config/harmony.hconfig and look for the line that references main.c.ftl near the bottom and modify it to look like this:
     
    ifblock (INCLUDE_MAIN_C_TEMPLATE)
    template MAIN_C "$HARMONY_VERSION_PATH/utilities/mhc/templates/app/main.c.ftl" to "$PROJECT_SOURCE_FILES/app/main.c"
    endif

     
    You can then create similar options for "app.c".  I never bothered because we don't ever use that file anyway.
    post edited by jdeguire - 2019/03/28 10:25:52
    #10
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: Harmony in C++?? 2019/03/28 16:23:02 (permalink)
    0
    Thanks, very helpful!
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5