Advertisement
Guest User

Untitled

a guest
Jan 13th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. void main(void)
  2. {
  3.     const uint32_t MS_PER_LOOP = 10;
  4.     const uint32_t MS_RESET_ASSERTED = 500;
  5.  
  6.     // State for debouncing the reset button
  7.     // (We don't want to clear this after triggering the reset relay)
  8.     BOOL isResetButtonPushed_stable = FALSE;
  9.     BOOL isResetButtonPushed_last = FALSE;
  10.     uint32_t msSinceResetButtonChange = 0;
  11.     const uint32_t DEBOUNCE_MS = 50;
  12.  
  13.     // Loop forever
  14.     while (TRUE)
  15.     {
  16.         //------------------------------------------
  17.         // Loop until it's time to reset (either reset button released or
  18.         // watchdog expired.  
  19.         //------------------------------------------
  20.         while (TRUE)
  21.         {      
  22.             // State for the watchdog input
  23.             // (We do want to clear this after triggering the reset relay)
  24.             BOOL lastWatchdogState = ReadWatchdogInput();
  25.             uint32_t msSinceWatchdogChange = 0;
  26.             const uint32_t WATCHDOG_TIMEOUT = 150;
  27.        
  28.             //--------------------------------------------------------------------------
  29.             // Deal with the button:
  30.             // - If it's state hasn't changed from last loop, increment the counter
  31.             // - If the counter is big enough, consider the current value as stable
  32.             // - If the stable value is changing from true->false, the button was just released,
  33.             //   so it's time for a reset.  Exit the loop
  34.             //--------------------------------------------------------------------------
  35.             const BOOL isResetButtonPushed_now = ReadResetButton();
  36.             if (isResetButtonPushed_now != isResetButtonPushed_last)
  37.             {
  38.                 // Button input changed, zero counter
  39.                 msSinceResetButtonChange = 0;
  40.                 isResetButtonPushed_last = isResetButtonPushed_now;
  41.             }
  42.             else
  43.             {
  44.                 // Button hasn't changed, increment counter
  45.                 msSinceResetButtonChange += MS_PER_LOOP;
  46.                
  47.                 // See if it's held still long enough
  48.                 if (msSinceResetButtonChange > DEBOUNCE_MS)
  49.                 {
  50.                     msSinceResetButtonChange = DEBOUNCE_MS; // Don't let counter overflow if button
  51.                                                             // doesn't change for a long time
  52.                    
  53.                     const BOOL newStableState = isResetButtonPushed_now;
  54.                     const BOOL isButtonBeingReleased = !newStableState && isResetButtonPushed_stable;
  55.                     isResetButtonPushed_stable = newStableState;
  56.                    
  57.                     // if it was released, we're done waiting for a reset event
  58.                     if (isButtonBeingReleased)
  59.                     {
  60.                         break;
  61.                     }
  62.                 }    
  63.             }
  64.            
  65.             //--------------------------------------------------------------------------
  66.             // Deal with the watchdog
  67.             // - If the input changes, zero the counter
  68.             // - If the input doesn't change, increment the counter
  69.             // - If the counter gets big, it's time for a reset so exit the loop
  70.             //--------------------------------------------------------------------------
  71.             const BOOL newWatchdogState = ReadWatchdogInput();
  72.             if (newWatchdogState != lastWatchdogState)
  73.             {
  74.                 msSinceWatchdogChange = 0;
  75.             }
  76.             else
  77.             {
  78.                 msSinceWatchdogChange += MS_PER_LOOP;
  79.             }
  80.             lastWatchdogState = newWatchdogState;
  81.            
  82.             // See if it's time for a reset
  83.             if (msSinceWatchdogChange > WATCHDOG_TIMEOUT)
  84.             {
  85.                 break;
  86.             }
  87.        
  88.             //--------------------------------------------------------------------------
  89.             // Don't need to reset this loop, wait for next
  90.             //
  91.             // The loop will run slightly longer than the intended time because
  92.             // we don't account for the time it takes to run, but we're not integrating
  93.             // time over many loops, so it'll be fine      
  94.             //--------------------------------------------------------------------------
  95.             delay(MS_PER_LOOP);
  96.         }
  97.  
  98.         //------------------------------------------
  99.         // Do the reset
  100.         //------------------------------------------
  101.         ApplyReset();
  102.         delay(MS_RESET_ASSERTED);
  103.         RemoveReset();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement