Author Topic: High Level code on micro-controllers  (Read 4437 times)

0 Members and 1 Guest are viewing this topic.

Offline Crazy_EngineerTopic starter

  • Contributor
  • Posts: 13
  • Country: us
High Level code on micro-controllers
« on: March 27, 2018, 01:23:38 pm »
I learned to code in assembly and eventually moved up to C.  In the last couple of years I have noticed that most IDEs support C++, and RTOS integration.  With ram and flash being rather cheap now days, the overhead from the abstraction is less of a concern and I have been wondering if it is time move up.  What are your thoughts on using "higher level" object oriented code and possibly a real time operating system vs plain old C?   
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: High Level code on micro-controllers
« Reply #1 on: March 27, 2018, 01:55:27 pm »
Getting Lua going on a microcontroller is high on my wish list.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline C

  • Super Contributor
  • ***
  • Posts: 1346
  • Country: us
Re: High Level code on micro-controllers
« Reply #2 on: March 27, 2018, 03:14:52 pm »

Not all higher level abstraction costs in code space.
Often the higher level is just better source checking or a different way to do same thing.

If every thing is an integer then you lose the option of compiler throwing an error when you try to combine a temperature integer & something else. Checks like these costs Source space but not code space.

Often a different way to do same thing can let the compiler do a better job.
When you read a "C" program you see "DEFINE" used all over the place.
Think of foundation of "DEFINE". Read source text and replace matching text with new text. Compiler can not check "DEFINE" only the replaced text for proper syntax.
Remember that the compiler writer has to allow for all possible valid uses of "DEFINE".
For example if you use "DEFINE" to work around CPU differences you have a problem. To verify your code you need to recompile for each CPU possible in the "DEFINE" to check for errors.
If the higher level language allows, you could just have a list of CPU's and the compiler check all for errors.

You do not need a higher level of abstraction if the language allows same result with out abstraction.

Think back when each printer needed a different driver due to code sequence to do BOLD for example.
You just need to swap functions based on what printer.

Back in the 70's, I worked with what you would call and embedded system. Code changes happened often and happened while the program was running. Proper language can make this very easy.

Think about "C", will you get an error if you do a dumb think and connect two RTOS things?
Not hard to have error checking for code separation if the language defines such a thing.
"C" gives you great power, but makes you check for errors that are simple with proper language.

Today a language should know what an event is and help with program checks.

So lots of things can become something the compiler can check if the compiler knows what it is and their for what rules to use in checking.
In the process you have to type/input more text but gain in better compiler checking which saves debug time and finds errors better.

Think of what an abstraction is. A way of hiding needed facts from something.

No need to hide something if you can state the facts.
 

Offline free_electron

  • Super Contributor
  • ***
  • Posts: 8517
  • Country: us
    • SiliconValleyGarage
Re: High Level code on micro-controllers
« Reply #3 on: March 27, 2018, 03:19:56 pm »
Getting Lua going on a microcontroller is high on my wish list.

has been done. stm32 can do it. so can esp8266 ....

for many processor cores :
http://www.eluaproject.net/overview/status

for ESP8266:
https://nodemcu-build.com/

ive been running ESP8266 without problems
« Last Edit: March 27, 2018, 03:27:20 pm by free_electron »
Professional Electron Wrangler.
Any comments, or points of view expressed, are my own and not endorsed , induced or compensated by my employer(s).
 

Offline Crazy_EngineerTopic starter

  • Contributor
  • Posts: 13
  • Country: us
Re: High Level code on micro-controllers
« Reply #4 on: March 27, 2018, 05:43:23 pm »
Abstracting through macros is something I do often.  Indeed many of my projects start off with a long list of macros that relieve me from having to know what pin an input or output is assigned to.  Unfortunately  the C pre-processor has its limits which I have run into when trying to make driver libraries that function across a family of chips.  The driver library in question was for an LCD screen for msp430 series processors.   When writing it there were several times that I thought "A class would probably be helpful here."; but I was dissuaded by the resource inefficiency horror stories that came from my peers. 
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: High Level code on micro-controllers
« Reply #5 on: March 27, 2018, 05:49:29 pm »
AFAIK most RTOS is written in C. As of object oriented programming you can do it in plain C too. (If you really think about it the stdio.h filesystem access API is really a FILE class and its methods.)

