Skip to content

Commit

Permalink
Merge pull request #391 from adafruit/update-ramdisk-with-start-stop
Browse files Browse the repository at this point in the history
Update ramdisk with start stop
  • Loading branch information
hathach authored Feb 20, 2024
2 parents e39183c + dac1eee commit 2913c9c
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions examples/MassStorage/msc_ramdisk/msc_ramdisk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// 8KB is the smallest size that windows allow to mount
#define DISK_BLOCK_NUM 16
#define DISK_BLOCK_SIZE 512

#include "ramdisk.h"

Adafruit_USBD_MSC usb_msc;
Expand Down Expand Up @@ -42,6 +43,10 @@ void setup() {
TinyUSB_Device_Init(0);
#endif

#ifdef BTN_EJECT
pinMode(BTN_EJECT, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
#endif

// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
usb_msc.setID("Adafruit", "Mass Storage", "1.0");

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

// Set callback
usb_msc.setReadWriteCallback(msc_read_callback, msc_write_callback, msc_flush_callback);
usb_msc.setStartStopCallback(msc_start_stop_callback);
usb_msc.setReadyCallback(msc_ready_callback);

// Set Lun ready (RAM disk is always ready)
usb_msc.setUnitReady(true);

#ifdef BTN_EJECT
pinMode(BTN_EJECT, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
usb_msc.setReadyCallback(msc_ready_callback);
#endif

usb_msc.begin();

Serial.begin(115200);
//while ( !Serial ) delay(10); // wait for native usb
// while ( !Serial ) delay(10); // wait for native usb

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

void loop()
{
void loop() {
// nothing to do
}

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

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

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

// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void msc_flush_callback (void)
{
void msc_flush_callback(void) {
// nothing to do
}

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

#ifdef BTN_EJECT
// Invoked when received Test Unit Ready command.
// return true allowing host to read/write this LUN e.g SD card inserted
bool msc_ready_callback(void)
{
bool msc_ready_callback(void) {
#ifdef BTN_EJECT
// button not active --> medium ready
return digitalRead(BTN_EJECT) != activeState;
#else
return true;
#endif
}
#endif

0 comments on commit 2913c9c

Please sign in to comment.