32fab7547ea25048bd33bd9325ab444bc210fac1
[AROS.git] / arch / ppc-sam440 / battclock / writebattclock.c
blob32fab7547ea25048bd33bd9325ab444bc210fac1
1 /*
2 Copyright � 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: WriteBattClock()
6 Lang: english
7 */
8 #include "battclock_intern.h"
10 #define DEBUG 1
11 #include <aros/debug.h>
12 /*****************************************************************************
14 NAME */
15 #include <proto/battclock.h>
16 #include <proto/utility.h>
17 #include <proto/exec.h>
18 #include <proto/oop.h>
19 #include <hidd/i2c.h>
20 #include <utility/date.h>
21 #include <asm/amcc440.h>
22 #include <stdarg.h>
24 AROS_LH1(void, WriteBattClock,
26 /* SYNOPSIS */
27 AROS_LHA(ULONG, time, D0),
29 /* LOCATION */
30 APTR *, BattClockBase, 3, Battclock)
32 /* FUNCTION
33 Set the systems battery backed up clock to the time specified. The
34 value should be the number of seconds since 00:00:00 on 1.1.1978.
36 INPUTS
37 time - The number of seconds elapsed since 00:00:00 1.1.1978
39 RESULT
40 The clock will be set.
42 NOTES
43 This may not do anything on some systems where the battery backed
44 up clock either doesn't exist, or may not be writable.
46 EXAMPLE
48 BUGS
50 SEE ALSO
51 ReadBattClock, ResetBattClock
53 INTERNALS
55 HISTORY
56 27-11-96 digulla automatically created from
57 battclock_lib.fd and clib/battclock_protos.h
59 *****************************************************************************/
61 AROS_LIBFUNC_INIT
63 struct ClockData date;
64 OOP_Object *i2c;
65 OOP_AttrBase __IHidd_I2CDevice;
67 /* The code here looks more complex than it really is */
69 struct pHidd_I2C_ProbeAddress p, *msg=&p;
70 struct Library *OOPBase = OpenLibrary("oop.library", 0);
71 struct Library *I2CBase = OpenLibrary("i2c-amcc440.library", 0);
73 Amiga2Date(time, &date);
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 wb[8] = {1,};
96 struct TagItem attrs[] = {
97 { aHidd_I2CDevice_Driver, (IPTR)i2c },
98 { aHidd_I2CDevice_Address, 0xd0 },
99 { aHidd_I2CDevice_Name, (IPTR)"RTC" },
100 { TAG_DONE, 0UL }
103 OOP_Object *obj = OOP_NewObject(NULL, CLID_Hidd_I2CDevice, attrs);
105 D(bug("[BATT] RTC found. Object=%08x\n", obj));
107 /* i2c device object created. read data now */
108 if (obj)
110 struct pHidd_I2CDevice_WriteRead msg;
112 msg.mID = OOP_GetMethodID((STRPTR)IID_Hidd_I2CDevice, moHidd_I2CDevice_WriteRead);
113 msg.readBuffer = &wb[1];
114 msg.readLength = 7;
115 msg.writeBuffer = &wb[0];
116 msg.writeLength = 1;
118 OOP_DoMethod(obj, &msg.mID);
120 int i;
122 D(bug("[BATT] Old dump: "));
123 for (i=1; i < 8; i++)
125 D(bug("%02x ", wb[i]));
127 D(bug("\n"));
129 /* Data read. Modify the bits and pieces now */
130 wb[1] = (wb[1] & 0x80) | (date.sec / 10) << 4 | (date.sec % 10);
131 wb[2] = (date.min / 10) << 4 | (date.min % 10);
132 wb[3] = (wb[3] & 0xc0) | (date.hour / 10) << 4 | (date.hour % 10);
133 wb[4] = (wb[4] & 0x80) | (date.wday);
134 wb[5] = (date.mday / 10) << 4 | (date.mday % 10);
135 wb[6] = (date.month / 10) << 4 | (date.month % 10);
136 wb[7] = ((date.year - 2000) / 10) << 4 | ((date.year - 2000) % 10);
138 D(bug("[BATT] New dump: "));
139 for (i=1; i < 8; i++)
141 D(bug("%02x ", wb[i]));
143 D(bug("\n"));
145 /* Write data to RTC */
146 wb[0] = 1;
147 msg.readBuffer = NULL;
148 msg.readLength = 0;
149 msg.writeBuffer = &wb[0];
150 msg.writeLength = 8;
152 OOP_DoMethod(obj, &msg.mID);
154 /* Done with i2c device */
155 OOP_DisposeObject(obj);
159 /* Done with i2c bus */
160 OOP_DisposeObject(i2c);
163 /* Cleanup */
164 OOP_ReleaseAttrBase(IID_Hidd_I2CDevice);
165 if (OOPBase)
166 CloseLibrary(OOPBase);
167 if (I2CBase)
168 CloseLibrary(I2CBase);
170 AROS_LIBFUNC_EXIT
171 } /* WriteBattClock */