From 0fddc61e6ed458ee4358069b7592d03663b5e0ba Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 17 Nov 2024 17:34:32 +0800 Subject: [PATCH 1/4] Implement picotool reboot Works: ``` > sudo ./build/picotool reboot -f -u > sudo ./build/picotool reboot -f ``` TODO: Can we keep USB VID/PID? Signed-off-by: Daniel Schaefer --- qtpy/src/main.rs | 9 +++- qtpy/src/picotool_reset.rs | 93 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 qtpy/src/picotool_reset.rs diff --git a/qtpy/src/main.rs b/qtpy/src/main.rs index 5b2e79d0..b4d9ac92 100644 --- a/qtpy/src/main.rs +++ b/qtpy/src/main.rs @@ -40,6 +40,9 @@ use usb_device::{class_prelude::*, prelude::*}; // USB Communications Class Device support use usbd_serial::{SerialPort, USB_CLASS_CDC}; +mod picotool_reset; +use picotool_reset::PicoToolReset; + // Used to demonstrate writing formatted strings // use core::fmt::Write; // use heapless::String; @@ -99,10 +102,12 @@ fn main() -> ! { &mut pac.RESETS, )); + let mut picotool: PicoToolReset<_> = PicoToolReset::new(&usb_bus); + // Set up the USB Communications Class Device driver let mut serial = SerialPort::new(&usb_bus); - let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(FRAMEWORK_VID, COMMUNITY_PID)) + let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x2e8a, 0x000a)) .strings(&[StringDescriptors::new(LangID::EN_US) .manufacturer("Adafruit") .product("QT PY - Framework 16 Inputmodule FW")]) @@ -147,7 +152,7 @@ fn main() -> ! { } // Check for new data - if usb_dev.poll(&mut [&mut serial]) { + if usb_dev.poll(&mut [&mut picotool, &mut serial]) { let mut buf = [0u8; 64]; match serial.read(&mut buf) { Err(_e) => { diff --git a/qtpy/src/picotool_reset.rs b/qtpy/src/picotool_reset.rs new file mode 100644 index 00000000..748291b4 --- /dev/null +++ b/qtpy/src/picotool_reset.rs @@ -0,0 +1,93 @@ +//! USB reset interface used by picotool to enter the RP2040 bootloader. +//! +//! Adapted from https://github.com/ithinuel/usbd-picotool-reset (v0.3.0). + +// MIT License +// +// Copyright (c) 2023 rp-rs organization +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +use core::marker::PhantomData; +use usb_device::{class_prelude::*, control, LangID}; + +const RESET_REQUEST_BOOTSEL: u8 = 0x01; + +pub struct PicoToolReset { + interface: InterfaceNumber, + string: StringIndex, + _bus: PhantomData, +} + +impl PicoToolReset { + pub fn new(allocator: &UsbBusAllocator) -> Self { + Self { + interface: allocator.interface(), + string: allocator.string(), + _bus: PhantomData, + } + } + + fn is_reset_interface(&self, request: &control::Request) -> bool { + request.request_type == control::RequestType::Class + && request.recipient == control::Recipient::Interface + && request.index == u16::from(u8::from(self.interface)) + } +} + +impl UsbClass for PicoToolReset { + fn get_configuration_descriptors( + &self, + writer: &mut DescriptorWriter, + ) -> usb_device::Result<()> { + // Pico SDK reset interface: vendor class, subclass 0, protocol 1. + writer.interface_alt(self.interface, 0, 0xff, 0, 1, Some(self.string)) + } + + fn get_string(&self, index: StringIndex, _lang_id: LangID) -> Option<&str> { + (index == self.string).then_some("Reset") + } + + fn control_out(&mut self, transfer: ControlOut) { + let request = transfer.request(); + if !self.is_reset_interface(request) { + return; + } + if request.request != RESET_REQUEST_BOOTSEL { + let _ = transfer.reject(); + return; + } + + // Bit 8 enables the activity LED; bits 9..13 select its GPIO. + let gpio_mask = if request.value & 0x100 != 0 { + 1 << (request.value >> 9) + } else { + 0 + }; + // Forward the bootloader interface flags supplied by picotool. + rp2040_hal::rom_data::reset_to_usb_boot(gpio_mask, u32::from(request.value & 0x7f)); + unreachable!(); + } + + fn control_in(&mut self, transfer: ControlIn) { + if self.is_reset_interface(transfer.request()) { + let _ = transfer.reject(); + } + } +} From 4654fab3a2e499d31e1668dbdb69ead8c23d8837 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 17 Nov 2024 20:54:13 +0800 Subject: [PATCH 2/4] qtpy: Switch back to Framework VID/PID Depends on: https://github.com/raspberrypi/picotool/pull/177 Signed-off-by: Daniel Schaefer --- qtpy/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtpy/src/main.rs b/qtpy/src/main.rs index b4d9ac92..65b73af7 100644 --- a/qtpy/src/main.rs +++ b/qtpy/src/main.rs @@ -107,7 +107,7 @@ fn main() -> ! { // Set up the USB Communications Class Device driver let mut serial = SerialPort::new(&usb_bus); - let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x2e8a, 0x000a)) + let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(FRAMEWORK_VID, COMMUNITY_PID)) .strings(&[StringDescriptors::new(LangID::EN_US) .manufacturer("Adafruit") .product("QT PY - Framework 16 Inputmodule FW")]) From efa72c8675bebbb23f0b1a3566cdf278ae61335c Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Mon, 18 Nov 2024 14:29:24 +0800 Subject: [PATCH 3/4] fwupd: Add initial xml and Makefile Signed-off-by: Daniel Schaefer --- fwupd/.gitignore | 1 + fwupd/Makefile | 3 +++ fwupd/firmware.metainfo.xml | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 fwupd/.gitignore create mode 100644 fwupd/Makefile create mode 100644 fwupd/firmware.metainfo.xml diff --git a/fwupd/.gitignore b/fwupd/.gitignore new file mode 100644 index 00000000..c2498874 --- /dev/null +++ b/fwupd/.gitignore @@ -0,0 +1 @@ +*.cab diff --git a/fwupd/Makefile b/fwupd/Makefile new file mode 100644 index 00000000..dfc882d4 --- /dev/null +++ b/fwupd/Makefile @@ -0,0 +1,3 @@ +qtpy.cab: firmware.metainfo.xml + cp ../target/thumbv6m-none-eabi/release/qtpy.uf2 . + gcab -c -v $@ qtpy.uf2 firmware.metainfo.xml diff --git a/fwupd/firmware.metainfo.xml b/fwupd/firmware.metainfo.xml new file mode 100644 index 00000000..c511a7ca --- /dev/null +++ b/fwupd/firmware.metainfo.xml @@ -0,0 +1,43 @@ + + + work.frame.inputmodule.qtpy + Framework QT Py + Firmware for the Framework QT Py + +

+ Updating the firmware on your Framework QT Py improves performance and + adds new features. +

+
+ + + 5a5b58d0-9cb4-5f8b-a449-e7b7c820b0cb + + https://github.com/FrameworkComputer/inputmodule-rs + CC0-1.0 + proprietary + + X-Device + + + + + + New firmware! + + + + {{ min_ver }} + org.freedesktop.fwupd + + + + com.microsoft.uf2 + + +
From bf9276f17bc1cb7dfc878b412c304b3998b10245 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Mon, 16 Nov 2026 10:28:06 +0800 Subject: [PATCH 4/4] Add picotool reset support to LED matrix --- fl16-inputmodules/src/lib.rs | 1 + {qtpy => fl16-inputmodules}/src/picotool_reset.rs | 0 ledmatrix/src/main.rs | 4 +++- qtpy/src/main.rs | 3 +-- 4 files changed, 5 insertions(+), 3 deletions(-) rename {qtpy => fl16-inputmodules}/src/picotool_reset.rs (100%) diff --git a/fl16-inputmodules/src/lib.rs b/fl16-inputmodules/src/lib.rs index df107681..af25ad6a 100644 --- a/fl16-inputmodules/src/lib.rs +++ b/fl16-inputmodules/src/lib.rs @@ -36,4 +36,5 @@ pub mod minimal_hal; pub mod qtpy_hal; pub mod control; +pub mod picotool_reset; pub mod serialnum; diff --git a/qtpy/src/picotool_reset.rs b/fl16-inputmodules/src/picotool_reset.rs similarity index 100% rename from qtpy/src/picotool_reset.rs rename to fl16-inputmodules/src/picotool_reset.rs diff --git a/ledmatrix/src/main.rs b/ledmatrix/src/main.rs index 9337f053..8297d25b 100644 --- a/ledmatrix/src/main.rs +++ b/ledmatrix/src/main.rs @@ -147,6 +147,7 @@ use fl16_inputmodules::control::*; use fl16_inputmodules::games::{pong, snake}; use fl16_inputmodules::matrix::*; use fl16_inputmodules::patterns::*; +use fl16_inputmodules::picotool_reset::PicoToolReset; use fl16_inputmodules::serialnum::{device_release, get_serialnum}; // FRA - Framwork @@ -201,6 +202,7 @@ fn main() -> ! { // Set up the USB Communications Class Device driver let mut serial = SerialPort::new(&usb_bus); + let mut picotool = PicoToolReset::new(&usb_bus); let serialnum = if let Some(serialnum) = get_serialnum() { serialnum.serialnum @@ -412,7 +414,7 @@ fn main() -> ! { } // Check for new data - if usb_dev.poll(&mut [&mut serial]) { + if usb_dev.poll(&mut [&mut serial, &mut picotool]) { match usb_dev.state() { // Default: Device has just been created or reset // Addressed: Device has received an address for the host diff --git a/qtpy/src/main.rs b/qtpy/src/main.rs index 65b73af7..aff9340c 100644 --- a/qtpy/src/main.rs +++ b/qtpy/src/main.rs @@ -40,8 +40,7 @@ use usb_device::{class_prelude::*, prelude::*}; // USB Communications Class Device support use usbd_serial::{SerialPort, USB_CLASS_CDC}; -mod picotool_reset; -use picotool_reset::PicoToolReset; +use fl16_inputmodules::picotool_reset::PicoToolReset; // Used to demonstrate writing formatted strings // use core::fmt::Write;