📄 ds1501.c
字号:
** This routine prints the current RTC date.** RETURNS: N/A**/ void rtcDateShow(void){ rtcSpIoctl (GET_DATE); logMsg ("Date: %s, %02d-%s-%4d.\n", (int)dayTable[rtcDateTime.day], rtcDateTime.date, (int)monthTable[rtcDateTime.month], ((rtcDateTime.century * 100) + rtcDateTime.year),0,0); }/******************************************************************************** rtcTimeShow - print the time** This routine prints the current RTC time.** RETURNS: N/A**/ void rtcTimeShow(void){ rtcSpIoctl (GET_TIME); if (rtcDateTime.hourMode == 24) logMsg("Time = %02d:%02d:%02d MIL\n", rtcDateTime.hour, rtcDateTime.minute, rtcDateTime.second, 0, 0, 0); else /* 12 hour mode */ { if (rtcDateTime.hour > 12) logMsg("Time = %02d:%02d:%02d PM\n", rtcDateTime.hour - 12, rtcDateTime.minute, rtcDateTime.second, 0, 0, 0); else if (rtcDateTime.hour == 12) logMsg("Time = %02d:%02d:%02d PM\n", 12, rtcDateTime.minute, rtcDateTime.second, 0, 0, 0); else if (rtcDateTime.hour == 0) logMsg("Time = %02d:%02d:%02d AM\n", 12, rtcDateTime.minute, rtcDateTime.second, 0, 0, 0); else logMsg("Time = %02d:%02d:%02d AM\n", rtcDateTime.hour, rtcDateTime.minute, rtcDateTime.second, 0, 0, 0); }}#ifdef RTC_DEBUG/******************************************************************************** rtcRegShow - dump the RTC command registers** NOMANUAL*/void rtcRegShow(void){ UINT8 val; val = rtcRegRead(DS1501_CTRLA_OFFSET); printf("CTRLA = 0x%x\n", val); val = rtcRegRead(DS1501_CTRLB_OFFSET); printf("CTRLB = 0x%x\n", val);}/******************************************************************************** rtcDateTimeRegShow - dump the RTC date, time, and alarm registers** NOMANUAL*/void rtcDateTimeRegShow(void){ UINT8 val; val = rtcRegReadBcd(DS1501_HOURS_OFFSET); printf("HOUR = %d\n", val); val = rtcRegReadBcd(DS1501_MINUTES_OFFSET); printf("MINUTE = %d\n",val); val = rtcRegReadBcd(DS1501_SECONDS_OFFSET); printf("SECOND = %d\n",val); val = rtcRegReadBcd(DS1501_HOURS_ALARM_OFFSET); printf("ALARM HOUR = %d\n", val); val = rtcRegReadBcd(DS1501_MINUTES_ALARM_OFFSET); printf("ALARM MINUTE = %d\n",val); val = rtcRegReadBcd(DS1501_SECONDS_ALARM_OFFSET); printf("ALARM SECOND = %d\n",val); val = rtcRegReadBcd(DS1501_CENTURY_OFFSET); printf("CENTURY = %d\n", val); val = rtcRegReadBcd(DS1501_YEAR_OFFSET); printf("YEAR = %d\n", val); val = rtcRegReadBcd(DS1501_MONTH_OFFSET); printf("MONTH = %d\n", val); val = rtcRegReadBcd(DS1501_DATE_OFFSET); printf("DATE = %d\n", val); val = rtcRegReadBcd(DS1501_DAY_OFFSET); printf("DAY = %d\n", val);}#endif /* RTC_DEBUG */ #ifndef CPU1_IMAGE /* CPU0 only *//*********************************************************************** DS1501Setup** Description :* Initializes the RTC fully or partially depending on input* flag value and the RTC's current state. Minimum* initialization includes disabling and clearing all* interrupts. Full initialization includes also setting default* values and mode for time, date and alarm registers and* clearing additional flags. See code comments for more* details.** Inputs :* force reset flag; originally always = FALSE.** Return Value : none.** Comments :* It is intended that this function be called, with forceSetup* equal to false, during initialization where the card may have* lost power or been forced to reset while the RTC remained* running. This will allow the RTC to be set to a valid state* on fresh power up while still preserving the time/date data* and modes in the event that the RTC has standby power or the* card is being reset without being powered off/on.** The RTC can not keep time when the oscillator disabled.* If the condition exists, it is* appropriate to fully initialize the RTC. Also, since the BSP* never leaves the clock update in the inhibited mode,* it is assumed that finding the RTC in this state* means that it is also safe to initialize the RTC to a default* time.** Assumption : none.**/static void DS1501Setup (BOOL forceSetup){ BOOL fullInit = FALSE; UINT8 ctrlA, ctrlB, month; /* * First determine if a full initialization is required or if a * partial initialization is desireable (see function comments). */ ctrlA = rtcRegRead(DS1501_CTRLA_OFFSET); ctrlB = rtcRegRead(DS1501_CTRLB_OFFSET); month = rtcRegRead(DS1501_MONTH_OFFSET); if ((forceSetup) || (month & DS1501_MONTH_NOT_EOSC) || /* oscillator off */ (ctrlA & DS1501_CTRLA_WDF) || /* watch dog flag */ !(ctrlB & DS1501_CTRLB_TE)) /* update transfer inhibited */ { fullInit = TRUE; } /* Set control registers A & B */ rtcRegWrite(DS1501_CTRLA_OFFSET, 0); rtcRegWrite(DS1501_CTRLB_OFFSET, DS1501_CTRLB_TE); /* * For Full initialization set all time and alarm registers to zero * and set the date to Wednesday 1 Jan 2003. Start clock on * completion of setup. */ if (fullInit) { rtcRegWrite(DS1501_SECONDS_OFFSET, 0); rtcRegWrite(DS1501_MINUTES_OFFSET, 0); rtcRegWrite(DS1501_HOURS_OFFSET, 0); rtcRegWrite(DS1501_DAY_OFFSET, 4); /* Wednesday */ rtcRegWrite(DS1501_DATE_OFFSET, 1); /* 1st */ rtcRegWrite(DS1501_MONTH_OFFSET, 1); /* January */ rtcRegWrite(DS1501_YEAR_OFFSET, 3); rtcRegWrite(DS1501_CENTURY_OFFSET, 0x20); /* 2003 */ rtcRegWrite(DS1501_SECONDS_ALARM_OFFSET, 0); rtcRegWrite(DS1501_MINUTES_ALARM_OFFSET, 0); rtcRegWrite(DS1501_HOURS_ALARM_OFFSET, 0); rtcRegWrite(DS1501_DATE_ALARM_OFFSET, 0x80); /* match hh:mm:ss */ rtcRegWrite(DS1501_WD_10TH_100TH_SEC_OFFSET, 0); rtcRegWrite(DS1501_WD_10_1_SEC_OFFSET, 0); } rtcRegRead(DS1501_CTRLA_OFFSET); /* clear interrupts */}#endif/******************************************************************************** rtcClose - Closes the driver*/LOCAL int rtcClose (RTC_DEV *pRtcDv){ return (OK);}/******************************************************************************** rtcDevCreate - creates driver** This routine initializes the driver and adds it to the I/O system** RETURNS: OK, or ERROR if the driver could not be added to the device list.** NOMANUAL*/LOCAL STATUS rtcDevCreate (void){ /* test if driver already installed */ if (rtcDrvNum <= 0) { (void) errnoSet (S_ioLib_NO_DRIVER); return (ERROR); } /* if there is a device already created, don't do it */ if (rtcDv.created) return (OK); rtcDv.created = TRUE; rtcRegRead(DS1501_CTRLA_OFFSET); /* clear interrupts */ intConnect ((void *)RTC, rtcInt, 0); intEnable (RTC); return (iosDevAdd ((DEV_HDR *) &rtcDv, RTC_DEVICE, rtcDrvNum));}/******************************************************************************** rtcDrv - installs driver** This routine adds the drivers routines for I/O functions* into the driver table. ** RETURNS: OK, or ERROR if the driver was unable to be installed** NOMANUAL*/LOCAL STATUS rtcDrv (void){ if (rtcDrvNum > 0) return (OK); rtcDrvNum = iosDrvInstall (rtcOpen, (FUNCPTR) NULL, rtcOpen, rtcClose, (FUNCPTR) NULL, (FUNCPTR) NULL, rtcIoctl); return (rtcDrvNum == ERROR ? ERROR : OK);}/******************************************************************************** rtcInt - rtc interrupt handler ** This routine handles the RTC alarm, kickstart, and watchdog interrupts.* Upon the alarm interrupt, and user handler is invokded if it is installed.** RETURNS: N/A** NOMANUAL*/LOCAL void rtcInt (void){ UINT8 ctrlA = rtcRegRead(DS1501_CTRLA_OFFSET); if (ctrlA & DS1501_CTRLA_TDF) { if (rtcAlarmRoutine != NULL) rtcAlarmRoutine(rtcAlarmArg); else { logMsg("Default RTC Alarm Handler\n",0,0,0,0,0,0);#ifdef INCLUDE_RTC_SHOW rtcTimeShow();#endif } } else if (ctrlA & DS1501_CTRLA_KSF) logMsg("Default RTC Kickstart handler\n",0,0,0,0,0,0); else if (ctrlA & DS1501_CTRLA_WDF) logMsg("Default RTC Watchdog handler\n",0,0,0,0,0,0);} /******************************************************************************** rtcIoctl - handles the I/O for the driver** This routine handles I/O requests from the user.** RETURNS: OK, or ERROR if the I/O request fails.** NOMANUAL*/LOCAL STATUS rtcIoctl (RTC_DEV *pRtcDv, int request, int notUsed){ STATUS state = OK; switch (request) { case SET_DATE: rtcUpdateDisable(); rtcRegWriteBcd(DS1501_DAY_OFFSET, rtcDateTime.day); rtcRegWriteBcd(DS1501_DATE_OFFSET, rtcDateTime.date); rtcRegWriteBcd(DS1501_MONTH_OFFSET, rtcDateTime.month); rtcRegWriteBcd(DS1501_YEAR_OFFSET, rtcDateTime.year); rtcRegWriteBcd(DS1501_CENTURY_OFFSET, rtcDateTime.century); rtcUpdateEnable(); break; case GET_DATE: rtcDateTime.day = rtcRegReadBcd(DS1501_DAY_OFFSET); rtcDateTime.date = rtcRegReadBcd(DS1501_DATE_OFFSET); rtcDateTime.month = rtcRegReadBcd(DS1501_MONTH_OFFSET); rtcDateTime.year = rtcRegReadBcd(DS1501_YEAR_OFFSET); rtcDateTime.century = rtcRegReadBcd(DS1501_CENTURY_OFFSET); break; case SET_TIME: rtcUpdateDisable(); rtcRegWriteBcd(DS1501_SECONDS_OFFSET, rtcDateTime.second); rtcRegWriteBcd(DS1501_MINUTES_OFFSET, rtcDateTime.minute); rtcRegWriteBcd(DS1501_HOURS_OFFSET, rtcDateTime.hour); rtcUpdateEnable(); break; case GET_TIME: rtcDateTime.second = rtcRegReadBcd(DS1501_SECONDS_OFFSET); rtcDateTime.minute = rtcRegReadBcd(DS1501_MINUTES_OFFSET); rtcDateTime.hour = rtcRegReadBcd(DS1501_HOURS_OFFSET); break; case MODE_24HR: rtcDateTime.hourMode = 24; break; case MODE_12HR: rtcDateTime.hourMode = 12; break; default: state = S_ioLib_UNKNOWN_REQUEST; break; } return OK;}/******************************************************************************** rtcOpen - Opens the driver */LOCAL int rtcOpen (RTC_DEV *pRtcDv){ return ((int)pRtcDv);}/******************************************************************************** rtcRegRead - read to RTC register** This routine reads to the register at the given offset.** RETURNS: N/A** NOMANUAL*/LOCAL UINT8 rtcRegRead (UINT8 offset){ volatile UINT8 *base = (volatile UINT8 *)CPU_RTC_BASE_ADRS; return (base[offset]);}/******************************************************************************** rtcRegWrite - write to RTC register** This routine writes to the register at the given offset.** RETURNS: N/A** NOMANUAL*/LOCAL void rtcRegWrite (UINT8 offset, UINT8 val){ volatile UINT8 *base = RTC_BASE_ADDR; base[offset] = val;}/******************************************************************************** rtcRegReadBcd - read to RTC register** This routine reads to the register at the given offset,* and converts the value from BCD to binary format.** RETURNS: N/A** NOMANUAL*/LOCAL UINT8 rtcRegReadBcd (UINT8 offset){ volatile UINT8 *base = RTC_BASE_ADDR; UINT8 val = base[offset]; return ((val >> 4)*10 + (val & 0x0F));}/******************************************************************************** rtcRegWriteBcd - write to RTC register** This routine writes to the register at the given offset.* The written value is converted from binary to BCD format.** RETURNS: N/A** NOMANUAL*/LOCAL void rtcRegWriteBcd (UINT8 offset, UINT8 val){ volatile UINT8 *base = RTC_BASE_ADDR; base[offset] = (((val/10) << 4) | (val%10));}/******************************************************************************** rtcUpdateDisable - disable RTC time and calendar update** This routine disables the RTC from updating the time, and calendar bytes* while being initialized.** RETURNS: N/A** NOMANUAL*/LOCAL void rtcUpdateDisable(void){ UINT8 ctrlB = rtcRegRead(DS1501_CTRLB_OFFSET) & ~DS1501_CTRLB_TE; rtcRegWrite(DS1501_CTRLB_OFFSET, ctrlB); }/******************************************************************************** rtcUpdateEnable - enable RTC time and calendar update** This routine enables the RTC to update the time, and calendar bytes.** RETURNS: N/A** NOMANUAL*/LOCAL void rtcUpdateEnable(void) { UINT8 ctrlB = rtcRegRead(DS1501_CTRLB_OFFSET) | DS1501_CTRLB_TE; rtcRegWrite(DS1501_CTRLB_OFFSET, ctrlB); }/******************************************************************************** rtcSpIoctl - handles special I/O requests for the driver** This routine opens the device file for I/O** RETURNS: N/A** SEE ALSO: rtcDosHook()*/LOCAL void rtcSpIoctl (int request){ int fd = open (RTC_DEVICE, 0, 0); ioctl (fd, request, 0); close (fd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -