Teensy & Arduino

Status
Not open for further replies.

Fatima_H

Active member
Hello every one

I would like to ask you a question:

when I read about Teensy I knew that almost all codes for arduino can be used with teensy.

How is this right, since the chip used to build arduino(Atmega) is different from that used to build teensy(MK20DX128).

please can any one explain me the point?

I need your help, I need to understand.

thanx a lot.
 
Neither Teensy ARM nor AVRs can understand the code written in Arduino IDE, which is C/C++
https://en.wikipedia.org/wiki/The_C_Programming_Language
What can understand Arduino C is the GCC compiler which is running behind the Arduino IDE and features back ends for a wide variety of chips allowing us to say 'add A and B and store in C' and let the compiler work out what the chip specific instructions to actually achieve that.

edit: summary of the build chain is here
https://www.arduino.cc/en/Hacking/BuildProcess
 
This is a pretty big question.

You can also ask this about how Arduino DUE and Arduino M0... Work as well as they are not Atmega8 processors as well.

Several parts to this:

a) First off, with the Arduino if you build for one of the Atmega boards, it only will work on one of the Atmega boards. i.e. the generated hex file will only run on the board that it was built for.

b) When you install different architectures, they download a complete build platform for type of chip, including compiler, include files... There are files included as part of this download (boards.txt, platform.txt...) which tells the Arduino ide which commands to use to do things like build, or upload...

c) A lot of source code does not depend on the underlying hardware specifics and can be simply be recompiled for the different processors. Are there as some gotchas to watch out for. Things like if your program uses data types like: int, or float or... The size of these variables may be different on different boards. Example int on Atmega8 boards are 16 bit, whereas on the ARM boards they are 32 bits... So if you need/want specific size you should use data types like int16_t or uint32_t or ... which are the same size on the different boards.

d) In lots of libraries and programs, if you need/want to do hardware specific code, you can put them under #if compiling so you can do specific things for specific boards. You will see some of that in many different libraries.

e) Paul (and others here ) have worked hard to make AVR specific code work on the Teensy boards. This includes things like emulating several of the AVR specific registers. For example there is code that if you had SPI code that was developed to use the AVR specific registers, the emulation code will translate those register accesses to use the standard SPI code...

f) Likewise: a lot of work has gone into trying to get all of the major (and lot of minor) libraries to work on Teensy. That is why for example if you download Teensyduino and installed it, you will see a lot of optional libraries to be installed.

Hope that helps
 
&
GremlinWrangler

THANX for your replying

KurtE, If I understand your point, we will never be able to use commands like analogRead() and analogWrite() and a lot of similar functions especially serial communication_related functions.

because analogRead (for example) is a macro that use the registers of the chip that used to built arduino (Atmega), so we have to write the code with commands that call the registers of MK20dx

right?
 
KurtE, If I understand your point, we will never be able to use commands like analogRead() and analogWrite() and a lot of similar functions especially serial communication_related functions.

because analogRead (for example) is a macro that use the registers of the chip that used to built arduino (Atmega), so we have to write the code with commands that call the registers of MK20dx

right?
No that is not correct. As all of these core functions like digitalRead, digitalWrite, analogRead, analogWrite, Serial.print, millis, micros, ... Are part of the core code and as part of the download the appropriate sources for these are included as part of the install. The internals of which may be very different. Also some of the functionality of some of these may be much enhanced.

Example: analogRead - by default has the same resolution (10 bits) as the AVR. But there is a new api part of the Teensy that allows you to set a higher resolution, like 12, or ... bits.

So hopefully without exception, (or very few exceptions), everything on the Arduino reference pages should work with Teensy.
 
Should mention, if you are interested, I would suggest that you download a recent version of Arduino, like 1.8.2 (or 3). Note: when Arduino releases a new release there is some delay before a version of Teensyduino is released that supports it, as it updates some of the capabilities of the IDE (like how the terminal monitor works).

If you install 1.8.2 you can install the current released version of Teensyduino from the main PJRC download page. With the 1.8.3, there is a beta version that you can download from the forum.

I would suggest that you install one of these versions and try building different programs (either your own) or some of the example programs...

And if you are really interested, you can then look at the sources for Teensy stuff...

For example on my machine I have Arduino installed at: d:\arduino-1.8.3

The sources and some board specific library sources for the main Arduino AVR boards is at: D:\arduino-1.8.3\hardware\arduino\avr

The Teensy versions are installed at: D:\arduino-1.8.3\hardware\teensy\avr

Under this: there are several subdirectories.
libraries - All of the libraries that are installed by Teensyduino that only get used when one of the Teensy boards is the current board type.
cores - Is where all of the main code is, with subdirectories. The main ones are
cores\teensy - This is where the code for Teensy 2.x boards, these are AVR Atmega boards like the Arduino UNO, actually closer to Leonardo
cores\teensy3 - this is where all the main code base is for Teensy 3.x and LC code is that gets built as part of your program.

Also if you are interested also installed is the GCC stuff.
A lot of this is installed at: D:\arduino-1.8.3\hardware\tools
Under this is a couple directories: avr and arm for the different setups...

Note: these directories are specific to where I installed Arduino on my machine which is windows 10. Location on your machine may and most likely be different.
 
KurtE
Thank soooooo much , its a small word to say for you, your answer is very helpful for me.
Thanx again .
 
If you do use Teensy, and you find something that works on AVR but not on 32 bit ARM-based Teensy, please post a detailed message on this forum.

In most cases, I do investigate and fix whatever is incompatible. Several years of this kind of work has made almost everything in the Arduino world pretty compatible. :)
 
Status
Not open for further replies.
Back
Top