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

4.115
Last change on this file since 6279149 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

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