STM32 V5 source code
guowenxue
2018-02-04 785deec23b4cb1e7c4c4d81eb808f195adb1d98a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (c) 2007, Swedish Institute of Computer Science
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
 
/*
 * Device driver for the Sensirion SHT1x/SHT7x family of humidity and
 * temperature sensors.
 */
 
#include "contiki.h"
#include <stdio.h>
#include <dev/sht11.h>
#include "sht11-arch.h"
 
#define DEBUG 0
 
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
 
#ifndef SDA_0
#define SDA_0()   (SHT11_PxDIR |=  BV(SHT11_ARCH_SDA))    /* SDA Output=0 */
#define SDA_1()   (SHT11_PxDIR &= ~BV(SHT11_ARCH_SDA))    /* SDA Input */
#define SDA_IS_1  (SHT11_PxIN & BV(SHT11_ARCH_SDA))
 
#define SCL_0()   (SHT11_PxOUT &= ~BV(SHT11_ARCH_SCL))    /* SCL Output=0 */
#define SCL_1()   (SHT11_PxOUT |=  BV(SHT11_ARCH_SCL))    /* SCL Output=1 */
#endif
                /* adr   command  r/w */
#define  STATUS_REG_W   0x06    /* 000    0011    0 */
#define  STATUS_REG_R   0x07    /* 000    0011    1 */
#define  MEASURE_TEMP   0x03    /* 000    0001    1 */
#define  MEASURE_HUMI   0x05    /* 000    0010    1 */
#define  RESET          0x1e    /* 000    1111    0 */
 
/* This can probably be reduced to 250ns according to data sheet. */
#ifndef delay_400ns
#define delay_400ns() _NOP()
#endif
/*---------------------------------------------------------------------------*/
static void
sstart(void)
{
  SDA_1(); SCL_0();
  delay_400ns();
  SCL_1();
  delay_400ns();
  SDA_0();
  delay_400ns();
  SCL_0();
  delay_400ns();
  SCL_1();
  delay_400ns();
  SDA_1();
  delay_400ns();
  SCL_0();
}
/*---------------------------------------------------------------------------*/
static void
sreset(void)
{
  int i;
  SDA_1();
  SCL_0();
  for(i = 0; i < 9 ; i++) {
    SCL_1();
    delay_400ns();
    SCL_0();
    delay_400ns();
  }
  sstart();            /* Start transmission, why??? */
}
/*---------------------------------------------------------------------------*/
/*
 * Return true if we received an ACK.
 */
static int
swrite(unsigned _c)
{
  unsigned char c = _c;
  int i;
  int ret;
 
  for(i = 0; i < 8; i++, c <<= 1) {
    if(c & 0x80) {
      SDA_1();
    } else {
      SDA_0();
    }
    SCL_1();
    delay_400ns();
    SCL_0();
    delay_400ns();
  }
 
  SDA_1();
  SCL_1();
  delay_400ns();
  ret = !SDA_IS_1;
 
  SCL_0();
 
  return ret;
}
/*---------------------------------------------------------------------------*/
static unsigned
sread(int send_ack)
{
  int i;
  unsigned char c = 0x00;
 
  SDA_1();
  for(i = 0; i < 8; i++) {
    c <<= 1;
    SCL_1();
    delay_400ns();
    if(SDA_IS_1) {
      c |= 0x1;
    }
    SCL_0();
    delay_400ns();
  }
 
  if(send_ack) {
    SDA_0();
  }
  SCL_1();
  delay_400ns();
  SCL_0();
 
  SDA_1();            /* Release SDA */
 
  return c;
}
/*---------------------------------------------------------------------------*/
#define CRC_CHECK
#ifdef CRC_CHECK
static unsigned char
rev8bits(unsigned char v)
{
  unsigned char r = v;
  int s = 7;
 
  for (v >>= 1; v; v >>= 1) {
    r <<= 1;
    r |= v & 1;
    s--;
  }
  r <<= s;            /* Shift when v's highest bits are zero */
  return r;
}
/*---------------------------------------------------------------------------*/
/* BEWARE: Bit reversed CRC8 using polynomial ^8 + ^5 + ^4 + 1 */
static unsigned
crc8_add(unsigned acc, unsigned byte)
{
  int i;
  acc ^= byte;
  for(i = 0; i < 8; i++) {
    if(acc & 0x80) {
      acc = (acc << 1) ^ 0x31;
    } else {
      acc <<= 1;
    }
  }
  return acc & 0xff;
}
#endif /* CRC_CHECK */
/*---------------------------------------------------------------------------*/
/*
 * Power up the device. The device can be used after an additional
 * 11ms waiting time.
 */
