Skip to content

Commit 2179aeb

Browse files
committed
most example works with ch32: hid,msc,midi though mouse_ramdisk does not.
1 parent 2e8f1b5 commit 2179aeb

File tree

19 files changed

+487
-633
lines changed

19 files changed

+487
-633
lines changed

examples/CDC/no_serial/no_serial.ino

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ int led = LED_BUILTIN;
2323

2424
void setup()
2525
{
26+
// Manual begin() is required on core without built-in support e.g. mbed rp2040
27+
if (!TinyUSBDevice.isInitialized()) {
28+
TinyUSBDevice.begin(0);
29+
}
30+
2631
// clear configuration will remove all USB interfaces including CDC (Serial)
2732
TinyUSBDevice.clearConfiguration();
2833

@@ -31,8 +36,16 @@ void setup()
3136

3237
void loop()
3338
{
34-
digitalWrite(led, HIGH);
35-
delay(1000);
36-
digitalWrite(led, LOW);
37-
delay(1000);
39+
#ifdef TINYUSB_NEED_POLLING_TASK
40+
// Manual call tud_task since it isn't called by Core's background
41+
TinyUSBDevice.task();
42+
#endif
43+
44+
// toggle LED
45+
static uint32_t ms = 0;
46+
static uint8_t led_state = 0;
47+
if (millis() - ms > 1000) {
48+
ms = millis();
49+
digitalWrite(LED_BUILTIN, 1-led_state);
50+
}
3851
}

examples/Composite/mouse_external_flash/.feather52833.test.skip

Whitespace-only changes.

examples/Composite/mouse_external_flash/.pico_rp2040_tinyusb_host.test.skip

Whitespace-only changes.

examples/Composite/mouse_external_flash/mouse_external_flash.ino

Lines changed: 0 additions & 200 deletions
This file was deleted.

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// 8KB is the smallest size that windows allow to mount
2424
#define DISK_BLOCK_NUM 16
2525
#define DISK_BLOCK_SIZE 512
26+
2627
#include "ramdisk.h"
2728

2829
Adafruit_USBD_MSC usb_msc;
@@ -41,37 +42,41 @@ uint8_t const desc_hid_report[] = {
4142
Adafruit_USBD_HID usb_hid;
4243

4344
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
44-
const int pin = 4; // Left Button
45-
bool activeState = true;
45+
const int pin = 4; // Left Button
46+
bool activeState = true;
4647

4748
#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
48-
const int pin = BUTTON_DOWN;
49-
bool activeState = true;
49+
const int pin = BUTTON_DOWN;
50+
bool activeState = true;
5051

5152
#elif defined PIN_BUTTON1
52-
const int pin = PIN_BUTTON1;
53-
bool activeState = false;
53+
const int pin = PIN_BUTTON1;
54+
bool activeState = false;
5455

5556
#elif defined(ARDUINO_ARCH_ESP32)
56-
const int pin = 0;
57-
bool activeState = false;
57+
const int pin = 0;
58+
bool activeState = false;
59+
60+
#elif defined(ARDUINO_ARCH_RP2040)
61+
const int pin = D0;
62+
bool activeState = false;
5863

5964
#else
60-
const int pin = 12;
61-
bool activeState = false;
65+
const int pin = A0;
66+
bool activeState = false;
6267
#endif
6368

69+
6470
// the setup function runs once when you press reset or power the board
65-
void setup()
66-
{
67-
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
68-
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
69-
TinyUSB_Device_Init(0);
70-
#endif
71+
void setup() {
72+
// Manual begin() is required on core without built-in support e.g. mbed rp2040
73+
if (!TinyUSBDevice.isInitialized()) {
74+
TinyUSBDevice.begin(0);
75+
}
7176

7277
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
7378
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
74-
79+
7580
// Set disk size
7681
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
7782

@@ -80,7 +85,7 @@ void setup()
8085

8186
// Set Lun ready (RAM disk is always ready)
8287
usb_msc.setUnitReady(true);
83-
88+
8489
usb_msc.begin();
8590

8691
// Set up button
@@ -93,32 +98,23 @@ void setup()
9398
usb_hid.begin();
9499

95100
Serial.begin(115200);
96-
while( !TinyUSBDevice.mounted() ) delay(1); // wait for native usb
97-
98101
Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example");
99102
}
100103

101-
void loop()
102-
{
103-
// poll gpio once each 10 ms
104-
delay(10);
105-
104+
void process_hid() {
106105
// button is active low
107106
uint32_t const btn = (digitalRead(pin) == activeState);
108107

109108
// Remote wakeup
110-
if ( TinyUSBDevice.suspended() && btn )
111-
{
109+
if (TinyUSBDevice.suspended() && btn) {
112110
// Wake up host if we are in suspend mode
113111
// and REMOTE_WAKEUP feature is enabled by host
114112
tud_remote_wakeup();
115113
}
116114

117115
/*------------- Mouse -------------*/
118-
if ( usb_hid.ready() )
119-
{
120-
if ( btn )
121-
{
116+
if (usb_hid.ready()) {
117+
if (btn) {
122118
int8_t const delta = 5;
123119
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
124120

@@ -128,11 +124,29 @@ void loop()
128124
}
129125
}
130126

127+
void loop() {
128+
#ifdef TINYUSB_NEED_POLLING_TASK
129+
// Manual call tud_task since it isn't called by Core's background
130+
TinyUSBDevice.task();
131+
#endif
132+
133+
// not enumerated()/mounted() yet: nothing to do
134+
if (!TinyUSBDevice.mounted()) {
135+
return;
136+
}
137+
138+
// poll gpio once each 10 ms
139+
static uint32_t ms = 0;
140+
if (millis() - ms > 10) {
141+
ms = millis();
142+
process_hid();
143+
}
144+
}
145+
131146
// Callback invoked when received READ10 command.
132147
// Copy disk's data to buffer (up to bufsize) and
133148
// return number of copied bytes (must be multiple of block size)
134-
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
135-
{
149+
int32_t msc_read_cb(uint32_t lba, void* buffer, uint32_t bufsize) {
136150
uint8_t const* addr = msc_disk[lba];
137151
memcpy(buffer, addr, bufsize);
138152

@@ -142,8 +156,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
142156
// Callback invoked when received WRITE10 command.
143157
// Process data in buffer to disk's storage and
144158
// return number of written bytes (must be multiple of block size)
145-
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
146-
{
159+
int32_t msc_write_cb(uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
147160
uint8_t* addr = msc_disk[lba];
148161
memcpy(addr, buffer, bufsize);
149162

@@ -152,7 +165,6 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
152165

153166
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
154167
// used to flush any pending cache.
155-
void msc_flush_cb (void)
156-
{
168+
void msc_flush_cb(void) {
157169
// nothing to do
158170
}

0 commit comments

Comments
 (0)