root / firmware / fx2 / lib / i2c-compiler-bug.c @ 70eae1d2
History | View | Annotate | Download (2.6 kB)
| 1 |
/* -*- 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 |
|
| 28 |
// issue a stop bus cycle and wait for completion
|
| 29 |
|
| 30 |
|
| 31 |
// returns non-zero if successful, else 0
|
| 32 |
unsigned char |
| 33 |
i2c_read (unsigned char addr, xdata unsigned char *buf, unsigned char len) |
| 34 |
{
|
| 35 |
volatile unsigned char junk; |
| 36 |
|
| 37 |
if (len == 0) // reading zero bytes always works |
| 38 |
return 1; |
| 39 |
|
| 40 |
// memset (buf, 0, len); // FIXME, remove
|
| 41 |
|
| 42 |
while (I2CS & bmSTOP) // wait for stop to clear |
| 43 |
; |
| 44 |
|
| 45 |
|
| 46 |
I2CS = bmSTART; |
| 47 |
I2DAT = (addr << 1) | 1; // write address and direction (1's the read bit) |
| 48 |
|
| 49 |
while ((I2CS & bmDONE) == 0) |
| 50 |
; |
| 51 |
|
| 52 |
if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered... |
| 53 |
goto fail;
|
| 54 |
|
| 55 |
if (len == 1) |
| 56 |
I2CS |= bmLASTRD; |
| 57 |
|
| 58 |
junk = I2DAT; // trigger the first read cycle
|
| 59 |
|
| 60 |
#if 1 |
| 61 |
while (len != 1){ |
| 62 |
while ((I2CS & bmDONE) == 0) |
| 63 |
; |
| 64 |
|
| 65 |
if (I2CS & bmBERR)
|
| 66 |
goto fail;
|
| 67 |
|
| 68 |
len--; |
| 69 |
if (len == 1) |
| 70 |
I2CS |= bmLASTRD; |
| 71 |
|
| 72 |
*buf++ = I2DAT; // get data, trigger another read
|
| 73 |
} |
| 74 |
#endif
|
| 75 |
|
| 76 |
// wait for final byte
|
| 77 |
|
| 78 |
while ((I2CS & bmDONE) == 0) |
| 79 |
; |
| 80 |
|
| 81 |
if (I2CS & bmBERR)
|
| 82 |
goto fail;
|
| 83 |
|
| 84 |
I2CS |= bmSTOP; |
| 85 |
*buf = I2DAT; |
| 86 |
|
| 87 |
return 1; |
| 88 |
|
| 89 |
fail:
|
| 90 |
I2CS |= bmSTOP; |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
// returns non-zero if successful, else 0
|
| 97 |
unsigned char |
| 98 |
i2c_write (unsigned char addr, xdata const unsigned char *buf, unsigned char len) |
| 99 |
{
|
| 100 |
while (I2CS & bmSTOP) // wait for stop to clear |
| 101 |
; |
| 102 |
|
| 103 |
I2CS = bmSTART; |
| 104 |
I2DAT = (addr << 1) | 0; // write address and direction (0's the write bit) |
| 105 |
|
| 106 |
while ((I2CS & bmDONE) == 0) |
| 107 |
; |
| 108 |
|
| 109 |
if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered... |
| 110 |
goto fail;
|
| 111 |
|
| 112 |
while (len > 0){ |
| 113 |
I2DAT = *buf++; |
| 114 |
len--; |
| 115 |
|
| 116 |
while ((I2CS & bmDONE) == 0) |
| 117 |
; |
| 118 |
|
| 119 |
if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered... |
| 120 |
goto fail;
|
| 121 |
} |
| 122 |
|
| 123 |
I2CS |= bmSTOP; |
| 124 |
return 1; |
| 125 |
|
| 126 |
fail:
|
| 127 |
I2CS |= bmSTOP; |
| 128 |
return 0; |
| 129 |
} |