Faster Arduino Code With Forced Inline Functions (c++). Visual Demonstration.
HTML-код
- Опубликовано: 14 июл 2024
- This is a tutorial that shows how to get inline functions/methods working for Arduino. With Arduino IDE's default c++ compiler optimization parameters it ignores the "inline" keyword. So you need to force it with "__attribute__((always_inline))".
Article about forcing inline functions with Arduino:
circuitjournal.com/arduino-fo...
Music by Epidemic Sound (www.epidemicsound.com) - Наука
As far as I know, the default optimization setting for Arduino is "optimize for size", which explains (at least sometimes) why the compiler ignores the "inline" keyword. I wasn't aware of the attribute you showed here - it can be useful in extreme cases, but as usual with optimization, one must never trust *any* technique without measurement.
Yes, you are right. It is possible that if you use some other compiler then "__attribute__((always_inline))" might not work. Or might not be even recognized as valid syntax.
I am pretty sure I could make the "inline" keyword work if I changed the compiler optimization parameters for Arduino IDE. But then it wouldn't be as simple as just download the code and and run.
6
@@DerekWelchElectric I'll see your 6 and raise you 7! :-D
Thank you! This saved me a ton of time searching for the right way to do inline functions. Perfect for speed freaks! I also like the way you have taken the trouble to speed up the video in places to keep the focus alive on your presentation!
Thanks for the feedback!
Great video, concise and to the point. I'm surprised it doesn't have more views.
Thanks!
Every time i watch this channel, i learn tons of new things/ Thanx you so much
Clear, useful and simple. Excellent 10/10
Thanks!
Useful one
Great video, thanks 👍🏻
Is there also a way to loop it, so you get a continuous square wave output?
Or to you always have to wait to finish the rest of the code/loop?
Hey It would be better to use timer interrupts to generate continuous square waves. That you you can even generate up to 8Mhz signal.
I have a video about generating pulses with interrupts:
ruclips.net/video/m4EpTYaBBJ4/видео.html
Thanks
Interesting video. I'm getting an error doing inlining and stumbled across your video in my search for a solution. The error I get is:
*warning: 'always_line' attribute directive ignored [-Wattributes]*
I thought it might be because the standard avr gcc optimisation is set to -Os (space optimisation) and using inline functions goes against that!
However, I tried all the optimisation functions (-O0, -O1, -O2, -O3) and the same warning appeared and the code size did not change, indicating that inlining was not taking place.
I'd be interested in the version of the compiler/linker you used here, as in 1.8.13 it refused to allow me that option.
I usually use Eclipse (not the Arduino IDE) but as it calls the same underlying core it also does not allow it.
Hey! I tried with the latest Arduino IDE 1.8.13 and for me, it worked. When I remove the always_line attribute the .hex files get smaller. Have you tried your code with Arduino IDE also? Maybe the tools for Eclipse aren't exactly the same or it adds some compiler options that Arduino IDE doesn't add.
@@IndrekL Yes, I've tried in both platforms. I'm using the standard Arduino AVR core but even with the MiniCore I get the same error, probably because the underlying compiler/linker is the same. You don't get any warnings? Have you got "verbose output during compilation" and "Compiler Warnings" set to "All"? I'm stumped! Can you see anything in the command lines below that might give me the warning?
Compiling sketch...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_793303\\sketch\\Arduino_Double_Blink_Temp.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_793303\\sketch\\Arduino_Double_Blink_Temp.ino.cpp.o"
I:\Arduino Sketches\Arduino_Double_Blink_Temp\Arduino_Double_Blink_Temp.ino:4:54: warning: 'always_line' attribute directive ignored [-Wattributes]
inline void showLEDhigh() __attribute__((always_line));
FIXED!!! I guess proof-reading the attribute name would have helped had I taken the time to do so. I realised my error because I changed to *noinline* which worked and then back to *always_inline* at which point I discovered that I had originally had *always_line* (without the in). Doh! Anyway, all works as expected now. I'll do my very best to mention your website/RUclips video on this subject.
Thank you for replying back with the solution! I thought it was strange that it didn't work with Arduino IDE. I thought that Eclipse may add some compiler option that Arduino IDE doesn't, but it didn't make sense that the default Arduino IDE works differently than mine.
I also didn't notice the "always_line" in "warning: 'always_line' attribute directive ignored [-Wattributes]"
How about code size of online vs without inline?
My sketch 82% of flash.
intresing aproach! i am a physics teacher who love STEM and arduino simplecity but i need sometimes to create lab tools with precise time control or the sketch seem easy but not exactly accurate in action-i am an intermediate or beginner level progammer-.Can you recommend tutorial or books to upgrade my knowledge? thanks!
Try to first know the programming logic
Can you please tell me what was like the time period for each half cycle at the output? Was it like 62.5 nanoseconds?
Yes. It is an 8Mhz signal and the full period is about 125ns.
Here is also the Oscilloscope image:
circuitjournal.com/img/articles/8_arduino_force_inline/no_gaps_beteen_pulses.png
One division on the horizontal scale is 200ns
Is it possible to down load this code?
This may be the compilers optimization settings, should test this in atmel studio and change optimizer to performance, then inline may work without the attribute.
Yes, the default optimization parameter in Arduino ide is to reduce size (-Os)
Faster, but uses more storage due to unlined functions….
don't waste your time with arduino. they create arduino for kids and non technical base user friendly in mind. with some silent killer underlying code twists that lead pro user to disaster if not careful. if you need more juice from avr just learn mplabx or atmel studio
Agreed …. asm all the way… then you can talk about speed….