void WDT_Start(void) { /* Unlock the WDT registers for modification */ CySysWdtUnlock(); /* Setup ISR */ WDT_Interrupt_StartEx(&Timer_Interrupt); /* Write the mode to generate interrupt on match */ CySysWdtWriteMode(WDT_COUNTER, CY_SYS_WDT_MODE_INT); /* Configure the WDT counter clear on a match setting */ CySysWdtWriteClearOnMatch(WDT_COUNTER, WDT_COUNTER_ENABLE); /* Configure the WDT counter match comparison value */ CySysWdtWriteMatch(WDT_COUNTER, WDT_1SEC); /* Reset WDT counter */ CySysWdtResetCounters(WDT_COUNTER); /* Enable the specified WDT counter */ CySysWdtEnable(WDT_COUNTER_MASK); /* Lock out configuration changes to the Watchdog timer registers */ CySysWdtLock(); }
int main() { /* Place your initialization/startup code here (e.g. MyInst_Start()) */ initialize(); for(;;) { /* Place your application code here. */ //debug pin toggle P4_0_Debug_Write(0xff); //measure sensor inputs sensors_updateSensors(); //debug pin toggle P4_0_Debug_Write(0x00); //iterate the control loop control_IterateControl(); //feed watchdog CySysWdtResetCounters(CY_SYS_WDT_COUNTER1_RESET); } }
/***************************************************************************** * Function Name: _Watchdog_Handler() ****************************************************************************** * Summary: * Watchdog interrupt routine. * * Parameters: * None. * * Return: * None. * * Note: * *****************************************************************************/ void _Watchdog_Handler(void) { // If the Interrupt source is Counter 0 match, then process. if(CySysWdtGetInterruptSource() & CY_SYS_WDT_COUNTER0_INT) { // Clear Watchdog Interrupt from Counter 0. CySysWdtClearInterrupt(CY_SYS_WDT_COUNTER0_INT); /********************************************************************/ /********************************************************************/ // Unlock the WDT registers for modification. CySysWdtUnlock(); // Disable Counter 0 to allow modifications. CySysWdtDisable(CY_SYS_WDT_COUNTER0_MASK); // Reset Counter 0 and give ~3 LFCLK cycles to take effect. CySysWdtResetCounters(CY_SYS_WDT_COUNTER0_RESET); CyDelayUs(WATCHDOG_REG_UPDATE_WAIT_TIME); // Write the Counter 0 match value for 20 millisecond and give // ~3 LFCLK cycles to take effect. CySysWdtWriteMatch(CY_SYS_WDT_COUNTER0, WATCHDOG_COUNTER0_PERIOD); CyDelayUs(WATCHDOG_REG_UPDATE_WAIT_TIME); // Enable Watchdog Counter 0. CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK); // Lock Watchdog to prevent any further change. CySysWdtLock(); } // If the Interrupt source is Counter 1 match, then process. else if(CySysWdtGetInterruptSource() & CY_SYS_WDT_COUNTER1_INT) { // Clear Watchdog Interrupt from Counter 0. CySysWdtClearInterrupt(CY_SYS_WDT_COUNTER1_INT); /********************************************************************/ // Stores the previous state of the BLE module. static uint8 previousState = 0xFF; // If in advertising mode, flash the LED. if(CyBle_GetState() == CYBLE_STATE_ADVERTISING) { previousState = CYBLE_STATE_ADVERTISING; if(_LED_Green_On) _LED_TurnOffLED(TRUE); else _LED_TurnOnLED(TRUE); } // If state changed, turn all the LEDs off. if (CyBle_GetState() != previousState) _LED_TurnOffLED(TRUE); // If disconnected, turn all the LEDs off. else if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED) { previousState = CYBLE_STATE_DISCONNECTED; _LED_TurnOffLED(TRUE); } // If connected, turn the LED on. else if(CyBle_GetState() == CYBLE_STATE_CONNECTED) { previousState = CYBLE_STATE_CONNECTED; _LED_TurnOffLED(FALSE); _LED_TurnOnLED(TRUE); } /********************************************************************/ // Unlock the WDT registers for modification. CySysWdtUnlock(); // Disable Counter 1 to allow modifications. CySysWdtDisable(CY_SYS_WDT_COUNTER1_MASK); // Reset Counter 1 and give ~3 LFCLK cycles to take effect. CySysWdtResetCounters(CY_SYS_WDT_COUNTER1_RESET); CyDelayUs(WATCHDOG_REG_UPDATE_WAIT_TIME); // Write the Counter 1 match value for 500 millisecond and give // ~3 LFCLK cycles to take effect. CySysWdtWriteMatch(CY_SYS_WDT_COUNTER1, WATCHDOG_COUNTER1_PERIOD); CyDelayUs(WATCHDOG_REG_UPDATE_WAIT_TIME); // Enable Watchdog Counter 1. CySysWdtEnable(CY_SYS_WDT_COUNTER1_MASK); // Lock Watchdog to prevent any further change. CySysWdtLock(); } }