eth_worker.c 14 KB

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