Skip to content

Commit ac76a59

Browse files
committed
Added comments.
1 parent bb29418 commit ac76a59

File tree

2 files changed

+102
-30
lines changed

2 files changed

+102
-30
lines changed

src/pi3usb9281c.cpp

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
#endif
88

99
/**
10+
* @brief Initializes the PI3USB9281C device
1011
*
11-
* @param[in] i2c_library
12-
* @param[in] i2c_address
13-
* @param[in] pin_enb
12+
* This function sets up the I2C communication with the device and configures the enable pin.
13+
* The device only supports I2C address 0x25.
14+
*
15+
* @param[in] i2c_library Reference to the TwoWire I2C library instance to use
16+
* @param[in] i2c_address I2C address of the device (must be 0x25)
17+
* @param[in] pin_enb GPIO pin number connected to the device's enable pin
18+
* @return 0 on success, -EINVAL if invalid I2C address
1419
*/
1520
int pi3usb9281c::setup(TwoWire& i2c_library, const uint8_t i2c_address, const int pin_enb) {
1621

@@ -32,7 +37,11 @@ int pi3usb9281c::setup(TwoWire& i2c_library, const uint8_t i2c_address, const in
3237
}
3338

3439
/**
40+
* @brief Detects if a PI3USB9281C device is present
41+
*
42+
* Reads the device ID register and verifies it matches the expected value (0x18).
3543
*
44+
* @return true if device is detected, false otherwise
3645
*/
3746
bool pi3usb9281c::detect(void) {
3847
uint8_t reg_id;
@@ -44,7 +53,11 @@ bool pi3usb9281c::detect(void) {
4453
}
4554

4655
/**
56+
* @brief Performs a software reset of the device
57+
*
58+
* Writes to the reset register to trigger a device reset.
4759
*
60+
* @return 0 on success, -EIO on I2C communication error
4861
*/
4962
int pi3usb9281c::reset(void) {
5063

@@ -59,8 +72,13 @@ int pi3usb9281c::reset(void) {
5972
}
6073

6174
/**
62-
* @param[in] timeout_ms
63-
* @return
75+
* @brief Waits for a device attachment event
76+
*
77+
* Polls the interrupt register for the device attachment flag.
78+
* When detected, clears the interrupt flag.
79+
*
80+
* @param[in] timeout_ms Maximum time to wait in milliseconds
81+
* @return 0 on device attached, -ETIMEDOUT on timeout, -EIO on I2C error
6482
*/
6583
int pi3usb9281c::device_attach_wait(const uint32_t timeout_ms) {
6684

@@ -94,7 +112,13 @@ int pi3usb9281c::device_attach_wait(const uint32_t timeout_ms) {
94112
}
95113

96114
/**
115+
* @brief Gets the type of attached USB device or charger
116+
*
117+
* Reads the device type and charger status registers to determine what kind of
118+
* device is attached. Can detect various USB ports (SDP, CDP, DCP) and charger types.
97119
*
120+
* @param[out] type Pointer to store the detected device type
121+
* @return 0 on success, -EIO on I2C communication error
98122
*/
99123
int pi3usb9281c::device_type_get(enum pi3usb9281c_device_type* const type) {
100124

@@ -133,7 +157,14 @@ int pi3usb9281c::device_type_get(enum pi3usb9281c_device_type* const type) {
133157
}
134158

135159
/**
160+
* @brief Controls the state of the D+/D- switch
136161
*
162+
* Sets the switch to either automatic control, manually open, or manually closed.
163+
* In automatic mode, the device controls the switch based on detected device type.
164+
* Manual modes allow forcing the switch state regardless of device type.
165+
*
166+
* @param[in] state Desired switch state (auto, manual open, or manual closed)
167+
* @return 0 on success, -EINVAL for invalid state, -EIO on I2C error
137168
*/
138169
int pi3usb9281c::switch_state_set(const enum pi3usb9281c_switch_state state) {
139170

@@ -157,10 +188,13 @@ int pi3usb9281c::switch_state_set(const enum pi3usb9281c_switch_state state) {
157188
}
158189

159190
/**
160-
* Reads the contents of the given register.
161-
* @param[in] reg_address The address of the register.
162-
* @param[out] reg_content A pointer to a variable that will be updated with the contents of the register.
163-
* @return 0 in case of success, or a negative error code otherwise.
191+
* @brief Reads a device register
192+
*
193+
* Performs an I2C read transaction to get the contents of the specified register.
194+
*
195+
* @param[in] reg_address Register address to read
196+
* @param[out] reg_content Pointer to store the register value
197+
* @return 0 on success, -EINVAL if not initialized, -EIO on I2C error
164198
*/
165199
int pi3usb9281c::register_read(const enum pi3usb9281c_register reg_address, uint8_t* const reg_content) {
166200
int res;
@@ -179,7 +213,7 @@ int pi3usb9281c::register_read(const enum pi3usb9281c_register reg_address, uint
179213
}
180214

181215
/* Read data */
182-
m_i2c_library->requestFrom(m_i2c_address, (uint8_t)1, (uint8_t) true);
216+
m_i2c_library->requestFrom(m_i2c_address, (uint8_t)1, (uint8_t)true);
183217
res = m_i2c_library->available();
184218
if (res == 0) {
185219
return -EIO;
@@ -191,10 +225,13 @@ int pi3usb9281c::register_read(const enum pi3usb9281c_register reg_address, uint
191225
}
192226

193227
/**
194-
* Updates the content of the given register.
195-
* @param[in] reg_address The address of the register.
196-
* @param[in] reg_content The new content of the register.
197-
* @return 0 in case of success, or a negative error code otherwise.
228+
* @brief Writes to a device register
229+
*
230+
* Performs an I2C write transaction to update the specified register.
231+
*
232+
* @param[in] reg_address Register address to write
233+
* @param[in] reg_content Value to write to the register
234+
* @return 0 on success, -EINVAL if not initialized, -EIO on I2C error
198235
*/
199236
int pi3usb9281c::register_write(const enum pi3usb9281c_register reg_address, const uint8_t reg_content) {
200237
int res;

src/pi3usb9281c.h

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@
1111
#include <stdint.h>
1212

1313
/**
14+
* @brief Device register addresses
1415
*
16+
* List of all accessible registers in the PI3USB9281C device.
17+
* These registers control device operation and provide status information.
1518
*/
1619
enum pi3usb9281c_register {
17-
PI3USB9281C_REGISTER_ID = 0x01,
18-
PI3USB9281C_REGISTER_CONTROL = 0x02,
19-
PI3USB9281C_REGISTER_INTERRUPT = 0x03,
20-
PI3USB9281C_REGISTER_INTERRUPT_MASK = 0x05,
21-
PI3USB9281C_REGISTER_DEVICE_TYPE = 0x0A,
22-
PI3USB9281C_REGISTER_VBUS_DETECT = 0x0B,
23-
PI3USB9281C_REGISTER_CHARGER_STATUS = 0x0E,
24-
PI3USB9281C_REGISTER_MANUAL_SWITCH = 0x13,
25-
PI3USB9281C_REGISTER_RESET = 0x1B,
26-
PI3USB9281C_REGISTER_VBUS = 0x1D,
20+
PI3USB9281C_REGISTER_ID = 0x01, //!< Device ID register
21+
PI3USB9281C_REGISTER_CONTROL = 0x02, //!< Control register
22+
PI3USB9281C_REGISTER_INTERRUPT = 0x03, //!< Interrupt status register
23+
PI3USB9281C_REGISTER_INTERRUPT_MASK = 0x05, //!< Interrupt mask register
24+
PI3USB9281C_REGISTER_DEVICE_TYPE = 0x0A, //!< Connected device type register
25+
PI3USB9281C_REGISTER_VBUS_DETECT = 0x0B, //!< VBUS detection register
26+
PI3USB9281C_REGISTER_CHARGER_STATUS = 0x0E, //!< Charger status register
27+
PI3USB9281C_REGISTER_MANUAL_SWITCH = 0x13, //!< Manual D+/D- switch control register
28+
PI3USB9281C_REGISTER_RESET = 0x1B, //!< Software reset register
29+
PI3USB9281C_REGISTER_VBUS = 0x1D, //!< VBUS status register
2730
};
2831

2932
/**
30-
* List of different switch states for the D+ and D- signals
33+
* @brief D+/D- Switch control states
34+
*
35+
* Controls how the internal D+/D- switch operates:
36+
* - Auto: Switch controlled by device based on detected USB/charger type
37+
* - Manual Open: Force switch to disconnected state
38+
* - Manual Closed: Force switch to connected state
3139
*/
3240
enum pi3usb9281c_switch_state {
3341
PI3USB9281C_SWITCH_STATE_AUTO,
@@ -36,7 +44,11 @@ enum pi3usb9281c_switch_state {
3644
};
3745

3846
/**
39-
* List of different USB devices the IC can detect
47+
* @brief Types of USB devices and chargers that can be detected
48+
*
49+
* Represents the different types of USB ports and charging devices that
50+
* the PI3USB9281C can identify, including standard USB ports (SDP/CDP/DCP)
51+
* and various charging capabilities.
4052
*/
4153
enum pi3usb9281c_device_type {
4254
PI3USB9281C_DEVICE_TYPE_UNKNOWN,
@@ -46,27 +58,50 @@ enum pi3usb9281c_device_type {
4658
PI3USB9281C_DEVICE_TYPE_CHARGER_1A, //!< 1.0 Amps charger
4759
PI3USB9281C_DEVICE_TYPE_CHARGER_2A, //!< 2.0 Amps charger
4860
PI3USB9281C_DEVICE_TYPE_CHARGER_2_4A, //!< 2.4 Amps charger
49-
PI3USB9281C_DEVICE_TYPE_CARKIT1, //!<
50-
PI3USB9281C_DEVICE_TYPE_CARKIT2, //!<
61+
PI3USB9281C_DEVICE_TYPE_CARKIT1, //!< CarKit type 1 accessory
62+
PI3USB9281C_DEVICE_TYPE_CARKIT2, //!< CarKit type 2 accessory
5163
};
5264

5365
/**
66+
* @brief PI3USB9281C USB charger detection IC driver class
5467
*
68+
* This class provides an interface to the PI3USB9281C IC, which can detect various
69+
* types of USB ports and charging devices. The IC communicates over I2C and provides
70+
* functionality to:
71+
* - Detect USB port types (SDP/CDP/DCP)
72+
* - Identify charging capabilities
73+
* - Control D+/D- switch routing
74+
* - Monitor device attachment
5575
*/
5676
class pi3usb9281c {
5777
public:
78+
//!@{
79+
//! Initialization and setup
5880
int setup(TwoWire &i2c_library, const uint8_t i2c_address, const int pin_res);
5981
bool detect(void);
6082
int reset(void);
83+
//!@}
84+
85+
//!@{
86+
//! Device detection and monitoring
6187
int device_attach_wait(const uint32_t timeout_ms);
6288
int device_type_get(enum pi3usb9281c_device_type *const type);
89+
//!@}
90+
91+
//!@{
92+
//! Switch control
6393
int switch_state_set(const enum pi3usb9281c_switch_state state);
94+
//!@}
95+
96+
//!@{
97+
//! Register access
6498
int register_read(const enum pi3usb9281c_register reg_address, uint8_t *const reg_content);
6599
int register_write(const enum pi3usb9281c_register reg_address, const uint8_t reg_content);
100+
//!@}
66101

67102
protected:
68-
TwoWire *m_i2c_library = NULL;
69-
uint8_t m_i2c_address = 0;
103+
TwoWire *m_i2c_library = NULL; //!< Pointer to I2C library instance
104+
uint8_t m_i2c_address = 0; //!< Device I2C address
70105
};
71106

72107
#endif

0 commit comments

Comments
 (0)