1 // ====================================================================
5 // ====================================================================
6 //####ECOSGPLCOPYRIGHTBEGIN####
7 // -------------------------------------------
8 // This file is part of eCos, the Embedded Configurable Operating System.
9 // Copyright (C) 2005 eCosCentric LTD
11 // eCos is free software; you can redistribute it and/or modify it under
12 // the terms of the GNU General Public License as published by the Free
13 // Software Foundation; either version 2 or (at your option) any later version.
15 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
16 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // You should have received a copy of the GNU General Public License along
21 // with eCos; if not, write to the Free Software Foundation, Inc.,
22 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 // As a special exception, if other files instantiate templates or use macros
25 // or inline functions from this file, or you compile this file and link it
26 // with other works to produce a work based on this file, this file does not
27 // by itself cause the resulting work to be covered by the GNU General Public
28 // License. However the source code for this file must still be made available
29 // in accordance with section (3) of the GNU General Public License.
31 // This exception does not invalidate any other reasons why a work based on
32 // this file might be covered by the GNU General Public License.
33 // ====================================================================
34 //#####DESCRIPTIONBEGIN####
36 // Author(s): Harald Brandl (harald.brandl@fh-joanneum.at)
37 // Contributors: Harald Brandl
39 // Purpose: EEPROM I/O
42 //####DESCRIPTIONEND####
44 // ====================================================================
46 #include <cyg/hal/hal_netarm.h>
47 #include <sys/types.h>
49 static void wait(int time)
53 for(i=0; i < time; i++);
56 static void i2cStop(void)
59 *PORTC &= ~0x000000c0; // SDA = 0, SLK = 0
61 *PORTC |= 0x00000040; // SLK = 1
63 *PORTC |= 0x00000080; // SDA = 1
66 static void i2cStart(void)
68 *PORTC |= 0x00c000c0; // SDA = 1, SLK = 1
70 *PORTC &= ~0x00000080; // SDA = 0
72 *PORTC &= ~0x00000040; // SLK = 0
75 static void i2cPutByte(char byte)
83 bit = (byte >> i) & 1;
84 *PORTC = (*PORTC & ~0x80) | (bit << 7); // SDA = data
85 *PORTC |= 0x00000040; // SLK = 1
87 *PORTC &= ~0x00000040; // SLK = 0
90 *PORTC |= 0x00000080; // SDA = 1
93 static char i2cGetByte(void)
98 *PORTC &= ~0x00800000;
101 *PORTC |= 0x00000040; // SLK = 1
102 byte |= ((*PORTC & 0x80) >> 7) << i; // data = SDA
104 *PORTC &= ~0x00000040; // SLK = 0
107 *PORTC |= 0x00800080; // SDA = 1
111 static void i2cGiveAck(void)
113 *PORTC |= 0x00c00000;
114 *PORTC &= ~0x00000080; // SDA = 0
116 *PORTC |= 0x00000040; // SLK = 1
118 *PORTC &= ~0x00000040; // SLK = 0
120 *PORTC |= 0x00000080; // SDA = 1
124 static void i2cGetAck(void)
126 *PORTC &= ~0x00800000; // SDA in
127 *PORTC |= 0x00400040; // SLK = 1
128 while(*PORTC & 0x80); // wait for SDA = 1
129 *PORTC &= ~0x00000040; // SLK = 0
134 *PORTC &= ~0xc0000000; // mode GPIO
138 void eepromPollAck(int deviceAddr)
142 *PORTC |= 0x00400040; // SLK = 1
146 i2cPutByte(deviceAddr);
147 *PORTC &= ~0x00800000; // SDA in
148 *PORTC |= 0x00400040; // SLK = 1
149 if((*PORTC & 0x80) == 0)
151 *PORTC &= ~0x00000040; // SLK = 0
154 *PORTC &= ~0x00000040; // SLK = 0
158 void eepromRead(int deviceAddr, int readAddr, char *buf, int numBytes)
164 i2cPutByte(deviceAddr);
166 i2cPutByte(readAddr >> 8);
168 i2cPutByte(readAddr & 0xff);
171 i2cPutByte(deviceAddr | 1); // set read flag
174 for(i=0;i<numBytes;i++)
176 buf[i] = i2cGetByte();
184 void eepromWrite(int deviceAddr, int writeAddr, char *buf, int numBytes)
190 i2cPutByte(deviceAddr);
192 i2cPutByte(writeAddr >> 8);
194 i2cPutByte(writeAddr & 0xff);
197 for(i=0; i<numBytes; i++)