📄 cp2200.c
字号:
// packet_type contents of ethertype field
// return: void
// date: 2006/11/10
// note: cp220x_eth.c, datasheet page 48
// write: han
// (8 bytes) 48-bit 48-bit 16-bit 0-1500 bytes
// ----------------------------------------------------------------------
// | Preamble| SFD | Dest |Source| Type/Length |Data Field | Pad | FCS |
// | | | Addr | Addr | Field | | | (CRC) |
// ----------------------------------------------------------------------
// supplied by | supplied by the MCU | supplied
// CP220x | (minimum 64 bytes) | by CP220x
==========================================================*/
void CP220x_Send( union ethernet_address_type *pDestAddr, union netcard *txdnet,
unsigned int buffer_length, unsigned int packet_type)
{
int i;
unsigned int ramaddr;
unsigned char tmp;
// Define Macro to increment the RAM address Pointer
#define INC_RAMADDR {ramaddr++; \
WriteReg(RAMADDRH,(ramaddr >> 8));\
WriteReg(RAMADDRL,(ramaddr & 0x00FF));}
// Step 1: Poll TXBUSY until it becomes 0x00
tmp = ReadReg(TXBUSY);
while(tmp)
{
tmp = ReadReg(TXBUSY);
}
// Step 2: Set the TXSTARTH:TXSTARTL address to 0x0000
WriteReg(TXSTARTH,0x00);
WriteReg(TXSTARTL,0x00);
// Step 3: Load data into transmit buffer
// When the random access method is used, we do not need to check for
// aborted packets. This method will be slightly slower than the Autowrite
// method, however, it reduces code space requirements.
// Setup RAM Address Pointer To 0x0000
WriteReg(RAMADDRH,0x00);
WriteReg(RAMADDRL,0x00);
ramaddr = 0x0000;
// Step 3a: Load the source address
for(i = 0; i < 6; i++)
{
txdnet->etherframe.sourcenodeid[i] = myNode.node.mac[i];
}
// Step 3b: Load the destination address
for(i = 0; i < 6; i++)
{
txdnet->etherframe.destnodeid[i] = pDestAddr->bytes[i];
}
// Step 3c: Load the Type/Length Field
txdnet->etherframe.protocal = packet_type;
// Step 3d: Load the packet payload
for(i = 4; i < buffer_length+4;i++)
{
WriteReg(RAMTXDATA,txdnet->bytedata.bytebuf[i]);
INC_RAMADDR;
}
// Step 3e: Pad short packets
while(ramaddr < 64)
{
WriteReg(RAMTXDATA,0);
INC_RAMADDR;
}
// Set the TXENDH:TXENDL address to <ramaddr - 1>
ramaddr--;
WriteReg(TXENDH,(ramaddr >> 8));
WriteReg(TXENDL,(ramaddr & 0x00FF));
// Step 4: Set the TXSTARTH:TXSTARTL address back to 0x0000
WriteReg(TXSTARTH,0x00);
WriteReg(TXSTARTL,0x00);
// Step 5: Write '1' to TXGO to begin transmission
WriteReg(TXCN,0x01);
}
/*==========================================================
// function: reads the current packet from the CP220x receive buffer and
// copies it to the passed buffer.
// Parameter: rxdnet received packet buffer, max 314bytes
// return: unsigned int number of bytes added to the buffer
// date: 2006/11/10
// note: cp220x_eth.c, datasheet page 58
// write: han
// --------------------------------------------------------------------------
// | Preamble | SFD | Dest | Source | Type/Length | Data Field | Pad | FCS |
// | | | Addr | Addr | Field | | | (CRC) |
// --------------------------------------------------------------------------
// supplied by | supplied by the MCU | supplied by
// CP220x | | CP220x
//-----------------------------------------------------------------------------
==========================================================*/
unsigned int CP220x_Receive(union netcard *rxdnet)
{
unsigned char rx_ok;
unsigned char skip = 0;
unsigned int cplen;
unsigned int i;
unsigned char tmp;
// Step 1: Check the RXOK bit to see if packet was received correctly
rx_ok = (ReadReg(CPINFOL) & RXOK) && (ReadReg(CPINFOH) & RXVALID);
// Step 2: If packet received correctly, read the length, otherwise, skip packet.
if(rx_ok)
{
// Read the packet length
cplen = ((unsigned int)ReadReg(CPLENH) << 8);
cplen += ReadReg(CPLENL);
}
else
{
// Set packet length to zero
cplen = 0;
// Skip packet
skip = 1;
}
// Step 3: Read the entire packet from the buffer
if(cplen <= 314)
{
for(i = 4; i < cplen+4; i++)
{
rxdnet->bytedata.bytebuf[i] = ReadReg(RXAUTORD);
}
rxdnet->etherframe.length = cplen-4;
}
else
{
// Set packet length to zero
cplen = 0;
// Skip packet
skip = 1;
}
// Step 4: Skip the packet, or clear the valid bit if the entire packet
// has been unloaded from the buffer.
tmp = ReadReg(RXCN);
if(skip)
{
WriteReg(RXCN,(tmp | 0x02)); // Skip the packet
}
else
{
WriteReg(RXCN,(tmp |= 0x04)); // Clear the valid bit only
}
// If there are no more packets in the receive buffer, enable reception
tmp = ReadReg(TLBVALID);
if(tmp == 0x00)
{
WriteReg(RXCN,0x00);
}
// Return the number of bytes added to the buffer
return cplen-4;
}
unsigned char CPRcv(void)
{
unsigned char tmp;
tmp = ReadReg(CPINFOH);
if(tmp & RXVALID)
{
tmp = 1;
}
else
{
tmp = 0;
}
return tmp;
}
//-----------------------------------------------------------------------------
// 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;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -