3 #include <avr/pgmspace.h>
6 #include "mmu2/error_codes.h"
7 #include "mmu2/progress_codes.h"
8 #include "mmu2/buttons.h"
9 #include "mmu2/registers.h"
10 #include "mmu2_protocol.h"
14 template <
typename T, u
int8_t N>
20 inline constexpr T *begin()
const {
return data; }
21 inline constexpr T *end()
const {
return data + N; }
22 static constexpr uint8_t size() {
return N; }
23 inline T &operator[](uint8_t i) {
31 #include "../../../../../../Prusa-Firmware-MMU/src/logic/error_codes.h"
32 #include "../../../../../../Prusa-Firmware-MMU/src/logic/progress_codes.h"
36 #include "../../../../../../Prusa-Firmware-MMU/src/modules/protocol.h"
38 #include "registers.h"
41 #include "mmu2_serial.h"
46 static constexpr uint8_t MAX_RETRIES = 3U;
64 CommunicationRecovered,
77 static constexpr uint8_t maxOccurrences = 10;
78 static_assert(maxOccurrences > 1,
"we should really silently ignore at least 1 comm drop out if recovered immediately afterwards");
88 inline void Reset() { occurrences = maxOccurrences; }
92 uint8_t occurrences = maxOccurrences;
107 void ToolChange(uint8_t slot);
109 void UnloadFilament();
110 void LoadFilament(uint8_t slot);
111 void EjectFilament(uint8_t slot);
112 void CutFilament(uint8_t slot);
113 void ResetMMU(uint8_t mode = 0);
114 void Button(uint8_t index);
115 void Home(uint8_t mode);
116 void ReadRegister(uint8_t address);
117 void WriteRegister(uint8_t address, uint16_t data);
123 initRegs8[0] = eld_mm;
145 ErrorCode
Error()
const {
return errorCode; }
148 ProgressCode
Progress()
const {
return progressCode; }
151 Buttons
Button()
const {
return buttonCode; }
153 uint8_t CommandInProgress()
const;
155 inline bool Running()
const {
156 return state == State::Running;
159 inline bool FindaPressed()
const {
163 inline uint16_t FailStatistics()
const {
167 inline uint8_t MmuFwVersionMajor()
const {
168 return mmuFwVersion[0];
171 inline uint8_t MmuFwVersionMinor()
const {
172 return mmuFwVersion[1];
175 inline uint8_t MmuFwVersionRevision()
const {
176 return mmuFwVersion[2];
184 void DecrementRetryAttempts();
187 void ResetRetryAttempts();
189 void ResetCommunicationTimeoutAttempts();
191 constexpr
bool InAutoRetry()
const {
return inAutoRetry; }
192 void SetInAutoRetry(
bool iar) {
196 inline void SetPrinterError(ErrorCode ec) {
197 explicitPrinterError = ec;
199 inline void ClearPrinterError() {
200 explicitPrinterError = ErrorCode::OK;
202 inline bool IsPrinterError()
const {
203 return explicitPrinterError != ErrorCode::OK;
206 return explicitPrinterError;
218 bool Elapsed(uint32_t timeout)
const;
219 void RecordUARTActivity();
220 void RecordReceivedByte(uint8_t c);
221 void FormatLastReceivedBytes(
char *dst);
222 void FormatLastResponseMsgAndClearLRB(
char *dst);
223 void LogRequestMsg(
const uint8_t *txbuff, uint8_t size);
224 void LogError(
const char *reason_P);
227 void SwitchFromStartToIdle();
229 ErrorCode explicitPrinterError;
231 enum class State : uint_fast8_t {
237 enum class Scope : uint_fast8_t {
248 bool ExpectsResponse()
const {
return ((uint8_t)scopeState & (uint8_t)ScopeState::NotExpectsResponse) == 0; }
252 enum class ScopeState : uint_fast8_t {
259 FilamentSensorStateSent,
260 Reading8bitRegisters,
261 Reading16bitRegisters,
262 WritingInitRegisters,
268 NotExpectsResponse = 0x80,
269 Wait = NotExpectsResponse + 1,
270 Ready = NotExpectsResponse + 2,
271 RecoveringProtocolError = NotExpectsResponse + 3,
274 ScopeState scopeState;
289 void CheckAndReportAsyncEvents();
291 void StartReading8bitRegisters();
292 void ProcessRead8bitRegister();
293 void StartReading16bitRegisters();
294 ScopeState ProcessRead16bitRegister(ProtocolLogic::ScopeState stateAtEnd);
295 void StartWritingInitRegisters();
297 bool ProcessWritingInitRegister();
298 void SendAndUpdateFilamentSensor();
299 void SendButton(uint8_t btn);
300 void SendVersion(uint8_t stage);
301 void SendReadRegister(uint8_t index, ScopeState nextState);
302 void SendWriteRegister(uint8_t index, uint16_t value, ScopeState nextState);
304 StepStatus ProcessVersionResponse(uint8_t stage);
309 static constexpr uint8_t maxRetries = 6;
312 void StartSeqRestart();
313 void DelayedRestartRestart();
315 void CommandRestart();
323 StepStatus StoppedStep() {
return Processing; }
330 inline const RequestMsg &ReqMsg()
const {
return rq; }
348 bool ActivatePlannedRequest();
350 uint32_t lastUARTActivityMs;
351 DropOutFilter dataTO;
365 ProgressCode progressCode;
376 static constexpr uint8_t regs8Count = 3;
377 static_assert(regs8Count > 0);
378 static const Register regs8Addrs[regs8Count] PROGMEM;
379 uint8_t regs8[regs8Count] = { 0, 0, 0 };
382 static constexpr uint8_t regs16Count = 2;
383 static_assert(regs16Count > 0);
384 static const Register regs16Addrs[regs16Count] PROGMEM;
385 uint16_t regs16[regs16Count] = { 0, 0 };
388 static constexpr uint8_t initRegs8Count = 2;
389 static_assert(initRegs8Count > 0);
390 static const Register initRegs8Addrs[initRegs8Count] PROGMEM;
391 uint8_t initRegs8[initRegs8Count];
395 uint8_t mmuFwVersion[3] = { 0, 0, 0 };
396 uint16_t mmuFwVersionBuild;
398 uint8_t retryAttempts;
Filter of short consecutive drop outs which are recovered instantly.
Definition: mmu2_protocol_logic.h:75
void Reset()
Rearms the object for further processing - basically call this once the MMU responds with something m...
Definition: mmu2_protocol_logic.h:88
StepStatus InitialCause() const
Definition: mmu2_protocol_logic.h:85
A minimal serial interface for the MMU.
Definition: mmu2_serial.h:8
Logic layer of the MMU vs. printer communication protocol.
Definition: mmu2_protocol_logic.h:96
ErrorCode Error() const
Definition: mmu2_protocol_logic.h:145
void PlanExtraLoadDistance(uint8_t eld_mm)
Definition: mmu2_protocol_logic.h:122
void PlanPulleySlowFeedRate(uint8_t psfr)
Definition: mmu2_protocol_logic.h:133
uint8_t ExtraLoadDistance() const
Definition: mmu2_protocol_logic.h:126
uint8_t PulleySlowFeedRate() const
Definition: mmu2_protocol_logic.h:137
constexpr uint8_t RetryAttempts() const
Current number of retry attempts left.
Definition: mmu2_protocol_logic.h:180
Buttons Button() const
Definition: mmu2_protocol_logic.h:151
ProgressCode Progress() const
Definition: mmu2_protocol_logic.h:148
Definition: mmu2_protocol.h:133
static constexpr uint8_t MaxRequestSize()
Definition: mmu2_protocol.h:162
Definition: mmu2_protocol_logic.h:15
New MMU2 protocol logic.
Definition: mmu2.cpp:26
constexpr uint32_t dataLayerTimeout
data layer communication timeout
Definition: mmu2_protocol_logic.h:69
constexpr uint32_t linkLayerTimeout
default link layer communication timeout
Definition: mmu2_protocol_logic.h:68
StepStatus
ProtocolLogic stepping statuses.
Definition: mmu2_protocol_logic.h:53
@ VersionMismatch
the MMU reports its firmware version incompatible with our implementation
Definition: mmu2_protocol_logic.h:62
@ Finished
Scope finished successfully.
Definition: mmu2_protocol_logic.h:56
@ ProtocolError
bytes read from the MMU didn't form a valid response
Definition: mmu2_protocol_logic.h:59
@ Interrupted
received "Finished" message related to a different command than originally issued (most likely the MM...
Definition: mmu2_protocol_logic.h:57
@ ButtonPushed
The MMU reported the user pushed one of its three buttons.
Definition: mmu2_protocol_logic.h:65
@ CommandRejected
the MMU rejected the command due to some other command in progress, may be the user is operating the ...
Definition: mmu2_protocol_logic.h:60
@ PrinterError
printer's explicit error - MMU is fine, but the printer was unable to complete the requested operatio...
Definition: mmu2_protocol_logic.h:63
@ MessageReady
a message has been successfully decoded from the received bytes
Definition: mmu2_protocol_logic.h:55
@ CommunicationTimeout
the MMU failed to respond to a request within a specified time frame
Definition: mmu2_protocol_logic.h:58
@ CommandError
the command in progress stopped due to unrecoverable error, user interaction required
Definition: mmu2_protocol_logic.h:61
constexpr uint32_t heartBeatPeriod
period of heart beat messages (Q0)
Definition: mmu2_protocol_logic.h:70
The MMU communication protocol implementation and related stuff.
Definition: mmu2_protocol.cpp:15
A request message - requests are being sent by the printer into the MMU.
Definition: mmu2_protocol.h:46
A response message - responses are being sent from the MMU into the printer as a response to a reques...
Definition: mmu2_protocol.h:88