1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// platform_i2c.c
|
4 |
|
|
//
|
5 |
|
|
// Optional I2C support for LPC2468
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2008 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Uwe Kindler <uwe_kindler@web.de>
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2008-09-11
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
//
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
//
|
50 |
|
|
//==========================================================================
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
//=============================================================================
|
54 |
|
|
// INCLUDES
|
55 |
|
|
//=============================================================================
|
56 |
|
|
#include <pkgconf/system.h>
|
57 |
|
|
#include <cyg/infra/cyg_type.h>
|
58 |
|
|
#include <cyg/hal/hal_io.h>
|
59 |
|
|
#include <cyg/infra/cyg_ass.h>
|
60 |
|
|
#include <cyg/hal/hal_endian.h>
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
#ifdef CYGHWR_HAL_ARM_LPC24XX_I2C0_SUPP
|
64 |
|
|
#include <cyg/io/i2c.h>
|
65 |
|
|
#include <cyg/io/i2c_lpc2xxx.h>
|
66 |
|
|
//=============================================================================
|
67 |
|
|
// Setup I2C bus 0
|
68 |
|
|
//=============================================================================
|
69 |
|
|
static void ea2468_i2c0_init(struct cyg_i2c_bus* bus)
|
70 |
|
|
{
|
71 |
|
|
//
|
72 |
|
|
// The LPC24xx variant sets up the I2C0 clock and powers up the I2C0
|
73 |
|
|
// on-chip peripheral. We only need to setup the pins here and
|
74 |
|
|
// leave the I2C driver to take care of the rest.
|
75 |
|
|
//
|
76 |
|
|
hal_set_pin_function(0, 27, 1); // SDA0
|
77 |
|
|
hal_set_pin_function(0, 28, 1); // SCL0
|
78 |
|
|
cyg_lpc2xxx_i2c_init(bus);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
//-----------------------------------------------------------------------------
|
83 |
|
|
// I2C bus 0
|
84 |
|
|
//
|
85 |
|
|
CYG_LPC2XXX_I2C_BUS(hal_ea2468_i2c0_bus,
|
86 |
|
|
&ea2468_i2c0_init,
|
87 |
|
|
CYGARC_HAL_LPC24XX_REG_I2C0_BASE,
|
88 |
|
|
CYGNUM_HAL_INTERRUPT_I2C,
|
89 |
|
|
CYGNUM_HAL_ARM_LPC24XX_I2C0_INT_PRIO,
|
90 |
|
|
CYGNUM_HAL_ARM_LPC24XX_I2C0_CLK,
|
91 |
|
|
CYGNUM_HAL_ARM_LPC24XX_I2C0_BUS_FREQ);
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
//-----------------------------------------------------------------------------
|
95 |
|
|
// 256-Kb I2C CMOS Serial EEPROM
|
96 |
|
|
//
|
97 |
|
|
CYG_I2C_DEVICE(i2c_cat24c256_eeprom,
|
98 |
|
|
&hal_ea2468_i2c0_bus,
|
99 |
|
|
0x50,
|
100 |
|
|
0,
|
101 |
|
|
CYG_I2C_DEFAULT_DELAY);
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
//===========================================================================
|
105 |
|
|
// Check if device is busy - Acknowledge polling can be used to determine
|
106 |
|
|
// if the CAT24C256 is busy writing or is ready to accept commands.
|
107 |
|
|
// Polling is implemented by interrogating the device with a Selective Read
|
108 |
|
|
// command (see READ OPERATIONS). The CAT24C256 will not acknowledge the
|
109 |
|
|
// Slave address, as long as internal Write is in progress.
|
110 |
|
|
//===========================================================================
|
111 |
|
|
static bool cat24c256_is_busy(void)
|
112 |
|
|
{
|
113 |
|
|
cyg_uint16 addr = 0;
|
114 |
|
|
cyg_uint32 result;
|
115 |
|
|
|
116 |
|
|
cyg_i2c_transaction_begin(&i2c_cat24c256_eeprom);
|
117 |
|
|
result = cyg_i2c_transaction_tx(&i2c_cat24c256_eeprom,
|
118 |
|
|
true, (cyg_uint8*)&addr, 2, true);
|
119 |
|
|
cyg_i2c_transaction_end(&i2c_cat24c256_eeprom);
|
120 |
|
|
|
121 |
|
|
return (result != 2);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
//===========================================================================
|
126 |
|
|
// Write up to 64 bytes of data - one single write cycle
|
127 |
|
|
//===========================================================================
|
128 |
|
|
static cyg_uint32 cat24c256_do_write_cyle(cyg_uint16 addr,
|
129 |
|
|
cyg_uint8 *pdata,
|
130 |
|
|
cyg_uint8 size)
|
131 |
|
|
{
|
132 |
|
|
cyg_uint32 result;
|
133 |
|
|
|
134 |
|
|
cyg_i2c_transaction_begin(&i2c_cat24c256_eeprom);
|
135 |
|
|
addr = CYG_CPU_TO_BE16(addr);
|
136 |
|
|
result = cyg_i2c_transaction_tx(&i2c_cat24c256_eeprom,
|
137 |
|
|
true, (cyg_uint8*)&addr, 2, false);
|
138 |
|
|
if (result)
|
139 |
|
|
{
|
140 |
|
|
result = cyg_i2c_transaction_tx(&i2c_cat24c256_eeprom,
|
141 |
|
|
false, pdata, size, true);
|
142 |
|
|
}
|
143 |
|
|
cyg_i2c_transaction_end(&i2c_cat24c256_eeprom);
|
144 |
|
|
|
145 |
|
|
return result;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
//===========================================================================
|
150 |
|
|
// Write data to eeprom (page write)
|
151 |
|
|
// If the call is blocking (blocking == true) then the function will poll
|
152 |
|
|
// the device if it is busy writing. If it is non blocking then the function
|
153 |
|
|
// immediately return 0 if the device is busy
|
154 |
|
|
//===========================================================================
|
155 |
|
|
cyg_uint32 hal_lpc_eeprom_write(cyg_uint16 addr,
|
156 |
|
|
cyg_uint8 *pdata,
|
157 |
|
|
cyg_uint16 size,
|
158 |
|
|
cyg_bool blocking)
|
159 |
|
|
{
|
160 |
|
|
cyg_uint32 result;
|
161 |
|
|
cyg_uint16 wsize;
|
162 |
|
|
cyg_uint16 datasize = size;
|
163 |
|
|
|
164 |
|
|
CYG_ASSERT(addr < 0x8000, "Address out of range");
|
165 |
|
|
CYG_ASSERT(pdata, "Invalid data pointer");
|
166 |
|
|
CYG_ASSERT((size <= 0x8000) && (size > 0), "Invalid data size");
|
167 |
|
|
CYG_ASSERT((addr + size) < 0x8000,
|
168 |
|
|
"Attempt to write data beyond end of memory.");
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
do
|
172 |
|
|
{
|
173 |
|
|
if (cat24c256_is_busy())
|
174 |
|
|
{
|
175 |
|
|
if (blocking)
|
176 |
|
|
{
|
177 |
|
|
while (cat24c256_is_busy()) {};
|
178 |
|
|
}
|
179 |
|
|
else
|
180 |
|
|
{
|
181 |
|
|
return 0;
|
182 |
|
|
}
|
183 |
|
|
} // if (cat24c256_is_busy())
|
184 |
|
|
wsize = (datasize > 64) ? 64 : datasize;
|
185 |
|
|
result = cat24c256_do_write_cyle(addr, pdata, wsize);
|
186 |
|
|
datasize -= result;
|
187 |
|
|
addr += wsize;
|
188 |
|
|
pdata += wsize;
|
189 |
|
|
} while (datasize && (result == wsize));
|
190 |
|
|
|
191 |
|
|
return size - datasize;
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
//===========================================================================
|
196 |
|
|
// Read data from eeprom (selective, sequential read)
|
197 |
|
|
// If the call is blocking (blocking == true) then the function will poll
|
198 |
|
|
// the device if it is busy writing. If it is non blocking then the function
|
199 |
|
|
// immediately return 0 if the device is busy
|
200 |
|
|
//===========================================================================
|
201 |
|
|
cyg_uint32 hal_lpc_eeprom_read(cyg_uint16 addr,
|
202 |
|
|
cyg_uint8 *pdata,
|
203 |
|
|
cyg_uint16 size,
|
204 |
|
|
cyg_bool blocking)
|
205 |
|
|
{
|
206 |
|
|
cyg_uint32 result;
|
207 |
|
|
|
208 |
|
|
CYG_ASSERT(addr < 0x8000, "Address out of range");
|
209 |
|
|
CYG_ASSERT(pdata, "Invalid data pointer");
|
210 |
|
|
CYG_ASSERT((size <= 64) && (size > 0), "Invalid data size");
|
211 |
|
|
CYG_ASSERT((addr + size) < 0x8000,
|
212 |
|
|
"Attempt to read data beyond end of memory.");
|
213 |
|
|
|
214 |
|
|
if (cat24c256_is_busy())
|
215 |
|
|
{
|
216 |
|
|
if (blocking)
|
217 |
|
|
{
|
218 |
|
|
while (cat24c256_is_busy());
|
219 |
|
|
}
|
220 |
|
|
else
|
221 |
|
|
{
|
222 |
|
|
return 0;
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
cyg_i2c_transaction_begin(&i2c_cat24c256_eeprom);
|
227 |
|
|
addr = CYG_CPU_TO_BE16(addr);
|
228 |
|
|
result = cyg_i2c_transaction_tx(&i2c_cat24c256_eeprom,
|
229 |
|
|
true, (cyg_uint8*)&addr, 2, false);
|
230 |
|
|
if (result)
|
231 |
|
|
{
|
232 |
|
|
result = cyg_i2c_transaction_rx(&i2c_cat24c256_eeprom,
|
233 |
|
|
true, pdata, size, true, true);
|
234 |
|
|
}
|
235 |
|
|
cyg_i2c_transaction_end(&i2c_cat24c256_eeprom);
|
236 |
|
|
|
237 |
|
|
return result;
|
238 |
|
|
}
|
239 |
|
|
#endif // #ifdef CYGHWR_HAL_ARM_LPC24XX_I2C0_SUPP
|
240 |
|
|
|
241 |
|
|
//-----------------------------------------------------------------------------
|
242 |
|
|
// EOF platform_i2c.c
|
243 |
|
|
|