Arduino Language vs. C/C++

I have a few questions ... maybe some answers too. Still I'd like some clarification from someone more expert than me (there are lots of!).

Using the Arduino IDE I'm writing code using the Arduino Language (which is based on C/C++ but it's not). In order to write/compile/upload pure C/C++ code for my Arduino (or any other AVR processor) I should use the AVR GCC toolchain instead, right?

And then again, I have the feeling that the Arduino IDE generates bigger machine code than AVR GCC is able to. Is that right? Why is that? I guess it's not simp,y because of poor optimization. There must be more.

Anyway, all those questions because I'm considering moving away from the Arduino IDE if that can help in improving my programming skills with the AVR processors. I fear that won't be easy. So I'm considering what's involved.

which is based on C/C++ but it's not

But it is.

I should use the AVR GCC toolchain instead, right?

You already are. Hold the Shift key and click Verify. The status window will fill with calls to the GCC compiler.

And then again, I have the feeling that the Arduino IDE generates bigger machine code than AVR GCC is able to

Nope. GCC compiler whether you use Arduino or any other front-end.

I guess it's not simply because of poor optimization

Nope. Arduino uses aggressive optimization settings and the GCC compiler does a fine job of optimizing.

There must be more.

The Arduino core adds from 300 to 1300 bytes of code.

Anyway, all those questions because I'm considering moving away from the Arduino IDE if that can help in improving my programming skills with the AVR processors

That would be a foolish reason to leave the Arduino IDE.

Your concept of the differences between arduino programs Vs 'pure C/C++ ' is more likely not understanding which common functions are arduino library functions Vs functions you might wish to write yourself. You are free to use or not use any of the arduino functions (like digitalWrite(), digitalRead(), etc), and instead write your own that can be optimized (for speed or size) for your specific needs.

Arduino programming is 'pure' C/C++ with added arduino function libraries and a little preprocessing performed before passing on the source to the AVR Gcc compiler.

Should I consider the Arduino Language a Super Set of C/C++?

And then again, I have the feeling that the Arduino IDE generates bigger machine code than AVR GCC is able to

Nope. GCC compiler whether you use Arduino or any other front-end.
...
Nope. Arduino uses aggressive optimization settings and the GCC compiler does a fine job of optimizing.

There must be more.

The Arduino core adds from 300 to 1300 bytes of code.

That's something I still don't know how it works (maybe because I have no clue what an Arduino core is whatsoever :frowning:

Should I consider the Arduino Language a Super Set of C/C++?

No, consider it just AVR Gcc C/C++, with additional arduino supplied library functions that you are free to use or not.

Lefty

Should I consider the Arduino Language a Super Set of C/C++?

No, it's just

  • An "abstraction" layer that simplifies various hardware concepts that C/C++ does not handle natively.
  • A set of libraries to implement that abstraction (and replace various posix libraries that aren't applicable on a microcontroller.)
  • A censored set of documentation that avoids mentioning any number of C/C++ concepts believed to be too confusing for beginners.

The Arduino core adds from 300 to 1300 bytes of code.

That's something I still don't know how it works (maybe because I have no clue what an Arduino core is whatsoever :frowning:

Navigate to...
{ArduinoRootDirectory}\hardware\arduino\cores\arduino

That's a "core". For the most part, it is a library that provides the Arduino functions (pinMode, digitalRead, digitalWrite).

See also "Analysis of an Empty Arduino Sketch"

retrolefty:

Should I consider the Arduino Language a Super Set of C/C++?

No, consider it just AVR Gcc C/C++, with additional arduino supplied library functions that you are free to use or not.

Lefty

That's quite clear now.

That also means I can start studying avr-gcc/avr-libc documentation and implement what's in there without worrying about the Arduino Libraries too much. Maybe I will not get results so quickly as when using the Arduino Library (that simplyfies a lot to the user's eyes) but I could learn something new at a lower programming level. Right?

I don't know if that's efficient or not, I don't even care right now. I'd really like to know more so that I can better decide which approach to take with future programming, especially when dealing with microprocessors not directly supported by the Arduino IDE (which is obviously mainly ment to support the processors used on the Arduino boards only).

I'm wondering how every other Arduino library will (if it's even possible) work with other processors (like the Attiny series). Will I be able to use soething like the Standard Wire library or the Contributed LedControl library with e.g. an attiny2313? I mean, apart from the obvious library dimension which most certainly will not fit into the small memory this processor has.

westfw:
See also "Analysis of an Empty Arduino Sketch"

Interesting :slight_smile:

That's quite clear now.

That also means I can start studying avr-gcc/avr-libc documentation and implement what's in there without worrying about the Arduino Libraries too much. Maybe I will not get results so quickly as when using the Arduino Library (that simplyfies a lot to the user's eyes) but I could learn something new at a lower programming level. Right?

