cnlohr / rv003usb

CH32V003 RISC-V Pure Software USB Controller
MIT License
459 stars 47 forks source link

WIP: Add configurable Boot-Button #40

Closed xsrf closed 9 months ago

xsrf commented 9 months ago

This adds the option to configure a Button you have to press during boot to enter the Bootloader instead of (or in addition to) using Timeouts. The Pin can be configured freely and even be on a different Port than the USB. It can be set to be a floating input, or pulled up or down. The level that triggers the bootloader can also be configured.

// Bootloader Button Config
// If you want to use a Button during boot to enter bootloader, use these defines 
// to setup the Button. If you do, it makes sense to also set DISABLE_BOOTLOAD above, 
// set BOOTLOADER_TIMEOUT_PWR to 0 and disable BOOTLOADER_TIMEOUT_USB
#define BOOTLOADER_BTN_PORT D
#define BOOTLOADER_BTN_PIN 2
#define BOOTLOADER_BTN_TRIG_LEVEL 0 // 1 = HIGH; 0 = LOW
#define BOOTLOADER_BTN_PULL 1 // 1 = Pull-Up; 0 = Pull-Down; Optional, comment out for floating input

While you can still use the Timeouts and USB Host detection feature from https://github.com/cnlohr/rv003usb/pull/39 together with the button, it makes sense to disable them. All features together won't fit the 1920 Bytes anymore.

#define DISABLE_BOOTLOAD
#define BOOTLOADER_TIMEOUT_PWR 0
//#define BOOTLOADER_TIMEOUT_USB 0

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

xsrf commented 9 months ago

Okay, I messed this one up. BOOTLOADER_BTN_PORT != USB_PORT doesn't work as expected. I guess because both are undefined in the end as A, C or D are not defined...

xsrf commented 9 months ago

So I came up with this:

#define PORTIDA 0
#define PORTIDC 2
#define PORTIDD 3
#define LOCAL_CONCAT(A, B) A##B
#define LOCAL_EXP(A, B) LOCAL_CONCAT(A,B)
#define LOCAL_EQUALS(A, B) (LOCAL_CONCAT(PORTID,A) == LOCAL_CONCAT(PORTID,B))

// Then use it like this:
#if defined(BOOTLOADER_BTN_PORT) && LOCAL_EQUALS(BOOTLOADER_BTN_PORT, USB_PORT)
...

Is there an easier way? One without additional #defines ? I couldn't get it to work with the existing GPIO_PortSourceGPIOx ...