Skip to content

Commit 7ac9e42

Browse files
committed
add callbacks for usbdev msc
1 parent cab12d2 commit 7ac9e42

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

cores/nRF5/usb/tinyusb/src/class/msc/msc_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct");
4747
#endif
4848

4949
#ifndef CFG_TUD_MSC_BUFSIZE
50-
#error CFG_TUD_MSC_BUFSIZE must be defined, value of CFG_TUD_MSC_BLOCK_SZ should work well, the more the better
50+
#error CFG_TUD_MSC_BUFSIZE must be defined, value of a block size should work well, the more the better
5151
#endif
5252

5353
#ifndef CFG_TUD_MSC_VENDOR

libraries/Adafruit_USBDev_MSC/Adafruit_USBDev_MSC.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ void Adafruit_USBDev_MSC::getCapacity(uint32_t* block_count, uint16_t* block_siz
5858
(*block_size) = _block_size;
5959
}
6060

61+
void Adafruit_USBDev_MSC::setCallback(read_callback_t rd_cb, write_callback_t wr_cb, flush_callback_t fl_cb)
62+
{
63+
_rd_cb = rd_cb;
64+
_wr_cb = wr_cb;
65+
_fl_cb = fl_cb;
66+
}
67+
6168
bool Adafruit_USBDev_MSC::begin(void)
6269
{
6370
if ( !USBDevice.addInterface(*this) ) return false;
@@ -123,43 +130,32 @@ int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer,
123130
return resplen;
124131
}
125132

126-
#define CFG_TUD_MSC_BLOCK_SZ 512
127-
128133
// Callback invoked when received READ10 command.
129134
// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
130135
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
131136
{
132-
(void) lun;
133-
if (!_msc_dev) return -1;
137+
if ( !(_msc_dev && _msc_dev->_rd_cb) ) return -1;
134138

135-
136-
return flash_qspi_read(buffer, lba * CFG_TUD_MSC_BLOCK_SZ + offset, bufsize);
139+
return _msc_dev->_rd_cb(lun, lba, offset, buffer, bufsize);
137140
}
138141

139142
// Callback invoked when received WRITE10 command.
140143
// Process data in buffer to disk's storage and return number of written bytes
141144
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
142145
{
143-
(void) lun;
144-
if (!_msc_dev) return -1;
145-
146-
uint32_t wrcount = flash_qspi_write(lba * CFG_TUD_MSC_BLOCK_SZ + offset, buffer, bufsize);
146+
if ( !(_msc_dev && _msc_dev->_wr_cb) ) return -1;
147147

148-
// update fatfs's cache if address matches
149-
extern void ExternalFS_usbmsc_write (uint32_t lba, void const* buffer, uint32_t bufsize);
150-
if ( ExternalFS_usbmsc_write ) ExternalFS_usbmsc_write(lba, buffer, bufsize);
151-
152-
return wrcount;
148+
return _msc_dev->_wr_cb(lun, lba, offset, buffer, bufsize);
153149
}
154150

155151
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
156152
// used to flush any pending cache.
157153
void tud_msc_write10_complete_cb (uint8_t lun)
158154
{
159-
(void) lun;
155+
if ( !(_msc_dev && _msc_dev->_fl_cb) ) return;
160156

161157
// flush pending cache when write10 is complete
162-
flash_qspi_flush();
158+
return _msc_dev->_fl_cb(lun);
163159
}
164160

165161
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)

libraries/Adafruit_USBDev_MSC/Adafruit_USBDev_MSC.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,33 @@
2929

3030
class Adafruit_USBDev_MSC : Adafruit_USBDev_Interface
3131
{
32-
private:
33-
uint32_t _block_count;
34-
uint16_t _block_size;
35-
3632
public:
33+
typedef int32_t (*read_callback_t ) (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
34+
typedef int32_t (*write_callback_t) (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
35+
typedef void (*flush_callback_t) (uint8_t lun);
36+
3737
Adafruit_USBDev_MSC(void);
3838

3939
bool begin(void);
4040

4141
void setCapacity(uint32_t block_count, uint16_t block_size);
4242
void getCapacity(uint32_t* block_count, uint16_t* block_size);
43+
void setCallback(read_callback_t rd_cb, write_callback_t wr_cb, flush_callback_t fl_cb);
4344

4445
// from Adafruit_USBInterface
4546
virtual uint16_t getDescriptor(uint8_t* buf, uint16_t bufsize);
47+
48+
private:
49+
uint32_t _block_count;
50+
uint16_t _block_size;
51+
52+
read_callback_t _rd_cb;
53+
write_callback_t _wr_cb;
54+
flush_callback_t _fl_cb;
55+
56+
friend int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
57+
friend int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
58+
friend void tud_msc_write10_complete_cb (uint8_t lun);
4659
};
4760

4861
#endif /* ADAFRUIT_USBDEV_MSC_H_ */

libraries/FileSystem/examples/External_ListFiles/External_ListFiles.ino

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
any redistribution
1313
*********************************************************************/
1414

15+
#include "flash/flash_qspi.h"
1516
#include <bluefruit.h>
1617
#include <Adafruit_USBDev_MSC.h>
1718
#include <Bluefruit_FileIO.h>
@@ -30,6 +31,7 @@ void setup()
3031
{
3132
// block count and size are defined in variant.h
3233
usbmsc.setCapacity(USB_MSC_BLOCK_COUNT, USB_MSC_BLOCK_SIZE);
34+
usbmsc.setCallback(fl_read_cb, fl_write_cb, fl_flush_cb);
3335
usbmsc.begin();
3436

3537
Serial.begin(115200);
@@ -64,6 +66,27 @@ void loop()
6466
}
6567
}
6668

69+
int32_t fl_read_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
70+
{
71+
return flash_qspi_read(buffer, lba * USB_MSC_BLOCK_SIZE + offset, bufsize);
72+
}
73+
74+
int32_t fl_write_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
75+
{
76+
uint32_t wrcount = flash_qspi_write(lba * USB_MSC_BLOCK_SIZE + offset, buffer, bufsize);
77+
78+
// update fatfs's cache if address matches
79+
extern void ExternalFS_usbmsc_write (uint32_t lba, void const* buffer, uint32_t bufsize);
80+
if ( ExternalFS_usbmsc_write ) ExternalFS_usbmsc_write(lba, buffer, bufsize);
81+
82+
return wrcount;
83+
}
84+
85+
void fl_flush_cb (uint8_t lun)
86+
{
87+
flash_qspi_flush();
88+
}
89+
6790
/**************************************************************************/
6891
/*!
6992
@brief Print out whole directory tree of an folder

0 commit comments

Comments
 (0)