Skip to content

Commit e3ad125

Browse files
committed
msc_ramdisk run ok with mbed rp2040 as proof of concept
1 parent 6a3f7c8 commit e3ad125

File tree

3 files changed

+73
-25
lines changed

3 files changed

+73
-25
lines changed

examples/MassStorage/msc_ramdisk/msc_ramdisk.ino

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ Adafruit_USBD_MSC usb_msc;
2121
// the setup function runs once when you press reset or power the board
2222
void setup()
2323
{
24+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
25+
// Manual begin() is required on unsupported BSP such as mbed rp2040
26+
// VID/PID, Manufacturer & Product string also need to be set as well
27+
TinyUSBDevice.begin(0);
28+
#endif
29+
2430
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
2531
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
26-
32+
2733
// Set disk size
2834
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
2935

@@ -32,7 +38,7 @@ void setup()
3238

3339
// Set Lun ready (RAM disk is always ready)
3440
usb_msc.setUnitReady(true);
35-
41+
3642
usb_msc.begin();
3743

3844
Serial.begin(115200);
@@ -47,8 +53,8 @@ void loop()
4753
}
4854

4955
// Callback invoked when received READ10 command.
50-
// Copy disk's data to buffer (up to bufsize) and
51-
// return number of copied bytes (must be multiple of block size)
56+
// Copy disk's data to buffer (up to bufsize) and
57+
// return number of copied bytes (must be multiple of block size)
5258
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
5359
{
5460
uint8_t const* addr = msc_disk[lba];
@@ -58,7 +64,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
5864
}
5965

6066
// Callback invoked when received WRITE10 command.
61-
// Process data in buffer to disk's storage and
67+
// Process data in buffer to disk's storage and
6268
// return number of written bytes (must be multiple of block size)
6369
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
6470
{

src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,72 @@
2727
#if defined ARDUINO_ARCH_RP2040 && TUSB_OPT_DEVICE_ENABLED
2828

2929
#include "Arduino.h"
30+
31+
// mbed old pico-sdk need to wrap with cpp
32+
extern "C" {
3033
#include "hardware/irq.h"
3134
#include "pico/bootrom.h"
3235
#include "pico/mutex.h"
3336
#include "pico/time.h"
34-
#include "pico/unique_id.h"
37+
#include "hardware/flash.h"
38+
}
3539

3640
#include "arduino/Adafruit_TinyUSB_API.h"
3741
#include "tusb.h"
3842

43+
// USB processing will be a periodic timer task
44+
#define USB_TASK_INTERVAL 1000
45+
#define USB_TASK_IRQ 31
46+
3947
//--------------------------------------------------------------------+
4048
// Forward USB interrupt events to TinyUSB IRQ Handler
41-
// rp2040 implementation will install approriate handler when initializing
49+
// rp2040 implementation will install appropriate handler when initializing
4250
// tinyusb. There is no need to forward IRQ from application
4351
//--------------------------------------------------------------------+
4452

4553
//--------------------------------------------------------------------+
46-
// Porting API
54+
// Earle Philhower and mbed specific
4755
//--------------------------------------------------------------------+
4856

49-
// USB processing will be a periodic timer task
50-
#define USB_TASK_INTERVAL 1000
51-
#define USB_TASK_IRQ 31
57+
// mbed use old pico-sdk does not have unique_id
58+
#ifdef ARDUINO_ARCH_MBED
59+
60+
#include "mbed.h"
61+
static mbed::Ticker _usb_ticker;
62+
63+
#define get_unique_id(_serial) flash_get_unique_id(_serial)
64+
65+
static void ticker_task(void)
66+
{
67+
irq_set_pending(USB_TASK_IRQ);
68+
}
69+
70+
static void setup_periodic_usb_hanlder(uint64_t us)
71+
{
72+
_usb_ticker.attach(ticker_task, (std::chrono::microseconds) us);
73+
}
74+
75+
#else
76+
77+
#include "pico/unique_id.h"
78+
79+
#define get_unique_id(_serial) pico_get_unique_board_id((pico_unique_board_id_t *)_serial);
80+
81+
static int64_t timer_task(__unused alarm_id_t id, __unused void *user_data) {
82+
irq_set_pending(USB_TASK_IRQ);
83+
return USB_TASK_INTERVAL;
84+
}
85+
86+
static void setup_periodic_usb_hanlder(uint64_t us)
87+
{
88+
add_alarm_in_us(us, timer_task, NULL, true);
89+
}
90+
91+
#endif // mbed
92+
93+
//--------------------------------------------------------------------+
94+
// Porting API
95+
//--------------------------------------------------------------------+
5296

5397
// Big, global USB mutex, shared with all USB devices to make sure we don't
5498
// have multiple cores updating the TUSB state in parallel
@@ -64,21 +108,23 @@ static void usb_irq() {
64108
}
65109
}
66110

67-
static int64_t timer_task(__unused alarm_id_t id, __unused void *user_data) {
68-
irq_set_pending(USB_TASK_IRQ);
69-
return USB_TASK_INTERVAL;
70-
}
71-
72111
void TinyUSB_Port_InitDevice(uint8_t rhport) {
73112
(void)rhport;
74113

75114
mutex_init(&__usb_mutex);
115+
116+
irq_set_enabled(USBCTRL_IRQ, false);
117+
irq_handler_t current_handler = irq_get_exclusive_handler(USBCTRL_IRQ);
118+
if(current_handler) {
119+
irq_remove_handler(USBCTRL_IRQ, current_handler);
120+
}
121+
76122
tusb_init();
77123

124+
// soft irq for periodically task runner
78125
irq_set_exclusive_handler(USB_TASK_IRQ, usb_irq);
79126
irq_set_enabled(USB_TASK_IRQ, true);
80-
81-
add_alarm_in_us(USB_TASK_INTERVAL, timer_task, NULL, true);
127+
setup_periodic_usb_hanlder(USB_TASK_INTERVAL);
82128
}
83129

84130
void TinyUSB_Port_EnterDFU(void) {
@@ -88,8 +134,8 @@ void TinyUSB_Port_EnterDFU(void) {
88134
}
89135

90136
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) {
91-
pico_get_unique_board_id((pico_unique_board_id_t *)serial_id);
92-
return PICO_UNIQUE_BOARD_ID_SIZE_BYTES;
137+
get_unique_id(serial_id);
138+
return FLASH_UNIQUE_ID_SIZE_BYTES;
93139
}
94140

95141
//--------------------------------------------------------------------+
@@ -109,4 +155,4 @@ void TinyUSB_Device_Task(void) {
109155
}
110156
}
111157

112-
#endif // USE_TINYUSB
158+
#endif

src/arduino/ports/rp2040/tusb_config_rp2040.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ extern "C" {
3232
//--------------------------------------------------------------------
3333
// COMMON CONFIGURATION
3434
//--------------------------------------------------------------------
35-
#ifdef USE_TINYUSB
3635
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
37-
#else
38-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_NONE
39-
#endif
4036

4137
#ifndef CFG_TUSB_MCU
4238
#define CFG_TUSB_MCU OPT_MCU_RP2040

0 commit comments

Comments
 (0)