Cortex code with Teensy

Status
Not open for further replies.

floating.

Well-known member
I got a few Teensy 3.2 and 3.5 boards for several relatively simple projects that use Arduino code. They work fine that purpose.

However, for an upcoming project I would like to have bit more performance and control, and I wonder if it's possible to use either Teensy board as a Cortex board without the Arduino layer between my program and the CPU. If yes, how?

TIA .
 
It's certainly possible to program the Teensy hardware without Arduino. If you turn on "Show verbose output during compilation" in the Arduino editor (IDE?) preferences, you can see the gcc commands to build the Teensy hex file. The paths in the compiler output should lead you to all sorts of interesting things. Plenty of source code is here.

There really isn't much between your code and the microcontroller. Teensyduino code performs the processor initialization and supplies a clean interface to the GPIO. Lots of optional libraries provide other functionality. Nothing stops you from accessing configuration registers or writing inline assembler.

The setup() and loop() functions are just a preprocessor hack to make Arduino more beginner friendly.

I'd suggest you write your project in a straightforward way using Arduino libraries when they help finish more quickly. If you do run into performance problems, people here are generally very helpful. Even if you intend to go on and do things that have already been done for the sake of learning, having a working version to benchmark and compare against would help us all understand the real overhead, if any.
 
Before going down this path, better to read this and this post.

In short, unless you are an experienced developer with a lot of ARM notches on your belt, ditching Arduino/Teensyduino will probably not result in any more performance, stability or control over the chip. Try to optimise your code and experimenting with the different compiler options first. It's also possible to control a lot of the Teensy hardware directly from inside the Arduino code. My suggestion would be to switch to more low-level programming only when you have fully developed and tested your application in Arduino (or any other IDE), unless the goal of your project is to learn ARM development and not developing a specific application.
 
There are 2 ways of viewing this question.

#1 - Many people do use something other than the Arduino IDE. The most common are Visual Micro, Eclipse with Jantje's plugin, and running a makefile from the command line. For this last option, the Teensyduino installer puts a sample makefile in hardware/teensy/avr/cores/teensy3. Usually the same core library and libraries you'd use with Arduino get used with these environments.

I personally use Arduino's "use external editor" feature, from File > Preferences, and they I edit code with my favorite text editor.

#2 - Often we get people who believe they'll get higher performance if they write everything from scratch. This question comes up over and over on this forum. Usually it's a pretty misguided idea, partly because it already is a bare metal system, partly because people familiar with very old architectures like PIC16 vastly underestimate the complexity & learning curve of these modern parts and their peripherals, and partly because so much of the existing code has a tremendous amount of optimization work that very few people could manage to even match with less than a man-year of intense work.

However, not everything is fast. For example, digitalWrite versus digitalWriteFast, or analogRead versus the ADC library, or SD versus SdFat. Often we get people asking these sorts of questions, and after much discussion it's finally revealed they're doing something like bitbashing waveforms which could have been accomplished using timers (maybe with DMA). Taking the best approach and fully leveraging powerful peripherals usually results in far better overall performance than hand optimizing code (much of which is already pretty well optimized in the libraries).

But if you really want to go full from-scratch bare metal, the makefile and linker script are there for your hacking pleasure. Maybe at least start with the exist code and replace it piecewise. Some parts like the USB have a particularly steep learning curve!

If you think long term though, one pretty compelling reason to use the existing code through its published API is easy compatibility with future chips. In the not too distant future 400 to 600 MHz microcontrollers will become the norm. If you want to use those, you'll have a lot of work to redo if you've tied all your code very tightly to today's 100 to 180 MHz chips.
 
Thanks for the replies. I already changed "Show verbose output during compilation" and some other option to get more feedback from the compiler. I guess I was a little terse and not very clear in my OP, so maybe a few bits of additional explanation can help to clear things up.

First, it's not really me who wants to get familiar with the Cortex, it's my boss.

The majority of our products are more than big enough to house an Arduino or a Teensy to do certain things, and the code is more than fast enough for what we use them. However, there is a plan for a solar powered device with battery backup, so power consumption will be paramount, and I'm not sure if a Teensy 3.5 or 3.6 can be as good as a bare Cortex in that respect. I must admit that I'm not familiar enough with the Teensys or the possible Cortex CPUs/uCs in that respect. As far as I concern, I'd be more than happy to write Arduino/Teensyduino code, but the decision on the CPU in that planned product is not mine, though I may have some input on it.
 
and I'm not sure if a Teensy 3.5 or 3.6 can be as good as a bare Cortex in that respect.
Well, no project is a bare Cortex without any additional HW you'll need some in- or output at least. If it is solar powered - you will not notice any difference. It simply does not matter if the cpu uses some μA more or less. A short shadow from a tree or little cloud costs more power.Your boss is not very smart... he's going to spend much much very expensive time for nothing.. but ok... ;)
Then, the ONLY difference to "bare metal" in this respect is the systick-timer. Just disable it, and the only code that will run is your code. No Arduino. Just don't use any libraries (wtf?, ok, you want it..) edit: and use a while(1){} inside loop(){} to prevent unneeded things like serialEvent - or better: write your own idle(){}

There is no "Arduino layer". It is not a operating system. Your code runs directly on the MCU. There is nothing between. really ;-)
 
Last edited:
If low power consumption is your goal, I'll buy you a beer if you ever get even three micro-amps lower than what is possible with Duffs' Snooze library :p . 6µA on a Teensy LC, 15µA on a 3.x.
 
If low power consumption is your goal, I'll buy you a beer if you ever get even three micro-amps lower than what is possible with Duffs' Snooze library :p . 6µA on a Teensy LC, 15µA on a 3.x.
It may very well be -- I just simply don't know. Our Teensys arrived Monday afrenoon, so I have about 3 - 4 days of experience with them. And no previous Cortex experience either, though I have some Arduino experience, plus 10+ years of other, related experience.

If it is solar powered - you will not notice any difference. It simply does not matter if the cpu uses some μA more or less. Your boss is not very smart... he's going to spend much much very expensive time for nothing.. but ok... ;)
It should also run when there is no Sun (at last until the battery runs down, that is) so a μA or two may actually matter.

My boss is actually very smart in some respects (he is an EE, BTW) but seeing some of the old code written before my time -- following and enforcing good programming practices is not among them. :p

In any case, as long as I get paid, he can choose the CPU.
 
Status
Not open for further replies.
Back
Top