Now you got it. The Arduino platform was designed for the non-technical types, artists and such, so the arduino team took lots of existing 3rd party open sourced projects from other groups (processing for the IDE, Gcc for the compiler, AVRDUDE for the uploader, etc), combined them and designed lots of 'core libraries' that abstracted lots of low level details so people new to programming could get stuff working quicker without such a learning curve burden starting off. However the genius of the arduino platform, is that one is free to use as little or as much of these core libraries and just write their own low level C/C++ functions as they see fit.

I don't know if that's efficient or not, I don't even care right now. I'd really like to know more so that I can better decide which approach to take with future programming, especially when dealing with microprocessors not directly supported by the Arduino IDE (which is obviously mainly ment to support the processors used on the Arduino boards only).

That's a good plan. You are in charge of what and when you want to learn. The Arduino will place no restrictions on you that can't be bypassed or ignored.

I'm wondering how every other Arduino library will (if it's even possible) work with other processors (like the Attiny series). Will I be able to use soething like the Standard Wire library or the Contributed LedControl library with e.g. an attiny2313? I mean, apart from the obvious library dimension which most certainly will not fit into the small memory this processor has.

That gets a little more tricky. The 'standard' ardiuno core libraries were written and/or updated to support the specific AVR processors that the Arduino firm uses in their boards they sell, AVR mega8, mega168, mega328, mega1280, and mega2560. When using other AVR chips one has to evaluate if the libraries would have to be modified to work on other processors. In some cases they can be used as is, other times modified, and other times certain libraries will not work at all due to not having required hardware resourse on the chip in question.

I'm wondering how every other Arduino library will (if it's even possible) work with other processors (like the Attiny series).

The few libraries that have crossed my path are written specifically for the ATmega family with no attempt to make them portable to the ATtiny family. There are even some things in the current core that are not portable to the ATtiny.

The ATmega family has fairly uniform hardware (timer 1 on the 328 is the same as timer 3 on the 1280). The ATmega processors seem to be very general purpose. Porting from one ATmega processor to another is fairly straightforward.

The ATtiny family is not at all uniform. Each processor seems to be designed for a specific application (861 to control three-phase motors; 85 for very high speed PWM). For libraries to be portable to and within the ATtiny family, a "hardware abstraction" layer is first needed.

Will I be able to use soething like the Standard Wire library

No. BroHogan has contributed a replacement.

or the Contributed LedControl library with e.g. an attiny2313?

Can't say.

I mean, apart from the obvious library dimension which most certainly will not fit into the small memory this processor has.

Anything that uses a timer is generally not easily ported (e.g. Servo library). The USART is missing from most ATtiny processors. Either SPI or I2C is possible but the hardware is dramatically different.

I just downloaded the TinyWire library and looking into it.

I see it was made to work with the attiny85, I wonder if it works with the attiny2313 too. What do you reckon?

robitabu:
I see it was made to work with the attiny85, I wonder if it works with the attiny2313 too. What do you reckon?

Um ... let's see ... "Universal Serial Interface – USI" ... the 2313 has the right hardware for TinyWire so it should work.

I've made my first attempt with the TinyWire library and the attiny2313 ... it doesn't look good :frowning:

I've setup a simple circuit on a breadboard; the attiny2313 is being programmed with my UNO as ISP; hooked up a at20c02 eeprom which I previously filled with know byte values; connected 8 leds (with resistors) to be used as binary display and a switch to walk the eeprom memory one byte after the other.
The same circuit (eeprom, switch and leds) works perfectly when directly connected to the UNO and using the Wire library. I can display every byte value with the 8 leds.

