platformio / platform-atmelsam

Atmel SAM: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelsam
Apache License 2.0
78 stars 105 forks source link

SBU not being linked on firmware #168

Open alvarolb opened 2 years ago

alvarolb commented 2 years ago

I cannot make SBU working (and I assume other SAMD libraries like SDU, SFU, SNU...).

With this basic Sketch on Arduino, it compiles fine and includes the code required in SBU (basically detect firmware files from external storage on Ublox chip, and flash before boot):

#include <MKRNB.h>
#include <SBU.h>

void setup() {
}

void loop() {

}

When compiling this sketch, it is clear that the SBU library is compiled and linked:

El Sketch usa **46584 bytes (17%)** del espacio de almacenamiento de programa. El máximo es **262144 bytes**.
Las variables Globales usan **2692 bytes (8%)** de la memoria dinámica, dejando 30076 bytes para las variables locales. El máximo es 32768 bytes.

If I comment out the #include <SBU.h>, then the code size is reduced from 17% to 5%:

El Sketch usa **13816 bytes (5%)** del espacio de almacenamiento de programa. El máximo es 262144 bytes.
Las variables Globales usan **2692 bytes (8%)** de la memoria dinámica, dejando 30076 bytes para las variables locales. El máximo es 32768 bytes.

By the way, the sketch compiled in Platformio IDE has always the same size, independently of the SBU.h include

Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   6.7% (used 2204 bytes from 32768 bytes)
Flash: [          ]   4.6% (used 11968 bytes from 262144 bytes)

This is the platformio.ini used:

[env:mkrnb1500]
platform = atmelsam
board = mkrnb1500
framework = arduino

I suspect that this is cased by the SBU.h header definition, that literally does nothing:

#ifndef _SBU_H_INCLUDED
#define _SBU_H_INCLUDED

/* Nothing to do */

#endif /* _SBU_H_INCLUDED */

However, the .cpp contains code that it is not being included. Whats the reason? How can we solve it?

Here is the link for SBU source code:

https://github.com/arduino/ArduinoCore-samd/tree/master/libraries/SBU/src

valeros commented 2 years ago

Hi @alvarolb ! It seems that this library is optimized during the build process. Does it work if you link the dependencies as object files using the following configuration:

[env:mkrnb1500]
platform = atmelsam
framework = arduino
board = mkrnb1500
lib_archive = no
alvarolb commented 2 years ago

Hi, It does not work, as the SBU does not run at startup. I can confirm it because:

Moreover, if I flash the following sketch in Arduino, it flash the OTA file present in the device. So, the firmware is well placed and it is valid:

#include <SBU.h>

void setup()
{
  Serial.begin(9600);
  while (!Serial) { }
  // wait a bit
  delay(1000);
  String message;
  message += "Sketch compile date and time: ";
  message += __DATE__;
  message += " ";
  message += __TIME__;
  // print out the sketch compile date and time on the serial port
  Serial.println(message);
}

void loop()
{
  // add you own code here
}

The strange thing is that the firmware size increases a lot with this flag, but it seems that the SBU code is never executed.

alvarolb commented 2 years ago

I have also tried to directly include the contents of SBU.cpp in the sketch, but it does not work. It seems that the __attribute__ code is not working properly?

__attribute__ ((section(".sketch_boot")))
unsigned char SBU_BOOT[0x8000] = {
#if defined(ARDUINO_SAMD_MKRNB1500)
   #include "boot/mkrnb1500.h"
#else
  #error "Unsupported board!"
#endif
};
alvarolb commented 2 years ago

After testing with more devices, other implementations with SDU, SFU, SNU, etc., seems to work adding lib_archive = no. Not sure why SBU (which is the first I tested) does not work properly.