@@ -77,6 +77,7 @@ static LGFX_ILI9342_M5StackS3 tft;
7777class M5StackCoreS3DisplayUsermod : public Usermod {
7878 private:
7979 bool enabled = true ;
80+ bool _initOk = false ;
8081
8182 // needRedraw marks if redraw is required to prevent often redrawing.
8283 bool needRedraw = true ;
@@ -140,7 +141,7 @@ class M5StackCoreS3DisplayUsermod : public Usermod {
140141 Wire.write (0x90 );
141142 Wire.endTransmission (false );
142143 Wire.requestFrom ((uint8_t )AXP2101_ADDR, (uint8_t )1 );
143- byte pwr_orig = Wire.read ();
144+ byte pwr_orig = Wire.available () ? Wire. read () : 0x00 ;
144145 Serial.printf (" M5StackS3Display: 0x90 orig: 0x%02X\n " , pwr_orig);
145146
146147 // Set DLDO1 voltage to 3.3V (voltage reg 0x99)
@@ -155,7 +156,7 @@ class M5StackCoreS3DisplayUsermod : public Usermod {
155156 Wire.write (0x99 );
156157 Wire.endTransmission (false );
157158 Wire.requestFrom ((uint8_t )AXP2101_ADDR, (uint8_t )1 );
158- byte dldo1_v = Wire.read ();
159+ byte dldo1_v = Wire.available () ? Wire. read () : 0x00 ;
159160 Serial.printf (" M5StackS3Display: DLDO1 0x99: 0x%02X\n " , dldo1_v);
160161
161162 // Enable DLDO1 via bit 7 of 0x90
@@ -172,7 +173,7 @@ class M5StackCoreS3DisplayUsermod : public Usermod {
172173 Wire.write (0x90 );
173174 Wire.endTransmission (false );
174175 Wire.requestFrom ((uint8_t )AXP2101_ADDR, (uint8_t )1 );
175- byte pwr_after = Wire.read ();
176+ byte pwr_after = Wire.available () ? Wire. read () : 0x00 ;
176177 Serial.printf (" M5StackS3Display: 0x90 after: 0x%02X\n " , pwr_after);
177178
178179 // AW9523 GPIO expander for LCD reset
@@ -209,7 +210,7 @@ class M5StackCoreS3DisplayUsermod : public Usermod {
209210 Wire.write (0x03 ); // P1 output register
210211 Wire.endTransmission (false );
211212 Wire.requestFrom ((uint8_t )AW9523B_ADDR, (uint8_t )1 );
212- byte p1_state = Wire.read ();
213+ byte p1_state = Wire.available () ? Wire. read () : 0x00 ;
213214 Serial.printf (" M5StackS3Display: AW9523B P1 state: 0x%02X\n " , p1_state);
214215
215216 Serial.println (" M5StackS3Display: init TFT" );
@@ -222,11 +223,13 @@ class M5StackCoreS3DisplayUsermod : public Usermod {
222223 { (gpio_num_t )TFT_CS, true },
223224 { (gpio_num_t )TFT_DC, true }
224225 };
225- if (pinManager.allocateMultiplePins (pins, 4 , PinOwner::UM_Unspecified)) {
226- Serial.println (" M5StackS3Display: SPI pins allocated" );
227- } else {
228- Serial.println (" M5StackS3Display: SPI pin allocation FAILED" );
226+ if (!pinManager.allocateMultiplePins (pins, 4 , PinOwner::UM_Unspecified)) {
227+ Serial.println (" M5StackS3Display: SPI pin allocation FAILED — disabling usermod" );
228+ enabled = false ;
229+ _initOk = false ;
230+ return ;
229231 }
232+ Serial.println (" M5StackS3Display: SPI pins allocated" );
230233
231234 Serial.printf (" M5StackS3Display: TFT width: %d, height: %d\n " , tft.width (), tft.height ());
232235
@@ -248,13 +251,14 @@ class M5StackCoreS3DisplayUsermod : public Usermod {
248251 tft.drawString (" Loading..." , tft.width () - 10 , textY);
249252
250253 Serial.println (" M5StackS3Display: setup complete" );
254+ _initOk = true ;
251255 }
252256
253257 /*
254258 * loop() is called continuously. Here you can check for events, read sensors, etc.
255259 */
256260 void loop () {
257- if (!enabled) return ;
261+ if (!enabled || !_initOk ) return ;
258262
259263 unsigned long now = millis ();
260264
0 commit comments