Once I switch to the attiny2313 and use the TinyWire functions instead, the program compiles and uploads without problem but the result is garbage. The leds don't display the expected byte values. Every time I reboot the system I get the very same sequences back again but it's not related to the known values in any way.

Something is broken. I don't know if I have to use the TinyWire functions differently or it simply doesn't support the attiny2313. Will inspect more and try and find out what's going on. Any suggestions where to look for? Do you already know some caveats of the tinyWire library?

I started reading the TinyWire's source (TinyWireM\USI_TWI_Master.h), it looks like BroHogan started a few #define considering the attiny2313 as well but then overwrote it all in order to make it work with the ATtiny85 only.

#if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
    #define DDR_USI             DDRB
    #define PORT_USI            PORTB
    #define PIN_USI             PINB
    #define PORT_USI_SDA        PORTB5
    #define PORT_USI_SCL        PORTB7
    #define PIN_USI_SDA         PINB5
    #define PIN_USI_SCL         PINB7
#endif

/* From the original .h
// Device dependant defines - These for ATtiny2313. // CHANGED FOR ATtiny85

    #define DDR_USI             DDRB
    #define PORT_USI            PORTB
    #define PIN_USI             PINB
    #define PORT_USI_SDA        PORTB0   // was PORTB5 - N/U
    #define PORT_USI_SCL        PORTB2   // was PORTB7 - N/U
    #define PIN_USI_SDA         PINB0    // was PINB5
    #define PIN_USI_SCL         PINB2    // was PINB7
*/

So, even if at first he if_defined stuff for the attiny2313, he ended up with the attiny85 specific ports/pins only.

I'm a first time C/C++ coder, so I wonder if only deleting those last hardcoded define can make it to work with the attiny2313.
I don't have my hardware at hand right now, I'll do that in the evening at home and let you know.

Ooops!!! I really did miss the commenting in the source ... :blush:

So that's not the problem with my attiny2313. I have to look further.

Just my 2 cents. If you are interested in a professional degree in ECE, then losing the arduino "training wheel" is absolutely the right thing to do. Indeed losing the C/C++ at some point and embracing the assembly code is needed as well. If you do this just as a hobby, then you will need to gauge the time you spend going on your own and knowledge you may gain. The lower-level programming language/abstraction you go, say "arduino"->"pure c/c++"->assembly, the more details you need to attend to as a developer. Sometimes it is necessary and it makes or breaks a certain project but lots of other times you just make it harder on yourself and there will be less that can help you the further down the level you descend ]:smiley:

BTW, I totally agree with the others that arduino language is c/c++ but not super set of it. You have to be able to support additional syntax or something to be a super class of c/c++. Libraries are written in c/c++ code. They don't expand the language but rather the functionality of the arduino. Think of libraries as shelves of books, written in the same language the reader can understand, not really expanding the language but literature instead.

First of all: your post is encouraging and I agree and understand quite a few of your statements.

liudr:
If you are interested in a professional degree in ECE

I don't think so.

If you do this just as a hobby, then you will need to gauge the time you spend going on your own and knowledge you may gain.

That's more likely. You see I'm not a professional and I don't aim at being one in this field. I like spending time with electronics, mechanics, simulators, software and hardware. I have a small project in mind, it consists of a few electronics-mechanics-hardware-software elements which I need to build on my own and I'm building the foundation knowledge in order to get there. It's a hobby, but still, the more I learn the more I'm intrigued in the basics (kind of a predisposition I always had).

Not that I'm willing to embrace assembly programming ( if not strictly needed) but I like to make things clear in mind. And this forum is very helpful indeed, there are a lot of people willing to share knowledge and experiences, and correct stupid statements like the ones I sometimes write in here. That's a big help :slight_smile:

The lower-level programming language/abstraction you go, say "arduino"->"pure c/c++"->assembly, the more details you need to attend to as a developer.

I don't see that as a problem to me, as I said, it's a hobby, I don't have timelines.

BTW, I totally agree with the others that arduino language is c/c++ but not super set of it. You have to be able to support additional syntax or something to be a super class of c/c++. Libraries are written in c/c++ code. They don't expand the language but rather the functionality of the arduino. Think of libraries as shelves of books, written in the same language the reader can understand, not really expanding the language but literature instead.

Nice metaphore :slight_smile: