Jump to content
43oh

Coding for MSP430 in C++


Recommended Posts

As you now know, my experience is exactly the opposite, and the opinion is generally shared among performance-minded devs. You can certainly use C++ with good results, but for large projects where fast, compact, and easy to navigate code is a requirement, it's a bad choice.

 

You might want to check out oPossum's first link.

 

You're right with your concerns regarding performance and memory when using regular C++ classes. My experience as well, at least if your project does not require OOP features (inheritance, dynamic dispatching etc). Though your point about "easy to navigate" is arguable and probably more depends on the author than the language.

 

However oPossum does use C++ templates. As shown in his first post C++ templates have some advantages when implementing reusable and configurable libraries. Check it out - initially I was skeptical too.

http://forum.43oh.com/topic/2358-software-iic-master-in-c-c-class-and-c-template/

 

Regards

Adrian

Link to post
Share on other sites
  • 2 weeks later...

I'm reminded of Torvalds qoute on C++: "C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it"

 

Yes, Linus and I are in agreement here, although I concede that I've met and worked-with good devs who like C++. The line I prefer from Linus's rant is the one that follows, which I paraphrased as: "a good reason to use C is to prevent C++ devs from messing up your work." Any good C++ dev isn't afraid of C, but the dunderheads will avoid it -- this is really most important when considering Java programmers who might attempt to mess-up a C++ project, but who will avoid C like the plague.

Link to post
Share on other sites
  • 1 month later...

Disclaimer: this is my general opinion based on a lot of experience developing hyper-optimized embedded stuff for communications, RTOS, signal processing, graphics, and crypto, for which I think C++ is horribly suited. There are always exceptions, so it's not a riff at anyone personally who likes C++. Of course, C++ can do everything that C can do, so what we are really discussing are "best practices" of typical C++ coders.

...

 

 

Thing is everything you described is coding practice / habits, or are having to do with style. Not the language its self. I've seen C code that is equally disgraceful, and I have written a lot of that ,myself.  Some of it I have seen on these very forums. We're all code snobs in one way or another. Because we think for ourselves. and are used to or like things the way we do them.

 

A language is a tool, one that may / may not be suited for different jobs. A tool can also be used  incorrectly. Such as a hammer. You may be able to drive nails with the handle, but the job may take longer, and eventually you end up breaking the tool.

 

So after having seen some of oPossum's code on the forum here, I would have little doubt that C++ would be just as or more efficient than C written by him. oPossum also seems to be a very experienced with micro processors. Programmers who write code as you mentioned above obviously do not know their hardware, and possibly the C++ language as used in such a context either. But these "kids" did not grow up programming software for very limited hardware such as some of us may have. For them the answer is not optimizing their code, but to throw bigger / badder hardware at the problem. 

 

So with all that said I would have to agree with everything you said with one exception. It's not the languages fault, but the developer(s). One huge example I would relate would be game developers now compared to in the early 90's and before. in the 90's no one would dream of using intensive multiply / divide, especially using floating point numbers. Now days, the hardware can handle it, so this is whats done. In the end however, are we better for it, or not ? I could argue either way.

 

EDIT: for clarity (typos, etc )

Link to post
Share on other sites

I never tried this and I could easilly check this for myself.

But out of curiosity, I wonder if it is possible to code for the MSP430 in C++. In particular I was thinking of mspgcc which is freely available.

 

Has anyone tried this already?

 

Thanks.

--

to

As you can see a question like this will spark debate between developers. Mostly relating to the given developers preferences, and experience with the different languages. My advice to you would be to use what makes you happy, and forget about what anyone else says. Start somewhere, then expand your horizons. 

 

As any good developer you should be constantly learning, and willing to change where those changes make sense.

Link to post
Share on other sites

....

 

As any good developer you should be constantly learning, and willing to change where those changes make sense.

I'm know I'm a noob (ignore my post count ;-)), but I'd like to add, that being willing to change may also mean using multiple disciplines simultaneously, as in you have a project and C may be better for one part of the code and C++ might be better for another.
Link to post
Share on other sites

Here is something that I found rather funny / ironic.

 

 

 

 

? obviously we used more memory and more time than really necessary
? how do we solve this ?
? code algorithms in unreadable low-level C
? OR: keep readability, use template meta-programming

From this http://itee.uq.edu.au/~conrad/misc/sanderson_templates_lecture_uqcomp7305.pdf

 

The ironic bit of course being a C++ programmer, talking about template meta-programming, where C is hard to read . . . lol Uh . . .yeah ! Anyway, just thought I'd share this.

 

 

