Skip to content

Commit 2913c9c

Browse files
authored
Merge pull request #391 from adafruit/update-ramdisk-with-start-stop
Update ramdisk with start stop
2 parents e39183c + dac1eee commit 2913c9c

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

examples/MassStorage/msc_ramdisk/msc_ramdisk.ino

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// 8KB is the smallest size that windows allow to mount
1515
#define DISK_BLOCK_NUM 16
1616
#define DISK_BLOCK_SIZE 512
17+
1718
#include "ramdisk.h"
1819

1920
Adafruit_USBD_MSC usb_msc;
@@ -42,6 +43,10 @@ void setup() {
4243
TinyUSB_Device_Init(0);
4344
#endif
4445

46+
#ifdef BTN_EJECT
47+
pinMode(BTN_EJECT, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
48+
#endif
49+
4550
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
4651
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
4752

@@ -50,33 +55,27 @@ void setup() {
5055

5156
// Set callback
5257
usb_msc.setReadWriteCallback(msc_read_callback, msc_write_callback, msc_flush_callback);
58+
usb_msc.setStartStopCallback(msc_start_stop_callback);
59+
usb_msc.setReadyCallback(msc_ready_callback);
5360

5461
// Set Lun ready (RAM disk is always ready)
5562
usb_msc.setUnitReady(true);
56-
57-
#ifdef BTN_EJECT
58-
pinMode(BTN_EJECT, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
59-
usb_msc.setReadyCallback(msc_ready_callback);
60-
#endif
61-
6263
usb_msc.begin();
6364

6465
Serial.begin(115200);
65-
//while ( !Serial ) delay(10); // wait for native usb
66+
// while ( !Serial ) delay(10); // wait for native usb
6667

6768
Serial.println("Adafruit TinyUSB Mass Storage RAM Disk example");
6869
}
6970

70-
void loop()
71-
{
71+
void loop() {
7272
// nothing to do
7373
}
7474

7575
// Callback invoked when received READ10 command.
7676
// Copy disk's data to buffer (up to bufsize) and
7777
// return number of copied bytes (must be multiple of block size)
78-
int32_t msc_read_callback (uint32_t lba, void* buffer, uint32_t bufsize)
79-
{
78+
int32_t msc_read_callback(uint32_t lba, void* buffer, uint32_t bufsize) {
8079
uint8_t const* addr = msc_disk[lba];
8180
memcpy(buffer, addr, bufsize);
8281

@@ -86,8 +85,7 @@ int32_t msc_read_callback (uint32_t lba, void* buffer, uint32_t bufsize)
8685
// Callback invoked when received WRITE10 command.
8786
// Process data in buffer to disk's storage and
8887
// return number of written bytes (must be multiple of block size)
89-
int32_t msc_write_callback (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
90-
{
88+
int32_t msc_write_callback(uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
9189
uint8_t* addr = msc_disk[lba];
9290
memcpy(addr, buffer, bufsize);
9391

@@ -96,18 +94,22 @@ int32_t msc_write_callback (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
9694

9795
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
9896
// used to flush any pending cache.
99-
void msc_flush_callback (void)
100-
{
97+
void msc_flush_callback(void) {
10198
// nothing to do
10299
}
103100

101+
bool msc_start_stop_callback(uint8_t power_condition, bool start, bool load_eject) {
102+
Serial.printf("Start/Stop callback: power condition %u, start %u, load_eject %u\n", power_condition, start, load_eject);
103+
return true;
104+
}
104105

105-
#ifdef BTN_EJECT
106106
// Invoked when received Test Unit Ready command.
107107
// return true allowing host to read/write this LUN e.g SD card inserted
108-
bool msc_ready_callback(void)
109-
{
108+
bool msc_ready_callback(void) {
109+
#ifdef BTN_EJECT
110110
// button not active --> medium ready
111111
return digitalRead(BTN_EJECT) != activeState;
112+
#else
113+
return true;
114+
#endif
112115
}
113-
#endif

0 commit comments

Comments
 (0)