source: rtems/cpukit/libi2c/libi2c.h @ 6a03edd5

4.104.114.84.9
Last change on this file since 6a03edd5 was 6a03edd5, checked in by Till Straumann <strauman@…>, on Jan 17, 2007 at 6:19:38 AM

2007-01-16 Till Straumann <strauman@…>

  • libi2c/libi2c.c, libi2c/libi2c.h: Added SLAC/Stanford Authorship Note / Copyright + Liability Disclaimer.
  • Property mode set to 100644
File size: 9.7 KB
Line 
1#ifndef RTEMS_LIBI2C_H
2#define RTEMS_LIBI2C_H
3/*$Id$*/
4
5/*
6 * Authorship
7 * ----------
8 * This software was created by
9 *     Till Straumann <strauman@slac.stanford.edu>, 2005,
10 *         Stanford Linear Accelerator Center, Stanford University.
11 *
12 * Acknowledgement of sponsorship
13 * ------------------------------
14 * This software was produced by
15 *     the Stanford Linear Accelerator Center, Stanford University,
16 *         under Contract DE-AC03-76SFO0515 with the Department of Energy.
17 *
18 * Government disclaimer of liability
19 * ----------------------------------
20 * Neither the United States nor the United States Department of Energy,
21 * nor any of their employees, makes any warranty, express or implied, or
22 * assumes any legal liability or responsibility for the accuracy,
23 * completeness, or usefulness of any data, apparatus, product, or process
24 * disclosed, or represents that its use would not infringe privately owned
25 * rights.
26 *
27 * Stanford disclaimer of liability
28 * --------------------------------
29 * Stanford University makes no representations or warranties, express or
30 * implied, nor assumes any liability for the use of this software.
31 *
32 * Stanford disclaimer of copyright
33 * --------------------------------
34 * Stanford University, owner of the copyright, hereby disclaims its
35 * copyright and all other rights in this software.  Hence, anyone may
36 * freely use it for any purpose without restriction. 
37 *
38 * Maintenance of notices
39 * ----------------------
40 * In the interest of clarity regarding the origin and status of this
41 * SLAC software, this and all the preceding Stanford University notices
42 * are to remain affixed to any copy or derivative of this software made
43 * or distributed by the recipient and are to be affixed to any copy of
44 * software made or distributed by the recipient that contains a copy or
45 * derivative of this software.
46 *
47 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
48 */ 
49#include <rtems.h>
50
51#include <rtems/io.h>
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/* Simple I2C driver API */
58
59/* Initialize the libary - may fail if no semaphore or no driver slot is available */
60int rtems_libi2c_initialize ();
61
62/* Bus Driver API
63 *
64 * Bus drivers provide access to low-level i2c functions
65 * such as 'send start', 'send address', 'get bytes' etc.
66 */
67
68/* first field must be a pointer to ops; driver
69 * may add its own fields after this.
70 * the struct that is registered with the library
71 * is not copied; a pointer will we passed
72 * to the callback functions (ops).
73 */
74typedef struct rtems_libi2c_bus_t_
75{
76  struct rtems_libi2c_bus_ops_ *ops;
77  int size;                     /* size of whole structure */
78} rtems_libi2c_bus_t;
79
80/* Access functions a low level driver must provide;
81 *
82 * All of these, except read_bytes and write_bytes
83 * return RTEMS_SUCCESSFUL on success and an error status
84 * otherwise. The read and write ops return the number
85 * of chars read/written or -(status code) on error.
86 */
87typedef struct rtems_libi2c_bus_ops_
88{
89  /* Initialize the bus; might be called again to reset the bus driver */
90  rtems_status_code (*init) (rtems_libi2c_bus_t * bushdl);
91  /* Send start condition */
92  rtems_status_code (*send_start) (rtems_libi2c_bus_t * bushdl);
93  /* Send stop  condition */
94  rtems_status_code (*send_stop) (rtems_libi2c_bus_t * bushdl);
95  /* initiate transfer from (rw!=0) or to a device */
96  rtems_status_code (*send_addr) (rtems_libi2c_bus_t * bushdl,
97                                  uint32_t addr, int rw);
98  /* read a number of bytes */
99  int (*read_bytes) (rtems_libi2c_bus_t * bushdl, unsigned char *bytes,
100                     int nbytes);
101  /* write a number of bytes */
102  int (*write_bytes) (rtems_libi2c_bus_t * bushdl, unsigned char *bytes,
103                      int nbytes);
104} rtems_libi2c_bus_ops_t;
105
106
107/*
108 * Register a lowlevel driver
109 *
110 * TODO: better description
111 *
112 * This  allocates a major number identifying *this* driver
113 * (i.e., libi2c) and the minor number encodes a bus# and a i2c address.
114 *
115 * The name will be registered in the filesystem (parent
116 * directories must exist). It may be NULL in which case
117 * the library will pick a default.
118 *
119 * RETURNS: bus # (>=0) or -1 on error (errno set).
120 */
121
122int rtems_libi2c_register_bus (char *name, rtems_libi2c_bus_t * bus);
123
124extern rtems_device_major_number rtems_libi2c_major;
125
126#define RTEMS_LIBI2C_MAKE_MINOR(busno, i2caddr) \
127        ((((busno)&((1<<3)-1))<<10) | ((i2caddr)&((1<<10)-1)))
128
129/* After the library is initialized, a major number is available.
130 * As soon as a low-level bus driver is registered (above routine
131 * returns a 'busno'), a device node can be created in the filesystem
132 * with a major/minor number pair of
133 *
134 *    rtems_libi2c_major / RTEMS_LIBI2C_MAKE_MINOR(busno, i2caddr)
135 *
136 * and a 'raw' hi-level driver is then attached to this device
137 * node.
138 * This 'raw' driver has very simple semantics:
139 *
140 *   'open'         sends a start condition
141 *   'read'/'write' address the device identified by the i2c bus# and address
142 *                  encoded in the minor number and read or write, respectively
143 *                  a stream of bytes from or to the device. Every time the
144 *                  direction is changed, a 're-start' condition followed by
145 *                  an 'address' cycle is generated on the i2c bus.
146 *   'close'        sends a stop condition.
147 *
148 * Hence, using the 'raw' driver, e.g., 100 bytes at offset 0x200 can be
149 * read from an EEPROM by the following pseudo-code:
150 *
151 *   mknod("/dev/i2c-54", mode, MKDEV(rtems_libi2c_major, RTEMS_LIBI2C_MAKE_MINOR(0,0x54)))
152 *
153 *   int fd;
154 *   char off[2]={0x02,0x00};
155 *
156 *   fd = open("/dev/i2c-54",O_RDWR);
157 *   write(fd,off,2);
158 *   read(fd,buf,100);
159 *   close(fd);
160 *
161 */
162
163/* Higher Level Driver API
164 *
165 * Higher level drivers know how to deal with specific i2c
166 * devices (independent of the bus interface chip) and provide
167 * an abstraction, i.e., the usual read/write/ioctl access.
168 *
169 * Using the above example, such a high level driver could
170 * prevent the user from issuing potentially destructive write
171 * operations (the aforementioned EEPROM interprets any 3rd
172 * and following byte written to the device as data, i.e., the
173 * contents could easily be changed!).
174 * The correct 'read-pointer offset' programming could be
175 * implemented in 'open' and 'ioctl' of a high-level driver and
176 * the user would then only have to perform harmless read
177 * operations, e.g.,
178 *
179 *    fd = open("/dev/i2c.eeprom",O_RDONLY) / * opens and sets EEPROM read pointer * /
180 *    ioctl(fd, IOCTL_SEEK, 0x200)                      / * repositions the read pointer       * /
181 *    read(fd, buf, 100)
182 *    close(fd)
183 *
184 */
185
186/* struct provided at driver registration. The driver may store
187 * private data behind the mandatory first fields but the size
188 * must be set to the size of the entire struct, e.g.,
189 *
190 * struct driver_pvt {
191 *      rtems_libi2c_drv_t pub;
192 *      struct {  ...    } pvt;
193 * } my_driver = {
194 *              {  ops: my_ops,
195 *                size: sizeof(my_driver)
196 *              },
197 *              { ...};
198 * };
199 *
200 * A pointer to this struct is passed to the callback ops.
201 */
202
203typedef struct rtems_libi2c_drv_t_
204{
205  rtems_driver_address_table *ops;      /* the driver ops */
206  int size;                     /* size of whole structure (including appended private data) */
207} rtems_libi2c_drv_t;
208
209/*
210 * The high level driver must be registered with a particular
211 * bus number and i2c address.
212 *
213 * The registration procedure also creates a filesystem node,
214 * i.e., the returned minor number is not really needed.
215 *
216 * If the 'name' argument is NULL, no filesystem node is
217 * created (but this can be done 'manually' using rtems_libi2c_major
218 * and the return value of this routine).
219 *
220 * RETURNS minor number (FYI) or -1 on failure
221 */
222int
223rtems_libi2c_register_drv (char *name, rtems_libi2c_drv_t * drvtbl,
224                           unsigned bus, unsigned i2caddr);
225
226/* Operations available to high level drivers */
227
228/* NOTES: The bus a device is attached to is LOCKED from the first send_start
229 *        until send_stop is executed!
230 *
231 *        Bus tenure MUST NOT span multiple system calls - otherwise, a single
232 *        thread could get into the protected sections (or would deadlock if the
233 *                mutex was not nestable).
234 *                E.g., consider what happens if 'open' sends a 'start' and 'close'
235 *                sends a 'stop' (i.e., the bus mutex would be locked in 'open' and
236 *        released in 'close'. A single thread could try to open two devices
237 *        on the same bus and would either deadlock or nest into the bus mutex
238 *        and potentially mess up the i2c messages.
239 *
240 *        The correct way is to *always* relinquish the i2c bus (i.e., send 'stop'
241 *                from any driver routine prior to returning control to the caller.
242 *                Consult the implementation of the generic driver routines (open, close, ...)
243 *                below or the examples in i2c-2b-eeprom.c and i2c-2b-ds1621.c
244 *
245 * Drivers just pass the minor number on to these routines...
246 */
247rtems_status_code rtems_libi2c_send_start (rtems_device_minor_number minor);
248
249rtems_status_code rtems_libi2c_send_stop (rtems_device_minor_number minor);
250
251rtems_status_code
252rtems_libi2c_send_addr (rtems_device_minor_number minor, int rw);
253
254/* the read/write routines return the number of bytes transferred
255 * or -(status_code) on error.
256 */
257int
258rtems_libi2c_read_bytes (rtems_device_minor_number minor,
259                         unsigned char *bytes, int nbytes);
260
261int
262rtems_libi2c_write_bytes (rtems_device_minor_number minor,
263                          unsigned char *bytes, int nbytes);
264
265/* Send start, send address and read bytes */
266int
267rtems_libi2c_start_read_bytes (uint32_t minor, unsigned char *bytes,
268                               int nbytes);
269
270/* Send start, send address and write bytes */
271int
272rtems_libi2c_start_write_bytes (uint32_t minor, unsigned char *bytes,
273                                int nbytes);
274
275#ifdef __cplusplus
276}
277#endif
278
279#endif
Note: See TracBrowser for help on using the repository browser.