凌云物联网实验室ISKBoard(IoT Starter Kits Board)开发板项目源码
guowenxue
2023-04-03 32806da6f5647ac637fa7d48aa9c221b091ab35e
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
/*
 * sht20.c
 *
 *  Created on: Jan 19, 2023
 *      Author: Wenxue
 */
 
#include <sht20.h>
#include <stdio.h>
#include <string.h>
 
#define delay_ms(x)        HAL_Delay(x)
 
enum
{
    TRIG_T_MEASUREMENT_HM    = 0xE3, // command trig. temp meas. hold master
    TRIG_RH_MEASUREMENT_HM   = 0xE5, // command trig. humidity meas. hold master
    TRIG_T_MEASUREMENT_POLL  = 0xF3, // command trig. temp meas. no hold master
    TRIG_RH_MEASUREMENT_POLL = 0xF5, // command trig. humidity meas. no hold master
    USER_REG_W               = 0xE6, // command writing user register
    USER_REG_R               = 0xE7, // command reading user register
    SOFT_RESET               = 0xFE  // command soft reset
};
 
 
//#define CONFIG_SHT2X_DEBUG
 
#ifdef CONFIG_SHT2X_DEBUG
static inline void dump_buf(uint8_t *buf, uint32_t size)
{
    int      i;
    
    if(!buf)
        return ;
    
    for(i=0; i<size; i++)
        printf("%02x ", buf[i]);
    
    printf("\r\n");
}
#endif
 
#ifdef CONFIG_SHT2X_DEBUG
#define sht2x_print(format,args...) printf(format, ##args)
#else
#define sht2x_print(format,args...) do{} while(0);
#endif
    
    
static int sht2x_measure_value(uint8_t command, uint16_t *val);
static int sht2x_softreset(void);
 
 
uint32_t sht20_sample_TrH(uint32_t *temperature, uint32_t *humdity)
{
    uint16_t       sT;
    uint16_t       sRH;
    uint32_t       T;
    uint32_t       RH;
    uint32_t       TRH=TRH_FAIL_VAL;
 
    i2c_term(SHT2X_I2CBUS);
 
    if( i2c_init(SHT2X_I2CBUS, SHT2X_CHIPADDR) )
    {
        sht2x_print("SHT20 initial I2C bus failed.\r\n");
        return TRH;
    }
 
    if( sht2x_softreset() )
    {
        sht2x_print("SHT20 soft reset failed.\r\n");
        goto OUT;
    }
 
    sht2x_print("Start to measurement temperature...\r\n");
    if( sht2x_measure_value(TRIG_T_MEASUREMENT_POLL, &sT) < 0 )
    {
        sht2x_print("I2C measurement failed\r\n");
        goto OUT;
    }
  
    T=(uint32_t)(100*(float)((175.72*sT)/65536-46.85));
    sht2x_print("Measure temperature: %lu.%lu\r\n", T/100, T%100);
    if( temperature )
        *temperature=T;
 
    TRH = ((T/100)<<8|(T%100))<<16 | 0xFFFF;
  
    sht2x_print("Start to measurement relative humidity...\r\n");
    if( sht2x_measure_value(TRIG_RH_MEASUREMENT_POLL, &sRH) < 0 )
    {
        goto OUT;
    }
 
    RH=(uint32_t)(100*(float)((125*sRH)/65535-6.0));
    sht2x_print("Measure relative humidity: %lu.%lu%%\r\n", RH/100, RH%100);
    if( humdity )
        *humdity=RH;
    
    TRH &=  0xFFFF0000 | ((uint16_t)RH/100)<<8 | ((uint16_t)RH%100) ;
 
OUT:  
    i2c_term(SHT2X_I2CBUS);
    return TRH;
}
 
int sht2x_softreset(void)
{
    uint8_t           command = SOFT_RESET;
    int               rv = 0;
    
    sht2x_print("Start soft reset sht2x\r\n");
    rv=i2c_write(SHT2X_I2CBUS, &command, 1);
    
    if( rv )
    {
        sht2x_print("SHT2X send soft reset command 0x%0x failure: rv=0x%02x\r\n", command, rv);
        return -rv;
    }
    
    delay_ms(15);
    return 0;
}
 
static int sht2x_checkcrc(uint8_t *data, uint8_t bytes, uint8_t checksum)
{
  uint8_t crc = 0;    
  uint8_t i;
  uint8_t bit;
  
  //calculates 8-Bit checksum with given polynomial
  for (i=0; i<bytes; ++i)
  { 
    crc ^= (data[i]);
    for (bit=8; bit>0; --bit)
    { 
      if (crc & 0x80) 
        crc = (crc << 1) ^ 0x0131; //POLYNOMIAL;
      else 
        crc = (crc << 1);
    }
  }
 
  if (crc != checksum)
  {
    return -1;
  }
  else 
  {
    return 0;
  }
}
 
static int sht2x_measure_value(uint8_t command, uint16_t *val)
{
    uint8_t        buf[3];    /* I2C������buffer */
    int            count=4;    /* �ܹ����Ĵ��� */
  
    if( !val )
    {
        sht2x_print("SHT2X invalid input arguments\r\n");
        return -1;
    }
    
    if(TRIG_T_MEASUREMENT_POLL!=command && TRIG_RH_MEASUREMENT_POLL !=command )
    {
        sht2x_print("SHT2X unsupport command: 0x%0x\r\n", command);
        return -2;
    }
 
    if( NO_ERROR != i2c_write(SHT2X_I2CBUS, &command, 1) )
    {
        sht2x_print("SHT2X send measure command 0x%0x failure\r\n", command);
        return -3;
    }
  
    if(TRIG_T_MEASUREMENT_POLL == command)
        delay_ms(85);
    else
        delay_ms(29);
 
    while(count--)
    {
        memset(buf, 0, 3);
 
        if( !i2c_read(SHT2X_I2CBUS, buf, 3) )
        {
            break;
        }
        delay_ms(5);
    }
 
    if( sht2x_checkcrc(buf, 2, buf[2])< 0 )
    {
#ifdef CONFIG_SHT2X_DEBUG        
        sht2x_print("Measurement data checksum failure:\r\n");
        dump_buf(buf, 3);
#endif
        return -4;
    }
 
    if(TRIG_T_MEASUREMENT_POLL == command)
        *val = buf[0]<<8|(buf[1]&0xFC); //14bits(1111 1100)
    else
        *val = buf[0]<<8|(buf[1]&0xFF); //12bits(1100 0000)
 
    sht2x_print("Measurement temperature value: 0x%04x\r\n", *val);
  
  return 0;
}