Random thoughts on Channels & Ports

New to XMOS and XCore? Get started here.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

Interactive_Matter,
..but C++ would probably eat up too much precious RAM.
Slightly off topic but I have to jump in here. The above statement is just not true.

I while ago I might have also assumed that C++ was a memory hog and slow and ...whatever. And that C is preferable for small embedded systems. Then I found out that the Arduino users are using C++ on their little 8 bit AVRs. What?! I had to investigate this and do some experiments. Turns out to be a myth that C++ is big and slow.

Yes, if you use that standard C++ libs, and yes if you are using C++ string class and streams etc. BUT if you keep an eye on what you are doing C++ can be exactly as small and efficient as doing the same thing in C but with the benefits offered by C++.

For example:

Lets assume you have a set of C functions that can operate on some data structure. Perhaps you might have a pointer parameter to the data structure in the parameters of all those functions. This give the possibility to have multiple instances of the data structure, the pointer parameter directs the functions as to which instance to operate on. As a programmer you have to take care of all those pointers and clutter up all the calls with the pointer parameter.

Well, in C++ the data structure becomes a class and the functions become methods of the class and all those pointer parameters disappear. Everything becomes much neater.

Turns out that the code generated by a C++ compiler for those classes and the callers is pretty much exactly the same as what you would get by compiling what I described for C functions with structures and pointer parameters.

In fact in some of my experimental code the generated code was smaller!!

Here is a nice article discussing C vs C++ for embedded systems and busting many myths about it.

http://www.embedded-systems.com/98/9802fe3.htm


User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am
Contact:

Post by Interactive_Matter »

Heater wrote:Interactive_Matter,
..but C++ would probably eat up too much precious RAM.


Yes, if you use that standard C++ libs, and yes if you are using C++ string class and streams etc. BUT if you keep an eye on what you are doing C++ can be exactly as small and efficient as doing the same thing in C but with the benefits offered by C++.



http://www.embedded-systems.com/98/9802fe3.htm
OK, I have not investigated it due to lack of time. But you are completely right. Since with an processor you instantiate a certain object on certain ports. Eg. a DAC on the SPI port or in XMOS channels are so good examples for real objects.
Will try it.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

Trouble with C++ isn't memory or complexity rather it is that you loose the valuable event model that XC builds in.

regards
Al
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am
Contact:

Post by Interactive_Matter »

Folknology wrote:Trouble with C++ isn't memory or complexity rather it is that you loose the valuable event model that XC builds in.

regards
Al
Event model?
Either I do not know what you mean or did miss that.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

Event model:

In XC you can find yourself waiting on a channel or a timer or some other event.

What if you want to wait on multiple events at the same time? Say you want to get data from a channel but also have a time out.

Then you are going to wrap things up in select statement.

How does one integrate that "event model" with C++ classes?
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

XC has select and select functions, native ports, channels and timers which can be used therein.

regards
Al
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am
Contact:

Post by Interactive_Matter »

I thought those things would be impleented in XC anyway and just called from C++.
The objects 'hide' the complex XC stuff - or can even provide a way to use more generic less understandable XC stuff that can be shared between different C++ Objects or even implementations.

In my mind C++ just provides nice high level interfaces.
Treczoks
Active Member
Posts: 38
Joined: Thu Mar 21, 2013 11:18 am
Contact:

Post by Treczoks »

Corin wrote:This is already possible with XC. e.g. a structure to hold some ports is declared as:
I vote for inclusion of this piece of information into the XC Programming Manual!

There are so many interesting ideas and tricks in this forum (and probably in the wiki) that would belong into "The Manual". For me as a newbie to XC it is quite hard to find some important answers, and even harder if you don't know what you should be looking for. I found this gem by accident, and I'm very happy that I found it, as it makes structuring the code much easier, improves readibility and maintainability, reduces the risk of errors. Perfect stuff to put it into a core manual.

Yours, Christian
Post Reply