Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DhtSensor.cpp « Sensors « Heating « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15da14bb12121fa34a26279e3878c14ec28c2a11 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * DhtSensor.cpp
 *
 *  Created on: 15 Sep 2017
 *      Author: Christian
 */

#include "DhtSensor.h"
#include "Platform.h"
#include "RepRap.h"
#include "GCodes/GCodeBuffer.h"

#if SUPPORT_DHT_SENSOR

constexpr uint32_t MinimumReadInterval = 2000;		// ms
constexpr uint32_t MaximumReadTime = 20;			// ms
constexpr uint32_t MinimumOneBitLength = 50;		// microseconds
constexpr uint32_t MinimumOneBitStepClocks = (StepClockRate * MinimumOneBitLength)/1000000;

# include "Tasks.h"

// Static data members of class DhtSensorHardwareInterface
Mutex DhtSensorHardwareInterface::dhtMutex;
Task<DhtSensorHardwareInterface::DhtTaskStackWords> *DhtSensorHardwareInterface::dhtTask = nullptr;
DhtSensorHardwareInterface *DhtSensorHardwareInterface::activeSensors[MaxSpiTempSensors] = { 0 };

extern "C" void DhtTask(void * pvParameters)
{
	DhtSensorHardwareInterface::SensorTask();
}

// Pulse ISR
extern "C" void DhtDataTransition(CallbackParameter cp)
{
	static_cast<DhtSensorHardwareInterface*>(cp.vp)->Interrupt();
}

DhtSensorHardwareInterface::DhtSensorHardwareInterface(Pin p_pin)
	: sensorPin(p_pin), type(DhtSensorType::none), lastResult(TemperatureError::notInitialised),
	  lastTemperature(BAD_ERROR_TEMPERATURE), lastHumidity(BAD_ERROR_TEMPERATURE), badTemperatureCount(0)
{
	IoPort::SetPinMode(sensorPin, INPUT_PULLUP);
}

TemperatureError DhtSensorHardwareInterface::GetTemperatureOrHumidity(float& t, bool wantHumidity) const
{
	t = (wantHumidity) ? lastHumidity : lastTemperature;
	return lastResult;
}

/*static*/ GCodeResult DhtSensorHardwareInterface::Configure(TemperatureSensor *ts, unsigned int relativeChannel, unsigned int mCode, unsigned int heater, GCodeBuffer& gb, const StringRef& reply)
{
	MutexLocker lock(dhtMutex);

	if (relativeChannel >= MaxSpiTempSensors || activeSensors[relativeChannel] == nullptr)
	{
		reply.copy("invalid channel");
		return GCodeResult::error;
	}

	return activeSensors[relativeChannel]->Configure(ts, mCode, heater, gb, reply);
}

GCodeResult DhtSensorHardwareInterface::Configure(TemperatureSensor *ts, unsigned int mCode, unsigned int heater, GCodeBuffer& gb, const StringRef& reply)
{
	GCodeResult rslt = GCodeResult::ok;
	if (mCode == 305)
	{
		bool seen = false;
		ts->TryConfigureHeaterName(gb, seen);

		if (gb.Seen('T'))
		{
			seen = true;

			const int dhtType = gb.GetIValue();
			switch (dhtType)
			{
			case 11:
				type = DhtSensorType::Dht11;
				break;
			case 21:
				type = DhtSensorType::Dht21;
				break;
			case 22:
				type = DhtSensorType::Dht22;
				break;
			default:
				reply.copy("Invalid DHT sensor type");
				rslt = GCodeResult::error;
				break;
			}
		}

		if (!seen && !gb.Seen('X'))
		{
			ts->CopyBasicHeaterDetails(heater, reply);

			const char *sensorTypeString;
			switch (type)
			{
			case DhtSensorType::Dht11:
				sensorTypeString = "DHT11";
				break;
			case DhtSensorType::Dht21:
				sensorTypeString = "DHT21";
				break;
			case DhtSensorType::Dht22:
				sensorTypeString = "DHT22";
				break;
			default:
				sensorTypeString = "unknown";
				break;
			}
			reply.catf(", sensor type %s", sensorTypeString);
		}
	}
	return rslt;
}

// Create a hardware interface object for the specified channel if there isn't already
DhtSensorHardwareInterface *DhtSensorHardwareInterface::Create(unsigned int relativeChannel)
{
	if (relativeChannel >= MaxSpiTempSensors)
	{
		return nullptr;
	}

	MutexLocker lock(dhtMutex);

	if (activeSensors[relativeChannel] == nullptr)
	{
		activeSensors[relativeChannel] = new DhtSensorHardwareInterface(SpiTempSensorCsPins[relativeChannel]);
	}

	if (dhtTask == nullptr)
	{
		dhtTask = new Task<DhtTaskStackWords>;
		dhtTask->Create(DhtTask, "DHTSENSOR", nullptr, TaskBase::HeatPriority);
	}

	return activeSensors[relativeChannel];
}

/*static*/ void DhtSensorHardwareInterface::InitStatic()
{
	dhtMutex.Create();
}

/*static*/ TemperatureError DhtSensorHardwareInterface::GetTemperatureOrHumidity(unsigned int relativeChannel, float& t, bool wantHumidity)
{
	if (relativeChannel >= MaxSpiTempSensors)
	{
		t = BAD_ERROR_TEMPERATURE;
		return TemperatureError::unknownChannel;
	}

	MutexLocker lock(dhtMutex);

	if (activeSensors[relativeChannel] == nullptr)
	{
		t = BAD_ERROR_TEMPERATURE;
		return TemperatureError::notInitialised;
	}

	return activeSensors[relativeChannel]->GetTemperatureOrHumidity(t, wantHumidity);
}

void DhtSensorHardwareInterface::Interrupt()
{
	if (numPulses < ARRAY_SIZE(pulses))
	{
		const uint16_t now = Platform::GetInterruptClocks16();
		if (IoPort::ReadPin(sensorPin))
		{
			lastPulseTime = now;
		}
		else if (lastPulseTime != 0)
		{
			pulses[numPulses++] = now - lastPulseTime;
		}
	}
}

void DhtSensorHardwareInterface::TakeReading()
{
	if (type != DhtSensorType::none)			// if sensor has been configured
	{
		// Send the start bit. This must be at least 18ms for the DHT11, 0.8ms for the DHT21, and 1ms long for the DHT22.
		IoPort::SetPinMode(sensorPin, OUTPUT_LOW);
		delay(20);

		{
			TaskCriticalSectionLocker lock;		// make sure the Heat task doesn't interrupt the sequence

			// End the start signal by setting data line high. the sensor will respond with the start bit in 20 to 40us.
			// We need only force the data line high long enough to charge the line capacitance, after that the pullup resistor keeps it high.
			IoPort::WriteDigital(sensorPin, HIGH);		// this will generate an interrupt, but we will ignore it
			delayMicroseconds(3);

			// Now start reading the data line to get the value from the DHT sensor
			IoPort::SetPinMode(sensorPin, INPUT_PULLUP);

			// It appears that switching the pin to an output disables the interrupt, so we need to call attachInterrupt here
			// We are likely to get an immediate interrupt at this point corresponding to the low-to-high transition. We must ignore this.
			numPulses = ARRAY_SIZE(pulses);		// tell the ISR not to collect data yet
			attachInterrupt(sensorPin, DhtDataTransition, INTERRUPT_MODE_CHANGE, this);
			lastPulseTime = 0;
			numPulses = 0;						// tell the ISR to collect data
		}

		// Wait for the incoming signal to be read by the ISR (1 start bit + 40 data bits), or until timeout.
		// We don't have the ISR wake the process up, because that would require the priority of the pin change interrupt to be reduced.
		// So we just delay for long enough for the data to have been sent. It takes typically 4 to 5ms.
		delay(MaximumReadTime);

		detachInterrupt(sensorPin);

		// Attempt to convert the signal into temp+RH values
		const TemperatureError rslt = ProcessReadings();
		if (rslt == TemperatureError::success)
		{
			lastResult = rslt;
			badTemperatureCount = 0;
		}
		else if (badTemperatureCount < MAX_BAD_TEMPERATURE_COUNT)
		{
			badTemperatureCount++;
		}
		else
		{
			lastResult = rslt;
			lastTemperature = BAD_ERROR_TEMPERATURE;
			lastHumidity = BAD_ERROR_TEMPERATURE;
		}
	}
}

// Code executed by the DHT sensor task.
// This is run at the same priority as the Heat task, so it must not sit in any spin loops.
/*static*/ void DhtSensorHardwareInterface::SensorTask()
{
	for (;;)
	{
		for (DhtSensorHardwareInterface *&sensor : activeSensors)
		{
			{
				MutexLocker lock(dhtMutex);

				if (sensor != nullptr)
				{
					sensor->TakeReading();
				}
			}
			delay(MinimumReadInterval/MaxSpiTempSensors);
		}
	}
}

// Process a reading. If success then update the temperature and humidity and return TemperatureError::success.
// Else return the TemperatureError code but do not update the readings.
TemperatureError DhtSensorHardwareInterface::ProcessReadings()
{
	// Check enough bits received and check start bit
	if (numPulses != ARRAY_SIZE(pulses) || pulses[0] < MinimumOneBitStepClocks)
	{
//		debugPrintf("pulses %u p0 %u\n", numPulses, (unsigned int)pulses[0]);
		return TemperatureError::ioError;
	}

	// Reset 40 bits of received data to zero
	uint8_t data[5] = { 0, 0, 0, 0, 0 };

	// Inspect each high pulse and determine which ones are 0 (less than 50us) or 1 (more than 50us). Ignore the start bit.
	for (size_t i = 0; i < 40; ++i)
	{
		data[i / 8] <<= 1;
		if (pulses[i + 1] >= MinimumOneBitStepClocks)
		{
			data[i / 8] |= 1;
		}
	}

//	debugPrintf("Data: %02x %02x %02x %02x %02x\n", data[0], data[1], data[2], data[3], data[4]);
	// Verify checksum
	if (((data[0] + data[1] + data[2] + data[3]) & 0xFF) != data[4])
	{
//		debugPrintf("Cks err\n");
		return TemperatureError::ioError;
	}

	// Generate final results
	switch (type)
	{
	case DhtSensorType::Dht11:
		lastHumidity = data[0];
		lastTemperature = data[2];
		return TemperatureError::success;

	case DhtSensorType::Dht21:
	case DhtSensorType::Dht22:
		lastHumidity = ((data[0] * 256) + data[1]) * 0.1;
		lastTemperature = (((data[2] & 0x7F) * 256) + data[3]) * 0.1;
		if (data[2] & 0x80)
		{
			lastTemperature *= -1.0;
		}
		return TemperatureError::success;

	default:
		return TemperatureError::notInitialised;
	}
}

// Class DhtTemperatureSensor members
DhtTemperatureSensor::DhtTemperatureSensor(unsigned int channel)
	: TemperatureSensor(channel, "DHT-temperature")
{
}

void DhtTemperatureSensor::Init()
{
	DhtSensorHardwareInterface::Create(GetSensorChannel() - FirstDhtTemperatureChannel);
}

GCodeResult DhtTemperatureSensor::Configure(unsigned int mCode, unsigned int heater, GCodeBuffer& gb, const StringRef& reply)
{
	return DhtSensorHardwareInterface::Configure(this, GetSensorChannel() - FirstDhtTemperatureChannel, mCode, heater, gb, reply);
}

DhtTemperatureSensor::~DhtTemperatureSensor()
{
	// We don't delete the hardware interface object because the humidity channel may still be using it
}

TemperatureError DhtTemperatureSensor::GetTemperature(float& t)
{
	return DhtSensorHardwareInterface::GetTemperatureOrHumidity(GetSensorChannel() - FirstDhtTemperatureChannel, t, false);
}

// Class DhtHumiditySensor members
DhtHumiditySensor::DhtHumiditySensor(unsigned int channel)
	: TemperatureSensor(channel, "DHT-humidity")
{
}

void DhtHumiditySensor::Init()
{
	DhtSensorHardwareInterface::Create(GetSensorChannel() - FirstDhtHumidityChannel);
}

GCodeResult DhtHumiditySensor::Configure(unsigned int mCode, unsigned int heater, GCodeBuffer& gb, const StringRef& reply)
{
	return DhtSensorHardwareInterface::Configure(this, GetSensorChannel() - FirstDhtHumidityChannel, mCode, heater, gb, reply);
}

DhtHumiditySensor::~DhtHumiditySensor()
{
	// We don't delete the hardware interface object because the temperature channel may still be using it
}

TemperatureError DhtHumiditySensor::GetTemperature(float& t)
{
	return DhtSensorHardwareInterface::GetTemperatureOrHumidity(GetSensorChannel() - FirstDhtHumidityChannel, t, true);
}

#endif

// End