unsigned char wdt_triggered(void){ reset_cause_t resetCause; resetCause = reset_cause_get_causes(); if(resetCause &= CHIP_RESET_CAUSE_WDT){ return TRUE; }else{ return FALSE; } }
/** * \brief This function runs a SHA204 Wakeup / Sleep test. * * This test wakes up the device, reads its Wakeup response, * and sends a Sleep command. It then sends a command and * expects a response timeout to verify that the device has * gone to sleep. * * \param test current test case */ static void test_sha204_wakeup(const struct test_case *test) { uint8_t sha204_status = SHA204_SUCCESS; // Catch watchdog timeout in case no Security Xplained board is connected. if ((reset_cause_get_causes() & CHIP_RESET_CAUSE_WDT) != CHIP_RESET_CAUSE_WDT) { reset_cause_clear_causes(CHIP_RESET_CAUSE_POR | CHIP_RESET_CAUSE_EXTRST | CHIP_RESET_CAUSE_BOD_CPU | CHIP_RESET_CAUSE_OCD | CHIP_RESET_CAUSE_SOFT | CHIP_RESET_CAUSE_SPIKE); } else { wdt_disable(); reset_cause_clear_causes(CHIP_RESET_CAUSE_WDT); test_assert_false(test, sha204_status == SHA204_SUCCESS, "No Device."); return; } /* Start watchdog timer. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_250CLK); // 250 ms wdt_enable(); uint8_t response[DEVREV_RSP_SIZE]; success = false; sha204_status = sha204c_wakeup(response); // The TWI_M driver is not hanging. We can disable the watchdog now. wdt_disable(); test_assert_true(test, sha204_status == SHA204_SUCCESS, "Sending Wakeup token failed."); sha204_status = sha204p_sleep(); test_assert_true(test, sha204_status == SHA204_SUCCESS, "Sending Sleep command failed."); // Make sure the device is asleep. The code below works only for TWI, not for SWI, // because there is no acknowledging of a TWI address for SWI. uint8_t command[DEVREV_COUNT] = {DEVREV_COUNT, SHA204_DEVREV, 0, 0, 0, 0x03, 0x5d}; sha204_status = sha204p_send_command(sizeof(command), command); test_assert_false(test, sha204_status == SHA204_SUCCESS, "Device is not asleep."); success = true; }
/** \brief Main function. */ int main(void) { uint8_t i; /* Initialize the board hardware and system clocks. */ board_init(); sysclk_init(); /* Detection of all RESET excepted WDT RESET. */ if (reset_cause_get_causes() & ~CHIP_RESET_CAUSE_WDT) { /* Wait for 2 s. */ delay_ms(2000); state_flag = START_OF_PROG; reset_cause_clear_causes( CHIP_RESET_CAUSE_POR | CHIP_RESET_CAUSE_EXTRST | CHIP_RESET_CAUSE_BOD_CPU | CHIP_RESET_CAUSE_OCD | CHIP_RESET_CAUSE_SOFT | CHIP_RESET_CAUSE_SPIKE); } else { reset_cause_clear_causes(CHIP_RESET_CAUSE_WDT); } while (true) { led_display(state_flag); switch (state_flag) { case START_OF_PROG: /* Writing test. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_8CLK); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_8CLK) { state_flag = ERROR_STATE; break; } wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_250CLK); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_250CLK) { state_flag = ERROR_STATE; break; } /* Wait for 2 s. */ delay_ms(2000); state_flag = WDT_MCU_RESET; break; case WDT_MCU_RESET: /* Wait for 2 s. */ delay_ms(2000); state_flag = REFRESH_NO_WIN; wdt_reset_mcu(); break; case REFRESH_NO_WIN: /* Enable WDT 500 ms. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_500CLK); wdt_enable(); for (i = 0; i < 8; i++) { /* Wait for 8x 250 ms = 2 s. */ delay_ms(250); wdt_reset(); } wdt_disable(); state_flag = REFRESH_WINDOW; break; case REFRESH_WINDOW: /* Enable Window 250 ms & WDT 500 ms. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_500CLK); wdt_enable(); if (!(wdt_set_window_period(WDT_WINDOW_PERIOD_250CLK))) { state_flag = ERROR_STATE; break; } if (!(wdt_enable_window_mode())) { state_flag = ERROR_STATE; break; } for (i = 0; i < 4; i++) { /* Wait for 500 ms. */ delay_ms(500); wdt_reset(); } wdt_disable(); state_flag = WDT_RST_NO_WIN; break; case WDT_RST_NO_WIN: state_flag = WDT_RST_WINDOW; /* Enable WDT 2 s. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_2KCLK); wdt_enable(); while (true) { /* Wait for Watchdog reset. */ } break; case WDT_RST_WINDOW: state_flag = WDT_RST_RFSH_W; /* Enable Window 1 s & WDT 1 s. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_1KCLK); wdt_enable(); if (!(wdt_set_window_period(WDT_WINDOW_PERIOD_1KCLK))) { state_flag = ERROR_STATE; break; } if (!(wdt_enable_window_mode())) { state_flag = ERROR_STATE; break; } while (true) { /* Wait for Watchdog reset. */ } break; case WDT_RST_RFSH_W: state_flag = END_OF_PROG; /* Enable Window 4 s & WDT 250 ms. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_250CLK); wdt_enable(); if (!(wdt_set_window_period(WDT_WINDOW_PERIOD_4KCLK))) { state_flag = ERROR_STATE; break; } if (!(wdt_enable_window_mode())) { state_flag = ERROR_STATE; break; } /* Wait for 2 s. */ delay_ms(2000); wdt_reset(); while (true) { /* Wait for Watchdog reset. */ } break; case ERROR_STATE: while (true) { led_display(ERROR_STATE); /* Wait for 500 ms. */ delay_ms(500); /* Blinking. */ led_display(~ERROR_STATE); /* Wait for 500 ms. */ delay_ms(500); } break; case END_OF_PROG: default: /* Wait for 2 s. */ delay_ms(2000); reset_do_soft_reset(); break; } } }
/** \brief Main function. */ int main(void) { uint8_t delay_counter; volatile uint16_t delay; /* Initialize the board. * The board-specific conf_board.h file contains the configuration of * the board initialization. */ sysclk_init(); board_init(); /* Detection of all RESET except WDT RESET. */ if ((reset_cause_get_causes() & CHIP_RESET_CAUSE_WDT) != CHIP_RESET_CAUSE_WDT) { state_flag = START_OF_PROG; reset_cause_clear_causes(CHIP_RESET_CAUSE_POR | CHIP_RESET_CAUSE_EXTRST | CHIP_RESET_CAUSE_BOD_CPU); } else { reset_cause_clear_causes(CHIP_RESET_CAUSE_WDT); } wdt_disable(); while (true) { switch (state_flag) { case START_OF_PROG: /* Writing test. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_2KCLK); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_2KCLK) { state_flag = ERROR_STATE; break; } wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_1024KCLK); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_1024KCLK) { state_flag = ERROR_STATE; break; } /* Wait for 2 s. */ delay = 2000; delay_ms(delay); state_flag = WDT_MCU_RESET; break; case WDT_MCU_RESET: state_flag = REFRESH_WDT; wdt_reset_mcu(); break; case REFRESH_WDT: /* Enable WDT 500 ms. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_64KCLK); wdt_enable(SYSTEM_RESET_MODE); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_64KCLK) { state_flag = ERROR_STATE; break; } for (delay_counter = 0; delay_counter < 8; delay_counter++) { /* Wait for 8x 250 ms = 2 s. */ delay = 250; delay_ms(delay); wdt_reset(); } wdt_disable(); state_flag = WDT_RST; break; case WDT_RST: state_flag = WDT_INTERRUPT; /* Enable WDT 2 s. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_256KCLK); wdt_enable(SYSTEM_RESET_MODE); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_256KCLK) { state_flag = ERROR_STATE; break; } while (true) { /* Wait for Watchdog reset. */ } /* No break is needed */ case WDT_INTERRUPT: /* Enable WDT 250 ms. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_32KCLK); wdt_set_interrupt_callback(wdt_timer_callback); wdt_enable(INTERRUPT_MODE); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_32KCLK) { state_flag = ERROR_STATE; break; } cpu_irq_enable(); /* Wait for interrupt to get triggered */ delay = 400; delay_ms(delay); wdt_disable(); break; case WDT_RST_INTERRUPT: /* Enable WDT 125 ms. */ wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_16KCLK); wdt_set_interrupt_callback(wdt_timer_callback); wdt_enable(INTERRUPT_SYSTEM_RESET_MODE); if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_16KCLK) { state_flag = ERROR_STATE; break; } /* Wait for 200 ms. */ delay = 200; delay_ms(delay); wdt_reset(); while (true) { /* Wait for Watchdog reset. */ } /* No break is needed */ case ERROR_STATE: wdt_disable(); while (true) { LED_On(LED_PIN); /* Wait for 500 ms. */ delay = 500; delay_ms(delay); /* Blinking. */ LED_Off(LED_PIN); /* Wait for 500 ms. */ delay = 500; delay_ms(delay); } /* No break is needed */ case END_OF_PROG: default: LED_On(LED_PIN); while (true) { } /* No break is needed */ } } }