summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/i2c.c
blob: ed12145da968ad24265514530af876bca27aa5f5 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "lcd.h"
#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "debug.h"
#include "system.h"

/* cute little functions, atomic read-modify-write */
#if CONFIG_I2C == I2C_GMINI

/* This is done like this in the Archos' firmware.
 * However, the TCC370 has an integrated I2C
 * controller (bound to the same lines). It should be
 * possible to use it and thus save some space in flash.
 */
#define SDA_LO     (P3 &= ~0x20)
#define SDA_HI     (P3 |= 0x20)
#define SDA_INPUT  (P3CONH &= ~0x0C)
#define SDA_OUTPUT (P3CONH |= 0x04)
#define SDA        (P3 & 0x20)

#define SCL_LO     (P3 &= ~0x10)
#define SCL_HI     (P3 |= 0x10)
#define SCL_INPUT  (P3CONH &= ~0x03)
#define SCL_OUTPUT (P3CONH |= 0x01)
#define SCL        (P3 & 0x10)

#else /* non Gmini below */

/* SDA is PB7 */
#define SDA_LO     and_b(~0x80, &PBDRL)
#define SDA_HI     or_b(0x80, &PBDRL)
#define SDA_INPUT  and_b(~0x80, &PBIORL)
#define SDA_OUTPUT or_b(0x80, &PBIORL)
#define SDA     (PBDR & 0x80)

#if CONFIG_I2C == I2C_ONDIO
/* Ondio pinout, SCL moved to PB6 */
#define SCL_INPUT  and_b(~0x40, &PBIORL)
#define SCL_OUTPUT or_b(0x40, &PBIORL)
#define SCL_LO     and_b(~0x40, &PBDRL)
#define SCL_HI     or_b(0x40, &PBDRL)
#define SCL     (PBDR & 0x0040)
#else 
/* "classic" pinout, SCL is PB13 */
#define SCL_INPUT  and_b(~0x20, &PBIORH)
#define SCL_OUTPUT or_b(0x20, &PBIORH)
#define SCL_LO     and_b(~0x20, &PBDRH)
#define SCL_HI     or_b(0x20, &PBDRH)
#define SCL     (PBDR & 0x2000)
#endif
#endif /* ! I2C_GMINI */

/* arbitrary delay loop */
#define DELAY   do { int _x; for(_x=0;_x<20;_x++);} while (0)

static struct mutex i2c_mtx;

void i2c_begin(void)
{
    mutex_lock(&i2c_mtx);
}

void i2c_end(void)
{
    mutex_unlock(&i2c_mtx);
}

void i2c_start(void)
{
    SDA_OUTPUT;
    SDA_HI;
    SCL_HI;
    SDA_LO;
    DELAY;
    SCL_LO;
}

void i2c_stop(void)
{
   SDA_LO;
   SCL_HI;
   DELAY;
   SDA_HI;
}

void i2c_init(void)
{
   int i;

#if CONFIG_I2C == I2C_GMINI
   SCL_INPUT;
   SDA_INPUT;   
#else
#if CONFIG_I2C == I2C_ONDIO
   /* make PB5, PB6 & PB7 general I/O */
   PBCR2 &= ~0xfc00; /* includes PB5, see FIXME below */
#else /* not Gmini, not Ondio */
   /* make PB5, PB7 & PB13 general I/O */
   PBCR1 &= ~0x0c00; /* PB13 */
   PBCR2 &= ~0xcc00; /* PB5 and PB7, see FIXME below */
#endif
   
   /* PB5 is "MAS enable" (no I2C signal!). make it output and high */
   /* FIXME: this is true only for Players, and should go into mas.c */
   /* for Recorders, it shuts off the charger, for FM/V2 it holds power */
   or_b(0x20, &PBIORL);
   or_b(0x20, &PBDRL);
#endif /* end of non-Gmini */

   SCL_OUTPUT;
   SDA_OUTPUT;
   SDA_HI;
   SCL_LO;
   for (i=0;i<3;i++)
      i2c_stop();
}

void i2c_ack(int bit)
{
    /* Here's the deal. The MAS is slow, and sometimes needs to wait
       before it can receive the acknowledge. Therefore it forces the clock
       low until it is ready. We need to poll the clock line until it goes
       high before we release the ack. */
    
    SCL_LO;      /* Set the clock low */
    if ( bit )
    {
        SDA_HI;
    }
    else
    {
        SDA_LO;
    }
    
    SCL_INPUT;   /* Set the clock to input */
    while(!SCL)  /* and wait for the MAS to release it */
        sleep_thread();
    wake_up_thread();

    DELAY;
    SCL_OUTPUT;
    SCL_LO;
}

int i2c_getack(void)
{
    int ret = 1;

    /* Here's the deal. The MAS is slow, and sometimes needs to wait
       before it can send the acknowledge. Therefore it forces the clock
       low until it is ready. We need to poll the clock line until it goes
       high before we read the ack. */

#ifdef HAVE_I2C_LOW_FIRST
    SDA_LO;      /* First, discharge the data line */
#endif
    SDA_INPUT;   /* And set to input */
    SCL_INPUT;   /* Set the clock to input */
    while(!SCL)  /* and wait for the MAS to release it */
        sleep_thread();
    wake_up_thread();
    
    if (SDA)
        /* ack failed */
        ret = 0;
    
    SCL_OUTPUT;
    SCL_LO;
    SDA_HI;
    SDA_OUTPUT;
    return ret;
}

void 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;
      }
      SCL_HI;
      SCL_LO;
   }

   SDA_HI;
}

unsigned char i2c_inb(int ack)
{
   int i;
   unsigned char byte = 0;

   /* clock in each bit, MSB first */
   for ( i=0x80; i; i>>=1 ) {
#ifdef HAVE_I2C_LOW_FIRST
       /* Tricky business. Here we discharge the data line by driving it low
          and then set it to input to see if it stays low or goes high */
       SDA_LO;      /* First, discharge the data line */
#endif
       SDA_INPUT;   /* And set to input */
       SCL_HI;
       if ( SDA )
           byte |= i;
       SCL_LO;
       SDA_OUTPUT;
   }
   
   i2c_ack(ack);
   
   return byte;
}

int i2c_write(int address, const unsigned char* buf, int count )
{
    int i,x=0;

    i2c_start();
    i2c_outb(address & 0xfe);
    if (i2c_getack())
    {
        for (i=0; i<count; i++)
        {
            i2c_outb(buf[i]);
            if (!i2c_getack())
            {
                x=-2;
                break;
            }
        }
    }
    else
    {
        debugf("i2c_write() - no ack\n");
        x=-1;
    }
    i2c_stop();
    return x;
}

int i2c_read(int address, unsigned char* buf, int count )
{
    int i,x=0;
    
    i2c_start();
    i2c_outb(address | 1);
    if (i2c_getack()) {
        for (i=0; i<count; i++) {
            buf[i] = i2c_inb(0);
        }
    }
    else
        x=-1;
    i2c_stop();
    return x;
}