mmu2_protocol.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /// @file
  2. #include "mmu2_protocol.h"
  3. // protocol definition
  4. // command: Q0
  5. // meaning: query operation status
  6. // Query/command: query
  7. // Expected reply from the MMU:
  8. // any of the running operation statuses: OID: [T|L|U|E|C|W|K][0-4]
  9. // <OID> P[0-9] : command being processed i.e. operation running, may contain a state number
  10. // <OID> E[0-9][0-9] : error 1-9 while doing a tool change
  11. // <OID> F[0-9] : operation finished - will be repeated to "Q" messages until a new command is issued
  12. namespace modules {
  13. namespace protocol {
  14. // decoding automaton
  15. // states: input -> transition into state
  16. // Code QTLMUXPSBEWK -> msgcode
  17. // \n ->start
  18. // * ->error
  19. // error \n ->start
  20. // * ->error
  21. // msgcode 0-9 ->msgvalue
  22. // * ->error
  23. // msgvalue 0-9 ->msgvalue
  24. // \n ->start successfully accepted command
  25. DecodeStatus Protocol::DecodeRequest(uint8_t c) {
  26. switch (rqState) {
  27. case RequestStates::Code:
  28. switch (c) {
  29. case 'Q':
  30. case 'T':
  31. case 'L':
  32. case 'M':
  33. case 'U':
  34. case 'X':
  35. case 'P':
  36. case 'S':
  37. case 'B':
  38. case 'E':
  39. case 'W': // write is gonna be a special one
  40. case 'K':
  41. case 'F':
  42. case 'f':
  43. case 'H':
  44. case 'R':
  45. requestMsg.code = (RequestMsgCodes)c;
  46. requestMsg.value = 0;
  47. requestMsg.value2 = 0;
  48. requestMsg.crc8 = 0;
  49. rqState = (c == 'W') ? RequestStates::Address : RequestStates::Value; // prepare special automaton path for Write commands
  50. return DecodeStatus::NeedMoreData;
  51. default:
  52. requestMsg.code = RequestMsgCodes::unknown;
  53. rqState = RequestStates::Error;
  54. return DecodeStatus::Error;
  55. }
  56. case RequestStates::Value:
  57. if (IsHexDigit(c)) {
  58. requestMsg.value <<= 4U;
  59. requestMsg.value |= Char2Nibble(c);
  60. return DecodeStatus::NeedMoreData;
  61. } else if (IsCRCSeparator(c)) {
  62. rqState = RequestStates::CRC;
  63. return DecodeStatus::NeedMoreData;
  64. } else {
  65. requestMsg.code = RequestMsgCodes::unknown;
  66. rqState = RequestStates::Error;
  67. return DecodeStatus::Error;
  68. }
  69. case RequestStates::Address:
  70. if (IsHexDigit(c)) {
  71. requestMsg.value <<= 4U;
  72. requestMsg.value |= Char2Nibble(c);
  73. return DecodeStatus::NeedMoreData;
  74. } else if (c == ' ') { // end of address, value coming
  75. rqState = RequestStates::WriteValue;
  76. return DecodeStatus::NeedMoreData;
  77. } else {
  78. requestMsg.code = RequestMsgCodes::unknown;
  79. rqState = RequestStates::Error;
  80. return DecodeStatus::Error;
  81. }
  82. case RequestStates::WriteValue:
  83. if (IsHexDigit(c)) {
  84. requestMsg.value2 <<= 4U;
  85. requestMsg.value2 |= Char2Nibble(c);
  86. return DecodeStatus::NeedMoreData;
  87. } else if (IsCRCSeparator(c)) {
  88. rqState = RequestStates::CRC;
  89. return DecodeStatus::NeedMoreData;
  90. } else {
  91. requestMsg.code = RequestMsgCodes::unknown;
  92. rqState = RequestStates::Error;
  93. return DecodeStatus::Error;
  94. }
  95. case RequestStates::CRC:
  96. if (IsHexDigit(c)) {
  97. requestMsg.crc8 <<= 4U;
  98. requestMsg.crc8 |= Char2Nibble(c);
  99. return DecodeStatus::NeedMoreData;
  100. } else if (IsNewLine(c)) {
  101. // check CRC at this spot
  102. if (requestMsg.crc8 != requestMsg.ComputeCRC8()) {
  103. // CRC mismatch
  104. requestMsg.code = RequestMsgCodes::unknown;
  105. rqState = RequestStates::Error;
  106. return DecodeStatus::Error;
  107. } else {
  108. rqState = RequestStates::Code;
  109. return DecodeStatus::MessageCompleted;
  110. }
  111. }
  112. default: //case error:
  113. if (IsNewLine(c)) {
  114. rqState = RequestStates::Code;
  115. return DecodeStatus::MessageCompleted;
  116. } else {
  117. requestMsg.code = RequestMsgCodes::unknown;
  118. rqState = RequestStates::Error;
  119. return DecodeStatus::Error;
  120. }
  121. }
  122. }
  123. uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) {
  124. txbuff[0] = (uint8_t)msg.code;
  125. uint8_t i = 1 + UInt8ToHex(msg.value, txbuff + 1);
  126. i += AppendCRC(msg.CRC(), txbuff + i);
  127. txbuff[i] = '\n';
  128. ++i;
  129. return i;
  130. static_assert(7 <= MaxRequestSize(), "Request message length exceeded the maximum size, increase the magic constant in MaxRequestSize()");
  131. }
  132. uint8_t Protocol::EncodeWriteRequest(uint8_t address, uint16_t value, uint8_t *txbuff) {
  133. const RequestMsg msg(RequestMsgCodes::Write, address, value);
  134. uint8_t i = BeginEncodeRequest(msg, txbuff);
  135. // dump the value
  136. i += UInt16ToHex(value, txbuff + i);
  137. i += AppendCRC(msg.CRC(), txbuff + i);
  138. txbuff[i] = '\n';
  139. ++i;
  140. return i;
  141. }
  142. DecodeStatus Protocol::DecodeResponse(uint8_t c) {
  143. switch (rspState) {
  144. case ResponseStates::RequestCode:
  145. switch (c) {
  146. case 'Q':
  147. case 'T':
  148. case 'L':
  149. case 'M':
  150. case 'U':
  151. case 'X':
  152. case 'P':
  153. case 'S':
  154. case 'B':
  155. case 'E':
  156. case 'W':
  157. case 'K':
  158. case 'F':
  159. case 'f':
  160. case 'H':
  161. case 'R':
  162. responseMsg.request.code = (RequestMsgCodes)c;
  163. responseMsg.request.value = 0;
  164. responseMsg.request.value2 = 0;
  165. responseMsg.request.crc8 = 0;
  166. rspState = ResponseStates::RequestValue;
  167. return DecodeStatus::NeedMoreData;
  168. case 0x0a:
  169. case 0x0d:
  170. // skip leading whitespace if any (makes integration with other SW easier/tolerant)
  171. return DecodeStatus::NeedMoreData;
  172. default:
  173. rspState = ResponseStates::Error;
  174. return DecodeStatus::Error;
  175. }
  176. case ResponseStates::RequestValue:
  177. if (IsHexDigit(c)) {
  178. responseMsg.request.value <<= 4U;
  179. responseMsg.request.value += Char2Nibble(c);
  180. return DecodeStatus::NeedMoreData;
  181. } else if (c == ' ') {
  182. rspState = ResponseStates::ParamCode;
  183. return DecodeStatus::NeedMoreData;
  184. } else {
  185. rspState = ResponseStates::Error;
  186. return DecodeStatus::Error;
  187. }
  188. case ResponseStates::ParamCode:
  189. switch (c) {
  190. case 'P':
  191. case 'E':
  192. case 'F':
  193. case 'A':
  194. case 'R':
  195. case 'B':
  196. rspState = ResponseStates::ParamValue;
  197. responseMsg.paramCode = (ResponseMsgParamCodes)c;
  198. responseMsg.paramValue = 0;
  199. return DecodeStatus::NeedMoreData;
  200. default:
  201. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  202. rspState = ResponseStates::Error;
  203. return DecodeStatus::Error;
  204. }
  205. case ResponseStates::ParamValue:
  206. if (IsHexDigit(c)) {
  207. responseMsg.paramValue <<= 4U;
  208. responseMsg.paramValue += Char2Nibble(c);
  209. return DecodeStatus::NeedMoreData;
  210. } else if (IsCRCSeparator(c)) {
  211. rspState = ResponseStates::CRC;
  212. return DecodeStatus::NeedMoreData;
  213. } else {
  214. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  215. rspState = ResponseStates::Error;
  216. return DecodeStatus::Error;
  217. }
  218. case ResponseStates::CRC:
  219. if (IsHexDigit(c)) {
  220. responseMsg.request.crc8 <<= 4U;
  221. responseMsg.request.crc8 += Char2Nibble(c);
  222. return DecodeStatus::NeedMoreData;
  223. } else if (IsNewLine(c)) {
  224. // check CRC at this spot
  225. if (responseMsg.request.crc8 != responseMsg.ComputeCRC8()) {
  226. // CRC mismatch
  227. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  228. rspState = ResponseStates::Error;
  229. return DecodeStatus::Error;
  230. } else {
  231. rspState = ResponseStates::RequestCode;
  232. return DecodeStatus::MessageCompleted;
  233. }
  234. } else {
  235. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  236. rspState = ResponseStates::Error;
  237. return DecodeStatus::Error;
  238. }
  239. default: //case error:
  240. if (IsNewLine(c)) {
  241. rspState = ResponseStates::RequestCode;
  242. return DecodeStatus::MessageCompleted;
  243. } else {
  244. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  245. return DecodeStatus::Error;
  246. }
  247. }
  248. }
  249. uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) {
  250. // BEWARE:
  251. // ResponseMsg rsp(RequestMsg(msg.code, msg.value), ar, 0);
  252. // ... is NOT the same as:
  253. // ResponseMsg rsp(msg, ar, 0);
  254. // ... because of the usually unused parameter value2 (which only comes non-zero in write requests).
  255. // It took me a few hours to find out why the CRC from the MMU never matched all the other sides (unit tests and the MK3S)
  256. // It is because this was the only place where the original request kept its value2 non-zero.
  257. // In the response, we must make sure value2 is actually zero unless being sent along with it (which is not right now)
  258. const ResponseMsg rsp(RequestMsg(msg.code, msg.value), ar, 0); // this needs some cleanup @@TODO - check assembly how bad is it
  259. uint8_t i = BeginEncodeRequest(rsp.request, txbuff);
  260. txbuff[i] = (uint8_t)ar;
  261. ++i;
  262. i += AppendCRC(rsp.CRC(), txbuff + i);
  263. txbuff[i] = '\n';
  264. ++i;
  265. return i;
  266. }
  267. uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) {
  268. return EncodeResponseRead(msg, true, findaValue, txbuff);
  269. }
  270. uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff) {
  271. const ResponseMsg rsp(msg, rcs.code, rcs.value);
  272. uint8_t i = BeginEncodeRequest(msg, txbuff);
  273. txbuff[i] = (uint8_t)rsp.paramCode;
  274. ++i;
  275. i += UInt16ToHex(rsp.paramValue, txbuff + i);
  276. i += AppendCRC(rsp.CRC(), txbuff + i);
  277. txbuff[i] = '\n';
  278. return i + 1;
  279. }
  280. uint8_t Protocol::EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff) {
  281. const ResponseMsg rsp(msg,
  282. accepted ? ResponseMsgParamCodes::Accepted : ResponseMsgParamCodes::Rejected,
  283. accepted ? value2 : 0 // be careful about this value for CRC computation - rejected status doesn't have any meaningful value which could be reconstructed from the textual form of the message
  284. );
  285. uint8_t i = BeginEncodeRequest(msg, txbuff);
  286. txbuff[i] = (uint8_t)rsp.paramCode;
  287. ++i;
  288. if (accepted) {
  289. // dump the value
  290. i += UInt16ToHex(value2, txbuff + i);
  291. }
  292. i += AppendCRC(rsp.CRC(), txbuff + i);
  293. txbuff[i] = '\n';
  294. return i + 1;
  295. }
  296. uint8_t Protocol::UInt8ToHex(uint8_t value, uint8_t *dst) {
  297. if (value == 0) {
  298. *dst = '0';
  299. return 1;
  300. }
  301. uint8_t v = value >> 4U;
  302. uint8_t charsOut = 1;
  303. if (v != 0) { // skip the first '0' if any
  304. *dst = Nibble2Char(v);
  305. ++dst;
  306. charsOut = 2;
  307. }
  308. v = value & 0xfU;
  309. *dst = Nibble2Char(v);
  310. return charsOut;
  311. }
  312. uint8_t Protocol::UInt16ToHex(uint16_t value, uint8_t *dst) {
  313. constexpr uint16_t topNibbleMask = 0xf000;
  314. if (value == 0) {
  315. *dst = '0';
  316. return 1;
  317. }
  318. // skip initial zeros
  319. uint8_t charsOut = 4;
  320. while ((value & topNibbleMask) == 0) {
  321. value <<= 4U;
  322. --charsOut;
  323. }
  324. for (uint8_t i = 0; i < charsOut; ++i) {
  325. uint8_t n = (value & topNibbleMask) >> (8U + 4U);
  326. value <<= 4U;
  327. *dst = Nibble2Char(n);
  328. ++dst;
  329. }
  330. return charsOut;
  331. }
  332. uint8_t Protocol::BeginEncodeRequest(const RequestMsg &msg, uint8_t *dst) {
  333. dst[0] = (uint8_t)msg.code;
  334. uint8_t i = 1 + UInt8ToHex(msg.value, dst + 1);
  335. dst[i] = ' ';
  336. return i + 1;
  337. }
  338. uint8_t Protocol::AppendCRC(uint8_t crc, uint8_t *dst) {
  339. dst[0] = '*'; // reprap-style separator of CRC
  340. return 1 + UInt8ToHex(crc, dst + 1);
  341. }
  342. } // namespace protocol
  343. } // namespace modules