printer_receive.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <stdint.h>
  2. #include <furi.h>
  3. #include <gblink/include/gblink.h>
  4. #include "printer_i.h"
  5. #define TAG "printer_receive"
  6. /* NOTE:
  7. * These numbers are empirically gathered from a few different games thus far.
  8. * There are many notes floating around the internet of the GB Printer having
  9. * a 100 ms limit between packets where it will reset. However, I've seen
  10. * Pokemon Pinball wait 99.5 ms between packets after a print command which is
  11. * a bit too close for comfort. As this code tracks timestamps _after_ each byte,
  12. * that ends up just over 110 ms which trips the hard timeout and resets state.
  13. * This often cofuses the hell out of games.
  14. *
  15. * Additionally, on the other end of the spectrum, Pokemon Gold absolutely uses
  16. * the hard timeout to reset the printer between packets. It waits ~278 ms, when
  17. * it could just send an init command.
  18. *
  19. * Even more silly, Pokemon Pinball has a fun quirk where if the print completes
  20. * immediately (usually the Flipper will mark a print complete with a single
  21. * packet turnaround), it asks for status a couple of times, then starts (presumably)
  22. * another status packet, but the second byte in the transfer is stopped mid-byte.
  23. * Between that point and the start of the next, real packet, is 30 ms, 32.6 ms
  24. * if you go from end of last byte received to start of next, real packet.
  25. *
  26. * This means there is some "soft" timeout that the printer uses to reset a packet
  27. * transfer in progress, but don't reset the whole printer state.
  28. *
  29. * There are wisps of some "unknown" bit timeout of 1.49 ms. But I've not yet
  30. * seen that in action.
  31. *
  32. * As far as I know, no one has dumped and reverse engineered the ROM of the
  33. * Game Boy Printer directly. I think all of the existing documentation was from
  34. * reverse engineering the communication channel. Maybe someday I'll dump the
  35. * GB Printer ROM and try to better understand all of it.
  36. */
  37. #define HARD_TIMEOUT_US 125000
  38. #define SOFT_TIMEOUT_US 20000
  39. static void printer_reset(struct printer_proto *printer)
  40. {
  41. /* Clear out the current packet data */
  42. memset(printer->packet, '\0', sizeof(struct packet));
  43. printer->image->data_sz = 0;
  44. /* This is technically redundant, done for completeness */
  45. printer->packet->state = START_L;
  46. /* Packet timeout start */
  47. printer->packet->time = DWT->CYCCNT;
  48. }
  49. static void byte_callback(void *context, uint8_t val)
  50. {
  51. struct printer_proto *printer = context;
  52. struct packet *packet = printer->packet;
  53. const uint32_t time_ticks = furi_hal_cortex_instructions_per_microsecond() * HARD_TIMEOUT_US;
  54. uint8_t data_out = 0x00;
  55. if ((DWT->CYCCNT - packet->time) > time_ticks)
  56. printer_reset(printer);
  57. if ((DWT->CYCCNT - packet->time) > furi_hal_cortex_instructions_per_microsecond() * SOFT_TIMEOUT_US)
  58. packet->state = START_L;
  59. /* Packet timeout restart */
  60. packet->time = DWT->CYCCNT;
  61. /* TODO: flash led? */
  62. switch (packet->state) {
  63. case START_L:
  64. if (val == PKT_START_L) {
  65. packet->state = START_H;
  66. packet->zero_counter = 0;
  67. }
  68. if (val == 0x00) {
  69. packet->zero_counter++;
  70. if (packet->zero_counter == 16)
  71. printer_reset(printer);
  72. }
  73. break;
  74. case START_H:
  75. if (val == PKT_START_H)
  76. packet->state = COMMAND;
  77. else
  78. packet->state = START_L;
  79. break;
  80. case COMMAND:
  81. packet->cmd = val;
  82. packet->state = COMPRESS;
  83. packet->cksum_calc += val;
  84. break;
  85. case COMPRESS:
  86. packet->cksum_calc += val;
  87. packet->state = LEN_L;
  88. if (val) {
  89. FURI_LOG_E(TAG, "Compression not supported!");
  90. packet->status |= STATUS_PKT_ERR;
  91. }
  92. break;
  93. case LEN_L:
  94. packet->cksum_calc += val;
  95. packet->state = LEN_H;
  96. packet->len = (val & 0xff);
  97. break;
  98. case LEN_H:
  99. packet->cksum_calc += val;
  100. packet->len |= ((val & 0xff) << 8);
  101. /* Override length for a TRANSFER */
  102. if (packet->cmd == CMD_TRANSFER)
  103. packet->len = TRANSFER_RECV_SZ;
  104. if (packet->len) {
  105. packet->state = COMMAND_DAT;
  106. } else {
  107. packet->state = CKSUM_L;
  108. }
  109. break;
  110. case COMMAND_DAT:
  111. packet->cksum_calc += val;
  112. packet->recv_data[packet->recv_data_sz] = val;
  113. packet->recv_data_sz++;
  114. if (packet->recv_data_sz == packet->len)
  115. packet->state = CKSUM_L;
  116. break;
  117. case CKSUM_L:
  118. packet->state = CKSUM_H;
  119. packet->cksum = (val & 0xff);
  120. break;
  121. case CKSUM_H:
  122. packet->state = ALIVE;
  123. packet->cksum |= ((val & 0xff) << 8);
  124. if (packet->cksum != packet->cksum_calc)
  125. packet->status |= STATUS_CKSUM;
  126. // TRANSFER does not set checksum bytes
  127. if (packet->cmd == CMD_TRANSFER)
  128. packet->status &= ~STATUS_CKSUM;
  129. data_out = PRINTER_ID;
  130. break;
  131. case ALIVE:
  132. packet->state = STATUS;
  133. data_out = packet->status;
  134. break;
  135. case STATUS:
  136. packet->state = START_L;
  137. switch (packet->cmd) {
  138. case CMD_INIT:
  139. printer_reset(printer);
  140. break;
  141. case CMD_DATA:
  142. if (printer->image->data_sz < PRINT_FULL_SZ) {
  143. if ((printer->image->data_sz + packet->len) <= PRINT_FULL_SZ) {
  144. memcpy((printer->image->data)+printer->image->data_sz, packet->recv_data, packet->len);
  145. printer->image->data_sz += packet->len;
  146. } else {
  147. memcpy((printer->image->data)+printer->image->data_sz, packet->recv_data, ((printer->image->data_sz + packet->len)) - PRINT_FULL_SZ);
  148. printer->image->data_sz += (PRINT_FULL_SZ - (printer->image->data_sz + packet->len));
  149. furi_assert(printer->image->data_sz <= PRINT_FULL_SZ);
  150. }
  151. }
  152. /* Any time data is written to the buffer, READY is set */
  153. packet->status |= STATUS_READY;
  154. furi_thread_flags_set(printer->thread, THREAD_FLAGS_DATA);
  155. break;
  156. case CMD_TRANSFER:
  157. /* XXX: TODO: Check to see if we're still printing when getting
  158. * a transfer command. If so, then we have failed to beat the clock.
  159. */
  160. case CMD_PRINT:
  161. /* TODO: Be able to memcpy these */
  162. printer->image->num_sheets = packet->recv_data[0];
  163. printer->image->margins = packet->recv_data[1];
  164. printer->image->palette = packet->recv_data[2];
  165. printer->image->exposure = packet->recv_data[3];
  166. packet->status &= ~STATUS_READY;
  167. packet->status |= (STATUS_PRINTING | STATUS_FULL);
  168. furi_thread_flags_set(printer->thread, THREAD_FLAGS_PRINT);
  169. break;
  170. case CMD_STATUS:
  171. /* READY cleared on status request */
  172. packet->status &= ~STATUS_READY;
  173. if ((packet->status & STATUS_PRINTING) &&
  174. (packet->status & STATUS_PRINTED)) {
  175. packet->status &= ~(STATUS_PRINTING | STATUS_PRINTED);
  176. furi_thread_flags_set(printer->thread, THREAD_FLAGS_COMPLETE);
  177. }
  178. }
  179. packet->recv_data_sz = 0;
  180. packet->cksum_calc = 0;
  181. /* XXX: TODO: if the command had something we need to do, do it here. */
  182. /* done! flush our buffers, deal with any status changes like
  183. * not printing -> printing -> not printing, etc.
  184. */
  185. /* Do a callback here?
  186. * if so, I guess we should wait for callback completion before accepting more recv_data?
  187. * but that means the callback is in an interrupt context, which, is probably okay?
  188. */
  189. /* XXX: TODO: NOTE: FIXME:
  190. * all of the notes..
  191. * This module needs to maintain the whole buffer, but it can be safely assumed that the buffer
  192. * will never exceed 20x18 tiles (no clue how many bytes) as that is the max the printer can
  193. * take on in a single print. Printing mulitples needs a print, and then a second print with
  194. * no margin. So the margins are important and need to be passed to the final application,
  195. * SOMEHOW.
  196. *
  197. * More imporatntly, is the completed callback NEEDS to have a return value. This allows
  198. * the end application to take that whole panel, however its laid out, and do whatever
  199. * it wants to do with it. Write it to a file, convert, etc., etc., so that this module
  200. * will forever return that it is printing until the callback returns true.
  201. *
  202. * Once we call the callback and it shows a true, then we can be sure the higher module
  203. * is done with the buffer, and we can tell the host that yes, its done, you can continue
  204. * if you want.
  205. */
  206. /* XXX: On TRANSFER, there is no checking of status, it is only two packets in total.
  207. * I can assume that if we delay a bit in moving the buffer around that should be okay
  208. * but we probably don't want to wait too long.
  209. * Upon testing, transfer seems to doesn't
  210. */
  211. break;
  212. default:
  213. FURI_LOG_E(TAG, "unknown status!");
  214. break;
  215. }
  216. /* transfer next byte */
  217. gblink_transfer(printer->gblink_handle, data_out);
  218. }
  219. void printer_receive_start(void *printer_handle)
  220. {
  221. struct printer_proto *printer = printer_handle;
  222. /* Set up defaults the receive path needs */
  223. gblink_callback_set(printer->gblink_handle, byte_callback, printer);
  224. gblink_clk_source_set(printer->gblink_handle, GBLINK_CLK_EXT);
  225. printer_reset(printer);
  226. gblink_start(printer->gblink_handle);
  227. }
  228. void printer_receive_print_complete(void *printer_handle)
  229. {
  230. struct printer_proto *printer = printer_handle;
  231. printer->packet->status |= STATUS_PRINTED;
  232. }