1 | /* $Id$ */ |
---|
2 | |
---|
3 | /* Trivial i2c driver for the maxim DS1621 temperature sensor; |
---|
4 | * just implements reading constant conversions with 8-bit |
---|
5 | * resolution. |
---|
6 | * Demonstrates the implementation of a i2c high-level driver. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Authorship |
---|
11 | * ---------- |
---|
12 | * This software was created by |
---|
13 | * Till Straumann <strauman@slac.stanford.edu>, 2005, |
---|
14 | * Stanford Linear Accelerator Center, Stanford University. |
---|
15 | * |
---|
16 | * Acknowledgement of sponsorship |
---|
17 | * ------------------------------ |
---|
18 | * This software was produced by |
---|
19 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
20 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
21 | * |
---|
22 | * Government disclaimer of liability |
---|
23 | * ---------------------------------- |
---|
24 | * Neither the United States nor the United States Department of Energy, |
---|
25 | * nor any of their employees, makes any warranty, express or implied, or |
---|
26 | * assumes any legal liability or responsibility for the accuracy, |
---|
27 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
28 | * disclosed, or represents that its use would not infringe privately owned |
---|
29 | * rights. |
---|
30 | * |
---|
31 | * Stanford disclaimer of liability |
---|
32 | * -------------------------------- |
---|
33 | * Stanford University makes no representations or warranties, express or |
---|
34 | * implied, nor assumes any liability for the use of this software. |
---|
35 | * |
---|
36 | * Stanford disclaimer of copyright |
---|
37 | * -------------------------------- |
---|
38 | * Stanford University, owner of the copyright, hereby disclaims its |
---|
39 | * copyright and all other rights in this software. Hence, anyone may |
---|
40 | * freely use it for any purpose without restriction. |
---|
41 | * |
---|
42 | * Maintenance of notices |
---|
43 | * ---------------------- |
---|
44 | * In the interest of clarity regarding the origin and status of this |
---|
45 | * SLAC software, this and all the preceding Stanford University notices |
---|
46 | * are to remain affixed to any copy or derivative of this software made |
---|
47 | * or distributed by the recipient and are to be affixed to any copy of |
---|
48 | * software made or distributed by the recipient that contains a copy or |
---|
49 | * derivative of this software. |
---|
50 | * |
---|
51 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
52 | */ |
---|
53 | #include <rtems.h> |
---|
54 | #include <rtems/libi2c.h> |
---|
55 | |
---|
56 | #include <libchip/i2c-ds1621.h> |
---|
57 | |
---|
58 | #include <rtems/libio.h> |
---|
59 | |
---|
60 | |
---|
61 | static rtems_status_code |
---|
62 | ds1621_init (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
63 | void *arg) |
---|
64 | { |
---|
65 | int sc; |
---|
66 | unsigned char csr[2] = { DS1621_CMD_CSR_ACCESS, 0 }, cmd; |
---|
67 | |
---|
68 | /* First start command acquires a lock for the bus */ |
---|
69 | |
---|
70 | /* Initialize; switch continuous conversion on */ |
---|
71 | sc = rtems_libi2c_start_write_bytes (minor, csr, 1); |
---|
72 | if (sc < 0) |
---|
73 | return -sc; |
---|
74 | |
---|
75 | sc = rtems_libi2c_start_read_bytes (minor, csr + 1, 1); |
---|
76 | if (sc < 0) |
---|
77 | return -sc; |
---|
78 | |
---|
79 | csr[1] &= ~DS1621_CSR_1SHOT; |
---|
80 | |
---|
81 | sc = rtems_libi2c_start_write_bytes (minor, csr, 2); |
---|
82 | if (sc < 0) |
---|
83 | return -sc; |
---|
84 | |
---|
85 | /* Start conversion */ |
---|
86 | cmd = DS1621_CMD_START_CONV; |
---|
87 | |
---|
88 | sc = rtems_libi2c_start_write_bytes (minor, &cmd, 1); |
---|
89 | if (sc < 0) |
---|
90 | return -sc; |
---|
91 | |
---|
92 | /* sending 'stop' relinquishes the bus mutex -- don't hold it |
---|
93 | * across system calls! |
---|
94 | */ |
---|
95 | return rtems_libi2c_send_stop (minor); |
---|
96 | } |
---|
97 | |
---|
98 | static rtems_status_code |
---|
99 | ds1621_read (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
100 | void *arg) |
---|
101 | { |
---|
102 | int sc; |
---|
103 | rtems_libio_rw_args_t *rwargs = arg; |
---|
104 | unsigned char cmd = DS1621_CMD_READ_TEMP; |
---|
105 | |
---|
106 | sc = rtems_libi2c_start_write_bytes (minor, &cmd, 1); |
---|
107 | if (sc < 0) |
---|
108 | return -sc; |
---|
109 | if (sc < 1) |
---|
110 | return RTEMS_IO_ERROR; |
---|
111 | sc = rtems_libi2c_start_read_bytes(minor, (unsigned char *)rwargs->buffer, 1); |
---|
112 | if (sc < 0) { |
---|
113 | rwargs->bytes_moved = 0; |
---|
114 | return -sc; |
---|
115 | } |
---|
116 | rwargs->bytes_moved = 1; |
---|
117 | return rtems_libi2c_send_stop (minor); |
---|
118 | } |
---|
119 | |
---|
120 | static rtems_driver_address_table myops = { |
---|
121 | .initialization_entry = ds1621_init, |
---|
122 | .read_entry = ds1621_read, |
---|
123 | }; |
---|
124 | |
---|
125 | static rtems_libi2c_drv_t my_drv_tbl = { |
---|
126 | .ops = &myops, |
---|
127 | .size = sizeof (my_drv_tbl), |
---|
128 | }; |
---|
129 | |
---|
130 | rtems_libi2c_drv_t *i2c_ds1621_driver_descriptor = &my_drv_tbl; |
---|