eth_worker.c 13 KB

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