aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/dht.c
blob: 3c744fef01316e13dafca9c02604afc05361e7eb (plain)
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
/*
 * DHT11/22 driver
 *
 * Copyright (c) 2018 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "dht.h"
#include "util.h"

#include <string.h>
#include <avr/interrupt.h>


#define DHT_PORT	PORTB
#define DHT_PIN		PINB
#define DHT_DDR		DDRB
#define DHT_BIT		4u


enum dht_state {
	DHT_WAIT,		/* Wait for the next data transfer */
	DHT_START_OUT_LOW,	/* Start: Output low */
	DHT_START_OUT_HIGH,	/* Start: Output high */
	DHT_START_IN_LOW,	/* Start: Input low */
	DHT_START_IN_HIGH,	/* Start: Input high */
	DHT_DATA_IN_LOW,	/* Data bit input: Low level */
	DHT_DATA_IN_HIGH,	/* Data bit input: High level */
};

struct dht {
	enum dht_state state;
	dht_callback_t callback;
	uint8_t timer_count;
	uint8_t byte_count;
	uint8_t bit_mask;
	uint8_t data[DHT_DATA_LEN];
	bool finished;
};


static struct dht dht;


static uint8_t dht_checksum(const uint8_t *data)
{
	uint8_t i, checksum = 0u;

	for (i = 0u; i < DHT_DATA_LEN - 1u; i++)
		checksum = ((uint8_t)(checksum + data[i]) & 0xFFu);

	return checksum;
}

static void dht_pin_change_irq_en(bool enable)
{
	if (enable) {
		GIFR = 1u << PCIF;
		GIMSK |= 1u << PCIE;
	} else {
		GIMSK &= (uint8_t)~(1u << PCIE);
	}
}

static void dht_pin_output(bool output, bool state)
{
	if (output) {
		DHT_DDR |= 1u << DHT_BIT;
		if (state)
			DHT_PORT |= 1u << DHT_BIT;
		else
			DHT_PORT &= (uint8_t)~(1u << DHT_BIT);

	} else {
		DHT_DDR &= (uint8_t)~(1u << DHT_BIT);
		DHT_PORT |= 1u << DHT_BIT;
	}
}

static void dht_pin_init(void)
{
	dht_pin_change_irq_en(false);
	PCMSK |= 1u << PCINT4;

	dht_pin_output(false, false);
}

static void dht_timer_init(void)
{
	TIMSK &= (uint8_t)~((1u << OCIE1A) | (1u << OCIE1B) | (1u << TOIE1));
	TIFR = (1u << OCF1A) | (1u << OCF1B) | (1u << TOV1);

	TCCR1 = 0u;
	TCNT1 = 0u;
	OCR1A = 0u;
	OCR1B = 0u;
	OCR1C = 0u;
	GTCCR &= (uint8_t)~((1u << PWM1B) |
			    (1u << COM1B1) | (1u << COM1B0) |
			    (1u << FOC1B) | (1u << FOC1A) |
			    (1u << PSR1));
	TCCR1 = (1u << CTC1) | (0u << PWM1A) |
		(0u << COM1A1) | (0u << COM1A0) |
		(0u << CS13) | (0u << CS12) | (0u << CS11) | (0u << CS10);
}

static void dht_timer_arm(void)
{
	uint8_t cs = 0u;
	uint8_t ocr = 0u;

	switch (dht.state) {
	case DHT_WAIT:
		/* Idle wait time. */

		/* Prescaler = 16384 */
		cs = (1u << CS13) | (1u << CS12) | (1u << CS11) | (1u << CS10);
		ocr = 98u;
		/* Wait duration = 100 ms * 13 = 1.3 s*/
		dht.timer_count = 13u;
		dht_pin_change_irq_en(false);
		break;
	case DHT_START_OUT_LOW:
		/* The start-out low period is about to start. */

		/* Prescaler = 16 */
		cs = (1u << CS13) | (1u << CS12) | (1u << CS11) | (1u << CS10);
		/* Output low duration = 20 ms */
		ocr = 20u;
		break;
	case DHT_START_OUT_HIGH:
	case DHT_START_IN_LOW:
	case DHT_START_IN_HIGH:
	case DHT_DATA_IN_LOW:
	case DHT_DATA_IN_HIGH:
		/* One of the periods is about to start. */

		/* Prescaler = 16 */
		cs = (0u << CS13) | (1u << CS12) | (0u << CS11) | (1u << CS10);
		/* Timeout = 150 us */
		ocr = 150u;

		if (dht.state == DHT_START_OUT_HIGH) {
			/* Enable pin change interrupt */
			dht_pin_change_irq_en(true);
		}
		break;
	}

	/* Re-program the timer. */
	TCCR1 &= (uint8_t)~((1u << CS13) | (1u << CS12) | (1u << CS11) | (1u << CS10));
	OCR1A = ocr;
	OCR1C = ocr;
	TCNT1 = 0u;
	TIFR = 1u << OCF1A;
	TIMSK |= 1u << OCIE1A;
	TCCR1 |= cs;
}

static void dht_timeout(void)
{
	/* Configure signal pin as input with pullup level = high */
	dht_pin_output(false, false);

	/* Go to initial state */
	dht.state = DHT_WAIT;
	dht_timer_arm();
}

/* Timer interrupt.
 * This interrupt is triggered on timing events or timeouts.
 */
ISR(TIMER1_COMPA_vect)
{
	memory_barrier();

	switch (dht.state) {
	case DHT_WAIT:
		/* Idle wait time. */
		if (--dht.timer_count == 0u) {
			/* End of wait time. */

			/* Configure signal pin as output with level = low */
			dht_pin_output(true, false);

			dht.state = DHT_START_OUT_LOW;
			dht_timer_arm();
		}
		break;
	case DHT_START_OUT_LOW:
		/* The start-out low period just ended. */

		/* Configure signal pin as input with pullup level = high */
		dht_pin_output(false, false);

		dht.state = DHT_START_OUT_HIGH;
		dht_timer_arm();
		break;
	case DHT_START_OUT_HIGH:
	case DHT_START_IN_LOW:
	case DHT_START_IN_HIGH:
	case DHT_DATA_IN_LOW:
	case DHT_DATA_IN_HIGH:
	default:
		/* Timeout waiting for input signal.
		 * The pin change interrupt did not trigger. */
		dht_timeout();
		break;
	}

	memory_barrier();
}

/* Pin change interrupt.
 * This interrupt is triggered on signal pin changes.
 */
ISR(PCINT0_vect)
{
	uint8_t timer, calc_checksum, data_checksum;
	bool pin_state;

	memory_barrier();

	/* First get the signal timer and signal pin states. */
	timer = TCNT1;
	pin_state = !!(DHT_PIN & (1u << DHT_BIT));

	switch (dht.state) {
	default:
	case DHT_WAIT:
	case DHT_START_OUT_LOW:
		/* Error: Invalid state machine state. */
		dht_timeout();
		break;
	case DHT_START_OUT_HIGH:
		/* The start-out high period just ended. */
		if (pin_state) {
			/* Error: Invalid pin state. */
			dht_timeout();
		} else {
			dht.state = DHT_START_IN_LOW;
			dht_timer_arm();
		}
		break;
	case DHT_START_IN_LOW:
		/* The start-in low period just ended. */
		if (pin_state) {
			dht.state = DHT_START_IN_HIGH;
			dht_timer_arm();
		} else {
			/* Error: Invalid pin state. */
			dht_timeout();
		}
		break;
	case DHT_START_IN_HIGH:
		/* The start-in high period just ended. */
		if (pin_state) {
			/* Error: Invalid pin state. */
			dht_timeout();
		} else {
			/* Prepare state for the first bit. */
			dht.byte_count = 0u;
			dht.bit_mask = 0x80u;
			dht.finished = false;

			dht.state = DHT_DATA_IN_LOW;
			dht_timer_arm();
		}
		break;
	case DHT_DATA_IN_LOW:
		/* The data-in low period just ended. */
		if (pin_state) {
			if (dht.finished) {
				dht.state = DHT_WAIT;

				/* We are done. Calculate and compare the checksum.
				 * If it matches, call the user callback. */
				calc_checksum = dht_checksum(dht.data);
				data_checksum = dht.data[DHT_DATA_LEN - 1u];
				if (calc_checksum == data_checksum)
					dht.callback(dht.data);
			} else
				dht.state = DHT_DATA_IN_HIGH;
			dht_timer_arm();
		} else {
			/* Error: Invalid pin state. */
			dht_timeout();
		}
		break;
	case DHT_DATA_IN_HIGH:
		/* The data-in high period just ended. */
		if (pin_state) {
			/* Error: Invalid pin state. */
			dht_timeout();
		} else {
			/* If the period was a long one, we have a 1 bit */
			if (timer >= 48u) {
				dht.data[dht.byte_count] |= dht.bit_mask;
			} else {
				dht.data[dht.byte_count] &= (uint8_t)~dht.bit_mask;
			}

			/* Shift mask to next bit. */
			dht.bit_mask >>= 1;
			if (dht.bit_mask == 0u) {
				dht.bit_mask = 0x80u;
				if (++dht.byte_count >= sizeof(dht.data)) {
					dht.byte_count = 0u;
					dht.finished = true;
				}
			}

			/* State = next bit */
			dht.state = DHT_DATA_IN_LOW;
			dht_timer_arm();
		}
		break;
	}

	memory_barrier();
}

void dht_start(dht_callback_t callback)
{
	if (dht.state == DHT_WAIT) {
		dht.callback = callback;
		memory_barrier();
		dht_timer_arm();
	}
}

void dht_init(void)
{
	memset(&dht, 0, sizeof(dht));
	dht.state = DHT_WAIT;

	dht_pin_init();
	dht_timer_init();
}
bues.ch cgit interface