Beispiel #1
0
/**
 * \brief Application entry point for RTC example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_key;

	/* Initialize the SAM system */
	sysclk_init();
	board_init();

	/* Initialize the console uart */
	configure_console();

	/* Output example information */
	puts(STRING_HEADER);

	/* Default RTC configuration, 24-hour mode */
	rtc_set_hour_mode(RTC, 0);

	/* Configure RTC interrupts */
	NVIC_DisableIRQ(RTC_IRQn);
	NVIC_ClearPendingIRQ(RTC_IRQn);
	NVIC_SetPriority(RTC_IRQn, 0);
	NVIC_EnableIRQ(RTC_IRQn);
	rtc_enable_interrupt(RTC, RTC_IER_SECEN | RTC_IER_ALREN);

	/* Refresh display once */
	refresh_display();

	/* Handle keypresses */
	while (1) {

		while (uart_read(CONSOLE_UART, &uc_key));

		/* Set time */
		if (uc_key == 't') {
			gs_ul_state = STATE_SET_TIME;

			do {
				puts("\n\r\n\r Set time(hh:mm:ss): ");
			} while (get_new_time());

			/* If valid input, none of the variables for time is 0xff. */
			if (gs_ul_new_hour != 0xFFFFFFFF && (gs_uc_rtc_time[2] == ':')
					&& (gs_uc_rtc_time[5] == ':')) {
				if (rtc_set_time(RTC, gs_ul_new_hour, gs_ul_new_minute,
						gs_ul_new_second)) {
					puts("\n\r Time not set, invalid input!\r");
				}
			} else {
				gs_uc_rtc_time[2] = ':';
				gs_uc_rtc_time[5] = ':';
				puts("\n\r Time not set, invalid input!\r");
			}

			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			refresh_display();
		}

		/* Set date */
		if (uc_key == 'd') {
			gs_ul_state = STATE_SET_DATE;

			do {
				puts("\n\r\n\r Set date(mm/dd/yyyy): ");
			} while (get_new_date());

			/* If valid input, none of the variables for date is 0xff(ff). */
			if (gs_ul_new_year != 0xFFFFFFFF && (gs_uc_date[2] == '/')
					&& (gs_uc_date[5] == '/')) {
				if (rtc_set_date(RTC, gs_ul_new_year, gs_ul_new_month,
						gs_ul_new_day, gs_ul_new_week)) {
					puts("\n\r Date not set, invalid input!\r");
				}
			} else {
				gs_uc_date[2] = '/';
				gs_uc_date[5] = '/';
				puts("\n\r Time not set, invalid input!\r");
			}

			/* Only 'mm/dd' is input. */
			if (gs_ul_new_month != 0xFFFFFFFF &&
						gs_ul_new_year == 0xFFFFFFFF) {
				puts("\n\r Not Set for no year field!\r");
			}

			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			refresh_display();
		}

		/* Set time alarm */
		if (uc_key == 'i') {
			gs_ul_state = STATE_SET_TIME_ALARM;

			rtc_clear_date_alarm(RTC);

			do {
				puts("\n\r\n\r Set time alarm(hh:mm:ss): ");
			} while (get_new_time());

			if (gs_ul_new_hour != 0xFFFFFFFF && (gs_uc_rtc_time[2] == ':')
					&& (gs_uc_rtc_time[5] == ':')) {
				if (rtc_set_time_alarm(RTC, 1, gs_ul_new_hour,
						1, gs_ul_new_minute, 1, gs_ul_new_second)) {
					puts("\n\r Time alarm not set, invalid input!\r");
				} else {
					printf("\n\r Time alarm is set at %02u:%02u:%02u!",
						(unsigned int)gs_ul_new_hour, (unsigned int)gs_ul_new_minute,
						(unsigned int)gs_ul_new_second);
				}
			} else {
				gs_uc_rtc_time[2] = ':';
				gs_uc_rtc_time[5] = ':';
				puts("\n\r Time not set, invalid input!\r");
			}
			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			gs_ul_alarm_triggered = 0;
			refresh_display();
		}

		/* Set date alarm */
		if (uc_key == 'm') {
			gs_ul_state = STATE_SET_DATE_ALARM;

			rtc_clear_time_alarm(RTC);

			do {
				puts("\n\r\n\r Set date alarm(mm/dd/yyyy): ");
			} while (get_new_date());

			if (gs_ul_new_year != 0xFFFFFFFF && (gs_uc_date[2] == '/')
					&& (gs_uc_date[5] == '/')) {
				if (rtc_set_date_alarm(RTC, 1, gs_ul_new_month, 1,
						gs_ul_new_day)) {
					puts("\n\r Date alarm not set, invalid input!\r");
				} else {
					printf("\n\r Date alarm is set on %02u/%02u/%4u!",
							(unsigned int)gs_ul_new_month, (unsigned int)gs_ul_new_day,
							(unsigned int)gs_ul_new_year);
				}
			} else {
				gs_uc_date[2] = '/';
				gs_uc_date[5] = '/';
				puts("\n\r Date alarm not set, invalid input!\r");
			}
			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			gs_ul_alarm_triggered = 0;
			refresh_display();
		}

#if ((SAM3S8) || (SAM3SD8) || (SAM4S) || (SAM4C) || (SAM4CP) || (SAM4CM))
		/* Generate Waveform */
		if (uc_key == 'w') {
			gs_ul_state = STATE_WAVEFORM;
			puts("\n\rMenu:\n\r"
					"  0 - No Waveform\n\r"
					"  1 - 1 Hz square wave\n\r"
					"  2 - 32 Hz square wave\n\r"
					"  3 - 64 Hz square wave\n\r"
					"  4 - 512 Hz square wave\n\r"
					"  5 - Toggles when alarm flag rise\n\r"
					"  6 - Copy of the alarm flag\n\r"
					"  7 - Duty cycle programmable pulse\n\r"
					"  8 - Quit\r");

			while (1) {
				while (uart_read(CONSOLE_UART, &uc_key));

				if ((uc_key >= '0') && (uc_key <= '7')) {
					rtc_set_waveform(RTC, 0, char_to_digit(uc_key));
				}

				if (uc_key == '8') {
					gs_ul_state = STATE_MENU;
					gs_ul_menu_shown = 0;
					refresh_display();
					break;
				}
			}
		}
#endif
		/* Clear trigger flag */
		if (uc_key == 'c') {
			gs_ul_alarm_triggered = 0;
			gs_ul_menu_shown = 0;
			refresh_display();
		}

	}

}
int main(void)
{
	uint8_t ucKey;

	/* Disable watchdog */
	wdt_disable();

	/* Configure console */
	board_cfg_console();

	/* Output example information */
	printf("\r\n\r\n\r\n");
	printf("-- RTC Example " SOFTPACK_VERSION " --\r\n");
	printf("-- " BOARD_NAME "\r\n");
	printf("-- Compiled: " __DATE__ " " __TIME__ " --\n\r");

	// put 25 °C as a default temp, if there is no temprature sensor
	Temperature = 25;

	printf("Configure TC.\r\n");
	configure_tc();

	/* Default RTC configuration */
	rtc_set_hour_mode(0);	/* 24-hour mode */
	struct _time empty_time = {0,0,0};
	if (rtc_set_time_alarm(&empty_time)) {
		printf("\r\n Disable time alarm fail!");
	}
	struct _date empty_date = {0,0,0};
	if (rtc_set_date_alarm(&empty_date)) {
		printf("\r\n Disable date alarm fail!");
	}

	/* Configure RTC interrupts */
	rtc_enable_it(RTC_IER_SECEN | RTC_IER_ALREN);
	aic_set_source_vector(ID_SYSC, sysc_handler);
	aic_enable(ID_SYSC);

	/* Refresh display once */
	_RefreshDisplay();
	new_time.hour = 0;
	new_time.min = 0;
	new_time.sec = 30;
	rtc_set_time_alarm(&new_time);
	bMenuShown = 0;
	alarmTriggered = 0;
	rtc_calibration(Temperature);

	/* Handle keypresses */
	while (1) {
		ucKey = console_get_char();

		/* set time */
		if (ucKey == 't') {
			bState = STATE_SET_TIME;
			aic_disable(ID_TC0);

			do {
				printf("\r\n\r\n Set time(hh:mm:ss): ");
			} while (get_new_time());

			/* if valid input, none of variable for time is 0xff */
			if (new_time.hour != 0xFF) {
				if (rtc_set_time (&new_time)) {
					printf
					    ("\r\n Time not set, invalid input!\n\r");
				}
			}

			bState = STATE_MENU;
			bMenuShown = 0;
			_RefreshDisplay();
			CountDownTimer = 0;
			aic_enable(ID_TC0);
		}

		/* clock calibration */
		else if (ucKey == 'p') {

			rtc_calibration(30);

			bState = STATE_MENU;
			bMenuShown = 0;
			_RefreshDisplay();
		}

		/* set date */
		else if (ucKey == 'd') {
			bState = STATE_SET_DATE;
			aic_disable(ID_TC0);

			do {
				printf("\r\n\r\n Set date(mm/dd/yyyy): ");
			} while (get_new_date());

			/* if valid input, none of variable for date is 0xff(ff) */
			if (new_date.year != 0xFFFF) {
				if (rtc_set_date(&new_date)) {
					printf
					    ("\r\n Date not set, invalid input!\r\n");
				}
			}

			/* only 'mm/dd' inputed */
			if (new_date.month != 0xFF && new_date.year == 0xFFFF) {
				printf("\r\n Not Set for no year field!\r\n");
			}

			bState = STATE_MENU;
			bMenuShown = 0;
			CountDownTimer = 0;
			aic_enable(ID_TC0);
			_RefreshDisplay();
		}

		/* set time alarm */
		else if (ucKey == 'i') {
			bState = STATE_SET_TIME_ALARM;
			aic_disable(ID_TC0);

			do {
				printf("\r\n\r\n Set time alarm(hh:mm:ss): ");
			} while (get_new_time());

			if (new_time.hour != 0xFF) {
				if (rtc_set_time_alarm(&new_time)) {
					printf
					    ("\r\n Time alarm not set, invalid input!\r\n");
				} else {
					printf
					    ("\r\n Time alarm is set at %02d:%02d:%02d!",
					     new_time.hour, new_time.min, new_time.sec);
				}
			}
			bState = STATE_MENU;
			bMenuShown = 0;
			alarmTriggered = 0;
			CountDownTimer = 0;
			aic_enable(ID_TC0);
			_RefreshDisplay();
		}

		/* set date alarm */
		else if (ucKey == 'm') {
			bState = STATE_SET_DATE_ALARM;
			aic_disable(ID_TC0);

			do {
				printf("\r\n\r\n Set date alarm(mm/dd/): ");
			} while (get_new_date());

			if (new_date.year == 0xFFFF && new_date.month != 0xFF) {
				if (rtc_set_date_alarm(&new_date)) {
					printf
					    ("\r\n Date alarm not set, invalid input!\r\n");
				} else {
					printf
					    ("\r\n Date alarm is set on %02d/%02d!",
					     new_date.month, new_date.day);
				}

			}
			bState = STATE_MENU;
			bMenuShown = 0;
			alarmTriggered = 0;
			CountDownTimer = 0;
			aic_enable(ID_TC0);
			_RefreshDisplay();
		}

		/* clear trigger flag */
		else if (ucKey == 'c') {
			alarmTriggered = 0;
			bMenuShown = 0;
			_RefreshDisplay();
		}

		/* quit */
		else if (ucKey == 'q') {
			break;
		}
	}

	return 0;
}