/*
 * Set the RTC
 */
int rtc_set (struct rtc_time *tmp){

	unsigned long time;
	unsigned i;

	DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);

	if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
		printf("WARNING: year should be between 1970 and 2069!\n");

	time = mktime(tmp->tm_year, tmp->tm_mon,
			tmp->tm_mday, tmp->tm_hour,
			tmp->tm_min, tmp->tm_sec);

	DEBUGR ("Set RTC s since 1.1.1970: %ld (0x%02lx)\n", time, time);

	/* write to RTC_TOD_CNT_BYTEn_ADDR */
	for (i = 0; i <= 3; i++) {
		rtc_write_raw(RtcTodAddr[i], (unsigned char)(time & 0xff));
		time = time >> 8;
	}

	/* Start clock */
	rtc_write(RTC_CTL_ADDR, RTC_CTL_BIT_EN_OSC, FALSE);

	return 0;
}
Beispiel #2
0
void rtc_init(void)
{
	printf("Starting RTC/Watchdog (DS1374) Initialization...\n");

	uchar res;

	// Read Trickle Charger config register
	res = rtc_read(RTC_TCS_DS_ADDR);

	if (ds1374_wd_enable) {
		// Read Watchdog/Alarm Counter
		int cnt = 0x0B4000; // 180 s * 4096
		uchar wd_alm_byte0, wd_alm_byte1, wd_alm_byte2;
		wd_alm_byte0 = rtc_read(RTC_WD_ALM_CNT_BYTE0_ADDR);
		wd_alm_byte1 = rtc_read(RTC_WD_ALM_CNT_BYTE1_ADDR);
		wd_alm_byte2 = rtc_read(RTC_WD_ALM_CNT_BYTE2_ADDR);
		printf("WD/ALARM Counter (Power-up): %x %x %x\n",wd_alm_byte2, wd_alm_byte1, wd_alm_byte0);

		// Periodic Alarm
		//rtc_write(RTC_CTL_ADDR, (RTC_CTL_BIT_WACE | RTC_CTL_BIT_AIE),TRUE);

		// Write to Watchdog/Alarm Counter Register
		rtc_write_raw(RTC_WD_ALM_CNT_BYTE0_ADDR, (cnt & 0xff));
		rtc_write_raw(RTC_WD_ALM_CNT_BYTE1_ADDR, ((cnt >> 8) & 0xff));
		rtc_write_raw(RTC_WD_ALM_CNT_BYTE2_ADDR, ((cnt >> 16) & 0xff));

		wd_alm_byte0 = rtc_read(RTC_WD_ALM_CNT_BYTE0_ADDR);
		wd_alm_byte1 = rtc_read(RTC_WD_ALM_CNT_BYTE1_ADDR);
		wd_alm_byte2 = rtc_read(RTC_WD_ALM_CNT_BYTE2_ADDR);
		printf("Set WD/ALARM Counter to %02x %02x %02x / 4096 s\n",
				wd_alm_byte2, wd_alm_byte1, wd_alm_byte0);

		//Enable Watchdog Interrupt
		rtc_write(RTC_CTL_ADDR, (RTC_CTL_BIT_WACE | RTC_CTL_BIT_WD_ALM),TRUE);
		res = rtc_read(RTC_CTL_ADDR);
		if((res & 0x60) == 0x60)
			printf("Watchdog enabled!\n");
	} else {