Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file ds2482.c \brief Dallas DS2482 I2C-to-Dallas1Wire Master Library. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'ds2482.c' |
||
5 | // Title : Dallas DS2482 I2C-to-Dallas1Wire Master Library |
||
6 | // Author : Pascal Stang - Copyright (C) 2004 |
||
7 | // Created : 2004.09.27 |
||
8 | // Revised : 2004.09.27 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR Series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
14 | // to be lacking in some functionality or documentation, or may not be fully |
||
15 | // tested. Nonetheless, you can expect most functions to work. |
||
16 | // |
||
17 | // This code is distributed under the GNU Public License |
||
18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
19 | // |
||
20 | //***************************************************************************** |
||
21 | |||
22 | #include <avr/io.h> |
||
23 | #include <avr/interrupt.h> |
||
24 | |||
25 | #include "global.h" |
||
26 | #include "i2c.h" |
||
27 | #include "ds2482.h" |
||
28 | |||
29 | #include "rprintf.h" |
||
30 | #include "timer.h" |
||
31 | |||
32 | // global variables |
||
33 | u08 DS2482I2cAddr; |
||
34 | |||
35 | // Functions |
||
36 | u08 ds2482Init(u08 i2cAddr) |
||
37 | { |
||
38 | // select device |
||
39 | DS2482I2cAddr = i2cAddr; |
||
40 | // reset DS2482 chip |
||
41 | return ds2482Reset(DS2482I2cAddr); |
||
42 | |||
43 | } |
||
44 | |||
45 | u08 ds2482Reset(u08 i2cAddr) |
||
46 | { |
||
47 | // select device |
||
48 | DS2482I2cAddr = i2cAddr; |
||
49 | return ds2482SendCmd(DS2482_CMD_DRST); |
||
50 | } |
||
51 | |||
52 | u08 ds2482SendCmd(u08 cmd) |
||
53 | { |
||
54 | u08 data; |
||
55 | u08 i2cStat; |
||
56 | |||
57 | // send command |
||
58 | i2cStat = i2cMasterSendNI(DS2482I2cAddr, 1, &cmd); |
||
59 | if(i2cStat == I2C_ERROR_NODEV) |
||
60 | { |
||
61 | rprintf("No I2C Device\r\n"); |
||
62 | return i2cStat; |
||
63 | } |
||
64 | // check status |
||
65 | i2cStat = i2cMasterReceiveNI(DS2482I2cAddr, 1, &data); |
||
66 | |||
67 | // rprintf("Cmd=0x%x Status=0x%x\r\n", cmd, data); |
||
68 | |||
69 | return (i2cStat == I2C_OK); |
||
70 | } |
||
71 | |||
72 | u08 ds2482SendCmdArg(u08 cmd, u08 arg) |
||
73 | { |
||
74 | u08 data[2]; |
||
75 | u08 i2cStat; |
||
76 | |||
77 | // prepare command |
||
78 | data[0] = cmd; |
||
79 | data[1] = arg; |
||
80 | // send command |
||
81 | i2cStat = i2cMasterSendNI(DS2482I2cAddr, 2, data); |
||
82 | if(i2cStat == I2C_ERROR_NODEV) |
||
83 | { |
||
84 | rprintf("No I2C Device\r\n"); |
||
85 | return i2cStat; |
||
86 | } |
||
87 | // check status |
||
88 | i2cStat = i2cMasterReceiveNI(DS2482I2cAddr, 1, data); |
||
89 | |||
90 | // rprintf("Cmd=0x%x Arg=0x%x Status=0x%x\r\n", cmd, arg, data[0]); |
||
91 | |||
92 | return (i2cStat == I2C_OK); |
||
93 | } |
||
94 | |||
95 | u08 ds2482BusyWait(void) |
||
96 | { |
||
97 | u08 status; |
||
98 | // set read pointer to status register |
||
99 | ds2482SendCmdArg(DS2482_CMD_SRP, DS2482_READPTR_SR); |
||
100 | // check status until busy bit is cleared |
||
101 | do |
||
102 | { |
||
103 | i2cMasterReceiveNI(DS2482I2cAddr, 1, &status); |
||
104 | } while(status & DS2482_STATUS_1WB); |
||
105 | // return the status register value |
||
106 | return status; |
||
107 | } |
||
108 | |||
109 | u08 ds2482BusReset(void) |
||
110 | { |
||
111 | u08 status; |
||
112 | // send 1-Wire bus reset command |
||
113 | ds2482SendCmd(DS2482_CMD_1WRS); |
||
114 | // wait for bus reset to finish, and get status |
||
115 | status = ds2482BusyWait(); |
||
116 | // return state of the presence bit |
||
117 | return (status & DS2482_STATUS_PPD); |
||
118 | } |
||
119 | |||
120 | u08 ds2482BusTransferBit(u08 bit) |
||
121 | { |
||
122 | u08 status; |
||
123 | // writes and reads a bit on the bus |
||
124 | // wait for DS2482 to be ready |
||
125 | ds2482BusyWait(); |
||
126 | // send 1WSB command |
||
127 | ds2482SendCmdArg(DS2482_CMD_1WSB, bit?0x00:0x80); |
||
128 | // wait for command to finish |
||
129 | status = ds2482BusyWait(); |
||
130 | // return read-slot bit value |
||
131 | if(status & DS2482_STATUS_SBR) |
||
132 | return 1; |
||
133 | else |
||
134 | return 0; |
||
135 | } |
||
136 | |||
137 | u08 ds2482BusTriplet(u08 dir) |
||
138 | { |
||
139 | u08 status; |
||
140 | // this command is used to simplify search-rom operations |
||
141 | // generates two read timeslots and one write timeslot |
||
142 | // dir input determines value of write if reads are both 0 |
||
143 | |||
144 | // wait for DS2482 to be ready |
||
145 | ds2482BusyWait(); |
||
146 | // send 1WSB command |
||
147 | ds2482SendCmdArg(DS2482_CMD_1WT, dir?0x00:0x80); |
||
148 | // wait for command to finish |
||
149 | status = ds2482BusyWait(); |
||
150 | // return the value of the read slots |
||
151 | return (status & (DS2482_STATUS_SBR|DS2482_STATUS_TSB))>>5; |
||
152 | } |
||
153 | |||
154 | u08 ds2482BusLevel(void) |
||
155 | { |
||
156 | u08 status; |
||
157 | // get status |
||
158 | status = ds2482BusyWait(); |
||
159 | // return bus level value |
||
160 | if(status & DS2482_STATUS_LL) |
||
161 | return 1; |
||
162 | else |
||
163 | return 0; |
||
164 | } |
||
165 | |||
166 | void ds2482BusWriteByte(u08 data) |
||
167 | { |
||
168 | // wait for DS2482 to be ready |
||
169 | ds2482BusyWait(); |
||
170 | // send 1WWB command |
||
171 | ds2482SendCmdArg(DS2482_CMD_1WWB, data); |
||
172 | } |
||
173 | |||
174 | u08 ds2482BusReadByte(void) |
||
175 | { |
||
176 | u08 data; |
||
177 | // wait for DS2482 to be ready |
||
178 | ds2482BusyWait(); |
||
179 | // send 1WRB command |
||
180 | ds2482SendCmd(DS2482_CMD_1WRB); |
||
181 | // wait for read to finish |
||
182 | ds2482BusyWait(); |
||
183 | // set read pointer to data register |
||
184 | ds2482SendCmdArg(DS2482_CMD_SRP, DS2482_READPTR_RDR); |
||
185 | // read data |
||
186 | i2cMasterReceiveNI(DS2482I2cAddr, 1, &data); |
||
187 | // return data |
||
188 | return data; |
||
189 | } |
Powered by WebSVN v2.8.3