PlatformIO breaks my arduino code

I have a large (900+ LOC) arduino single sketch which is becoming a nightmare to maintain, so I decided to refactor and clean it up. To make my life easier I decided to leave Arduino IDE and use PlatoformIO, but PlatormIO compiled program doesn’t work as expected on my arduino (or at least as Arduino IDE compiled sketch).

My code uses SPI

My arduino is an atmega328p nano

I figured out this might be because of some wrong settings of the compiler, what I tried:

  1. Adding the same compile flags Arduino IDE uses

  2. Using all levels of optimization: -O0 works as expected but inacceptably slow, -Os -O1 -O2 -O3 compiled code seems to get stuck into a for loop in loop() function

  3. Check if I’m missing some volatile declarations

Can anybody help me?
Many thanks

Does your sketch have .ino extension? Try to rename it to .cpp and manully declare all prototypes. Does it work now?

I’m sorry, I forgot: yes I tried that way and it doesn’t change

Try to build project in verbose mode via pio run -v and compre with Arduino IDE build output. Maybe it will help.

Done. The differences between the two outputs are more or less just about locations for avr and other tools on my hard disk. Anyway I noticed that that pio builds of my code are 4kb smaller that Arduino IDE ones, even with the same optimization flags

Last version of AVR toolchain produces smaller binaries. I believe this was included first with Arduino 1.6.11 and recently has been updated in PIO.

Please open an issue here Issues · platformio/platform-atmelavr · GitHub