|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 3 | + |
| 4 | +/* This sketch demonstrates USB Mass Storage and HID mouse (and CDC) |
| 5 | + * - Enumerated as disk using on-board external flash |
| 6 | + * - Press button pin will move mouse toward bottom right of monitor |
| 7 | + */ |
| 8 | + |
| 9 | +#include "SPI.h" |
| 10 | +#include "SdFat.h" |
| 11 | +#include "Adafruit_SPIFlash.h" |
| 12 | +#include "Adafruit_TinyUSB.h" |
| 13 | + |
| 14 | +#if defined(__SAMD51__) || defined(NRF52840_XXAA) |
| 15 | + Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS, PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3); |
| 16 | +#else |
| 17 | + #if (SPI_INTERFACES_COUNT == 1) |
| 18 | + Adafruit_FlashTransport_SPI flashTransport(SS, &SPI); |
| 19 | + #else |
| 20 | + Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1); |
| 21 | + #endif |
| 22 | +#endif |
| 23 | + |
| 24 | +Adafruit_SPIFlash flash(&flashTransport); |
| 25 | + |
| 26 | +Adafruit_USBD_MSC usb_msc; |
| 27 | + |
| 28 | +// HID report descriptor using TinyUSB's template |
| 29 | +// Single Report (no ID) descriptor |
| 30 | +uint8_t const desc_hid_report[] = |
| 31 | +{ |
| 32 | + TUD_HID_REPORT_DESC_MOUSE() |
| 33 | +}; |
| 34 | + |
| 35 | +Adafruit_USBD_HID usb_hid; |
| 36 | + |
| 37 | +const int pin = 7; |
| 38 | + |
| 39 | +// the setup function runs once when you press reset or power the board |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + flash.begin(); |
| 43 | + |
| 44 | + pinMode(LED_BUILTIN, OUTPUT); |
| 45 | + |
| 46 | + // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 47 | + usb_msc.setID("Adafruit", "External Flash", "1.0"); |
| 48 | + |
| 49 | + // Set callback |
| 50 | + usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); |
| 51 | + |
| 52 | + // Set disk size, block size should be 512 regardless of spi flash page size |
| 53 | + usb_msc.setCapacity(flash.pageSize()*flash.numPages()/512, 512); |
| 54 | + |
| 55 | + // MSC is ready for read/write |
| 56 | + usb_msc.setUnitReady(true); |
| 57 | + |
| 58 | + usb_msc.begin(); |
| 59 | + |
| 60 | + |
| 61 | + // Set up button |
| 62 | + pinMode(pin, INPUT_PULLUP); |
| 63 | + |
| 64 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 65 | + usb_hid.begin(); |
| 66 | + |
| 67 | + Serial.begin(115200); |
| 68 | + while ( !Serial ) delay(10); // wait for native usb |
| 69 | + |
| 70 | + Serial.println("Adafruit TinyUSB Mouse + Mass Storage (external flash) example"); |
| 71 | +} |
| 72 | + |
| 73 | +void loop() |
| 74 | +{ |
| 75 | + // poll gpio once each 10 ms |
| 76 | + delay(10); |
| 77 | + |
| 78 | + // button is active low |
| 79 | + uint32_t const btn = 1 - digitalRead(pin); |
| 80 | + |
| 81 | + // Remote wakeup |
| 82 | + if ( tud_suspended() && btn ) |
| 83 | + { |
| 84 | + // Wake up host if we are in suspend mode |
| 85 | + // and REMOTE_WAKEUP feature is enabled by host |
| 86 | + tud_remote_wakeup(); |
| 87 | + } |
| 88 | + |
| 89 | + /*------------- Mouse -------------*/ |
| 90 | + if ( usb_hid.ready() ) |
| 91 | + { |
| 92 | + if ( btn ) |
| 93 | + { |
| 94 | + int8_t const delta = 5; |
| 95 | + usb_hid.mouseMove(0, delta, delta); // no ID: right + down |
| 96 | + |
| 97 | + // delay a bit before attempt to send keyboard report |
| 98 | + delay(10); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Callback invoked when received READ10 command. |
| 104 | +// Copy disk's data to buffer (up to bufsize) and |
| 105 | +// return number of copied bytes (must be multiple of block size) |
| 106 | +int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) |
| 107 | +{ |
| 108 | + // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks |
| 109 | + // already include 4K sector caching internally. We don't need to cache it, yahhhh!! |
| 110 | + flash.readBlocks(lba, (uint8_t*) buffer, bufsize/512); |
| 111 | + return bufsize; |
| 112 | +} |
| 113 | + |
| 114 | +// Callback invoked when received WRITE10 command. |
| 115 | +// Process data in buffer to disk's storage and |
| 116 | +// return number of written bytes (must be multiple of block size) |
| 117 | +int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) |
| 118 | +{ |
| 119 | + // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks |
| 120 | + // already include 4K sector caching internally. We don't need to cache it, yahhhh!! |
| 121 | + flash.writeBlocks(lba, buffer, bufsize/512); |
| 122 | + return bufsize; |
| 123 | +} |
| 124 | + |
| 125 | +// Callback invoked when WRITE10 command is completed (status received and accepted by host). |
| 126 | +// used to flush any pending cache. |
| 127 | +void msc_flush_cb (void) |
| 128 | +{ |
| 129 | + flash.syncBlocks(); |
| 130 | +} |
0 commit comments