Link to post
Share on other sites

Here is something that I found rather funny / ironic.

 

 

 

From this http://itee.uq.edu.au/~conrad/misc/sanderson_templates_lecture_uqcomp7305.pdf

 

The ironic bit of course being a C++ programmer, talking about template meta-programming, where C is hard to read . . . lol Uh . . .yeah ! Anyway, just thought I'd share this.

The last two slides basically come to the conclusion that unreadable template code is no better than unreadable C.

 

That said, is it worse than C Preprocessor Kung Fu?  Probably not.  Back in the day, I used to use the M4 preprocessor, but it's not very common in embedded IDEs.  That helped.

Link to post
Share on other sites

Well I just found that one block of comments rather ironic on multiple levels., First, because template meta programming is supposed to be very hard to grasp, and requires lots of forethought( by comparison )  while designing code as such.  Also, on a personal level, I find meta-templates very hard to read through. Even when you know the why's and for's. Secondly, because I feel that is is *very* important for anyone to be good with C++, they should also be very good with C. In my own mind, this is a given.

 

Lots of people now days treat the two languages as though they are strictly two ( pure ) separate languages in of themselves. And I have to admit for a long time I was pretty much a purist myself.But this is not like say Java, and C# used together to achieve an end goal. This is one language, that has been built on top of the other. If only in syntax( but we both know it is not this simple ). I often ask myself that *if* it is possible to write C++ code without using C programming concepts, how useful would said application really be? Assuming the application would be useful, at which point does it stop making sense ? 

 

After all, almost everything that C++ *is*, it owes to C Of course, with some very important differences. Many of which can be emulated, or mimicked in C.

 

On some level, I find C much easier to read through than C++ specific code ( <--- ah! did I hit on something here ? ). On another level, I find C++ easier to understand in broad terms where the whole application is concerned. So perhaps meta-templates in prototype are very hard to read through, the usage of the same templates makes / can make the application overall more readable. Of course, this is just an example, and I do not think meta-templates should be used except where they make sense.

 

With power comes responsibility, which is what I think you were saying several posts back. If not, then I am sure you know this.

 

Now if you want to blame a single language for the current state.of reckless programming. Blame Java ;) I know I do :P

Link to post
Share on other sites

Now if you want to blame a single language for the current state.of reckless programming. Blame Java ;-) I know I do :-P

We are singing the same song.  I don't really like C++ either.  Some parts are nice, but there is too much junk, too many ways to mess things up.  There is a guy who has a quote, "Within C++ there is an elegant language trying to break free."  the guy is none other than Bjarne Stroustroup.   :grin:

Link to post
Share on other sites

We are singing the same song.  I don't really like C++ either.  Some parts are nice, but there is too much junk, too many ways to mess things up.  There is a guy who has a quote, "Within C++ there is an elegant language trying to break free."  the guy is none other than Bjarne Stroustroup.   :grin:

That is so true. I guess it can be said about most languages, but I just think there are more ways to abuse C++ than most other languages

Link to post
Share on other sites
  • 5 weeks later...

So after experimenting with C++ template class stuff, I have to say I am liking it a lot now. It does really help when you have awesome examples to experiment with from other people.

 

One of the really cool things I am noticing if that when done correctly, you pull in very little overhead (if any ). Also, you *can* write a huge template class, include it as a header file, and when member methods are used, only the methods used are what gets pulled into the project code. Things get a bit more interesting when you create a typedef'd object with generic / specialized templates. Here you trade off a little bit of overhead, for a given level of functionality. So if for example you would prefer something like:

 

Debug << "Hello world\r\n";

vs

Object.Write("Hello world\r\n");

 

You can do so, trading this functionality for a small amount of overhead. Mostly as far as I can tell is related to reference object instancing. But, you just need to pay attention to what is happening is all. Which is why the above is as is instead of using something like "endl". In that specific case adding endl functionality increases code size on target by ~20 bytes (when in reality "\r\n" is only two bytes ).

 

Anyhow, yes, there are some pro's and con's with various different things. But so far I am very happily using C++ templates classes to good effect. With the above code, adding that functionality for text only made the code size on target ~216 bytes. This included UART initialization, generic BAUD, and CPU freq passing when instancing the object. Considering that a bare main.cpp file with #include <msp430.h> with a bare return 0; main is 107 bytes on target . . . well personally I think this is very tiny. *That* is as good as C, or better, with a lot of functionality added in. Plus stronger type checking as a side benefit of using C++ and generic templates.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...