cnlohr / rv003usb

CH32V003 RISC-V Pure Software USB Controller
MIT License
436 stars 43 forks source link

Bootloader timeout configuration with Host detection #39

Closed xsrf closed 7 months ago

xsrf commented 8 months ago

Adds two new #defines to configure the bootloader timeout

// Timeout for bootloader after power-up, set to 0 to stay in bootloader forever
// 75ms per unit; 67 ~= 5s
#define BOOTLOADER_TIMEOUT_PWR 67

// Timeout (reset) for bootloader once USB Host is detected, set to 0 to stay in bootloader forever
// 75ms per unit; 0 costs 28 Bytes, >0 costs 48 Bytes; Comment out if not used
#define BOOTLOADER_TIMEOUT_USB 0

If set to e.g. 4 and 0 this will cause the bootloader to boot usercode after only 300ms if no USB Host is present. If an USB Host is present on power up, it will stay in bootloader forever. Perfectly for devices that only need USB for firmware updates but are otherwise just powered by battery / USB wallplug.

Also added a flag to configure if the Port used for USB is reset to all Inputs at the start, or kept as is (besides the USB pins). This can save few Bytes if not required, so it's off now by default.

// Set this if you want the bootloader to keep the Port configuration untouched
// Otherwise it will reset all Pins on the Port used to input; costs 8-16 Bytes
#define BOOTLOADER_KEEP_PORT_CFG

With all features enabled at max and PORTD used, the bootloader will exceed it's maximum size, so you may have to take some trade-offs.

This includes https://github.com/cnlohr/rv003usb/pull/38