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
28 * by the rtc-ds1339c and later by the m:robe-100 code by Robert Kukla
31 /* cute little functions, atomic read-modify-write */
36 #define SCL (GPIOC_INPUT_VAL & 0x00000010)
37 #define SCL_OUT_LO GPIOC_OUTPUT_VAL&=~0x00000010
38 #define SCL_LO GPIOC_OUTPUT_EN |= 0x00000010
39 #define SCL_HI GPIOC_OUTPUT_EN &=~0x00000010
42 #define SDA (GPIOC_INPUT_VAL & 0x00000020)
43 #define SDA_OUT_LO GPIOC_OUTPUT_VAL&=~0x00000020
44 #define SDA_LO GPIOC_OUTPUT_EN |= 0x00000020
45 #define SDA_HI GPIOC_OUTPUT_EN &=~0x00000020
47 #define DELAY do { volatile int _x; for(_x=0;_x<22;_x++);} while(0)
52 #define SCL ( 0x00001000 & GPIO_READ)
53 #define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
54 #define SCL_LO or_l( 0x00001000, &GPIO_ENABLE)
55 #define SCL_HI and_l(~0x00001000, &GPIO_ENABLE)
57 /* SDA is GPIO1, 13 */
58 #define SDA ( 0x00002000 & GPIO1_READ)
59 #define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
60 #define SDA_LO or_l( 0x00002000, &GPIO1_ENABLE)
61 #define SDA_HI and_l(~0x00002000, &GPIO1_ENABLE)
63 /* delay loop to achieve 400kHz at 120MHz CPU frequency */
68 "move.l #21, %[_x_] \r\n" \
70 "subq.l #1, %[_x_] \r\n" \
78 void sw_i2c_init(void)
81 or_l(0x00001000, &GPIO_FUNCTION
);
82 or_l(0x00002000, &GPIO1_FUNCTION
);
94 static void sw_i2c_start(void)
110 static void sw_i2c_stop(void)
122 static void sw_i2c_ack(void)
135 static void sw_i2c_nack(void)
137 SDA_HI
; /* redundant */
148 static bool sw_i2c_getack(void)
151 /* int count = 10; */
153 SDA_HI
; /* sets to input */
158 /* while (SDA && count--) */
173 static void sw_i2c_outb(unsigned char byte
)
177 /* clock out each bit, MSB first */
178 for ( i
=0x80; i
; i
>>=1 )
195 static unsigned char sw_i2c_inb(void)
198 unsigned char byte
= 0;
200 SDA_HI
; /* sets to input */
202 /* clock in each bit, MSB first */
203 for ( i
=0x80; i
; i
>>=1 )
210 while(SCL
==0); /* wait for any SCL clock stretching */
219 int sw_i2c_write(unsigned char chip
, unsigned char location
, unsigned char* buf
, int count
)
224 sw_i2c_outb((chip
& 0xfe) | SW_I2C_WRITE
);
225 if (!sw_i2c_getack())
231 #ifdef MROBE_100 /* does not use register addressing */
234 sw_i2c_outb(location
);
235 if (!sw_i2c_getack())
242 for (i
=0; i
<count
; i
++)
245 if (!sw_i2c_getack())
257 int sw_i2c_read(unsigned char chip
, unsigned char location
, unsigned char* buf
, int count
)
261 #ifdef MROBE_100 /* does not use register addressing */
265 sw_i2c_outb((chip
& 0xfe) | SW_I2C_WRITE
);
266 if (!sw_i2c_getack())
272 sw_i2c_outb(location
);
273 if (!sw_i2c_getack())
281 sw_i2c_outb((chip
& 0xfe) | SW_I2C_READ
);
282 if (!sw_i2c_getack())
288 for (i
=0; i
<count
-1; i
++)
290 buf
[i
] = sw_i2c_inb();
295 buf
[i
] = sw_i2c_inb();