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/fl16-inputmodules/src/picotool_reset.rs b/fl16-inputmodules/src/picotool_reset.rs new file mode 100644 index 00000000..748291b4 --- /dev/null +++ b/fl16-inputmodules/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(); + } + } +} 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 + + +
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 5b2e79d0..aff9340c 100644 --- a/qtpy/src/main.rs +++ b/qtpy/src/main.rs @@ -40,6 +40,8 @@ use usb_device::{class_prelude::*, prelude::*}; // USB Communications Class Device support use usbd_serial::{SerialPort, USB_CLASS_CDC}; +use fl16_inputmodules::picotool_reset::PicoToolReset; + // Used to demonstrate writing formatted strings // use core::fmt::Write; // use heapless::String; @@ -99,6 +101,8 @@ 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); @@ -147,7 +151,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) => {