Personally I don't use a lot of C++ on microcontrollers. AFAIK smaller chips like 8051, STM8 as well as 8-bit and 16-bit PIC don't even support C++ (there was a patchset for xc16 that adds C++ support, but that was flaky AF and abandoned.) OTOH that object-oriented C is very widely used across my own code.

When writing it there were several times that I thought "A class would probably be helpful here."; but I was dissuaded by the resource inefficiency horror stories that came from my peers. 
You can do classes and OOP in C. Ever heard of the concept "callback function pointers?"
 

Offline Crazy_EngineerTopic starter

  • Contributor
  • Posts: 13
  • Country: us
Re: High Level code on micro-controllers
« Reply #6 on: March 27, 2018, 06:11:36 pm »
You can do classes and OOP in C. Ever heard of the concept "callback function pointers?"

Looks like I have some reading to do.  Thanks.
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: High Level code on micro-controllers
« Reply #7 on: March 27, 2018, 06:15:24 pm »
You can do classes and OOP in C. Ever heard of the concept "callback function pointers?"

Looks like I have some reading to do.  Thanks.
From what I see in that book, that fellow took an approach fairly similar to the Objective-C runtime though.
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: High Level code on micro-controllers
« Reply #8 on: March 27, 2018, 06:45:41 pm »
Getting Lua going on a microcontroller is high on my wish list.
has been done. stm32 can do it. so can esp8266 ....

for many processor cores :
http://www.eluaproject.net/overview/status
Unfortunately the Eluaproject needs a lot of work to get some decent platform support for  NXP's LPC devices.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline abraxa

  • Frequent Contributor
  • **
  • Posts: 377
  • Country: de
  • Sigrok associate
Re: High Level code on micro-controllers
« Reply #9 on: March 27, 2018, 07:44:03 pm »
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: High Level code on micro-controllers
« Reply #10 on: March 28, 2018, 06:34:17 am »
@C
100++/100 agree with you :-+
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: High Level code on micro-controllers
« Reply #11 on: March 28, 2018, 06:45:58 am »
#define pin instead of typedef enum?
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: High Level code on micro-controllers
« Reply #12 on: March 28, 2018, 06:46:28 am »
Personally I don't use a lot of C++ on microcontrollers. AFAIK smaller chips like 8051, STM8 as well as 8-bit and 16-bit PIC don't even support C++ (there was a patchset for xc16 that adds C++ support, but that was flaky AF and abandoned.)
IMO, the lack of a quality C++ compiler is a good reason to avoid a particular architecture. (if you have the choice, of course).

Quote
OTOH that object-oriented C is very widely used across my own code.
Uh huh.  ::)
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: High Level code on micro-controllers
« Reply #13 on: March 28, 2018, 06:50:06 am »
Unfortunately the Eluaproject needs a lot of work to get some decent platform support for  NXP's LPC devices.

yup, and a bootstrapper  :popcorn:
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: High Level code on micro-controllers
« Reply #14 on: March 28, 2018, 07:39:41 am »
I think there are certain types of MCU devices that can be considered suitable for moving the level op abstraction up a bit. But it all starts with a good compiler. I would not go with interpreted languages such a Lua (which sucks imo). Dicking around with C can do OO too is all very well when you're on a 8051 (or a small AVR) and you have no other choice, but not for larger teams with mixed skill sets.

Ask yourself what parts of the code need to be close to the metal because of performance or efficiency of interfacing. The process flow, driving UI and higher order logic should be readable (and maintainable) code foremost.

Personally I think (C++ 20) meta programming is a very interesting topic for the embedded domain, but here again it's about the compiler. Also a higher level of abstraction may not need a new language but merely a different mindset and design. I (usually) cringe when I see most C library API designs where to my mind all the wrong choices of separation of concerns and single responsibilities have been made. Designing good library APIs is hard.

