revert between 56095 -> 55830 in arch
[AROS.git] / arch / ppc-sam440 / battclock / readbattclock.c
blobab9949739e3f22801fe5c639e091fc6cbd10116f
1 /*
2 Copyright © 1995-2016, 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 #include <proto/battclock.h>
14 #include <proto/utility.h>
15 #include <proto/exec.h>
16 #include <proto/oop.h>
17 #include <hidd/i2c.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)
26 AROS_LIBFUNC_INIT
28 struct ClockData date;
29 ULONG secs=0;
30 OOP_Object *i2c;
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);
41 /* New i2c driver */
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));
48 if (i2c)
50 /* i2c class successfully created. Probe for RTC on the bus... */
51 D(bug("[BATT] Probing i2c RTC...\n"));
53 p.address = 0xd0;
55 if (OOP_DoMethod(i2c, (OOP_Msg) msg))
57 /* Got it. Now read the data (7 bytes) from address 1 in RTC */
58 char data[7];
59 char wb[1] = {1};
61 struct TagItem attrs[] = {
62 { aHidd_I2CDevice_Driver, (IPTR)i2c },
63 { aHidd_I2CDevice_Address, 0xd0 },
64 { aHidd_I2CDevice_Name, (IPTR)"RTC" },
65 { TAG_DONE, 0UL }
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 */
73 if (obj)
75 struct pHidd_I2CDevice_WriteRead msg;
77 msg.mID = OOP_GetMethodID((STRPTR)IID_Hidd_I2CDevice, moHidd_I2CDevice_WriteRead);
78 msg.readBuffer = &data[0];
79 msg.readLength = 7;
80 msg.writeBuffer = &wb[0];
81 msg.writeLength = 1;
83 OOP_DoMethod(obj, &msg.mID);
85 int i;
87 D(bug("[BATT] Dump: "));
88 for (i=0; i < 7; i++)
90 D(bug("%02x ", data[i]));
92 D(bug("\n"));
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);
113 /* Cleanup */
114 OOP_ReleaseAttrBase(IID_Hidd_I2CDevice);
115 if (OOPBase)
116 CloseLibrary(OOPBase);
117 if (I2CBase)
118 CloseLibrary(I2CBase);
120 return secs;
122 AROS_LIBFUNC_EXIT
123 } /* ReadBattClock */