lwiplib.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * \file lwiplib.c
  3. *
  4. * \brief lwip related initializations
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. /*
  38. ** Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  39. ** ALL RIGHTS RESERVED
  40. */
  41. /*
  42. ** lwIP Compile Time Options for SitaraWare.
  43. */
  44. #include "lwiplib.h"
  45. #include "lwip/dhcp.h" //add by wulianwei
  46. #include "lwip/init.h" //add by wulianwei
  47. #include "netif/sitaraif.h" //add by wulianwei
  48. /*
  49. ** lwIP high-level API/Stack/IPV4/SNMP/Network Interface/PPP codes
  50. */
  51. //#include "src/api/api_lib.c"
  52. //#include "src/api/api_msg.c"
  53. //#include "src/api/err.c"
  54. //#include "src/api/netbuf.c"
  55. //#include "src/api/netdb.c"
  56. //#include "src/api/netifapi.c"
  57. //#include "src/api/tcpip.c"
  58. //#include "src/api/sockets.c"
  59. //
  60. //#include "src/core/dhcp.c"
  61. //#include "src/core/dns.c"
  62. //#include "src/core/init.c"
  63. //#include "src/core/mem.c"
  64. //#include "src/core/memp.c"
  65. //#include "src/core/netif.c"
  66. //#include "src/core/pbuf.c"
  67. //#include "src/core/raw.c"
  68. //#include "src/core/stats.c"
  69. //#include "src/core/sys.c"
  70. //#include "src/core/tcp.c"
  71. //#include "src/core/tcp_in.c"
  72. //#include "src/core/tcp_out.c"
  73. //#include "src/core/udp.c"
  74. //
  75. //#include "src/core/ipv4/autoip.c"
  76. //#include "src/core/ipv4/icmp.c"
  77. //#include "src/core/ipv4/igmp.c"
  78. //#include "src/core/ipv4/inet.c"
  79. //#include "src/core/ipv4/inet_chksum.c"
  80. //#include "src/core/ipv4/ip.c"
  81. //#include "src/core/ipv4/ip_addr.c"
  82. //#include "src/core/ipv4/ip_frag.c"
  83. //
  84. //#include "src/core/snmp/asn1_dec.c"
  85. //#include "src/core/snmp/asn1_enc.c"
  86. //#include "src/core/snmp/mib2.c"
  87. //#include "src/core/snmp/mib_structs.c"
  88. //#include "src/core/snmp/msg_in.c"
  89. //#include "src/core/snmp/msg_out.c"
  90. //
  91. //#include "src/netif/etharp.c"
  92. //#include "src/netif/loopif.c"
  93. //
  94. //#include "src/netif/ppp/auth.c"
  95. //#include "src/netif/ppp/chap.c"
  96. //#include "src/netif/ppp/chpms.c"
  97. //#include "src/netif/ppp/fsm.c"
  98. //#include "src/netif/ppp/ipcp.c"
  99. //#include "src/netif/ppp/lcp.c"
  100. //#include "src/netif/ppp/magic.c"
  101. //#include "src/netif/ppp/md5.c"
  102. //#include "src/netif/ppp/pap.c"
  103. //#include "src/netif/ppp/ppp.c"
  104. //#include "src/netif/ppp/ppp_oe.c"
  105. //#include "src/netif/ppp/randm.c"
  106. //#include "src/netif/ppp/vj.c"
  107. //
  108. ///*
  109. //** Sitara-specific lwIP interface/porting layer code.
  110. //*/
  111. //#include "ports/am1808/perf.c"
  112. //#include "ports/am1808/sys_arch.c"
  113. //#include "ports/am1808/netif/sitaraif.c"
  114. //#include "locator.c"
  115. /******************************************************************************
  116. ** INTERNAL VARIABLE DEFINITIONS
  117. ******************************************************************************/
  118. /*
  119. ** The lwIP network interface structure for the Sitara Ethernet MAC.
  120. */
  121. static struct netif sitaraNetIF[MAX_EMAC_INSTANCE];
  122. /******************************************************************************
  123. ** FUNCTION DEFINITIONS
  124. ******************************************************************************/
  125. /**
  126. *
  127. * \brief Initializes the lwIP TCP/IP stack.
  128. *
  129. * \param instNum The instance index of the EMAC module
  130. * \param macArray Pointer to the MAC Address
  131. * \param ipAddr The IP address to be used
  132. * \param netMask The network mask to be used
  133. * \param gwAddr The Gateway address to be used
  134. * \param ipMode The IP Address Mode.
  135. * ipMode can take the following values\n
  136. * IPADDR_USE_STATIC - force static IP addressing to be used \n
  137. * IPADDR_USE_DHCP - force DHCP with fallback to Link Local \n
  138. * IPADDR_USE_AUTOIP - force Link Local only
  139. *
  140. * This function performs initialization of the lwIP TCP/IP stack for the
  141. * Sitara EMAC, including DHCP and/or AutoIP, as configured.
  142. *
  143. * \return IP Address.
  144. */
  145. unsigned int lwIPInit(unsigned int instNum, unsigned char *macArray,
  146. unsigned int ipAddr, unsigned int netMask,
  147. unsigned int gwAddr, unsigned int ipMode)
  148. {
  149. struct ip_addr ip_addr;
  150. struct ip_addr net_mask;
  151. struct ip_addr gw_addr;
  152. volatile unsigned char *state;
  153. unsigned int *ipAddrPtr;
  154. volatile unsigned int cnt = 0x3FFFFFFF;
  155. lwip_init();
  156. /* Setup the network address values. */
  157. if(ipMode == IPADDR_USE_STATIC)
  158. {
  159. ip_addr.addr = htonl(ipAddr);
  160. net_mask.addr = htonl(netMask);
  161. gw_addr.addr = htonl(gwAddr);
  162. }
  163. else
  164. {
  165. ip_addr.addr = 0;
  166. net_mask.addr = 0;
  167. gw_addr.addr = 0;
  168. }
  169. sitaraif_macaddrset(instNum, macArray);
  170. /*
  171. ** Create, configure and add the Ethernet controller interface with
  172. ** default settings. ip_input should be used to send packets directly to
  173. ** the stack. The lwIP will internaly call the sitaraif_init function.
  174. */
  175. if(NULL ==
  176. netif_add(&sitaraNetIF[instNum], &ip_addr, &net_mask, &gw_addr, &instNum,
  177. sitaraif_init, ip_input))
  178. {
  179. return 0;
  180. };
  181. netif_set_default(&sitaraNetIF[instNum]);
  182. /* Start DHCP, if enabled. */
  183. #if LWIP_DHCP
  184. if(ipMode == IPADDR_USE_DHCP)
  185. {
  186. dhcp_start(&sitaraNetIF[instNum]);
  187. }
  188. #endif
  189. /* Start AutoIP, if enabled and DHCP is not. */
  190. #if LWIP_AUTOIP
  191. if(ipMode == IPADDR_USE_AUTOIP)
  192. {
  193. autoip_start(&sitaraNetIF[instNum]);
  194. }
  195. #endif
  196. if(ipMode != IPADDR_USE_STATIC)
  197. {
  198. /* wait till the dynamic IP address is properly assigned */
  199. state = &(sitaraNetIF[instNum].dhcp->state);
  200. while((*state != DHCP_BOUND) && (cnt--));
  201. if(0 == cnt)
  202. {
  203. return 0;
  204. }
  205. }
  206. else
  207. {
  208. /* Bring the interface up */
  209. netif_set_up(&sitaraNetIF[instNum]);
  210. }
  211. ipAddrPtr = (unsigned int*)&(sitaraNetIF[instNum].ip_addr);
  212. return (*ipAddrPtr);
  213. }
  214. /*
  215. * \brief Checks if the ethernet link is up
  216. *
  217. * \param instNum The instance number of EMAC module
  218. *
  219. * \return Interface status.
  220. */
  221. unsigned int lwIPNetIfStatusGet(unsigned int instNum)
  222. {
  223. return (sitaraif_netif_status(&sitaraNetIF[instNum]));
  224. }
  225. /*
  226. * \brief Checks if the ethernet link is up
  227. *
  228. * \param instNum The instance number of EMAC module
  229. *
  230. * \return The link status.
  231. */
  232. unsigned int lwIPLinkStatusGet(unsigned int instNum)
  233. {
  234. return (sitaraif_link_status(&sitaraNetIF[instNum]));
  235. }
  236. /**
  237. * \brief Interrupt handler for Receive Interrupt. Directly calls the
  238. * sitara interface receive interrupt handler.
  239. *
  240. * \param instNum The instance number of EMAC module for which receive
  241. * interrupt happened
  242. *
  243. * \return None.
  244. */
  245. void lwIPRxIntHandler(unsigned int instNum)
  246. {
  247. sitaraif_rx_inthandler(&sitaraNetIF[instNum]);
  248. }
  249. /**
  250. * \brief Interrupt handler for Transmit Interrupt. Directly calls the
  251. * sitara interface transmit interrupt handler.
  252. *
  253. * \param instNum The instance number of EMAC module for which transmit
  254. * interrupt happened
  255. *
  256. * \return None.
  257. */
  258. void lwIPTxIntHandler(unsigned int instNum)
  259. {
  260. sitaraif_tx_inthandler(&sitaraNetIF[instNum]);
  261. }
  262. /***************************** End Of File ***********************************/