1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
26 * I2C-functions are copied and ported from fmradio.c.
27 * later fixed, adapted and moved to a seperate file so they can be re-used by the rtc-ds1339c code by Robert Kukla
30 /* cute little functions, atomic read-modify-write */
33 #define SCL ( 0x00001000 & GPIO_READ)
34 #define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
35 #define SCL_LO or_l( 0x00001000, &GPIO_ENABLE)
36 #define SCL_HI and_l(~0x00001000, &GPIO_ENABLE)
38 /* SDA is GPIO1, 13 */
39 #define SDA ( 0x00002000 & GPIO1_READ)
40 #define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
41 #define SDA_LO or_l( 0x00002000, &GPIO1_ENABLE)
42 #define SDA_HI and_l(~0x00002000, &GPIO1_ENABLE)
44 /* delay loop to achieve 400kHz at 120MHz CPU frequency */
49 "move.l #21, %[_x_] \r\n" \
51 "subq.l #1, %[_x_] \r\n" \
57 void sw_i2c_init(void)
59 or_l(0x00001000, &GPIO_FUNCTION
);
60 or_l(0x00002000, &GPIO1_FUNCTION
);
70 static void sw_i2c_start(void)
86 static void sw_i2c_stop(void)
98 static void sw_i2c_ack(void)
111 static void sw_i2c_nack(void)
113 SDA_HI
; /* redundant */
124 static bool sw_i2c_getack(void)
127 /* int count = 10; */
129 SDA_HI
; /* sets to input */
134 /* while (SDA && count--) */
149 static void sw_i2c_outb(unsigned char byte
)
153 /* clock out each bit, MSB first */
154 for ( i
=0x80; i
; i
>>=1 )
171 static unsigned char sw_i2c_inb(void)
174 unsigned char byte
= 0;
176 SDA_HI
; /* sets to input */
178 /* clock in each bit, MSB first */
179 for ( i
=0x80; i
; i
>>=1 )
186 while(SCL
==0); /* wait for any SCL clock stretching */
195 int sw_i2c_write(unsigned char chip
, unsigned char location
, const unsigned char* buf
, int count
)
200 sw_i2c_outb((chip
& 0xfe) | SW_I2C_WRITE
);
201 if (!sw_i2c_getack())
207 sw_i2c_outb(location
);
208 if (!sw_i2c_getack())
214 for (i
=0; i
<count
; i
++)
217 if (!sw_i2c_getack())
229 int sw_i2c_read(unsigned char chip
, unsigned char location
, unsigned char* buf
, int count
)
234 sw_i2c_outb((chip
& 0xfe) | SW_I2C_WRITE
);
235 if (!sw_i2c_getack())
241 sw_i2c_outb(location
);
242 if (!sw_i2c_getack())
249 sw_i2c_outb((chip
& 0xfe) | SW_I2C_READ
);
250 if (!sw_i2c_getack())
256 for (i
=0; i
<count
-1; i
++)
258 buf
[i
] = sw_i2c_inb();
263 buf
[i
] = sw_i2c_inb();