void
sht11_init(void)
{
  /*
   * SCL Output={0,1}
   * SDA 0: Output=0
   *     1: Input and pull-up (Output=0)
   */
#ifdef SHT11_INIT
  SHT11_INIT();
#else
  /* As this driver is bit-bang based, disable the I2C first
     This assumes the SDA/SCL pins passed in the -arch.h file are 
     actually the same used for I2C operation, else comment out the following
  */
  #warning SHT11: DISABLING I2C BUS
  SHT11_PxSEL &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
  #if defined(__MSP430_HAS_MSP430X_CPU__) || defined(__MSP430_HAS_MSP430XV2_CPU__)
    SHT11_PxREN &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
  #endif
 
  /* Configure SDA/SCL as GPIOs */
  SHT11_PxOUT |= BV(SHT11_ARCH_PWR);
  SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
  SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
#endif
}
/*---------------------------------------------------------------------------*/
/*
 * Power of device.
 */
void
sht11_off(void)
{
#ifdef SHT11_OFF
  SHT11_OFF();
#else
  SHT11_PxOUT &= ~BV(SHT11_ARCH_PWR);
  SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
  SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
#endif
}
/*---------------------------------------------------------------------------*/
/*
 * Only commands MEASURE_HUMI or MEASURE_TEMP!
 */
static unsigned int
scmd(unsigned cmd)
{
  unsigned int n;
 
  if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
    PRINTF("Illegal command: %d\n", cmd);
    return -1;
  }
 
  sstart();            /* Start transmission */
  if(!swrite(cmd)) {
    PRINTF("SHT11: scmd - swrite failed\n");
    goto fail;
  }
 
  for(n = 0; n < 20000; n++) {
    if(!SDA_IS_1) {
      unsigned t0, t1, rcrc;
      t0 = sread(1);
      t1 = sread(1);
      rcrc = sread(0);
      PRINTF("SHT11: scmd - read %d, %d\n", t0, t1);
#ifdef CRC_CHECK
      {
    unsigned crc;
    crc = crc8_add(0x0, cmd);
    crc = crc8_add(crc, t0);
    crc = crc8_add(crc, t1);
    if(crc != rev8bits(rcrc)) {
      PRINTF("SHT11: scmd - crc check failed %d vs %d\n",
         crc, rev8bits(rcrc));
      goto fail;
    }
      }
#endif
      return (t0 << 8) | t1;
    }
    /* short wait before next loop */
    clock_wait(1);
  }
 fail:
  sreset();
  return -1;
}
/*---------------------------------------------------------------------------*/
/*
 * Call may take up to 210ms.
 */
unsigned int
sht11_temp(void)
{
  return scmd(MEASURE_TEMP);
}
/*---------------------------------------------------------------------------*/
/*
 * Call may take up to 210ms.
 */
unsigned int
sht11_humidity(void)
{
  return scmd(MEASURE_HUMI);
}
/*---------------------------------------------------------------------------*/
#if 1 /* But ok! */
unsigned
sht11_sreg(void)
{
  unsigned sreg, rcrc;
 
  sstart();            /* Start transmission */
  if(!swrite(STATUS_REG_R)) {
    goto fail;
  }
 
  sreg = sread(1);
  rcrc = sread(0);
 
#ifdef CRC_CHECK
  {
    unsigned crc;
    crc = crc8_add(0x0, STATUS_REG_R);
    crc = crc8_add(crc, sreg);
    if (crc != rev8bits(rcrc))
      goto fail;
  }
#endif
 
  return sreg;
 
 fail:
  sreset();
  return -1;
}
#endif
/*---------------------------------------------------------------------------*/
#if 0
int
sht11_set_sreg(unsigned sreg)
{
  sstart();            /* Start transmission */
  if(!swrite(STATUS_REG_W)) {
    goto fail;
  }
  if(!swrite(sreg)) {
    goto fail;
  }
 
  return 0;
 
 fail:
  sreset();
  return -1;
}
#endif
/*---------------------------------------------------------------------------*/
#if 0
int
sht11_reset(void)
{
  sstart();            /* Start transmission */
  if(!swrite(RESET)) {
    goto fail;
  }
 
  return 0;
 
 fail:
  sreset();
  return -1;
}
#endif
/*---------------------------------------------------------------------------*/