Skip to content

Commit f20ccaf

Browse files
committed
add msc host setWriteCompleteCallback()
1 parent 3ce9ec3 commit f20ccaf

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

examples/DualRole/msc_data_logger/msc_data_logger.ino

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define HOST_PIN_VBUS_EN_STATE 1
4141

4242

43-
#define LOG_FILE "cpu_temp.txt"
43+
#define LOG_FILE "cpu_temp.csv"
4444
#define LOG_INTERVAL 5000
4545

4646
// USB Host object
@@ -62,7 +62,7 @@ volatile bool is_mounted = false;
6262

6363
void setup()
6464
{
65-
Serial1.begin(115200);
65+
pinMode(LED_BUILTIN, OUTPUT);
6666

6767
Serial.begin(115200);
6868
//while ( !Serial ) delay(10); // wait for native usb
@@ -72,17 +72,25 @@ void setup()
7272

7373
void loop()
7474
{
75-
// nothing to do
76-
if (!is_mounted) return;
75+
if (!is_mounted) {
76+
// nothing to do
77+
delay(1000);
78+
return;
79+
}
80+
81+
// Turn on LED when start writing
82+
digitalWrite(LED_BUILTIN, HIGH);
7783

7884
f_log = fatfs.open(LOG_FILE, O_WRITE | O_APPEND | O_CREAT);
7985

8086
if (!f_log) {
8187
Serial.println("Cannot create file: " LOG_FILE);
8288
}else {
8389
float cpu_temp = analogReadTemp();
90+
uint32_t ms = millis();
8491

85-
f_log.printf("%u\t%.02f\r\n", millis(), cpu_temp);
92+
Serial.printf("%u,%.02f\r\n", millis(), cpu_temp);
93+
f_log.printf("%u,%.02f\r\n", millis(), cpu_temp);
8694

8795
f_log.close();
8896
}
@@ -157,6 +165,8 @@ void tuh_msc_mount_cb(uint8_t dev_addr)
157165
// For simplicity this example only support LUN 0
158166
msc_block_dev.setActiveLUN(0);
159167

168+
msc_block_dev.setWriteCompleteCallback(write_complete_callback);
169+
160170
is_mounted = fatfs.begin(&msc_block_dev);
161171

162172
if (is_mounted) {
@@ -179,3 +189,17 @@ void tuh_msc_umount_cb(uint8_t dev_addr)
179189
msc_block_dev.end();
180190
}
181191

192+
193+
bool write_complete_callback(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data)
194+
{
195+
(void) dev_addr;
196+
(void) cb_data;
197+
198+
// turn off LED after write is complete
199+
// Note this only marks the usb transfer is complete, device can take longer to actual
200+
// write data to physical flash
201+
digitalWrite(LED_BUILTIN, LOW);
202+
203+
return true;
204+
}
205+

examples/DualRole/msc_file_explorer/msc_file_explorer.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ bool is_mounted = false;
5555

5656
void setup()
5757
{
58-
Serial1.begin(115200);
59-
6058
Serial.begin(115200);
6159
//while ( !Serial ) delay(10); // wait for native usb
6260

src/arduino/msc/Adafruit_USBH_MSC.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
Adafruit_USBH_MSC_BlockDevice::Adafruit_USBH_MSC_BlockDevice() {
3535
_daddr = _lun = 0;
3636
_busy = false;
37+
_wr_cb = NULL;
3738
}
3839

3940
bool Adafruit_USBH_MSC_BlockDevice::begin(uint8_t dev_addr) {
@@ -45,6 +46,9 @@ bool Adafruit_USBH_MSC_BlockDevice::setActiveLUN(uint8_t lun) {
4546
_lun = lun;
4647
return true;
4748
}
49+
void Adafruit_USBH_MSC_BlockDevice::setWriteCompleteCallback(tuh_msc_complete_cb_t cb) {
50+
_wr_cb = cb;
51+
}
4852

4953
void Adafruit_USBH_MSC_BlockDevice::end(void) { _daddr = _lun = 0; }
5054

@@ -70,6 +74,15 @@ bool Adafruit_USBH_MSC_BlockDevice::_io_complete_cb(
7074

7175
// TODO skip csw status: assuming io is successful
7276
_busy = false;
77+
78+
switch(cb_data->cbw->command[0]) {
79+
case SCSI_CMD_WRITE_10:
80+
if (_wr_cb) {
81+
_wr_cb(dev_addr, cb_data);
82+
}
83+
break;
84+
}
85+
7386
return true;
7487
}
7588

src/arduino/msc/Adafruit_USBH_MSC.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ class Adafruit_USBH_MSC_BlockDevice : public FsBlockDeviceInterface {
3939
bool begin(uint8_t dev_addr);
4040
void end(void);
4141

42+
bool mounted(void);
43+
4244
// Set active LUN
4345
bool setActiveLUN(uint8_t lun);
44-
45-
bool mounted(void);
46+
void setWriteCompleteCallback(tuh_msc_complete_cb_t cb);
4647

4748
//------------- SdFat v2 FsBlockDeviceInterface API -------------//
4849
virtual bool isBusy();
@@ -66,6 +67,8 @@ class Adafruit_USBH_MSC_BlockDevice : public FsBlockDeviceInterface {
6667
// implementation
6768
volatile bool _busy;
6869

70+
tuh_msc_complete_cb_t _wr_cb;
71+
6972
bool wait_for_io(void);
7073
};
7174

0 commit comments

Comments
 (0)