Arduino or C

Status
Not open for further replies.

Spicedreams

Well-known member
The Arduino paradigm has been great to get going, but I am getting concerned that the Arduino paradigm starts to add some complexity too.

If I wanted to use Teensy hardware but without the Arduino environment, what of Paul's magnificent software would I lose?

Thanks
Graham
 
The Arduino paradigm has been great to get going, but I am getting concerned that the Arduino paradigm starts to add some complexity too.

If I wanted to use Teensy hardware but without the Arduino environment, what of Paul's magnificent software would I lose?

Thanks
Graham

Nothing, you can use the core libraries and all other libraries outside Aduino, I used makefile, Eclipse, and also Visual Micro on Visual Studio.
makefile and other IDE require hand configurations of what is where.
but this is all obvious.
I may have misunderstood your point, but the fact, that you asked this questions, indicates to me that you are not an experienced programmer, so maybe it would be better to stick with Arduino.
 
As a latecomer to Arduino with previous experience with Microchip devices, I view Arduino as a double edge sword. First it is just incredible that you can program a device as complicated as the Teensy processors without even looking at the datasheet. On the other hand, the concept of virtual pin numbers and single bit I/O invites inefficient hardware design and inefficient software is the result. On the other other hand, the incredible power of the Teensy makes up for most of the inefficiencies.

In the Arduino environment you are not stuck with using the single bit I/O. You can bypass the added complexity. All the internal device registers are defined and you can use them at any time. So open the datasheet and take a look. There is a lot to explore without using a new tool chain.

Here is a little example program that toggles the LED in different ways. It uses some of the I/O registers directly. The toggle register test was changed to digitalWriteFast ( which turns out to be just as fast as the Set and Clear register test ) . I post this as an example that you can use the parts of Arduino that make programming easy ( like the pinMode statements ), yet can directly manipulate the internal registers anytime you wish.
Code:
/* LED Blink, Teensyduino Tutorial #1
   http://www.pjrc.com/teensy/tutorial.html
 
   This example code is in the public domain.
*/

// Teensy 2.0 has the LED on pin 11
// Teensy++ 2.0 has the LED on pin 6
// Teensy 3.x / Teensy LC have the LED on pin 13
const int ledPin = 13;
elapsedMicros time1;
float time3, time4, time2;


// the setup() method runs once, when the sketch starts

void setup() {
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  
}

// the loop() method runs over and over again,
// as long as the board has power

void loop() {
long i;

  time1 = 0;
  for( i = 0; i < 10000000L; ++i ){
    digitalWrite(ledPin, HIGH);   // set the LED on
    digitalWrite(ledPin, LOW);   // set the LED off
  }
  time3 = time1;

  time1 = 0;
  for( i = 0; i < 10000000L; ++i ){
  // use the internal register name for bit set, bit clear
     GPIOC_PSOR =  ( 1 << 5 );
     GPIOC_PCOR =  ( 1 << 5 );
  }
  time4 = time1;

  // just for fun toggle the LED
  // changed to test digitalWriteFast
  time1 = 0;
  for( i = 0; i < 10000000L; ++i ){
    //  GPIOC_PTOR =  ( 1 << 5 );
    digitalWriteFast(ledPin, HIGH);   // set the LED on
    digitalWriteFast(ledPin, LOW);   // set the LED off

  }
  time2 = time1;

  Serial.println( "Teensy 3.6 running at 120 mhz");
  Serial.print( "Digital Write Test ");  Serial.print( time3/1000000.0,3 ); Serial.println(" seconds");
  Serial.print( " Set Clear Test    ");  Serial.print( time4/1000000.0,3 ); Serial.println(" seconds");
  Serial.print( "Digital Write Fast ");  Serial.print( time2/1000000.0,3 ); Serial.println(" seconds");
  Serial.println();
  
  delay(1000);                  
  
}
 
digitalWriteFast(LED_BUILTIN, HIGH); shows how you can have easy to use and understand zero-cost abstractions. They are zero-cost in two senses: no inefficient ASM code and no mental burden on the programmer. Any reasonable person can understand what that means, and the compiler will emit the optimal instructions!
 
The trouble with directly accessing registers is your code becomes tightly tied to one particular generation of hardware.

