Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# m41t80.c

    00001
    00002 // system includes
    00003 #include "at91sam7s64.h"
    00004 #include "global.h"
    00005
    00006 // local includes
    00007 #include "i2c.h"
    00008 #include "rprintf.h"
    00009 #include "m41t80.h"
    00010
    00011 // functions
    00012 void m41t80Init(void)
    00013 {
    00014     // check if clock is running
    00015     if(m41t80ReadReg(M41T80_REG_SECONDS) & 0x80)
    00016     {
    00017         // STOP bit is set, clock is not running
    00018         // lets start it
    00019         m41t80Reset();
    00020     }
    00021     // otherwise, clock is already running
    00022     // Do not disturb.
    00023 }
    00024
    00025 void m41t80Reset(void)
    00026 {
    00027     // reset registers to initialized state
    00028     m41t80WriteReg(M41T80_REG_CTRL, 0x80);
    00029     // start clock
    00030     m41t80WriteReg(M41T80_REG_SECONDS, 0x00);
    00031     // reset time
    00032     m41t80SetTime(0);
    00033     // start clock updates
    00034     m41t80WriteReg(M41T80_REG_AL_HOUR, 0x00);
    00035 }
    00036
    00037 void m41t80WriteReg(u08 regaddr, u08 data)
    00038 {
    00039     u08 packet[2];
    00040     // setup write
    00041     packet[0] = regaddr;
    00042     packet[1] = data;
    00043     // write the register
    00044     i2cMasterSend(M41T80_I2C_BASE_ADDR, 2, packet);
    00045 }
    00046
    00047 u08 m41t80ReadReg(u08 regaddr)
    00048 {
    00049     u08 data;
    00050     // set register address to read
    00051     i2cMasterSend(M41T80_I2C_BASE_ADDR, 1, &regaddr);
    00052     // read the register
    00053     i2cMasterReceive(M41T80_I2C_BASE_ADDR, 1, &data);
    00054     // return data
    00055     return data;
    00056 }
    00057
    00058 void m41t80SetOutState(int state)
    00059 {
    00060     // set the OUT pin state
    00061     // if state == FALSE, OUT is set low
    00062     // if state == TRUE, OUT is set open (open-collector)
    00063
    00064     // modify OUT bit
    00065     if(state)
    00066         m41t80WriteReg(M41T80_REG_CTRL, m41t80ReadReg(M41T80_REG_CTRL) | 0x80);
    00067     else
    00068         m41t80WriteReg(M41T80_REG_CTRL, m41t80ReadReg(M41T80_REG_CTRL) & ~0x80);
    00069 }
    00070
    00071
    00072 void m41t80SetTime(u32 time)
    00073 {
    00074     u08 packet[5];
    00075
    00076     // prepare write
    00077     packet[0] = M41T80_REG_DSECONDS;
    00078     packet[1] = time>>24;
    00079     packet[2] = time>>16;
    00080     packet[3] = time>>8;
    00081     packet[4] = time;
    00082     // write new time
    00083     i2cMasterSend(M41T80_I2C_BASE_ADDR, 5, packet);
    00084 }
    00085
    00086 u32 m41t80GetTime(void)
    00087 {
    00088     u08 reg;
    00089     u32 time;
    00090
    00091     // set the register to access
    00092     reg = M41T80_REG_DSECONDS;
    00093     i2cMasterSend(M41T80_I2C_BASE_ADDR, 1, &reg);
    00094     // get the current time
    00095     i2cMasterReceive(M41T80_I2C_BASE_ADDR, 4, (unsigned char*)&time);
    00096     // return time fields
    00097     // -- All fields BCD encoded --
    00098     // bits 31-30 : century bits
    00099     // bits 29-28 : 10's hours
    00100     // bits 27-24 : 1's hours
    00101     // bits 23-20 : 10's minutes
    00102     // bits 19-16 : 1's minutes
    00103     // bits 15-12 : 10's seconds
    00104     // bits 11-08 : 1's seconds
    00105     // bits 07-04 : 0.1's seconds
    00106     // bits 03-00 : 0.01's seconds
    00107     return time;
    00108 }
    00109
    00110 void m41t80PrintTime(u32 time)
    00111 {
    00112     // mask off century bits
    00113     time &= 0x3FFFFFFF;
    00114     // print hours
    00115     rprintfu08(time>>24);
    00116     rprintfChar(':');
    00117     // print minutes
    00118     rprintfu08(time>>16);
    00119     rprintfChar(':');
    00120     // print seconds
    00121     rprintfu08(time>>8);
    00122     rprintfChar('.');
    00123     // print decimal seconds
    00124     rprintfu08(time);
    00125 }