So yes, it is definitely time to move to a higher level, but not necessary time for a new language and certainly not an interpreted language (unless you aim to run user/custom scripts on the device itself).
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: High Level code on micro-controllers
« Reply #15 on: March 28, 2018, 10:33:44 am »
(unless you aim to run user/custom scripts on the device itself).
Even if I plan to run user scripts, those are always native code loaded into the RAM. Whatever language you are using, compile it down to native code and link against the symbol list of the main application please.
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2604
  • Country: us
Re: High Level code on micro-controllers
« Reply #16 on: March 28, 2018, 05:49:35 pm »
Unfortunately  the C pre-processor has its limits which I have run into when trying to make driver libraries that function across a family of chips.  The driver library in question was for an LCD screen for msp430 series processors.   When writing it there were several times that I thought "A class would probably be helpful here."; but I was dissuaded by the resource inefficiency horror stories that came from my peers.

For cross-platform mid- or high-level libraries, using layers of functions is a better approach than macros.  You write a layer of short, simple low-level functions that interface with hardware (setting modes, starting hardware transfers, handling events, etc), then a layer of functions that sit above that to handle protocol level stuff (sending/receiving via buffers, etc).  Depending on the application, where you delineate the two layers may shift, but when moving to a new platform you only need to rewrite the low level functions.  For simple things like basic UARTs you COULD do the lower level with macros, but functions are more versatile, and all things being equal an inlined function won't produce any extra overhead.

If you wanted to be more objecty about it, you could also wrap up a whole interface as a struct containing a reference to the hardware instance and function pointers to your low-level code.  Your higher-level functions would take such a struct as an argument, and then as needed extract the hardware instance and pointer to the appropriate function and pass the former to the latter (along with whatever other parameters are needed).
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3893
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: High Level code on micro-controllers
« Reply #17 on: March 28, 2018, 06:19:32 pm »
Don't you rather need a quad core ARM written in Java to blink the LED?  >:D

I do not have nothing against high level code, any kind of a complex application will certainly need one. However LUA, Java, Python, etc is not a high level code, that is highly resource hungry garbage, that should be kept out of them, even out of any PC, especially Java. I hate that junk!
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14476
  • Country: fr
Re: High Level code on micro-controllers
« Reply #18 on: March 28, 2018, 06:24:53 pm »
Don't you rather need a quad core ARM written in Java to blink the LED?  >:D

 :-DD

I do not have nothing against high level code, any kind of a complex application will certainly need one. However LUA, Java, Python, etc is not a high level code, that is highly resource hungry garbage, that should be kept out of them, even out of any PC, especially Java. I hate that junk!

Ditto. :-DD

A lot of people, especially in the purely software world, confuse high-level languages with the existence of a monster (and unavoidable) runtime accompanied with a lazy memory model.
And if you look closely, what a lot of those people are after are actually those gigantic runtimes and libraries, not necessarily the language constructs themselves.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: High Level code on micro-controllers
« Reply #19 on: March 28, 2018, 07:34:14 pm »
Don't you rather need a quad core ARM written in Java to blink the LED?  >:D

Yes! Because the Quad ARM costs less than the other less capable parts you might consider and I am done in 5 seconds because of the higher abstraction level. My solution will costs a tenth of your custom optimized one.

I am exaggerating to make a point here, but the question is: does it matter it is not super lean when the price is significantly lower. Software development hours are not cheap. You could buy a lot of hardware for that. Of course there are specs that may make you to choose otherwise (power, performance etc) but still, the hardware is capable.
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19509
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: High Level code on micro-controllers
« Reply #20 on: March 28, 2018, 07:35:47 pm »
A lot of people, especially in the purely software world, confuse high-level languages with the existence of a monster (and unavoidable) runtime accompanied with a lazy memory model.
And if you look closely, what a lot of those people are after are actually those gigantic runtimes and libraries, not necessarily the language constructs themselves.

Yes indeed; just so.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: High Level code on micro-controllers
« Reply #21 on: March 28, 2018, 08:51:31 pm »
Just an information drop: there is this thing called gnustep-libobjc2-baremetal. Yes this is about that odd language Objective-C,

