2 Copyright © 1995-2016, The AROS Development Team. All rights reserved.
5 Desc: ReadBattClock() function.
8 #include "battclock_intern.h"
11 #include <aros/debug.h>
13 #include <proto/battclock.h>
14 #include <proto/utility.h>
15 #include <proto/exec.h>
16 #include <proto/oop.h>
18 #include <utility/date.h>
19 #include <asm/amcc440.h>
21 /* See rom/battclock/readbattclock.c for documentation */
23 AROS_LH0(ULONG
, ReadBattClock
,
24 struct BattClockBase
*, BattClockBase
, 2, Battclock
)
28 struct ClockData date
;
31 OOP_AttrBase __IHidd_I2CDevice
;
33 /* The code here looks more complex than it really is */
35 struct pHidd_I2C_ProbeAddress p
, *msg
=&p
;
36 struct Library
*OOPBase
= OpenLibrary("oop.library", 0);
37 struct Library
*I2CBase
= OpenLibrary("i2c-amcc440.library", 0);
39 __IHidd_I2CDevice
= OOP_ObtainAttrBase(IID_Hidd_I2CDevice
);
42 i2c
= OOP_NewObject(NULL
, CLID_I2C_AMCC440
, NULL
);
44 p
.mID
= OOP_GetMethodID((STRPTR
)IID_Hidd_I2C
, moHidd_I2C_ProbeAddress
);
46 D(bug("[BATT] i2c=%08x\n", i2c
));
50 /* i2c class successfully created. Probe for RTC on the bus... */
51 D(bug("[BATT] Probing i2c RTC...\n"));
55 if (OOP_DoMethod(i2c
, (OOP_Msg
) msg
))
57 /* Got it. Now read the data (7 bytes) from address 1 in RTC */
61 struct TagItem attrs
[] = {
62 { aHidd_I2CDevice_Driver
, (IPTR
)i2c
},
63 { aHidd_I2CDevice_Address
, 0xd0 },
64 { aHidd_I2CDevice_Name
, (IPTR
)"RTC" },
68 OOP_Object
*obj
= OOP_NewObject(NULL
, CLID_Hidd_I2CDevice
, attrs
);
70 D(bug("[BATT] RTC found. Object=%08x\n", obj
));
72 /* i2c device object created. read data now */
75 struct pHidd_I2CDevice_WriteRead msg
;
77 msg
.mID
= OOP_GetMethodID((STRPTR
)IID_Hidd_I2CDevice
, moHidd_I2CDevice_WriteRead
);
78 msg
.readBuffer
= &data
[0];
80 msg
.writeBuffer
= &wb
[0];
83 OOP_DoMethod(obj
, &msg
.mID
);
87 D(bug("[BATT] Dump: "));
90 D(bug("%02x ", data
[i
]));
94 /* Ok, let's hope data was successfully read from RTC. Convert it to ClockDate structure */
95 date
.year
= 2000 + (data
[6] & 0xf) + 10*(data
[6] >> 4);
96 date
.month
= (data
[5] & 0xf) + (data
[5] >> 4)*10;
97 date
.mday
= (data
[4] & 0xf) + (data
[4] >> 4)*10;
98 date
.hour
= (data
[2] & 0xf) + ((data
[2] & 0x30) >> 4)*10;
99 date
.min
= (data
[1] & 0xf) + ((data
[1] & 0x70) >> 4)*10;
100 date
.sec
= (data
[0] & 0xf) + ((data
[0] & 0x70) >> 4)*10;
102 /* Done with i2c device */
103 OOP_DisposeObject(obj
);
107 /* Done with i2c bus */
108 OOP_DisposeObject(i2c
);
110 secs
=Date2Amiga(&date
);
114 OOP_ReleaseAttrBase(IID_Hidd_I2CDevice
);
116 CloseLibrary(OOPBase
);
118 CloseLibrary(I2CBase
);
123 } /* ReadBattClock */