📄 eth.lst
字号:
176 1 free(outbuf);
177 1 printf("ETH:send frame.\n");
178 1 }
179
C51 COMPILER V8.05a ETH 05/16/2007 11:29:11 PAGE 4
180 //------------------------------------------------------------------------
181 // This functions checks the 8019 receive event status
182 // word to see if an ethernet frame has arrived. If so,
183 // set EVENT_ETH_ARRIVED bit in global event_word
184 //------------------------------------------------------------------------
185 void query_8019(void)
186 {
187 1 char bnry,curr;
188 1 page(0);
189 1 bnry=reg03; //bnry page have read 读页指针
190 1 page(1);
191 1 curr=reg07; //curr writepoint 8019写页指针
192 1 page(0);
193 1 if ((curr==0)) return;
194 1 bnry=bnry++;
195 1 if (bnry>0x7f) bnry=0x4c;
196 1 if (bnry!=curr) //此时表示有新的数据包在缓冲区里
197 1 {
198 2 EA = 0;
199 2 event_word |= EVENT_ETH_ARRIVED;
200 2 EA = 1;
201 2 }
202 1 reg0b=0x00; reg0a=0x00; reg00=0x22;//complete dma page 0
203 1 }
204
205 //------------------------------------------------------------------------
206 // This function gets an incoming Ethernet frame from the 8019.
207 // There may be more than 1 waiting but just allocate memory for
208 // one and read one in. Use the 8019 to queue incoming packets.
209 //------------------------------------------------------------------------
210 UCHAR xdata * rcve_frame(void)//如果收到一个有效的数据包,返回收到的数据,否则返回NULL
211 {
212 1 UCHAR bnry,curr,next_page;
213 1
214 1 UINT len, ii;
215 1 UCHAR temp;
216 1 UCHAR xdata * buf;
217 1
218 1 page(0);
219 1 bnry=reg03; //bnry page have read 读页指针
220 1 page(1);
221 1 curr=reg07; //curr writepoint 8019写页指针
222 1 page(0);
223 1 if ((curr==0)) return NULL; //读的过程出错
224 1 next_page=bnry;
225 1 bnry=bnry++;
226 1 if (bnry>0x7f) bnry=0x4c;
227 1 if (bnry!=curr) //此时表示有新的数据包在缓冲区里
228 1 {
229 2 //读取一包的前4个字节:4字节的8019头部
230 2 page(0);
231 2 reg09=bnry; //read page address high
232 2 reg08=0x00; //read page address low
233 2 reg0b=0x00; //read count high
234 2 reg0a=4; //read count low;
235 2 reg00=0x0a; //read dma
236 2
237 2 temp = reg10; temp = reg10;
238 2 next_page = temp-1; //next page start-1
239 2 len = reg10; temp = reg10;
240 2 len += temp<<8;
241 2 reg0b=0x00; reg0a=0x00; reg00=0x22;//complete dma page 0
C51 COMPILER V8.05a ETH 05/16/2007 11:29:11 PAGE 5
242 2
243 2 // Allocate enough memory to hold the incoming frame
244 2 buf = (UCHAR xdata *)malloc(len);
245 2 if (buf == NULL)
246 2 {
247 3 // out of RAM
248 3 // Tell 8019 to skip the frame
249 3 page(1);
250 3 curr=reg07; //page1
251 3 page(0); //切换回page0
252 3 bnry = curr -1;
253 3 if (bnry < 0x4c) bnry =0x7f;
254 3 reg03=bnry; //write to bnry
255 3 reg07=0xff; //清除中断状态可以不用
256 3 return NULL;
257 3 }
258 2 // This flag keeps track of allocated rcve memory
259 2 rcve_buf_allocated = TRUE;
260 2 // Call the assembler function to get the incoming frame
261 2 reg09=bnry; //read page address high
262 2 reg08=4; //read page address low
263 2 reg0b=len>>8; //read count high
264 2 reg0a=len&0xff; //read count low;
265 2 reg00=0x0a; //read dma
266 2 for(ii=0;ii<len;ii++)
267 2 {
268 3 buf[ii]=reg10;
269 3 }
270 2 reg0b=0x00; reg0a=0x00; reg00=0x22; //dma complete page0
271 2 // Return pointer to start of buffer
272 2 bnry=next_page;
273 2 if (bnry<0x4c) bnry=0x7f;
274 2 reg03=bnry; //write to bnry
275 2 reg07=0xff;
276 2 return (buf);
277 2 printf("ETH:read frame.\n");
278 2 }
279 1 return NULL;
280 1 }
281
282
283
284
285
286
287 void eth_send(UCHAR xdata * outbuf, UCHAR * hwaddr, UINT ptype, UINT len)
288 {
289 1 ETH_HEADER xdata * eth;
290 1
291 1 eth = (ETH_HEADER xdata *)outbuf;
292 1
293 1 // Add 14 byte Ethernet header
294 1 memcpy(eth->dest_hwaddr, hwaddr, 6);
295 1 memcpy(eth->source_hwaddr, my_hwaddr, 6);
296 1 eth->frame_type = ptype;
297 1
298 1 // We just added 14 bytes to length
299 1 send_frame(outbuf, len + 14);
300 1 }
301
302 //------------------------------------------------------------------------
303 // This is the handler for incoming Ethernet frames
C51 COMPILER V8.05a ETH 05/16/2007 11:29:11 PAGE 6
304 // This is designed to handle standard Ethernet (RFC 893) frames
305 // See "TCP/IP Illustrated, Volume 1" Sect 2.2
306 //------------------------------------------------------------------------
307 void eth_rcve(UCHAR xdata * inbuf)
308 {
309 1 ETH_HEADER xdata * eth;
310 1
311 1 eth = (ETH_HEADER xdata *)inbuf;
312 1
313 1 // Reject frames in IEEE 802 format where Eth type field
314 1 // is used for length. Todo: Make it handle this format
315 1 if (eth->frame_type < 1520)
316 1 {
317 2 if (debug) printf("ETH: IEEE 802 pkt rejected\n");
318 2 return;
319 2 }
320 1
321 1 // Figure out what type of frame it is from Eth header
322 1 // Call appropriate handler and supply address of buffer
323 1 switch (eth->frame_type)
324 1 {
325 2 case ARP_PACKET:
326 2 arp_rcve(inbuf);
327 2 break;
328 2
329 2 case IP_PACKET:
330 2 ip_rcve(inbuf);
331 2 break;
332 2
333 2 default:
334 2 if (debug) printf("Error: Unknown pkt rcvd\n");
335 2 break;
336 2 }
337 1 }
338
339
340
341
342
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1221 ----
CONSTANT SIZE = 89 ----
XDATA SIZE = ---- 26
PDATA SIZE = ---- ----
DATA SIZE = ---- 2
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -