• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
3  *
4  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *        http://www.st.com/software_license_agreement_liberty_v2
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ethernetif.h"
18 #include "lan8720.h"
19 #include "lwip.h"
20 #include "netif/etharp.h"
21 #include "string.h"
22 
LowLevelInit(struct netif * netif)23 static err_t LowLevelInit(struct netif *netif)
24 {
25 #ifdef CHECKSUM_BY_HARDWARE
26 	int i;
27 #endif
28 	netif->hwaddr_len = ETHARP_HWADDR_LEN;
29 	netif->hwaddr[5] = lwipdev.mac[5];
30 	netif->hwaddr[4] = lwipdev.mac[4];
31 	netif->hwaddr[3] = lwipdev.mac[3];
32 	netif->hwaddr[2] = lwipdev.mac[2];
33 	netif->hwaddr[1] = lwipdev.mac[1];
34 	netif->hwaddr[0] = lwipdev.mac[0];
35 	netif->flags = NETIF_FLAG_ETHARP | NETIF_FLAG_BROADCAST | NETIF_FLAG_LINK_UP;
36 	netif->mtu = 1500; /* 1500, maximum transfer unit (in bytes) */
37 	ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
38 	ETH_DMATxDescChainInit(DMATxDscrTab, Tx_Buff, ETH_TXBUFNB);
39 	ETH_DMARxDescChainInit(DMARxDscrTab, Rx_Buff, ETH_RXBUFNB);
40 #ifdef CHECKSUM_BY_HARDWARE
41 	for (i = 0; i < ETH_TXBUFNB; i++) {
42 		ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
43 	}
44 #endif
45 	ETH_Start();
46 	return ERR_OK;
47 }
48 
LowLevelOutput(struct netif * netif,struct pbuf * p)49 static err_t LowLevelOutput(struct netif *netif, struct pbuf *p)
50 {
51 	u8 res;
52 	int lenght = 0; /* 0, initial value */
53 	struct pbuf *qbuffer;
54 	u8 *buffer = (u8*)EthGetCurrentTxBuffer();
55 	for (qbuffer = p; qbuffer != NULL; qbuffer = qbuffer->next) {
56 		memcpy((u8_t*)&buffer[lenght], qbuffer->payload, qbuffer->len);
57 		lenght = lenght + qbuffer->len;
58 	}
59 	res = EthTxPacket(lenght);
60 	if(res == ETH_ERROR) {
61 		return ERR_MEM;
62 	}
63 	printf("		LowLevelOutput end\n\r");
64 	return ERR_OK;
65 }
66 
LowLevelInput(struct netif * netif)67 static struct pbuf *LowLevelInput(struct netif *netif)
68 {
69 	int l = 0; /* 0, initial value */
70 	u16_t lenght;
71 	u8 *buffer;
72 	struct pbuf *pbuffer;
73 	struct pbuf *qbuffer;
74 	FrameTypeDef frame;
75 
76 	pbuffer = NULL;
77 	frame = EthRxPacket();
78 	lenght = frame.length;
79 	buffer = (u8*)frame.buffer;
80 	pbuffer = pbuf_alloc(PBUF_RAW, lenght, PBUF_POOL);
81 	if (pbuffer != NULL) {
82 		for (qbuffer = pbuffer; qbuffer != NULL; qbuffer = qbuffer->next) {
83 			memcpy((u8_t*)qbuffer->payload, (u8_t*)&buffer[l], qbuffer->len);
84 			l = l + qbuffer->len;
85 		}
86 	}
87 	frame.descriptor->Status = ETH_DMARxDesc_OWN;
88 	if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) {
89 		ETH->DMASR = ETH_DMASR_RBUS;
90 		ETH->DMARPDR = 0; /* 0, register status */
91 	}
92 	return pbuffer;
93 }
94 
EthernetifInput(struct netif * netif)95 err_t EthernetifInput(struct netif *netif)
96 {
97 	err_t error;
98 	struct pbuf *pbuffer;
99 	pbuffer = LowLevelInput(netif);
100 	if (pbuffer == NULL) {
101 		return ERR_MEM;
102 	}
103 	error = netif->input(pbuffer, netif);
104 	if (error != ERR_OK) {
105 		LWIP_DEBUGF(NETIF_DEBUG, ("EthernetifInput: IP input error\n"));
106 		pbuf_free(pbuffer);
107 		pbuffer = NULL;
108 	}
109 	return error;
110 }
111 
EthernetifInit(struct netif * netif)112 err_t EthernetifInit(struct netif *netif)
113 {
114 	LWIP_ASSERT("netif!=NULL", (netif != NULL));
115 #if LWIP_NETIF_HOSTNAME
116 	netif->hostname="lwip";
117 #endif
118 	netif->output = etharp_output;
119 	netif->linkoutput = LowLevelOutput;
120 	netif->name[0] = IFNAME0;
121 	netif->name[1] = IFNAME1;
122 	LowLevelInit(netif);
123 	return ERR_OK;
124 }
125