1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
28 * I2C-functions are copied and ported from fmradio.c.
29 * later fixed, adapted and moved to a seperate file so they can be re-used
30 * by the rtc-ds1339c and later by the m:robe-100 code by Robert Kukla
33 /* cute little functions, atomic read-modify-write */
38 #define SCL (GPIOC_INPUT_VAL & 0x00000010)
39 #define SCL_OUT_LO GPIOC_OUTPUT_VAL&=~0x00000010
40 #define SCL_LO GPIOC_OUTPUT_EN |= 0x00000010
41 #define SCL_HI GPIOC_OUTPUT_EN &=~0x00000010
44 #define SDA (GPIOC_INPUT_VAL & 0x00000020)
45 #define SDA_OUT_LO GPIOC_OUTPUT_VAL&=~0x00000020
46 #define SDA_LO GPIOC_OUTPUT_EN |= 0x00000020
47 #define SDA_HI GPIOC_OUTPUT_EN &=~0x00000020
49 #define DELAY do { volatile int _x; for(_x=0;_x<22;_x++);} while(0)
54 #define SCL ( 0x00001000 & GPIO_READ)
55 #define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
56 #define SCL_LO or_l( 0x00001000, &GPIO_ENABLE)
57 #define SCL_HI and_l(~0x00001000, &GPIO_ENABLE)
59 /* SDA is GPIO1, 13 */
60 #define SDA ( 0x00002000 & GPIO1_READ)
61 #define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
62 #define SDA_LO or_l( 0x00002000, &GPIO1_ENABLE)
63 #define SDA_HI and_l(~0x00002000, &GPIO1_ENABLE)
65 /* delay loop to achieve 400kHz at 120MHz CPU frequency */
70 "move.l #21, %[_x_] \r\n" \
72 "subq.l #1, %[_x_] \r\n" \
80 void sw_i2c_init(void)
83 or_l(0x00001000, &GPIO_FUNCTION
);
84 or_l(0x00002000, &GPIO1_FUNCTION
);
96 static void sw_i2c_start(void)
112 static void sw_i2c_stop(void)
124 static void sw_i2c_ack(void)
137 static void sw_i2c_nack(void)
139 SDA_HI
; /* redundant */
150 static bool sw_i2c_getack(void)
153 /* int count = 10; */
155 SDA_HI
; /* sets to input */
160 /* while (SDA && count--) */
175 static void sw_i2c_outb(unsigned char byte
)
179 /* clock out each bit, MSB first */
180 for ( i
=0x80; i
; i
>>=1 )
197 static unsigned char sw_i2c_inb(void)
200 unsigned char byte
= 0;
202 SDA_HI
; /* sets to input */
204 /* clock in each bit, MSB first */
205 for ( i
=0x80; i
; i
>>=1 )
212 while(SCL
==0); /* wait for any SCL clock stretching */
221 int sw_i2c_write(unsigned char chip
, unsigned char location
, unsigned char* buf
, int count
)
226 sw_i2c_outb((chip
& 0xfe) | SW_I2C_WRITE
);
227 if (!sw_i2c_getack())
233 #ifdef MROBE_100 /* does not use register addressing */
236 sw_i2c_outb(location
);
237 if (!sw_i2c_getack())
244 for (i
=0; i
<count
; i
++)
247 if (!sw_i2c_getack())
259 int sw_i2c_read(unsigned char chip
, unsigned char location
, unsigned char* buf
, int count
)
263 #ifdef MROBE_100 /* does not use register addressing */
267 sw_i2c_outb((chip
& 0xfe) | SW_I2C_WRITE
);
268 if (!sw_i2c_getack())
274 sw_i2c_outb(location
);
275 if (!sw_i2c_getack())
283 sw_i2c_outb((chip
& 0xfe) | SW_I2C_READ
);
284 if (!sw_i2c_getack())
290 for (i
=0; i
<count
-1; i
++)
292 buf
[i
] = sw_i2c_inb();
297 buf
[i
] = sw_i2c_inb();