diff options
-rw-r--r-- | include/fusb302b.h | 3 | ||||
-rw-r--r-- | src/fusb302b.cpp | 18 |
2 files changed, 21 insertions, 0 deletions
diff --git a/include/fusb302b.h b/include/fusb302b.h index ce473c5..ad41366 100644 --- a/include/fusb302b.h +++ b/include/fusb302b.h @@ -82,6 +82,9 @@ public: bool runCCLineSelection() const; + // Measure VBus with the MADC and check if its connected + bool isVBUSConnected() const; + private: const uint8_t DeviceAddress; // I2C address for this device // I2C bus access functions, should return true if command worked diff --git a/src/fusb302b.cpp b/src/fusb302b.cpp index 10b5153..c2792f8 100644 --- a/src/fusb302b.cpp +++ b/src/fusb302b.cpp @@ -189,6 +189,24 @@ bool FUSB302::runCCLineSelection() const { } return true; } + +bool FUSB302::isVBUSConnected() const { + // So we want to set MEAS_VBUS to enable measuring the VBus signal + // Then check the status + uint8_t measureBackup = fusb_read_byte(FUSB_MEASURE); + uint8_t switchesBackup = fusb_read_byte(FUSB_SWITCHES0); + // clear MEAS_CCx bits + fusb_write_byte(FUSB_SWITCHES0, switchesBackup & 0b11110011); + osDelay(10); + fusb_write_byte(FUSB_MEASURE, 0b01000000); + osDelay(100); + uint8_t status = fusb_read_byte(FUSB_STATUS0); + // Write back original value + fusb_write_byte(FUSB_MEASURE, measureBackup); + fusb_write_byte(FUSB_SWITCHES0, switchesBackup); + return (status & (0b00100000)) != 0; +} + bool FUSB302::fusb_get_status(fusb_status *status) const { /* Read the interrupt and status flags into status */ |