@@ -1242,7 +1242,7 @@ void MDMParser::_incModemStateChangeCount(void) {
12421242 }
12431243}
12441244
1245- int MDMParser::getModemStateChangeCount (void ) {
1245+ int MDMParser::getModemStateChangeCount (void ) const {
12461246 return _mdm_state_change_count;
12471247}
12481248
@@ -1253,6 +1253,13 @@ bool MDMParser::powerState(void) const {
12531253 // We are definitely powered on
12541254 return true ;
12551255 }
1256+ // RI is low (so the modem is probably off)
1257+ // Check whether modem has seen a state change at least once
1258+ // and current power state, that only gets reset if we've confirmed that
1259+ // modem was reset with a condition below.
1260+ if (getModemStateChangeCount () > 0 && !_pwr) {
1261+ return false ;
1262+ }
12561263 // We are still not sure whether we are off or not,
12571264 // because RI may be held low for over a second under some conditions
12581265 // Sleeping for 1.1s and checking again
@@ -1306,7 +1313,6 @@ bool MDMParser::powerOff(void)
13061313 _resetFailureAttempts = 0 ;
13071314 bool ok = false ;
13081315 bool continue_cancel = false ;
1309- bool check_ri = false ;
13101316
13111317 MDM_INFO (" %s = = = = = = = = = = = = = =" , POWER_OFF_MSG);
13121318
@@ -1315,7 +1321,8 @@ bool MDMParser::powerOff(void)
13151321 resume (); // make sure we can use the AT parser
13161322 }
13171323
1318- check_ri = true ;
1324+ bool power_state = true ;
1325+
13191326 if (!softPowerOff ()) {
13201327 if (_dev.dev == DEV_SARA_R410) {
13211328 // If memory issue is present, ensure we don't force a power off too soon
@@ -1347,7 +1354,7 @@ bool MDMParser::powerOff(void)
13471354 }
13481355 }
13491356 // Skip power off sequence if power is already off
1350- if (powerState () && _dev.dev != DEV_SARA_G350) {
1357+ if (_dev.dev != DEV_SARA_G350 && (power_state = powerState ()) ) {
13511358 MDM_INFO (" %s Modem not responsive, trying PWR_UC..." , POWER_OFF_MSG);
13521359 HAL_GPIO_Write (PWR_UC, 0 );
13531360 // >1.5 seconds on SARA R410M
@@ -1358,25 +1365,19 @@ bool MDMParser::powerOff(void)
13581365 }
13591366 }
13601367
1361- // Verify power off, or delay
1362- if (check_ri) {
1363- system_tick_t t0 = HAL_Timer_Get_Milli_Seconds ();
1364- bool power_state = true ;
1365- while ((power_state = powerState ()) && !TIMEOUT (t0, 15000 )) {
1366- HAL_Delay_Milliseconds (1 ); // just wait
1367- }
1368- // if V_INT is low, indicate power is off
1369- if (!power_state) {
1370- _pwr = false ;
1371- MDM_INFO (" %s took %lu ms" , POWER_OFF_MSG, HAL_Timer_Get_Milli_Seconds () - t0);
1372- } else {
1373- MDM_INFO (" %s failed" , POWER_OFF_MSG);
1374- }
1375- } else {
1368+ // Verify power off
1369+ system_tick_t t0 = HAL_Timer_Get_Milli_Seconds ();
1370+ // NOTE: initial power_state value is checked here first
1371+ while (power_state && !TIMEOUT (t0, 15000 )) {
1372+ HAL_Delay_Milliseconds (1 ); // just wait
1373+ power_state = powerState ();
1374+ }
1375+ // if V_INT is low, indicate power is off
1376+ if (!power_state) {
13761377 _pwr = false ;
1377- // todo - add if these are automatically done on power down
1378- // _activated = false;
1379- // _attached = false ;
1378+ MDM_INFO ( " %s took %lu ms " , POWER_OFF_MSG, HAL_Timer_Get_Milli_Seconds () - t0);
1379+ } else {
1380+ MDM_INFO ( " %s failed " , POWER_OFF_MSG) ;
13801381 }
13811382 HAL_Delay_Milliseconds (1000 ); // give peace a chance
13821383 // Increment the state change counter to show that the modem has been powered on -> off
0 commit comments