summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/sw_i2c.c
blob: 028995e768c4282e1bd68d9603a65dbcf85215a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Miika Pekkarinen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "system.h"
#include "logf.h"
#include "inttypes.h"

#include "sw_i2c.h"

/**
 * I2C-functions are copied and ported from fmradio.c.
 * later fixed, adapted and moved to a seperate file so they can be re-used 
 * by the rtc-ds1339c and later by the m:robe-100 code by Robert Kukla
 */

/* cute little functions, atomic read-modify-write */

#ifdef MROBE_100

/* SCL is GPIOC, 4 */
#define SCL       (GPIOC_INPUT_VAL &  0x00000010)
#define SCL_OUT_LO GPIOC_OUTPUT_VAL&=~0x00000010
#define SCL_LO     GPIOC_OUTPUT_EN |= 0x00000010
#define SCL_HI     GPIOC_OUTPUT_EN &=~0x00000010

/* SDA is GPIOC, 5 */
#define SDA       (GPIOC_INPUT_VAL &  0x00000020)
#define SDA_OUT_LO GPIOC_OUTPUT_VAL&=~0x00000020
#define SDA_LO     GPIOC_OUTPUT_EN |= 0x00000020
#define SDA_HI     GPIOC_OUTPUT_EN &=~0x00000020

#define DELAY  do { volatile int _x; for(_x=0;_x<22;_x++);} while(0)

#else

/* SCL is GPIO, 12 */
#define SCL             ( 0x00001000 & GPIO_READ)
#define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
#define SCL_LO      or_l( 0x00001000, &GPIO_ENABLE)
#define SCL_HI     and_l(~0x00001000, &GPIO_ENABLE)

/* SDA is GPIO1, 13 */
#define SDA             ( 0x00002000 & GPIO1_READ)
#define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
#define SDA_LO      or_l( 0x00002000, &GPIO1_ENABLE)
#define SDA_HI     and_l(~0x00002000, &GPIO1_ENABLE)

/* delay loop to achieve 400kHz at 120MHz CPU frequency */
#define DELAY   \
    ({                                \
        int _x_;                      \
        asm volatile (                \
            "move.l #21, %[_x_] \r\n" \
        "1:                     \r\n" \
            "subq.l #1, %[_x_]  \r\n" \
            "bhi.b  1b          \r\n" \
            : [_x_]"=&d"(_x_)         \
        );                            \
    })

#endif

void sw_i2c_init(void)
{
#ifndef MROBE_100
    or_l(0x00001000, &GPIO_FUNCTION);
    or_l(0x00002000, &GPIO1_FUNCTION);
#endif
  
    SDA_HI;
    SCL_HI;
    SDA_OUT_LO;
    SCL_OUT_LO;
}

/*   in: C=? D=?
 *  out: C=L D=L
 */
static void sw_i2c_start(void)
{
    SCL_LO;
    DELAY;
    SDA_HI;
    DELAY;
    SCL_HI;
    DELAY;
    SDA_LO;
    DELAY;
    SCL_LO;
}

/*   in: C=L D=?
 *  out: C=H D=H
 */
static void sw_i2c_stop(void)
{
    SDA_LO;
    DELAY;
    SCL_HI;
    DELAY;
    SDA_HI;
}

/*   in: C=L D=H
 *  out: C=L D=L
 */
static void sw_i2c_ack(void)
{
    SDA_LO;
    DELAY;
    
    SCL_HI;
    DELAY;
    SCL_LO;
}

/*   in: C=L D=H
 *  out: C=L D=H
 */
static void sw_i2c_nack(void)
{
    SDA_HI;          /* redundant */
    DELAY;
    
    SCL_HI;
    DELAY;
    SCL_LO;
}

/*   in: C=L D=?
 *  out: C=L D=H
 */
static bool sw_i2c_getack(void)
{
    bool ret = true;
/*    int count = 10; */

    SDA_HI;   /* sets to input */
    DELAY;
    SCL_HI;
    DELAY;

/*    while (SDA && count--) */
/*        DELAY; */
    
    if (SDA)
        /* ack failed */
        ret = false;
    
    SCL_LO;

    return ret;
}

/*   in: C=L D=?
 *  out: C=L D=?
 */
static void sw_i2c_outb(unsigned char byte)
{
    int i;

    /* clock out each bit, MSB first */
    for ( i=0x80; i; i>>=1 )
    {
        if ( i & byte )
            SDA_HI;
        else
            SDA_LO;
        DELAY;

        SCL_HI;
        DELAY;
        SCL_LO;
    }
}

/*   in: C=L D=?
 *  out: C=L D=H
 */
static unsigned char sw_i2c_inb(void)
{
    int i;
    unsigned char byte = 0;

    SDA_HI;   /* sets to input */
    
    /* clock in each bit, MSB first */
    for ( i=0x80; i; i>>=1 ) 
    {
        DELAY;
        do {
          SCL_HI;
          DELAY;
        }
        while(SCL==0);    /* wait for any SCL clock stretching */
        if ( SDA )
            byte |= i;
        SCL_LO;
    }

    return byte;
}

int sw_i2c_write(unsigned char chip, unsigned char location, unsigned char* buf, int count)
{
    int i;

    sw_i2c_start();
    sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -1;
    }

#ifdef MROBE_100 /* does not use register addressing */
    (void) location;
#else    
    sw_i2c_outb(location);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -2;
    }
#endif  

    for (i=0; i<count; i++)
    {
        sw_i2c_outb(buf[i]);
        if (!sw_i2c_getack())
        {
            sw_i2c_stop();
            return -3;
        }
    }

    sw_i2c_stop();
    
    return 0;
}

int sw_i2c_read(unsigned char chip, unsigned char location, unsigned char* buf, int count)
{
    int i;

#ifdef MROBE_100 /* does not use register addressing */
    (void) location;
#else    
    sw_i2c_start();
    sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -1;
    }

    sw_i2c_outb(location);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -2;
    }
#endif  

    sw_i2c_start();
    sw_i2c_outb((chip & 0xfe) | SW_I2C_READ);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -3;
    }
    
    for (i=0; i<count-1; i++)
    {
      buf[i] = sw_i2c_inb();
      sw_i2c_ack();
    }

    /* 1byte min */
    buf[i] = sw_i2c_inb();
    sw_i2c_nack();
        
    sw_i2c_stop();
    
    return 0;
}