cnlohr / rv003usb

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

Help: Reset to bootloader by software or hardware button #52

Closed vslinuxdotnet closed 5 months ago

vslinuxdotnet commented 5 months ago

Hello, Is possible to reset the MCU to bootloader starts? The reset button, reset by software or watchdog only resets it to run the code flash, and not to bootloader. EX software reset: PFIC->CFGR = PFIC_RESETSYS | PFIC_KEY3 may miss some pointer to define the start reset vector flash if exits ? this: actual = Code flash start is 0x0800 need = Bootloader flash start is 0x1FFF

Thank you, VS

xsrf commented 5 months ago

you can use this to reset into bootloader

FLASH->BOOT_MODEKEYR = FLASH_KEY1;
FLASH->BOOT_MODEKEYR = FLASH_KEY2;
FLASH->STATR = 1<<14; // boot into bootloader
PFIC->SCTLR = 1<<31; // Reset

but be aware that there is still a condition required for the bootloader to stay active or it will just boot the user-code again.

vslinuxdotnet commented 5 months ago

you can use this to reset into bootloader

FLASH->BOOT_MODEKEYR = FLASH_KEY1;
FLASH->BOOT_MODEKEYR = FLASH_KEY2;
FLASH->STATR = 1<<14; // boot into bootloader
PFIC->SCTLR = 1<<31; // Reset

but be aware that there is still a condition required for the bootloader to stay active or it will just boot the user-code again.

It works in code, but no usb device appears, some code to reset the usb device ? What is the condition required for the bootloader to stay active ? Thanks

xsrf commented 5 months ago

USB_PIN_DM needs to be low for few µs before you reset to ensure a USB re-enumeration. Depending on your schematic either disable the pull-up on D- or force D- low (output low). You confirmed that the bootloader works otherwise on power-up, right?

vslinuxdotnet commented 5 months ago

USB_PIN_DM needs to be low for few µs before you reset to ensure a USB re-enumeration. Depending on your schematic either disable the pull-up on D- or force D- low (output low). You confirmed that the bootloader works otherwise on power-up, right?

MY D- pull-up is connected to the MCU GPIO. Yes in power up the bootloader works by USB and wait 5 seconds, but with your code just wait 2/3 seconds. so it I set the GPIO pull-up resistor before the code reset will work ?

vslinuxdotnet commented 5 months ago

I try "USB_PIN_DM needs to be low for few µs before you reset to ensure a USB re-enumeration. Depending on your schematic either disable the pull-up on D- or force D- low (output low)." with no success. So I see the "void boot_usercode()" in bootloader example and try...

procedure: FLASH->BOOT_MODEKEYR = FLASH_KEY1; FLASH->BOOT_MODEKEYR = FLASH_KEY2; FLASH->STATR = 0; // 1<<14 is zero, so, boot user code. FLASH->CTLR = CR_LOCK_Set; FLASH->STATR = 1<<14; // boot into bootloader PFIC->SCTLR = 1<<31; // Reset

reset the device correctly, and goes to bootloader with USB detected just fine!

Thanks for the help @xsrf