Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main(void)
- {
- const uint32_t MS_PER_LOOP = 10;
- const uint32_t MS_RESET_ASSERTED = 500;
- // State for debouncing the reset button
- // (We don't want to clear this after triggering the reset relay)
- BOOL isResetButtonPushed_stable = FALSE;
- BOOL isResetButtonPushed_last = FALSE;
- uint32_t msSinceResetButtonChange = 0;
- const uint32_t DEBOUNCE_MS = 50;
- // Loop forever
- while (TRUE)
- {
- //------------------------------------------
- // Loop until it's time to reset (either reset button released or
- // watchdog expired.
- //------------------------------------------
- while (TRUE)
- {
- // State for the watchdog input
- // (We do want to clear this after triggering the reset relay)
- BOOL lastWatchdogState = ReadWatchdogInput();
- uint32_t msSinceWatchdogChange = 0;
- const uint32_t WATCHDOG_TIMEOUT = 150;
- //--------------------------------------------------------------------------
- // Deal with the button:
- // - If it's state hasn't changed from last loop, increment the counter
- // - If the counter is big enough, consider the current value as stable
- // - If the stable value is changing from true->false, the button was just released,
- // so it's time for a reset. Exit the loop
- //--------------------------------------------------------------------------
- const BOOL isResetButtonPushed_now = ReadResetButton();
- if (isResetButtonPushed_now != isResetButtonPushed_last)
- {
- // Button input changed, zero counter
- msSinceResetButtonChange = 0;
- isResetButtonPushed_last = isResetButtonPushed_now;
- }
- else
- {
- // Button hasn't changed, increment counter
- msSinceResetButtonChange += MS_PER_LOOP;
- // See if it's held still long enough
- if (msSinceResetButtonChange > DEBOUNCE_MS)
- {
- msSinceResetButtonChange = DEBOUNCE_MS; // Don't let counter overflow if button
- // doesn't change for a long time
- const BOOL newStableState = isResetButtonPushed_now;
- const BOOL isButtonBeingReleased = !newStableState && isResetButtonPushed_stable;
- isResetButtonPushed_stable = newStableState;
- // if it was released, we're done waiting for a reset event
- if (isButtonBeingReleased)
- {
- break;
- }
- }
- }
- //--------------------------------------------------------------------------
- // Deal with the watchdog
- // - If the input changes, zero the counter
- // - If the input doesn't change, increment the counter
- // - If the counter gets big, it's time for a reset so exit the loop
- //--------------------------------------------------------------------------
- const BOOL newWatchdogState = ReadWatchdogInput();
- if (newWatchdogState != lastWatchdogState)
- {
- msSinceWatchdogChange = 0;
- }
- else
- {
- msSinceWatchdogChange += MS_PER_LOOP;
- }
- lastWatchdogState = newWatchdogState;
- // See if it's time for a reset
- if (msSinceWatchdogChange > WATCHDOG_TIMEOUT)
- {
- break;
- }
- //--------------------------------------------------------------------------
- // Don't need to reset this loop, wait for next
- //
- // The loop will run slightly longer than the intended time because
- // we don't account for the time it takes to run, but we're not integrating
- // time over many loops, so it'll be fine
- //--------------------------------------------------------------------------
- delay(MS_PER_LOOP);
- }
- //------------------------------------------
- // Do the reset
- //------------------------------------------
- ApplyReset();
- delay(MS_RESET_ASSERTED);
- RemoveReset();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement