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.
34 //####ECOSGPLCOPYRIGHTEND####
35 // ====================================================================
36 //#####DESCRIPTIONBEGIN####
38 // Author(s): Harald Brandl (harald.brandl@fh-joanneum.at)
39 // Contributors: Harald Brandl
41 // Purpose: EEPROM I/O
44 //####DESCRIPTIONEND####
46 // ====================================================================
48 #include <cyg/hal/hal_modnet50.h>
51 static void wait(int time)
55 for(i=0; i < time; i++);
58 /* send i2c stop condition */
59 static void i2cStop(void)
61 HAL_OR_UINT32(PORTC, 0x00c00000);
62 HAL_AND_UINT32(PORTC, ~0x000000c0); // SDA = 0, SLK = 0
64 HAL_OR_UINT32(PORTC, 0x00000040); // SLK = 1
66 HAL_OR_UINT32(PORTC, 0x00000080); // SDA = 1
69 /* send i2c start condition */
70 static void i2cStart(void)
72 HAL_OR_UINT32(PORTC, 0x00c000c0); // SDA = 1, SLK = 1
74 HAL_AND_UINT32(PORTC, ~0x00000080); // SDA = 0
76 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
79 static void i2cPutByte(char byte)
83 HAL_OR_UINT32(PORTC, 0x00c00000);
87 bit = (byte >> i) & 1;
88 HAL_READ_UINT32(PORTC, reg);
89 HAL_WRITE_UINT32(PORTC, (reg & ~0x80) | (bit << 7)); // SDA = data
90 HAL_OR_UINT32(PORTC, 0x00000040); // SLK = 1
92 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
95 HAL_OR_UINT32(PORTC, 0x00000080); // SDA = 1
98 static char i2cGetByte(void)
103 HAL_AND_UINT32(PORTC, ~0x00800000);
104 for(i=7; i >= 0; i--)
106 HAL_OR_UINT32(PORTC, 0x00000040); // SLK = 1
107 HAL_READ_UINT32(PORTC, reg);
108 byte |= ((reg & 0x80) >> 7) << i; // data = SDA
110 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
113 HAL_OR_UINT32(PORTC, 0x00800080); // SDA = 1
117 /* acknowledge received bytes */
118 static void i2cGiveAck(void)
120 HAL_OR_UINT32(PORTC, 0x00c00000);
121 HAL_AND_UINT32(PORTC, ~0x00000080); // SDA = 0
123 HAL_OR_UINT32(PORTC, 0x00000040); // SLK = 1
125 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
127 HAL_OR_UINT32(PORTC, 0x00000080); // SDA = 1
131 /* wait for acknowledge from slaves */
132 static void i2cGetAck(void)
136 HAL_AND_UINT32(PORTC, ~0x00800000); // SDA in
137 HAL_OR_UINT32(PORTC, 0x00400040); // SLK = 1
140 HAL_READ_UINT32(PORTC, reg);
141 }while(reg & 0x80); // wait for SDA = 1
142 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
145 void cyg_netarm_initI2C(void)
147 HAL_AND_UINT32(PORTC, ~0xc0000000); // mode GPIO
151 /* wait until eeprom's internal write cycle has finished */
152 void cyg_netarm_eepromPollAck(int deviceAddr)
158 HAL_OR_UINT32(PORTC, 0x00400040); // SLK = 1
162 i2cPutByte(deviceAddr);
163 HAL_AND_UINT32(PORTC, ~0x00800000); // SDA in
164 HAL_OR_UINT32(PORTC, 0x00400040); // SLK = 1
165 HAL_READ_UINT32(PORTC, reg);
166 if((reg & 0x80) == 0)
168 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
171 HAL_AND_UINT32(PORTC, ~0x00000040); // SLK = 0
175 /* reads numBytes from eeprom starting at readAddr into buf */
176 void cyg_netarm_eepromRead(int deviceAddr, int readAddr, char *buf, int numBytes)
182 i2cPutByte(deviceAddr);
184 i2cPutByte(readAddr >> 8);
186 i2cPutByte(readAddr & 0xff);
189 i2cPutByte(deviceAddr | 1); // set read flag
192 for(i=0;i<numBytes;i++)
194 buf[i] = i2cGetByte();
202 /* writes up to a page of 32 bytes from buf into eeprom;
203 max. number of bytes depends on the offset within a page, indexed by the
204 lower 5 bits of writeAddr and equals 32 - offset */
205 void cyg_netarm_eepromWrite(int deviceAddr, int writeAddr, char *buf, int numBytes)
211 i2cPutByte(deviceAddr);
213 i2cPutByte(writeAddr >> 8);
215 i2cPutByte(writeAddr & 0xff);
218 for(i=0; i<numBytes; i++)