/**
 * \brief Run WDT unit tests
 *
 * Initializes the system and serial output, then sets up the
 * WDT unit test suite and runs it.
 */
int main(void)
{
	/* Check whether reset cause was Watchdog */
#if (SAML21)
	wdr_flag = (system_get_reset_cause() & RSTC_RCAUSE_WDT);
#else
	wdr_flag = (system_get_reset_cause() & PM_RCAUSE_WDT);
#endif
	system_init();

	/* Reset the Watchdog count */
	wdt_reset_count();

	struct wdt_conf config_wdt;
	/* Get the Watchdog default configuration */
	wdt_get_config_defaults(&config_wdt);
	if(wdr_flag) {
		config_wdt.enable = false;
	}
	/* Set the desired configuration */
#if !(SAML21)
	config_wdt.clock_source         = CONF_WDT_GCLK_GEN;
#endif
	config_wdt.timeout_period       = CONF_WDT_TIMEOUT_PERIOD;
	config_wdt.early_warning_period = CONF_WDT_EARLY_WARNING_PERIOD;
	wdt_set_config(&config_wdt);

	cdc_uart_init();

	DEFINE_TEST_CASE(wdt_early_warning_test, NULL,
			run_wdt_early_warning_test, wait_for_wdt_reset,
			"WDT Early Warning Test");

	DEFINE_TEST_CASE(reset_cause_test, NULL,
			run_reset_cause_test, NULL,
			"Confirming Watchdog Reset");

	/* Put test case addresses in an array */
	DEFINE_TEST_ARRAY(wdt_tests) = {
			&wdt_early_warning_test,
			&reset_cause_test,
			};

	/* Define the test suite */
	DEFINE_TEST_SUITE(wdt_suite, wdt_tests,
			"SAM WDT driver test suite");

	/* Run all tests in the suite*/
	test_suite_run(&wdt_suite);

	while (1) {
		/* Intentionally left empty */
	}

}
Пример #2
0
int main(void)
{
	system_init();

	//! [setup_init]
	configure_wdt();
	//! [setup_init]

	//! [main]
	//! [main_1]
	enum system_reset_cause reset_cause = system_get_reset_cause();
	//! [main_1]

	//! [main_2]
	if (reset_cause == SYSTEM_RESET_CAUSE_WDT) {
		port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
	}
	else {
		port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
	}
	//! [main_2]

	//! [main_3]
	while (true) {
	//! [main_3]
		//! [main_4]
		if (port_pin_get_input_level(BUTTON_0_PIN) == false) {
		//! [main_4]
		//! [main_5]
			port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);

			wdt_reset_count();
		//! [main_5]
		}
	}
	//! [main]
}
/* loads stored state from persistent storage and sets up correct boot parameters */
void configure_state_from_reboot(void) {
	// send first read command
	read_state_from_storage();

	#ifdef OVERRIDE_INIT_SAT_STATE
		current_sat_state =		OVERRIDE_INIT_SAT_STATE;
		current_task_states =	OVERRIDE_INIT_TASK_STATES;
	#else

	// based on satellite state, set initial state
	// (it will actually be set as tasks come online)
	// (this is done differently than during normal satellite operation,
	// because we're still initializing and tasks can't be simply suspended
	// right now (RTOS isn't even running yet). So, we configure what
	// needs to be done and configure tasks as they boot up and report
	// to the function below that they're ready)
	sat_state_t state_at_reboot = cache_get_sat_state();
	if (state_at_reboot == INITIAL
		|| state_at_reboot == ANTENNA_DEPLOY
		|| state_at_reboot == HELLO_WORLD) {

		current_sat_state =		INITIAL;
		// the distinction between these two is related to T_STATE_ANY;
		// we use boot_task_states to explicitly set state for all tasks on boot
		// (so at the end of this it won't contain any T_STATE_ANYs),
		// while current_task_states signifies the current abstract state
		current_task_states =	INITIAL_TASK_STATES;

	} else {
		current_sat_state =		IDLE_NO_FLASH;
		current_task_states =	IDLE_NO_FLASH_TASK_STATES;
	}
	#endif

	// always start antenna deploy task
	current_task_states.states[ANTENNA_DEPLOY_TASK] = true;
	
	// add any errors we can from MRAM cache
	// (NOTE; no one should've logged any yet, or else they may be overwritten!)
	populate_error_stacks(&error_equistack);
	
	// if the satellite restarted because of the watchdog, log that as an error so we know
	enum system_reset_cause cause = system_get_reset_cause();
	if (cause == SYSTEM_RESET_CAUSE_WDT) {
		log_error(ELOC_WATCHDOG, ECODE_WATCHDOG_DID_KICK, true);
	} else if (cause == SYSTEM_RESET_CAUSE_SOFTWARE) {
		log_error(ELOC_WATCHDOG, ECODE_SOFTWARE_RESET, false); // mostly for debug; hopefully no aliens are hacking us
	} else if (cause == SYSTEM_RESET_CAUSE_EXTERNAL_RESET) {
		log_error(ELOC_WATCHDOG, ECODE_SAT_RESET, true);
	}

	// if we had to rewrite program memory due to corruption, log a low-pri error
	if (cache_get_prog_mem_rewritten()) {
		log_error(ELOC_BOOTLOADER, ECODE_REWROTE_PROG_MEM, false);
		update_sat_event_history(0, 0, 0, 0, 0, 0, 1);  // rare enough that the double-write here is fine
	}

	// note we've rebooted
	increment_reboot_count();
	
	// initial hardware settings (last because they have delays)
	setRadioState(false, false); // turn radio off and don't confirm (won't confirm going off)
}