changeset 561:dc1e0a1c100f

sleep logic change: allow big sleep when UART or SIM is blocking deep sleep
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 06 Jan 2019 21:12:51 +0000
parents d380b62e1019
children adae731ae50d
files src/cs/layer1/cfile/l1_pwmgr.c src/cs/layer1/cust0/l1_cust.c
diffstat 2 files changed, 67 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/cs/layer1/cfile/l1_pwmgr.c	Sat Dec 29 04:06:37 2018 +0000
+++ b/src/cs/layer1/cfile/l1_pwmgr.c	Sun Jan 06 21:12:51 2019 +0000
@@ -782,7 +782,7 @@
     #if (W_A_CALYPSO_PLUS_SPR_19599 == 1)
       BOOL     extended_page_mode_state = 0;       //Store state of extended page mode
     #endif
-    WORD32 time_from_last_wakeup=0;
+    WORD32 time_from_last_wakeup, min_time_from_last_wakeup;
     UWORD32  sleep_mode;
 
     #if (OP_BT == 1)
@@ -793,8 +793,33 @@
     why_big_sleep = BIG_SLEEP_DUE_TO_UNDEFINED;
     wakeup_type   = WAKEUP_FOR_UNDEFINED;
 
+    /*
+     * FreeCalypso change: TI's original code implemented logic to
+     * suppress both big and deep sleep (i.e., retry on the next frame)
+     * if less than 7 frames have elapsed since the last wakeup and
+     * if the previous sleep cycle was CLOCK_STOP.  We are changing
+     * this logic in two ways: the check has been moved up here
+     * (originally the check code was way down, wasting work on other
+     * logic if there will be no sleep anyway), and we additionally
+     * suppress both big and deep sleep (effecting retry on the next
+     * frame) if the previous sleep cycle was FRAME_STOP and less than
+     * 5 frames have elapsed since wakeup.  The reason for the latter
+     * addition is that we now allow big sleep while UART and/or SIM
+     * activity timers are running (suppressing deep sleep), and
+     * holding off for 5 frames before going into another big sleep
+     * keeps us from fluttering in and out of big sleep as the external
+     * host or the SIM is trying to talk to us.
+     */
     time_from_last_wakeup = (sleep_time - last_wakeup + 42432) % 42432;
 
+    if (previous_sleep == CLOCK_STOP)
+      min_time_from_last_wakeup = 7;
+    else
+      min_time_from_last_wakeup = 5;
+
+    if (time_from_last_wakeup < min_time_from_last_wakeup)
+      return;
+
     //=================================================
     // check System (SIM, UART, LDC ..... )
     //=================================================
