SAMD board is bricked after upload with a lot of files

Hi! I made a custom Arduino Zero based board. It works great with the blink sketch and everything. All the pins work, and SPI and I2C work, too. I'm trying to upload a custom autopilot sketch I wrote, however, once I upload the sketch, everything stops working. I try to blink an LED in of the first lines of the code and still that doesn't work. The USB interface doesn't work either so I need to double tap the reset button to upload new code. I think it's an issue with memory because of how my code is structured. I have never really needed to deal with memory before even though my code is quite complex.

When I upload, it says this:

Sketch uses 52548 bytes (20%) of program storage space. Maximum is 262144 bytes.
Global variables use 4540 bytes (13%) of dynamic memory, leaving 28228 bytes for local variables. Maximum is 32768 bytes.

I would appreciate the help! The code can be found here: StratoSoar-MK3/Code/autopilot at main · crnicholson/StratoSoar-MK3 · GitHub.

Huh. I commented out the "imu.h" file and everything associated with it and I got the sketch to not brick. But the sketch won't work properly without it. I don't know what to do.

That would indicate that some part of your code overrides variables used by the CDC stack. E.g. writing outside the bounds of arrays; so check that first.

I'm trying to download your code from github but seem to have some ISP problems.

I've downloaded all files manually and after that I installed additional libraries that I did not have.

Full list of used libraries based on verbose compilation output.

Using library tiny-bme280-master in folder: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\tiny-bme280-master (legacy)
Using library Wire at version 1.0 in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14\libraries\Wire 
Using library SparkFun External EEPROM Arduino Library at version 3.2.9 in folder: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\SparkFun_External_EEPROM_Arduino_Library 
Using library TinyGPSPlus at version 1.0.3 in folder: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\TinyGPSPlus 
Using library SPI at version 1.0 in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14\libraries\SPI 
Using library LoRa at version 0.8.0 in folder: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\LoRa 
Using library Servo at version 1.2.2 in folder: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\Servo 

If not done so yet, please enable verbose output during compilation and set compiler warnings to All, both under file/preferences and compile your sketch.

I'm not sure how you have managed to compile because I get an error

Compilation error: narrowing conversion of '32768' from 'int' to 'int16_t {aka short int}' inside { } [-Wnarrowing]

I'm using the latest board package in IDE 2.x

FQBN: arduino:samd:arduino_zero_native
Using board 'arduino_zero_native' from platform in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14
Using core 'arduino' from platform in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14

The below might need attention

C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\gps.cpp: In function 'void gpsSleepTime(long int)':
C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\gps.cpp:118:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   } else if (millis() - sleepStart >= ms) {
              ~~~~~~~~~~~~~~~~~~~~~~^~~~~

And something like this is nonsense (no offence intended); you can only return one value, not multiple values.

C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\gps.cpp:70:19: warning: left operand of comma operator has no effect [-Wunused-value]
       return lat, lon, altitude, year, month, day, hour, minute, second; // Return the previous values if the GPS is not valid.
                   ^~~

Things like this also need attention

C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\lora.cpp:70:25: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   byte p1 = (data >> 2) & 0x1 ^ (data >> 1) & 0x1 ^ (data >> 0) & 0x1;
             ~~~~~~~~~~~~^~~~~

If you want to redefine PI, you will first have to undefine it :wink:

"C:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\arm-none-eabi-gcc\\7-2017q4/bin/arm-none-eabi-g++" -mcpu=cortex-m0plus -mthumb -c -g -Os -Wall -Wextra -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -DF_CPU=48000000L -DARDUINO=10607 -DARDUINO_SAMD_ZERO -DARDUINO_ARCH_SAMD -D__SAMD21G18A__ -DUSB_VID=0x2341 -DUSB_PID=0x804d -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" "-DUSB_PRODUCT=\"Arduino Zero\"" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\CMSIS\\4.5.0/CMSIS/Include/" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\CMSIS-Atmel\\1.2.0/CMSIS/Device/ATMEL/" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\samd\\1.8.14\\cores\\arduino/api/deprecated" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\samd\\1.8.14\\cores\\arduino/api/deprecated-avr-comp" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\samd\\1.8.14\\cores\\arduino" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\samd\\1.8.14\\variants\\arduino_zero" "-Ic:\\Users\\bugge\\OneDrive\\Documents\\Arduino\\libraries\\tiny-bme280-master" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\samd\\1.8.14\\libraries\\Wire" "-Ic:\\Users\\bugge\\OneDrive\\Documents\\Arduino\\libraries\\SparkFun_External_EEPROM_Arduino_Library\\src" "-Ic:\\Users\\bugge\\OneDrive\\Documents\\Arduino\\libraries\\TinyGPSPlus\\src" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\samd\\1.8.14\\libraries\\SPI" "-Ic:\\Users\\bugge\\OneDrive\\Documents\\Arduino\\libraries\\LoRa\\src" "-Ic:\\Users\\bugge\\OneDrive\\Documents\\Arduino\\libraries\\Servo\\src" "C:\\Users\\bugge\\AppData\\Local\\Temp\\arduino\\sketches\\580F796918EA4DE960CCD7260E2A1E55\\sketch\\sd.cpp" -o "C:\\Users\\bugge\\AppData\\Local\\Temp\\arduino\\sketches\\580F796918EA4DE960CCD7260E2A1E55\\sketch\\sd.cpp.o"
In file included from C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14\cores\arduino/api/Interrupts.h:8:0,
                 from C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14\cores\arduino/api/ArduinoAPI.h:29,
                 from C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14\cores\arduino/Arduino.h:23,
                 from C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\i2c_scan.h:22,
                 from C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\i2c_scan.cpp:19:
C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.14\cores\arduino/api/Common.h:32:0: warning: "PI" redefined
 #define PI          3.1415926535897932384626433832795
 
In file included from C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\i2c_scan.h:21:0,
                 from C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\i2c_scan.cpp:19:
C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1317930\ManualDownload\autopilot\settings.h:113:0: note: this is the location of the previous definition
 #define PI 3.1415926

You will need casts in line 575 of ICM20948.cpp (int16_t mag_max[3] = {0x8000, 0x8000, 0x8000}, to get rid of the error.

Wow! Thanks for the detailed response! Most of the times when I post in the forum it's someone grilling on me on my crappy code and note telling how to fix it.

I have verbose on and I can compile alright. Not sure how you're getting those errors!

That's odd. It has worked for me in the past. I guess I'll just put them in a struct.

I did a lot more digging today and I think the issue is the library itself. I changed to a different one and it seems to be working. I need to integrate it now, but that should be easy enough.

Thanks for spending so much time on this, and for spotting so many errors!