eth_worker.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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->state = worker->next_state = EthWorkerStateNotInited;
  145. eth_log(EthWorkerProcessReset, "reset module");
  146. break;
  147. case EthWorkerProcessExit:
  148. if(worker->state != EthWorkerStateNotAllocated) {
  149. worker->next_state = EthWorkerStateStop;
  150. furi_thread_join(worker->thread);
  151. furi_thread_free(worker->thread);
  152. worker->state = EthWorkerStateNotAllocated;
  153. }
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. /************************** Ethernet Worker Thread *****************************/
  160. static uint8_t ip_assigned = 0;
  161. static GpioPin cspin = {.port = GPIOA, .pin = GPIO_PIN_4};
  162. static GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  163. static void W5500_Select(void) {
  164. furi_hal_gpio_write(&cspin, false);
  165. }
  166. static void W5500_Unselect(void) {
  167. furi_hal_gpio_write(&cspin, true);
  168. }
  169. static void Callback_IPAssigned(void) {
  170. eth_log(
  171. EthWorkerProcessDHCP, "Callback: IP assigned! Leased time: %d sec", getDHCPLeasetime());
  172. ip_assigned = 1;
  173. }
  174. static void Callback_IPConflict(void) {
  175. eth_log(EthWorkerProcessDHCP, "Callback: IP conflict!");
  176. }
  177. static void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  178. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  179. }
  180. static void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  181. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  182. }
  183. static uint8_t W5500_ReadByte(void) {
  184. uint8_t byte;
  185. W5500_ReadBuff(&byte, sizeof(byte));
  186. return byte;
  187. }
  188. static void W5500_WriteByte(uint8_t byte) {
  189. W5500_WriteBuff(&byte, sizeof(byte));
  190. }
  191. static void wait_ms(int ms) {
  192. furi_delay_ms(ms);
  193. }
  194. static wiz_NetInfo gWIZNETINFO;
  195. void update_WIZNETINFO() {
  196. furi_assert(static_worker);
  197. memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
  198. memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
  199. memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
  200. memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
  201. memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
  202. gWIZNETINFO.dhcp = NETINFO_STATIC;
  203. }
  204. int32_t eth_worker_task(void* context) {
  205. furi_assert(context);
  206. EthWorker* worker = (EthWorker*)context;
  207. furi_hal_power_insomnia_enter();
  208. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  209. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  210. uint8_t dhcp_buffer[2000];
  211. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  212. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  213. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  214. furi_hal_gpio_write(&resetpin, true);
  215. furi_hal_gpio_write(&cspin, true);
  216. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  217. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  218. while(worker->next_state != EthWorkerStateStop) {
  219. if(worker->state == EthWorkerStateNotInited) {
  220. if(worker->next_state != EthWorkerStateInit &&
  221. worker->next_state != EthWorkerStateNotInited) {
  222. eth_log(EthWorkerProcessActive, "[error] try using not inited module");
  223. worker->next_state = EthWorkerStateNotInited;
  224. }
  225. if(worker->next_state == EthWorkerStateInit) {
  226. worker->state = EthWorkerStateInit;
  227. furi_hal_power_enable_otg();
  228. furi_delay_ms(300);
  229. furi_hal_gpio_write(&resetpin, false);
  230. furi_delay_ms(50);
  231. furi_hal_gpio_write(&resetpin, true);
  232. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  233. eth_log(EthWorkerProcessInit, "[error] W5500 init fail");
  234. worker->state = worker->next_state = EthWorkerStateNotInited;
  235. continue;
  236. }
  237. eth_log(EthWorkerProcessInit, "W5500 inited");
  238. furi_delay_ms(50);
  239. update_WIZNETINFO();
  240. wizchip_setnetinfo(&gWIZNETINFO);
  241. setSHAR(gWIZNETINFO.mac);
  242. wiz_PhyConf conf;
  243. wizphy_getphyconf(&conf);
  244. eth_log(
  245. EthWorkerProcessInit,
  246. "conf %d %d %d %d",
  247. conf.by,
  248. conf.mode,
  249. conf.speed,
  250. conf.duplex);
  251. eth_log(EthWorkerProcessInit, "net info setted");
  252. eth_log(
  253. EthWorkerProcessInit,
  254. "mac: %02X-%02X-%02X-%02X-%02X-%02X",
  255. gWIZNETINFO.mac[0],
  256. gWIZNETINFO.mac[1],
  257. gWIZNETINFO.mac[2],
  258. gWIZNETINFO.mac[3],
  259. gWIZNETINFO.mac[4],
  260. gWIZNETINFO.mac[5]);
  261. worker->state = EthWorkerStateInited;
  262. continue;
  263. }
  264. } else if(worker->state == EthWorkerStateInited) {
  265. }
  266. furi_delay_ms(50);
  267. }
  268. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  269. furi_hal_power_disable_otg();
  270. furi_hal_power_insomnia_exit();
  271. return 0;
  272. }
  273. #define DHCP_SOCKET 0
  274. #define PING_SOCKET 1
  275. // MAC-àäðåñ
  276. #define ETHADDR0 0x00 // The first octet of the Ethernet address
  277. #define ETHADDR1 0x08 // The second octet of the Ethernet address
  278. #define ETHADDR2 0xDC // The third octet of the Ethernet address
  279. #define ETHADDR3 0x47 // The fourth octet of the Ethernet address
  280. #define ETHADDR4 0x47 // The fifth octet of the Ethernet address
  281. #define ETHADDR5 0x54 // The sixth octet of the Ethernet address
  282. // Ïàðàìåòðû IP (åñëè DHCP îòêëþ÷åí)
  283. #define IPADDR0 192 // The first octet of the IP address of this uIP node
  284. #define IPADDR1 168 // The second octet of the IP address of this uIP node
  285. #define IPADDR2 1 // The third octet of the IP address of this uIP node
  286. #define IPADDR3 137 // The fourth octet of the IP address of this uIP node
  287. #define NETMASK0 255 // The first octet of the netmask of this uIP node
  288. #define NETMASK1 255 // The second octet of the netmask of this uIP node
  289. #define NETMASK2 255 // The third octet of the netmask of this uIP node
  290. #define NETMASK3 0 // The fourth octet of the netmask of this uIP node
  291. #define DRIPADDR0 192 // The first octet of the IP address of the default router
  292. #define DRIPADDR1 168 // The second octet of the IP address of the default router
  293. #define DRIPADDR2 1 // The third octet of the IP address of the default router
  294. #define DRIPADDR3 1 // The fourth octet of the IP address of the default router
  295. // wiz_NetInfo gWIZNETINFO = {
  296. // .mac = {ETHADDR0, ETHADDR1, ETHADDR2, ETHADDR3, ETHADDR4, ETHADDR5},
  297. // .ip = {IPADDR0, IPADDR1, IPADDR2, IPADDR3},
  298. // .sn = {NETMASK0, NETMASK1, NETMASK2, NETMASK3},
  299. // .gw = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  300. // .dns = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  301. // .dhcp = NETINFO_DHCP // NETINFO_STATIC
  302. // };
  303. void eth_worker_dhcp(EthWorker* eth_worker) {
  304. furi_assert(eth_worker);
  305. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  306. uint8_t temp;
  307. uint8_t W5500FifoSize[2][8] = {
  308. {
  309. 2,
  310. 2,
  311. 2,
  312. 2,
  313. 2,
  314. 2,
  315. 2,
  316. 2,
  317. },
  318. {2, 2, 2, 2, 2, 2, 2, 2}};
  319. uint8_t dhcp_buffer[2000];
  320. FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
  321. FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
  322. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  323. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  324. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  325. furi_hal_gpio_write(&resetpin, true);
  326. furi_hal_gpio_write(&cspin, true);
  327. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  328. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  329. furi_hal_power_enable_otg();
  330. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  331. furi_delay_ms(1000);
  332. furi_hal_gpio_write(&resetpin, false);
  333. furi_delay_ms(10);
  334. furi_hal_gpio_write(&resetpin, true);
  335. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  336. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  337. FURI_LOG_I(TAG, "W5500 initialized fail");
  338. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  339. while(1)
  340. ;
  341. //break;
  342. }
  343. FURI_LOG_I(TAG, "W5500 initialized success");
  344. furi_delay_ms(200);
  345. wizchip_setnetinfo(&gWIZNETINFO);
  346. FURI_LOG_I(TAG, "W5500 info setted 1");
  347. setSHAR(gWIZNETINFO.mac);
  348. FURI_LOG_I(TAG, "W5500 info setted 2");
  349. //check phy status
  350. do {
  351. if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
  352. FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  353. }
  354. furi_delay_ms(1);
  355. } while(temp == PHY_LINK_OFF);
  356. FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  357. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  358. furi_delay_ms(1000);
  359. FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
  360. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  361. ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
  362. if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
  363. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  364. uint8_t dhcp_ret = DHCP_STOPPED;
  365. while(
  366. !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
  367. (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
  368. dhcp_ret = DHCP_run();
  369. switch(dhcp_ret) {
  370. case DHCP_IP_ASSIGN:
  371. case DHCP_IP_CHANGED:
  372. case DHCP_IP_LEASED:
  373. getIPfromDHCP(gWIZNETINFO.ip);
  374. getGWfromDHCP(gWIZNETINFO.gw);
  375. getSNfromDHCP(gWIZNETINFO.sn);
  376. getDNSfromDHCP(gWIZNETINFO.dns);
  377. gWIZNETINFO.dhcp = NETINFO_DHCP;
  378. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  379. FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
  380. break;
  381. case DHCP_FAILED:
  382. FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
  383. gWIZNETINFO.dhcp = NETINFO_STATIC;
  384. break;
  385. }
  386. furi_delay_ms(1);
  387. }
  388. wizchip_getnetinfo(&gWIZNETINFO);
  389. FURI_LOG_I(
  390. TAG,
  391. "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
  392. gWIZNETINFO.mac[0],
  393. gWIZNETINFO.mac[1],
  394. gWIZNETINFO.mac[2],
  395. gWIZNETINFO.mac[3],
  396. gWIZNETINFO.mac[4],
  397. gWIZNETINFO.mac[5]);
  398. if(gWIZNETINFO.dhcp == NETINFO_DHCP)
  399. FURI_LOG_I(TAG, "DHCP\n\r");
  400. else
  401. FURI_LOG_I(TAG, "Static IP\n\r");
  402. FURI_LOG_I(
  403. TAG,
  404. "IP address : %d.%d.%d.%d\n\r",
  405. gWIZNETINFO.ip[0],
  406. gWIZNETINFO.ip[1],
  407. gWIZNETINFO.ip[2],
  408. gWIZNETINFO.ip[3]);
  409. FURI_LOG_I(
  410. TAG,
  411. "SM Mask : %d.%d.%d.%d\n\r",
  412. gWIZNETINFO.sn[0],
  413. gWIZNETINFO.sn[1],
  414. gWIZNETINFO.sn[2],
  415. gWIZNETINFO.sn[3]);
  416. FURI_LOG_I(
  417. TAG,
  418. "Gate way : %d.%d.%d.%d\n\r",
  419. gWIZNETINFO.gw[0],
  420. gWIZNETINFO.gw[1],
  421. gWIZNETINFO.gw[2],
  422. gWIZNETINFO.gw[3]);
  423. FURI_LOG_I(
  424. TAG,
  425. "DNS Server : %d.%d.%d.%d\n\r",
  426. gWIZNETINFO.dns[0],
  427. gWIZNETINFO.dns[1],
  428. gWIZNETINFO.dns[2],
  429. gWIZNETINFO.dns[3]);
  430. ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
  431. furi_delay_ms(20000);
  432. uint8_t pDestaddr[4] = {8, 8, 8, 8};
  433. uint8_t tmp = ping_auto(1, pDestaddr);
  434. //tmp = ping_count(0,3,pDestaddr);
  435. if(tmp == SUCCESS) {
  436. ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
  437. FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
  438. } else {
  439. ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
  440. FURI_LOG_I(TAG, "----------ERROR = %d----------\r\n", tmp);
  441. }
  442. furi_delay_ms(3000);
  443. furi_delay_ms(2000);
  444. ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
  445. }
  446. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  447. }
  448. static void w5500_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. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  456. furi_hal_gpio_write(&resetpin, true);
  457. furi_hal_gpio_write(&cspin, true);
  458. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  459. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  460. }
  461. static void w5500_deinit() {
  462. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  463. }
  464. void eth_worker_w5500(EthWorker* eth_worker) {
  465. furi_assert(eth_worker);
  466. //uint8_t temp;
  467. FURI_LOG_I(TAG, "Ehtping_Init");
  468. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  469. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  470. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  471. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  472. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  473. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  474. FURI_LOG_I(TAG, "Registered W5500 callbacks");
  475. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  476. furi_hal_gpio_write(&resetpin, true);
  477. furi_hal_gpio_write(&cspin, true);
  478. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  479. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  480. FURI_LOG_I(TAG, "GPIO inited");
  481. furi_hal_power_enable_otg();
  482. furi_delay_ms(1000);
  483. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  484. furi_delay_ms(2000);
  485. furi_hal_gpio_write(&resetpin, false);
  486. furi_delay_ms(10);
  487. furi_hal_gpio_write(&resetpin, true);
  488. FURI_LOG_I(TAG, "GPIO used");
  489. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  490. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  491. FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
  492. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  493. }
  494. FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
  495. furi_delay_ms(2000);
  496. wizchip_setnetinfo(&gWIZNETINFO);
  497. FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
  498. setSHAR(gWIZNETINFO.mac);
  499. FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
  500. //check phy status
  501. //do
  502. //{
  503. // if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
  504. // {
  505. // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  506. // }
  507. // furi_delay_ms(1);
  508. //} while (temp == PHY_LINK_OFF);
  509. //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  510. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  511. FURI_LOG_I(TAG, "W5500 before delay\r\n");
  512. furi_delay_ms(2000);
  513. FURI_LOG_I(TAG, "W5500 after delay\r\n");
  514. //furi_hal_power_disable_otg();
  515. //FURI_LOG_I(TAG, "W5500 power off\r\n");
  516. ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  517. furi_delay_ms(2000);
  518. ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
  519. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  520. }