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

Subversion Repositories gecko3

[/] [gecko3/] [trunk/] [GECKO3COM/] [gecko3com-fw/] [firmware/] [lib/] [i2c.c] - Blame information for rev 32

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nussgipfel
/* -*- c++ -*- */
2
/*
3
 * Copyright 2003 Free Software Foundation, Inc.
4
 *
5
 * This file is part of GNU Radio
6
 *
7
 * GNU Radio is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3, or (at your option)
10
 * any later version.
11
 *
12
 * GNU Radio is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with GNU Radio; see the file COPYING.  If not, write to
19
 * the Free Software Foundation, Inc., 51 Franklin Street,
20
 * Boston, MA 02110-1301, USA.
21
 */
22
 
23
#include "i2c.h"
24
#include "fx2regs.h"
25
#include <string.h>
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.