1 |
786 |
skrzyp |
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
I2C Support
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
Overview
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
Overview
|
43 |
|
|
eCos Support for I2C, the Inter IC Bus
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
Description
|
47 |
|
|
|
48 |
|
|
The Inter IC Bus (I2C) is one of a number of serial bus technologies.
|
49 |
|
|
It can be used to connect a processor to one or more peripheral chips,
|
50 |
|
|
for example analog-to-digital convertors or real time clocks, using
|
51 |
|
|
only a small number of pins and PCB tracks. The technology was
|
52 |
|
|
originally developed by Philips Semiconductors but is supported by
|
53 |
|
|
many other vendors. The bus specification is freely available.
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
In a typical I2C system the processor acts as the I2C bus master. The
|
57 |
|
|
peripheral chips act as slaves. The bus consists of just two wires:
|
58 |
|
|
SCL carries a clock signal generated by the master, and SDA is a
|
59 |
|
|
bi-directional data line. The normal clock frequency is 100KHz. Each
|
60 |
|
|
slave has a 7-bit address. With some chips the address is hard-wired,
|
61 |
|
|
and it is impossible to have two of these chips on the same bus. With
|
62 |
|
|
other chips it is possible to choose between one of a small number of
|
63 |
|
|
addresses by connecting spare pins to either VDD or GND.
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
An I2C data transfer involves a number of stages:
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
The bus master generates a start condition, a high-to-low transition
|
71 |
|
|
on the SDA line while SCL is kept high. This signalling cannot occur
|
72 |
|
|
during data transfer.
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
The bus master clocks the 7-bit slave address onto the SDA line,
|
76 |
|
|
followed by a direction bit to distinguish between reads and writes.
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
The addressed device acknowledges. If the master does not see an
|
80 |
|
|
acknowledgement then this suggests it is using the wrong address for
|
81 |
|
|
the slave device.
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
If the master is transmitting data to the slave then it will send this
|
85 |
|
|
data one byte at a time. The slave acknowledges each byte. If the
|
86 |
|
|
slave is unable to accept more data, for example because it has run
|
87 |
|
|
out of buffer space, then it will generate a nack and the master
|
88 |
|
|
should stop sending.
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
If the master is receiving data from the slave then the slave will
|
92 |
|
|
send this data one byte at a time. The master should acknowledge each
|
93 |
|
|
byte, until the last one. When the master has received all the data it
|
94 |
|
|
wants it should generate a nack and the slave will stop sending. This
|
95 |
|
|
nack is essential because it causes the slave to stop driving the SDA
|
96 |
|
|
line, releasing it back to the master.
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
It is possible to switch direction in a single transfer, using what is
|
100 |
|
|
known as a repeated start. This involves generating another start
|
101 |
|
|
condition, sending the 7-bit address again, followed by a new
|
102 |
|
|
direction bit.
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
At the end of a transfer the master should generate a stop condition,
|
106 |
|
|
a low-to-high transition on the SDA line while SCL is kept high. Again
|
107 |
|
|
this signalling does not occur at other times.
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
There are a number of extensions. The I2C bus supports multiple bus
|
112 |
|
|
masters and there is an arbitration procedure to allow a master to
|
113 |
|
|
claim the bus. Some devices can have 10-bit addresses rather than
|
114 |
|
|
7-bit addresses. There is a fast mode operating at 400KHz instead of
|
115 |
|
|
the usual 100KHz, and a high-speed mode operating at 3.4MHz. Currently
|
116 |
|
|
most I2C-based systems do not involve any of these extensions.
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
At the hardware level I2C bus master support can be implemented in one
|
120 |
|
|
of two ways. Some processors provide a dedicated I2C device, with the
|
121 |
|
|
hardware performing much of the work. On other processors the I2C
|
122 |
|
|
device is implemented in software, by bit-banging some GPIO pins. The
|
123 |
|
|
latter approach can consume a significant number of cpu cycles, but is
|
124 |
|
|
often acceptable because only occasional access to the I2C devices is
|
125 |
|
|
needed.
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
eCos Support for I2C
|
130 |
|
|
|
131 |
|
|
The eCos I2C support for any given platform is spread over a number of
|
132 |
|
|
different packages:
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
This package, CYGPKG_IO_I2C, exports a generic API
|
137 |
|
|
for accessing devices attached to an I2C bus. This API handles issues
|
138 |
|
|
such as locking between threads. The package does not contain any
|
139 |
|
|
hardware-specific code. Instead it will use a separate I2C bus driver
|
140 |
|
|
to handle the hardware, and it defines the interface that such bus
|
141 |
|
|
drivers should provide. The package only provides support for a bus
|
142 |
|
|
master, not for acting as a slave device.
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
CYGPKG_IO_I2C also provides the
|
146 |
|
|
hardware-independent portion of a bit-banged bus implementation. This
|
147 |
|
|
needs to be complemented by a hardware-specific function that actually
|
148 |
|
|
manipulates the SDA and SCL lines.
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
If the processor has a dedicated I2C device then there will be a bus
|
152 |
|
|
driver package for that hardware. The processor may be used on
|
153 |
|
|
many different platforms and the same bus driver can be used on each one.
|
154 |
|
|
The actual I2C devices attached to the bus will vary from one platform to
|
155 |
|
|
the next.
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
The generic API depends on cyg_i2c_device
|
159 |
|
|
data structures. These contain the information needed by a bus driver,
|
160 |
|
|
for example the device address. Usually the data structures are
|
161 |
|
|
provided by the platform HAL since it is that package which knows
|
162 |
|
|
about all the devices on the platform.
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
On some development boards the I2C lines are brought out to expansion
|
166 |
|
|
connectors, allowing end users to add extra devices. In such cases the
|
167 |
|
|
platform HAL may not know about all the devices on the board. Data
|
168 |
|
|
structures for the additional devices can instead be supplied by
|
169 |
|
|
application code.
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
If the board uses a bit-banged bus then typically the platform HAL
|
173 |
|
|
will also instantiate the bus instance, providing the function that
|
174 |
|
|
handles the low-level SDA and SCL manipulation. Usually this code
|
175 |
|
|
cannot be shared because each board may use different GPIO pins for
|
176 |
|
|
driving SCL and SDA, so the code belongs in the platform HAL rather
|
177 |
|
|
than in a separate package.
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
Some types of I2C devices may have their own driver package. For
|
181 |
|
|
example a common type of I2C device is a battery-backed wallclock, and
|
182 |
|
|
eCos defines how these devices should be supported. Such an I2C device
|
183 |
|
|
will have its own wallclock device driver and the device will not be
|
184 |
|
|
accessed directly by application code. For other types of device eCos
|
185 |
|
|
does not define an API and there will not be separate device driver
|
186 |
|
|
packages. Instead application code is expected to use the
|
187 |
|
|
cyg_i2c_device structures directly to access
|
188 |
|
|
the hardware.
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
Typically all appropriate packages will be loaded automatically when
|
193 |
|
|
you configure eCos for a given platform. If the application does not use
|
194 |
|
|
any of the I2C I/O facilities, directly or indirectly, then linker
|
195 |
|
|
garbage collection should eliminate all unnecessary code and data. All
|
196 |
|
|
necessary initialization should happen automatically. However the
|
197 |
|
|
exact details may depend on the platform, so the platform HAL
|
198 |
|
|
documentation should be checked for further details.
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
There is one important exception to this: if the I2C devices are
|
202 |
|
|
attached to an expansion connector then the platform HAL will not know
|
203 |
|
|
about these devices. Instead more work will have to be done by
|
204 |
|
|
application code.
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
I2C Interface
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
I2C Functions
|
216 |
|
|
allow applications and other packages to access I2C devices
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
#include <cyg/io/i2c.h>
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
cyg_uint32 cyg_i2c_tx
|
225 |
|
|
const cyg_i2c_device* device
|
226 |
|
|
const cyg_uint8* tx_data
|
227 |
|
|
cyg_uint32 count
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
cyg_uint32 cyg_i2c_rx
|
231 |
|
|
const cyg_i2c_device* device
|
232 |
|
|
cyg_uint8* rx_data
|
233 |
|
|
cyg_uint32 count
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
void cyg_i2c_transaction_begin
|
237 |
|
|
const cyg_i2c_device* device
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
cyg_bool cyg_i2c_transaction_begin_nb
|
241 |
|
|
const cyg_i2c_device* device
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
cyg_uint32 cyg_i2c_transaction_tx
|
245 |
|
|
const cyg_i2c_device* device
|
246 |
|
|
cyg_bool send_start
|
247 |
|
|
const cyg_uint8* tx_data
|
248 |
|
|
cyg_uint32 count
|
249 |
|
|
cyg_bool send_stop
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
cyg_uint32 cyg_i2c_transaction_rx
|
253 |
|
|
const cyg_i2c_device* device
|
254 |
|
|
cyg_bool send_start
|
255 |
|
|
cyg_uint8* rx_data
|
256 |
|
|
cyg_uint32 count
|
257 |
|
|
cyg_bool send_nack
|
258 |
|
|
cyg_bool send_stop
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
void cyg_i2c_transaction_stop
|
262 |
|
|
const cyg_i2c_device* device
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
void cyg_i2c_transaction_end
|
266 |
|
|
const cyg_i2c_device* device
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
Description
|
272 |
|
|
|
273 |
|
|
All I2C functions take a pointer to a
|
274 |
|
|
cyg_i2c_device structure as their first
|
275 |
|
|
argument. These structures are usually provided by the platform HAL.
|
276 |
|
|
They contain the information needed by the I2C bus driver to interact
|
277 |
|
|
with the device, for example the device address.
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
An I2C transaction involves the following stages:
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
Perform thread-level locking on the bus. Only one thread at a time is
|
285 |
|
|
allowed to access an I2C bus. This eliminates the need to worry about
|
286 |
|
|
locking at the bus driver level. If a platform involves multiple I2C
|
287 |
|
|
buses then each one will have its own lock.
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
Generate a start condition, send the address and direction bit, and
|
291 |
|
|
wait for an acknowledgement from the addressed device.
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
Either transmit data to or receive data from the addressed device.
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
The previous two steps may be repeated several times, allowing data to
|
298 |
|
|
move in both directions during a single transfer.
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
Generate a stop condition, ending the current data transfer. It is
|
302 |
|
|
now possible to start another data transfer while the bus is still
|
303 |
|
|
locked, if desired.
|
304 |
|
|
|
305 |
|
|
|
306 |
|
|
End the transaction by unlocking the bus, allowing other threads to
|
307 |
|
|
access other devices on the bus.
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
The simple functions cyg_i2c_tx and
|
312 |
|
|
cyg_i2c_rx perform all these steps in a single
|
313 |
|
|
call, making them suitable for many I/O operations. The alternative
|
314 |
|
|
transaction-oriented functions provide greater control when
|
315 |
|
|
appropriate, for example if a repeated start is necessary for a
|
316 |
|
|
bi-directional data transfer.
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
With the exception of
|
320 |
|
|
cyg_i2c_transaction_begin_nb all the functions
|
321 |
|
|
will block until completion. The tx routines will return 0 if the
|
322 |
|
|
specified device does not respond to its address, or the number of
|
323 |
|
|
bytes actually transferred. This may be less than the number requested
|
324 |
|
|
if the device sends an early nack, for example because it has run out
|
325 |
|
|
of buffer space. The rx routines will return 0 or the number of bytes
|
326 |
|
|
received. Usually this will be the same as the
|
327 |
|
|
count parameter. A slave device has no way of
|
328 |
|
|
indicating to the master that no more data is available, so the rx
|
329 |
|
|
operation cannot complete early.
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
I2C operations should always be performed at thread-level or during
|
333 |
|
|
system initialization, and not inside an ISR or DSR. This greatly
|
334 |
|
|
simplifies locking. Also a typical ISR or DSR should not perform a
|
335 |
|
|
blocking operation such as an I2C transfer.
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
Simple Transfers
|
340 |
|
|
|
341 |
|
|
cyg_i2c_tx and cyg_i2c_rx
|
342 |
|
|
can be used for simple data transfers. They both go through the
|
343 |
|
|
following steps: lock the bus, generate the start condition, send the
|
344 |
|
|
device address and the direction bit, either send or receive the data,
|
345 |
|
|
generate the stop condition, and unlock the bus. At the end of a
|
346 |
|
|
transfer the bus is back in its idle state, ready for the next
|
347 |
|
|
transfer.
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
cyg_i2c_tx returns the number of bytes actually
|
351 |
|
|
transmitted. This may be 0 if the device does not respond when its
|
352 |
|
|
address is sent out. It may be less than the number of bytes requested
|
353 |
|
|
if the device generates an early nack, typically because it has run
|
354 |
|
|
out of buffer space.
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
cyg_i2c_rx returns 0 if the device does not
|
358 |
|
|
respond when its address is sent out, or the number of bytes actually
|
359 |
|
|
received. Usually this will be the number of bytes requested because
|
360 |
|
|
an I2C slave device has no way of aborting an rx operation early.
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
Transactions
|
365 |
|
|
|
366 |
|
|
To allow multiple threads to access devices on the I2C some locking is
|
367 |
|
|
required. This is encapsulated inside transactions. The
|
368 |
|
|
cyg_i2c_tx and cyg_i2c_rx
|
369 |
|
|
functions implicitly use such transactions, but the functionality is
|
370 |
|
|
also available directly to application code. Amongst other things
|
371 |
|
|
transactions can be used for more complicated interactions with I2C
|
372 |
|
|
devices, in particular ones involving repeated starts.
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
cyg_i2c_transaction_begin must be used at the
|
376 |
|
|
start of a transaction. This performs thread-level locking on the bus,
|
377 |
|
|
blocking if it is currently in use by another thread.
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
cyg_i2c_transaction_begin_nb is a non-blocking
|
381 |
|
|
variant, useful for threads which cannot afford to block for an
|
382 |
|
|
indefinite period. If the bus is currently locked the function returns
|
383 |
|
|
false immediately. If the bus is not locked then it acts as
|
384 |
|
|
cyg_i2c_transaction_begin and returns true.
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
Once the bus has been locked it is possible to perform one or more
|
388 |
|
|
data transfers by calling
|
389 |
|
|
cyg_i2c_transaction_tx,
|
390 |
|
|
cyg_i2c_transaction_rx and
|
391 |
|
|
cyg_i2c_transaction_stop. Code should ensure that
|
392 |
|
|
a stop condition has been generated by the end of a transaction.
|
393 |
|
|
|
394 |
|
|
|
395 |
|
|
Once the transaction is complete
|
396 |
|
|
cyg_i2c_transaction_end should be called. This
|
397 |
|
|
unlocks the bus, allowing other threads to perform I2C I/O to devices
|
398 |
|
|
on the same bus.
|
399 |
|
|
|
400 |
|
|
|
401 |
|
|
As an example consider reading the registers in an FS6377 programmable
|
402 |
|
|
clock generator. The first step is to write a byte 0 to the device,
|
403 |
|
|
setting the current register to 0. Then a repeated start condition
|
404 |
|
|
should be generated and it is possible to read the 16 byte-wide
|
405 |
|
|
registers, starting with the current one. Typical code for this might
|
406 |
|
|
look like:
|
407 |
|
|
|
408 |
|
|
|
409 |
|
|
cyg_uint8 tx_data[1];
|
410 |
|
|
cyg_uint8 rx_data[16];
|
411 |
|
|
|
412 |
|
|
cyg_i2c_transaction_begin(&hal_alaia_i2c_fs6377);
|
413 |
|
|
tx_data[0] = 0x00;
|
414 |
|
|
cyg_i2c_transaction_tx(&hal_alaia_i2c_fs6377,
|
415 |
|
|
true, tx_data, 1, false);
|
416 |
|
|
cyg_i2c_transaction_rx(&hal_alaia_i2c_fs6377,
|
417 |
|
|
true, rx_data, 16, true, true);
|
418 |
|
|
cyg_i2c_transaction_end(&hal_alaia_i2c_fs6377);
|
419 |
|
|
|
420 |
|
|
|
421 |
|
|
Here hal_alaia_i2c_fs6377 is a
|
422 |
|
|
cyg_i2c_device structure provided by the
|
423 |
|
|
platform HAL. A transaction is begun, locking the bus. Then there is a
|
424 |
|
|
transmit for a single byte. This transmit involves generating a start
|
425 |
|
|
condition and sending the address and direction bit, but not a stop
|
426 |
|
|
condition. Next there is a receive for 16 bytes. This also involves a
|
427 |
|
|
start condition, which the device will interpret as a repeated start
|
428 |
|
|
because it has not yet seen a stop. The start condition will be
|
429 |
|
|
followed by the address and direction bit, and then the device will
|
430 |
|
|
start transmitting the register contents. Once all 16 bytes have been
|
431 |
|
|
received the rx routine will send a nack rather than an ack, halting
|
432 |
|
|
the transfer, and then a stop condition is generated. Finally the
|
433 |
|
|
transaction is ended, unlocking the bus.
|
434 |
|
|
|
435 |
|
|
|
436 |
|
|
The arguments to cyg_i2c_transaction_tx are as
|
437 |
|
|
follows:
|
438 |
|
|
|
439 |
|
|
|
440 |
|
|
|
441 |
|
|
const cyg_i2c_device* device
|
442 |
|
|
|
443 |
|
|
This identifies the I2C device that should be used.
|
444 |
|
|
|
445 |
|
|
|
446 |
|
|
|
447 |
|
|
cyg_bool send_start
|
448 |
|
|
|
449 |
|
|
If true, generate a start condition and send the address and direction
|
450 |
|
|
bit. If false, skip those steps and go straight to transmitting the
|
451 |
|
|
actual data. The latter can be useful if the data to be transmitted is
|
452 |
|
|
spread over several buffers. The first tx call will involve generating
|
453 |
|
|
the start condition but subsequent tx calls can skip this and just
|
454 |
|
|
continue from the previous one.
|
455 |
|
|
|
456 |
|
|
send_start must be true if the tx call is the first
|
457 |
|
|
operation in a transaction, or if the previous call was an rx or stop.
|
458 |
|
|
|
459 |
|
|
|
460 |
|
|
|
461 |
|
|
const cyg_uint8* tx_data
|
462 |
|
|
cyg_uint32 count
|
463 |
|
|
|
464 |
|
|
These arguments specify the data to be transmitted to the device.
|
465 |
|
|
|
466 |
|
|
|
467 |
|
|
|
468 |
|
|
cyg_bool send_stop
|
469 |
|
|
|
470 |
|
|
If true, generate a stop condition at the end of the transmit. Usually
|
471 |
|
|
this is done only if the transmit is the last operation in a
|
472 |
|
|
transaction.
|
473 |
|
|
|
474 |
|
|
|
475 |
|
|
|
476 |
|
|
|
477 |
|
|
The arguments to cyg_i2c_transaction_rx are as
|
478 |
|
|
follows:
|
479 |
|
|
|
480 |
|
|
|
481 |
|
|
|
482 |
|
|
const cyg_i2c_device* device
|
483 |
|
|
|
484 |
|
|
This identifies the I2C device that should be used.
|
485 |
|
|
|
486 |
|
|
|
487 |
|
|
|
488 |
|
|
cyg_bool send_start
|
489 |
|
|
|
490 |
|
|
If true, generate a start condition and send the address and direction
|
491 |
|
|
bit. If false, skip those steps and go straight to receiving the
|
492 |
|
|
actual data. The latter can be useful if the incoming data should be
|
493 |
|
|
spread over several buffers. The first rx call will involve generating
|
494 |
|
|
the start condition but subsequent rx calls can skip this and just
|
495 |
|
|
continue from the previous one. Another use is for devices which can
|
496 |
|
|
send variable length data, consisting of an initial length and then
|
497 |
|
|
the actual data. The first rx will involve generating the start
|
498 |
|
|
condition and reading the length, a subsequent rx will then just read
|
499 |
|
|
the data.
|
500 |
|
|
|
501 |
|
|
send_start must be true if the rx call is the first
|
502 |
|
|
operation in a transaction, if the previous call was a tx or stop, or
|
503 |
|
|
if the previous call was an an rx and the send_nack
|
504 |
|
|
flag was set.
|
505 |
|
|
|
506 |
|
|
|
507 |
|
|
|
508 |
|
|
cyg_uint8* rx_data
|
509 |
|
|
cyg_uint32 count
|
510 |
|
|
|
511 |
|
|
These arguments specify how much data should be received and where it
|
512 |
|
|
should be placed.
|
513 |
|
|
|
514 |
|
|
|
515 |
|
|
|
516 |
|
|
cyg_bool send_nack
|
517 |
|
|
|
518 |
|
|
If true generate a nack instead of an ack for the last byte received.
|
519 |
|
|
This causes the slave to end its transmit. The next operation should
|
520 |
|
|
either involve a repeated start or a stop.
|
521 |
|
|
send_nack should be set to false only if
|
522 |
|
|
send_stop is also false, the next operation will be
|
523 |
|
|
another rx, and that rx does not specify send_start.
|
524 |
|
|
|
525 |
|
|
|
526 |
|
|
|
527 |
|
|
cyg_bool send_stop
|
528 |
|
|
|
529 |
|
|
If true, generate a stop condition at the end of the transmit. Usually
|
530 |
|
|
this is done only if the transmit is the last operation in a
|
531 |
|
|
transaction.
|
532 |
|
|
|
533 |
|
|
|
534 |
|
|
|
535 |
|
|
|
536 |
|
|
The final transaction-oriented function is
|
537 |
|
|
cyg_i2c_transaction_stop. This just generates a
|
538 |
|
|
stop condition. It should be used if the previous operation was a tx
|
539 |
|
|
or rx that, for some reason, did not set the
|
540 |
|
|
send_stop flag. A stop condition must be generated
|
541 |
|
|
before the transaction is ended.
|
542 |
|
|
|
543 |
|
|
|
544 |
|
|
|
545 |
|
|
Initialization
|
546 |
|
|
|
547 |
|
|
The generic package CYGPKG_IO_I2C arranges for all
|
548 |
|
|
I2C bus devices to be initialized via a single prioritized C++ static
|
549 |
|
|
constructor. This constructor will run early on during system startup,
|
550 |
|
|
before any application code, with priority
|
551 |
|
|
CYG_INIT_BUS_I2C. Other code should not try to
|
552 |
|
|
access any of the I2C devices until after the buses have been
|
553 |
|
|
initialized.
|
554 |
|
|
|
555 |
|
|
|
556 |
|
|
|
557 |
|
|
|
558 |
|
|
|
559 |
|
|
|
560 |
|
|
|
561 |
|
|
Porting to New Hardware
|
562 |
|
|
|
563 |
|
|
|
564 |
|
|
Porting
|
565 |
|
|
Adding I2C support to new hardware
|
566 |
|
|
|
567 |
|
|
|
568 |
|
|
Description
|
569 |
|
|
|
570 |
|
|
Adding I2C support to an eCos port involves a number of steps. The
|
571 |
|
|
generic I2C package CYGPKG_IO_I2C should be
|
572 |
|
|
included in the appropriate ecos.db target entry
|
573 |
|
|
or entries. Next cyg_i2c_device structures
|
574 |
|
|
should be provided for every device on the bus. Usually this is the
|
575 |
|
|
responsibility of the platform HAL. In the case of development boards
|
576 |
|
|
where the I2C SDA and SCL lines are accessible via an expansion
|
577 |
|
|
connector, more devices may have been added and it will be the
|
578 |
|
|
application's responsibility to provide the structures. Finally
|
579 |
|
|
there is a need for one or more cyg_i2c_bus
|
580 |
|
|
structures. Amongst other things these structures provide functions
|
581 |
|
|
for actually driving the bus. If the processor has dedicated I2C
|
582 |
|
|
hardware then this structure will usually be provided by a device
|
583 |
|
|
driver package. If the bus is implemented by bit-banging then the bus
|
584 |
|
|
structure will usually be provided by the platform HAL.
|
585 |
|
|
|
586 |
|
|
|
587 |
|
|
|
588 |
|
|
Adding a Device
|
589 |
|
|
|
590 |
|
|
The eCos I2C API works in terms of
|
591 |
|
|
cyg_i2c_device structures, and these provide
|
592 |
|
|
the information needed to access the hardware. A
|
593 |
|
|
cyg_i2c_device structure contains the
|
594 |
|
|
following fields:
|
595 |
|
|
|
596 |
|
|
|
597 |
|
|
|
598 |
|
|
cyg_i2c_bus* i2c_bus
|
599 |
|
|
|
600 |
|
|
This specifies the bus which the slave device is connected to. Most
|
601 |
|
|
boards will only have a single I2C bus, but multiple buses are possible.
|
602 |
|
|
|
603 |
|
|
|
604 |
|
|
|
605 |
|
|
cyg_uint16 i2c_address
|
606 |
|
|
|
607 |
|
|
For most devices this will be the 7-bit I2C address the device will
|
608 |
|
|
respond to. There is room for future expansion, for example to support
|
609 |
|
|
10-bit addresses.
|
610 |
|
|
|
611 |
|
|
|
612 |
|
|
|
613 |
|
|
cyg_uint16 i2c_flags
|
614 |
|
|
|
615 |
|
|
This field is not used at present. It exists for future expansion, for
|
616 |
|
|
example to allow for fast mode or high-speed mode, and incidentally
|
617 |
|
|
pads the structure to a 32-bit boundary.
|
618 |
|
|
|
619 |
|
|
|
620 |
|
|
|
621 |
|
|
cyg_uint32 i2c_delay
|
622 |
|
|
|
623 |
|
|
This holds the clock period which should be used when interacting with
|
624 |
|
|
the device, in nanoseconds. Usually this will be 10000 ns,
|
625 |
|
|
corresponding to a 100KHz clock, and the header
|
626 |
|
|
class="headerfile">cyg/io/i2c.h provides a
|
627 |
|
|
#define CYG_I2C_DEFAULT_DELAY
|
628 |
|
|
for this. Sometimes it may be desirable to use a slower clock, for
|
629 |
|
|
example to reduce noise problems.
|
630 |
|
|
|
631 |
|
|
|
632 |
|
|
|
633 |
|
|
|
634 |
|
|
The normal way to instantiate a cyg_i2c_device
|
635 |
|
|
structure uses the CYG_I2C_DEVICE macro, also
|
636 |
|
|
provided by :
|
637 |
|
|
|
638 |
|
|
|
639 |
|
|
#include <cyg/io/i2c.h>
|
640 |
|
|
|
641 |
|
|
CYG_I2C_DEVICE(cyg_i2c_wallclock_ds1307,
|
642 |
|
|
&hal_alaia_i2c_bus,
|
643 |
|
|
0x68,
|
644 |
|
|
0x00,
|
645 |
|
|
CYG_I2C_DEFAULT_DELAY);
|
646 |
|
|
|
647 |
|
|
CYG_I2C_DEVICE(hal_alaia_i2c_fs6377,
|
648 |
|
|
&hal_alaia_i2c_bus,
|
649 |
|
|
0x58,
|
650 |
|
|
0x00,
|
651 |
|
|
CYG_I2C_DEFAULT_DELAY);
|
652 |
|
|
|
653 |
|
|
|
654 |
|
|
The arguments to the macro are the variable name, an I2C bus pointer,
|
655 |
|
|
the device address, the flags field, and the delay field. The above
|
656 |
|
|
code fragment defines two I2C device variables,
|
657 |
|
|
cyg_i2c_wallclock_ds1307 and
|
658 |
|
|
hal_alaia_i2c_fs6377, which can be used for the
|
659 |
|
|
first argument to the cyg_i2c_ functions. Both
|
660 |
|
|
devices are on the same bus. The device addresses are 0x68 and 0x58
|
661 |
|
|
respectively, and the devices do not have any special requirements.
|
662 |
|
|
|
663 |
|
|
|
664 |
|
|
When the platform HAL provides these structures it should also export
|
665 |
|
|
them for use by the application and other packages. Usually this
|
666 |
|
|
involves an entry in
|
667 |
|
|
class="headerfile">cyg/hal/plf_io.h, which gets included
|
668 |
|
|
automatically via one of the main exported HAL header files
|
669 |
|
|
class="headerfile">cyg/hal/hal_io.h. Unfortunately
|
670 |
|
|
exporting the structures directly can be problematical because of
|
671 |
|
|
circular dependencies between the I2C header and the HAL headers.
|
672 |
|
|
Instead the platform HAL should define a macro
|
673 |
|
|
HAL_I2C_EXPORTED_DEVICES:
|
674 |
|
|
|
675 |
|
|
|
676 |
|
|
# define HAL_I2C_EXPORTED_DEVICES \
|
677 |
|
|
extern cyg_i2c_bus hal_alaia_i2c_bus; \
|
678 |
|
|
extern cyg_i2c_device cyg_i2c_wallclock_ds1307; \
|
679 |
|
|
extern cyg_i2c_device hal_alaia_i2c_fs6377;
|
680 |
|
|
|
681 |
|
|
|
682 |
|
|
This macro gets expanded automatically by
|
683 |
|
|
class="headerfile">cyg/io/i2c.h once the data structures
|
684 |
|
|
themselves have been defined, so application code can just include
|
685 |
|
|
that header and all the buses and devices will be properly exported
|
686 |
|
|
and usable.
|
687 |
|
|
|
688 |
|
|
|
689 |
|
|
There is no single convention for naming the I2C devices. If the
|
690 |
|
|
device will be used by some other package then typically that
|
691 |
|
|
specifies the name that should be used. For example the DS1307
|
692 |
|
|
wallclock driver expects the I2C device to be called
|
693 |
|
|
cyg_i2c_wallclock_ds1307, so failing to observe
|
694 |
|
|
that convention will lead to compile-time and link-time errors. If the
|
695 |
|
|
device will not be used by any other package then it is up to the
|
696 |
|
|
platform HAL to select the name, and as long as reasonable care is
|
697 |
|
|
taken to avoid name space pollution the exact name does not matter.
|
698 |
|
|
|
699 |
|
|
|
700 |
|
|
|
701 |
|
|
Bit-banged Bus
|
702 |
|
|
|
703 |
|
|
Some processors come with dedicated I2C hardware. On other hardware
|
704 |
|
|
the I2C bus involves simply connecting some GPIO pins to the SCL and
|
705 |
|
|
SDA lines and then using software to implement the I2C protocol. This
|
706 |
|
|
is usually referred to as bit-banging the bus. The generic I2C package
|
707 |
|
|
CYGPKG_IO_I2C provides the main code for a
|
708 |
|
|
bit-banged implementation, requiring one platform-specific function
|
709 |
|
|
that does the actual GPIO pin manipulation. This function is usually
|
710 |
|
|
hardware-specific because different boards will use different pins for
|
711 |
|
|
the I2C bus, so typically it is left to the platform HAL to provide
|
712 |
|
|
this function and instantiate the I2C bus object. There is no point in
|
713 |
|
|
creating a separate package for this because the code cannot be
|
714 |
|
|
re-used for other platforms.
|
715 |
|
|
|
716 |
|
|
|
717 |
|
|
Instantiating a bit-banged I2C bus requires the following:
|
718 |
|
|
|
719 |
|
|
|
720 |
|
|
#include <cyg/io/i2c.h>
|
721 |
|
|
|
722 |
|
|
static cyg_bool
|
723 |
|
|
hal_alaia_i2c_bitbang(cyg_i2c_bus* bus, cyg_i2c_bitbang_op op)
|
724 |
|
|
{
|
725 |
|
|
cyg_bool result = 0;
|
726 |
|
|
switch(op) {
|
727 |
|
|
…
|
728 |
|
|
}
|
729 |
|
|
return result;
|
730 |
|
|
}
|
731 |
|
|
|
732 |
|
|
CYG_I2C_BITBANG_BUS(hal_alaia_i2c_bus, &hal_alaia_i2c_bitbang);
|
733 |
|
|
|
734 |
|
|
|
735 |
|
|
This gives a structure hal_alaia_i2c_bus which can
|
736 |
|
|
be used when defining the cyg_i2c_device
|
737 |
|
|
structures. The second argument specifies the function which will
|
738 |
|
|
do the actual bit-banging. It takes two arguments. The first
|
739 |
|
|
identifies the bus, which can be useful if the hardware has multiple
|
740 |
|
|
I2C buses. The second specifies the bit-bang operation that should be
|
741 |
|
|
performed. To understand these operations consider how I2C devices
|
742 |
|
|
should be wired up according to the specification:
|
743 |
|
|
|
744 |
|
|
|
745 |
|
|
|
746 |
|
|
|
747 |
|
|
|
748 |
|
|
|
749 |
|
|
|
750 |
|
|
|
751 |
|
|
|
752 |
|
|
Master and slave devices are interfaced to the bus in exactly the same
|
753 |
|
|
way. The default state of the bus is to have both lines high via the
|
754 |
|
|
pull-up resistors. Any device on the bus can lower either line, when
|
755 |
|
|
allowed to do so by the protocol. Usually the SDA line only changes
|
756 |
|
|
while SCL is low, but the start and stop conditions involve SDA
|
757 |
|
|
changing while SCL is high. All devices have the ability to both read
|
758 |
|
|
and write both lines. In reality not all bit-banged hardware works
|
759 |
|
|
quite like this. Instead just two GPIO pins are used, and these are
|
760 |
|
|
switched between input and output mode as required.
|
761 |
|
|
|
762 |
|
|
|
763 |
|
|
The bitbang function should support the following operations:
|
764 |
|
|
|
765 |
|
|
|
766 |
|
|
|
767 |
|
|
CYG_I2C_BITBANG_INIT
|
768 |
|
|
|
769 |
|
|
This will be called during system initialization, as a side effect of
|
770 |
|
|
a prioritized C++ static constructor. The bitbang function should
|
771 |
|
|
ensure that both SCL and SDA are driven high.
|
772 |
|
|
|
773 |
|
|
|
774 |
|
|
|
775 |
|
|
CYG_I2C_BITBANG_SCL_HIGH
|
776 |
|
|
CYG_I2C_BITBANG_SCL_LOW
|
777 |
|
|
CYG_I2C_BITBANG_SDA_HIGH
|
778 |
|
|
CYG_I2C_BITBANG_SDA_LOW
|
779 |
|
|
|
780 |
|
|
These operations simply set the appropriate lines high or low.
|
781 |
|
|
|
782 |
|
|
|
783 |
|
|
|
784 |
|
|
CYG_I2C_BITBANG_SCL_HIGH_CLOCKSTRETCH
|
785 |
|
|
|
786 |
|
|
In its simplest form this operation should simply set the SCL line
|
787 |
|
|
high, indicating that the data on the SDA line is stable. However
|
788 |
|
|
there is a complication: if a device is not ready yet then it can
|
789 |
|
|
throttle back the master by keeping the SCL line low. This is known as
|
790 |
|
|
clock-stretching. Hence for this operation the bitbang function should
|
791 |
|
|
allow the SCL line to float high, then poll it until it really has
|
792 |
|
|
become high. If a single pin is used for the SCL line then this pin
|
793 |
|
|
should be turned back into a high output at the end of the call.
|
794 |
|
|
|
795 |
|
|
|
796 |
|
|
|
797 |
|
|
CYG_I2C_BITBANG_SCL_LOW_SDA_INPUT
|
798 |
|
|
|
799 |
|
|
This is used when there is a change of direction and the slave device
|
800 |
|
|
is about to start driving the SDA line. This can be significant if a
|
801 |
|
|
single pin is used to handle both input and output of SDA, to avoid
|
802 |
|
|
a situation where both the master and the slave are driving the SDA
|
803 |
|
|
line for an extended period of time. The operation combines dropping
|
804 |
|
|
the SCL line and switching SDA to an input in an atomic or near-atomic
|
805 |
|
|
operation.
|
806 |
|
|
|
807 |
|
|
|
808 |
|
|
|
809 |
|
|
CYG_I2C_BITBANG_SDA_READ
|
810 |
|
|
|
811 |
|
|
The SDA line is currently set as an input and the bitbang function
|
812 |
|
|
should sample and return the current state.
|
813 |
|
|
|
814 |
|
|
|
815 |
|
|
|
816 |
|
|
|
817 |
|
|
The bitbang function returns a boolean. For most operations this
|
818 |
|
|
return value is ignored. For
|
819 |
|
|
CYG_I2C_BITBANG_SDA_READ it should be the current
|
820 |
|
|
level of the SDA line.
|
821 |
|
|
|
822 |
|
|
|
823 |
|
|
Depending on the hardware some care may have to be taken when
|
824 |
|
|
manipulating the GPIO pins. Although the I2C subsystem performs the
|
825 |
|
|
required locking at the bus level, the device registers controlling
|
826 |
|
|
the GPIO pins may get used by other subsystems or by the application.
|
827 |
|
|
It is the responsibility of the bitbang function to perform
|
828 |
|
|
appropriate locking, whether via a mutex or by briefly disabling
|
829 |
|
|
interrupts around the register accesses.
|
830 |
|
|
|
831 |
|
|
|
832 |
|
|
|
833 |
|
|
Full Bus Driver
|
834 |
|
|
|
835 |
|
|
If the processor has dedicated I2C hardware then usually this will
|
836 |
|
|
involve a separate device driver package in the
|
837 |
|
|
devs/i2c hierarchy of the eCos component
|
838 |
|
|
repository. That package should also be included in the appropriate
|
839 |
|
|
ecos.db target entry or entries. The device
|
840 |
|
|
driver may exist already, or it may have to be written from scratch.
|
841 |
|
|
|
842 |
|
|
|
843 |
|
|
A new I2C driver basically involves creating an
|
844 |
|
|
cyg_i2c_bus structure. The device driver
|
845 |
|
|
should supply the following fields:
|
846 |
|
|
|
847 |
|
|
|
848 |
|
|
|
849 |
|
|
i2c_init_fn
|
850 |
|
|
|
851 |
|
|
This function will be called during system initialization to set up
|
852 |
|
|
the I2C hardware. The generic I2C code creates a static object with a
|
853 |
|
|
prioritized constructor, and this constructor will invoke the init
|
854 |
|
|
functions for the various I2C buses in the system.
|
855 |
|
|
|
856 |
|
|
|
857 |
|
|
|
858 |
|
|
i2c_tx_fn
|
859 |
|
|
i2c_rx_fn
|
860 |
|
|
i2c_stop_fn
|
861 |
|
|
|
862 |
|
|
These functions implement the core I2C functionality. The arguments
|
863 |
|
|
and results are the same as for the transaction functions
|
864 |
|
|
cyg_i2c_transaction_tx,
|
865 |
|
|
cyg_i2c_transaction_rx and
|
866 |
|
|
cyg_i2c_transaction_stop.
|
867 |
|
|
|
868 |
|
|
|
869 |
|
|
|
870 |
|
|
void* i2c_extra
|
871 |
|
|
|
872 |
|
|
This field holds any extra information that may be needed by the
|
873 |
|
|
device driver. Typically it will be a pointer to some driver-specific
|
874 |
|
|
data structure.
|
875 |
|
|
|
876 |
|
|
|
877 |
|
|
|
878 |
|
|
|
879 |
|
|
To assist with instantiating a cyg_i2c_bus
|
880 |
|
|
object the header file
|
881 |
|
|
class="headerfile">cyg/io/i2c.h provides a macro. Typical
|
882 |
|
|
usage would be:
|
883 |
|
|
|
884 |
|
|
|
885 |
|
|
struct xyzzy_data {
|
886 |
|
|
…
|
887 |
|
|
} xyzzy_object;
|
888 |
|
|
|
889 |
|
|
static void
|
890 |
|
|
xyzzy_i2c_init(struct cyg_i2c_bus* bus)
|
891 |
|
|
{
|
892 |
|
|
…
|
893 |
|
|
}
|
894 |
|
|
|
895 |
|
|
static cyg_uint32
|
896 |
|
|
xyzzy_i2c_tx(const cyg_i2c_device* dev,
|
897 |
|
|
cyg_bool send_start,
|
898 |
|
|
const cyg_uint8* tx_data, cyg_uint32 count,
|
899 |
|
|
cyg_bool send_stop)
|
900 |
|
|
{
|
901 |
|
|
…
|
902 |
|
|
}
|
903 |
|
|
|
904 |
|
|
static cyg_uint32
|
905 |
|
|
xyzzy_i2c_rx(const cyg_i2c_device* dev,
|
906 |
|
|
cyg_bool send_start,
|
907 |
|
|
cyg_uint8* rx_data, cyg_uint32 count,
|
908 |
|
|
cyg_bool send_nack, cyg_bool send_stop)
|
909 |
|
|
{
|
910 |
|
|
…
|
911 |
|
|
}
|
912 |
|
|
|
913 |
|
|
static void
|
914 |
|
|
xyzzy_i2c_stop(const cyg_i2c_device* dev)
|
915 |
|
|
{
|
916 |
|
|
…
|
917 |
|
|
}
|
918 |
|
|
|
919 |
|
|
CYG_I2C_BUS(cyg_i2c_xyzzy_bus,
|
920 |
|
|
&xyzzy_i2c_init,
|
921 |
|
|
&xyzzy_i2c_tx,
|
922 |
|
|
&xyzzy_i2c_rx,
|
923 |
|
|
&xyzzy_i2c_stop,
|
924 |
|
|
(void*) &xyzzy_object);
|
925 |
|
|
|
926 |
|
|
|
927 |
|
|
The generic I2C code contains these functions for a bit-banged I2C bus
|
928 |
|
|
device. It can be used as a starting point for new drivers. Note that
|
929 |
|
|
the bit-bang code uses the i2c_extra field to hold
|
930 |
|
|
the hardware-specific bitbang function rather than a pointer to some
|
931 |
|
|
data structure.
|
932 |
|
|
|
933 |
|
|
|
934 |
|
|
|
935 |
|
|
|
936 |
|
|
|
937 |
|
|
|