diff --git a/Dockerfile b/Dockerfile.pico1 similarity index 65% rename from Dockerfile rename to Dockerfile.pico1 index e70464f..1970fc2 100644 --- a/Dockerfile +++ b/Dockerfile.pico1 @@ -1,9 +1,9 @@ # # To Build: -# docker build -t usbvalve/arduino-cli . +# docker build -t usbvalve-pico1/arduino-cli -f Dockerfile.pico1 . # # To Run: -# docker run --rm --name usbvalve -v $PWD:/mnt usbvalve/arduino-cli /mnt/USBvalve +# docker run --rm --name usbvalve -v $PWD:/mnt usbvalve-pico1/arduino-cli /mnt/USBvalve # FROM ubuntu:22.04 @@ -23,17 +23,17 @@ RUN cd /app \ && ./install.sh \ && export PATH=$PATH:/app/arduino-cli/bin \ && arduino-cli --additional-urls https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json core search 2040 \ - && arduino-cli --additional-urls https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json core install rp2040:rp2040 \ - && arduino-cli lib install "Adafruit TinyUSB Library@3.4.0" \ - && arduino-cli lib install "Adafruit SSD1306@2.5.12" \ + && arduino-cli --additional-urls https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json core install rp2040:rp2040@4.4.0 \ + && arduino-cli lib install "Adafruit TinyUSB Library@3.4.2" \ + && arduino-cli lib install "Adafruit SSD1306@2.5.13" \ && arduino-cli lib install "Pico PIO USB@0.6.1" \ && arduino-cli lib install "XxHash_arduino@2.1.0" \ - && arduino-cli lib install "GFX Library for Arduino@1.4.9" + && arduino-cli lib install "GFX Library for Arduino@1.5.0" # Compilation setup RUN echo "#!/bin/bash" > /app/entrypoint.sh \ && echo "export PATH=\$PATH:/app/arduino-cli/bin" >> /app/entrypoint.sh \ - && echo "arduino-cli compile --fqbn rp2040:rp2040:rpipico --board-options \"usbstack=tinyusb\" --board-options \"freq=120\" --output-dir \"/mnt/USBvalve_out\" \"\$1\"" >> /app/entrypoint.sh \ + && echo "arduino-cli compile --fqbn rp2040:rp2040:rpipico --build-property \"build.extra_flags=-DCFG_TUD_CDC=1\" --board-options \"usbstack=tinyusb\" --board-options \"freq=120\" --output-dir \"/mnt/USBvalve_out\" \"\$1\"" >> /app/entrypoint.sh \ && chmod +x /app/entrypoint.sh ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/Dockerfile.pico2 b/Dockerfile.pico2 new file mode 100644 index 0000000..4b570c6 --- /dev/null +++ b/Dockerfile.pico2 @@ -0,0 +1,39 @@ +# +# To Build: +# docker build -t usbvalve-pico2/arduino-cli -f Dockerfile.pico2 . +# +# To Run: +# docker run --rm --name usbvalve -v $PWD:/mnt usbvalve-pico2/arduino-cli /mnt/USBvalve +# + +FROM ubuntu:22.04 +WORKDIR /app + +# OS setup +RUN apt-get update -y \ + && apt-get install -y git wget python3 \ + && apt-get autoremove -y \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +# arduino-cli setup +RUN cd /app \ + && git clone --recursive https://github.com/arduino/arduino-cli.git \ + && cd arduino-cli \ + && ./install.sh \ + && export PATH=$PATH:/app/arduino-cli/bin \ + && arduino-cli --additional-urls https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json core search 2040 \ + && arduino-cli --additional-urls https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json core install rp2040:rp2040@4.4.0 \ + && arduino-cli lib install "Adafruit TinyUSB Library@3.4.2" \ + && arduino-cli lib install "Adafruit SSD1306@2.5.13" \ + && arduino-cli lib install "Pico PIO USB@0.6.1" \ + && arduino-cli lib install "XxHash_arduino@2.1.0" \ + && arduino-cli lib install "GFX Library for Arduino@1.5.0" + +# Compilation setup +RUN echo "#!/bin/bash" > /app/entrypoint.sh \ + && echo "export PATH=\$PATH:/app/arduino-cli/bin" >> /app/entrypoint.sh \ + && echo "arduino-cli compile --fqbn rp2040:rp2040:rpipico2 --build-property \"build.extra_flags=-DCFG_TUD_CDC=1\" --board-options \"usbstack=tinyusb\" --board-options \"freq=120\" --output-dir \"/mnt/USBvalve_out\" \"\$1\"" >> /app/entrypoint.sh \ + && chmod +x /app/entrypoint.sh + +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/README.md b/README.md index 5bb03e4..895ee1a 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ This is also fully compatible with the [Waveshare RP2040-LCD-1.28](https://www.w If you want to build your own, you need: -- A Raspberry Pi Pico (or another RP2040 based board, like Arduino Nano RP2040) +- A Raspberry Pi Pico 1 or 2 (or another RP2040 based board, like Arduino Nano RP2040) - an I2C OLED screen 128x64 or 128x32 (SSD1306) - (optional) a **USBvalve** PCB or a breadboard - (optional) a 3D printed spacer to isolate the screen from the board (https://www.thingiverse.com/thing:4748043), but you can use a piece of electrical tape instead @@ -195,21 +195,23 @@ I grouped most of the variables you may want to modify in this section ([see Doc Obviously you can also build your own firmware. To build the *standard* one I used: -- Arduino IDE `2.3.3` -- `Adafruit TinyUSB Library` version `3.4.0`, `Pico-PIO-USB` version `0.6.1`, Board `Raspberry Pi RP2040 (4.1.1)` setting Tools=>CPU Speed at `133MHz` and Tools=>USB Stack to `Adafruit TinyUSB` -- `Adafruit_SSD1306` OLED library version `2.5.12` +- Arduino IDE `2.3.4` +- `Adafruit TinyUSB Library` version `3.4.2`, `Pico-PIO-USB` version `0.6.1`, Board `Raspberry Pi RP2040 (4.4.0)` setting Tools=>CPU Speed at `133MHz` and Tools=>USB Stack to `Adafruit TinyUSB` +- `Adafruit_SSD1306` OLED library version `2.5.13` -Remember to add `https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json` in the `Additional Board Manager URLs` to install the proper board. +Remember to add `https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json` in the `Additional Board Manager URLs` to install the proper board. Also, starting from `TinyUSB` version `3.4.2` is necessary to force the following macro setting `DCFG_TUD_CDC=1`. I strongly suggesto you to use the provided *Dockerfiles* (see below). If you want to re-create a new fake filesystem, you may want to have a look to the `utils` folder, where I placed some utilities to build a new one. #### Dockerfile -If you want to build your own firmware, after you customized it, I provide a `Dockerfile` which builds a complete **Arduino** environment and compile the firmware. Enter the following commands in the main `USBvalve` folder: +If you want to build your own firmware, after you customized it, I provide a `Dockerfile` which builds a complete **Arduino** environment and compile the firmware. I added them for both `Pico` version 1 and 2. + +Enter the following commands in the main `USBvalve` folder: ``` -docker build -t usbvalve/arduino-cli . -docker run --rm --name usbvalve -v $PWD:/mnt usbvalve/arduino-cli /mnt/USBvalve +docker build -t usbvalve-pico1/arduino-cli -f Dockerfile.pico1 . +docker run --rm --name usbvalve -v $PWD:/mnt usbvalve-pico1/arduino-cli /mnt/USBvalve ``` The firmware will be placed with extension `uf2` in folder `USBvalve_out`. diff --git a/USBvalve/USBvalve.ino b/USBvalve/USBvalve.ino index 5c1ee35..0285ccb 100644 --- a/USBvalve/USBvalve.ino +++ b/USBvalve/USBvalve.ino @@ -21,7 +21,7 @@ // Uncomment the following to compile for the RP2040 based TFT round display // https://www.raspberrypi.com/news/how-to-build-your-own-raspberry-pi-watch/ -//#define PIWATCH +#define PIWATCH #include #include "Adafruit_TinyUSB.h" @@ -116,7 +116,7 @@ bool activeState = false; // // USBvalve globals // -#define VERSION "USBvalve - 0.19.1" +#define VERSION "USBvalve - 0.20.0" boolean readme = false; boolean autorun = false; boolean written = false; @@ -126,6 +126,8 @@ boolean deleted_reported = false; boolean hid_sent = false; boolean hid_reported = false; +static spin_lock_t *lock; + // // Anti-Detection settings. // @@ -159,6 +161,9 @@ uint valid_hash = 2362816530; // Core 0 Setup: will be used for the USB mass device functions void setup() { + // Initialize the spinlock + lock = spin_lock_instance(0); + // Change all the USB Pico settings TinyUSBDevice.setID(USB_VENDORID, USB_PRODUCTID); TinyUSBDevice.setProductDescriptor(USB_DESCRIPTOR); @@ -537,6 +542,9 @@ static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; // Invoked when device with hid interface is mounted void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { + + uint32_t lock_num = spin_lock_blocking(lock); + uint16_t vid, pid; const char* protocol_str[] = { "None", "Keyboard", "Mouse" }; @@ -557,20 +565,26 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re if (!tuh_hid_receive_report(dev_addr, instance)) { SerialTinyUSB.printf("Error: cannot request to receive report\r\n"); } + + spin_unlock(lock, lock_num); } // Invoked when device with hid interface is un-mounted void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { + uint32_t lock_num = spin_lock_blocking(lock); SerialTinyUSB.printf("HID device address = %d, instance = %d unmounted\r\n", dev_addr, instance); // Reset HID sent flag hid_sent = false; hid_reported = false; + spin_unlock(lock, lock_num); } // Invoked when received report from device void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { + uint32_t lock_num = spin_lock_blocking(lock); + static bool kbd_printed = false; static bool mouse_printed = false; @@ -608,6 +622,8 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons if (!tuh_hid_receive_report(dev_addr, instance)) { SerialTinyUSB.println("Error: cannot request to receive report"); } + + spin_unlock(lock, lock_num); } static inline bool find_key_in_report(hid_keyboard_report_t const* report, uint8_t keycode) { @@ -727,24 +743,32 @@ void cursor_movement(int8_t x, int8_t y, int8_t wheel) { // Invoked when a device with MassStorage interface is mounted void tuh_msc_mount_cb(uint8_t dev_addr) { + uint32_t lock_num = spin_lock_blocking(lock); printout("\n[++] Mass Device"); SerialTinyUSB.printf("Mass Device attached, address = %d\r\n", dev_addr); + spin_unlock(lock, lock_num); } // Invoked when a device with MassStorage interface is unmounted void tuh_msc_umount_cb(uint8_t dev_addr) { + uint32_t lock_num = spin_lock_blocking(lock); SerialTinyUSB.printf("Mass Device unmounted, address = %d\r\n", dev_addr); + spin_unlock(lock, lock_num); } // Invoked when a device with CDC (Communication Device Class) interface is mounted void tuh_cdc_mount_cb(uint8_t idx) { + uint32_t lock_num = spin_lock_blocking(lock); printout("\n[++] CDC Device"); SerialTinyUSB.printf("CDC Device attached, idx = %d\r\n", idx); + spin_unlock(lock, lock_num); } // Invoked when a device with CDC (Communication Device Class) interface is unmounted void tuh_cdc_umount_cb(uint8_t idx) { + uint32_t lock_num = spin_lock_blocking(lock); SerialTinyUSB.printf("CDC Device unmounted, idx = %d\r\n", idx); + spin_unlock(lock, lock_num); } // END of OTHER Host devices detector section diff --git a/firmware/USBvalve-0.19.1-32.uf2 b/firmware/USBvalve-0.20.0-32-pico1.uf2 similarity index 70% rename from firmware/USBvalve-0.19.1-32.uf2 rename to firmware/USBvalve-0.20.0-32-pico1.uf2 index 5921d3a..96023b5 100644 Binary files a/firmware/USBvalve-0.19.1-32.uf2 and b/firmware/USBvalve-0.20.0-32-pico1.uf2 differ diff --git a/firmware/USBvalve-0.20.0-32-pico2.uf2 b/firmware/USBvalve-0.20.0-32-pico2.uf2 new file mode 100644 index 0000000..4f49d93 Binary files /dev/null and b/firmware/USBvalve-0.20.0-32-pico2.uf2 differ diff --git a/firmware/USBvalve-0.19.1-64.uf2 b/firmware/USBvalve-0.20.0-64-pico1.uf2 similarity index 70% rename from firmware/USBvalve-0.19.1-64.uf2 rename to firmware/USBvalve-0.20.0-64-pico1.uf2 index 1f88773..ddb3995 100644 Binary files a/firmware/USBvalve-0.19.1-64.uf2 and b/firmware/USBvalve-0.20.0-64-pico1.uf2 differ diff --git a/firmware/USBvalve-0.20.0-64-pico2.uf2 b/firmware/USBvalve-0.20.0-64-pico2.uf2 new file mode 100644 index 0000000..d1709c5 Binary files /dev/null and b/firmware/USBvalve-0.20.0-64-pico2.uf2 differ diff --git a/firmware/USBvalve-0.19.1-PIWATCH.uf2 b/firmware/USBvalve-0.20.0-PIWATCH.uf2 similarity index 68% rename from firmware/USBvalve-0.19.1-PIWATCH.uf2 rename to firmware/USBvalve-0.20.0-PIWATCH.uf2 index 97722aa..5d6c5fe 100644 Binary files a/firmware/USBvalve-0.19.1-PIWATCH.uf2 and b/firmware/USBvalve-0.20.0-PIWATCH.uf2 differ