Contiki 3.x
sht15.c
1 /* Copyright (c) 2009 ARAGO SYSTEMS
2  All rights reserved.
3 
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions are met:
6 
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in
11  the documentation and/or other materials provided with the
12  distribution.
13  * Neither the name of the copyright holders nor the names of
14  contributors may be used to endorse or promote products derived
15  from this software without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "dev/sht15.h"
30 
31 #define DATA_OUT() P3DIR |= BIT7
32 #define DATA_IN() P3DIR &= ~BIT7
33 #define DATA_SET() P3OUT |= BIT7; halMcuWaitUs(10)
34 #define DATA_CLR() P3OUT &= ~BIT7; halMcuWaitUs(10)
35 #define DATA_VAL() (P3IN & BIT7)
36 
37 #define SCK_OUT() P5DIR |= BIT4
38 #define SCK_SET() P5OUT |= BIT4; halMcuWaitUs(10)
39 #define SCK_CLR() P5OUT &= ~BIT4; halMcuWaitUs(10)
40 
41 
42 /***********************************************************************************
43 * @fn halMcuWaitUs
44 *
45 * @brief Busy wait function. Waits the specified number of microseconds. Use
46 * assumptions about number of clock cycles needed for the various
47 * instructions. The duration of one cycle depends on MCLK. In this HAL
48 * , it is set to 8 MHz, thus 8 cycles per usec.
49 *
50 * NB! This function is highly dependent on architecture and compiler!
51 *
52 * @param uint16 usec - number of microseconds delay
53 *
54 * @return none
55 */
56 
57 /* #pragma optimize=none */
58 void
59 halMcuWaitUs(uint16_t usec) /* 5 cycles for calling */
60 {
61  /* The least we can wait is 3 usec: */
62  /* ~1 usec for call, 1 for first compare and 1 for return */
63  while(usec > 3) /* 2 cycles for compare */
64  { /* 2 cycles for jump */
65  nop(); /* 1 cycles for nop */
66  nop(); /* 1 cycles for nop */
67  nop(); /* 1 cycles for nop */
68  nop(); /* 1 cycles for nop */
69  nop(); /* 1 cycles for nop */
70  nop(); /* 1 cycles for nop */
71  nop(); /* 1 cycles for nop */
72  nop(); /* 1 cycles for nop */
73  usec -= 2; /* 1 cycles for optimized decrement */
74  }
75 } /* 4 cycles for returning */
76 
77 
78 /**
79  SHT15/75 Driver
80 
81  !!! be advise that the SHT15 and SHT75 are not i2C compliant sensors
82  they are just designed to not disturb i2C devices on a 2 wire bus
83  this driver allow to drive the sensor with GPIO and delay
84 */
85 
86 /***********************************************************************************
87 * @fn sht15_send_start
88 *
89 * @brief This function perform the start sequence asked by SHT15 and SHT75
90 *
91 *
92 *
93 * @param none
94 *
95 * @return none
96 */
97 void
98 sht15_send_start(void)
99 {
100  /* Sequence is to set data line to 1 then clock line to 1
101  then set data line to 0, clock line to 0
102  then set clock line to 1 and data line to 1
103  ___________ ________
104  data line : _____/ \___________/
105  ___________ ____________
106  clock line : _________/ \___/
107  */
108 
109  DATA_OUT();
110  DATA_SET();
111  SCK_SET();
112  DATA_CLR();
113  SCK_CLR();
114  SCK_SET();
115  DATA_SET();
116  SCK_CLR();
117 }
118 /***********************************************************************************
119 * @fn sht15_read16()
120 *
121 * @brief
122 *
123 *
124 *
125 * @param none
126 *
127 * @return uint16_t
128 */
129 uint16_t
130 sht15_read16(void)
131 {
132  uint16_t i;
133  DATA_IN();
134 
135  SCK_CLR();
136  uint16_t val = 0;
137 
138  for(i = 0; i < 18; i++) {
139  if((i != 8) && (i != 17)) {
140  SCK_SET();
141  if(DATA_VAL()) {
142  val |= 1;
143  }
144 
145  val <<= 1;
146  SCK_CLR();
147  } else if(i == 8) { /* Wait for first ACK from SHT15 */
148  DATA_OUT();
149 
150  DATA_CLR();
151 
152  SCK_SET();
153  SCK_CLR();
154 
155  DATA_IN();
156  } else if(i == 17) { /* Wait for second ACK from SHT15 */
157  DATA_OUT();
158 
159  DATA_SET();
160 
161  SCK_SET();
162  SCK_CLR();
163  }
164  }
165  return val;
166 }
167 /***********************************************************************************
168 * @fn sht15_write8
169 *
170 * @brief
171 *
172 *
173 *
174 * @param uint8 val
175 *
176 * @return none
177 */
178 void
179 sht15_write8(uint8_t val)
180 {
181  uint16_t i;
182 
183  DATA_OUT();
184 
185  for(i = 0; i < 8; i++) {
186  halMcuWaitUs(4);
187  SCK_CLR();
188 
189  if(val & 0x80) {
190  DATA_SET();
191  } else {
192  DATA_CLR();
193  }
194  val <<= 1;
195 
196  SCK_SET();
197  }
198 
199  DATA_IN();
200 
201  SCK_CLR();
202 
203  while(DATA_VAL());
204 
205  SCK_SET();
206  SCK_CLR();
207 }
208 /***********************************************************************************
209 * @fn sht15_wait_measure
210 *
211 * @brief
212 *
213 *
214 *
215 * @param none
216 *
217 * @return none
218 */
219 void
220 sht15_wait_measure(void)
221 {
222  while(DATA_VAL());
223 }
224 /***********************************************************************************
225 * @fn sht15_init
226 *
227 * @brief
228 *
229 *
230 *
231 * @param none
232 *
233 * @return none
234 */
235 void
236 sht15_init(void)
237 {
238  /* DATA and SCK lines are I/O */
239  P3SEL &= ~BIT7;
240  P5SEL &= ~BIT4;
241  /* Set SCK and DATA as output */
242  DATA_OUT();
243  SCK_OUT();
244  DATA_SET();
245  SCK_SET();
246 }
247 /***********************************************************************************
248 * @fn sht15_measure_temp
249 *
250 * @brief
251 *
252 *
253 *
254 * @param none
255 *
256 * @return none
257 */
258 void
259 sht15_measure_temp(void)
260 {
261  sht15_send_start();
262  sht15_write8(3);
263 }
264 /***********************************************************************************
265 * @fn sht15_measure_hum
266 *
267 * @brief
268 *
269 *
270 *
271 * @param none
272 *
273 * @return none
274 */
275 void sht15_measure_hum()
276 {
277  sht15_send_start();
278  sht15_write8(5);
279 }
280 /***********************************************************************************
281 * @fn sht15_read_status
282 *
283 * @brief
284 *
285 *
286 *
287 * @param none
288 *
289 * @return none
290 */
291 void
292 sht15_read_status()
293 {
294  sht15_send_start();
295  sht15_write8(7);
296 }
297 /***********************************************************************************
298 * @fn sht15_write_status
299 *
300 * @brief
301 *
302 *
303 *
304 * @param none
305 *
306 * @return none
307 */
308 void
309 sht15_write_status(void)
310 {
311  sht15_send_start();
312  sht15_write8(6);
313 }
314 /***********************************************************************************
315 * @fn sht15_soft_reset
316 *
317 * @brief
318 *
319 *
320 *
321 * @param none
322 *
323 * @return none
324 */
325 void
326 sht15_soft_reset(void)
327 {
328  sht15_send_start();
329  sht15_write8(30);
330 }