OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.61/] [tools/] [fx2/] [src/] [lib/] [i2c.c] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 wfjm
/* -*- c++ -*- */
2
/* $Id: i2c.c 395 2011-07-17 22:02:55Z mueller $ */
3
/*-----------------------------------------------------------------------------
4
 * I2C read/write functions for FX2
5
 *-----------------------------------------------------------------------------
6
 * Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,
7
 * Copyright 2003 Free Software Foundation, Inc.
8
 *-----------------------------------------------------------------------------
9
 * This code is part of usbjtag. usbjtag is free software; you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License as
11
 * published by the Free Software Foundation; either version 2 of the License,
12
 * or (at your option) any later version. usbjtag is distributed in the hope
13
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.  You should have received a
16
 * copy of the GNU General Public License along with this program in the file
17
 * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin
18
 * St, Fifth Floor, Boston, MA  02110-1301  USA
19
 *-----------------------------------------------------------------------------
20
 */
21
 
22
#include "i2c.h"
23
#include "fx2regs.h"
24
#include <string.h>
25
 
26
 
27
// issue a stop bus cycle and wait for completion
28
 
29
 
30
// returns non-zero if successful, else 0
31
unsigned char
32
i2c_read (unsigned char addr, xdata unsigned char *buf, unsigned char len)
33
{
34
  volatile unsigned char        junk;
35
 
36
  if (len == 0)                  // reading zero bytes always works
37
    return 1;
38
 
39
  while (I2CS & bmSTOP)         // wait for stop to clear
40
    ;
41
 
42
  I2CS = bmSTART;
43
  I2DAT = (addr << 1) | 1;      // write address and direction (1's the read bit)
44
 
45
  while ((I2CS & bmDONE) == 0)
46
    ;
47
 
48
  if ((I2CS & bmBERR) || (I2CS & bmACK) == 0)    // no device answered...
49
    goto fail;
50
 
51
  if (len == 1)
52
    I2CS |= bmLASTRD;
53
 
54
  junk = I2DAT;                 // trigger the first read cycle
55
 
56
  while (--len != 0){
57
    while ((I2CS & bmDONE) == 0)
58
      ;
59
 
60
    if (I2CS & bmBERR)
61
      goto fail;
62
 
63
    if (len == 1)
64
      I2CS |= bmLASTRD;
65
 
66
    *buf++ = I2DAT;             // get data, trigger another read
67
  }
68
 
69
  // wait for final byte
70
 
71
  while ((I2CS & bmDONE) == 0)
72
    ;
73
 
74
  if (I2CS & bmBERR)
75
    goto fail;
76
 
77
  I2CS |= bmSTOP;
78
  *buf = I2DAT;
79
 
80
  return 1;
81
 
82
 fail:
83
  I2CS |= bmSTOP;
84
  return 0;
85
}
86
 
87
 
88
 
89
// returns non-zero if successful, else 0
90
unsigned char
91
i2c_write (unsigned char addr, xdata const unsigned char *buf, unsigned char len)
92
{
93
  while (I2CS & bmSTOP)         // wait for stop to clear
94
    ;
95
 
96
  I2CS = bmSTART;
97
  I2DAT = (addr << 1) | 0;       // write address and direction (0's the write bit)
98
 
99
  while ((I2CS & bmDONE) == 0)
100
    ;
101
 
102
  if ((I2CS & bmBERR) || (I2CS & bmACK) == 0)    // no device answered...
103
    goto fail;
104
 
105
  while (len > 0){
106
    I2DAT = *buf++;
107
    len--;
108
 
109
    while ((I2CS & bmDONE) == 0)
110
      ;
111
 
112
    if ((I2CS & bmBERR) || (I2CS & bmACK) == 0)  // no device answered...
113
      goto fail;
114
  }
115
 
116
  I2CS |= bmSTOP;
117
  return 1;
118
 
119
 fail:
120
  I2CS |= bmSTOP;
121
  return 0;
122
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.