It appears the eraseFlash() function for SAMD51 is not correct. When trying to flash sketches to an Adafruit Grand Cental M4 Express board (through WifiEspAt) the data written to flash was corrupted. After some searching I found in the SAM D5x datasheet 'The NVM <...> is organized into blocks, where each block contains sixteen pages' and as the memory is to be erased in blocks before writing the the attached patch was applied. Then upload and flash was successful.
InternalStorage.cpp.patch