⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cp220x_core.c

📁 cf8020+cp2200(网络)的驱动实现
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// CP220x_CORE.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
// 
// This file contains core functions used when accessing the CP220x.
// 
// FID:            
// Target:         Multiple
// Tool chain:     Keil C51 7.20 / Keil EVAL C51
//                 Silicon Laboratories IDE version 2.72 
// Command Line:   See Readme.txt
// Project Name:   CP220x_Ethernet_Routines
//
// 
//
// Release 1.1
//    - Configures C8051F120 SYSCLK to 98 MHz
//
// Release 1.0
//    -Initial Release (FB)
//    -30 MAY 2006
//


//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "global.h"


//----------------------------------------------------------------------------
//extern struct
//
extern unsigned char my_hwaddr[];
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
bit full_duplex = 0;

//-----------------------------------------------------------------------------
// Exported Function Prototypes
//-----------------------------------------------------------------------------

// Initialization routines
unsigned char CP220x_HW_Reset(void);
unsigned char PHY_Init(void);
void MAC_Init(void);

// MAC read/write routines
void MAC_Write(unsigned char mac_reg_offset, unsigned int mac_reg_data);
void MAC_GetAddress(MACADDRESS* pMAC);
void MAC_SetAddress(MACADDRESS* pMAC);

// FLASH read/write/erase routines
unsigned char poll_flash_busy (void);
unsigned char CPFLASH_ByteRead (unsigned int addr);
unsigned char CPFLASH_ByteWrite (unsigned int addr, char byte);
unsigned char CPFLASH_PageErase (unsigned int addr);

//-----------------------------------------------------------------------------
// Initialization Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// CP220x_HW_Reset
//-----------------------------------------------------------------------------
//
// Return Value : 
//   unsigned char - '0' on success, or one of the following error codes:
//       MEM_ERROR
//       OSC_ERROR
//       CAL_ERROR
//
// Parameters   : None
//
// Performs Steps 1 - 5 of Reset Initialization (See Section 6.2 of the CP220x
// Datasheet for more details)
//-----------------------------------------------------------------------------
unsigned char CP220x_HW_Reset(void)
{  
   unsigned char temp_char;

   // Reset the CP2200 by holding the /RST pin low for at least 15 us
      #if(UART_ENABLED)
	      printf("enter CP220x_HW_Reset");
      #endif
   CP220x_RST_Low();//将cp2200的复位脚置低
   wait_ms(20);
   
   //--------------------------------------------------------------------------
   // Step 1: Wait for the reset pin to rise.
   //--------------------------------------------------------------------------
   CP220x_RST_High();//将cp2200的复位脚置低
   
   //--------------------------------------------------------------------------
   // Step 2 + 3: Wait for oscillator and self initializaion to complete
   //--------------------------------------------------------------------------
      #if(UART_ENABLED)
	      printf("CP220x_RST_High");
      #endif
   
   // Start a one second timeout
   reset_timeout(ONE_SECOND*5);

   // Wait for the interrupt pin to go low
   // Loop will exit successfully if interrupt detected
   // The function will return error if the reset pin goes low,
   // or the one second timeout expires
 	while(INT_PIN){//等待中断脚变低,                        
   	   #if(UART_ENABLED)
	      printf(" in INT_PIN_AB4_RST_State");
      #endif
      // Check the state of the reset pin     
      if(!AB4_RST_State()){//如果等待reset信号,退出,返回错误
         return OSC_ERROR;
      }
      
      // Check the state of the one second timeout
	   #if(UART_ENABLED)
	      printf(" in INT_PIN_timeout_expired");
      #endif
      if(timeout_expired()){//等待超时退出,返回错误
         return OSC_ERROR;
      }
   }
      #if(UART_ENABLED)
	      printf(" out INT_PIN");
      #endif
   // Start a new one second timeout
   reset_timeout(ONE_SECOND);
   
   // Wait for Oscillator Initialization and Self Initialization to complete
   // Verify that both SELFINTR and OSCINTR are set and that 
   // INT0 is not reading 0xFF;
	do {//等待晶震初始完毕,返回INT0正常
      temp_char = INT0RD;
      
      if(timeout_expired()){//等待超时,返回错误
         return CAL_ERROR;
      }
      
   } while(((temp_char & 0x30) != 0x30) || (temp_char == 0xFF)) ;           
       #if(UART_ENABLED)
	      printf(" out ait for Oscillator Initialization");
      #endif
   //--------------------------------------------------------------------------
   // Additional Step: Verify Communication
   //--------------------------------------------------------------------------

   // Verify communication
   if(RXHASHH != 0x04){//检查是否可以正常通信了
      #if(UART_ENABLED)
      printf("EMIF Failure. Check EMI0CN.");
      #endif
      return MEM_ERROR;
   }   
   
   // Read and write the RAM at 0x7FF in the transmit buffer
   RAMADDRH = 0x07;
   RAMADDRL = 0xFF;

   RAMTXDATA = 0x00;
   if(RAMTXDATA != 0x00){
      #if(UART_ENABLED)
      printf("EMIF Failure. Cannot set data bus to 0x00.");
      #endif
      return MEM_ERROR;
   } 
   
   RAMTXDATA = 0xFF;
   if(RAMTXDATA != 0xFF){
      #if(UART_ENABLED)
      printf("EMIF Failure. Cannot set data bus to 0xFF. Please Reset Device.");
      #endif
      return MEM_ERROR;
   } 
      

   #if(UART_ENABLED)
      printf("Oscillator and Self Initialization Complete\n");
   #endif
 
   //--------------------------------------------------------------------------
   // Step 4: Disable Interrupts For Events that will not be monitored
   //--------------------------------------------------------------------------   
   
   // Disable All Interrupts except for the packet received interrupt
   INT0EN = 0x03;     //关中断                    
   INT1EN = 0x00;
   
   // Clear all Interrupt Flags by reading the self-clearing status registers                          
   temp_char = INT0;  //清空中断寄存器                    
   temp_char = INT1;                      
   
   return 0;
}


//-----------------------------------------------------------------------------
// PHY_Init
//-----------------------------------------------------------------------------
//
// Return Value : 
//   unsigned char - '0' on success, or the following error code:
//       LINK_ERROR
//
// Parameters   : None
//
// Initializes the PHY using Autonegotiation
//-----------------------------------------------------------------------------
unsigned char PHY_Init()
{
   unsigned char temp_char;
   unsigned char retval = 0;
   
   //--------------------------------------------------------------------------
   // Auto-Negotiation Synchronization (Section 15.2 of CP220x Datasheet)
   //--------------------------------------------------------------------------
//自动协商同步
   // Step 1: Disable the PHY
   PHYCN = 0x00;  //禁止phy
   
   // Step 2: Enable the PHY with link integrity test and auto-negotiation 
   // turned off
      
      // A. Disable the Transmitter Power Save Option and Configure Options
      TXPWR = 0x80;//禁止传输电源
      PHYCF = ( SMSQ | JABBER | ADPAUSE | AUTOPOL );//配置phy工作方式

      // B. Enable the Physical Layer
      PHYCN = PHYEN;//允许phy

      // C. Wait for the physical layer to power up
      wait_ms(10);//等待phy层上电完成

      // D. Enable the Transmitter and Receiver
      PHYCN = ( PHYEN | TXEN | RXEN );//允许物理层开始工作
   
   // Step 3: Poll the Wake-on-Lan Interrupt
      
      // A. Clear Interrupt Flags
      temp_char = INT1;//清除中断标志

      // B. Start a new timeout for 1.5 seconds
      reset_timeout(ONE_SECOND+ONE_SECOND/2);
      
      // C. Check for a signal
      while(1)
      {
         // If a signal is detected, wait 250 ms, then continue
         if(INT1RD & WAKEINT){//如果检测到信息,再延时250ms
            wait_ms(250);
            break;
         }

         // If no signal is deteced, wait 1.5s, then continue
         if(timeout_expired()){//1.5秒超时,也继续
            break;
         }

      }


	AB4_LED1 = !AB4_LED1;
   //--------------------------------------------------------------------------
   // Physical Layer Initialization (Section 15.7 of CP220x Datasheet)
   //--------------------------------------------------------------------------
   //物理层初始化
   // Step 1: Synchronization procedure implemented above

   // Step 2: Disable the physical layer
   PHYCN = 0x00; 
   
   // Step 3: Configure the desired physical layer options including 
   // auto-negotiation and link integrity
   PHYCF = ( SMSQ | LINKINTG | JABBER | AUTONEG | ADPAUSE | AUTOPOL );
  
   // Step 4: Enable the physcial layer
      
      // A. Enable the Physical Layer
      PHYCN = PHYEN;

      // B. Wait for the physical layer to power up
      wait_ms(10);
//	for(temp_char=0; temp_char<250;temp_char++)
//		;

      // C. Enable the Transmitter and Receiver
      // Auto-negotiation begins now
      PHYCN = ( PHYEN | TXEN | RXEN );
  

   // Step 5: Wait for auto-negotiation to complete
//		for(temp_char=0; temp_char<250;temp_char++)
//		;
      // Clear INT1 Interrupt Flags
      temp_char = INT1;
        
      // Start a six second timeout
      reset_timeout(6*ONE_SECOND);
  
      // Check for autonegotiation fail or complete flag
     while(1){//检查网络是否连接,返回连接状态
         // If Auto-Negotiation Completes/Fails, break
         if(INT1RD & (ANCINT | ANFINT)){
            break;            
         }
      
         // If Timeout Expires, break
         if(timeout_expired()){
            break;            
         }
      }


      // Mask out all bits except for auto negotiation bits 
      temp_char = INT1RD;
      temp_char &= (ANCINT | ANFINT);

      // Check if Auto-Negotiation has FAILED 
      if(temp_char & ANFINT){
      
         // Auto-Negotiation has failed
         retval = LINK_ERROR;

         #if(UART_ENABLED)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -