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)
  • НаукаНаука

Комментарии • 33

  • @idogendel
    @idogendel 5 лет назад +10

    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.

    • @IndrekL
      @IndrekL  5 лет назад +2

      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.

    • @DerekWelchElectric
      @DerekWelchElectric 2 года назад

      6

    • @idogendel
      @idogendel 2 года назад

      @@DerekWelchElectric I'll see your 6 and raise you 7! :-D

  • @ramradhakrishnan9382
    @ramradhakrishnan9382 5 лет назад +3

    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!

    • @IndrekL
      @IndrekL  5 лет назад +1

      Thanks for the feedback!

  • @regal_7877
    @regal_7877 4 года назад +2

    Great video, concise and to the point. I'm surprised it doesn't have more views.

  • @innovativtexnologiya5782
    @innovativtexnologiya5782 2 года назад

    Every time i watch this channel, i learn tons of new things/ Thanx you so much

  • @rafa_dzto
    @rafa_dzto 4 года назад +2

    Clear, useful and simple. Excellent 10/10

  • @studentofedison421
    @studentofedison421 Год назад

    Useful one

  • @p_mouse8676
    @p_mouse8676 2 года назад

    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?

    • @IndrekL
      @IndrekL  2 года назад

      Hey It would be better to use timer interrupts to generate continuous square waves. That you you can even generate up to 8Mhz signal.

    • @IndrekL
      @IndrekL  2 года назад

      I have a video about generating pulses with interrupts:
      ruclips.net/video/m4EpTYaBBJ4/видео.html

  • @soerenraudonis
    @soerenraudonis Год назад

    Thanks

  • @RalphBacon
    @RalphBacon 4 года назад

    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.

    • @IndrekL
      @IndrekL  4 года назад

      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.

    • @RalphBacon
      @RalphBacon 4 года назад

      @@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));

    • @RalphBacon
      @RalphBacon 4 года назад +1

      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.

    • @IndrekL
      @IndrekL  4 года назад

      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]"

  • @galihtanu
    @galihtanu 2 года назад

    How about code size of online vs without inline?
    My sketch 82% of flash.

  • @nektarioskourakis8331
    @nektarioskourakis8331 2 года назад

    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!

    • @drakoky6894
      @drakoky6894 2 года назад

      Try to first know the programming logic

  • @may21136
    @may21136 3 года назад

    Can you please tell me what was like the time period for each half cycle at the output? Was it like 62.5 nanoseconds?

    • @IndrekL
      @IndrekL  3 года назад

      Yes. It is an 8Mhz signal and the full period is about 125ns.

    • @IndrekL
      @IndrekL  3 года назад

      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

  • @Tanuki5
    @Tanuki5 Год назад

    Is it possible to down load this code?

  • @LittleRainGames
    @LittleRainGames 3 года назад

    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.

    • @IndrekL
      @IndrekL  3 года назад

      Yes, the default optimization parameter in Arduino ide is to reduce size (-Os)

  • @gordmills1983
    @gordmills1983 4 месяца назад

    Faster, but uses more storage due to unlined functions….

  • @cheapmod
    @cheapmod Год назад

    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

    • @gordmills1983
      @gordmills1983 4 месяца назад

      Agreed …. asm all the way… then you can talk about speed….