Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
added reboot after download feature and revised bcdDevice version
Browse files Browse the repository at this point in the history
  • Loading branch information
majbthrd committed Dec 11, 2021
1 parent a7d18ba commit f1e4bda
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ The USBCRM mode should be universal, as it doesn't depend on optional external c

Pre-compiled images are already available via this project's Releases tab.

[Rowley Crossworks for ARM](http://www.rowley.co.uk/arm/) is presently suggested to compile this code, as it includes support for Clang; at this time, I am not aware of any other ready-to-use (and multi-OS to boot) Clang ARM cross-compiler package that I can readily point users to. With Crossworks for ARM v4.8.1, compiling v1.06 using the Clang 11.1.0 compiler produces a 995 byte image. The more mainstream GCC lags behind Clang, although more recent GCC versions produce code that is less overweight than in years past.
[Rowley Crossworks for ARM](http://www.rowley.co.uk/arm/) is suggested to compile this code, as it includes support for Clang; at this time, I am not aware of any other ready-to-use (and multi-OS to boot) Clang ARM cross-compiler package that I can readily point users to. The more mainstream GCC lags behind Clang, although more recent GCC versions produce code that is less overweight than in years past.

|bootloader variant|Clang 9.0.1 (-O1) |Clang 11.1.0 (-O1) |GNU Arm 2018-q3 (-Os) |GNU Arm 2019-q4 (-Os) |
|------------------|------------------|-------------------|----------------------|----------------------|
| USE_DBL_TAP | 1003 bytes | 995 bytes | 1044 bytes (too big!)| 1041 bytes (too big!)|
| GPIO input | 979 bytes | 975 bytes | 1008 bytes | 1006 bytes |
|bootloader variant |Clang 11.1.0 (-O1) |Clang 13.0.0 (-O1) |GNU Arm 2019-q4 (-Os) |
|-----------------------------------|-------------------|-------------------|------------------------|
| USE_DBL_TAP+REBOOT_AFTER_DOWNLOAD | 999 bytes | 999 bytes | 1024 bytes (just fits!)|
| USE_DBL_TAP | 983 bytes | 983 bytes | 1012 bytes |
| GPIO input+REBOOT_AFTER_DOWNLOAD | 979 bytes | 979 bytes | 996 bytes |
| GPIO input | 963 bytes | 963 bytes | 984 bytes |

A Makefile supporting GCC is provided, but even if you have the latest GCC, it may not be able to output a version within the 1024 bytes available. The latest GCC for ARM can be here: [GNU Arm Embedded Toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm). Note that if you are adapting the Makefile for use with clang, try replacing the "-Os" argument in CFLAGS with something like "-O1" if the output size is larger than expected.

## Programming targets with bootloader

[OpenOCD](http://openocd.org/) is open-source and freely available. Given a debug unit interfaced to the target, it will not only program the flash but it also has built-in support for setting the BOOTPROT bits to provide write-protection of the bootloader.

Testing was done with OpenOCD v0.10.0 using both the debug units built-in to the ATSAMD11-XPRO and ATSAMD21-XPRO as well as the [Dapper Miser CMSIS-DAP](https://github.com/majbthrd/DapperMiser) debug unit.
Testing was done with OpenOCD v0.10.0 using both the debug units built-in to the ATSAMD11-XPRO and ATSAMD21-XPRO as well as the [Dapper Miser CMSIS-DAP](https://github.com/majbthrd/DapperMiser) and [Dapper Mime CMSIS-DAP](https://github.com/majbthrd/DapperMime) debug units.

Your mileage may vary, particularly with earlier releases of OpenOCD. (The wisdom of the Internet implies earlier versions of OpenOCD had bugs with setting the BOOTPROT bits.) You don't have to use the BOOTPROT bits, but adding write-protection to the bootloader is generally a desirable attribute.

Expand Down
9 changes: 9 additions & 0 deletions bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

/*- Definitions -------------------------------------------------------------*/
#define USE_DBL_TAP /* comment out to use GPIO input for bootloader entry */
#define REBOOT_AFTER_DOWNLOAD /* comment out to prevent boot into app after it has been downloaded */
#define USB_CMD(dir, rcpt, type) ((USB_##dir##_TRANSFER << 7) | (USB_##type##_REQUEST << 5) | (USB_##rcpt##_RECIPIENT << 0))
#define SIMPLE_USB_CMD(rcpt, type) ((USB_##type##_REQUEST << 5) | (USB_##rcpt##_RECIPIENT << 0))

Expand Down Expand Up @@ -210,6 +211,14 @@ static void __attribute__((noinline)) USB_Service(void)
dfu_status = dfu_status_choices + 2;
dfu_addr = 0x400 + request->wValue * 64;
}
#ifdef REBOOT_AFTER_DOWNLOAD
else
{
/* the download has now finished, so now reboot */
WDT->CONFIG.reg = WDT_CONFIG_PER_8 | WDT_CONFIG_WINDOW_8;
WDT->CTRL.reg = WDT_CTRL_ENABLE;
}
#endif
/* fall through */
default: // DFU_UPLOAD & others
/* 0x00 == DFU_DETACH, 0x04 == DFU_CLRSTATUS, 0x06 == DFU_ABORT, and 0x01 == DFU_DNLOAD and 0x02 == DFU_UPLOAD */
Expand Down
2 changes: 1 addition & 1 deletion usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ usb_device_descriptor_t usb_device_descriptor __attribute__ ((aligned (4))) = /*
.bMaxPacketSize0 = 64,
.idVendor = 0x1209,
.idProduct = 0x2003,
.bcdDevice = 0x0105,
.bcdDevice = 0x0107,

.iManufacturer = USB_STR_ZERO,
.iProduct = USB_STR_ZERO,
Expand Down

0 comments on commit f1e4bda

Please sign in to comment.