blob: 7bce6644466bc7effb3a1c719d5cb3624e171d65 (
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
|
/*
* Duet3DFilamentSensor.cpp
*
* Created on: 20 Jul 2017
* Author: David
*/
#include "Duet3DFilamentMonitor.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"
#include "Platform.h"
#include "Movement/StepTimer.h"
#include "RepRap.h"
// Constructors
Duet3DFilamentMonitor::Duet3DFilamentMonitor(unsigned int extruder, unsigned int type) noexcept
: FilamentMonitor(extruder, type), overrunErrorCount(0), polarityErrorCount(0)
{
InitReceiveBuffer();
}
void Duet3DFilamentMonitor::InitReceiveBuffer() noexcept
{
edgeCaptureReadPointer = edgeCaptureWritePointer = 1;
edgeCaptures[0] = StepTimer::GetTimerTicks(); // pretend we just had a high-to-low transition
state = RxdState::waitingForStartBit;
}
// ISR for when the pin state changes. It should return true if the ISR wants the commanded extrusion to be fetched.
bool Duet3DFilamentMonitor::Interrupt() noexcept
{
uint32_t now = StepTimer::GetTimerTicks();
bool wantReading = false;
const size_t writePointer = edgeCaptureWritePointer; // capture volatile variable
if ((writePointer + 1) % EdgeCaptureBufferSize != edgeCaptureReadPointer) // if buffer is not full
{
if (GetPort().Read())
{
if ((writePointer & 1) == 0) // low-to-high transitions should occur on odd indices
{
++polarityErrorCount;
return false;
}
if (state == RxdState::waitingForStartBit && writePointer == edgeCaptureReadPointer && !HaveIsrStepsCommanded())
{
wantReading = true; // if this is a possible start bit, ask for the extrusion commanded
}
}
else
{
if ((writePointer & 1) != 0) // high-to-low transitions should occur on even indices
{
++polarityErrorCount;
return false;
}
now -= 40; // partial correction for skew caused by debounce filter on older Duet endstop inputs (measured skew = 74)
}
edgeCaptures[writePointer] = now; // record the time at which this edge was detected
edgeCaptureWritePointer = (writePointer + 1) % EdgeCaptureBufferSize;
}
else
{
++overrunErrorCount;
}
return wantReading;
}
// Call the following regularly to keep the status up to date
Duet3DFilamentMonitor::PollResult Duet3DFilamentMonitor::PollReceiveBuffer(uint16_t& measurement) noexcept
{
// For the Duet3D sensors we need to decode the received data from the transition times recorded in the edgeCaptures array
static constexpr uint32_t BitsPerSecond = 1000; // the nominal bit rate that the data is transmitted at
static constexpr uint32_t NominalBitLength = StepTimer::StepClockRate/BitsPerSecond; // the nominal bit length in step clocks
static constexpr uint32_t MinBitLength = (NominalBitLength * 10)/13; // allow 30% clock speed tolerance
static constexpr uint32_t MaxBitLength = (NominalBitLength * 13)/10; // allow 30% clock speed tolerance
static constexpr uint32_t ErrorRecoveryDelayBits = 8; // before a start bit we want the line to be low for this long
static constexpr uint32_t ErrorRecoveryTime = NominalBitLength * ErrorRecoveryDelayBits;
bool again;
do
{
again = false;
const size_t writePointer = edgeCaptureWritePointer; // capture volatile variable
const uint32_t now = StepTimer::GetTimerTicks();
switch (state)
{
case RxdState::waitingForStartBit:
if (writePointer != edgeCaptureReadPointer) // if we have recorded any new edges
{
if ((edgeCaptureReadPointer & 1u) == 0) // if we are out of sync (this is normal when the last stuffing bit was a 1)
{
edgeCaptureReadPointer = (edgeCaptureReadPointer + 1u) % EdgeCaptureBufferSize;
again = true;
}
else
{
if (edgeCaptures[edgeCaptureReadPointer] - edgeCaptures[(edgeCaptureReadPointer - 1) % EdgeCaptureBufferSize] < ErrorRecoveryTime)
{
// The input line has not been idle for long enough before the start bit
edgeCaptureReadPointer = (edgeCaptureReadPointer + 1u) % EdgeCaptureBufferSize; // ignore this start bit
state = RxdState::errorRecovery1;
again = true;
}
else
{
state = RxdState::waitingForEndOfStartBit;
again = true;
}
}
}
break;
case RxdState::waitingForEndOfStartBit:
// This state must time out because while we are in it, comparison of filament extruded is suspended
if ((writePointer - edgeCaptureReadPointer) % EdgeCaptureBufferSize >= 2)
{
// Check for a valid start bit
lastBitChangeIndex = (edgeCaptureReadPointer + 1u) % EdgeCaptureBufferSize;
startBitLength = edgeCaptures[lastBitChangeIndex] - edgeCaptures[edgeCaptureReadPointer];
edgeCaptureReadPointer = lastBitChangeIndex;
if (startBitLength >= MinBitLength && startBitLength <= MaxBitLength)
{
valueBeingAssembled = 0;
nibblesAssembled = 0;
state = RxdState::waitingForNibble;
again = true;
}
else
{
// Start bit too long or too short
state = RxdState::errorRecovery2;
again = true;
}
}
else if (now - edgeCaptures[edgeCaptureReadPointer] > MaxBitLength) // check for timeout
{
edgeCaptureReadPointer = (edgeCaptureReadPointer + 1u) % EdgeCaptureBufferSize;
state = RxdState::errorRecovery2;
again = true;
}
break;
case RxdState::waitingForNibble:
// This state must time out because while we are in it, comparison of filament extruded is suspended
{
const uint32_t nibbleStartTime = edgeCaptures[lastBitChangeIndex];
if (now - nibbleStartTime > (13 * startBitLength)/2)
{
// 6.5 bit times have passed since the start of the bit that preceded the current nibble, so we should have a complete nibble and the following stuffing bit
uint32_t samplePoint = (startBitLength * 3)/2; // sampling time after the end of the start bit for bit 7 (MSB)
uint8_t currentNibble = 0;
size_t nextBitChangeIndex = (lastBitChangeIndex + 1u) % EdgeCaptureBufferSize;
for (uint8_t numBits = 0; numBits < 5; ++numBits)
{
if (nextBitChangeIndex != edgeCaptureWritePointer && edgeCaptures[nextBitChangeIndex] - nibbleStartTime < samplePoint)
{
lastBitChangeIndex = nextBitChangeIndex;
nextBitChangeIndex = (lastBitChangeIndex + 1u) % EdgeCaptureBufferSize;
if (nextBitChangeIndex != writePointer && edgeCaptures[nextBitChangeIndex] - nibbleStartTime < samplePoint)
{
// We recorded two edges within one sample period
edgeCaptureReadPointer = nextBitChangeIndex;
state = RxdState::errorRecovery3;
again = true;
break;
}
}
currentNibble <<= 1;
currentNibble |= (uint8_t)(lastBitChangeIndex & 1u);
samplePoint += startBitLength;
}
if (state != RxdState::waitingForNibble)
{
break;
}
// The 5th bit we received should be the inverse of the 4th bit
if ((((currentNibble >> 1u) ^ currentNibble) & 0x01u) == 0)
{
edgeCaptureReadPointer = nextBitChangeIndex;
state = RxdState::errorRecovery4;
again = true;
break;
}
currentNibble >>= 1;
valueBeingAssembled = (valueBeingAssembled << 4) | currentNibble;
++nibblesAssembled;
if (nibblesAssembled == 4) // if we have a complete 16-bit word
{
edgeCaptureReadPointer = nextBitChangeIndex; // ready for a new word
measurement = valueBeingAssembled;
state = RxdState::waitingForStartBit;
return PollResult::complete;
}
again = true;
}
}
break;
default: // error recovery states
if (reprap.Debug(moduleFilamentSensors))
{
debugPrintf("Fil err %u\n", (unsigned int)state);
}
state = RxdState::waitingForStartBit;
return PollResult::error;
}
} while (again);
return PollResult::incomplete;
}
// Return true if we are on the process of receiving data form the filament monitor
bool Duet3DFilamentMonitor::IsReceiving() const noexcept
{
return state == RxdState::waitingForEndOfStartBit || state == RxdState::waitingForNibble;
}
// Return true if we are waiting for a start bit
bool Duet3DFilamentMonitor::IsWaitingForStartBit() const noexcept
{
return state == RxdState::waitingForStartBit;
}
// End
|