2 Copyright � 1995-2007, The AROS Development Team. All rights reserved.
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>
22 /* See rom/battclock/writebattclock.c for documentation */
24 AROS_LH1(void, WriteBattClock
,
25 AROS_LHA(ULONG
, time
, D0
),
26 APTR
*, BattClockBase
, 3, Battclock
)
30 struct ClockData date
;
32 OOP_AttrBase __IHidd_I2CDevice
;
34 /* The code here looks more complex than it really is */
36 struct pHidd_I2C_ProbeAddress p
, *msg
=&p
;
37 struct Library
*OOPBase
= OpenLibrary("oop.library", 0);
38 struct Library
*I2CBase
= OpenLibrary("i2c-amcc440.library", 0);
40 Amiga2Date(time
, &date
);
42 __IHidd_I2CDevice
= OOP_ObtainAttrBase(IID_Hidd_I2CDevice
);
45 i2c
= OOP_NewObject(NULL
, CLID_I2C_AMCC440
, NULL
);
47 p
.mID
= OOP_GetMethodID((STRPTR
)IID_Hidd_I2C
, moHidd_I2C_ProbeAddress
);
49 D(bug("[BATT] i2c=%08x\n", i2c
));
53 /* i2c class successfully created. Probe for RTC on the bus... */
54 D(bug("[BATT] Probing i2c RTC...\n"));
58 if (OOP_DoMethod(i2c
, (OOP_Msg
) msg
))
60 /* Got it. Now read the data (7 bytes) from address 1 in RTC */
63 struct TagItem attrs
[] = {
64 { aHidd_I2CDevice_Driver
, (IPTR
)i2c
},
65 { aHidd_I2CDevice_Address
, 0xd0 },
66 { aHidd_I2CDevice_Name
, (IPTR
)"RTC" },
70 OOP_Object
*obj
= OOP_NewObject(NULL
, CLID_Hidd_I2CDevice
, attrs
);
72 D(bug("[BATT] RTC found. Object=%08x\n", obj
));
74 /* i2c device object created. read data now */
77 struct pHidd_I2CDevice_WriteRead msg
;
79 msg
.mID
= OOP_GetMethodID((STRPTR
)IID_Hidd_I2CDevice
, moHidd_I2CDevice_WriteRead
);
80 msg
.readBuffer
= &wb
[1];
82 msg
.writeBuffer
= &wb
[0];
85 OOP_DoMethod(obj
, &msg
.mID
);
89 D(bug("[BATT] Old dump: "));
92 D(bug("%02x ", wb
[i
]));
96 /* Data read. Modify the bits and pieces now */
97 wb
[1] = (wb
[1] & 0x80) | (date
.sec
/ 10) << 4 | (date
.sec
% 10);
98 wb
[2] = (date
.min
/ 10) << 4 | (date
.min
% 10);
99 wb
[3] = (wb
[3] & 0xc0) | (date
.hour
/ 10) << 4 | (date
.hour
% 10);
100 wb
[4] = (wb
[4] & 0x80) | (date
.wday
);
101 wb
[5] = (date
.mday
/ 10) << 4 | (date
.mday
% 10);
102 wb
[6] = (date
.month
/ 10) << 4 | (date
.month
% 10);
103 wb
[7] = ((date
.year
- 2000) / 10) << 4 | ((date
.year
- 2000) % 10);
105 D(bug("[BATT] New dump: "));
106 for (i
=1; i
< 8; i
++)
108 D(bug("%02x ", wb
[i
]));
112 /* Write data to RTC */
114 msg
.readBuffer
= NULL
;
116 msg
.writeBuffer
= &wb
[0];
119 OOP_DoMethod(obj
, &msg
.mID
);
121 /* Done with i2c device */
122 OOP_DisposeObject(obj
);
126 /* Done with i2c bus */
127 OOP_DisposeObject(i2c
);
131 OOP_ReleaseAttrBase(IID_Hidd_I2CDevice
);
133 CloseLibrary(OOPBase
);
135 CloseLibrary(I2CBase
);
138 } /* WriteBattClock */