diff --git a/armsrc/rgb_indicator.c b/armsrc/rgb_indicator.c index a616d32f3..910676922 100644 --- a/armsrc/rgb_indicator.c +++ b/armsrc/rgb_indicator.c @@ -67,12 +67,16 @@ void rgb_indicator_update(void) { if (on_battery == last_state) { return; // edge-triggered: only write RGB when the state changes } - last_state = on_battery; - if (on_battery) { - RgbLedSet(0, 8, 0); // dim green: alive, on battery - } else { - RgbLedSet(0, 0, 0); // on USB: off (cable already signals power) + // Commit last_state only if the controller actually ACKed. On a cold boot the + // I2C RGB controller can still be powering up when the first write fires; if we + // latched last_state regardless, a NAK here would leave the LED dark until the + // next state change (which never comes on battery). Retrying next cycle costs + // one I2C poke and self-heals once the controller is ready. + bool ok = on_battery ? RgbLedSet(0, 8, 0) // dim green: alive, on battery + : RgbLedSet(0, 0, 0); // on USB: off (cable already signals power) + if (ok) { + last_state = on_battery; } } diff --git a/common_arm/rgb/rgb_apis.h b/common_arm/rgb/rgb_apis.h index 8789a8557..d0d5805b9 100644 --- a/common_arm/rgb/rgb_apis.h +++ b/common_arm/rgb/rgb_apis.h @@ -10,6 +10,6 @@ // driven by an I2C RGB controller). Other platforms don't build this module, so // callers must guard usage accordingly (e.g. the CMD_PM5_RGB_SET handler). //----------------------------------------------------------------------------- -void RgbLedSet(uint8_t r, uint8_t g, uint8_t b); +bool RgbLedSet(uint8_t r, uint8_t g, uint8_t b); #endif // RGB_APIS_H_ diff --git a/common_arm/rgb/rgb_hw_at32.c b/common_arm/rgb/rgb_hw_at32.c index e851d6b72..30e9e6739 100644 --- a/common_arm/rgb/rgb_hw_at32.c +++ b/common_arm/rgb/rgb_hw_at32.c @@ -12,11 +12,17 @@ #define RGB_REG_INDEX 0x02 #define RGB_REG_DATA 0x03 -void RgbLedSet(uint8_t r, uint8_t g, uint8_t b) { +bool RgbLedSet(uint8_t r, uint8_t g, uint8_t b) { uint8_t rgb[3] = { r, g, b }; StartTicks(); I2C_init(true); - I2C_WriteByte(0, RGB_REG_INDEX, RGB_I2C_ADDR); // address LED index 0 - I2C_WriteByte(1, RGB_REG_COUNT, RGB_I2C_ADDR); // 1 LED on the string - I2C_BufferWrite(rgb, sizeof(rgb), RGB_REG_DATA, RGB_I2C_ADDR); + // Report NAK: on a cold boot the controller may still be powering up when the + // first write fires. The caller uses this to retry instead of latching a failed + // write (which previously left the LED dark until the next state change - i.e. + // "worked once after flash, dark on every cold boot" on battery). + bool ok = true; + ok &= I2C_WriteByte(0, RGB_REG_INDEX, RGB_I2C_ADDR); // address LED index 0 + ok &= I2C_WriteByte(1, RGB_REG_COUNT, RGB_I2C_ADDR); // 1 LED on the string + ok &= I2C_BufferWrite(rgb, sizeof(rgb), RGB_REG_DATA, RGB_I2C_ADDR); + return ok; }