source: rtems/c/src/libchip/i2c/i2c-2b-eeprom.c @ 0945123

Last change on this file since 0945123 was 0945123, checked in by Till Straumann <strauman@…>, on 01/17/07 at 06:02:41

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

  • libchip/i2c/i2c-2b-eeprom.c, libchip/i2c/i2c-2b-eeprom.h,
  • libchip/i2c/i2c-ds1621.c, libchip/i2c/i2c-ds1621.h: Added SLAC/Stanford Authorship Note / Copyright + Liability Disclaimer.
  • Property mode set to 100644
File size: 4.9 KB
Line 
1/* $Id$ */
2
3/* Trivial i2c driver for reading "2-byte eeproms".
4 * On 'open' the read-pointer is reset to 0, subsequent
5 * read operations slurp data from there...
6 */
7
8/*
9 * Authorship
10 * ----------
11 * This software was created by
12 *     Till Straumann <strauman@slac.stanford.edu>, 2005,
13 *         Stanford Linear Accelerator Center, Stanford University.
14 *
15 * Acknowledgement of sponsorship
16 * ------------------------------
17 * This software was produced by
18 *     the Stanford Linear Accelerator Center, Stanford University,
19 *         under Contract DE-AC03-76SFO0515 with the Department of Energy.
20 *
21 * Government disclaimer of liability
22 * ----------------------------------
23 * Neither the United States nor the United States Department of Energy,
24 * nor any of their employees, makes any warranty, express or implied, or
25 * assumes any legal liability or responsibility for the accuracy,
26 * completeness, or usefulness of any data, apparatus, product, or process
27 * disclosed, or represents that its use would not infringe privately owned
28 * rights.
29 *
30 * Stanford disclaimer of liability
31 * --------------------------------
32 * Stanford University makes no representations or warranties, express or
33 * implied, nor assumes any liability for the use of this software.
34 *
35 * Stanford disclaimer of copyright
36 * --------------------------------
37 * Stanford University, owner of the copyright, hereby disclaims its
38 * copyright and all other rights in this software.  Hence, anyone may
39 * freely use it for any purpose without restriction. 
40 *
41 * Maintenance of notices
42 * ----------------------
43 * In the interest of clarity regarding the origin and status of this
44 * SLAC software, this and all the preceding Stanford University notices
45 * are to remain affixed to any copy or derivative of this software made
46 * or distributed by the recipient and are to be affixed to any copy of
47 * software made or distributed by the recipient that contains a copy or
48 * derivative of this software.
49 *
50 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
51 */
52
53
54#include <rtems.h>
55#include <rtems/libi2c.h>
56
57#include <libchip/i2c-2b-eeprom.h>
58#include <rtems/libio.h>
59
60#define EEPROM_PG_SZ    32
61#define ALGN(x) (((uint32_t)(x) + EEPROM_PG_SZ) & ~(EEPROM_PG_SZ-1))
62
63static rtems_status_code
64send_file_ptr (rtems_device_minor_number minor, unsigned pos, int tout)
65{
66  int sc;
67  unsigned char bytes[2];
68
69  bytes[0] = (pos >> 8) & 0xff;
70  bytes[1] = (pos) & 0xff;
71
72  /* poll addressing the next page; if 'tout' is <=0 we only try once
73   * and return the status. If 'tout' is positive, we try 'tout' times
74   * and return RTEMS_TIMEOUT if it didnt work
75   */
76  while ((sc = rtems_libi2c_start_write_bytes (minor, bytes, 2)) < 0) {
77    if (--tout <= 0)
78      return tout ? -sc : RTEMS_TIMEOUT;
79    rtems_task_wake_after (1);
80  }
81  return RTEMS_SUCCESSFUL;
82}
83
84static rtems_status_code
85i2c_2b_eeprom_write (rtems_device_major_number major,
86                     rtems_device_minor_number minor, void *arg)
87{
88  rtems_libio_rw_args_t *rwargs = arg;
89  unsigned off = rwargs->offset;
90  int cnt = rwargs->count;
91  unsigned char *buf = (unsigned char *)rwargs->buffer;
92  int sc;
93  unsigned end;
94  int l;
95
96  if (cnt <= 0)
97    return RTEMS_SUCCESSFUL;
98
99  if ((sc = send_file_ptr (minor, off, 0)))
100    return sc;
101
102  do {
103    /* write up to next page boundary */
104    end = ALGN (off);
105    l = end - off;
106    if (l > cnt)
107      l = cnt;
108
109    sc = rtems_libi2c_write_bytes (minor, buf, l);
110    if (sc < 0)
111      return -sc;
112
113    sc = rtems_libi2c_send_stop (minor);
114    if (sc)
115      return sc;
116
117    rwargs->bytes_moved += l;
118
119    buf += l;
120    cnt -= l;
121    off += l;
122
123    /* poll addressing the next page */
124    if ((sc = send_file_ptr (minor, off, 100)))
125      return sc;
126
127  } while (cnt > 0);
128
129  return rtems_libi2c_send_stop (minor);
130}
131
132static rtems_status_code
133i2c_2b_eeprom_read (rtems_device_major_number major,
134                    rtems_device_minor_number minor, void *arg)
135{
136  int sc;
137  rtems_libio_rw_args_t *rwargs = arg;
138
139  if (RTEMS_SUCCESSFUL != (sc = send_file_ptr (minor, rwargs->offset, 0)))
140    return -sc;
141
142  sc = rtems_libi2c_start_read_bytes(
143    minor,
144    (unsigned char *)rwargs->buffer,
145    rwargs->count
146  );
147
148  if (sc < 0) {
149    rwargs->bytes_moved = 0;
150    return -sc;
151  }
152  rwargs->bytes_moved = sc;
153
154  return rtems_libi2c_send_stop (minor);
155}
156
157static rtems_driver_address_table myops = {
158  read_entry:  i2c_2b_eeprom_read,
159  write_entry: i2c_2b_eeprom_write,
160};
161
162static rtems_libi2c_drv_t my_drv_tbl = {
163  ops:         &myops,
164  size:        sizeof (my_drv_tbl),
165};
166
167/* provide a second table for R/O access */
168static rtems_driver_address_table my_ro_ops = {
169  read_entry:  i2c_2b_eeprom_read,
170};
171
172static rtems_libi2c_drv_t my_ro_drv_tbl = {
173  ops:         &my_ro_ops,
174  size:        sizeof (my_ro_drv_tbl),
175};
176
177
178rtems_libi2c_drv_t *i2c_2b_eeprom_driver_descriptor = &my_drv_tbl;
179rtems_libi2c_drv_t *i2c_2b_eeprom_ro_driver_descriptor = &my_ro_drv_tbl;
Note: See TracBrowser for help on using the repository browser.