Objective-C (yes the old Apple language before Swift,) especially the more modern Swift-compatible-Objective-C, has every feature Java has, but it does not rely on a huge runtime library for everything since it got rid of that virtual machine (uses native code instead) and the garbage collector (uses reference counting instead, with static code analysis used to insert 99.9% of the actual reference counting code at compile time through ARC.) The compiler also enforces a fairly rigid naming convention and class contract, throw tantrums if you deviate even a little from it. The mandatory runtime library itself, libobjc2, is no larger than libstdc++, and for the much larger but non mandatory "class library" Foundation.framework you can cherry pick classes and algorithms you actually need (or allow the linker to throw away unused classes and methods) to reduce code size significantly.

On a language feature level Swift is equivalent to Objective-C but with a more streamlined and concise syntax (since Objective-C have to maintain compatibility to C, being a strict superset of it.) However there is no ready to use libswift mandatory runtime library (Swift's version of libobjc2) that suits embedded development.
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: High Level code on micro-controllers
« Reply #22 on: March 28, 2018, 08:59:53 pm »
Don't you rather need a quad core ARM written in Java to blink the LED?  >:D

Yes! Because the Quad ARM costs less than the other less capable parts you might consider and I am done in 5 seconds because of the higher abstraction level. My solution will costs a tenth of your custom optimized one.

I am exaggerating to make a point here, but the question is: does it matter it is not super lean when the price is significantly lower. Software development hours are not cheap. You could buy a lot of hardware for that. Of course there are specs that may make you to choose otherwise (power, performance etc) but still, the hardware is capable.
Now your final product costs 40x more than your competitor's to make. Maybe they have to code on a lower level of abstraction for a week, but due to the vastly simpler hardware and the sheer volume they can churn out so much more products per unit cost the R&D cost per product is next to nothing. This, and overworkng the engineers to death with their creativity suppressed and punished, is how Chinese manufacturers can pump out so many cheap items.
 

Offline Crazy_EngineerTopic starter

  • Contributor
  • Posts: 13
  • Country: us
Re: High Level code on micro-controllers
« Reply #23 on: March 29, 2018, 12:20:23 pm »
Ask yourself what parts of the code need to be close to the metal because of performance or efficiency of interfacing. The process flow, driving UI and higher order logic should be readable (and maintainable) code foremost.
.....
Personally I think (C++ 20) meta programming is a very interesting topic for the embedded domain, but here again it's about the compiler. Also a higher level of abstraction may not need a new language but merely a different mindset and design.



Interesting points.  For me, I notice that my code becomes difficult to maintain once the program gets beyond a certain length; one of the main reasons I was inquiring about other languages.   It may be I need to more carefully plan, compartmentalize, and document my longer projects.   Speaking of which, does anyone know of any good resources on planning out embedded coding projects?
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19509
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: High Level code on micro-controllers
« Reply #24 on: March 29, 2018, 01:24:22 pm »
Ask yourself what parts of the code need to be close to the metal because of performance or efficiency of interfacing. The process flow, driving UI and higher order logic should be readable (and maintainable) code foremost.
.....
Personally I think (C++ 20) meta programming is a very interesting topic for the embedded domain, but here again it's about the compiler. Also a higher level of abstraction may not need a new language but merely a different mindset and design.

Interesting points.  For me, I notice that my code becomes difficult to maintain once the program gets beyond a certain length; one of the main reasons I was inquiring about other languages.   It may be I need to more carefully plan, compartmentalize, and document my longer projects.   Speaking of which, does anyone know of any good resources on planning out embedded coding projects?

The best resources and tools are brainpower, good taste, appropriate design patterns, time, and refactoring. Purchased tools can only support those, they can't ensure success.

If you don't already know about it, strongly consider event-driven programming in conjunction with explicit FSM design patterns (i.e. not horrific ad-hoc deeply nested case statements!). Then you can cleanly separate generation of events from what reaction occurs as a result of an event. Test each event generation and reaction in isolation, then when working combine them. Keep a log of events and states, so that when (not if) things don't work as you expected, you can figure out how you got to that point!

Harel statecharts are a good starting point, a.k.a. UML state diagrams.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf