|
| 1 | +// This sketch program rp2040 by copying UF2 file from SDCard to |
| 2 | +// rp2040 bootrom |
| 3 | +// Hardware wiring: |
| 4 | +// - Brain's Target GPIO28 to RP2 bootsel |
| 5 | +// - Brain's Target Reset to RP2 Reset |
| 6 | +// - Brain's USB host to RP2040 USB interface |
| 7 | + |
| 8 | +// required for Host MSC block device |
| 9 | +#include "SdFat_Adafruit_Fork.h" |
| 10 | + |
| 11 | +// required for USB host |
| 12 | +#include "pio_usb.h" |
| 13 | +#include "Adafruit_TinyUSB.h" |
| 14 | + |
| 15 | +#include "Adafruit_TestBed_Brains.h" |
| 16 | + |
| 17 | +// firmware.h is converted using tools/file2carray.py e.g |
| 18 | +// python tools/file2carray.py cdc_msc.uf2 |
| 19 | +// above command will generate cdc_msc.uf2.h with bindata, bindata_len |
| 20 | +#include "metro_rp2350_cdc_msc.uf2.h" |
| 21 | + |
| 22 | +// RP2040 Boot VID/PID |
| 23 | +#define BOOT_VID 0x2e8a |
| 24 | +#define BOOT_PID_RP2040 0x0003 |
| 25 | +#define BOOT_PID_RP2350 0x000f |
| 26 | + |
| 27 | +// If USB filesystem is mounted |
| 28 | +volatile bool is_usbfs_mounted = false; |
| 29 | + |
| 30 | +//--------------------------------------------------------------------+ |
| 31 | +// Setup and Loop on Core0 |
| 32 | +//--------------------------------------------------------------------+ |
| 33 | +void print_speed(size_t count, uint32_t ms) { |
| 34 | + Brain.LCD_printf(0, "%.01fKB %.01fs", count/1000.0F, ms / 1000.0F); |
| 35 | + |
| 36 | + Serial.printf("Completed %u bytes in %.02f seconds.\r\n", count, ms / 1000.0F); |
| 37 | + Serial.printf("Speed : %.02f KB/s\r\n", (count / 1000.0F) / (ms / 1000.0F)); |
| 38 | +} |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Serial.begin(115200); |
| 42 | + while (!Serial) delay(10); |
| 43 | + Serial.println("Program RP2 by copy UF2 from Internal Flash to Bootloader!"); |
| 44 | + |
| 45 | + // sync: wait for Brain.begin() called in core1 before accessing SD or other peripherals |
| 46 | + while (!Brain.inited()) delay(10); |
| 47 | + |
| 48 | + // wait for USB filesystem is mounted. USB host bit-banging and task is |
| 49 | + // processed on core1 |
| 50 | + while (!is_usbfs_mounted) delay(10); |
| 51 | + |
| 52 | + // Copy UF2 file |
| 53 | + Brain.LCD_printf(0, "Copying firmware"); |
| 54 | + Serial.println("Copying UF2 from Flash to USBHFS"); |
| 55 | + |
| 56 | + uint32_t ms = millis(); |
| 57 | + size_t copied_bytes = Brain.rp2_programUF2(bindata, bindata_len); |
| 58 | + print_speed(copied_bytes, millis() - ms); |
| 59 | + |
| 60 | + // wait for rp2040 boot rom to reset |
| 61 | + // while (is_usbfs_mounted) delay(10); |
| 62 | +} |
| 63 | + |
| 64 | +void loop() { |
| 65 | + Serial.flush(); |
| 66 | +} |
| 67 | + |
| 68 | +//--------------------------------------------------------------------+ |
| 69 | +// Setup and Loop on Core1 |
| 70 | +//--------------------------------------------------------------------+ |
| 71 | + |
| 72 | +// call usbh_begin() here to make pio usb background task run on core1 |
| 73 | +// NOTE: Brain.begin() should be called here as well to prevent race condition |
| 74 | +void setup1() { |
| 75 | + Brain.begin(); |
| 76 | + Brain.usbh_begin(); |
| 77 | + |
| 78 | + Brain.LCD_printf(1, "No USB Device"); |
| 79 | + |
| 80 | + // reset rp2040 target into Boot Rom, default bootsel = 28, reset duration = 10 ms |
| 81 | + Brain.rp2_targetResetBootRom(); |
| 82 | +} |
| 83 | + |
| 84 | +// core1's loop: process usb host task on core1 |
| 85 | +void loop1() { |
| 86 | + Brain.USBHost.task(); |
| 87 | +} |
| 88 | + |
| 89 | +//--------------------------------------------------------------------+ |
| 90 | +// TinyUSB Host callbacks |
| 91 | +// Note: running in the same core where Brain.USBHost.task() is called |
| 92 | +//--------------------------------------------------------------------+ |
| 93 | +bool is_rp2_bootloader(uint16_t vid, uint16_t pid) { |
| 94 | + return (vid == BOOT_VID && (pid == BOOT_PID_RP2040 || pid == BOOT_PID_RP2350)); |
| 95 | +} |
| 96 | + |
| 97 | +extern "C" { |
| 98 | + |
| 99 | +// Invoked when device is mounted (configured) |
| 100 | +void tuh_mount_cb(uint8_t dev_addr) { |
| 101 | + uint16_t vid, pid; |
| 102 | + tuh_vid_pid_get(dev_addr, &vid, &pid); |
| 103 | + |
| 104 | + if (!is_rp2_bootloader(vid, pid)) { |
| 105 | + Brain.LCD_printf(1, "UnkDev %04x:%04x", vid, pid); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Invoked when device is unmounted (bus reset/unplugged) |
| 110 | +void tuh_umount_cb(uint8_t dev_addr) { |
| 111 | + (void)dev_addr; |
| 112 | + Brain.LCD_printf(1, "No USB Device"); |
| 113 | +} |
| 114 | + |
| 115 | +// Invoked when a device with MassStorage interface is mounted |
| 116 | +void tuh_msc_mount_cb(uint8_t dev_addr) { |
| 117 | + uint16_t vid, pid; |
| 118 | + tuh_vid_pid_get(dev_addr, &vid, &pid); |
| 119 | + |
| 120 | + if (is_rp2_bootloader(vid, pid)) { |
| 121 | + is_usbfs_mounted = Brain.usbh_mountFS(dev_addr); |
| 122 | + if (is_usbfs_mounted) { |
| 123 | + uint16_t rp2variant = (pid == BOOT_PID_RP2040 ? 2040 : 2350); |
| 124 | + Brain.LCD_printf(1, "RP%u Bootldr", rp2variant); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Invoked when a device with MassStorage interface is unmounted |
| 130 | +void tuh_msc_umount_cb(uint8_t dev_addr) { |
| 131 | + is_usbfs_mounted = false; |
| 132 | + Brain.usbh_umountFS(dev_addr); |
| 133 | +} |
| 134 | + |
| 135 | +} |
0 commit comments