My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wifi_module.cpp 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../inc/MarlinConfigPre.h"
  23. #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
  24. #include "draw_ui.h"
  25. #include "wifi_module.h"
  26. #include "wifi_upload.h"
  27. #include "SPI_TFT.h"
  28. #include "../../marlinui.h"
  29. #include "../../../MarlinCore.h"
  30. #include "../../../module/temperature.h"
  31. #include "../../../gcode/queue.h"
  32. #include "../../../gcode/gcode.h"
  33. #include "../../../sd/cardreader.h"
  34. #include "../../../module/planner.h"
  35. #include "../../../module/servo.h"
  36. #include "../../../module/probe.h"
  37. #if DISABLED(EMERGENCY_PARSER)
  38. #include "../../../module/motion.h"
  39. #endif
  40. #if ENABLED(POWER_LOSS_RECOVERY)
  41. #include "../../../feature/powerloss.h"
  42. #endif
  43. #if ENABLED(PARK_HEAD_ON_PAUSE)
  44. #include "../../../feature/pause.h"
  45. #endif
  46. #define WIFI_SET() WRITE(WIFI_RESET_PIN, HIGH);
  47. #define WIFI_RESET() WRITE(WIFI_RESET_PIN, LOW);
  48. #define WIFI_IO1_SET() WRITE(WIFI_IO1_PIN, HIGH);
  49. #define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW);
  50. extern uint8_t Explore_Disk (char *path , uint8_t recu_level);
  51. extern uint8_t commands_in_queue;
  52. extern uint8_t sel_id;
  53. int usartFifoAvailable(SZ_USART_FIFO *fifo);
  54. int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len);
  55. int writeUsartFifo(SZ_USART_FIFO * fifo, int8_t * buf, int32_t len);
  56. extern unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick);
  57. volatile SZ_USART_FIFO WifiRxFifo;
  58. #define WAIT_ESP_TRANS_TIMEOUT_TICK 10500
  59. int cfg_cloud_flag = 0;
  60. extern PRINT_TIME print_time;
  61. char wifi_firm_ver[20] = { 0 };
  62. WIFI_GCODE_BUFFER espGcodeFifo;
  63. extern uint8_t pause_resum;
  64. uint8_t wifi_connect_flg = 0;
  65. extern volatile uint8_t get_temp_flag;
  66. #define WIFI_MODE 2
  67. #define WIFI_AP_MODE 3
  68. int upload_result = 0;
  69. uint32_t upload_time = 0;
  70. uint32_t upload_size = 0;
  71. volatile WIFI_STATE wifi_link_state;
  72. WIFI_PARA wifiPara;
  73. IP_PARA ipPara;
  74. CLOUD_PARA cloud_para;
  75. char wifi_check_time = 0;
  76. extern uint8_t gCurDir[100];
  77. extern uint32_t wifi_loop_cycle;
  78. volatile TRANSFER_STATE esp_state;
  79. uint8_t left_to_send = 0;
  80. uint8_t left_to_save[96] = { 0 };
  81. volatile WIFI_DMA_RCV_FIFO wifiDmaRcvFifo;
  82. volatile WIFI_TRANS_ERROR wifiTransError;
  83. static bool need_ok_later = false;
  84. extern volatile WIFI_STATE wifi_link_state;
  85. extern WIFI_PARA wifiPara;
  86. extern IP_PARA ipPara;
  87. extern CLOUD_PARA cloud_para;
  88. extern bool once_flag, flash_preview_begin, default_preview_flg, gcode_preview_over;
  89. extern bool flash_dma_mode;
  90. uint32_t getWifiTick() { return millis(); }
  91. uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick) {
  92. if (lastTick <= curTick)
  93. return (curTick - lastTick) * TICK_CYCLE;
  94. else
  95. return (0xFFFFFFFF - lastTick + curTick) * TICK_CYCLE;
  96. }
  97. void wifi_delay(int n) {
  98. uint32_t begin = getWifiTick();
  99. uint32_t end = begin;
  100. while (getWifiTickDiff(begin, end) < (uint32_t)n) {
  101. watchdog_refresh();
  102. end = getWifiTick();
  103. }
  104. }
  105. void wifi_reset() {
  106. uint32_t start, now;
  107. start = getWifiTick();
  108. now = start;
  109. WIFI_RESET();
  110. while (getWifiTickDiff(start, now) < 500)
  111. now = getWifiTick();
  112. WIFI_SET();
  113. }
  114. void mount_file_sys(uint8_t disk_type) {
  115. if (disk_type == FILE_SYS_SD) {
  116. TERN_(SDSUPPORT, card.mount());
  117. }
  118. else if (disk_type == FILE_SYS_USB) {
  119. }
  120. }
  121. #include <libmaple/timer.h>
  122. #include <libmaple/util.h>
  123. #include <libmaple/rcc.h>
  124. #include <boards.h>
  125. #include <wirish.h>
  126. #include <libmaple/dma.h>
  127. #include <libmaple/bitband.h>
  128. #include <libmaple/libmaple.h>
  129. #include <libmaple/gpio.h>
  130. #include <libmaple/usart.h>
  131. #include <libmaple/ring_buffer.h>
  132. void changeFlashMode(const bool dmaMode) {
  133. if (flash_dma_mode != dmaMode) {
  134. flash_dma_mode = dmaMode;
  135. if (!flash_dma_mode) {
  136. dma_disable(DMA1, DMA_CH5);
  137. dma_clear_isr_bits(DMA1, DMA_CH4);
  138. }
  139. }
  140. }
  141. static bool longName2DosName(const char *longName, char *dosName) {
  142. uint8_t i;
  143. for (i = FILENAME_LENGTH; i--;) dosName[i] = '\0';
  144. while (*longName) {
  145. uint8_t c = *longName++;
  146. if (c == '.') { // For a dot...
  147. if (i == 0) return false;
  148. strcat_P(dosName, PSTR(".GCO"));
  149. break;
  150. }
  151. else {
  152. if (c < 0x21 || c == 0x7F) return false; // Check size, non-printable characters
  153. // Fail for illegal characters
  154. PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
  155. while (const uint8_t b = pgm_read_byte(p++)) if (b == c) return false;
  156. dosName[i++] = c + (WITHIN(c, 'a', 'z') ? 'A' - 'a' : 0); // Uppercase required for 8.3 name
  157. }
  158. if (i >= 5) {
  159. strcat_P(dosName, PSTR("~1.GCO"));
  160. break;
  161. }
  162. }
  163. return dosName[0] != '\0'; // Return true if any name was set
  164. }
  165. static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) {
  166. unsigned char tmpW = wifiDmaRcvFifo.write_cur;
  167. if (len > UDISKBUFLEN) return 0;
  168. if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) {
  169. memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len);
  170. wifiDmaRcvFifo.state[tmpW] = udisk_buf_full;
  171. wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. static void esp_dma_pre() {
  177. dma_channel_reg_map *channel_regs = dma_tube_regs(DMA1, DMA_CH5);
  178. CBI32(channel_regs->CCR, 0);
  179. channel_regs->CMAR = (uint32_t)WIFISERIAL.usart_device->rb->buf;
  180. channel_regs->CNDTR = 0x0000;
  181. channel_regs->CNDTR = UART_RX_BUFFER_SIZE;
  182. DMA1->regs->IFCR = 0xF0000;
  183. SBI32(channel_regs->CCR, 0);
  184. }
  185. static void dma_ch5_irq_handle() {
  186. uint8 status_bits = dma_get_isr_bits(DMA1, DMA_CH5);
  187. dma_clear_isr_bits(DMA1, DMA_CH5);
  188. if (status_bits & 0x8) {
  189. // DMA transmit Error
  190. }
  191. else if (status_bits & 0x2) {
  192. // DMA transmit complete
  193. if (esp_state == TRANSFER_IDLE)
  194. esp_state = TRANSFERING;
  195. if (storeRcvData(WIFISERIAL.usart_device->rb->buf, UART_RX_BUFFER_SIZE)) {
  196. esp_dma_pre();
  197. if (wifiTransError.flag != 0x1)
  198. WIFI_IO1_RESET();
  199. }
  200. else {
  201. WIFI_IO1_SET();
  202. esp_state = TRANSFER_STORE;
  203. }
  204. }
  205. else if (status_bits & 0x4) {
  206. // DMA transmit half
  207. WIFI_IO1_SET();
  208. }
  209. }
  210. static void wifi_usart_dma_init() {
  211. dma_init(DMA1);
  212. uint32_t flags = ( DMA_MINC_MODE | DMA_TRNS_CMPLT | DMA_HALF_TRNS | DMA_TRNS_ERR);
  213. dma_xfer_size dma_bit_size = DMA_SIZE_8BITS;
  214. dma_setup_transfer(DMA1, DMA_CH5, &USART1_BASE->DR, dma_bit_size,
  215. (volatile void*)WIFISERIAL.usart_device->rb->buf, dma_bit_size, flags);// Transmit buffer DMA
  216. dma_set_priority(DMA1, DMA_CH5, DMA_PRIORITY_LOW);
  217. dma_attach_interrupt(DMA1, DMA_CH5, &dma_ch5_irq_handle);
  218. dma_clear_isr_bits(DMA1, DMA_CH5);
  219. dma_set_num_transfers(DMA1, DMA_CH5, UART_RX_BUFFER_SIZE);
  220. bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 1);
  221. dma_enable(DMA1, DMA_CH5); // enable transmit
  222. for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) {
  223. wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i];
  224. wifiDmaRcvFifo.state[i] = udisk_buf_empty;
  225. }
  226. memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM);
  227. wifiDmaRcvFifo.read_cur = 0;
  228. wifiDmaRcvFifo.write_cur = 0;
  229. }
  230. void esp_port_begin(uint8_t interrupt) {
  231. WifiRxFifo.uart_read_point = 0;
  232. WifiRxFifo.uart_write_point = 0;
  233. #if 1
  234. #if ENABLED(MKS_WIFI_MODULE)
  235. WIFISERIAL.end();
  236. if (interrupt) {
  237. for (uint16_t i = 0; i < 65535; i++) { /*nada*/ }
  238. WIFISERIAL.begin(WIFI_BAUDRATE);
  239. millis_t serial_connect_timeout = millis() + 1000UL;
  240. while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  241. }
  242. else {
  243. WIFISERIAL.usart_device->regs->CR1 &= ~USART_CR1_RXNEIE;
  244. WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE);
  245. wifi_usart_dma_init();
  246. }
  247. #endif
  248. #else
  249. #if MKS_WIFI_MODULE
  250. WIFISERIAL.end();
  251. for (uint16_t i = 0; i < 65535; i++) { /*nada*/ }
  252. WIFISERIAL.begin(interrupt ? WIFI_BAUDRATE : WIFI_UPLOAD_BAUDRATE);
  253. millis_t serial_connect_timeout = millis() + 1000UL;
  254. while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  255. #endif
  256. if (!interrupt) wifi_usart_dma_init();
  257. #endif
  258. }
  259. #if ENABLED(MKS_WIFI_MODULE)
  260. int raw_send_to_wifi(uint8_t *buf, int len) {
  261. if (buf == 0 || len <= 0) return 0;
  262. for (int i = 0; i < len; i++)
  263. WIFISERIAL.write(*(buf + i));
  264. return len;
  265. }
  266. #endif
  267. void wifi_ret_ack() {}
  268. uint8_t buf_to_wifi[256];
  269. int index_to_wifi = 0;
  270. int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len) {
  271. uint8_t wifi_ret_head = 0xA5;
  272. uint8_t wifi_ret_tail = 0xFC;
  273. if (type == WIFI_PARA_SET) {
  274. int data_offset = 4;
  275. int apLen = strlen((const char *)uiCfg.wifi_name);
  276. int keyLen = strlen((const char *)uiCfg.wifi_key);
  277. ZERO(buf_to_wifi);
  278. index_to_wifi = 0;
  279. buf_to_wifi[data_offset] = gCfgItems.wifi_mode_sel;
  280. buf_to_wifi[data_offset + 1] = apLen;
  281. memcpy(&buf_to_wifi[data_offset + 2], (const char *)uiCfg.wifi_name, apLen);
  282. buf_to_wifi[data_offset + apLen + 2] = keyLen;
  283. memcpy(&buf_to_wifi[data_offset + apLen + 3], (const char *)uiCfg.wifi_key, keyLen);
  284. buf_to_wifi[data_offset + apLen + keyLen + 3] = wifi_ret_tail;
  285. index_to_wifi = apLen + keyLen + 3;
  286. buf_to_wifi[0] = wifi_ret_head;
  287. buf_to_wifi[1] = type;
  288. buf_to_wifi[2] = index_to_wifi & 0xFF;
  289. buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
  290. raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
  291. ZERO(buf_to_wifi);
  292. index_to_wifi = 0;
  293. }
  294. else if (type == WIFI_TRANS_INF) {
  295. if (len > (int)(sizeof(buf_to_wifi) - index_to_wifi - 5)) {
  296. ZERO(buf_to_wifi);
  297. index_to_wifi = 0;
  298. return 0;
  299. }
  300. if (len > 0) {
  301. memcpy(&buf_to_wifi[4 + index_to_wifi], buf, len);
  302. index_to_wifi += len;
  303. if (index_to_wifi < 1)
  304. return 0;
  305. if (buf_to_wifi[index_to_wifi + 3] == '\n') {
  306. // mask "wait" "busy" "X:"
  307. if (((buf_to_wifi[4] == 'w') && (buf_to_wifi[5] == 'a') && (buf_to_wifi[6] == 'i') && (buf_to_wifi[7] == 't') )
  308. || ((buf_to_wifi[4] == 'b') && (buf_to_wifi[5] == 'u') && (buf_to_wifi[6] == 's') && (buf_to_wifi[7] == 'y') )
  309. || ((buf_to_wifi[4] == 'X') && (buf_to_wifi[5] == ':') )
  310. ) {
  311. ZERO(buf_to_wifi);
  312. index_to_wifi = 0;
  313. return 0;
  314. }
  315. buf_to_wifi[0] = wifi_ret_head;
  316. buf_to_wifi[1] = type;
  317. buf_to_wifi[2] = index_to_wifi & 0xFF;
  318. buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
  319. buf_to_wifi[4 + index_to_wifi] = wifi_ret_tail;
  320. raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
  321. ZERO(buf_to_wifi);
  322. index_to_wifi = 0;
  323. }
  324. }
  325. }
  326. else if (type == WIFI_EXCEP_INF) {
  327. ZERO(buf_to_wifi);
  328. buf_to_wifi[0] = wifi_ret_head;
  329. buf_to_wifi[1] = type;
  330. buf_to_wifi[2] = 1;
  331. buf_to_wifi[3] = 0;
  332. buf_to_wifi[4] = *buf;
  333. buf_to_wifi[5] = wifi_ret_tail;
  334. raw_send_to_wifi(buf_to_wifi, 6);
  335. ZERO(buf_to_wifi);
  336. index_to_wifi = 0;
  337. }
  338. else if (type == WIFI_CLOUD_CFG) {
  339. int data_offset = 4;
  340. int urlLen = strlen((const char *)uiCfg.cloud_hostUrl);
  341. ZERO(buf_to_wifi);
  342. index_to_wifi = 0;
  343. buf_to_wifi[data_offset] = gCfgItems.cloud_enable ? 0x0A : 0x05;
  344. buf_to_wifi[data_offset + 1] = urlLen;
  345. memcpy(&buf_to_wifi[data_offset + 2], (const char *)uiCfg.cloud_hostUrl, urlLen);
  346. buf_to_wifi[data_offset + urlLen + 2] = uiCfg.cloud_port & 0xFF;
  347. buf_to_wifi[data_offset + urlLen + 3] = (uiCfg.cloud_port >> 8) & 0xFF;
  348. buf_to_wifi[data_offset + urlLen + 4] = wifi_ret_tail;
  349. index_to_wifi = urlLen + 4;
  350. buf_to_wifi[0] = wifi_ret_head;
  351. buf_to_wifi[1] = type;
  352. buf_to_wifi[2] = index_to_wifi & 0xFF;
  353. buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
  354. raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
  355. ZERO(buf_to_wifi);
  356. index_to_wifi = 0;
  357. }
  358. else if (type == WIFI_CLOUD_UNBIND) {
  359. ZERO(buf_to_wifi);
  360. buf_to_wifi[0] = wifi_ret_head;
  361. buf_to_wifi[1] = type;
  362. buf_to_wifi[2] = 0;
  363. buf_to_wifi[3] = 0;
  364. buf_to_wifi[4] = wifi_ret_tail;
  365. raw_send_to_wifi(buf_to_wifi, 5);
  366. ZERO(buf_to_wifi);
  367. index_to_wifi = 0;
  368. }
  369. return 1;
  370. }
  371. #define SEND_OK_TO_WIFI send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n"))
  372. int send_to_wifi(uint8_t *buf, int len) { return package_to_wifi(WIFI_TRANS_INF, buf, len); }
  373. void set_cur_file_sys(int fileType) { gCfgItems.fileSysType = fileType; }
  374. void get_file_list(char *path) {
  375. if (!path) return;
  376. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  377. TERN_(SDSUPPORT, card.mount());
  378. }
  379. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  380. // udisk
  381. }
  382. Explore_Disk(path, 0);
  383. }
  384. char wait_ip_back_flag = 0;
  385. typedef struct {
  386. int write_index;
  387. uint8_t saveFileName[30];
  388. uint8_t fileTransfer;
  389. uint32_t fileLen;
  390. uint32_t tick_begin;
  391. uint32_t tick_end;
  392. } FILE_WRITER;
  393. FILE_WRITER file_writer;
  394. int32_t lastFragment = 0;
  395. char saveFilePath[50];
  396. static SdFile upload_file, *upload_curDir;
  397. static filepos_t pos;
  398. int write_to_file(char *buf, int len) {
  399. int i;
  400. int res = 0;
  401. for (i = 0; i < len; i++) {
  402. public_buf[file_writer.write_index++] = buf[i];
  403. if (file_writer.write_index >= 512) {
  404. res = upload_file.write(public_buf, file_writer.write_index);
  405. if (res == -1) {
  406. upload_file.close();
  407. const char * const fname = card.diveToFile(false, upload_curDir, saveFilePath);
  408. if (upload_file.open(upload_curDir, fname, O_WRITE)) {
  409. upload_file.setpos(&pos);
  410. res = upload_file.write(public_buf, file_writer.write_index);
  411. }
  412. }
  413. if (res == -1) return -1;
  414. upload_file.getpos(&pos);
  415. file_writer.write_index = 0;
  416. }
  417. }
  418. if (res == -1) {
  419. ZERO(public_buf);
  420. file_writer.write_index = 0;
  421. return -1;
  422. }
  423. return 0;
  424. }
  425. #define ESP_PROTOC_HEAD (uint8_t)0xA5
  426. #define ESP_PROTOC_TAIL (uint8_t)0xFC
  427. #define ESP_TYPE_NET (uint8_t)0x0
  428. #define ESP_TYPE_GCODE (uint8_t)0x1
  429. #define ESP_TYPE_FILE_FIRST (uint8_t)0x2
  430. #define ESP_TYPE_FILE_FRAGMENT (uint8_t)0x3
  431. #define ESP_TYPE_WIFI_LIST (uint8_t)0x4
  432. uint8_t esp_msg_buf[UART_RX_BUFFER_SIZE] = { 0 };
  433. uint16_t esp_msg_index = 0;
  434. typedef struct {
  435. uint8_t head;
  436. uint8_t type;
  437. uint16_t dataLen;
  438. uint8_t *data;
  439. uint8_t tail;
  440. } ESP_PROTOC_FRAME;
  441. static int cut_msg_head(uint8_t *msg, uint16_t msgLen, uint16_t cutLen) {
  442. if (msgLen < cutLen) return 0;
  443. else if (msgLen == cutLen) {
  444. memset(msg, 0, msgLen);
  445. return 0;
  446. }
  447. for (int i = 0; i < (msgLen - cutLen); i++)
  448. msg[i] = msg[cutLen + i];
  449. memset(&msg[msgLen - cutLen], 0, cutLen);
  450. return msgLen - cutLen;
  451. }
  452. uint8_t Explore_Disk(char *path , uint8_t recu_level) {
  453. char tmp[200];
  454. char Fstream[200];
  455. if (!path) return 0;
  456. const uint8_t fileCnt = card.get_num_Files();
  457. for (uint8_t i = 0; i < fileCnt; i++) {
  458. card.getfilename_sorted(SD_ORDER(i, fileCnt));
  459. ZERO(tmp);
  460. strcpy(tmp, card.filename);
  461. ZERO(Fstream);
  462. strcpy(Fstream, tmp);
  463. if (card.flag.filenameIsDir && recu_level <= 10)
  464. strcat_P(Fstream, PSTR(".DIR"));
  465. strcat_P(Fstream, PSTR("\r\n"));
  466. send_to_wifi((uint8_t*)Fstream, strlen(Fstream));
  467. }
  468. return fileCnt;
  469. }
  470. static void wifi_gcode_exec(uint8_t *cmd_line) {
  471. int8_t tempBuf[100] = { 0 };
  472. uint8_t *tmpStr = 0;
  473. int cmd_value;
  474. volatile int print_rate;
  475. if (strchr((char *)cmd_line, '\n') && (strchr((char *)cmd_line, 'G') || strchr((char *)cmd_line, 'M') || strchr((char *)cmd_line, 'T'))) {
  476. tmpStr = (uint8_t *)strchr((char *)cmd_line, '\n');
  477. if (tmpStr) *tmpStr = '\0';
  478. tmpStr = (uint8_t *)strchr((char *)cmd_line, '\r');
  479. if (tmpStr) *tmpStr = '\0';
  480. tmpStr = (uint8_t *)strchr((char *)cmd_line, '*');
  481. if (tmpStr) *tmpStr = '\0';
  482. tmpStr = (uint8_t *)strchr((char *)cmd_line, 'M');
  483. if (tmpStr) {
  484. cmd_value = atoi((char *)(tmpStr + 1));
  485. tmpStr = (uint8_t *)strchr((char *)tmpStr, ' ');
  486. switch (cmd_value) {
  487. case 20: // M20: Print SD / µdisk file
  488. file_writer.fileTransfer = 0;
  489. if (uiCfg.print_state == IDLE) {
  490. int index = 0;
  491. if (tmpStr == 0) {
  492. gCfgItems.fileSysType = FILE_SYS_SD;
  493. send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n"));
  494. get_file_list((char *)"0:/");
  495. send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n"));
  496. SEND_OK_TO_WIFI;
  497. break;
  498. }
  499. while (tmpStr[index] == ' ') index++;
  500. if (gCfgItems.wifi_type == ESP_WIFI) {
  501. char *path = (char *)tempBuf;
  502. if (strlen((char *)&tmpStr[index]) < 80) {
  503. send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n"));
  504. if (strncmp((char *)&tmpStr[index], "1:", 2) == 0)
  505. gCfgItems.fileSysType = FILE_SYS_SD;
  506. else if (strncmp((char *)&tmpStr[index], "0:", 2) == 0)
  507. gCfgItems.fileSysType = FILE_SYS_USB;
  508. strcpy((char *)path, (char *)&tmpStr[index]);
  509. get_file_list(path);
  510. send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n"));
  511. }
  512. SEND_OK_TO_WIFI;
  513. }
  514. }
  515. break;
  516. case 21:
  517. /*init sd card*/
  518. SEND_OK_TO_WIFI;
  519. break;
  520. case 23:
  521. /*select the file*/
  522. if (uiCfg.print_state == IDLE) {
  523. int index = 0;
  524. while (tmpStr[index] == ' ') index++;
  525. if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) {
  526. if (strlen((char *)&tmpStr[index]) < 80) {
  527. ZERO(list_file.file_name[sel_id]);
  528. ZERO(list_file.long_name[sel_id]);
  529. uint8_t has_path_selected = 0;
  530. if (gCfgItems.wifi_type == ESP_WIFI) {
  531. if (strncmp_P((char *)&tmpStr[index], PSTR("1:"), 2) == 0) {
  532. gCfgItems.fileSysType = FILE_SYS_SD;
  533. has_path_selected = 1;
  534. }
  535. else if (strncmp_P((char *)&tmpStr[index], PSTR("0:"), 2) == 0) {
  536. gCfgItems.fileSysType = FILE_SYS_USB;
  537. has_path_selected = 1;
  538. }
  539. else if (tmpStr[index] != '/')
  540. strcat_P((char *)list_file.file_name[sel_id], PSTR("/"));
  541. if (file_writer.fileTransfer == 1) {
  542. char dosName[FILENAME_LENGTH];
  543. uint8_t fileName[sizeof(list_file.file_name[sel_id])];
  544. fileName[0] = '\0';
  545. if (has_path_selected == 1) {
  546. strcat((char *)fileName, (char *)&tmpStr[index + 3]);
  547. strcat_P((char *)list_file.file_name[sel_id], PSTR("/"));
  548. }
  549. else strcat((char *)fileName, (char *)&tmpStr[index]);
  550. if (!longName2DosName((const char *)fileName, dosName))
  551. strcpy_P(list_file.file_name[sel_id], PSTR("notValid"));
  552. strcat((char *)list_file.file_name[sel_id], dosName);
  553. strcat((char *)list_file.long_name[sel_id], dosName);
  554. }
  555. else {
  556. strcat((char *)list_file.file_name[sel_id], (char *)&tmpStr[index]);
  557. strcat((char *)list_file.long_name[sel_id], (char *)&tmpStr[index]);
  558. }
  559. }
  560. else
  561. strcpy(list_file.file_name[sel_id], (char *)&tmpStr[index]);
  562. char *cur_name=strrchr(list_file.file_name[sel_id],'/');
  563. card.openFileRead(cur_name);
  564. if (card.isFileOpen())
  565. send_to_wifi((uint8_t *)"File selected\r\n", strlen("File selected\r\n"));
  566. else {
  567. send_to_wifi((uint8_t *)"file.open failed\r\n", strlen("file.open failed\r\n"));
  568. strcpy_P(list_file.file_name[sel_id], PSTR("notValid"));
  569. }
  570. SEND_OK_TO_WIFI;
  571. }
  572. }
  573. }
  574. break;
  575. case 24:
  576. if (strcmp_P(list_file.file_name[sel_id], PSTR("notValid")) != 0) {
  577. if (uiCfg.print_state == IDLE) {
  578. clear_cur_ui();
  579. reset_print_time();
  580. start_print_time();
  581. preview_gcode_prehandle(list_file.file_name[sel_id]);
  582. uiCfg.print_state = WORKING;
  583. lv_draw_printing();
  584. #if ENABLED(SDSUPPORT)
  585. if (!gcode_preview_over) {
  586. char *cur_name = strrchr(list_file.file_name[sel_id], '/');
  587. SdFile file;
  588. SdFile *curDir;
  589. card.abortFilePrintNow();
  590. const char * const fname = card.diveToFile(false, curDir, cur_name);
  591. if (!fname) return;
  592. if (file.open(curDir, fname, O_READ)) {
  593. gCfgItems.curFilesize = file.fileSize();
  594. file.close();
  595. update_spi_flash();
  596. }
  597. card.openFileRead(cur_name);
  598. if (card.isFileOpen()) {
  599. //saved_feedrate_percentage = feedrate_percentage;
  600. feedrate_percentage = 100;
  601. planner.flow_percentage[0] = 100;
  602. planner.e_factor[0] = planner.flow_percentage[0] * 0.01f;
  603. #if EXTRUDERS == 2
  604. planner.flow_percentage[1] = 100;
  605. planner.e_factor[1] = planner.flow_percentage[1] * 0.01f;
  606. #endif
  607. card.startOrResumeFilePrinting();
  608. TERN_(POWER_LOSS_RECOVERY, recovery.prepare());
  609. once_flag = false;
  610. }
  611. }
  612. #endif
  613. }
  614. else if (uiCfg.print_state == PAUSED) {
  615. uiCfg.print_state = RESUMING;
  616. clear_cur_ui();
  617. start_print_time();
  618. if (gCfgItems.from_flash_pic)
  619. flash_preview_begin = true;
  620. else
  621. default_preview_flg = true;
  622. lv_draw_printing();
  623. }
  624. else if (uiCfg.print_state == REPRINTING) {
  625. uiCfg.print_state = REPRINTED;
  626. clear_cur_ui();
  627. start_print_time();
  628. if (gCfgItems.from_flash_pic)
  629. flash_preview_begin = true;
  630. else
  631. default_preview_flg = true;
  632. lv_draw_printing();
  633. }
  634. }
  635. SEND_OK_TO_WIFI;
  636. break;
  637. case 25:
  638. /*pause print file*/
  639. if (uiCfg.print_state == WORKING) {
  640. stop_print_time();
  641. clear_cur_ui();
  642. #if ENABLED(SDSUPPORT)
  643. card.pauseSDPrint();
  644. uiCfg.print_state = PAUSING;
  645. #endif
  646. if (gCfgItems.from_flash_pic)
  647. flash_preview_begin = true;
  648. else
  649. default_preview_flg = true;
  650. lv_draw_printing();
  651. SEND_OK_TO_WIFI;
  652. }
  653. break;
  654. case 26:
  655. /*stop print file*/
  656. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED) || (uiCfg.print_state == REPRINTING)) {
  657. stop_print_time();
  658. clear_cur_ui();
  659. #if ENABLED(SDSUPPORT)
  660. uiCfg.print_state = IDLE;
  661. card.abortFilePrintSoon();
  662. #endif
  663. lv_draw_ready_print();
  664. SEND_OK_TO_WIFI;
  665. }
  666. break;
  667. case 27:
  668. /*report print rate*/
  669. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)|| (uiCfg.print_state == REPRINTING)) {
  670. print_rate = uiCfg.totalSend;
  671. ZERO(tempBuf);
  672. sprintf_P((char *)tempBuf, PSTR("M27 %d\r\n"), print_rate);
  673. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  674. }
  675. break;
  676. case 28:
  677. /*begin to transfer file to filesys*/
  678. if (uiCfg.print_state == IDLE) {
  679. int index = 0;
  680. while (tmpStr[index] == ' ') index++;
  681. if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) {
  682. strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
  683. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  684. ZERO(tempBuf);
  685. sprintf_P((char *)tempBuf, PSTR("%s"), file_writer.saveFileName);
  686. }
  687. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  688. ZERO(tempBuf);
  689. sprintf_P((char *)tempBuf, PSTR("%s"), (char *)file_writer.saveFileName);
  690. }
  691. mount_file_sys(gCfgItems.fileSysType);
  692. #if ENABLED(SDSUPPORT)
  693. char *cur_name = strrchr(list_file.file_name[sel_id], '/');
  694. card.openFileWrite(cur_name);
  695. if (card.isFileOpen()) {
  696. ZERO(file_writer.saveFileName);
  697. strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
  698. ZERO(tempBuf);
  699. sprintf_P((char *)tempBuf, PSTR("Writing to file: %s\r\n"), (char *)file_writer.saveFileName);
  700. wifi_ret_ack();
  701. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  702. wifi_link_state = WIFI_WAIT_TRANS_START;
  703. }
  704. else {
  705. wifi_link_state = WIFI_CONNECTED;
  706. clear_cur_ui();
  707. lv_draw_dialog(DIALOG_TRANSFER_NO_DEVICE);
  708. }
  709. #endif
  710. }
  711. }
  712. break;
  713. case 105:
  714. case 991:
  715. ZERO(tempBuf);
  716. if (cmd_value == 105) {
  717. SEND_OK_TO_WIFI;
  718. char *outBuf = (char *)tempBuf;
  719. char tbuf[34];
  720. sprintf_P(tbuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0));
  721. const int tlen = strlen(tbuf);
  722. sprintf_P(outBuf, PSTR("T:%s"), tbuf);
  723. outBuf += 2 + tlen;
  724. strcpy_P(outBuf, PSTR(" B:"));
  725. outBuf += 3;
  726. #if HAS_HEATED_BED
  727. sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegBed(), thermalManager.degTargetBed());
  728. #else
  729. strcpy_P(outBuf, PSTR("0 /0"));
  730. #endif
  731. outBuf += 4;
  732. strcat_P(outBuf, PSTR(" T0:"));
  733. strcat(outBuf, tbuf);
  734. outBuf += 4 + tlen;
  735. strcat_P(outBuf, PSTR(" T1:"));
  736. outBuf += 4;
  737. #if HAS_MULTI_HOTEND
  738. sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1));
  739. #else
  740. strcat_P(outBuf, PSTR("0 /0"));
  741. #endif
  742. outBuf += 4;
  743. strcat_P(outBuf, PSTR(" @:0 B@:0\r\n"));
  744. }
  745. else {
  746. sprintf_P((char *)tempBuf, PSTR("T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n"),
  747. thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0),
  748. #if HAS_HEATED_BED
  749. thermalManager.wholeDegBed(), thermalManager.degTargetBed(),
  750. #else
  751. 0, 0,
  752. #endif
  753. thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0),
  754. #if HAS_MULTI_HOTEND
  755. thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1)
  756. #else
  757. 0, 0
  758. #endif
  759. );
  760. }
  761. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  762. queue.enqueue_one_P(PSTR("M105"));
  763. break;
  764. case 992:
  765. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
  766. ZERO(tempBuf);
  767. sprintf_P((char *)tempBuf, PSTR("M992 %d%d:%d%d:%d%d\r\n"), print_time.hours/10, print_time.hours%10, print_time.minutes/10, print_time.minutes%10, print_time.seconds/10, print_time.seconds%10);
  768. wifi_ret_ack();
  769. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  770. }
  771. break;
  772. case 994:
  773. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
  774. ZERO(tempBuf);
  775. if (strlen((char *)list_file.file_name[sel_id]) > (100 - 1)) return;
  776. sprintf_P((char *)tempBuf, PSTR("M994 %s;%d\n"), list_file.file_name[sel_id], (int)gCfgItems.curFilesize);
  777. wifi_ret_ack();
  778. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  779. }
  780. break;
  781. case 997:
  782. if (uiCfg.print_state == IDLE) {
  783. wifi_ret_ack();
  784. send_to_wifi((uint8_t *)"M997 IDLE\r\n", strlen("M997 IDLE\r\n"));
  785. }
  786. else if (uiCfg.print_state == WORKING) {
  787. wifi_ret_ack();
  788. send_to_wifi((uint8_t *)"M997 PRINTING\r\n", strlen("M997 PRINTING\r\n"));
  789. }
  790. else if (uiCfg.print_state == PAUSED) {
  791. wifi_ret_ack();
  792. send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
  793. }
  794. else if (uiCfg.print_state == REPRINTING) {
  795. wifi_ret_ack();
  796. send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
  797. }
  798. if (!uiCfg.command_send) get_wifi_list_command_send();
  799. break;
  800. case 998:
  801. if (uiCfg.print_state == IDLE) {
  802. int v = atoi((char *)tmpStr);
  803. if (v == 0)
  804. set_cur_file_sys(0);
  805. else if (v == 1)
  806. set_cur_file_sys(1);
  807. wifi_ret_ack();
  808. }
  809. break;
  810. case 115:
  811. ZERO(tempBuf);
  812. SEND_OK_TO_WIFI;
  813. send_to_wifi((uint8_t *)"FIRMWARE_NAME:Robin_nano\r\n", strlen("FIRMWARE_NAME:Robin_nano\r\n"));
  814. break;
  815. default:
  816. strcat_P((char *)cmd_line, PSTR("\n"));
  817. if (espGcodeFifo.wait_tick > 5) {
  818. uint32_t left;
  819. if (espGcodeFifo.r > espGcodeFifo.w)
  820. left = espGcodeFifo.r - espGcodeFifo.w - 1;
  821. else
  822. left = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
  823. if (left >= strlen((const char *)cmd_line)) {
  824. uint32_t index = 0;
  825. while (index < strlen((const char *)cmd_line)) {
  826. espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
  827. espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
  828. index++;
  829. }
  830. if (left - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
  831. SEND_OK_TO_WIFI;
  832. else
  833. need_ok_later = true;
  834. }
  835. }
  836. break;
  837. }
  838. }
  839. else {
  840. strcat_P((char *)cmd_line, PSTR("\n"));
  841. if (espGcodeFifo.wait_tick > 5) {
  842. uint32_t left_g;
  843. if (espGcodeFifo.r > espGcodeFifo.w)
  844. left_g = espGcodeFifo.r - espGcodeFifo.w - 1;
  845. else
  846. left_g = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
  847. if (left_g >= strlen((const char *)cmd_line)) {
  848. uint32_t index = 0;
  849. while (index < strlen((const char *)cmd_line)) {
  850. espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
  851. espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
  852. index++;
  853. }
  854. if (left_g - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
  855. SEND_OK_TO_WIFI;
  856. else
  857. need_ok_later = true;
  858. }
  859. }
  860. }
  861. }
  862. }
  863. static int32_t charAtArray(const uint8_t *_array, uint32_t _arrayLen, uint8_t _char) {
  864. for (uint32_t i = 0; i < _arrayLen; i++)
  865. if (*(_array + i) == _char) return i;
  866. return -1;
  867. }
  868. void get_wifi_list_command_send() {
  869. uint8_t cmd_wifi_list[] = { 0xA5, 0x07, 0x00, 0x00, 0xFC };
  870. raw_send_to_wifi(cmd_wifi_list, COUNT(cmd_wifi_list));
  871. }
  872. static void net_msg_handle(uint8_t * msg, uint16_t msgLen) {
  873. int wifiNameLen, wifiKeyLen, hostLen, id_len, ver_len;
  874. if (msgLen <= 0) return;
  875. // ip
  876. sprintf_P(ipPara.ip_addr, PSTR("%d.%d.%d.%d"), msg[0], msg[1], msg[2], msg[3]);
  877. // port
  878. // connect state
  879. switch (msg[6]) {
  880. case 0x0A: wifi_link_state = WIFI_CONNECTED; break;
  881. case 0x0E: wifi_link_state = WIFI_EXCEPTION; break;
  882. default: wifi_link_state = WIFI_NOT_CONFIG; break;
  883. }
  884. // mode
  885. wifiPara.mode = msg[7];
  886. // wifi name
  887. wifiNameLen = msg[8];
  888. wifiKeyLen = msg[9 + wifiNameLen];
  889. if (wifiNameLen < 32) {
  890. ZERO(wifiPara.ap_name);
  891. memcpy(wifiPara.ap_name, &msg[9], wifiNameLen);
  892. memset(&wifi_list.wifiConnectedName, 0, sizeof(wifi_list.wifiConnectedName));
  893. memcpy(&wifi_list.wifiConnectedName, &msg[9], wifiNameLen);
  894. // wifi key
  895. if (wifiKeyLen < 64) {
  896. ZERO(wifiPara.keyCode);
  897. memcpy(wifiPara.keyCode, &msg[10 + wifiNameLen], wifiKeyLen);
  898. }
  899. }
  900. cloud_para.state =msg[10 + wifiNameLen + wifiKeyLen];
  901. hostLen = msg[11 + wifiNameLen + wifiKeyLen];
  902. if (cloud_para.state) {
  903. if (hostLen < 96) {
  904. ZERO(cloud_para.hostUrl);
  905. memcpy(cloud_para.hostUrl, &msg[12 + wifiNameLen + wifiKeyLen], hostLen);
  906. }
  907. cloud_para.port = msg[12 + wifiNameLen + wifiKeyLen + hostLen] + (msg[13 + wifiNameLen + wifiKeyLen + hostLen] << 8);
  908. }
  909. // id
  910. id_len = msg[14 + wifiNameLen + wifiKeyLen + hostLen];
  911. if (id_len == 20) {
  912. ZERO(cloud_para.id);
  913. memcpy(cloud_para.id, (const char *)&msg[15 + wifiNameLen + wifiKeyLen + hostLen], id_len);
  914. }
  915. ver_len = msg[15 + wifiNameLen + wifiKeyLen + hostLen + id_len];
  916. if (ver_len < 20) {
  917. ZERO(wifi_firm_ver);
  918. memcpy(wifi_firm_ver, (const char *)&msg[16 + wifiNameLen + wifiKeyLen + hostLen + id_len], ver_len);
  919. }
  920. if (uiCfg.configWifi) {
  921. if ((wifiPara.mode != gCfgItems.wifi_mode_sel)
  922. || (strncmp(wifiPara.ap_name, (const char *)uiCfg.wifi_name, 32) != 0)
  923. || (strncmp(wifiPara.keyCode, (const char *)uiCfg.wifi_key, 64) != 0)) {
  924. package_to_wifi(WIFI_PARA_SET, (uint8_t *)0, 0);
  925. }
  926. else uiCfg.configWifi = false;
  927. }
  928. if (cfg_cloud_flag == 1) {
  929. if (((cloud_para.state >> 4) != (char)gCfgItems.cloud_enable)
  930. || (strncmp(cloud_para.hostUrl, (const char *)uiCfg.cloud_hostUrl, 96) != 0)
  931. || (cloud_para.port != uiCfg.cloud_port)) {
  932. package_to_wifi(WIFI_CLOUD_CFG, (uint8_t *)0, 0);
  933. }
  934. else cfg_cloud_flag = 0;
  935. }
  936. }
  937. static void wifi_list_msg_handle(uint8_t * msg, uint16_t msgLen) {
  938. int wifiNameLen,wifiMsgIdex = 1;
  939. int8_t wifi_name_is_same = 0;
  940. int8_t i, j;
  941. int8_t wifi_name_num = 0;
  942. uint8_t *str = 0;
  943. int8_t valid_name_num;
  944. if (msgLen <= 0) return;
  945. if (disp_state == KEYBOARD_UI) return;
  946. wifi_list.getNameNum = msg[0];
  947. if (wifi_list.getNameNum < 20) {
  948. uiCfg.command_send = true;
  949. ZERO(wifi_list.wifiName);
  950. wifi_name_num = wifi_list.getNameNum;
  951. valid_name_num = 0;
  952. str = wifi_list.wifiName[0];
  953. if (wifi_list.getNameNum > 0) wifi_list.currentWifipage = 1;
  954. for (i = 0; i < wifi_list.getNameNum; i++) {
  955. wifiNameLen = msg[wifiMsgIdex++];
  956. if (wifiNameLen < 32) {
  957. memset(str, 0, WIFI_NAME_BUFFER_SIZE);
  958. memcpy(str, &msg[wifiMsgIdex], wifiNameLen);
  959. for (j = 0; j < valid_name_num; j++) {
  960. if (strcmp((const char *)str, (const char *)wifi_list.wifiName[j]) == 0) {
  961. wifi_name_is_same = 1;
  962. break;
  963. }
  964. }
  965. if (wifi_name_is_same != 1 && str[0] > 0x80)
  966. wifi_name_is_same = 1;
  967. if (wifi_name_is_same == 1) {
  968. wifi_name_is_same = 0;
  969. wifiMsgIdex += wifiNameLen;
  970. wifiMsgIdex += 1;
  971. wifi_name_num--;
  972. //i--;
  973. continue;
  974. }
  975. if (i < WIFI_TOTAL_NUMBER - 1)
  976. str = wifi_list.wifiName[++valid_name_num];
  977. }
  978. wifiMsgIdex += wifiNameLen;
  979. wifi_list.RSSI[i] = msg[wifiMsgIdex++];
  980. }
  981. wifi_list.getNameNum = wifi_name_num;
  982. wifi_list.getPage = wifi_list.getNameNum / NUMBER_OF_PAGE + ((wifi_list.getNameNum % NUMBER_OF_PAGE) != 0);
  983. wifi_list.nameIndex = 0;
  984. if (disp_state == WIFI_LIST_UI) disp_wifi_list();
  985. }
  986. }
  987. static void gcode_msg_handle(uint8_t * msg, uint16_t msgLen) {
  988. uint8_t gcodeBuf[100] = { 0 };
  989. char *index_s, *index_e;
  990. if (msgLen <= 0) return;
  991. index_s = (char *)msg;
  992. index_e = (char *)strchr((char *)msg, '\n');
  993. if (*msg == 'N') {
  994. index_s = (char *)strchr((char *)msg, ' ');
  995. while (*index_s == ' ') index_s++;
  996. }
  997. while ((index_e != 0) && ((int)index_s < (int)index_e)) {
  998. if ((int)(index_e - index_s) < (int)sizeof(gcodeBuf)) {
  999. ZERO(gcodeBuf);
  1000. memcpy(gcodeBuf, index_s, index_e - index_s + 1);
  1001. wifi_gcode_exec(gcodeBuf);
  1002. }
  1003. while ((*index_e == '\r') || (*index_e == '\n')) index_e++;
  1004. index_s = index_e;
  1005. index_e = (char *)strchr(index_s, '\n');
  1006. }
  1007. }
  1008. void utf8_2_unicode(uint8_t *source, uint8_t Len) {
  1009. uint8_t i = 0, char_i = 0, char_byte_num = 0;
  1010. uint16_t u16_h, u16_m, u16_l, u16_value;
  1011. uint8_t FileName_unicode[30];
  1012. ZERO(FileName_unicode);
  1013. while (1) {
  1014. char_byte_num = source[i] & 0xF0;
  1015. if (source[i] < 0x80) {
  1016. //ASCII --1byte
  1017. FileName_unicode[char_i] = source[i];
  1018. i += 1;
  1019. char_i += 1;
  1020. }
  1021. else if (char_byte_num == 0xC0 || char_byte_num == 0xD0) {
  1022. //--2byte
  1023. u16_h = (((uint16_t)source[i] << 8) & 0x1F00) >> 2;
  1024. u16_l = ((uint16_t)source[i + 1] & 0x003F);
  1025. u16_value = (u16_h | u16_l);
  1026. FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
  1027. FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
  1028. i += 2;
  1029. char_i += 2;
  1030. }
  1031. else if (char_byte_num == 0xE0) {
  1032. //--3byte
  1033. u16_h = (((uint16_t)source[i] << 8) & 0x0F00) << 4;
  1034. u16_m = (((uint16_t)source[i + 1] << 8) & 0x3F00) >> 2;
  1035. u16_l = ((uint16_t)source[i + 2] & 0x003F);
  1036. u16_value = (u16_h | u16_m | u16_l);
  1037. FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
  1038. FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
  1039. i += 3;
  1040. char_i += 2;
  1041. }
  1042. else if (char_byte_num == 0xF0) {
  1043. //--4byte
  1044. i += 4;
  1045. //char_i += 3;
  1046. }
  1047. else
  1048. break;
  1049. if (i >= Len || i >= 255) break;
  1050. }
  1051. COPY(source, FileName_unicode);
  1052. }
  1053. static void file_first_msg_handle(uint8_t * msg, uint16_t msgLen) {
  1054. uint8_t fileNameLen = *msg;
  1055. if (msgLen != fileNameLen + 5) return;
  1056. file_writer.fileLen = *((uint32_t *)(msg + 1));
  1057. ZERO(file_writer.saveFileName);
  1058. memcpy(file_writer.saveFileName, msg + 5, fileNameLen);
  1059. utf8_2_unicode(file_writer.saveFileName,fileNameLen);
  1060. ZERO(public_buf);
  1061. if (strlen((const char *)file_writer.saveFileName) > sizeof(saveFilePath))
  1062. return;
  1063. ZERO(saveFilePath);
  1064. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  1065. TERN_(SDSUPPORT, card.mount());
  1066. }
  1067. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  1068. }
  1069. file_writer.write_index = 0;
  1070. lastFragment = -1;
  1071. wifiTransError.flag = 0;
  1072. wifiTransError.start_tick = 0;
  1073. wifiTransError.now_tick = 0;
  1074. TERN_(SDSUPPORT, card.closefile());
  1075. wifi_delay(1000);
  1076. #if ENABLED(SDSUPPORT)
  1077. char dosName[FILENAME_LENGTH];
  1078. if (!longName2DosName((const char *)file_writer.saveFileName, dosName)) {
  1079. clear_cur_ui();
  1080. upload_result = 2;
  1081. wifiTransError.flag = 1;
  1082. wifiTransError.start_tick = getWifiTick();
  1083. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1084. return;
  1085. }
  1086. strcpy((char *)saveFilePath, dosName);
  1087. card.cdroot();
  1088. upload_file.close();
  1089. const char * const fname = card.diveToFile(false, upload_curDir, saveFilePath);
  1090. if (!upload_file.open(upload_curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  1091. clear_cur_ui();
  1092. upload_result = 2;
  1093. wifiTransError.flag = 1;
  1094. wifiTransError.start_tick = getWifiTick();
  1095. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1096. return;
  1097. }
  1098. #endif // SDSUPPORT
  1099. wifi_link_state = WIFI_TRANS_FILE;
  1100. upload_result = 1;
  1101. clear_cur_ui();
  1102. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1103. lv_task_handler();
  1104. file_writer.tick_begin = getWifiTick();
  1105. file_writer.fileTransfer = 1;
  1106. }
  1107. #define FRAG_MASK ~_BV32(31)
  1108. static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) {
  1109. uint32_t frag = *((uint32_t *)msg);
  1110. if ((frag & FRAG_MASK) != (uint32_t)(lastFragment + 1)) {
  1111. ZERO(public_buf);
  1112. file_writer.write_index = 0;
  1113. wifi_link_state = WIFI_CONNECTED;
  1114. upload_result = 2;
  1115. }
  1116. else {
  1117. if (write_to_file((char *)msg + 4, msgLen - 4) < 0) {
  1118. ZERO(public_buf);
  1119. file_writer.write_index = 0;
  1120. wifi_link_state = WIFI_CONNECTED;
  1121. upload_result = 2;
  1122. return;
  1123. }
  1124. lastFragment = frag;
  1125. if ((frag & (~FRAG_MASK)) != 0) {
  1126. int res = upload_file.write(public_buf, file_writer.write_index);
  1127. if (res == -1) {
  1128. upload_file.close();
  1129. const char * const fname = card.diveToFile(false, upload_curDir, saveFilePath);
  1130. if (upload_file.open(upload_curDir, fname, O_WRITE)) {
  1131. upload_file.setpos(&pos);
  1132. res = upload_file.write(public_buf, file_writer.write_index);
  1133. }
  1134. }
  1135. upload_file.close();
  1136. SdFile file, *curDir;
  1137. const char * const fname = card.diveToFile(false, curDir, saveFilePath);
  1138. if (file.open(curDir, fname, O_RDWR)) {
  1139. gCfgItems.curFilesize = file.fileSize();
  1140. file.close();
  1141. }
  1142. else {
  1143. ZERO(public_buf);
  1144. file_writer.write_index = 0;
  1145. wifi_link_state = WIFI_CONNECTED;
  1146. upload_result = 2;
  1147. return;
  1148. }
  1149. ZERO(public_buf);
  1150. file_writer.write_index = 0;
  1151. file_writer.tick_end = getWifiTick();
  1152. upload_time = getWifiTickDiff(file_writer.tick_begin, file_writer.tick_end) / 1000;
  1153. upload_size = gCfgItems.curFilesize;
  1154. wifi_link_state = WIFI_CONNECTED;
  1155. upload_result = 3;
  1156. }
  1157. }
  1158. }
  1159. void esp_data_parser(char *cmdRxBuf, int len) {
  1160. int32_t head_pos, tail_pos;
  1161. uint16_t cpyLen;
  1162. int16_t leftLen = len;
  1163. bool loop_again = false;
  1164. ESP_PROTOC_FRAME esp_frame;
  1165. while (leftLen > 0 || loop_again) {
  1166. loop_again = false;
  1167. if (esp_msg_index != 0) {
  1168. head_pos = 0;
  1169. cpyLen = (leftLen < (int16_t)((sizeof(esp_msg_buf) - esp_msg_index)) ? leftLen : sizeof(esp_msg_buf) - esp_msg_index);
  1170. memcpy(&esp_msg_buf[esp_msg_index], cmdRxBuf + len - leftLen, cpyLen);
  1171. esp_msg_index += cpyLen;
  1172. leftLen = leftLen - cpyLen;
  1173. tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
  1174. if (tail_pos == -1) {
  1175. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1176. ZERO(esp_msg_buf);
  1177. esp_msg_index = 0;
  1178. }
  1179. return;
  1180. }
  1181. }
  1182. else {
  1183. head_pos = charAtArray((uint8_t const *)&cmdRxBuf[len - leftLen], leftLen, ESP_PROTOC_HEAD);
  1184. if (head_pos == -1) return;
  1185. ZERO(esp_msg_buf);
  1186. memcpy(esp_msg_buf, &cmdRxBuf[len - leftLen + head_pos], leftLen - head_pos);
  1187. esp_msg_index = leftLen - head_pos;
  1188. leftLen = 0;
  1189. head_pos = 0;
  1190. tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
  1191. if (tail_pos == -1) {
  1192. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1193. ZERO(esp_msg_buf);
  1194. esp_msg_index = 0;
  1195. }
  1196. return;
  1197. }
  1198. }
  1199. esp_frame.type = esp_msg_buf[1];
  1200. if ( esp_frame.type != ESP_TYPE_NET
  1201. && esp_frame.type != ESP_TYPE_GCODE
  1202. && esp_frame.type != ESP_TYPE_FILE_FIRST
  1203. && esp_frame.type != ESP_TYPE_FILE_FRAGMENT
  1204. && esp_frame.type != ESP_TYPE_WIFI_LIST
  1205. ) {
  1206. ZERO(esp_msg_buf);
  1207. esp_msg_index = 0;
  1208. return;
  1209. }
  1210. esp_frame.dataLen = esp_msg_buf[2] + (esp_msg_buf[3] << 8);
  1211. if ((int)(4 + esp_frame.dataLen) > (int)(sizeof(esp_msg_buf))) {
  1212. ZERO(esp_msg_buf);
  1213. esp_msg_index = 0;
  1214. return;
  1215. }
  1216. if (esp_msg_buf[4 + esp_frame.dataLen] != ESP_PROTOC_TAIL) {
  1217. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1218. ZERO(esp_msg_buf);
  1219. esp_msg_index = 0;
  1220. }
  1221. return;
  1222. }
  1223. esp_frame.data = &esp_msg_buf[4];
  1224. switch (esp_frame.type) {
  1225. case ESP_TYPE_NET:
  1226. net_msg_handle(esp_frame.data, esp_frame.dataLen);
  1227. break;
  1228. case ESP_TYPE_GCODE:
  1229. gcode_msg_handle(esp_frame.data, esp_frame.dataLen);
  1230. break;
  1231. case ESP_TYPE_FILE_FIRST:
  1232. file_first_msg_handle(esp_frame.data, esp_frame.dataLen);
  1233. break;
  1234. case ESP_TYPE_FILE_FRAGMENT:
  1235. file_fragment_msg_handle(esp_frame.data, esp_frame.dataLen);
  1236. break;
  1237. case ESP_TYPE_WIFI_LIST:
  1238. wifi_list_msg_handle(esp_frame.data, esp_frame.dataLen);
  1239. break;
  1240. default: break;
  1241. }
  1242. esp_msg_index = cut_msg_head(esp_msg_buf, esp_msg_index, esp_frame.dataLen + 5);
  1243. if (esp_msg_index > 0) {
  1244. if (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) == -1) {
  1245. ZERO(esp_msg_buf);
  1246. esp_msg_index = 0;
  1247. return;
  1248. }
  1249. if ((charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) != -1) && (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL) != -1))
  1250. loop_again = true;
  1251. }
  1252. }
  1253. }
  1254. int32_t tick_net_time1, tick_net_time2;
  1255. int32_t readWifiFifo(uint8_t *retBuf, uint32_t bufLen) {
  1256. unsigned char tmpR = wifiDmaRcvFifo.read_cur;
  1257. if (bufLen >= UDISKBUFLEN && wifiDmaRcvFifo.state[tmpR] == udisk_buf_full) {
  1258. memcpy(retBuf, (unsigned char *)wifiDmaRcvFifo.bufferAddr[tmpR], UDISKBUFLEN);
  1259. wifiDmaRcvFifo.state[tmpR] = udisk_buf_empty;
  1260. wifiDmaRcvFifo.read_cur = (tmpR + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
  1261. return UDISKBUFLEN;
  1262. }
  1263. return 0;
  1264. }
  1265. void stopEspTransfer() {
  1266. if (wifi_link_state == WIFI_TRANS_FILE)
  1267. wifi_link_state = WIFI_CONNECTED;
  1268. TERN_(SDSUPPORT, card.closefile());
  1269. if (upload_result != 3) {
  1270. wifiTransError.flag = 1;
  1271. wifiTransError.start_tick = getWifiTick();
  1272. card.removeFile((const char *)saveFilePath);
  1273. }
  1274. wifi_delay(200);
  1275. WIFI_IO1_SET();
  1276. // disable dma
  1277. dma_clear_isr_bits(DMA1, DMA_CH5);
  1278. bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 0);
  1279. dma_disable(DMA1, DMA_CH5);
  1280. wifi_delay(200);
  1281. changeFlashMode(true); // Set SPI flash to use DMA mode
  1282. esp_port_begin(1);
  1283. wifi_delay(200);
  1284. W25QXX.init(SPI_QUARTER_SPEED);
  1285. TERN_(HAS_TFT_LVGL_UI_SPI, SPI_TFT.spi_init(SPI_FULL_SPEED));
  1286. TERN_(HAS_SERVOS, servo_init());
  1287. TERN_(HAS_Z_SERVO_PROBE, probe.servo_probe_init());
  1288. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1289. }
  1290. void wifi_rcv_handle() {
  1291. int32_t len = 0;
  1292. uint8_t ucStr[(UART_RX_BUFFER_SIZE) + 1] = {0};
  1293. int8_t getDataF = 0;
  1294. if (wifi_link_state == WIFI_TRANS_FILE) {
  1295. #if 0
  1296. if (WIFISERIAL.available() == UART_RX_BUFFER_SIZE) {
  1297. for (uint16_t i=0;i<UART_RX_BUFFER_SIZE;i++) {
  1298. ucStr[i] = WIFISERIAL.read();
  1299. len++;
  1300. }
  1301. }
  1302. #else
  1303. len = readWifiFifo(ucStr, UART_RX_BUFFER_SIZE);
  1304. #endif
  1305. if (len > 0) {
  1306. esp_data_parser((char *)ucStr, len);
  1307. if (wifi_link_state == WIFI_CONNECTED) {
  1308. clear_cur_ui();
  1309. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1310. stopEspTransfer();
  1311. }
  1312. getDataF = 1;
  1313. }
  1314. if (esp_state == TRANSFER_STORE) {
  1315. if (storeRcvData(WIFISERIAL.usart_device->rb->buf, UART_RX_BUFFER_SIZE)) {
  1316. esp_state = TRANSFERING;
  1317. esp_dma_pre();
  1318. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1319. }
  1320. else
  1321. WIFI_IO1_SET();
  1322. }
  1323. }
  1324. else {
  1325. len = readWifiBuf((int8_t *)ucStr, UART_RX_BUFFER_SIZE);
  1326. if (len > 0) {
  1327. esp_data_parser((char *)ucStr, len);
  1328. if (wifi_link_state == WIFI_TRANS_FILE) {
  1329. changeFlashMode(false); // Set SPI flash to use non-DMA mode
  1330. wifi_delay(10);
  1331. esp_port_begin(0);
  1332. wifi_delay(10);
  1333. tick_net_time1 = 0;
  1334. }
  1335. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1336. getDataF = 1;
  1337. }
  1338. if (need_ok_later && !queue.ring_buffer.full()) {
  1339. need_ok_later = false;
  1340. send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n"));
  1341. }
  1342. }
  1343. if (getDataF == 1) {
  1344. tick_net_time1 = getWifiTick();
  1345. }
  1346. else {
  1347. tick_net_time2 = getWifiTick();
  1348. if (wifi_link_state == WIFI_TRANS_FILE) {
  1349. if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 8000) {
  1350. wifi_link_state = WIFI_CONNECTED;
  1351. upload_result = 2;
  1352. clear_cur_ui();
  1353. stopEspTransfer();
  1354. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1355. }
  1356. }
  1357. if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 10000)
  1358. wifi_link_state = WIFI_NOT_CONFIG;
  1359. if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 120000) {
  1360. wifi_link_state = WIFI_NOT_CONFIG;
  1361. wifi_reset();
  1362. tick_net_time1 = getWifiTick();
  1363. }
  1364. }
  1365. if (wifiTransError.flag == 0x1) {
  1366. wifiTransError.now_tick = getWifiTick();
  1367. if (getWifiTickDiff(wifiTransError.start_tick, wifiTransError.now_tick) > WAIT_ESP_TRANS_TIMEOUT_TICK) {
  1368. wifiTransError.flag = 0;
  1369. WIFI_IO1_RESET();
  1370. }
  1371. }
  1372. }
  1373. void wifi_looping() {
  1374. do {
  1375. wifi_rcv_handle();
  1376. watchdog_refresh();
  1377. } while (wifi_link_state == WIFI_TRANS_FILE);
  1378. }
  1379. void mks_esp_wifi_init() {
  1380. wifi_link_state = WIFI_NOT_CONFIG;
  1381. SET_OUTPUT(WIFI_RESET_PIN);
  1382. WIFI_SET();
  1383. SET_OUTPUT(WIFI_IO1_PIN);
  1384. SET_INPUT_PULLUP(WIFI_IO0_PIN);
  1385. WIFI_IO1_SET();
  1386. esp_state = TRANSFER_IDLE;
  1387. esp_port_begin(1);
  1388. wifi_reset();
  1389. #if 0
  1390. if (update_flag == 0) {
  1391. res = f_open(&esp_upload.uploadFile, ESP_WEB_FIRMWARE_FILE, FA_OPEN_EXISTING | FA_READ);
  1392. if (res == FR_OK) {
  1393. f_close(&esp_upload.uploadFile);
  1394. wifi_delay(2000);
  1395. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1396. return;
  1397. }
  1398. clear_cur_ui();
  1399. draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE);
  1400. if (wifi_upload(1) >= 0) {
  1401. f_unlink("1:/MKS_WIFI_CUR");
  1402. f_rename(ESP_WEB_FIRMWARE_FILE,"/MKS_WIFI_CUR");
  1403. }
  1404. draw_return_ui();
  1405. update_flag = 1;
  1406. }
  1407. }
  1408. if (update_flag == 0) {
  1409. res = f_open(&esp_upload.uploadFile, ESP_WEB_FILE, FA_OPEN_EXISTING | FA_READ);
  1410. if (res == FR_OK) {
  1411. f_close(&esp_upload.uploadFile);
  1412. wifi_delay(2000);
  1413. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1414. return;
  1415. }
  1416. clear_cur_ui();
  1417. draw_dialog(DIALOG_TYPE_UPDATE_ESP_DATA);
  1418. if (wifi_upload(2) >= 0) {
  1419. f_unlink("1:/MKS_WEB_CONTROL_CUR");
  1420. f_rename(ESP_WEB_FILE,"/MKS_WEB_CONTROL_CUR");
  1421. }
  1422. draw_return_ui();
  1423. }
  1424. }
  1425. #endif
  1426. wifiPara.decodeType = WIFI_DECODE_TYPE;
  1427. wifiPara.baud = 115200;
  1428. wifi_link_state = WIFI_NOT_CONFIG;
  1429. }
  1430. void mks_wifi_firmware_update() {
  1431. card.openFileRead((char *)ESP_FIRMWARE_FILE);
  1432. if (card.isFileOpen()) {
  1433. card.closefile();
  1434. wifi_delay(2000);
  1435. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20)
  1436. return;
  1437. clear_cur_ui();
  1438. lv_draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE);
  1439. lv_task_handler();
  1440. watchdog_refresh();
  1441. if (wifi_upload(0) >= 0) {
  1442. card.removeFile((char *)ESP_FIRMWARE_FILE_RENAME);
  1443. SdFile file, *curDir;
  1444. const char * const fname = card.diveToFile(false, curDir, ESP_FIRMWARE_FILE);
  1445. if (file.open(curDir, fname, O_READ)) {
  1446. file.rename(curDir, (char *)ESP_FIRMWARE_FILE_RENAME);
  1447. file.close();
  1448. }
  1449. }
  1450. clear_cur_ui();
  1451. }
  1452. }
  1453. #define BUF_INC_POINTER(p) ((p + 1 == UART_FIFO_BUFFER_SIZE) ? 0:(p + 1))
  1454. int usartFifoAvailable(SZ_USART_FIFO *fifo) {
  1455. return WIFISERIAL.available();
  1456. }
  1457. void get_wifi_commands() {
  1458. static char wifi_line_buffer[MAX_CMD_SIZE];
  1459. static bool wifi_comment_mode = false;
  1460. static int wifi_read_count = 0;
  1461. if (espGcodeFifo.wait_tick > 5) {
  1462. while (!queue.ring_buffer.full() && (espGcodeFifo.r != espGcodeFifo.w)) {
  1463. espGcodeFifo.wait_tick = 0;
  1464. char wifi_char = espGcodeFifo.Buffer[espGcodeFifo.r];
  1465. espGcodeFifo.r = (espGcodeFifo.r + 1) % WIFI_GCODE_BUFFER_SIZE;
  1466. /**
  1467. * If the character ends the line
  1468. */
  1469. if (wifi_char == '\n' || wifi_char == '\r') {
  1470. wifi_comment_mode = false; // end of line == end of comment
  1471. if (!wifi_read_count) continue; // skip empty lines
  1472. wifi_line_buffer[wifi_read_count] = 0; // terminate string
  1473. wifi_read_count = 0; //reset buffer
  1474. char* command = wifi_line_buffer;
  1475. while (*command == ' ') command++; // skip any leading spaces
  1476. // Movement commands alert when stopped
  1477. if (IsStopped()) {
  1478. char* gpos = strchr(command, 'G');
  1479. if (gpos) {
  1480. switch (strtol(gpos + 1, nullptr, 10)) {
  1481. case 0 ... 1:
  1482. TERN_(ARC_SUPPORT, case 2 ... 3:)
  1483. TERN_(BEZIER_CURVE_SUPPORT, case 5:)
  1484. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  1485. LCD_MESSAGEPGM(MSG_STOPPED);
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. #if DISABLED(EMERGENCY_PARSER)
  1491. // Process critical commands early
  1492. if (strcmp(command, "M108") == 0) {
  1493. wait_for_heatup = false;
  1494. TERN_(HAS_LCD_MENU, wait_for_user = false);
  1495. }
  1496. if (strcmp(command, "M112") == 0) kill(M112_KILL_STR, nullptr, true);
  1497. if (strcmp(command, "M410") == 0) quickstop_stepper();
  1498. #endif
  1499. // Add the command to the queue
  1500. queue.enqueue_one_P(wifi_line_buffer);
  1501. }
  1502. else if (wifi_read_count >= MAX_CMD_SIZE - 1) {
  1503. }
  1504. else { // it's not a newline, carriage return or escape char
  1505. if (wifi_char == ';') wifi_comment_mode = true;
  1506. if (!wifi_comment_mode) wifi_line_buffer[wifi_read_count++] = wifi_char;
  1507. }
  1508. }
  1509. } // queue has space, serial has data
  1510. else
  1511. espGcodeFifo.wait_tick++;
  1512. }
  1513. int readWifiBuf(int8_t *buf, int32_t len) {
  1514. int i = 0;
  1515. while (i < len && WIFISERIAL.available())
  1516. buf[i++] = WIFISERIAL.read();
  1517. return i;
  1518. }
  1519. #endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE