/********************************************************************* DS1307.C JC Bertin, Mipsys 03/22/2000 This file handles the Dallas DS1307 Real Time Clock on Horoscas board. It use the mbus driver to communicate with the DS1307. **********************************************************************/ #include #include #include #include #include #include #include "ds1302.h" #define DS1307_ADDR 0xd0 static int mbus_fd; /******************************************************************* Setup_1302_Port Initialize Port Pins to access DS1302 Clock. Note that all three pins are defined initialliy as outputs. When you want to read a port pin, you have to turn it around. MCF5206... PP0 = CLOCK PP1 = RST* PP2 = DATA ********************************************************************/ void Setup_1302_Port(void) { if ((mbus_fd = open("/dev/mbus", O_RDWR)) < 0) { perror("/dev/mbus"); exit(1); } } /******************************************************************* Lock_1302 This routine will LOCK the clock and stop its oscillator to preserve Battery Life. Uses TimeBuffer[] as temporary storage. ********************************************************************/ void Lock_1302 (void) { int result; unsigned char buffer[4]; //temp storage buffer[0] = DS1307_ADDR; buffer[1] = 0; if ((result = read(mbus_fd, buffer, 3)) != 3) { fprintf(stderr, "DS1307 returned %d\n", result); exit(1); } buffer[2] |= 0x80; if ((result = write(mbus_fd, buffer, 3)) != 3) { fprintf(stderr, "DS1307 returned %d\n", result); exit(1); } } /* end proc Lock_1203() */ /******************************************************************* Set_Time This routine SETs the clock and start its oscillator, write- protecting the clock registers. Obtains time from TimeBuffer[]. ********************************************************************/ void Set_Time(unsigned char * TimeBuffer) { int result; unsigned char buffer[10]; TimeBuffer[7] = '\x80'; //write protect the chip after write buffer[0] = DS1307_ADDR; buffer[1] = 0; memcpy(&buffer[2], TimeBuffer, 8); if ((result = write(mbus_fd, buffer, 10)) != 10) { fprintf(stderr, "DS1307 returned %d\n", result); exit(1); } } /* end proc Set_Time() */ /******************************************************************* Get_Time This routine GETs the time from the Dallas 1302 clock. The time is returned in TimeBuffer. This function returns 0 when the clock is operating properly. If the clock was locked (stopped) the default time is returned as 1/1/99, 12:00Noon and the function returns non-zero. ********************************************************************/ unsigned int Get_Time(unsigned char * TimeBuffer) { unsigned int clock_locked; int result; unsigned char buffer[10]; buffer[0] = DS1307_ADDR; buffer[1] = 0; if ((result = read(mbus_fd, buffer, 10)) != 10) { fprintf(stderr, "DS1307 returned %d\n", result); exit(1); } memcpy(TimeBuffer, &buffer[2], 8); clock_locked = ((TimeBuffer[0] & 0x80) != 0); return(clock_locked); } /* end proc Get_Time() */