eth_worker.c 13 KB

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