usb_eth.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "at32f403a_407.h"
  2. #include "usb_eth.h"
  3. #include <stdbool.h>
  4. #include "dhserver.h"
  5. #include "dnserver.h"
  6. #include <stdlib.h>
  7. #ifdef PRINTF_STDLIB
  8. #include <stdio.h>
  9. #endif
  10. #ifdef PRINTF_CUSTOM
  11. #include "tinystdio.h"
  12. #endif
  13. #include "usbd_rndis_core.h"
  14. //#include "usbd_usr.h"
  15. //#include "usbd_desc.h"
  16. //#include "usb_conf.h"
  17. #include "netif/etharp.h"
  18. #include "lwip/init.h"
  19. #include "lwip/netif.h"
  20. #include "lwip/pbuf.h"
  21. #include "lwip/icmp.h"
  22. #include "lwip/udp.h"
  23. #include "lwip/opt.h"
  24. #include "lwip/arch.h"
  25. #include "lwip/api.h"
  26. #include "lwip/inet.h"
  27. #include "lwip/dns.h"
  28. #include "tcpip.h"
  29. //#include "time.h"
  30. //#include "usb_core.h"
  31. #include "usbd_core.h"
  32. #include "usb.h"
  33. #include "common.h"
  34. //#include "board.h"
  35. #include "FreeRTOS.h"
  36. #include "task.h"
  37. #include "semphr.h"
  38. static uint8_t hwaddr[6] = {0x20,0x89,0x84,0x6A,0x96,00};
  39. static uint8_t ipaddr[4] = {192, 168, 7, 1};
  40. static uint8_t netmask[4] = {255, 255, 255, 0};
  41. static uint8_t gateway[4] = {0, 0, 0, 0};
  42. #define USE_LINK_LED 0
  43. #define NUM_DHCP_ENTRY 3
  44. static dhcp_entry_t entries[NUM_DHCP_ENTRY] =
  45. {
  46. // mac ip address subnet mask lease time
  47. { {0}, {192, 168, 7, 2}, {255, 255, 255, 0}, 24 * 60 * 60 },
  48. { {0}, {192, 168, 7, 3}, {255, 255, 255, 0}, 24 * 60 * 60 },
  49. { {0}, {192, 168, 7, 4}, {255, 255, 255, 0}, 24 * 60 * 60 }
  50. };
  51. static dhcp_config_t dhcp_config =
  52. {
  53. {192, 168, 7, 1}, 67, // server address, port
  54. {192, 168, 7, 1}, // dns server
  55. "prs", // dns suffix
  56. NUM_DHCP_ENTRY, // num entry
  57. entries // entries
  58. };
  59. struct netif netif_data;
  60. static uint8_t received[ETH_MTU + 14];
  61. static int recvSize = 0;
  62. SemaphoreHandle_t xSemUsbEth;
  63. void on_packet(const char *data, int size)
  64. {
  65. BaseType_t xHigherPriorityTaskWoken = pdFALSE;;
  66. memcpy(received, data, size);
  67. recvSize = size;
  68. xSemaphoreGiveFromISR( xSemUsbEth, &xHigherPriorityTaskWoken );
  69. if( xHigherPriorityTaskWoken != pdFALSE )
  70. {
  71. //vPortYield();
  72. taskYIELD();
  73. }
  74. }
  75. void usb_polling()
  76. {
  77. xSemaphoreTake( xSemUsbEth, portMAX_DELAY);
  78. if (recvSize == 0)
  79. return;
  80. struct pbuf *frame;
  81. frame = pbuf_alloc(PBUF_RAW, recvSize, PBUF_POOL);
  82. if (frame == NULL)
  83. return;
  84. memcpy(frame->payload, received, recvSize);
  85. frame->len = recvSize;
  86. recvSize = 0;
  87. tcpip_input(frame, &netif_data);
  88. pbuf_free(frame);
  89. }
  90. static int outputs = 0;
  91. err_t output_fn(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
  92. {
  93. return etharp_output(netif, p, ipaddr);
  94. }
  95. err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
  96. {
  97. int i;
  98. struct pbuf *q;
  99. static char data[ETH_MTU + 14 + 4];
  100. int size = 0;
  101. for (i = 0; i < 200; i++)
  102. {
  103. if (rndis_can_send()) break;
  104. vTaskDelay(1);
  105. }
  106. for(q = p; q != NULL; q = q->next)
  107. {
  108. if (size + q->len > ETH_MTU + 14)
  109. return ERR_ARG;
  110. memcpy(data + size, (char *)q->payload, q->len);
  111. size += q->len;
  112. }
  113. if (!rndis_can_send())
  114. return ERR_USE;
  115. rndis_send(data, size);
  116. outputs++;
  117. return ERR_OK;
  118. }
  119. err_t netif_init_cb(struct netif *netif)
  120. {
  121. LWIP_ASSERT("netif != NULL", (netif != NULL));
  122. netif->mtu = ETH_MTU;
  123. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  124. netif->state = NULL;
  125. netif->name[0] = 'E';
  126. netif->name[1] = 'X';
  127. // netif->dhcp = NULL;
  128. netif->linkoutput = linkoutput_fn;
  129. //netif->output = output_fn;
  130. netif->output = (netif_output_fn)output_fn;
  131. //err_t output_fn(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
  132. //typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr);
  133. return ERR_OK;
  134. }
  135. #define PADDR(ptr) ((ip_addr_t *)ptr)
  136. void init_lwip()
  137. {
  138. struct netif *netif = &netif_data;
  139. netif->hwaddr_len = 6;
  140. memcpy(netif->hwaddr, hwaddr, 6);
  141. netif = netif_add(netif, PADDR(ipaddr), PADDR(netmask), PADDR(gateway), NULL, netif_init_cb, tcpip_input);
  142. netif_set_link_up(netif);
  143. netif_set_up(netif);
  144. }
  145. void usb_hw_init(void)
  146. {
  147. usb_init();
  148. rndis_rxproc = on_packet;
  149. xSemUsbEth = xSemaphoreCreateBinary();
  150. }
  151. bool dns_query_proc(const char *name, ip_addr_t *addr)
  152. {
  153. if (strcmp(name, "energomera.prs") == 0 || strcmp(name, "www.energomera.prs") == 0)
  154. {
  155. addr->addr = *(uint32_t *)ipaddr;
  156. return true;
  157. }
  158. return false;
  159. }
  160. void usb_eth_task(void *arg)
  161. {
  162. while (1)
  163. {
  164. usb_polling(); // usb device polling
  165. }
  166. }
  167. void usb_eth_init(void)
  168. {
  169. usb_hw_init();
  170. //while (rndis_state != rndis_initialized) ;
  171. init_lwip();
  172. while (!netif_is_up(&netif_data)) ;
  173. while (dhserv_init(&dhcp_config) != ERR_OK) ;
  174. while (dnserv_init(PADDR(ipaddr), 53, dns_query_proc) != ERR_OK) ;
  175. //xTaskCreate(usb_eth_task, ( char * ) "usb_eth_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  176. xTaskCreate(usb_eth_task, ( char * ) "usb_eth_task", 3*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  177. }
  178. #ifdef USE_USB_OTG_FS
  179. void OTG_FS_WKUP_IRQHandler(void)
  180. {
  181. if(USB_OTG_dev.cfg.low_power)
  182. {
  183. *(uint32_t *)(0xE000ED10) &= 0xFFFFFFF9 ;
  184. USB_OTG_UngateClock(&USB_OTG_dev);
  185. }
  186. EXTI_ClearITPendingBit(EXTI_Line18);
  187. }
  188. #endif