eth_worker.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include "eth_worker_i.h"
  2. #include "eth_worker.h"
  3. #include <furi_hal.h>
  4. #include "socket.h"
  5. #include "dhcp.h"
  6. #include "ping.h"
  7. #include "stm32wbxx_hal_gpio.h"
  8. #include "wizchip_conf.h"
  9. #define TAG "EthWorker"
  10. EthWorker* eth_worker_alloc() {
  11. EthWorker* eth_worker = malloc(sizeof(EthWorker));
  12. // Worker thread attributes
  13. eth_worker->thread = furi_thread_alloc();
  14. furi_thread_set_name(eth_worker->thread, "EthWorker");
  15. furi_thread_set_stack_size(eth_worker->thread, 8192);
  16. furi_thread_set_callback(eth_worker->thread, eth_worker_task);
  17. furi_thread_set_context(eth_worker->thread, eth_worker);
  18. eth_worker_change_state(eth_worker, EthWorkerStateModuleInit);
  19. eth_worker->init_process = ethernet_view_process_malloc(EthWorkerProcessInit);
  20. eth_worker->dhcp_process = ethernet_view_process_malloc(EthWorkerProcessDHCP);
  21. eth_worker->stat_process = ethernet_view_process_malloc(EthWorkerProcessStatic);
  22. eth_worker->ping_process = ethernet_view_process_malloc(EthWorkerProcessPing);
  23. eth_worker->reset_process = ethernet_view_process_malloc(EthWorkerProcessReset);
  24. eth_worker->active_process = eth_worker->init_process;
  25. //eth_worker->callback = eth_worker_change_state;
  26. return eth_worker;
  27. }
  28. void eth_worker_free(EthWorker* eth_worker) {
  29. furi_assert(eth_worker);
  30. furi_thread_free(eth_worker->thread);
  31. ethernet_view_process_free(eth_worker->init_process);
  32. ethernet_view_process_free(eth_worker->dhcp_process);
  33. ethernet_view_process_free(eth_worker->stat_process);
  34. ethernet_view_process_free(eth_worker->ping_process);
  35. ethernet_view_process_free(eth_worker->reset_process);
  36. free(eth_worker);
  37. }
  38. void eth_worker_change_state(EthWorker* eth_worker, EthWorkerState state) {
  39. furi_assert(eth_worker);
  40. eth_worker->state = state;
  41. }
  42. void eth_worker_set_active_process(EthWorker* eth_worker, EthWorkerProcess state) {
  43. furi_assert(eth_worker);
  44. switch(state) {
  45. case EthWorkerProcessInit:
  46. eth_worker->active_process = eth_worker->init_process;
  47. break;
  48. case EthWorkerProcessDHCP:
  49. eth_worker->active_process = eth_worker->dhcp_process;
  50. break;
  51. case EthWorkerProcessStatic:
  52. eth_worker->active_process = eth_worker->stat_process;
  53. break;
  54. case EthWorkerProcessPing:
  55. eth_worker->active_process = eth_worker->ping_process;
  56. break;
  57. case EthWorkerProcessReset:
  58. eth_worker->active_process = eth_worker->reset_process;
  59. break;
  60. }
  61. }
  62. /************************** Ethernet Worker Thread *****************************/
  63. int32_t eth_worker_task(void* context) {
  64. furi_assert(context);
  65. EthWorker* eth_worker = (EthWorker*)context;
  66. furi_hal_power_insomnia_enter();
  67. while(eth_worker->state != EthWorkerStateStop) {
  68. if(eth_worker->state == EthWorkerStateModuleInit) {
  69. eth_worker_w5500(eth_worker);
  70. eth_worker_dhcp(eth_worker);
  71. }
  72. if(eth_worker->state == EthWorkerStateDHCP) {
  73. eth_worker_dhcp(eth_worker);
  74. }
  75. }
  76. FURI_LOG_I(TAG, "eth_worker_task exit \r\n");
  77. eth_worker_change_state(eth_worker, EthWorkerStateStop);
  78. furi_hal_power_insomnia_exit();
  79. return 0;
  80. }
  81. #define DHCP_SOCKET 0
  82. #define PING_SOCKET 1
  83. // MAC-àäðåñ
  84. #define ETHADDR0 0x00 // The first octet of the Ethernet address
  85. #define ETHADDR1 0x08 // The second octet of the Ethernet address
  86. #define ETHADDR2 0xDC // The third octet of the Ethernet address
  87. #define ETHADDR3 0x47 // The fourth octet of the Ethernet address
  88. #define ETHADDR4 0x47 // The fifth octet of the Ethernet address
  89. #define ETHADDR5 0x54 // The sixth octet of the Ethernet address
  90. // Ïàðàìåòðû IP (åñëè DHCP îòêëþ÷åí)
  91. #define IPADDR0 192 // The first octet of the IP address of this uIP node
  92. #define IPADDR1 168 // The second octet of the IP address of this uIP node
  93. #define IPADDR2 1 // The third octet of the IP address of this uIP node
  94. #define IPADDR3 137 // The fourth octet of the IP address of this uIP node
  95. #define NETMASK0 255 // The first octet of the netmask of this uIP node
  96. #define NETMASK1 255 // The second octet of the netmask of this uIP node
  97. #define NETMASK2 255 // The third octet of the netmask of this uIP node
  98. #define NETMASK3 0 // The fourth octet of the netmask of this uIP node
  99. #define DRIPADDR0 192 // The first octet of the IP address of the default router
  100. #define DRIPADDR1 168 // The second octet of the IP address of the default router
  101. #define DRIPADDR2 1 // The third octet of the IP address of the default router
  102. #define DRIPADDR3 1 // The fourth octet of the IP address of the default router
  103. static uint8_t ip_assigned = 0;
  104. static GpioPin cspin = {.port = GPIOA, .pin = GPIO_PIN_4};
  105. void W5500_Select(void) {
  106. furi_hal_gpio_write(&cspin, false);
  107. }
  108. void W5500_Unselect(void) {
  109. furi_hal_gpio_write(&cspin, true);
  110. }
  111. void Callback_IPAssigned(void) {
  112. FURI_LOG_I(TAG, "Callback: IP assigned! Leased time: %d sec\r\n", getDHCPLeasetime());
  113. ip_assigned = 1;
  114. }
  115. void Callback_IPConflict(void) {
  116. FURI_LOG_I(TAG, "Callback: IP conflict!\r\n");
  117. }
  118. void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  119. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  120. //if(buff[0] != 0)
  121. // FURI_LOG_I(TAG, "spirx[%d]", buff[0]);
  122. }
  123. void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  124. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  125. }
  126. uint8_t W5500_ReadByte(void) {
  127. uint8_t byte;
  128. W5500_ReadBuff(&byte, sizeof(byte));
  129. return byte;
  130. }
  131. void W5500_WriteByte(uint8_t byte) {
  132. W5500_WriteBuff(&byte, sizeof(byte));
  133. }
  134. void wait_ms(int ms) {
  135. furi_delay_ms(ms);
  136. }
  137. wiz_NetInfo gWIZNETINFO = {
  138. .mac = {ETHADDR0, ETHADDR1, ETHADDR2, ETHADDR3, ETHADDR4, ETHADDR5},
  139. .ip = {IPADDR0, IPADDR1, IPADDR2, IPADDR3},
  140. .sn = {NETMASK0, NETMASK1, NETMASK2, NETMASK3},
  141. .gw = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  142. .dns = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  143. .dhcp = NETINFO_DHCP // NETINFO_STATIC
  144. };
  145. void eth_worker_dhcp(EthWorker* eth_worker) {
  146. furi_assert(eth_worker);
  147. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  148. uint8_t temp;
  149. uint8_t W5500FifoSize[2][8] = {
  150. {
  151. 2,
  152. 2,
  153. 2,
  154. 2,
  155. 2,
  156. 2,
  157. 2,
  158. 2,
  159. },
  160. {2, 2, 2, 2, 2, 2, 2, 2}};
  161. uint8_t dhcp_buffer[2000];
  162. FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
  163. FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
  164. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  165. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  166. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  167. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  168. furi_hal_gpio_write(&resetpin, true);
  169. furi_hal_gpio_write(&cspin, true);
  170. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  171. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  172. furi_hal_power_enable_otg();
  173. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  174. furi_delay_ms(1000);
  175. furi_hal_gpio_write(&resetpin, false);
  176. furi_delay_ms(10);
  177. furi_hal_gpio_write(&resetpin, true);
  178. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  179. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  180. FURI_LOG_I(TAG, "W5500 initialized fail");
  181. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  182. while(1)
  183. ;
  184. //break;
  185. }
  186. FURI_LOG_I(TAG, "W5500 initialized success");
  187. furi_delay_ms(200);
  188. wizchip_setnetinfo(&gWIZNETINFO);
  189. FURI_LOG_I(TAG, "W5500 info setted 1");
  190. setSHAR(gWIZNETINFO.mac);
  191. FURI_LOG_I(TAG, "W5500 info setted 2");
  192. //check phy status
  193. do {
  194. if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
  195. FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  196. }
  197. furi_delay_ms(1);
  198. } while(temp == PHY_LINK_OFF);
  199. FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  200. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  201. furi_delay_ms(1000);
  202. FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
  203. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  204. ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
  205. if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
  206. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  207. uint8_t dhcp_ret = DHCP_STOPPED;
  208. while(
  209. !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
  210. (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
  211. dhcp_ret = DHCP_run();
  212. switch(dhcp_ret) {
  213. case DHCP_IP_ASSIGN:
  214. case DHCP_IP_CHANGED:
  215. case DHCP_IP_LEASED:
  216. getIPfromDHCP(gWIZNETINFO.ip);
  217. getGWfromDHCP(gWIZNETINFO.gw);
  218. getSNfromDHCP(gWIZNETINFO.sn);
  219. getDNSfromDHCP(gWIZNETINFO.dns);
  220. gWIZNETINFO.dhcp = NETINFO_DHCP;
  221. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  222. FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
  223. break;
  224. case DHCP_FAILED:
  225. FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
  226. gWIZNETINFO.dhcp = NETINFO_STATIC;
  227. break;
  228. }
  229. furi_delay_ms(1);
  230. }
  231. wizchip_getnetinfo(&gWIZNETINFO);
  232. FURI_LOG_I(
  233. TAG,
  234. "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
  235. gWIZNETINFO.mac[0],
  236. gWIZNETINFO.mac[1],
  237. gWIZNETINFO.mac[2],
  238. gWIZNETINFO.mac[3],
  239. gWIZNETINFO.mac[4],
  240. gWIZNETINFO.mac[5]);
  241. if(gWIZNETINFO.dhcp == NETINFO_DHCP)
  242. FURI_LOG_I(TAG, "DHCP\n\r");
  243. else
  244. FURI_LOG_I(TAG, "Static IP\n\r");
  245. FURI_LOG_I(
  246. TAG,
  247. "IP address : %d.%d.%d.%d\n\r",
  248. gWIZNETINFO.ip[0],
  249. gWIZNETINFO.ip[1],
  250. gWIZNETINFO.ip[2],
  251. gWIZNETINFO.ip[3]);
  252. FURI_LOG_I(
  253. TAG,
  254. "SM Mask : %d.%d.%d.%d\n\r",
  255. gWIZNETINFO.sn[0],
  256. gWIZNETINFO.sn[1],
  257. gWIZNETINFO.sn[2],
  258. gWIZNETINFO.sn[3]);
  259. FURI_LOG_I(
  260. TAG,
  261. "Gate way : %d.%d.%d.%d\n\r",
  262. gWIZNETINFO.gw[0],
  263. gWIZNETINFO.gw[1],
  264. gWIZNETINFO.gw[2],
  265. gWIZNETINFO.gw[3]);
  266. FURI_LOG_I(
  267. TAG,
  268. "DNS Server : %d.%d.%d.%d\n\r",
  269. gWIZNETINFO.dns[0],
  270. gWIZNETINFO.dns[1],
  271. gWIZNETINFO.dns[2],
  272. gWIZNETINFO.dns[3]);
  273. ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
  274. furi_delay_ms(20000);
  275. uint8_t pDestaddr[4] = {8, 8, 8, 8};
  276. uint8_t tmp = ping_auto(1, pDestaddr);
  277. //tmp = ping_count(0,3,pDestaddr);
  278. if(tmp == SUCCESS) {
  279. ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
  280. FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
  281. } else {
  282. ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
  283. FURI_LOG_I(TAG, "----------ERROR = %d----------\r\n", tmp);
  284. }
  285. furi_delay_ms(3000);
  286. furi_delay_ms(2000);
  287. ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
  288. }
  289. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  290. }
  291. static void w5500_init() {
  292. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  293. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  294. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  295. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  296. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  297. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  298. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  299. furi_hal_gpio_write(&resetpin, true);
  300. furi_hal_gpio_write(&cspin, true);
  301. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  302. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  303. }
  304. static void w5500_deinit() {
  305. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  306. }
  307. void eth_worker_w5500(EthWorker* eth_worker) {
  308. furi_assert(eth_worker);
  309. //uint8_t temp;
  310. FURI_LOG_I(TAG, "Ehtping_Init");
  311. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  312. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  313. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  314. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  315. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  316. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  317. FURI_LOG_I(TAG, "Registered W5500 callbacks");
  318. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  319. furi_hal_gpio_write(&resetpin, true);
  320. furi_hal_gpio_write(&cspin, true);
  321. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  322. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  323. FURI_LOG_I(TAG, "GPIO inited");
  324. furi_hal_power_enable_otg();
  325. furi_delay_ms(1000);
  326. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  327. furi_delay_ms(2000);
  328. furi_hal_gpio_write(&resetpin, false);
  329. furi_delay_ms(10);
  330. furi_hal_gpio_write(&resetpin, true);
  331. FURI_LOG_I(TAG, "GPIO used");
  332. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  333. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  334. FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
  335. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  336. }
  337. FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
  338. furi_delay_ms(2000);
  339. wizchip_setnetinfo(&gWIZNETINFO);
  340. FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
  341. setSHAR(gWIZNETINFO.mac);
  342. FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
  343. //check phy status
  344. //do
  345. //{
  346. // if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
  347. // {
  348. // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  349. // }
  350. // furi_delay_ms(1);
  351. //} while (temp == PHY_LINK_OFF);
  352. //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  353. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  354. FURI_LOG_I(TAG, "W5500 before delay\r\n");
  355. furi_delay_ms(2000);
  356. FURI_LOG_I(TAG, "W5500 after delay\r\n");
  357. //furi_hal_power_disable_otg();
  358. //FURI_LOG_I(TAG, "W5500 power off\r\n");
  359. ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  360. furi_delay_ms(2000);
  361. ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
  362. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  363. }