I've been trying to upload a sketch using a programmer instead of the bootloader but the sketch doesn't work when it is programmed this way. I am using a SAMD21 microcontroller with in built USB.
I am using a simple test sketch that initialised the USB Serial port and prints a message every 500 millis. I am using the Adafruit Arduino core from here: GitHub - adafruit/ArduinoCore-samd
If I flash the bootloader to the device I can upload a sketch just fine, the USB serial port appears and I can open the serial motitor and see the output, but the following things faild to work:
If I use the 'export compiled binary' option and use Atmel Studio to flash the chip the sketch doesn't run, no USB port appears.
If I use the 'Upload Using Programmer' option in Arduino it will upload, but the sketch will not run and there is no USB Serial port - exactly the same as above.
Each time I have to re-flash the bootloader to get the board back.
I did an experiment and re-flashed the bootloader, then used it to uploaded a sketch and verify it was running properly. I then used Atmel Studio to download the devices memory as a hex file before erasing the device and re-fashing it with the binary - it works fine.
Also, if I use Atmel Studio to flash the
I assume what is hapening here is that the bootloader is not being included with the sketch when compiled into the binary, and that the bootloader contains essential code needed to initialise the USB peripheral, and possibly other sub systems as well.
Is there any way to generate a binary or hex file that includes the bootloader so it can (at some point in the future) be flashe by a production programmer ...? or have I completly misunderstood what is going on here?
it is about the target address. the start address after reset/power-up is always 0x0. so if you flash the application (sketch) to 0x0, the bootloader is gone and the sketch runs.
to preserve the bootloader the sketch must be flashed at address where the bootloader expects it. the address is in Arduino SAMD core in boards.txt as bootloader.size. default is 0x2000 for MKR and Zero bootloader. M0 arduino.org bootloader has 0x4000
I understand that, but it doesn't really deal with the problem that the sketch seems to require startup code in the bootloader and so it won't initialise the USB peripheral if the bootloader is not there.
It also doesn't deal with the question of how to generate a binary that includes everything required for the sketch to actually work.
BillBigge:
I understand that, but it doesn't really deal with the problem that the sketch seems to require startup code in the bootloader and so it won't initialise the USB peripheral if the bootloader is not there.
It also doesn't deal with the question of how to generate a binary that includes everything required for the sketch to actually work.
you don't need the bootloader in the bin if it is uploaded to the right address it doesn't delete the bootloader.
the sketch doesn't require the bootloader for USB. what board do you select? ad why do you use the fork of the SAMD boards instead of normal IDE install?
I'm using a fork because I have a custom board with custom pin definitions.
You DO need the bootloader in the bin IF you want to production program a board with a sketch and bootloader in a single shot. Otherwise you have to perform two flash operations, one for the bootloader and another for the sketch.
I need to double check that there wasn't some other problem going on here, but I pretty sure I tried this with working code and it wouldn't work if the bootloader was removed and the sketch loaded at the start of flash. If the bootloader was not needed at all then my sketch would run, but it didn't - I do need to re check because I ran through a lot of options trying to figure this out and may have missed something.