-
Notifications
You must be signed in to change notification settings - Fork 84
Open
Description
Yes this was written by AI.
M5Unified IP5306 Charging Detection Bug Fix Report
Executive Summary
CRITICAL BUG IDENTIFIED AND FIXED: The M5Unified library contains a critical bug in the IP5306 power management chip's charging detection method that causes intermittent battery charging and level detection on M5Stack Basic devices.
Root Cause Analysis
The Bug
Located in: M5Unified/src/utility/power/IP5306_Class.cpp
Original Buggy Code:
bool IP5306_Class::isCharging(void) {
std::uint8_t val = 0;
return (readRegister(0x71, &val, 1)) && (val % 0x0C); // BUG: Modulo operator
}
Fixed Code:
bool IP5306_Class::isCharging(void) {
std::uint8_t val = 0;
//CRITICAL BUG FIX - Original line used modulo operator (%) instead of bitwise AND (&)
//This causes intermittent charging detection because modulo produces inconsistent results
//return (readRegister(0x71, &val, 1)) && (val % 0x0C); // BUGGY ORIGINAL LINE
//FIXED - Use bitwise AND for proper bit masking operation
return (readRegister(0x71, &val, 1)) && (val & 0x0C);
}
Impact Analysis
Problem: The modulo operator %
was incorrectly used instead of bitwise AND &
for bit masking operations.
Consequences:
- Intermittent charging detection
- Unreliable battery status reporting
- Power management decisions based on incorrect charging state
- User frustration with inconsistent battery monitoring
Comparison with Working Implementation
Older M5Stack Library (Working):
bool POWER::isCharging() {
uint8_t data;
return (M5.I2C.readByte(IP5306_ADDR, IP5306_REG_READ0, &data))
? (data & CHARGE_ENABLE_BIT) // CORRECT: Uses bitwise AND
: false;
}
Key Differences:
- Register: Old uses 0x70 (REG_READ0), new uses 0x71 (REG_READ1)
- Bit Pattern: Old uses 0x08 (CHARGE_ENABLE_BIT), new uses 0x0C
- Operator: Old correctly uses
&
, new incorrectly used%
Technical Details
IP5306 Register Information
- Address: 0x75 (117 decimal)
- Register 0x71: Charging status register
- Bit Pattern 0x0C: Charging detection bits (bits 2 and 3)
Bit Masking vs Modulo
// WRONG (original bug):
val % 0x0C // Modulo operation - inconsistent for bit testing
// CORRECT (fixed):
val & 0x0C // Bitwise AND - proper bit masking
Why Modulo Failed
- Modulo operator
%
returns remainder of division - For bit pattern testing, this produces inconsistent results
- Bitwise AND
&
correctly masks specific bits - Example:
0x0F % 0x0C = 3
, but0x0F & 0x0C = 0x0C
Testing and Verification
Build Status
✅ SUCCESS: Project builds successfully with local fixed library
✅ Dependencies: All local dependencies resolved correctly
✅ Compatibility: No breaking changes to existing code
Expected Improvements
- Consistent Charging Detection: No more intermittent charging status
- Reliable Battery Monitoring: Accurate power management decisions
- Stable Power Management: Proper charging state-based logic
- User Experience: Consistent "CHARGING" vs "ON BATTERY" display
Files Modified
Core Fix
lib/M5Unified/src/utility/power/IP5306_Class.cpp
- CRITICAL BUG FIX
Metadata
Metadata
Assignees
Labels
No labels