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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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 "../../../../MarlinCore.h"
  29. #include "../../../../module/temperature.h"
  30. #include "../../../../gcode/queue.h"
  31. #include "../../../../gcode/gcode.h"
  32. #include "../../../../lcd/marlinui.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, uint8_t *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((char *)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((char *)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(true, 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 *)"Begin file list\r\n", strlen("Begin file list\r\n"));
  494. get_file_list((char *)"0:/");
  495. send_to_wifi((uint8_t *)"End file list\r\n", strlen("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 *)"Begin file list\r\n", strlen("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 *)"End file list\r\n", strlen("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. uint8_t 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], (char *)dosName);
  553. strcat((char *)list_file.long_name[sel_id], (char *)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. card.endFilePrint();
  588. SdFile file;
  589. SdFile *curDir;
  590. card.endFilePrint();
  591. const char * const fname = card.diveToFile(true, curDir, cur_name);
  592. if (!fname) return;
  593. if (file.open(curDir, fname, O_READ)) {
  594. gCfgItems.curFilesize = file.fileSize();
  595. file.close();
  596. update_spi_flash();
  597. }
  598. card.openFileRead(cur_name);
  599. if (card.isFileOpen()) {
  600. //saved_feedrate_percentage = feedrate_percentage;
  601. feedrate_percentage = 100;
  602. planner.flow_percentage[0] = 100;
  603. planner.e_factor[0] = planner.flow_percentage[0] * 0.01f;
  604. #if EXTRUDERS == 2
  605. planner.flow_percentage[1] = 100;
  606. planner.e_factor[1] = planner.flow_percentage[1] * 0.01f;
  607. #endif
  608. card.startFileprint();
  609. TERN_(POWER_LOSS_RECOVERY, recovery.prepare());
  610. once_flag = false;
  611. }
  612. }
  613. #endif
  614. }
  615. else if (uiCfg.print_state == PAUSED) {
  616. uiCfg.print_state = RESUMING;
  617. clear_cur_ui();
  618. start_print_time();
  619. if (gCfgItems.from_flash_pic)
  620. flash_preview_begin = true;
  621. else
  622. default_preview_flg = true;
  623. lv_draw_printing();
  624. }
  625. else if (uiCfg.print_state == REPRINTING) {
  626. uiCfg.print_state = REPRINTED;
  627. clear_cur_ui();
  628. start_print_time();
  629. if (gCfgItems.from_flash_pic)
  630. flash_preview_begin = true;
  631. else
  632. default_preview_flg = true;
  633. lv_draw_printing();
  634. }
  635. }
  636. SEND_OK_TO_WIFI;
  637. break;
  638. case 25:
  639. /*pause print file*/
  640. if (uiCfg.print_state == WORKING) {
  641. stop_print_time();
  642. clear_cur_ui();
  643. #if ENABLED(SDSUPPORT)
  644. card.pauseSDPrint();
  645. uiCfg.print_state = PAUSING;
  646. #endif
  647. if (gCfgItems.from_flash_pic)
  648. flash_preview_begin = true;
  649. else
  650. default_preview_flg = true;
  651. lv_draw_printing();
  652. SEND_OK_TO_WIFI;
  653. }
  654. break;
  655. case 26:
  656. /*stop print file*/
  657. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED) || (uiCfg.print_state == REPRINTING)) {
  658. stop_print_time();
  659. clear_cur_ui();
  660. #if ENABLED(SDSUPPORT)
  661. uiCfg.print_state = IDLE;
  662. card.flag.abort_sd_printing = true;
  663. #endif
  664. lv_draw_ready_print();
  665. SEND_OK_TO_WIFI;
  666. }
  667. break;
  668. case 27:
  669. /*report print rate*/
  670. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)|| (uiCfg.print_state == REPRINTING)) {
  671. print_rate = uiCfg.totalSend;
  672. ZERO(tempBuf);
  673. sprintf_P((char *)tempBuf, PSTR("M27 %d\r\n"), print_rate);
  674. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  675. }
  676. break;
  677. case 28:
  678. /*begin to transfer file to filesys*/
  679. if (uiCfg.print_state == IDLE) {
  680. int index = 0;
  681. while (tmpStr[index] == ' ') index++;
  682. if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) {
  683. strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
  684. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  685. ZERO(tempBuf);
  686. sprintf_P((char *)tempBuf, PSTR("%s"), file_writer.saveFileName);
  687. }
  688. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  689. ZERO(tempBuf);
  690. sprintf_P((char *)tempBuf, PSTR("%s"), (char *)file_writer.saveFileName);
  691. }
  692. mount_file_sys(gCfgItems.fileSysType);
  693. #if ENABLED(SDSUPPORT)
  694. char *cur_name = strrchr(list_file.file_name[sel_id], '/');
  695. card.openFileWrite(cur_name);
  696. if (card.isFileOpen()) {
  697. ZERO(file_writer.saveFileName);
  698. strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
  699. ZERO(tempBuf);
  700. sprintf_P((char *)tempBuf, PSTR("Writing to file: %s\r\n"), (char *)file_writer.saveFileName);
  701. wifi_ret_ack();
  702. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  703. wifi_link_state = WIFI_WAIT_TRANS_START;
  704. }
  705. else {
  706. wifi_link_state = WIFI_CONNECTED;
  707. clear_cur_ui();
  708. lv_draw_dialog(DIALOG_TRANSFER_NO_DEVICE);
  709. }
  710. #endif
  711. }
  712. }
  713. break;
  714. case 105:
  715. case 991:
  716. ZERO(tempBuf);
  717. if (cmd_value == 105) {
  718. SEND_OK_TO_WIFI;
  719. sprintf_P((char *)tempBuf, PSTR("T:%.1f /%.1f B:%.1f /%.1f T0:%.1f /%.1f T1:%.1f /%.1f @:0 B@:0\r\n"),
  720. (float)thermalManager.temp_hotend[0].celsius, (float)thermalManager.temp_hotend[0].target,
  721. #if HAS_HEATED_BED
  722. (float)thermalManager.temp_bed.celsius, (float)thermalManager.temp_bed.target,
  723. #else
  724. 0.0f, 0.0f,
  725. #endif
  726. (float)thermalManager.temp_hotend[0].celsius, (float)thermalManager.temp_hotend[0].target,
  727. #if DISABLED(SINGLENOZZLE) && HAS_MULTI_EXTRUDER
  728. (float)thermalManager.temp_hotend[1].celsius, (float)thermalManager.temp_hotend[1].target
  729. #else
  730. 0.0f, 0.0f
  731. #endif
  732. );
  733. }
  734. else {
  735. sprintf_P((char *)tempBuf, PSTR("T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n"),
  736. (int)thermalManager.temp_hotend[0].celsius, (int)thermalManager.temp_hotend[0].target,
  737. #if HAS_HEATED_BED
  738. (int)thermalManager.temp_bed.celsius, (int)thermalManager.temp_bed.target,
  739. #else
  740. 0, 0,
  741. #endif
  742. (int)thermalManager.temp_hotend[0].celsius, (int)thermalManager.temp_hotend[0].target,
  743. #if DISABLED(SINGLENOZZLE) && HAS_MULTI_EXTRUDER
  744. (int)thermalManager.temp_hotend[1].celsius, (int)thermalManager.temp_hotend[1].target
  745. #else
  746. 0, 0
  747. #endif
  748. );
  749. }
  750. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  751. queue.enqueue_one_P(PSTR("M105"));
  752. break;
  753. case 992:
  754. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
  755. ZERO(tempBuf);
  756. 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);
  757. wifi_ret_ack();
  758. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  759. }
  760. break;
  761. case 994:
  762. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
  763. ZERO(tempBuf);
  764. if (strlen((char *)list_file.file_name[sel_id]) > (100 - 1)) return;
  765. sprintf_P((char *)tempBuf, PSTR("M994 %s;%d\n"), list_file.file_name[sel_id], (int)gCfgItems.curFilesize);
  766. wifi_ret_ack();
  767. send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
  768. }
  769. break;
  770. case 997:
  771. if (uiCfg.print_state == IDLE) {
  772. wifi_ret_ack();
  773. send_to_wifi((uint8_t *)"M997 IDLE\r\n", strlen("M997 IDLE\r\n"));
  774. }
  775. else if (uiCfg.print_state == WORKING) {
  776. wifi_ret_ack();
  777. send_to_wifi((uint8_t *)"M997 PRINTING\r\n", strlen("M997 PRINTING\r\n"));
  778. }
  779. else if (uiCfg.print_state == PAUSED) {
  780. wifi_ret_ack();
  781. send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
  782. }
  783. else if (uiCfg.print_state == REPRINTING) {
  784. wifi_ret_ack();
  785. send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
  786. }
  787. if (!uiCfg.command_send) get_wifi_list_command_send();
  788. break;
  789. case 998:
  790. if (uiCfg.print_state == IDLE) {
  791. int v = atoi((char *)tmpStr);
  792. if (v == 0)
  793. set_cur_file_sys(0);
  794. else if (v == 1)
  795. set_cur_file_sys(1);
  796. wifi_ret_ack();
  797. }
  798. break;
  799. case 115:
  800. ZERO(tempBuf);
  801. SEND_OK_TO_WIFI;
  802. send_to_wifi((uint8_t *)"FIRMWARE_NAME:Robin_nano\r\n", strlen("FIRMWARE_NAME:Robin_nano\r\n"));
  803. break;
  804. default:
  805. strcat_P((char *)cmd_line, PSTR("\n"));
  806. if (espGcodeFifo.wait_tick > 5) {
  807. uint32_t left;
  808. if (espGcodeFifo.r > espGcodeFifo.w)
  809. left = espGcodeFifo.r - espGcodeFifo.w - 1;
  810. else
  811. left = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
  812. if (left >= strlen((const char *)cmd_line)) {
  813. uint32_t index = 0;
  814. while (index < strlen((const char *)cmd_line)) {
  815. espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
  816. espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
  817. index++;
  818. }
  819. if (left - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
  820. SEND_OK_TO_WIFI;
  821. else
  822. need_ok_later = true;
  823. }
  824. }
  825. break;
  826. }
  827. }
  828. else {
  829. strcat_P((char *)cmd_line, PSTR("\n"));
  830. if (espGcodeFifo.wait_tick > 5) {
  831. uint32_t left_g;
  832. if (espGcodeFifo.r > espGcodeFifo.w)
  833. left_g = espGcodeFifo.r - espGcodeFifo.w - 1;
  834. else
  835. left_g = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
  836. if (left_g >= strlen((const char *)cmd_line)) {
  837. uint32_t index = 0;
  838. while (index < strlen((const char *)cmd_line)) {
  839. espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
  840. espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
  841. index++;
  842. }
  843. if (left_g - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
  844. SEND_OK_TO_WIFI;
  845. else
  846. need_ok_later = true;
  847. }
  848. }
  849. }
  850. }
  851. }
  852. static int32_t charAtArray(const uint8_t *_array, uint32_t _arrayLen, uint8_t _char) {
  853. for (uint32_t i = 0; i < _arrayLen; i++)
  854. if (*(_array + i) == _char) return i;
  855. return -1;
  856. }
  857. void get_wifi_list_command_send() {
  858. uint8_t cmd_wifi_list[] = { 0xA5, 0x07, 0x00, 0x00, 0xFC };
  859. raw_send_to_wifi(cmd_wifi_list, COUNT(cmd_wifi_list));
  860. }
  861. static void net_msg_handle(uint8_t * msg, uint16_t msgLen) {
  862. int wifiNameLen, wifiKeyLen, hostLen, id_len, ver_len;
  863. if (msgLen <= 0) return;
  864. // ip
  865. sprintf_P(ipPara.ip_addr, PSTR("%d.%d.%d.%d"), msg[0], msg[1], msg[2], msg[3]);
  866. // port
  867. // connect state
  868. switch (msg[6]) {
  869. case 0x0A: wifi_link_state = WIFI_CONNECTED; break;
  870. case 0x0E: wifi_link_state = WIFI_EXCEPTION; break;
  871. default: wifi_link_state = WIFI_NOT_CONFIG; break;
  872. }
  873. // mode
  874. wifiPara.mode = msg[7];
  875. // wifi name
  876. wifiNameLen = msg[8];
  877. wifiKeyLen = msg[9 + wifiNameLen];
  878. if (wifiNameLen < 32) {
  879. ZERO(wifiPara.ap_name);
  880. memcpy(wifiPara.ap_name, &msg[9], wifiNameLen);
  881. memset(&wifi_list.wifiConnectedName, 0, sizeof(wifi_list.wifiConnectedName));
  882. memcpy(&wifi_list.wifiConnectedName, &msg[9], wifiNameLen);
  883. // wifi key
  884. if (wifiKeyLen < 64) {
  885. ZERO(wifiPara.keyCode);
  886. memcpy(wifiPara.keyCode, &msg[10 + wifiNameLen], wifiKeyLen);
  887. }
  888. }
  889. cloud_para.state =msg[10 + wifiNameLen + wifiKeyLen];
  890. hostLen = msg[11 + wifiNameLen + wifiKeyLen];
  891. if (cloud_para.state) {
  892. if (hostLen < 96) {
  893. ZERO(cloud_para.hostUrl);
  894. memcpy(cloud_para.hostUrl, &msg[12 + wifiNameLen + wifiKeyLen], hostLen);
  895. }
  896. cloud_para.port = msg[12 + wifiNameLen + wifiKeyLen + hostLen] + (msg[13 + wifiNameLen + wifiKeyLen + hostLen] << 8);
  897. }
  898. // id
  899. id_len = msg[14 + wifiNameLen + wifiKeyLen + hostLen];
  900. if (id_len == 20) {
  901. ZERO(cloud_para.id);
  902. memcpy(cloud_para.id, (const char *)&msg[15 + wifiNameLen + wifiKeyLen + hostLen], id_len);
  903. }
  904. ver_len = msg[15 + wifiNameLen + wifiKeyLen + hostLen + id_len];
  905. if (ver_len < 20) {
  906. ZERO(wifi_firm_ver);
  907. memcpy(wifi_firm_ver, (const char *)&msg[16 + wifiNameLen + wifiKeyLen + hostLen + id_len], ver_len);
  908. }
  909. if (uiCfg.configWifi) {
  910. if ((wifiPara.mode != gCfgItems.wifi_mode_sel)
  911. || (strncmp(wifiPara.ap_name, (const char *)uiCfg.wifi_name, 32) != 0)
  912. || (strncmp(wifiPara.keyCode, (const char *)uiCfg.wifi_key, 64) != 0)) {
  913. package_to_wifi(WIFI_PARA_SET, (uint8_t *)0, 0);
  914. }
  915. else uiCfg.configWifi = false;
  916. }
  917. if (cfg_cloud_flag == 1) {
  918. if (((cloud_para.state >> 4) != (char)gCfgItems.cloud_enable)
  919. || (strncmp(cloud_para.hostUrl, (const char *)uiCfg.cloud_hostUrl, 96) != 0)
  920. || (cloud_para.port != uiCfg.cloud_port)) {
  921. package_to_wifi(WIFI_CLOUD_CFG, (uint8_t *)0, 0);
  922. }
  923. else cfg_cloud_flag = 0;
  924. }
  925. }
  926. static void wifi_list_msg_handle(uint8_t * msg, uint16_t msgLen) {
  927. int wifiNameLen,wifiMsgIdex = 1;
  928. int8_t wifi_name_is_same = 0;
  929. int8_t i, j;
  930. int8_t wifi_name_num = 0;
  931. uint8_t *str = 0;
  932. int8_t valid_name_num;
  933. if (msgLen <= 0) return;
  934. if (disp_state == KEY_BOARD_UI) return;
  935. wifi_list.getNameNum = msg[0];
  936. if (wifi_list.getNameNum < 20) {
  937. uiCfg.command_send = true;
  938. ZERO(wifi_list.wifiName);
  939. wifi_name_num = wifi_list.getNameNum;
  940. valid_name_num = 0;
  941. str = wifi_list.wifiName[0];
  942. if (wifi_list.getNameNum > 0) wifi_list.currentWifipage = 1;
  943. for (i = 0; i < wifi_list.getNameNum; i++) {
  944. wifiNameLen = msg[wifiMsgIdex++];
  945. if (wifiNameLen < 32) {
  946. memset(str, 0, WIFI_NAME_BUFFER_SIZE);
  947. memcpy(str, &msg[wifiMsgIdex], wifiNameLen);
  948. for (j = 0; j < valid_name_num; j++) {
  949. if (strcmp((const char *)str, (const char *)wifi_list.wifiName[j]) == 0) {
  950. wifi_name_is_same = 1;
  951. break;
  952. }
  953. }
  954. if (wifi_name_is_same != 1 && str[0] > 0x80)
  955. wifi_name_is_same = 1;
  956. if (wifi_name_is_same == 1) {
  957. wifi_name_is_same = 0;
  958. wifiMsgIdex += wifiNameLen;
  959. wifiMsgIdex += 1;
  960. wifi_name_num--;
  961. //i--;
  962. continue;
  963. }
  964. if (i < WIFI_TOTAL_NUMBER - 1)
  965. str = wifi_list.wifiName[++valid_name_num];
  966. }
  967. wifiMsgIdex += wifiNameLen;
  968. wifi_list.RSSI[i] = msg[wifiMsgIdex++];
  969. }
  970. wifi_list.getNameNum = wifi_name_num;
  971. wifi_list.getPage = wifi_list.getNameNum / NUMBER_OF_PAGE + ((wifi_list.getNameNum % NUMBER_OF_PAGE) != 0);
  972. wifi_list.nameIndex = 0;
  973. if (disp_state == WIFI_LIST_UI) disp_wifi_list();
  974. }
  975. }
  976. static void gcode_msg_handle(uint8_t * msg, uint16_t msgLen) {
  977. uint8_t gcodeBuf[100] = { 0 };
  978. char *index_s, *index_e;
  979. if (msgLen <= 0) return;
  980. index_s = (char *)msg;
  981. index_e = (char *)strchr((char *)msg, '\n');
  982. if (*msg == 'N') {
  983. index_s = (char *)strchr((char *)msg, ' ');
  984. while (*index_s == ' ') index_s++;
  985. }
  986. while ((index_e != 0) && ((int)index_s < (int)index_e)) {
  987. if ((int)(index_e - index_s) < (int)sizeof(gcodeBuf)) {
  988. ZERO(gcodeBuf);
  989. memcpy(gcodeBuf, index_s, index_e - index_s + 1);
  990. wifi_gcode_exec(gcodeBuf);
  991. }
  992. while ((*index_e == '\r') || (*index_e == '\n')) index_e++;
  993. index_s = index_e;
  994. index_e = (char *)strchr(index_s, '\n');
  995. }
  996. }
  997. void utf8_2_unicode(uint8_t *source, uint8_t Len) {
  998. uint8_t i = 0, char_i = 0, char_byte_num = 0;
  999. uint16_t u16_h, u16_m, u16_l, u16_value;
  1000. uint8_t FileName_unicode[30];
  1001. ZERO(FileName_unicode);
  1002. while (1) {
  1003. char_byte_num = source[i] & 0xF0;
  1004. if (source[i] < 0X80) {
  1005. //ASCII --1byte
  1006. FileName_unicode[char_i] = source[i];
  1007. i += 1;
  1008. char_i += 1;
  1009. }
  1010. else if (char_byte_num == 0XC0 || char_byte_num == 0XD0) {
  1011. //--2byte
  1012. u16_h = (((uint16_t)source[i] << 8) & 0x1F00) >> 2;
  1013. u16_l = ((uint16_t)source[i + 1] & 0x003F);
  1014. u16_value = (u16_h | u16_l);
  1015. FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
  1016. FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
  1017. i += 2;
  1018. char_i += 2;
  1019. }
  1020. else if (char_byte_num == 0XE0) {
  1021. //--3byte
  1022. u16_h = (((uint16_t)source[i] << 8) & 0x0F00) << 4;
  1023. u16_m = (((uint16_t)source[i + 1] << 8) & 0x3F00) >> 2;
  1024. u16_l = ((uint16_t)source[i + 2] & 0x003F);
  1025. u16_value = (u16_h | u16_m | 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 += 3;
  1029. char_i += 2;
  1030. }
  1031. else if (char_byte_num == 0XF0) {
  1032. //--4byte
  1033. i += 4;
  1034. //char_i += 3;
  1035. }
  1036. else
  1037. break;
  1038. if (i >= Len || i >= 255) break;
  1039. }
  1040. COPY(source, FileName_unicode);
  1041. }
  1042. static void file_first_msg_handle(uint8_t * msg, uint16_t msgLen) {
  1043. uint8_t fileNameLen = *msg;
  1044. if (msgLen != fileNameLen + 5) return;
  1045. file_writer.fileLen = *((uint32_t *)(msg + 1));
  1046. ZERO(file_writer.saveFileName);
  1047. memcpy(file_writer.saveFileName, msg + 5, fileNameLen);
  1048. utf8_2_unicode(file_writer.saveFileName,fileNameLen);
  1049. ZERO(public_buf);
  1050. if (strlen((const char *)file_writer.saveFileName) > sizeof(saveFilePath))
  1051. return;
  1052. ZERO(saveFilePath);
  1053. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  1054. TERN_(SDSUPPORT, card.mount());
  1055. }
  1056. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  1057. }
  1058. file_writer.write_index = 0;
  1059. lastFragment = -1;
  1060. wifiTransError.flag = 0;
  1061. wifiTransError.start_tick = 0;
  1062. wifiTransError.now_tick = 0;
  1063. TERN_(SDSUPPORT, card.closefile());
  1064. wifi_delay(1000);
  1065. #if ENABLED(SDSUPPORT)
  1066. uint8_t dosName[FILENAME_LENGTH];
  1067. if (!longName2DosName((const char *)file_writer.saveFileName,dosName)) {
  1068. clear_cur_ui();
  1069. upload_result = 2;
  1070. wifiTransError.flag = 1;
  1071. wifiTransError.start_tick = getWifiTick();
  1072. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1073. return;
  1074. }
  1075. sprintf_P((char *)saveFilePath, PSTR("%s"), dosName);
  1076. card.cdroot();
  1077. upload_file.close();
  1078. const char * const fname = card.diveToFile(true, upload_curDir, saveFilePath);
  1079. if (!upload_file.open(upload_curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  1080. clear_cur_ui();
  1081. upload_result = 2;
  1082. wifiTransError.flag = 1;
  1083. wifiTransError.start_tick = getWifiTick();
  1084. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1085. return;
  1086. }
  1087. #endif // SDSUPPORT
  1088. wifi_link_state = WIFI_TRANS_FILE;
  1089. upload_result = 1;
  1090. clear_cur_ui();
  1091. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1092. lv_task_handler();
  1093. file_writer.tick_begin = getWifiTick();
  1094. file_writer.fileTransfer = 1;
  1095. }
  1096. #define FRAG_MASK ~_BV32(31)
  1097. static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) {
  1098. uint32_t frag = *((uint32_t *)msg);
  1099. if ((frag & FRAG_MASK) != (uint32_t)(lastFragment + 1)) {
  1100. ZERO(public_buf);
  1101. file_writer.write_index = 0;
  1102. wifi_link_state = WIFI_CONNECTED;
  1103. upload_result = 2;
  1104. }
  1105. else {
  1106. if (write_to_file((char *)msg + 4, msgLen - 4) < 0) {
  1107. ZERO(public_buf);
  1108. file_writer.write_index = 0;
  1109. wifi_link_state = WIFI_CONNECTED;
  1110. upload_result = 2;
  1111. return;
  1112. }
  1113. lastFragment = frag;
  1114. if ((frag & (~FRAG_MASK)) != 0) {
  1115. int res = upload_file.write(public_buf, file_writer.write_index);
  1116. if (res == -1) {
  1117. upload_file.close();
  1118. const char * const fname = card.diveToFile(true, upload_curDir, saveFilePath);
  1119. if (upload_file.open(upload_curDir, fname, O_WRITE)) {
  1120. upload_file.setpos(&pos);
  1121. res = upload_file.write(public_buf, file_writer.write_index);
  1122. }
  1123. }
  1124. upload_file.close();
  1125. SdFile file, *curDir;
  1126. const char * const fname = card.diveToFile(true, curDir, saveFilePath);
  1127. if (file.open(curDir, fname, O_RDWR)) {
  1128. gCfgItems.curFilesize = file.fileSize();
  1129. file.close();
  1130. }
  1131. else {
  1132. ZERO(public_buf);
  1133. file_writer.write_index = 0;
  1134. wifi_link_state = WIFI_CONNECTED;
  1135. upload_result = 2;
  1136. return;
  1137. }
  1138. ZERO(public_buf);
  1139. file_writer.write_index = 0;
  1140. file_writer.tick_end = getWifiTick();
  1141. upload_time = getWifiTickDiff(file_writer.tick_begin, file_writer.tick_end) / 1000;
  1142. upload_size = gCfgItems.curFilesize;
  1143. wifi_link_state = WIFI_CONNECTED;
  1144. upload_result = 3;
  1145. }
  1146. }
  1147. }
  1148. void esp_data_parser(char *cmdRxBuf, int len) {
  1149. int32_t head_pos, tail_pos;
  1150. uint16_t cpyLen;
  1151. int16_t leftLen = len;
  1152. bool loop_again = false;
  1153. ESP_PROTOC_FRAME esp_frame;
  1154. while (leftLen > 0 || loop_again) {
  1155. loop_again = false;
  1156. if (esp_msg_index != 0) {
  1157. head_pos = 0;
  1158. cpyLen = (leftLen < (int16_t)((sizeof(esp_msg_buf) - esp_msg_index)) ? leftLen : sizeof(esp_msg_buf) - esp_msg_index);
  1159. memcpy(&esp_msg_buf[esp_msg_index], cmdRxBuf + len - leftLen, cpyLen);
  1160. esp_msg_index += cpyLen;
  1161. leftLen = leftLen - cpyLen;
  1162. tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
  1163. if (tail_pos == -1) {
  1164. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1165. ZERO(esp_msg_buf);
  1166. esp_msg_index = 0;
  1167. }
  1168. return;
  1169. }
  1170. }
  1171. else {
  1172. head_pos = charAtArray((uint8_t const *)&cmdRxBuf[len - leftLen], leftLen, ESP_PROTOC_HEAD);
  1173. if (head_pos == -1) return;
  1174. ZERO(esp_msg_buf);
  1175. memcpy(esp_msg_buf, &cmdRxBuf[len - leftLen + head_pos], leftLen - head_pos);
  1176. esp_msg_index = leftLen - head_pos;
  1177. leftLen = 0;
  1178. head_pos = 0;
  1179. tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
  1180. if (tail_pos == -1) {
  1181. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1182. ZERO(esp_msg_buf);
  1183. esp_msg_index = 0;
  1184. }
  1185. return;
  1186. }
  1187. }
  1188. esp_frame.type = esp_msg_buf[1];
  1189. if ( esp_frame.type != ESP_TYPE_NET
  1190. && esp_frame.type != ESP_TYPE_GCODE
  1191. && esp_frame.type != ESP_TYPE_FILE_FIRST
  1192. && esp_frame.type != ESP_TYPE_FILE_FRAGMENT
  1193. && esp_frame.type != ESP_TYPE_WIFI_LIST
  1194. ) {
  1195. ZERO(esp_msg_buf);
  1196. esp_msg_index = 0;
  1197. return;
  1198. }
  1199. esp_frame.dataLen = esp_msg_buf[2] + (esp_msg_buf[3] << 8);
  1200. if ((int)(4 + esp_frame.dataLen) > (int)(sizeof(esp_msg_buf))) {
  1201. ZERO(esp_msg_buf);
  1202. esp_msg_index = 0;
  1203. return;
  1204. }
  1205. if (esp_msg_buf[4 + esp_frame.dataLen] != ESP_PROTOC_TAIL) {
  1206. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1207. ZERO(esp_msg_buf);
  1208. esp_msg_index = 0;
  1209. }
  1210. return;
  1211. }
  1212. esp_frame.data = &esp_msg_buf[4];
  1213. switch (esp_frame.type) {
  1214. case ESP_TYPE_NET:
  1215. net_msg_handle(esp_frame.data, esp_frame.dataLen);
  1216. break;
  1217. case ESP_TYPE_GCODE:
  1218. gcode_msg_handle(esp_frame.data, esp_frame.dataLen);
  1219. break;
  1220. case ESP_TYPE_FILE_FIRST:
  1221. file_first_msg_handle(esp_frame.data, esp_frame.dataLen);
  1222. break;
  1223. case ESP_TYPE_FILE_FRAGMENT:
  1224. file_fragment_msg_handle(esp_frame.data, esp_frame.dataLen);
  1225. break;
  1226. case ESP_TYPE_WIFI_LIST:
  1227. wifi_list_msg_handle(esp_frame.data, esp_frame.dataLen);
  1228. break;
  1229. default: break;
  1230. }
  1231. esp_msg_index = cut_msg_head(esp_msg_buf, esp_msg_index, esp_frame.dataLen + 5);
  1232. if (esp_msg_index > 0) {
  1233. if (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) == -1) {
  1234. ZERO(esp_msg_buf);
  1235. esp_msg_index = 0;
  1236. return;
  1237. }
  1238. if ((charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) != -1) && (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL) != -1))
  1239. loop_again = true;
  1240. }
  1241. }
  1242. }
  1243. int32_t tick_net_time1, tick_net_time2;
  1244. int32_t readWifiFifo(uint8_t *retBuf, uint32_t bufLen) {
  1245. unsigned char tmpR = wifiDmaRcvFifo.read_cur;
  1246. if (bufLen >= UDISKBUFLEN && wifiDmaRcvFifo.state[tmpR] == udisk_buf_full) {
  1247. memcpy(retBuf, (unsigned char *)wifiDmaRcvFifo.bufferAddr[tmpR], UDISKBUFLEN);
  1248. wifiDmaRcvFifo.state[tmpR] = udisk_buf_empty;
  1249. wifiDmaRcvFifo.read_cur = (tmpR + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
  1250. return UDISKBUFLEN;
  1251. }
  1252. return 0;
  1253. }
  1254. void stopEspTransfer() {
  1255. if (wifi_link_state == WIFI_TRANS_FILE)
  1256. wifi_link_state = WIFI_CONNECTED;
  1257. TERN_(SDSUPPORT, card.closefile());
  1258. if (upload_result != 3) {
  1259. wifiTransError.flag = 1;
  1260. wifiTransError.start_tick = getWifiTick();
  1261. card.removeFile((const char *)saveFilePath);
  1262. }
  1263. wifi_delay(200);
  1264. WIFI_IO1_SET();
  1265. // disable dma
  1266. dma_clear_isr_bits(DMA1, DMA_CH5);
  1267. bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 0);
  1268. dma_disable(DMA1, DMA_CH5);
  1269. wifi_delay(200);
  1270. changeFlashMode(true); // Set SPI flash to use DMA mode
  1271. esp_port_begin(1);
  1272. wifi_delay(200);
  1273. W25QXX.init(SPI_QUARTER_SPEED);
  1274. TERN_(HAS_TFT_LVGL_UI_SPI, SPI_TFT.spi_init(SPI_FULL_SPEED));
  1275. TERN_(HAS_SERVOS, servo_init());
  1276. TERN_(HAS_Z_SERVO_PROBE, probe.servo_probe_init());
  1277. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1278. }
  1279. void wifi_rcv_handle() {
  1280. int32_t len = 0;
  1281. uint8_t ucStr[(UART_RX_BUFFER_SIZE) + 1] = {0};
  1282. int8_t getDataF = 0;
  1283. if (wifi_link_state == WIFI_TRANS_FILE) {
  1284. #if 0
  1285. if (WIFISERIAL.available() == UART_RX_BUFFER_SIZE) {
  1286. for (uint16_t i=0;i<UART_RX_BUFFER_SIZE;i++) {
  1287. ucStr[i] = WIFISERIAL.read();
  1288. len++;
  1289. }
  1290. }
  1291. #else
  1292. len = readWifiFifo(ucStr, UART_RX_BUFFER_SIZE);
  1293. #endif
  1294. if (len > 0) {
  1295. esp_data_parser((char *)ucStr, len);
  1296. if (wifi_link_state == WIFI_CONNECTED) {
  1297. clear_cur_ui();
  1298. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1299. stopEspTransfer();
  1300. }
  1301. getDataF = 1;
  1302. }
  1303. if (esp_state == TRANSFER_STORE) {
  1304. if (storeRcvData(WIFISERIAL.usart_device->rb->buf, UART_RX_BUFFER_SIZE)) {
  1305. esp_state = TRANSFERING;
  1306. esp_dma_pre();
  1307. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1308. }
  1309. else
  1310. WIFI_IO1_SET();
  1311. }
  1312. }
  1313. else {
  1314. len = readWifiBuf((int8_t *)ucStr, UART_RX_BUFFER_SIZE);
  1315. if (len > 0) {
  1316. esp_data_parser((char *)ucStr, len);
  1317. if (wifi_link_state == WIFI_TRANS_FILE) {
  1318. changeFlashMode(false); // Set SPI flash to use non-DMA mode
  1319. wifi_delay(10);
  1320. esp_port_begin(0);
  1321. wifi_delay(10);
  1322. tick_net_time1 = 0;
  1323. }
  1324. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1325. getDataF = 1;
  1326. }
  1327. if (need_ok_later && (queue.length < BUFSIZE)) {
  1328. need_ok_later = false;
  1329. send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n"));
  1330. }
  1331. }
  1332. if (getDataF == 1) {
  1333. tick_net_time1 = getWifiTick();
  1334. }
  1335. else {
  1336. tick_net_time2 = getWifiTick();
  1337. if (wifi_link_state == WIFI_TRANS_FILE) {
  1338. if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 8000) {
  1339. wifi_link_state = WIFI_CONNECTED;
  1340. upload_result = 2;
  1341. clear_cur_ui();
  1342. stopEspTransfer();
  1343. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1344. }
  1345. }
  1346. if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 10000)
  1347. wifi_link_state = WIFI_NOT_CONFIG;
  1348. if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 120000) {
  1349. wifi_link_state = WIFI_NOT_CONFIG;
  1350. wifi_reset();
  1351. tick_net_time1 = getWifiTick();
  1352. }
  1353. }
  1354. if (wifiTransError.flag == 0x1) {
  1355. wifiTransError.now_tick = getWifiTick();
  1356. if (getWifiTickDiff(wifiTransError.start_tick, wifiTransError.now_tick) > WAIT_ESP_TRANS_TIMEOUT_TICK) {
  1357. wifiTransError.flag = 0;
  1358. WIFI_IO1_RESET();
  1359. }
  1360. }
  1361. }
  1362. void wifi_looping() {
  1363. do {
  1364. wifi_rcv_handle();
  1365. watchdog_refresh();
  1366. } while (wifi_link_state == WIFI_TRANS_FILE);
  1367. }
  1368. void mks_esp_wifi_init() {
  1369. wifi_link_state = WIFI_NOT_CONFIG;
  1370. SET_OUTPUT(WIFI_RESET_PIN);
  1371. WIFI_SET();
  1372. SET_OUTPUT(WIFI_IO1_PIN);
  1373. SET_INPUT_PULLUP(WIFI_IO0_PIN);
  1374. WIFI_IO1_SET();
  1375. esp_state = TRANSFER_IDLE;
  1376. esp_port_begin(1);
  1377. wifi_reset();
  1378. #if 0
  1379. if (update_flag == 0) {
  1380. res = f_open(&esp_upload.uploadFile, ESP_WEB_FIRMWARE_FILE, FA_OPEN_EXISTING | FA_READ);
  1381. if (res == FR_OK) {
  1382. f_close(&esp_upload.uploadFile);
  1383. wifi_delay(2000);
  1384. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1385. return;
  1386. }
  1387. clear_cur_ui();
  1388. draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE);
  1389. if (wifi_upload(1) >= 0) {
  1390. f_unlink("1:/MKS_WIFI_CUR");
  1391. f_rename(ESP_WEB_FIRMWARE_FILE,"/MKS_WIFI_CUR");
  1392. }
  1393. draw_return_ui();
  1394. update_flag = 1;
  1395. }
  1396. }
  1397. if (update_flag == 0) {
  1398. res = f_open(&esp_upload.uploadFile, ESP_WEB_FILE, FA_OPEN_EXISTING | FA_READ);
  1399. if (res == FR_OK) {
  1400. f_close(&esp_upload.uploadFile);
  1401. wifi_delay(2000);
  1402. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1403. return;
  1404. }
  1405. clear_cur_ui();
  1406. draw_dialog(DIALOG_TYPE_UPDATE_ESP_DATA);
  1407. if (wifi_upload(2) >= 0) {
  1408. f_unlink("1:/MKS_WEB_CONTROL_CUR");
  1409. f_rename(ESP_WEB_FILE,"/MKS_WEB_CONTROL_CUR");
  1410. }
  1411. draw_return_ui();
  1412. }
  1413. }
  1414. #endif
  1415. wifiPara.decodeType = WIFI_DECODE_TYPE;
  1416. wifiPara.baud = 115200;
  1417. wifi_link_state = WIFI_NOT_CONFIG;
  1418. }
  1419. void mks_wifi_firmware_update() {
  1420. card.openFileRead((char *)ESP_FIRMWARE_FILE);
  1421. if (card.isFileOpen()) {
  1422. card.closefile();
  1423. wifi_delay(2000);
  1424. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20)
  1425. return;
  1426. clear_cur_ui();
  1427. lv_draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE);
  1428. lv_task_handler();
  1429. watchdog_refresh();
  1430. if (wifi_upload(0) >= 0) {
  1431. card.removeFile((char *)ESP_FIRMWARE_FILE_RENAME);
  1432. SdFile file, *curDir;
  1433. const char * const fname = card.diveToFile(true, curDir, ESP_FIRMWARE_FILE);
  1434. if (file.open(curDir, fname, O_READ)) {
  1435. file.rename(curDir, (char *)ESP_FIRMWARE_FILE_RENAME);
  1436. file.close();
  1437. }
  1438. }
  1439. clear_cur_ui();
  1440. }
  1441. }
  1442. #define BUF_INC_POINTER(p) ((p + 1 == UART_FIFO_BUFFER_SIZE) ? 0:(p + 1))
  1443. int usartFifoAvailable(SZ_USART_FIFO *fifo) {
  1444. return WIFISERIAL.available();
  1445. }
  1446. void get_wifi_commands() {
  1447. static char wifi_line_buffer[MAX_CMD_SIZE];
  1448. static bool wifi_comment_mode = false;
  1449. static int wifi_read_count = 0;
  1450. if (espGcodeFifo.wait_tick > 5) {
  1451. while ((queue.length < BUFSIZE) && (espGcodeFifo.r != espGcodeFifo.w)) {
  1452. espGcodeFifo.wait_tick = 0;
  1453. char wifi_char = espGcodeFifo.Buffer[espGcodeFifo.r];
  1454. espGcodeFifo.r = (espGcodeFifo.r + 1) % WIFI_GCODE_BUFFER_SIZE;
  1455. /**
  1456. * If the character ends the line
  1457. */
  1458. if (wifi_char == '\n' || wifi_char == '\r') {
  1459. wifi_comment_mode = false; // end of line == end of comment
  1460. if (!wifi_read_count) continue; // skip empty lines
  1461. wifi_line_buffer[wifi_read_count] = 0; // terminate string
  1462. wifi_read_count = 0; //reset buffer
  1463. char* command = wifi_line_buffer;
  1464. while (*command == ' ') command++; // skip any leading spaces
  1465. // Movement commands alert when stopped
  1466. if (IsStopped()) {
  1467. char* gpos = strchr(command, 'G');
  1468. if (gpos) {
  1469. switch (strtol(gpos + 1, nullptr, 10)) {
  1470. case 0 ... 1:
  1471. #if ENABLED(ARC_SUPPORT)
  1472. case 2 ... 3:
  1473. #endif
  1474. #if ENABLED(BEZIER_CURVE_SUPPORT)
  1475. case 5:
  1476. #endif
  1477. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  1478. LCD_MESSAGEPGM(MSG_STOPPED);
  1479. break;
  1480. }
  1481. }
  1482. }
  1483. #if DISABLED(EMERGENCY_PARSER)
  1484. // Process critical commands early
  1485. if (strcmp(command, "M108") == 0) {
  1486. wait_for_heatup = false;
  1487. TERN_(HAS_LCD_MENU, wait_for_user = false);
  1488. }
  1489. if (strcmp(command, "M112") == 0) kill(M112_KILL_STR, nullptr, true);
  1490. if (strcmp(command, "M410") == 0) quickstop_stepper();
  1491. #endif
  1492. // Add the command to the queue
  1493. queue.enqueue_one_P(wifi_line_buffer);
  1494. }
  1495. else if (wifi_read_count >= MAX_CMD_SIZE - 1) {
  1496. }
  1497. else { // it's not a newline, carriage return or escape char
  1498. if (wifi_char == ';') wifi_comment_mode = true;
  1499. if (!wifi_comment_mode) wifi_line_buffer[wifi_read_count++] = wifi_char;
  1500. }
  1501. }
  1502. } // queue has space, serial has data
  1503. else
  1504. espGcodeFifo.wait_tick++;
  1505. }
  1506. int readWifiBuf(int8_t *buf, int32_t len) {
  1507. int i = 0;
  1508. while (i < len && WIFISERIAL.available())
  1509. buf[i++] = WIFISERIAL.read();
  1510. return i;
  1511. }
  1512. #endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE