Skip to content

Commit 02fcda8

Browse files
authored
Merge pull request #26 from alrvid/master
Add device connected callback support
2 parents c3ca999 + c9dbc9c commit 02fcda8

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/USBHost/USBHost.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ USBHost * USBHost::instHost = NULL;
3030

3131
#define MIN(a, b) ((a > b) ? b : a)
3232

33+
extern void (*mount_fnc)(void);
34+
3335
/**
3436
* How interrupts are processed:
3537
* - new device connected:
@@ -185,6 +187,11 @@ void USBHost::usb_process()
185187

186188
USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
187189

190+
// Call the device connected callback if registered
191+
if (nullptr != mount_fnc) {
192+
mount_fnc();
193+
}
194+
188195
#if MAX_HUB_NB
189196
if (buf[4] == HUB_CLASS) {
190197
for (k = 0; k < MAX_HUB_NB; k++) {

src/USBHostMSD/USBHostMSD.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#define GET_MAX_LUN (0xFE)
2929
#define BO_MASS_STORAGE_RESET (0xFF)
3030

31+
void (*mount_fnc)(void) = nullptr;
32+
3133
USBHostMSD::USBHostMSD()
3234
{
3335
/* register an object in FAT */
@@ -59,10 +61,10 @@ bool USBHostMSD::connected()
5961

6062
bool USBHostMSD::connect()
6163
{
62-
6364
if (dev_connected) {
6465
return true;
6566
}
67+
6668
host = USBHost::getHostInst();
6769

6870
for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
@@ -90,6 +92,7 @@ bool USBHostMSD::connect()
9092
host->registerDriver(dev, msd_intf, this, &USBHostMSD::init_usb);
9193

9294
dev_connected = true;
95+
9396
return true;
9497
}
9598
} //if()
@@ -430,4 +433,13 @@ const char *USBHostMSD::get_type() const
430433
{
431434
return "USBMSD";
432435
}
436+
437+
// The callback will only be called if connect() has been used at least once before,
438+
// because of an older bugfix (commit 72d0aa6), but this is still useful - see the
439+
// implementation in Arduino_POSIXStorage for an example
440+
bool USBHostMSD::attach_detected_callback(void (*cbk)()) {
441+
mount_fnc = cbk;
442+
return true;
443+
}
444+
433445
#endif

src/USBHostMSD/USBHostMSD.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class USBHostMSD : public IUSBEnumerator, public mbed::BlockDevice
6464
virtual mbed::bd_size_t get_erase_size() const;
6565
virtual mbed::bd_size_t size() const;
6666
virtual const char *get_type() const;
67-
67+
bool attach_detected_callback(void (*cbk)());
6868

6969

7070
protected:

0 commit comments

Comments
 (0)