For example, your access to GPIOC_PSOR may be optimal for Teensy 3.x. But when we have Teensy 4.x running at 600 MHz, you'll need to rewrite that code because the registers are different.
 
I think it depends on what you want to accomplish. If you want to become a C(++) programmer, start with that. If you want to start developing applications on the Teensy or Arduino platform, start with Arduino and switch to C(++) when you really run into the limits the Arduino macros can provide. For probably 99% of all Teensy users, that last issue won't ever present itself. And if it does, you will have mastered a lot of C++ syntax already with the Arduino dialect.
 
I think it depends on what you want to accomplish. If you want to become a C(++) programmer, start with that. If you want to start developing applications on the Teensy or Arduino platform, start with Arduino and switch to C(++) when you really run into the limits the Arduino macros can provide.

Thanks to everyone who has contributed to this thread.

The message I'm taking away is to persist with the Arduino paradigm for now, and if it ever truly runs out of steam it won't have painted me into a corner- I will be able to use the Arduino libraries with 'raw' C++ if I need to.

If I've misunderstood, please shout...

Thanks
Graham
 
Thanks to everyone who has contributed to this thread.

The message I'm taking away is to persist with the Arduino paradigm for now, and if it ever truly runs out of steam it won't have painted me into a corner- I will be able to use the Arduino libraries with 'raw' C++ if I need to.

If I've misunderstood, please shout...

Thanks
Graham

Maybe knowing what Arduino does will help
To my knowledge (but I'm not an expert in Arduino)

Arduino
-copies files to temp folder
-preprocesses the .ino file to generate a 'good' .cpp file.
for this it adds basic include files, forward declarations of functions and maybe something else.
(I never have done it but the generated file can be checked as it is stored in temp folder.)
-scans files for included libraries

If all is prepared
Arduino builds hex file with GCC.

There are two potential annoying issues:
one minor is: If one edits library files, Arduino seems to compile always the whole library and not the file one has changed.

another issue is that the conversion from ino to cpp can sometimes have undesired side effects
This can easily be avoided by having a complete empty .ino file and an additional .c or .cpp which setup() and loop()
eg. adding to sketch folder
myApp.c
Code:
#include "Arduino.h"
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

or myApp.cpp
Code:
#include "Arduino.h"
extern "C" {
    void setup(void);
    void loop(void);
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The only difference with respect to Arduino's .ino approach is that the .c/ .cpp files must be properly programmed in c/c++ (include files, forward declarations of functions, etc.)

I prefer the .cpp version as it lets me use the standard Serial class fo printouts. Also with this trick I can switch from Arduino IDE to eclipse or viusual micro forward and backward.
I use Arduino to make sure that my program can be compiled by other Teensy users, but I usually program with eclipse IDE.
 
Thanks for this clarity, I'm sure it will help me.

Maybe knowing what Arduino does will help
To my knowledge (but I'm not an expert in Arduino)

Arduino
-copies files to temp folder
-preprocesses the .ino file to generate a 'good' .cpp file.
for this it adds basic include files, forward declarations of functions and maybe something else.
(I never have done it but the generated file can be checked as it is stored in temp folder.)
-scans files for included libraries

If all is prepared
Arduino builds hex file with GCC.

There are two potential annoying issues:
one minor is: If one edits library files, Arduino seems to compile always the whole library and not the file one has changed.

another issue is that the conversion from ino to cpp can sometimes have undesired side effects
This can easily be avoided by having a complete empty .ino file and an additional .c or .cpp which setup() and loop()
eg. adding to sketch folder
myApp.c
Code:
#include "Arduino.h"
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

or myApp.cpp
Code:
#include "Arduino.h"
extern "C" {
    void setup(void);
    void loop(void);
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The only difference with respect to Arduino's .ino approach is that the .c/ .cpp files must be properly programmed in c/c++ (include files, forward declarations of functions, etc.)

I prefer the .cpp version as it lets me use the standard Serial class fo printouts. Also with this trick I can switch from Arduino IDE to eclipse or viusual micro forward and backward.
I use Arduino to make sure that my program can be compiled by other Teensy users, but I usually program with eclipse IDE.
 
Status
Not open for further replies.
Back
Top