Skip to content

Commit cab12d2

Browse files
committed
add msc set/get capacity
1 parent 5f65fae commit cab12d2

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

cores/nRF5/usb/tusb_config.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@
9292
// Buffer size of Device Mass storage
9393
#define CFG_TUD_MSC_BUFSIZE 512
9494

95-
// Block size
96-
#define CFG_TUD_MSC_BLOCK_SZ 512
97-
9895
// Vendor name included in Inquiry response, max 8 bytes
9996
#define CFG_TUD_MSC_VENDOR "Adafruit"
10097

libraries/Adafruit_USBDev_MSC/Adafruit_USBDev_MSC.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
#define EPIN 0x80
2929
#define EPSIZE 64 // TODO must be 512 for highspeed device
3030

31+
static Adafruit_USBDev_MSC* _msc_dev = NULL;
32+
3133
Adafruit_USBDev_MSC::Adafruit_USBDev_MSC(void)
3234
{
35+
_block_count = 0;
36+
_block_size = 0;
3337
}
3438

3539
uint16_t Adafruit_USBDev_MSC::getDescriptor(uint8_t* buf, uint16_t bufsize)
@@ -42,15 +46,29 @@ uint16_t Adafruit_USBDev_MSC::getDescriptor(uint8_t* buf, uint16_t bufsize)
4246
return len;
4347
}
4448

45-
void Adafruit_USBDev_MSC::begin(void)
49+
void Adafruit_USBDev_MSC::setCapacity(uint32_t block_count, uint16_t block_size)
50+
{
51+
_block_count = block_count;
52+
_block_size = block_size;
53+
}
54+
55+
void Adafruit_USBDev_MSC::getCapacity(uint32_t* block_count, uint16_t* block_size)
56+
{
57+
(*block_count) = _block_count;
58+
(*block_size) = _block_size;
59+
}
60+
61+
bool Adafruit_USBDev_MSC::begin(void)
4662
{
47-
USBDevice.addInterface(*this);
63+
if ( !USBDevice.addInterface(*this) ) return false;
64+
65+
_msc_dev = this;
66+
return true;
4867
}
4968

5069
extern "C"
5170
{
5271

53-
#include "nrf_gpio.h"
5472
#include "flash/flash_qspi.h"
5573

5674
// Callback invoked when received an SCSI command not in built-in list below
@@ -94,10 +112,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer,
94112
}
95113

96114
// return len must not larger than bufsize
97-
if ( resplen > bufsize )
98-
{
99-
resplen = bufsize;
100-
}
115+
if ( resplen > bufsize ) resplen = bufsize;
101116

102117
// copy response to stack's buffer if any
103118
if ( response && resplen )
@@ -108,11 +123,15 @@ int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer,
108123
return resplen;
109124
}
110125

126+
#define CFG_TUD_MSC_BLOCK_SZ 512
127+
111128
// Callback invoked when received READ10 command.
112129
// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
113130
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
114131
{
115132
(void) lun;
133+
if (!_msc_dev) return -1;
134+
116135

117136
return flash_qspi_read(buffer, lba * CFG_TUD_MSC_BLOCK_SZ + offset, bufsize);
118137
}
@@ -122,6 +141,7 @@ int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buf
122141
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
123142
{
124143
(void) lun;
144+
if (!_msc_dev) return -1;
125145

126146
uint32_t wrcount = flash_qspi_write(lba * CFG_TUD_MSC_BLOCK_SZ + offset, buffer, bufsize);
127147

@@ -145,9 +165,9 @@ void tud_msc_write10_complete_cb (uint8_t lun)
145165
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
146166
{
147167
(void) lun;
168+
if (!_msc_dev) return;
148169

149-
*block_count = flash_qspi_size() / CFG_TUD_MSC_BLOCK_SZ;
150-
*block_size = CFG_TUD_MSC_BUFSIZE;
170+
_msc_dev->getCapacity(block_count, block_size);
151171
}
152172

153173
}

libraries/Adafruit_USBDev_MSC/Adafruit_USBDev_MSC.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@
3030
class Adafruit_USBDev_MSC : Adafruit_USBDev_Interface
3131
{
3232
private:
33+
uint32_t _block_count;
34+
uint16_t _block_size;
3335

3436
public:
3537
Adafruit_USBDev_MSC(void);
3638

37-
void begin(void);
39+
bool begin(void);
40+
41+
void setCapacity(uint32_t block_count, uint16_t block_size);
42+
void getCapacity(uint32_t* block_count, uint16_t* block_size);
3843

3944
// from Adafruit_USBInterface
4045
virtual uint16_t getDescriptor(uint8_t* buf, uint16_t bufsize);

libraries/FileSystem/examples/External_ListFiles/External_ListFiles.ino

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*********************************************************************/
1414

1515
#include <bluefruit.h>
16+
#include <Adafruit_USBDev_MSC.h>
1617
#include <Bluefruit_FileIO.h>
1718

1819
/* This example print out External Flash contents up to
@@ -22,9 +23,15 @@
2223
*/
2324
#define MAX_LEVEL 2
2425

26+
Adafruit_USBDev_MSC usbmsc;
27+
2528
// the setup function runs once when you press reset or power the board
2629
void setup()
2730
{
31+
// block count and size are defined in variant.h
32+
usbmsc.setCapacity(USB_MSC_BLOCK_COUNT, USB_MSC_BLOCK_SIZE);
33+
usbmsc.begin();
34+
2835
Serial.begin(115200);
2936
while ( !Serial ) delay(10); // for nrf52840 with native usb
3037

variants/pca10056/variant.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ static const uint8_t SCK = PIN_SPI_SCK ;
129129
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
130130
#define EXTERNAL_FLASH_DEVICES MX25R6435F
131131

132+
133+
#define USB_MSC_BLOCK_SIZE 512
134+
#define USB_MSC_BLOCK_COUNT ((8*1024*1024) / USB_MSC_BLOCK_SIZE)
135+
132136
#ifdef __cplusplus
133137
}
134138
#endif

0 commit comments

Comments
 (0)