472fca12b2f5250e28543dab87d9c19519372e63
[AROS.git] / arch / ppc-sam440 / battclock / readbattclock.c
blob472fca12b2f5250e28543dab87d9c19519372e63
1 /*
2 Copyright � 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: ReadBattClock() function.
6 Lang: english
7 */
8 #include "battclock_intern.h"
10 #define DEBUG 1
11 #include <aros/debug.h>
13 /*****************************************************************************
15 NAME */
16 #include <proto/battclock.h>
17 #include <proto/utility.h>
18 #include <proto/exec.h>
19 #include <proto/oop.h>
20 #include <hidd/i2c.h>
21 #include <utility/date.h>
22 #include <asm/amcc440.h>
24 AROS_LH0(ULONG, ReadBattClock,
26 /* SYNOPSIS */
27 /* void */
29 /* LOCATION */
30 struct BattClockBase *, BattClockBase, 2, Battclock)
32 /* FUNCTION
33 Return the value stored in the battery back up clock. This value
34 is the number of seconds that have elapsed since midnight on the
35 1st of January 1978 (00:00:00 1.1.1978).
37 If the value of the battery clock is invalid, then the clock will
38 be reset.
40 INPUTS
42 RESULT
43 The number of seconds since 1.1.1978 00:00:00
45 NOTES
47 EXAMPLE
49 BUGS
51 SEE ALSO
52 WriteBattClock, ResetBattClock
54 INTERNALS
56 HISTORY
57 27-11-96 digulla automatically created from
58 battclock_lib.fd and clib/battclock_protos.h
60 *****************************************************************************/
62 AROS_LIBFUNC_INIT
64 struct ClockData date;
65 ULONG secs=0;
66 OOP_Object *i2c;
67 OOP_AttrBase __IHidd_I2CDevice;
69 /* The code here looks more complex than it really is */
71 struct pHidd_I2C_ProbeAddress p, *msg=&p;
72 struct Library *OOPBase = OpenLibrary("oop.library", 0);
73 struct Library *I2CBase = OpenLibrary("i2c-amcc440.library", 0);
75 __IHidd_I2CDevice = OOP_ObtainAttrBase(IID_Hidd_I2CDevice);
77 /* New i2c driver */
78 i2c = OOP_NewObject(NULL, CLID_I2C_AMCC440, NULL);
80 p.mID = OOP_GetMethodID((STRPTR)IID_Hidd_I2C, moHidd_I2C_ProbeAddress);
82 D(bug("[BATT] i2c=%08x\n", i2c));
84 if (i2c)
86 /* i2c class successfully created. Probe for RTC on the bus... */
87 D(bug("[BATT] Probing i2c RTC...\n"));
89 p.address = 0xd0;
91 if (OOP_DoMethod(i2c, (OOP_Msg) msg))
93 /* Got it. Now read the data (7 bytes) from address 1 in RTC */
94 char data[7];
95 char wb[1] = {1};
97 struct TagItem attrs[] = {
98 { aHidd_I2CDevice_Driver, (IPTR)i2c },
99 { aHidd_I2CDevice_Address, 0xd0 },
100 { aHidd_I2CDevice_Name, (IPTR)"RTC" },
101 { TAG_DONE, 0UL }
104 OOP_Object *obj = OOP_NewObject(NULL, CLID_Hidd_I2CDevice, attrs);
106 D(bug("[BATT] RTC found. Object=%08x\n", obj));
108 /* i2c device object created. read data now */
109 if (obj)
111 struct pHidd_I2CDevice_WriteRead msg;
113 msg.mID = OOP_GetMethodID((STRPTR)IID_Hidd_I2CDevice, moHidd_I2CDevice_WriteRead);
114 msg.readBuffer = &data[0];
115 msg.readLength = 7;
116 msg.writeBuffer = &wb[0];
117 msg.writeLength = 1;
119 OOP_DoMethod(obj, &msg.mID);
121 int i;
123 D(bug("[BATT] Dump: "));
124 for (i=0; i < 7; i++)
126 D(bug("%02x ", data[i]));
128 D(bug("\n"));
130 /* Ok, let's hope data was successfuly read from RTC. Convert it to ClockDate structure */
131 date.year = 2000 + (data[6] & 0xf) + 10*(data[6] >> 4);
132 date.month = (data[5] & 0xf) + (data[5] >> 4)*10;
133 date.mday = (data[4] & 0xf) + (data[4] >> 4)*10;
134 date.hour = (data[2] & 0xf) + ((data[2] & 0x30) >> 4)*10;
135 date.min = (data[1] & 0xf) + ((data[1] & 0x70) >> 4)*10;
136 date.sec = (data[0] & 0xf) + ((data[0] & 0x70) >> 4)*10;
138 /* Done with i2c device */
139 OOP_DisposeObject(obj);
143 /* Done with i2c bus */
144 OOP_DisposeObject(i2c);
146 secs=Date2Amiga(&date);
149 /* Cleanup */
150 OOP_ReleaseAttrBase(IID_Hidd_I2CDevice);
151 if (OOPBase)
152 CloseLibrary(OOPBase);
153 if (I2CBase)
154 CloseLibrary(I2CBase);
156 return secs;
158 AROS_LIBFUNC_EXIT
159 } /* ReadBattClock */