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

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