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 | /* Author: Till Straumann, 2005 */ |
---|
10 | #include <rtems.h> |
---|
11 | #include <rtems/libi2c.h> |
---|
12 | |
---|
13 | #include <libchip/i2c-ds1621.h> |
---|
14 | |
---|
15 | #include <rtems/libio.h> |
---|
16 | |
---|
17 | |
---|
18 | static rtems_status_code |
---|
19 | ds1621_init (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
20 | void *arg) |
---|
21 | { |
---|
22 | int sc; |
---|
23 | char csr[2] = { DS1621_CMD_CSR_ACCESS, 0 }, cmd; |
---|
24 | |
---|
25 | /* First start command acquires a lock for the bus */ |
---|
26 | |
---|
27 | /* Initialize; switch continuous conversion on */ |
---|
28 | sc = rtems_libi2c_start_write_bytes (minor, csr, 1); |
---|
29 | if (sc < 0) |
---|
30 | return -sc; |
---|
31 | |
---|
32 | sc = rtems_libi2c_start_read_bytes (minor, csr + 1, 1); |
---|
33 | if (sc < 0) |
---|
34 | return -sc; |
---|
35 | |
---|
36 | csr[1] &= ~DS1621_CSR_1SHOT; |
---|
37 | |
---|
38 | sc = rtems_libi2c_start_write_bytes (minor, csr, 2); |
---|
39 | if (sc < 0) |
---|
40 | return -sc; |
---|
41 | |
---|
42 | /* Start conversion */ |
---|
43 | cmd = DS1621_CMD_START_CONV; |
---|
44 | |
---|
45 | sc = rtems_libi2c_start_write_bytes (minor, &cmd, 1); |
---|
46 | if (sc < 0) |
---|
47 | return -sc; |
---|
48 | |
---|
49 | /* sending 'stop' relinquishes the bus mutex -- don't hold it |
---|
50 | * across system calls! |
---|
51 | */ |
---|
52 | return rtems_libi2c_send_stop (minor); |
---|
53 | } |
---|
54 | |
---|
55 | static rtems_status_code |
---|
56 | ds1621_read (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
57 | void *arg) |
---|
58 | { |
---|
59 | int sc; |
---|
60 | rtems_libio_rw_args_t *rwargs = arg; |
---|
61 | char cmd = DS1621_CMD_READ_TEMP; |
---|
62 | |
---|
63 | sc = rtems_libi2c_start_write_bytes (minor, &cmd, 1); |
---|
64 | if (sc < 0) |
---|
65 | return -sc; |
---|
66 | if (sc < 1) |
---|
67 | return RTEMS_IO_ERROR; |
---|
68 | sc = rtems_libi2c_start_read_bytes (minor, rwargs->buffer, 1); |
---|
69 | if (sc < 0) { |
---|
70 | rwargs->bytes_moved = 0; |
---|
71 | return -sc; |
---|
72 | } |
---|
73 | rwargs->bytes_moved = 1; |
---|
74 | return rtems_libi2c_send_stop (minor); |
---|
75 | } |
---|
76 | |
---|
77 | static rtems_driver_address_table myops = { |
---|
78 | initialization_entry: ds1621_init, |
---|
79 | read_entry: ds1621_read, |
---|
80 | }; |
---|
81 | |
---|
82 | static rtems_libi2c_drv_t my_drv_tbl = { |
---|
83 | ops: &myops, |
---|
84 | size: sizeof (my_drv_tbl), |
---|
85 | }; |
---|
86 | |
---|
87 | rtems_libi2c_drv_t *i2c_ds1621_driver_descriptor = &my_drv_tbl; |
---|