eth_worker.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. #include "eth_worker_i.h"
  2. #include "eth_worker.h"
  3. #include "eth_save_process.h"
  4. #include <furi_hal.h>
  5. #include "socket.h"
  6. #include "dhcp.h"
  7. #include "ping.h"
  8. #include "stm32wbxx_hal_gpio.h"
  9. #include "wizchip_conf.h"
  10. #define TAG "EthWorker"
  11. static EthWorker* static_worker = NULL;
  12. EthWorker* eth_worker_alloc() {
  13. EthWorker* worker = malloc(sizeof(EthWorker));
  14. worker->config = ehternet_save_process_malloc();
  15. furi_assert(worker->config);
  16. worker->init_process = ethernet_view_process_malloc(EthWorkerProcessInit, worker->config);
  17. worker->dhcp_process = ethernet_view_process_malloc(EthWorkerProcessDHCP, worker->config);
  18. worker->stat_process = ethernet_view_process_malloc(EthWorkerProcessStatic, worker->config);
  19. worker->ping_process = ethernet_view_process_malloc(EthWorkerProcessPing, worker->config);
  20. worker->reset_process = ethernet_view_process_malloc(EthWorkerProcessReset, worker->config);
  21. worker->active_process = worker->init_process;
  22. static_worker = worker;
  23. worker->state = worker->next_state = EthWorkerStateNotAllocated;
  24. eth_log(EthWorkerProcessReset, "Finik Ethernet [START]");
  25. return worker;
  26. }
  27. void eth_worker_free(EthWorker* worker) {
  28. eth_log(EthWorkerProcessReset, "Finik Ethernet [STOP]");
  29. eth_run(worker, EthWorkerProcessExit);
  30. static_worker = NULL;
  31. furi_assert(worker);
  32. ethernet_view_process_free(worker->init_process);
  33. ethernet_view_process_free(worker->dhcp_process);
  34. ethernet_view_process_free(worker->stat_process);
  35. ethernet_view_process_free(worker->ping_process);
  36. ethernet_view_process_free(worker->reset_process);
  37. ehternet_save_process_free(worker->config);
  38. free(worker);
  39. }
  40. void eth_worker_change_state(EthWorker* worker, EthWorkerState state) {
  41. furi_assert(worker);
  42. worker->state = state;
  43. }
  44. void eth_worker_set_active_process(EthWorker* worker, EthWorkerProcess state) {
  45. furi_assert(worker);
  46. switch(state) {
  47. case EthWorkerProcessInit:
  48. worker->active_process = worker->init_process;
  49. break;
  50. case EthWorkerProcessDHCP:
  51. worker->active_process = worker->dhcp_process;
  52. break;
  53. case EthWorkerProcessStatic:
  54. worker->active_process = worker->stat_process;
  55. break;
  56. case EthWorkerProcessPing:
  57. worker->active_process = worker->ping_process;
  58. break;
  59. case EthWorkerProcessReset:
  60. worker->active_process = worker->reset_process;
  61. break;
  62. }
  63. }
  64. void eth_worker_log(EthWorker* worker, const char* str) {
  65. furi_assert(worker);
  66. ehternet_save_process_print(worker->config, str);
  67. }
  68. static EthViewProcess* get_process(EthWorker* worker, EthWorkerProcess process) {
  69. furi_assert(worker);
  70. switch(process) {
  71. case EthWorkerProcessInit:
  72. return worker->init_process;
  73. case EthWorkerProcessDHCP:
  74. return worker->dhcp_process;
  75. case EthWorkerProcessStatic:
  76. return worker->stat_process;
  77. case EthWorkerProcessPing:
  78. return worker->ping_process;
  79. case EthWorkerProcessReset:
  80. return worker->reset_process;
  81. case EthWorkerProcessActive:
  82. return worker->active_process;
  83. default:
  84. NULL;
  85. }
  86. }
  87. void eth_log(EthWorkerProcess process, const char* format, ...) {
  88. furi_assert(static_worker);
  89. va_list args;
  90. va_start(args, format);
  91. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  92. const char* string = furi_string_get_cstr(fstring);
  93. va_end(args);
  94. FURI_LOG_I(TAG, "%s", string);
  95. ehternet_save_process_print(static_worker->config, string);
  96. ethernet_view_process_print(get_process(static_worker, process), string);
  97. if(process != EthWorkerProcessReset) {
  98. ethernet_view_process_print(get_process(static_worker, EthWorkerProcessReset), string);
  99. }
  100. furi_string_free(fstring);
  101. }
  102. void eth_run(EthWorker* worker, EthWorkerProcess process) {
  103. furi_assert(worker);
  104. switch(process) {
  105. case EthWorkerProcessInit:
  106. if(worker->state == EthWorkerStateNotAllocated) {
  107. worker->thread = furi_thread_alloc();
  108. furi_thread_set_name(worker->thread, "EthWorker");
  109. furi_thread_set_stack_size(worker->thread, 8192);
  110. furi_thread_set_callback(worker->thread, eth_worker_task);
  111. furi_thread_set_context(worker->thread, worker);
  112. worker->state = EthWorkerStateNotInited;
  113. worker->next_state = EthWorkerStateInit;
  114. furi_thread_start(worker->thread);
  115. }
  116. worker->state = EthWorkerStateNotInited;
  117. worker->next_state = EthWorkerStateInit;
  118. break;
  119. case EthWorkerProcessDHCP:
  120. if((uint8_t)worker->state < EthWorkerStateInited) {
  121. eth_log(EthWorkerProcessDHCP, "[error] module not inited");
  122. break;
  123. }
  124. worker->next_state = EthWorkerStateDHCP;
  125. eth_log(EthWorkerProcessDHCP, "Fuck you");
  126. break;
  127. case EthWorkerProcessStatic:
  128. if((uint8_t)worker->state < EthWorkerStateInited) {
  129. eth_log(EthWorkerProcessStatic, "[error] module not inited");
  130. break;
  131. }
  132. worker->next_state = EthWorkerStateStaticIp;
  133. eth_log(EthWorkerProcessStatic, "Fuck you");
  134. break;
  135. case EthWorkerProcessPing:
  136. if((uint8_t)worker->state < EthWorkerStateInited) {
  137. eth_log(EthWorkerProcessPing, "[error] module not inited");
  138. break;
  139. }
  140. worker->next_state = EthWorkerStatePing;
  141. eth_log(EthWorkerProcessPing, "Fuck you");
  142. break;
  143. case EthWorkerProcessReset:
  144. worker->next_state = EthWorkerStateNotInited;
  145. if((uint8_t)worker->state > EthWorkerStateNotAllocated) {
  146. worker->state = EthWorkerStateNotInited;
  147. }
  148. eth_log(EthWorkerProcessReset, "reset module");
  149. break;
  150. case EthWorkerProcessExit:
  151. if(worker->state != EthWorkerStateNotAllocated) {
  152. worker->next_state = EthWorkerStateStop;
  153. furi_thread_join(worker->thread);
  154. furi_thread_free(worker->thread);
  155. worker->state = EthWorkerStateNotAllocated;
  156. }
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. /************************** Ethernet Worker Thread *****************************/
  163. static uint8_t ip_assigned = 0;
  164. static GpioPin cspin = {.port = GPIOA, .pin = GPIO_PIN_4};
  165. static GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  166. static void W5500_Select(void) {
  167. furi_hal_gpio_write(&cspin, false);
  168. }
  169. static void W5500_Unselect(void) {
  170. furi_hal_gpio_write(&cspin, true);
  171. }
  172. static void Callback_IPAssigned(void) {
  173. eth_log(
  174. EthWorkerProcessDHCP, "Callback: IP assigned! Leased time: %d sec", getDHCPLeasetime());
  175. ip_assigned = 1;
  176. }
  177. static void Callback_IPConflict(void) {
  178. eth_log(EthWorkerProcessDHCP, "Callback: IP conflict!");
  179. }
  180. static void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  181. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  182. }
  183. static void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  184. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  185. }
  186. static uint8_t W5500_ReadByte(void) {
  187. uint8_t byte;
  188. W5500_ReadBuff(&byte, sizeof(byte));
  189. return byte;
  190. }
  191. static void W5500_WriteByte(uint8_t byte) {
  192. W5500_WriteBuff(&byte, sizeof(byte));
  193. }
  194. static void wait_ms(int ms) {
  195. furi_delay_ms(ms);
  196. }
  197. static wiz_NetInfo gWIZNETINFO;
  198. void update_WIZNETINFO(uint8_t is_dhcp) {
  199. furi_assert(static_worker);
  200. memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
  201. memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
  202. memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
  203. memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
  204. memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
  205. gWIZNETINFO.dhcp = is_dhcp ? NETINFO_DHCP : NETINFO_STATIC;
  206. }
  207. int32_t eth_worker_task(void* context) {
  208. furi_assert(context);
  209. EthWorker* worker = (EthWorker*)context;
  210. furi_hal_power_insomnia_enter();
  211. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  212. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  213. uint8_t dhcp_buffer[2000];
  214. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  215. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  216. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  217. furi_hal_gpio_write(&resetpin, true);
  218. furi_hal_gpio_write(&cspin, true);
  219. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  220. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  221. while(worker->next_state != EthWorkerStateStop) {
  222. if(worker->state == EthWorkerStateNotInited) {
  223. if(worker->next_state != EthWorkerStateInit &&
  224. worker->next_state != EthWorkerStateNotInited) {
  225. eth_log(EthWorkerProcessActive, "[error] try using not inited module");
  226. worker->next_state = EthWorkerStateNotInited;
  227. }
  228. if(worker->next_state == EthWorkerStateInit) {
  229. worker->state = EthWorkerStateInit;
  230. furi_hal_power_enable_otg();
  231. furi_delay_ms(300);
  232. furi_hal_gpio_write(&resetpin, false);
  233. furi_delay_ms(50);
  234. furi_hal_gpio_write(&resetpin, true);
  235. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  236. eth_log(EthWorkerProcessInit, "[error] W5500 init fail");
  237. worker->state = worker->next_state = EthWorkerStateNotInited;
  238. continue;
  239. }
  240. eth_log(EthWorkerProcessInit, "W5500 inited");
  241. furi_delay_ms(50);
  242. update_WIZNETINFO(false);
  243. wizchip_setnetinfo(&gWIZNETINFO);
  244. wiz_NetInfo readed_net_info;
  245. wizchip_getnetinfo(&readed_net_info);
  246. if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) {
  247. eth_log(EthWorkerProcessInit, "[error] module not detected");
  248. continue;
  249. }
  250. setSHAR(gWIZNETINFO.mac);
  251. wiz_PhyConf conf;
  252. wizphy_getphyconf(&conf);
  253. eth_log(
  254. EthWorkerProcessInit,
  255. "conf %d %d %d %d",
  256. conf.by,
  257. conf.mode,
  258. conf.speed,
  259. conf.duplex);
  260. eth_log(EthWorkerProcessInit, "net info setted");
  261. eth_log(
  262. EthWorkerProcessInit,
  263. "mac: %02X-%02X-%02X-%02X-%02X-%02X",
  264. gWIZNETINFO.mac[0],
  265. gWIZNETINFO.mac[1],
  266. gWIZNETINFO.mac[2],
  267. gWIZNETINFO.mac[3],
  268. gWIZNETINFO.mac[4],
  269. gWIZNETINFO.mac[5]);
  270. worker->state = EthWorkerStateInited;
  271. continue;
  272. }
  273. } else if(worker->state == EthWorkerStateInited) {
  274. if(worker->next_state == EthWorkerStateDHCP) {
  275. }
  276. }
  277. furi_delay_ms(50);
  278. }
  279. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  280. furi_hal_power_disable_otg();
  281. furi_hal_power_insomnia_exit();
  282. return 0;
  283. }
  284. // void eth_worker_dhcp(EthWorker* eth_worker) {
  285. // furi_assert(eth_worker);
  286. // furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  287. // uint8_t temp;
  288. // uint8_t W5500FifoSize[2][8] = {
  289. // {
  290. // 2,
  291. // 2,
  292. // 2,
  293. // 2,
  294. // 2,
  295. // 2,
  296. // 2,
  297. // 2,
  298. // },
  299. // {2, 2, 2, 2, 2, 2, 2, 2}};
  300. // uint8_t dhcp_buffer[2000];
  301. // FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
  302. // FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
  303. // reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  304. // reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  305. // reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  306. // furi_hal_gpio_write(&resetpin, true);
  307. // furi_hal_gpio_write(&cspin, true);
  308. // furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  309. // furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  310. // furi_hal_power_enable_otg();
  311. // //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  312. // furi_delay_ms(1000);
  313. // furi_hal_gpio_write(&resetpin, false);
  314. // furi_delay_ms(10);
  315. // furi_hal_gpio_write(&resetpin, true);
  316. // //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  317. // if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  318. // FURI_LOG_I(TAG, "W5500 initialized fail");
  319. // //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  320. // while(1)
  321. // ;
  322. // //break;
  323. // }
  324. // FURI_LOG_I(TAG, "W5500 initialized success");
  325. // furi_delay_ms(200);
  326. // wizchip_setnetinfo(&gWIZNETINFO);
  327. // FURI_LOG_I(TAG, "W5500 info setted 1");
  328. // setSHAR(gWIZNETINFO.mac);
  329. // FURI_LOG_I(TAG, "W5500 info setted 2");
  330. // //check phy status
  331. // do {
  332. // if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
  333. // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  334. // }
  335. // furi_delay_ms(1);
  336. // } while(temp == PHY_LINK_OFF);
  337. // FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  338. // ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  339. // furi_delay_ms(1000);
  340. // FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
  341. // reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  342. // ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
  343. // if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
  344. // DHCP_init(DHCP_SOCKET, dhcp_buffer);
  345. // uint8_t dhcp_ret = DHCP_STOPPED;
  346. // while(
  347. // !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
  348. // (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
  349. // dhcp_ret = DHCP_run();
  350. // switch(dhcp_ret) {
  351. // case DHCP_IP_ASSIGN:
  352. // case DHCP_IP_CHANGED:
  353. // case DHCP_IP_LEASED:
  354. // getIPfromDHCP(gWIZNETINFO.ip);
  355. // getGWfromDHCP(gWIZNETINFO.gw);
  356. // getSNfromDHCP(gWIZNETINFO.sn);
  357. // getDNSfromDHCP(gWIZNETINFO.dns);
  358. // gWIZNETINFO.dhcp = NETINFO_DHCP;
  359. // ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  360. // FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
  361. // break;
  362. // case DHCP_FAILED:
  363. // FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
  364. // gWIZNETINFO.dhcp = NETINFO_STATIC;
  365. // break;
  366. // }
  367. // furi_delay_ms(1);
  368. // }
  369. // wizchip_getnetinfo(&gWIZNETINFO);
  370. // FURI_LOG_I(
  371. // TAG,
  372. // "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
  373. // gWIZNETINFO.mac[0],
  374. // gWIZNETINFO.mac[1],
  375. // gWIZNETINFO.mac[2],
  376. // gWIZNETINFO.mac[3],
  377. // gWIZNETINFO.mac[4],
  378. // gWIZNETINFO.mac[5]);
  379. // if(gWIZNETINFO.dhcp == NETINFO_DHCP)
  380. // FURI_LOG_I(TAG, "DHCP\n\r");
  381. // else
  382. // FURI_LOG_I(TAG, "Static IP\n\r");
  383. // FURI_LOG_I(
  384. // TAG,
  385. // "IP address : %d.%d.%d.%d\n\r",
  386. // gWIZNETINFO.ip[0],
  387. // gWIZNETINFO.ip[1],
  388. // gWIZNETINFO.ip[2],
  389. // gWIZNETINFO.ip[3]);
  390. // FURI_LOG_I(
  391. // TAG,
  392. // "SM Mask : %d.%d.%d.%d\n\r",
  393. // gWIZNETINFO.sn[0],
  394. // gWIZNETINFO.sn[1],
  395. // gWIZNETINFO.sn[2],
  396. // gWIZNETINFO.sn[3]);
  397. // FURI_LOG_I(
  398. // TAG,
  399. // "Gate way : %d.%d.%d.%d\n\r",
  400. // gWIZNETINFO.gw[0],
  401. // gWIZNETINFO.gw[1],
  402. // gWIZNETINFO.gw[2],
  403. // gWIZNETINFO.gw[3]);
  404. // FURI_LOG_I(
  405. // TAG,
  406. // "DNS Server : %d.%d.%d.%d\n\r",
  407. // gWIZNETINFO.dns[0],
  408. // gWIZNETINFO.dns[1],
  409. // gWIZNETINFO.dns[2],
  410. // gWIZNETINFO.dns[3]);
  411. // ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
  412. // furi_delay_ms(20000);
  413. // uint8_t pDestaddr[4] = {8, 8, 8, 8};
  414. // uint8_t tmp = ping_auto(1, pDestaddr);
  415. // //tmp = ping_count(0,3,pDestaddr);
  416. // if(tmp == SUCCESS) {
  417. // ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
  418. // FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
  419. // } else {
  420. // ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
  421. // FURI_LOG_I(TAG, "----------ERROR = %d----------\r\n", tmp);
  422. // }
  423. // furi_delay_ms(3000);
  424. // furi_delay_ms(2000);
  425. // ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
  426. // }
  427. // furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  428. // }
  429. static void w5500_init() {
  430. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  431. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  432. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  433. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  434. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  435. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  436. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  437. furi_hal_gpio_write(&resetpin, true);
  438. furi_hal_gpio_write(&cspin, true);
  439. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  440. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  441. }
  442. static void w5500_deinit() {
  443. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  444. }
  445. void eth_worker_w5500(EthWorker* eth_worker) {
  446. furi_assert(eth_worker);
  447. //uint8_t temp;
  448. FURI_LOG_I(TAG, "Ehtping_Init");
  449. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  450. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  451. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  452. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  453. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  454. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  455. FURI_LOG_I(TAG, "Registered W5500 callbacks");
  456. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  457. furi_hal_gpio_write(&resetpin, true);
  458. furi_hal_gpio_write(&cspin, true);
  459. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  460. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  461. FURI_LOG_I(TAG, "GPIO inited");
  462. furi_hal_power_enable_otg();
  463. furi_delay_ms(1000);
  464. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  465. furi_delay_ms(2000);
  466. furi_hal_gpio_write(&resetpin, false);
  467. furi_delay_ms(10);
  468. furi_hal_gpio_write(&resetpin, true);
  469. FURI_LOG_I(TAG, "GPIO used");
  470. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  471. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  472. FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
  473. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  474. }
  475. FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
  476. furi_delay_ms(2000);
  477. wizchip_setnetinfo(&gWIZNETINFO);
  478. FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
  479. setSHAR(gWIZNETINFO.mac);
  480. FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
  481. //check phy status
  482. //do
  483. //{
  484. // if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
  485. // {
  486. // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  487. // }
  488. // furi_delay_ms(1);
  489. //} while (temp == PHY_LINK_OFF);
  490. //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  491. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  492. FURI_LOG_I(TAG, "W5500 before delay\r\n");
  493. furi_delay_ms(2000);
  494. FURI_LOG_I(TAG, "W5500 after delay\r\n");
  495. //furi_hal_power_disable_otg();
  496. //FURI_LOG_I(TAG, "W5500 power off\r\n");
  497. ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  498. furi_delay_ms(2000);
  499. ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
  500. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  501. }