-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for resetting RP2040 via the USB connection when using pi…
…co_stdio_usb - setting baud rate to magic value (default=1200) will cause a reset to BOOTSEL mode - a VENDOR interface along side the CDC interface can be used to reset via refular flash boot, or into BOOTSEL mode with control for the reset_usb_boot parameters for the latter either method can be configured/enabled/disabled via #define
- Loading branch information
1 parent
fb5a847
commit 329ec28
Showing
8 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/rp2_common/pico_stdio_usb/include/pico/stdio_usb/reset_interface.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#ifndef _PICO_STDIO_USB_RESET_INTERFACE_H | ||
#define _PICO_STDIO_USB_RESET_INTERFACE_H | ||
|
||
// We use VENDOR, 0, 0 for PICOBOOT, so lets use VENDOR, 0, 1 for RESET | ||
|
||
// VENDOR sub-class for the reset interface | ||
#define RESET_INTERFACE_SUBCLASS 0x00 | ||
// VENDOR protocol for the reset interface | ||
#define RESET_INTERFACE_PROTOCOL 0x01 | ||
|
||
// CONTROL requests: | ||
|
||
// reset to BOOTSEL | ||
#define RESET_REQUEST_BOOTSEL 0x01 | ||
// regular flash boot | ||
#define RESET_REQUEST_FLASH 0x02 | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
#include "pico/stdio_usb.h" | ||
|
||
#if PICO_STDIO_USB_ENABLE_RESET_VIA_VENDOR_INTERFACE | ||
#include "pico/bootrom.h" | ||
#include "pico/stdio_usb/reset_interface.h" | ||
#include "hardware/watchdog.h" | ||
#include "device/usbd_pvt.h" | ||
|
||
static uint8_t itf_num; | ||
|
||
static void resetd_init(void) { | ||
} | ||
|
||
static void resetd_reset(uint8_t __unused rhport) { | ||
itf_num = 0; | ||
} | ||
|
||
static uint16_t resetd_open(uint8_t __unused rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) { | ||
TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == itf_desc->bInterfaceClass && | ||
RESET_INTERFACE_SUBCLASS == itf_desc->bInterfaceSubClass && | ||
RESET_INTERFACE_PROTOCOL == itf_desc->bInterfaceProtocol, 0); | ||
|
||
uint16_t const drv_len = sizeof(tusb_desc_interface_t); | ||
TU_VERIFY(max_len >= drv_len, 0); | ||
|
||
itf_num = itf_desc->bInterfaceNumber; | ||
return drv_len; | ||
} | ||
|
||
// Support for parameterized reset via vendor interface control request | ||
static bool resetd_control_request_cb(uint8_t __unused rhport, tusb_control_request_t const *request) { | ||
if (request->wIndex == itf_num) { | ||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL | ||
if (request->bRequest == RESET_REQUEST_BOOTSEL) { | ||
uint gpio_mask = 0; | ||
if (request->wValue & 0x100) { | ||
gpio_mask = 1u << (request->wValue >> 9u); | ||
} | ||
reset_usb_boot(gpio_mask, request->wValue & 0x7f); | ||
// does not return, otherwise we'd return true | ||
} | ||
#endif | ||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_FLASH_BOOT | ||
if (request->bRequest == RESET_REQUEST_FLASH) { | ||
watchdog_reboot(0, SRAM_END, PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS); | ||
return true; | ||
} | ||
#endif | ||
} | ||
return false; | ||
} | ||
|
||
static bool resetd_control_complete_cb(uint8_t __unused rhport, tusb_control_request_t __unused const *request) { | ||
return true; | ||
} | ||
|
||
static bool resetd_xfer_cb(uint8_t __unused rhport, uint8_t __unused ep_addr, xfer_result_t __unused result, uint32_t __unused xferred_bytes) { | ||
return true; | ||
} | ||
|
||
static usbd_class_driver_t const _resetd_driver = | ||
{ | ||
#if CFG_TUSB_DEBUG >= 2 | ||
.name = "RESET", | ||
#endif | ||
.init = resetd_init, | ||
.reset = resetd_reset, | ||
.open = resetd_open, | ||
.control_request = resetd_control_request_cb, | ||
.control_complete = resetd_control_complete_cb, | ||
.xfer_cb = resetd_xfer_cb, | ||
.sof = NULL | ||
}; | ||
|
||
// Implement callback to add our custom driver | ||
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count) { | ||
*driver_count = 1; | ||
return &_resetd_driver; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters