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

CurrentLoopTemperatureSensor.cpp « Sensors « Heating « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9932cf8d69f06fb45bcc820417e6e2dab96a1544 (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
/*
 * LinearAdcTemperatureSensor.cpp
 *
 *  Created on: 8 Jun 2017
 *      Author: David
 */

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

const uint32_t MCP3204_Frequency = 1000000;		// maximum for MCP3204 is 1MHz @ 2.7V, will be slightly higher at 3.3V

// The MCP3204 samples input data on the rising edge and changes the output data on the rising edge.
const uint8_t MCP3204_SpiMode = SPI_MODE_0;

// Define the minimum interval between readings
const uint32_t MinimumReadInterval = 100;		// minimum interval between reads, in milliseconds

CurrentLoopTemperatureSensor::CurrentLoopTemperatureSensor(unsigned int sensorNum) noexcept
	: SpiTemperatureSensor(sensorNum, "Current Loop", MCP3204_SpiMode, MCP3204_Frequency),
	  tempAt4mA(DefaultTempAt4mA), tempAt20mA(DefaultTempAt20mA), chipChannel(DefaultChipChannel), isDifferential(false)
{
	CalcDerivedParameters();
}

// Configure this temperature sensor
GCodeResult CurrentLoopTemperatureSensor::Configure(GCodeBuffer& gb, const StringRef& reply)
{
	bool seen = false;
	if (!ConfigurePort(gb, reply, seen))
	{
		return GCodeResult::error;
	}

	gb.TryGetFValue('L', tempAt4mA, seen);
	gb.TryGetFValue('H', tempAt20mA, seen);
	gb.TryGetUIValue('C', chipChannel, seen);
	gb.TryGetUIValue('D', isDifferential, seen);
	TryConfigureSensorName(gb, seen);

	if (seen)
	{
		CalcDerivedParameters();

		// Initialise the sensor
		InitSpi();

		TemperatureError rslt;
		float t;
		for (unsigned int i = 0; i < 3; ++i)		// try 3 times
		{
			rslt = TryGetLinearAdcTemperature(t);
			if (lastResult == TemperatureError::success)
			{
				break;
			}
			delay(MinimumReadInterval);
		}

		SetResult(t, rslt);

		if (rslt != TemperatureError::success)
		{
			reprap.GetPlatform().MessageF(ErrorMessage, "Failed to initialise daughter board ADC: %s\n", TemperatureErrorString(rslt));
		}
	}
	else
	{
		CopyBasicDetails(reply);
		reply.catf(", temperature range %.1f to %.1fC", (double)tempAt4mA, (double)tempAt20mA);
	}
	return GCodeResult::ok;
}

void CurrentLoopTemperatureSensor::Poll() noexcept
{
	float t;
	const TemperatureError rslt = TryGetLinearAdcTemperature(t);
	SetResult(t, rslt);
}

void CurrentLoopTemperatureSensor::CalcDerivedParameters() noexcept
{
	minLinearAdcTemp = tempAt4mA - 0.25 * (tempAt20mA - tempAt4mA);
	linearAdcDegCPerCount = (tempAt20mA - minLinearAdcTemp) / 4096.0;
}

// Try to get a temperature reading from the linear ADC by doing an SPI transaction
TemperatureError CurrentLoopTemperatureSensor::TryGetLinearAdcTemperature(float& t) noexcept
{
	/*
	 * The MCP3204 waits for a high input input bit before it does anything. Call this clock 1.
	 * The next input bit it high for single-ended operation, low for differential. This is clock 2.
	 * The next 3 input bits are the channel selection bits. These are clocks 3..5.
	 * Clock 6 produces a null bit on its trailing edge, which is read by the processor on clock 7.
	 * Clocks 7..18 produce data bits B11..B0 on their trailing edges, which are read by the MCU on the leading edges of clocks 8-19.
	 * If we supply further clocks, then clocks 18..29 are the same data but LSB first, omitting bit 0.
	 * Clocks 30 onwards will be zeros.
	 * So we need to use at least 19 clocks. We round this up to 24 clocks, and we check that the extra 5 bits we receive are the 5 least significant data bits in reverse order.
	 *
	 * MCP3204 & MCP3208
	 * Single CH0 "C0" - Differential CH0-CH1 "80"
	 * Single CH1 "C8" - Differential CH1-CH0 "88"
	 * Single CH2 "D0" - Differential CH2-CH3 "90"
	 * Single CH3 "D8" - Differential CH3-CH2 "98"
	 * MCP3208 Only
	 * Single CH4 "E0" - Differential CH4-CH5 "A0"
	 * Single CH5 "E8" - Differential CH5-CH4 "A8"
	 * Single CH6 "F0" - Differential CH6-CH7 "B0"
	 * Single CH7 "F8" - Differential CH7-CH6 "B8"
	 *
	 * These values represent clocks 1 to 5.
	 */

	const uint8_t channelByte = ((isDifferential) ? 0x80 : 0xC0) | (chipChannel * 0x08);
	static const uint8_t adcData[] = { channelByte, 0x00, 0x00 };
	uint32_t rawVal;
	TemperatureError rslt = DoSpiTransaction(adcData, 3, rawVal);
	//debugPrintf("ADC data %u\n", rawVal);

	if (rslt == TemperatureError::success)
	{
		const uint32_t adcVal1 = (rawVal >> 5) & ((1 << 13) - 1);
		const uint32_t adcVal2 = ((rawVal & 1) << 5) | ((rawVal & 2) << 3) | ((rawVal & 4) << 1) | ((rawVal & 8) >> 1) | ((rawVal & 16) >> 3) | ((rawVal & 32) >> 5);
		if (adcVal1 >= 4096 || adcVal2 != (adcVal1 & ((1 << 6) - 1)))
		{
			rslt = TemperatureError::badResponse;
		}
		else
		{
			t = minLinearAdcTemp + (linearAdcDegCPerCount * (float)adcVal1);
		}
	}
	return rslt;
}

// End