1 |
786 |
skrzyp |
#ifndef CYGONCE_IO_I2C_H
|
2 |
|
|
#define CYGONCE_IO_I2C_H
|
3 |
|
|
|
4 |
|
|
//=============================================================================
|
5 |
|
|
//
|
6 |
|
|
// i2c.h
|
7 |
|
|
//
|
8 |
|
|
// Generic API for accessing devices on an I2C bus
|
9 |
|
|
//
|
10 |
|
|
//=============================================================================
|
11 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
12 |
|
|
// -------------------------------------------
|
13 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
14 |
|
|
// Copyright (C) 2004 Free Software Foundation, Inc.
|
15 |
|
|
//
|
16 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
17 |
|
|
// the terms of the GNU General Public License as published by the Free
|
18 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
19 |
|
|
// version.
|
20 |
|
|
//
|
21 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
24 |
|
|
// for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License
|
27 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
28 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
29 |
|
|
//
|
30 |
|
|
// As a special exception, if other files instantiate templates or use
|
31 |
|
|
// macros or inline functions from this file, or you compile this file
|
32 |
|
|
// and link it with other works to produce a work based on this file,
|
33 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
34 |
|
|
// the GNU General Public License. However the source code for this file
|
35 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
36 |
|
|
// General Public License v2.
|
37 |
|
|
//
|
38 |
|
|
// This exception does not invalidate any other reasons why a work based
|
39 |
|
|
// on this file might be covered by the GNU General Public License.
|
40 |
|
|
// -------------------------------------------
|
41 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
42 |
|
|
//=============================================================================
|
43 |
|
|
//####DESCRIPTIONBEGIN####
|
44 |
|
|
//
|
45 |
|
|
// Author(s): bartv
|
46 |
|
|
// Date: 2004-10-05
|
47 |
|
|
//####DESCRIPTIONEND####
|
48 |
|
|
//=============================================================================
|
49 |
|
|
|
50 |
|
|
#include <pkgconf/infra.h>
|
51 |
|
|
#include <cyg/infra/cyg_type.h>
|
52 |
|
|
#include <cyg/hal/drv_api.h>
|
53 |
|
|
#include <cyg/hal/hal_tables.h>
|
54 |
|
|
#include <cyg/hal/hal_io.h>
|
55 |
|
|
|
56 |
|
|
// The information needed for interacting with a specific I2C device.
|
57 |
|
|
// The bus provides the tx and rx functions. The address is (usually)
|
58 |
|
|
// a 7-bit number sent at the start of each operation. The flags is
|
59 |
|
|
// not currently used, but provides room for future expansion such as
|
60 |
|
|
// 10-bit addresses. The delay should be a clock period in
|
61 |
|
|
// nanoseconds. For the default 100KHz clock that gives a value of
|
62 |
|
|
// 10000
|
63 |
|
|
|
64 |
|
|
typedef struct cyg_i2c_device {
|
65 |
|
|
struct cyg_i2c_bus* i2c_bus;
|
66 |
|
|
cyg_uint16 i2c_address;
|
67 |
|
|
cyg_uint16 i2c_flags;
|
68 |
|
|
cyg_uint32 i2c_delay;
|
69 |
|
|
} cyg_i2c_device;
|
70 |
|
|
|
71 |
|
|
#define CYG_I2C_DEFAULT_DELAY 10000
|
72 |
|
|
|
73 |
|
|
// A utility macro for defining an I2C device
|
74 |
|
|
#define CYG_I2C_DEVICE(_name_, _bus_, _address_, _flags_, _delay_) \
|
75 |
|
|
cyg_i2c_device _name_ = { \
|
76 |
|
|
.i2c_bus = _bus_, \
|
77 |
|
|
.i2c_address = _address_, \
|
78 |
|
|
.i2c_flags = _flags_, \
|
79 |
|
|
.i2c_delay = _delay_ \
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
// The information needed for interacting over a particular I2C bus.
|
83 |
|
|
// Most hardware will only have one bus, but multiple buses are
|
84 |
|
|
// supported. Thread synchronization happens on a per-bus level.
|
85 |
|
|
typedef struct cyg_i2c_bus {
|
86 |
|
|
cyg_drv_mutex_t i2c_lock;
|
87 |
|
|
#ifdef CYGDBG_USE_ASSERTS
|
88 |
|
|
const cyg_i2c_device* i2c_current_device;
|
89 |
|
|
#endif
|
90 |
|
|
// The hardware-specific functions that do the real work
|
91 |
|
|
void (*i2c_init_fn)(struct cyg_i2c_bus*);
|
92 |
|
|
cyg_uint32 (*i2c_tx_fn)(const cyg_i2c_device*, cyg_bool, const cyg_uint8*, cyg_uint32, cyg_bool);
|
93 |
|
|
cyg_uint32 (*i2c_rx_fn)(const cyg_i2c_device*, cyg_bool, cyg_uint8*, cyg_uint32, cyg_bool, cyg_bool);
|
94 |
|
|
void (*i2c_stop_fn)(const cyg_i2c_device*);
|
95 |
|
|
// A spare field for use by the driver
|
96 |
|
|
void* i2c_extra;
|
97 |
|
|
} CYG_HAL_TABLE_TYPE cyg_i2c_bus;
|
98 |
|
|
|
99 |
|
|
#define CYG_I2C_BUS(_name_, _init_fn_, _tx_fn_, _rx_fn_, _stop_fn_, _extra_) \
|
100 |
|
|
cyg_i2c_bus _name_ CYG_HAL_TABLE_ENTRY( i2c_buses ) = { \
|
101 |
|
|
.i2c_init_fn = _init_fn_, \
|
102 |
|
|
.i2c_tx_fn = _tx_fn_, \
|
103 |
|
|
.i2c_rx_fn = _rx_fn_, \
|
104 |
|
|
.i2c_stop_fn = _stop_fn_, \
|
105 |
|
|
.i2c_extra = _extra_ \
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
// To support static initialization all buses are held in a table.
|
109 |
|
|
// Of course usually this table will only contain a single entry
|
110 |
|
|
extern cyg_i2c_bus cyg_i2c_buses[], cyg_i2c_buses_end;
|
111 |
|
|
|
112 |
|
|
// The main exported interface. There are high-level operations for
|
113 |
|
|
// simple transmits and receives, and transaction-oriented routines
|
114 |
|
|
// for more complicated operations including those involving repeated
|
115 |
|
|
// starts.
|
116 |
|
|
externC cyg_uint32 cyg_i2c_tx(const cyg_i2c_device*, const cyg_uint8*, cyg_uint32);
|
117 |
|
|
externC cyg_uint32 cyg_i2c_rx(const cyg_i2c_device*, cyg_uint8*, cyg_uint32);
|
118 |
|
|
externC void cyg_i2c_transaction_begin(const cyg_i2c_device*);
|
119 |
|
|
externC cyg_bool cyg_i2c_transaction_begin_nb(const cyg_i2c_device*);
|
120 |
|
|
externC cyg_uint32 cyg_i2c_transaction_tx(const cyg_i2c_device*, cyg_bool, const cyg_uint8*, cyg_uint32, cyg_bool);
|
121 |
|
|
externC cyg_uint32 cyg_i2c_transaction_rx(const cyg_i2c_device*, cyg_bool, cyg_uint8*, cyg_uint32, cyg_bool, cyg_bool);
|
122 |
|
|
externC void cyg_i2c_transaction_stop(const cyg_i2c_device*);
|
123 |
|
|
externC void cyg_i2c_transaction_end(const cyg_i2c_device*);
|
124 |
|
|
|
125 |
|
|
// Bit-bang support. This merely requires the platform HAL to provide
|
126 |
|
|
// a function for the actual bit-bang operation. The rest is handled
|
127 |
|
|
// by the generic code.
|
128 |
|
|
typedef enum cyg_i2c_bitbang_op {
|
129 |
|
|
CYG_I2C_BITBANG_INIT = 0,
|
130 |
|
|
CYG_I2C_BITBANG_SCL_HIGH = 1,
|
131 |
|
|
CYG_I2C_BITBANG_SCL_LOW = 2,
|
132 |
|
|
CYG_I2C_BITBANG_SCL_HIGH_CLOCKSTRETCH = 3,
|
133 |
|
|
CYG_I2C_BITBANG_SCL_LOW_SDA_INPUT = 4,
|
134 |
|
|
CYG_I2C_BITBANG_SDA_OUTPUT = 5,
|
135 |
|
|
CYG_I2C_BITBANG_SDA_HIGH = 6,
|
136 |
|
|
CYG_I2C_BITBANG_SDA_LOW = 7,
|
137 |
|
|
CYG_I2C_BITBANG_SDA_READ = 8
|
138 |
|
|
} cyg_i2c_bitbang_op;
|
139 |
|
|
|
140 |
|
|
typedef cyg_bool (*cyg_i2c_bitbang_fn)(cyg_i2c_bus*, cyg_i2c_bitbang_op);
|
141 |
|
|
|
142 |
|
|
// A bitbang bus can be instantiated by just providing the bitbang function.
|
143 |
|
|
// That is held in the extra field.
|
144 |
|
|
#define CYG_I2C_BITBANG_BUS(_name_, _bitbang_fn_) \
|
145 |
|
|
CYG_I2C_BUS(_name_, \
|
146 |
|
|
&cyg_i2c_bitbang_init, \
|
147 |
|
|
&cyg_i2c_bitbang_tx, \
|
148 |
|
|
&cyg_i2c_bitbang_rx, \
|
149 |
|
|
&cyg_i2c_bitbang_stop, \
|
150 |
|
|
(void*) _bitbang_fn_)
|
151 |
|
|
|
152 |
|
|
// The generic bit-bang functions. These are not called directly,
|
153 |
|
|
// but must be exported for use by the above macro.
|
154 |
|
|
externC void cyg_i2c_bitbang_init(cyg_i2c_bus*);
|
155 |
|
|
externC cyg_uint32 cyg_i2c_bitbang_tx(const cyg_i2c_device*, cyg_bool, const cyg_uint8*, cyg_uint32, cyg_bool);
|
156 |
|
|
externC cyg_uint32 cyg_i2c_bitbang_rx(const cyg_i2c_device*, cyg_bool, cyg_uint8*, cyg_uint32, cyg_bool, cyg_bool);
|
157 |
|
|
externC void cyg_i2c_bitbang_stop(const cyg_i2c_device*);
|
158 |
|
|
|
159 |
|
|
// Allow the HAL to export the buses and named devices. There are
|
160 |
|
|
// circular dependencies between this header and the HAL ones so
|
161 |
|
|
// it is not easy for the HAL headers to define cyg_i2c_device
|
162 |
|
|
// externs directly. Instead the HAL can define this macro which
|
163 |
|
|
// provides the externs.
|
164 |
|
|
#ifdef HAL_I2C_EXPORTED_DEVICES
|
165 |
|
|
HAL_I2C_EXPORTED_DEVICES
|
166 |
|
|
#endif
|
167 |
|
|
|
168 |
|
|
//-----------------------------------------------------------------------------
|
169 |
|
|
#endif // ifndef CYGONCE_IO_I2C_H
|
170 |
|
|
// End of i2c.h
|