eth_worker.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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() {
  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 = 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();
  243. wizchip_setnetinfo(&gWIZNETINFO);
  244. setSHAR(gWIZNETINFO.mac);
  245. wiz_PhyConf conf;
  246. wizphy_getphyconf(&conf);
  247. eth_log(
  248. EthWorkerProcessInit,
  249. "conf %d %d %d %d",
  250. conf.by,
  251. conf.mode,
  252. conf.speed,
  253. conf.duplex);
  254. eth_log(EthWorkerProcessInit, "net info setted");
  255. eth_log(
  256. EthWorkerProcessInit,
  257. "mac: %02X-%02X-%02X-%02X-%02X-%02X",
  258. gWIZNETINFO.mac[0],
  259. gWIZNETINFO.mac[1],
  260. gWIZNETINFO.mac[2],
  261. gWIZNETINFO.mac[3],
  262. gWIZNETINFO.mac[4],
  263. gWIZNETINFO.mac[5]);
  264. worker->state = EthWorkerStateInited;
  265. continue;
  266. }
  267. } else if(worker->state == EthWorkerStateInited) {
  268. }
  269. furi_delay_ms(50);
  270. }
  271. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  272. furi_hal_power_disable_otg();
  273. furi_hal_power_insomnia_exit();
  274. return 0;
  275. }
  276. #define DHCP_SOCKET 0
  277. #define PING_SOCKET 1
  278. // MAC-àäðåñ
  279. #define ETHADDR0 0x00 // The first octet of the Ethernet address
  280. #define ETHADDR1 0x08 // The second octet of the Ethernet address
  281. #define ETHADDR2 0xDC // The third octet of the Ethernet address
  282. #define ETHADDR3 0x47 // The fourth octet of the Ethernet address
  283. #define ETHADDR4 0x47 // The fifth octet of the Ethernet address
  284. #define ETHADDR5 0x54 // The sixth octet of the Ethernet address
  285. // Ïàðàìåòðû IP (åñëè DHCP îòêëþ÷åí)
  286. #define IPADDR0 192 // The first octet of the IP address of this uIP node
  287. #define IPADDR1 168 // The second octet of the IP address of this uIP node
  288. #define IPADDR2 1 // The third octet of the IP address of this uIP node
  289. #define IPADDR3 137 // The fourth octet of the IP address of this uIP node
  290. #define NETMASK0 255 // The first octet of the netmask of this uIP node
  291. #define NETMASK1 255 // The second octet of the netmask of this uIP node
  292. #define NETMASK2 255 // The third octet of the netmask of this uIP node
  293. #define NETMASK3 0 // The fourth octet of the netmask of this uIP node
  294. #define DRIPADDR0 192 // The first octet of the IP address of the default router
  295. #define DRIPADDR1 168 // The second octet of the IP address of the default router
  296. #define DRIPADDR2 1 // The third octet of the IP address of the default router
  297. #define DRIPADDR3 1 // The fourth octet of the IP address of the default router
  298. // wiz_NetInfo gWIZNETINFO = {
  299. // .mac = {ETHADDR0, ETHADDR1, ETHADDR2, ETHADDR3, ETHADDR4, ETHADDR5},
  300. // .ip = {IPADDR0, IPADDR1, IPADDR2, IPADDR3},
  301. // .sn = {NETMASK0, NETMASK1, NETMASK2, NETMASK3},
  302. // .gw = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  303. // .dns = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  304. // .dhcp = NETINFO_DHCP // NETINFO_STATIC
  305. // };
  306. void eth_worker_dhcp(EthWorker* eth_worker) {
  307. furi_assert(eth_worker);
  308. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  309. uint8_t temp;
  310. uint8_t W5500FifoSize[2][8] = {
  311. {
  312. 2,
  313. 2,
  314. 2,
  315. 2,
  316. 2,
  317. 2,
  318. 2,
  319. 2,
  320. },
  321. {2, 2, 2, 2, 2, 2, 2, 2}};
  322. uint8_t dhcp_buffer[2000];
  323. FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
  324. FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
  325. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  326. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  327. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  328. furi_hal_gpio_write(&resetpin, true);
  329. furi_hal_gpio_write(&cspin, true);
  330. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  331. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  332. furi_hal_power_enable_otg();
  333. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  334. furi_delay_ms(1000);
  335. furi_hal_gpio_write(&resetpin, false);
  336. furi_delay_ms(10);
  337. furi_hal_gpio_write(&resetpin, true);
  338. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  339. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  340. FURI_LOG_I(TAG, "W5500 initialized fail");
  341. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  342. while(1)
  343. ;
  344. //break;
  345. }
  346. FURI_LOG_I(TAG, "W5500 initialized success");
  347. furi_delay_ms(200);
  348. wizchip_setnetinfo(&gWIZNETINFO);
  349. FURI_LOG_I(TAG, "W5500 info setted 1");
  350. setSHAR(gWIZNETINFO.mac);
  351. FURI_LOG_I(TAG, "W5500 info setted 2");
  352. //check phy status
  353. do {
  354. if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
  355. FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  356. }
  357. furi_delay_ms(1);
  358. } while(temp == PHY_LINK_OFF);
  359. FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  360. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  361. furi_delay_ms(1000);
  362. FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
  363. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  364. ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
  365. if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
  366. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  367. uint8_t dhcp_ret = DHCP_STOPPED;
  368. while(
  369. !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
  370. (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
  371. dhcp_ret = DHCP_run();
  372. switch(dhcp_ret) {
  373. case DHCP_IP_ASSIGN:
  374. case DHCP_IP_CHANGED:
  375. case DHCP_IP_LEASED:
  376. getIPfromDHCP(gWIZNETINFO.ip);
  377. getGWfromDHCP(gWIZNETINFO.gw);
  378. getSNfromDHCP(gWIZNETINFO.sn);
  379. getDNSfromDHCP(gWIZNETINFO.dns);
  380. gWIZNETINFO.dhcp = NETINFO_DHCP;
  381. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  382. FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
  383. break;
  384. case DHCP_FAILED:
  385. FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
  386. gWIZNETINFO.dhcp = NETINFO_STATIC;
  387. break;
  388. }
  389. furi_delay_ms(1);
  390. }
  391. wizchip_getnetinfo(&gWIZNETINFO);
  392. FURI_LOG_I(
  393. TAG,
  394. "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
  395. gWIZNETINFO.mac[0],
  396. gWIZNETINFO.mac[1],
  397. gWIZNETINFO.mac[2],
  398. gWIZNETINFO.mac[3],
  399. gWIZNETINFO.mac[4],
  400. gWIZNETINFO.mac[5]);
  401. if(gWIZNETINFO.dhcp == NETINFO_DHCP)
  402. FURI_LOG_I(TAG, "DHCP\n\r");
  403. else
  404. FURI_LOG_I(TAG, "Static IP\n\r");
  405. FURI_LOG_I(
  406. TAG,
  407. "IP address : %d.%d.%d.%d\n\r",
  408. gWIZNETINFO.ip[0],
  409. gWIZNETINFO.ip[1],
  410. gWIZNETINFO.ip[2],
  411. gWIZNETINFO.ip[3]);
  412. FURI_LOG_I(
  413. TAG,
  414. "SM Mask : %d.%d.%d.%d\n\r",
  415. gWIZNETINFO.sn[0],
  416. gWIZNETINFO.sn[1],
  417. gWIZNETINFO.sn[2],
  418. gWIZNETINFO.sn[3]);
  419. FURI_LOG_I(
  420. TAG,
  421. "Gate way : %d.%d.%d.%d\n\r",
  422. gWIZNETINFO.gw[0],
  423. gWIZNETINFO.gw[1],
  424. gWIZNETINFO.gw[2],
  425. gWIZNETINFO.gw[3]);
  426. FURI_LOG_I(
  427. TAG,
  428. "DNS Server : %d.%d.%d.%d\n\r",
  429. gWIZNETINFO.dns[0],
  430. gWIZNETINFO.dns[1],
  431. gWIZNETINFO.dns[2],
  432. gWIZNETINFO.dns[3]);
  433. ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
  434. furi_delay_ms(20000);
  435. uint8_t pDestaddr[4] = {8, 8, 8, 8};
  436. uint8_t tmp = ping_auto(1, pDestaddr);
  437. //tmp = ping_count(0,3,pDestaddr);
  438. if(tmp == SUCCESS) {
  439. ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
  440. FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
  441. } else {
  442. ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
  443. FURI_LOG_I(TAG, "----------ERROR = %d----------\r\n", tmp);
  444. }
  445. furi_delay_ms(3000);
  446. furi_delay_ms(2000);
  447. ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
  448. }
  449. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  450. }
  451. static void w5500_init() {
  452. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  453. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  454. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  455. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  456. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  457. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  458. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  459. furi_hal_gpio_write(&resetpin, true);
  460. furi_hal_gpio_write(&cspin, true);
  461. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  462. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  463. }
  464. static void w5500_deinit() {
  465. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  466. }
  467. void eth_worker_w5500(EthWorker* eth_worker) {
  468. furi_assert(eth_worker);
  469. //uint8_t temp;
  470. FURI_LOG_I(TAG, "Ehtping_Init");
  471. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  472. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  473. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  474. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  475. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  476. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  477. FURI_LOG_I(TAG, "Registered W5500 callbacks");
  478. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  479. furi_hal_gpio_write(&resetpin, true);
  480. furi_hal_gpio_write(&cspin, true);
  481. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  482. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  483. FURI_LOG_I(TAG, "GPIO inited");
  484. furi_hal_power_enable_otg();
  485. furi_delay_ms(1000);
  486. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  487. furi_delay_ms(2000);
  488. furi_hal_gpio_write(&resetpin, false);
  489. furi_delay_ms(10);
  490. furi_hal_gpio_write(&resetpin, true);
  491. FURI_LOG_I(TAG, "GPIO used");
  492. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  493. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  494. FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
  495. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  496. }
  497. FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
  498. furi_delay_ms(2000);
  499. wizchip_setnetinfo(&gWIZNETINFO);
  500. FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
  501. setSHAR(gWIZNETINFO.mac);
  502. FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
  503. //check phy status
  504. //do
  505. //{
  506. // if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
  507. // {
  508. // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  509. // }
  510. // furi_delay_ms(1);
  511. //} while (temp == PHY_LINK_OFF);
  512. //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  513. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  514. FURI_LOG_I(TAG, "W5500 before delay\r\n");
  515. furi_delay_ms(2000);
  516. FURI_LOG_I(TAG, "W5500 after delay\r\n");
  517. //furi_hal_power_disable_otg();
  518. //FURI_LOG_I(TAG, "W5500 power off\r\n");
  519. ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  520. furi_delay_ms(2000);
  521. ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
  522. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  523. }