void m48_watchdog_arm(int usec) { int mpy, res; SYS_TOD_UNPROTECT(); if (usec == 0) { res = 0; mpy = 0; } else if (usec < 2000000) { /* Resolution: 1/16s if below 2s */ res = 0; mpy = (usec + 62499) / 62500; } else if (usec < 8000000) { /* Resolution: 1/4s if below 8s */ res = 1; mpy = (usec + 249999) / 250000; } else if (usec < 32000000) { /* Resolution: 1s if below 32s */ res = 2; mpy = (usec + 999999) / 1000000; } else { /* Resolution: 4s up to 124s */ res = 3; mpy = (usec + 3999999) / 4000000; if (mpy > 31) mpy = 31; } M48_ADDR[WATCH] = (0x80 | /* Steer to RST signal (IRQ = N/C) */ mpy << 2 | res); SYS_TOD_PROTECT(); }
int m48_tod_get(int *year, /* 1980-2079 */ int *month, /* 01-12 */ int *day, /* 01-31 */ int *hour, /* 00-23 */ int *minute, /* 00-59 */ int *second) /* 00-59 */ { int y; SYS_TOD_UNPROTECT(); M48_ADDR[CONTROL] |= 0x40; /* Set READ bit */ y = from_bcd(M48_ADDR[YEAR]); *year = y < 80 ? 2000 + y : 1900 + y; *month = from_bcd(M48_ADDR[MONTH]); *day = from_bcd(M48_ADDR[DAY]); /* day_of_week = M48_ADDR[DAY_OF_WEEK] & 0xf; */ *hour = from_bcd(M48_ADDR[HOUR]); *minute = from_bcd(M48_ADDR[MINUTE]); *second = from_bcd(M48_ADDR[SECOND] & 0x7f); M48_ADDR[CONTROL] &= ~0x40; /* Clear READ bit */ SYS_TOD_PROTECT(); return 0; }
int m48_tod_set(int year, /* 1980-2079 */ int month, /* 01-12 */ int day, /* 01-31 */ int hour, /* 00-23 */ int minute, /* 00-59 */ int second) /* 00-59 */ { SYS_TOD_UNPROTECT(); M48_ADDR[CONTROL] |= 0x80; /* Set WRITE bit */ M48_ADDR[YEAR] = to_bcd(year % 100); M48_ADDR[MONTH] = to_bcd(month); M48_ADDR[DAY] = to_bcd(day); M48_ADDR[DAY_OF_WEEK] = day_of_week(year, month, day) + 1; M48_ADDR[HOUR] = to_bcd(hour); M48_ADDR[MINUTE] = to_bcd(minute); M48_ADDR[SECOND] = to_bcd(second); M48_ADDR[CONTROL] &= ~0x80; /* Clear WRITE bit */ SYS_TOD_PROTECT(); return 0; }
int m48_tod_init(void) { SYS_TOD_UNPROTECT(); M48_ADDR[CONTROL] = 0; M48_ADDR[WATCH] = 0; M48_ADDR[INTCTL] = 0; /* * If the oscillator is currently stopped (as on a new part shipped * from the factory), start it running. * * Here is an example of the TOD bytes on a brand new M48T59Y part: * 00 00 00 00 00 00 00 00 00 88 8c c3 bf c8 f5 01 */ if (M48_ADDR[SECOND] & 0x80) M48_ADDR[SECOND] = 0; /* Is battery low */ if ( M48_ADDR[FLAGS] & 0x10) { printf("NOTICE: Battery low on Real-Time Clock (replace SNAPHAT).\n"); } SYS_TOD_PROTECT(); return 0; }
/* Write to NVRAM */ void nvram_write(long dest, const void *src, size_t count) { int i; volatile unsigned char* d = (unsigned char*)dest; volatile unsigned char* s = (unsigned char*)src; SYS_TOD_UNPROTECT(); for( i = 0; i < count;i++) d[i] = s[i]; SYS_TOD_PROTECT(); }
STATUS m48_tod_init(UINT8 *addr) { m48BaseAddr = addr; SYS_TOD_UNPROTECT(); M48_ADDR[CONTROL] = 0; M48_ADDR[WATCH] = 0; M48_ADDR[INTCTL] = 0; /* * If the oscillator is currently stopped (as on a new part shipped * from the factory), start it running. * * Here is an example of the TOD bytes on a brand new M48T59Y part: * 00 00 00 00 00 00 00 00 00 88 8c c3 bf c8 f5 01 */ if (M48_ADDR[SECOND] & 0x80) M48_ADDR[SECOND] = 0; SYS_TOD_PROTECT(); return OK; }