@@ -844,10 +869,15 @@
     // return and wait end of this activity (few TDMA frames) then check on next TDMA frames
     // if MS can go in deep sleep
     /*
-     * FreeCalypso change: the abstention from big sleep because of UART or SIM
-     * has been moved into Cust_check_system(), which now returns DO_NOT_SLEEP
-     * in these cases, while still returning FRAME_STOP if the cause is
-     * the backlight or charging.
+     * FreeCalypso change: we no longer abstain from big sleep because of UART
+     * and SIM activity timers, i.e., when deep sleep is suppressed because of
+     * either of those, we go into big sleep instead.  We also do big sleep
+     * if deep sleep is disallowed because of the backlight or charging.
+     * However, if the UART issue is not the running activity timer, but some
+     * output being drained from the Tx FIFO, for that case our new code in
+     * Cust_check_system() will return DO_NOT_SLEEP (checked above), causing
+     * us to retry on the next frame and hopefully go into deep sleep after
+     * another frame or two.
      */
     if (    !min_time
          || !HWtimer
@@ -933,6 +963,8 @@
         l1s.pw_mgr.sleep_performed = FRAME_STOP;
 
 
+      /* FreeCalypso change: check moved up and extended */
+    #if 0
       if ((previous_sleep == CLOCK_STOP) && (time_from_last_wakeup < 7))
       {
 		#if (CODE_VERSION != SIMULATION)
@@ -944,6 +976,7 @@
 		#endif // NOT SIMULATION
         return;
       }
+    #endif
 
      // update previous sleep
        previous_sleep = l1s.pw_mgr.sleep_performed;
--- a/src/cs/layer1/cust0/l1_cust.c	Sat Dec 29 04:06:37 2018 +0000
+++ b/src/cs/layer1/cust0/l1_cust.c	Sun Jan 06 21:12:51 2019 +0000
@@ -254,68 +254,58 @@
 #ifdef RVM_FCHG_SWE
   extern T_PWR_CTRL_BLOCK *pwr_ctrl;
 #endif
+  extern SYS_BOOL uart_sleep_timer_enabled;
 
 #if (CODE_VERSION != SIMULATION)
-#if (L2_L3_SIMUL == 0)
+
+    //cut ARMIO and UWIRE clocks in big sleep
+    l1s.pw_mgr.modules_status = ARMIO_CLK_CUT | UWIRE_CLK_CUT;
 
 #ifdef RVM_FCHG_SWE
     // Forbig deep sleep when charging
     if (pwr_ctrl && pwr_ctrl->state >= FCHG_STATE_I2V_CAL_1 &&
         pwr_ctrl->state <= FCHG_STATE_CV_CHARGING)
     {
-      //cut ARMIO and UWIRE clocks in big sleep
-      l1s.pw_mgr.modules_status = ARMIO_CLK_CUT | UWIRE_CLK_CUT ;
       why_big_sleep = BIG_SLEEP_DUE_TO_CHARGING;
       return(FRAME_STOP);  // BIG sleep
     }
 #endif
 
     // Forbid deep sleep if the light is on
-    if(LT_Status())
+    if (LT_Status())
     {
-      //cut ARMIO and UWIRE clocks in big sleep
-      l1s.pw_mgr.modules_status = ARMIO_CLK_CUT | UWIRE_CLK_CUT ;
       why_big_sleep = BIG_SLEEP_DUE_TO_LIGHT_ON;
       return(FRAME_STOP);  // BIG sleep
     }
 
     // Forbid deep sleep if the SIM and UARTs not ready
-    if(SIM_SleepStatus())
+    if (!SIM_SleepStatus())
+    {
+      why_big_sleep = BIG_SLEEP_DUE_TO_SIM;
+      return(FRAME_STOP);  // BIG sleep
+    }
+
+    /* FreeCalypso: check UART activity timer */
+    if (uart_sleep_timer_enabled)
     {
-#endif
+      why_big_sleep = BIG_SLEEP_DUE_TO_UART;
+      return(FRAME_STOP);  // BIG sleep
+    }
+
+    if ((l1s.pw_mgr.mode_authorized == DEEP_SLEEP) ||
+        (l1s.pw_mgr.mode_authorized == ALL_SLEEP))
+    {
       if(SER_UartSleepStatus()) 
-      {
         return(CLOCK_STOP);   // DEEP sleep
-      }
-      else why_big_sleep = BIG_SLEEP_DUE_TO_UART;
-#if (L2_L3_SIMUL == 0)
-   }
-   else why_big_sleep = BIG_SLEEP_DUE_TO_SIM;
-#endif
-        // cut ARMIO and UWIRE clocks in big sleep
-        l1s.pw_mgr.modules_status = ARMIO_CLK_CUT | UWIRE_CLK_CUT ;
-	/*
-	 * FreeCalypso change: in TI's original code, whenever
-	 * this Cust_check_system() function returns FRAME_STOP
-	 * instead of CLOCK_STOP, l1s_sleep_manager() would
-	 * abstain from all sleep and not just from deep sleep.
-	 * The comments there say that the intent is to avoid
-	 * big sleep when there is UART or SIM activity,
-	 * but the result is that when we tell L1 to do big sleep
-	 * instead of deep sleep because of the backlight or
-	 * charging, it doesn't sleep at all, except small sleep
-	 * via Nucleus idle loop.
-	 *
-	 * Our change preserves TI's abstention from big sleep
-	 * due to UART or SIM activity, but allows big sleep when
-	 * the cause is light or charging.  We've changed this
-	 * Cust_check_system() function to return DO_NOT_SLEEP
-	 * instead of FRAME_STOP for UART and SIM, while still
-	 * returning FRAME_STOP for light and charging, and
-	 * changed the code in l1s_sleep_manager() to do big sleep
-	 * when this Cust_check_system() function returns FRAME_STOP.
-	 */
-        return(DO_NOT_SLEEP);  // NO sleep
+      else
+        return(DO_NOT_SLEEP); /* wait another frame or two */
+    }
+    else
+    {
+      why_big_sleep = BIG_SLEEP_DUE_TO_SLEEP_MODE;
+      return(FRAME_STOP);  // BIG sleep
+    }
+
 #else // Simulation part
   return(CLOCK_STOP);   // DEEP sleep
 #endif