-
Notifications
You must be signed in to change notification settings - Fork 1
/
sht21.c
170 lines (153 loc) · 3.58 KB
/
sht21.c
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
/*
i2c.c
Created on: 15.01.2017
Author: benni
*/
#include "sht21.h"
#include <Arduino.h>
#include "i2c.h"
#define READ_FLAG 1
#define MEAS_TIME 150 ///< Measurement time in ms
#define MEAS_AND_CHECK 2 ///< Read 2 databytes and the checksum
// #define ICACHE_FLASH_ATTR
typedef struct {
uint16_t temperature;
uint16_t humidity;
uint16_t checksum;
uint8_t retries;
bool running;
tSht21MeasureTypes type;
} tSht21Context;
static MeasurementDoneHandler userCallback;
static tSht21Context tContext = {0, 0, 0, 0, false, TRG_TEMP_MEAS_NO_HOLD};
static bool sht_measurement_done();
typedef enum {
ADDRESS = 0x80,
TRG_TEMP_MEAS_HOLD = 0xE3,
TRG_HUMI_MEAS_HOLD = 0xE5,
// TRG_TEMP_MEAS_NO_HOLD = 0xF3,
// TRG_HUMI_MEAS_NO_HOLD = 0xF5,
WRITE_USER_REG = 0xE6,
READ_USER_REG = 0xE7,
TRG_SOFT_RESET = 0xFE
} tSht21Defines;
static void measureTimerCb()
{
bool ack = sht_measurement_done();
// If the measurement is already done we call the users callback otherwise we wait
if (!ack)
{
if (tContext.retries++ > 3)
{
tContext.running = false;
sht_soft_reset();
tContext.retries = 0;
if (userCallback != NULL)
{
userCallback(&tContext.temperature, &tContext.humidity);
}
return;
}
delay(150);
measureTimerCb();
}
else
{
tContext.running = false;
sht_soft_reset();
tContext.retries = 0;
// printf("meas done, calling user\n");
if (userCallback != NULL)
{
userCallback(&tContext.temperature, &tContext.humidity);
}
}
}
// Interface methods
void sht_init()
{
i2c_init();
delay(15);
}
bool sht_trigger_measurement(tSht21MeasureTypes type, MeasurementDoneHandler cb)
{
bool ret = false;
if (!tContext.running)
{
tContext.running = true;
tContext.type = type;
userCallback = cb;
bool ret;
i2c_start();
ret = i2c_write(ADDRESS);
if (i2c_write(type)) {
delayMicroseconds(20);
}
i2c_stop();
delay(1);
measureTimerCb();
ret = true;
}
return ret;
}
static bool sht_measurement_done()
{
bool ack = false;
if (tContext.running)
{
uint16 *data = tContext.type == TRG_TEMP_MEAS_NO_HOLD ? &tContext.temperature : &tContext.humidity;
i2c_start();
ack = i2c_write(ADDRESS | READ_FLAG);
if (ack)
{
uint8_t meas;
*data = 0;
for (meas = 0; meas <= MEAS_AND_CHECK; meas++)
{
uint8_t tmp = i2c_read(meas < MEAS_AND_CHECK);
if (meas < MEAS_AND_CHECK) {
*data |= tmp << (MEAS_AND_CHECK - meas - 1) * 8;
// printf("we received: %d\n", tmp);
}
}
}
i2c_stop();
// Clear last two bits
// TODO: Use last bits to retrieve which measurement has been started
*data &= ~0x02;
}
return ack;
}
bool sht_soft_reset()
{
// FIXME: Add reset time of 15 ms where no
// communication can be established after reset.
// Sending data in the reset time can render the
// sensor inoperable until the next restart.
return true;
if (!tContext.running)
{
i2c_start();
i2c_write(ADDRESS);
i2c_write(TRG_SOFT_RESET);
i2c_stop();
return true;
}
return false;
}
tShtSettings sht_get_user()
{
i2c_start();
i2c_write(ADDRESS);
i2c_write(READ_USER_REG);
i2c_start();
i2c_write(ADDRESS | READ_FLAG);
byte readVal = i2c_read(true);
i2c_stop();
tShtSettings ret = {0, 0, 0, 0};
ret.BatteryState = (readVal >> 6) & 0x01;
ret.DisableOtpReaload = (readVal >> 1) & 0x01;
ret.EnableHeater = (readVal >> 2) & 0x01;
ret.MeasResolution = ((readVal >> 6) & 0x02) | (readVal & 0x01);
return ret;
}