📄 cp220x_core.c
字号:
// Enable Link LED and Activity LED
IOPWR = 0x0C;
} else
// Timeout Occured.
{
// Timeout
retval = LINK_ERROR;
}
return retval;
}
//-----------------------------------------------------------------------------
// MAC_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Initializes the MAC and programs the MAC address using the MAC address
// stored at address 0x1FFA in CP220x Flash.
//-----------------------------------------------------------------------------
void MAC_Init(void)
{
// Check the duplex mode and perform duplex-mode specific initializations
if(PHYCN & 0x10){
// The device is in full-duplex mode, configure MAC registers
// Padding is turned on.
MAC_Write(MACCF, 0x40B3);
MAC_Write(IPGT, 0x0015);
} else {
// The device is in half-duplex mode, configure MAC registers
// Padding is turned off.
MAC_Write(MACCF, 0x4012);
MAC_Write(IPGT, 0x0012);
}
// Configure the IPGR register
MAC_Write(IPGR, 0x0C12);
// Configure the MAXLEN register to 1518 bytes
MAC_Write(MAXLEN, 0x05EE);
// Copy MAC Address Stored in Flash to MYMAC
FLASHADDRH = 0x1F;
FLASHADDRL = 0xFA;
MYMAC.Char[0] = FLASHAUTORD;
MYMAC.Char[1] = FLASHAUTORD;
MYMAC.Char[2] = FLASHAUTORD;
MYMAC.Char[3] = FLASHAUTORD;
MYMAC.Char[4] = FLASHAUTORD;
MYMAC.Char[5] = FLASHAUTORD;
// Program the MAC address
MAC_SetAddress(&MYMAC);
// Enable Reception and configure Loopback mode
MAC_Write(MACCN, 0x0001); // Enable Reception without loopback
}
//-----------------------------------------------------------------------------
// Indirect MAC Register Access
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// MAC_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) unsigned char mac_reg_offset - indirect register address
// 2) unsigned int mac_reg_data - the data to write to mac_reg_offset.
//
// Writes the value <mac_reg_data> to the indirect MAC register located at
// <mac_reg_offset>.
//-----------------------------------------------------------------------------
void MAC_Write(unsigned char mac_reg_offset, unsigned int mac_reg_data)
{
// Step 1: Write the address of the indirect register to MACADDR.
MACADDR = mac_reg_offset;
// Step 2: Copy the contents of <mac_reg_data> to MACDATAH:MACDATAL
MACDATAH = (mac_reg_data >> 8); // Copy High Byte
MACDATAL = (mac_reg_data & 0xFF); // Copy Low Byte
// Step 3: Perform a write on MACRW to transfer the contents of MACDATAH:MACDATAL
// to the indirect MAC register.
MACRW = 0;
return;
}
//-----------------------------------------------------------------------------
// MAC_SetAddress
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) MACADDRESS* pMAC - pointer to a 6-byte MAC address structure.
//
// Sets the current MAC address to the MAC address pointed to by <pMAC>.
//-----------------------------------------------------------------------------
void MAC_SetAddress(MACADDRESS* pMAC)
{
UINT temp_int;
temp_int.Char[0] = pMAC->Char[5];
temp_int.Char[1] = pMAC->Char[4];
MAC_Write(MACAD0, temp_int.Int);
temp_int.Char[0] = pMAC->Char[3];
temp_int.Char[1] = pMAC->Char[2];
MAC_Write(MACAD1, temp_int.Int);
temp_int.Char[0] = pMAC->Char[1];
temp_int.Char[1] = pMAC->Char[0];
MAC_Write(MACAD2, temp_int.Int);
return;
}
//-----------------------------------------------------------------------------
// CP220x Flash Routines
//-----------------------------------------------------------------------------
/* Not used. Commented to save code space.
//-----------------------------------------------------------------------------
// CPFLASH_ByteRead
//-----------------------------------------------------------------------------
//
// Return Value :
// unsigned char - the value of the Flash byte.
//
// Parameters :
// 1) unsigned int addr - the address in CP220x Flash.
//
// Reads a Flash byte and returns its value.
//-----------------------------------------------------------------------------
unsigned char CPFLASH_ByteRead (unsigned int addr)
{
// Set the Flash Address Pointer to <addr>
FLASHADDRH = (addr >> 8); // Copy High Byte
FLASHADDRL = (addr & 0xFF); // Copy Low Byte
// Read and Return the value in the Flash Data Register
return FLASHDATA;
}
//-----------------------------------------------------------------------------
// poll_flash_busy
//-----------------------------------------------------------------------------
//
// Return Value :
// unsigned char - Returns '0' on success or FLASH_ERROR if a problem
// is encountered.
//
// Parameters : None
//
// Waits for a Flash operation to start and complete
//
// Return Values:
//
//-----------------------------------------------------------------------------
unsigned char poll_flash_busy (void)
{
// Start Millisecond Timer and set timeout
reset_timeout(DEFAULT_TIMEOUT);
// Wait for operation to end
while((FLASHSTA & 0x08)){
if(!AB4_RST_State()){
#if(UART_ENABLED)
puts("Reset Pin Driven Low. Could indicate power failure.");
#endif
return FLASH_ERROR;
}
if(timeout_expired()){
#if(UART_ENABLED)
puts("Timeout: Flash operation has not ended.");
#endif
return FLASH_ERROR;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// CPFLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value :
// unsigned char - Returns '0' on success or FLASH_ERROR if a problem
// is encountered.
//
// Parameters :
// 1) unsigned int addr - the address of the Flash byte.
// 2) unsigned char byte - the data to write to Flash.
//
// Writes the value <byte> to the Flash address <addr>.
//
// Note: The addresses 0x1FFA through 0x1FFF cannot be written using this
// function because they contain the MAC address.
//
// Note: Software calling this function must ensure that the target Flash
// byte has been erased (value = 0xFF).
//
// Note: The Flash must be unlocked prior to calling this function.
//-----------------------------------------------------------------------------
unsigned char CPFLASH_ByteWrite (unsigned int addr, char byte)
{
// Check if address is in-range
if(addr < 0x1FFA)
{
// Set the Flash Address Pointer to <addr>
FLASHADDRH = (addr >> 8); // Copy High Byte
FLASHADDRL = (addr & 0xFF); // Copy Low Byte
// Write the Flash unlock sequence 0xA5, 0xF1
FLASHKEY = 0xA5;
FLASHKEY = 0xF1;
// Initiate the Flash write
FLASHDATA = byte;
// Wait for the Flash operation to start and complete
return poll_flash_busy();
}
return FLASH_ERROR;
}
//-----------------------------------------------------------------------------
// CPFLASH_PageErase
//-----------------------------------------------------------------------------
//
// Return Value :
// unsigned char - Returns '0' on success or FLASH_ERROR if a problem
// is encountered.
//
// Parameters :
// 1) unsigned int addr - the address of the Flash Page.
//
// Erases the Flash page containing address <addr>.
//
// Note: The last Flash page (0x1E00 - 0x1FFF) cannot be erased using this
// function because it contains the MAC address.
//
// Note: All data stored on a Flash page will be lost once the page is erased.
//
// Note: The Flash must be unlocked prior to calling this function.
//-----------------------------------------------------------------------------
unsigned char CPFLASH_PageErase (unsigned int addr)
{
// Check if address is in-range
if(addr < 0x1E00)
{
// Set the Flash Address Pointer to <addr>
FLASHADDRH = (addr >> 8); // Copy High Byte
FLASHADDRL = (addr & 0xFF); // Copy Low Byte
// Write the Flash unlock sequence 0xA5, 0xF1
FLASHKEY = 0xA5;
FLASHKEY = 0xF1;
// Initiate the Flash erase
FLASHERASE = 0x01;
// Wait for the Flash operation to start and complete
return poll_flash_busy();
}
return FLASH_ERROR;
}
*/
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -