usb_eth.c 4.3 KB

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