📄 cp220x_core.c
字号:
//-----------------------------------------------------------------------------
// 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.0
// -Initial Release (FB)
// -30 MAY 2006
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "global.h"
//-----------------------------------------------------------------------------
// 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
CP220x_RST_Low();
wait_ms(20);
//--------------------------------------------------------------------------
// Step 1: Wait for the reset pin to rise.
//--------------------------------------------------------------------------
CP220x_RST_High();
//--------------------------------------------------------------------------
// Step 2 + 3: Wait for oscillator and self initializaion to complete
//--------------------------------------------------------------------------
// 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){
// Check the state of the reset pin
if(!AB4_RST_State()){
return OSC_ERROR;
}
// Check the state of the one second timeout
if(timeout_expired()){
return OSC_ERROR;
}
}
// 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 {
temp_char = INT0RD;
if(timeout_expired()){
return CAL_ERROR;
}
} while(((temp_char & 0x30) != 0x30) || (temp_char == 0xFF)) ;
//--------------------------------------------------------------------------
// Additional Step: Verify Communication
//--------------------------------------------------------------------------
// Verify communication
if(RXHASHH != 0x04){
return MEM_ERROR;
}
// Read and write the RAM at 0x7FF in the transmit buffer
RAMADDRH = 0x07;
RAMADDRL = 0xFF;
RAMTXDATA = 0x00;
if(RAMTXDATA != 0x00){
return MEM_ERROR;
}
RAMTXDATA = 0xFF;
if(RAMTXDATA != 0xFF){
return MEM_ERROR;
}
//--------------------------------------------------------------------------
// 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;
// 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 );
// B. Enable the Physical Layer
PHYCN = PHYEN;
// C. Wait for the physical layer to power up
wait_ms(10);
// 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){
wait_ms(250);
break;
}
// If no signal is deteced, wait 1.5s, then continue
if(timeout_expired()){
break;
}
}
//--------------------------------------------------------------------------
// 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);
// C. Enable the Transmitter and Receiver
// Auto-negotiation begins now
PHYCN = ( PHYEN | TXEN | RXEN );
// Step 5: Wait for auto-negotiation to complete
// 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;
} else
// Check if Auto-Negotiation has PASSED
if(temp_char == ANCINT){
// Auto-Negotiation has passed
retval = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -