Skip to content

Commit 5167a86

Browse files
committed
Added non-blocking variant of the device_attach_wait function.
1 parent 4a39dd1 commit 5167a86

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ setup KEYWORD2
33
detect KEYWORD2
44
reset KEYWORD2
55
device_attach_wait KEYWORD2
6+
device_attach_get KEYWORD2
67
device_type_get KEYWORD2
78
switch_state_set KEYWORD2
89
register_read KEYWORD2

src/pi3usb9281c.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,39 @@ int pi3usb9281c::device_attach_wait(const uint32_t timeout_ms) {
111111
}
112112
}
113113

114+
/**
115+
* @brief Checks for device attachment (non-blocking)
116+
*
117+
* Reads the interrupt register once to check for device attachment.
118+
* If a device is attached, clears the interrupt flag.
119+
*
120+
* @return 1 if device attached, 0 if no device attached, -EIO on I2C error
121+
*/
122+
int pi3usb9281c::device_attach_get(void) {
123+
124+
/* Read interrupt flags */
125+
uint8_t reg_interrupt = 0;
126+
if (register_read(PI3USB9281C_REGISTER_INTERRUPT, &reg_interrupt) < 0) {
127+
return -EIO;
128+
}
129+
130+
/* Check accessory attached interrupt flag */
131+
if (reg_interrupt & 0b1) {
132+
133+
/* Clear interrupt flag */
134+
reg_interrupt = 0b1;
135+
if (register_write(PI3USB9281C_REGISTER_INTERRUPT, reg_interrupt) < 0) {
136+
return -EIO;
137+
}
138+
139+
/* Return device attached */
140+
return 1;
141+
}
142+
143+
/* Return no device attached */
144+
return 0;
145+
}
146+
114147
/**
115148
* @brief Gets the type of attached USB device or charger
116149
*

src/pi3usb9281c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class pi3usb9281c {
8585
//!@{
8686
//! Device detection and monitoring
8787
int device_attach_wait(const uint32_t timeout_ms);
88+
int device_attach_get(void);
8889
int device_type_get(enum pi3usb9281c_device_type *const type);
8990
//!@}
9091

0 commit comments

Comments
 (0)