📄 cp2200.lst
字号:
232 /*==========================================================
233 // function: Sets the current MAC address to the MAC address pointed to by <pMAC>.
234 // Parameter: pMAC pointer to a netnode
235 // return: void
236 // note: cp220x_core.c, datasheet page 80
237 ==========================================================*/
238 void MAC_SetAddress(union NetNode *pMAC)
239 {
240 1 INT16U MAC0,MAC1,MAC2;
241 1 MAC0=Swap16U(pMAC->nodebytes.macwords[2]);
C51 COMPILER V7.06 CP2200 12/04/2007 13:46:18 PAGE 5
242 1 MAC1=Swap16U(pMAC->nodebytes.macwords[1]);
243 1 MAC2=Swap16U(pMAC->nodebytes.macwords[0]);
244 1 MAC_Write(MACAD0, MAC0);
245 1 MAC_Write(MACAD1, MAC1);
246 1 MAC_Write(MACAD2, MAC2);
247 1 return;
248 1 }
249
250 /*==========================================================
251 // function: Initializes the MAC and programs the MAC address using the MAC address
252 // stored at address 0x1FFA in CP220x Flash.
253 // Parameter: none
254 // return: void
255 // note: cp220x_core.c, datasheet page 78
256 ==========================================================*/
257 void MAC_Init(void)
258 {
259 1
260 1
261 1 // Check the duplex mode and perform duplex-mode specific initializations
262 1
263 1 if(PHYCN & 0x10)
264 1 {
265 2
266 2 // The device is in full-duplex mode, configure MAC registers
267 2 // Padding is turned on.
268 2 MAC_Write(MACCF, 0x40B3);
269 2 MAC_Write(IPGT, 0x0015);
270 2
271 2 } else {
272 2
273 2 // The device is in half-duplex mode, configure MAC registers
274 2 // Padding is turned off.
275 2 MAC_Write(MACCF, 0x4012);
276 2 MAC_Write(IPGT, 0x0012);
277 2
278 2 }
279 1
280 1 // Configure the IPGR register
281 1 MAC_Write(IPGR, 0x0C12);
282 1
283 1 // Configure the MAXLEN register to 1518 bytes
284 1 MAC_Write(MAXLEN, 0x05EE);
285 1
286 1 // Copy MAC Address Stored in Flash to MYMAC
287 1 FLASHADDRH=0x1F;
288 1 FLASHADDRL=0xFA;
289 1
290 1 myNode.node.mac[0] = FLASHAUTORD;
291 1 myNode.node.mac[1] = FLASHAUTORD;
292 1 myNode.node.mac[2] = FLASHAUTORD;
293 1 myNode.node.mac[3] = FLASHAUTORD;
294 1 myNode.node.mac[4] = FLASHAUTORD;
295 1 myNode.node.mac[5] = FLASHAUTORD;
296 1
297 1 // Program the MAC address
298 1 MAC_SetAddress(&myNode);
299 1
300 1 // Enable Reception and configure Loopback mode
301 1 MAC_Write(MACCN, 0x0001); // Enable Reception without loopback
302 1 RXHASHH=0xff;
303 1 RXHASHL=0xff;
C51 COMPILER V7.06 CP2200 12/04/2007 13:46:18 PAGE 6
304 1 RXFILT=0x0c;
305 1 }
306
307 /*==========================================================
308 // function: read 8k flash data
309 // Parameter: hAdd high address, max 0x1F
310 // lAdd low address
311 // return: unsigned char read data
312 // note: cp220x_core.c, datasheet page 75
313 // write: han
314 ==========================================================*/
315 /*unsigned char ReadFlash(unsigned char hAdd,unsigned char lAdd)
316 {
317 unsigned char tmp;
318 if(hAdd > 0x1F)
319 return 0;
320 WriteReg(FLASHADDRH,hAdd);
321 WriteReg(FLASHADDRL,lAdd);
322 tmp = ReadReg(FLASHAUTORD);
323 return tmp;
324 }*/
325
326
327 /*==========================================================
328 // function: sends an IEEE 802.3 Ethernet packet using the CP220x.
329 // Parameter: pbuf 指向要发送的MAC数据包的指针
330 // buffer_length length of buffer 从目的MAC到数据末尾的长度
331 // return: void
332 // (8 bytes) 48-bit 48-bit 16-bit 0-1500 bytes
333 // ----------------------------------------------------------------------
334 // | Preamble| SFD | Dest |Source| Type/Length |Data Field | Pad | FCS |
335 // | | | Addr | Addr | Field | | | (CRC) |
336 // ----------------------------------------------------------------------
337 // supplied by | supplied by the MCU | supplied
338 // CP220x | (minimum 64 bytes) | by CP220x
339 ==========================================================*/
340 void CP220x_Send( const unsigned char xdata *pbuf, unsigned int buffer_length)
341 {
342 1
343 1 int i;
344 1 unsigned int ramaddr;
345 1 unsigned char tmp;
346 1
347 1 // Define Macro to increment the RAM address Pointer
348 1 #define INC_RAMADDR {ramaddr++; \
349 1 RAMADDRH=(ramaddr >> 8);\
350 1 RAMADDRL=(ramaddr & 0x00FF);}
351 1
352 1
353 1 // Step 1: Poll TXBUSY until it becomes 0x00
354 1 tmp = TXBUSY;
355 1 while(tmp)
356 1 {
357 2 tmp = TXBUSY;
358 2 }
359 1
360 1 // Step 2: Set the TXSTARTH:TXSTARTL address to 0x0000
361 1 TXSTARTH=0x00;
362 1 TXSTARTL=0x00;
363 1
364 1
365 1 // Step 3: Load data into transmit buffer
C51 COMPILER V7.06 CP2200 12/04/2007 13:46:18 PAGE 7
366 1 // When the random access method is used, we do not need to check for
367 1 // aborted packets. This method will be slightly slower than the Autowrite
368 1 // method, however, it reduces code space requirements.
369 1
370 1 // Setup RAM Address Pointer To 0x0000
371 1 RAMADDRH=0x00;
372 1 RAMADDRL=0x00;
373 1 ramaddr = 0x0000;
374 1
375 1 // printf ("\nsend\n");
376 1 // Step 3a: Load the source address
377 1 // Step 3d: Load the packet payload
378 1 for(i = 0; i < buffer_length;i++)
379 1 {
380 2 RAMTXDATA=*pbuf++;
381 2 INC_RAMADDR;
382 2 }
383 1
384 1
385 1 // Step 3e: Pad short packets
386 1 while(ramaddr < 64)
387 1 {
388 2 RAMTXDATA=0;
389 2 INC_RAMADDR;
390 2 }
391 1
392 1 // Set the TXENDH:TXENDL address to <ramaddr - 1>
393 1 ramaddr--;
394 1 TXENDH=(ramaddr >> 8);
395 1 TXENDL=(ramaddr & 0x00FF);
396 1
397 1
398 1 // Step 4: Set the TXSTARTH:TXSTARTL address back to 0x0000
399 1 TXSTARTH=0x00;
400 1 TXSTARTL=0x00;
401 1
402 1 // Step 5: Write '1' to TXGO to begin transmission
403 1 TXCN=0x01;
404 1 }
405
406 /*==========================================================
407 // function: reads the current packet from the CP220x receive buffer and
408 // copies it to the passed buffer.
409 // Parameter: rxdnet received packet buffer, max 314bytes
410 // return: unsigned int number of bytes added to the buffer
411 // note: cp220x_eth.c, datasheet page 58
412 // --------------------------------------------------------------------------
413 // | Preamble | SFD | Dest | Source | Type/Length | Data Field | Pad | FCS |
414 // | | | Addr | Addr | Field | | | (CRC) |
415 // --------------------------------------------------------------------------
416 // supplied by | supplied by the MCU | supplied by
417 // CP220x | | CP220x
418 //-----------------------------------------------------------------------------
419 ==========================================================*/
420 unsigned int CP220x_Receive( INT8U xdata *inbuf)
421 {
422 1 unsigned char rx_ok;
423 1 unsigned char skip = 0;
424 1 unsigned int cplen;
425 1 unsigned int i;
426 1 unsigned char tmp;
427 1 unsigned char interrupt_read;
C51 COMPILER V7.06 CP2200 12/04/2007 13:46:18 PAGE 8
428 1 unsigned char valid_bits;
429 1 unsigned char num_packets;
430 1 // Clear interrupt flags.
431 1 interrupt_read = INT1;
432 1 interrupt_read = INT0;
433 1
434 1 // Check for packet received interrupt
435 1 if( interrupt_read & RXINT)
436 1 {
437 2 // Count the number of packets in the receive buffer
438 2 // This is equal to the number of bits set to 1 in TLBVALID
439 2 valid_bits = TLBVALID;
440 2 for(num_packets = 0; valid_bits; num_packets++)
441 2 {
442 3 valid_bits &= valid_bits - 1;
443 3 }
444 2
445 2 // If the receive buffer has 7 packets, then disable reception.
446 2 if( num_packets >= 7)
447 2 {
448 3 RXCN = RXINH; // Inhibit New Packet Reception
449 3 }
450 2 }
451 1
452 1 // Step 1: Check the RXOK bit to see if packet was received correctly
453 1 rx_ok = ((CPINFOL & RXOK) && (CPINFOH & RXVALID));
454 1
455 1 // Step 2: If packet received correctly, read the length, otherwise, skip packet.
456 1 if(rx_ok)
457 1 {
458 2
459 2 // Read the packet length
460 2 cplen = ((unsigned int)(CPLENH << 8));
461 2 cplen += CPLENL;
462 2
463 2 }
464 1 else
465 1 {
466 2 // Set packet length to zero
467 2 cplen = 0;
468 2 // Skip packet
469 2 skip = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -