📄 main.#3
字号:
{
event_word &= (~EVENT_ARP_RETRANSMIT);
EA = 1;
arp_retransmit();
}
else if (event_word_copy & EVENT_AGE_ARP_CACHE)
{
event_word &= (~EVENT_AGE_ARP_CACHE);
EA = 1;
age_arp_cache();
}
else if (event_word_copy & EVENT_READ_ANALOG) //读AD输入的时间
{
event_word &= (~EVENT_READ_ANALOG);
EA = 1;
/// read_analog_inputs();
}
else if (event_word_copy & EVENT_RS232_ARRIVED)
{
event_word &= (~EVENT_RS232_ARRIVED);
EA = 1;
}
//////////copy web340
//-----------------------------
// End Main Application Loop
//-----------------------------
}
// Main do-while initialization loop -- code execution will now restart
} while(1);
}
//-----------------------------------------------------------------------------
// Ethernet_ISR
//-----------------------------------------------------------------------------
//
// This Interrupt Service Routine checks the number of packets in the receive
// buffer after each packet is received. If the buffer has 7 or more packets,
// then packet reception is disabled. Packet reception is re-enabled in the
// CP220x_Receive() routine after all packets have been unloaded.
//
// Allowed Latency: This interrupt service routine should be serviced within
// 51.2 us of the interrupt flag. This is the amount of time
// it takes to receive a minimum-sized 64-byte packet. To
// allow for greater latency (51.2us/packet), the maximum
// number of packets allowed in the receive buffer can be
// decreased.
//-----------------------------------------------------------------------------
void Ethernet_ISR(void)
{
unsigned char interrupt_read;
unsigned char valid_bits;
unsigned char num_packets;
// Clear interrupt flags.
interrupt_read = INT1;
interrupt_read = INT0;
// Check for packet received interrupt
if( interrupt_read & RXINT)
{
// Count the number of packets in the receive buffer
// This is equal to the number of bits set to 1 in TLBVALID
valid_bits = TLBVALID;
// Loop accumulates the number of bits set in Valid_Bits and
// stores this value in i. Uses the Brian Kernighan method
// for counting bits
for(num_packets = 0; valid_bits; num_packets++)
{
valid_bits &= valid_bits - 1;
}
// If the receive buffer has 7 packets, then disable reception.
if( num_packets >= 7)
{
RXCN = RXINH; // Inhibit New Packet Reception
}
}
// Check for the FIFO Full Interrupt
else if (interrupt_read & RXFINT)
{
// This interrupt could mean a few large packets were received,
// or a large number of small packets were received.
}
}
/**************************************************************************/
// 以太网帧接收函数
/**************************************************************************/
void eth_rcve(UCHAR xdata * inbuf)
{
ETH_HEADER xdata * eth;
eth = (ETH_HEADER xdata *)inbuf;
if (eth->frame_type < 1520) // 帧长度不能超过IEEE 802标准归定
{
return;
}
switch (eth->frame_type) //根据接收到的数据包类型,选择不同的处理方式
{
case ARP_PACKET:
arp_rcve(inbuf);
break;
case IP_PACKET:
ip_rcve(inbuf);
break;
default:break;
}
}
//-----------------------------------------------------------------------------
// CP220x_Send
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) MACADDRESS* pDestAddr - destination MAC address.
// 2) unsigned char* buffer - address of payload.
// 3) unsigned int buffer_length - length of payload.
// 4) unsigned int packet_type - contents of Ethertype field.
//
// This function sends an IEEE 802.3 Ethernet packet using the CP220x.
// Upon entry, there should be valid data in array <buffer>.
//
// (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( MACADDRESS* pDestAddr, unsigned char* buffer,
unsigned int buffer_length, unsigned int packet_type)
{
int i;
unsigned int ramaddr;
// Define Macro to increment the RAM address Pointer
#define INC_RAMADDR ramaddr++; \
RAMADDRH = (ramaddr >> 8);\
RAMADDRL = (ramaddr & 0x00FF);
// Step 1: Poll TXBUSY until it becomes 0x00
while(TXBUSY);
// Step 2: Set the TXSTARTH:TXSTARTL address to 0x0000
TXSTARTH = 0x00;
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
RAMADDRH = 0x00;
RAMADDRL = 0x00;
ramaddr = 0x0000;
// Step 3a: Load the destination address
for(i = 0; i < 6; i++){
RAMTXDATA = pDestAddr->Char[i];
INC_RAMADDR
}
// Step 3b: Load the source address
for(i = 0; i < 6; i++){
RAMTXDATA = MYMAC.Char[i];
INC_RAMADDR
}
// Step 3c: Load the Type/Length Field
RAMTXDATA = (packet_type >> 8);
INC_RAMADDR
RAMTXDATA = (packet_type & 0xFF);
INC_RAMADDR
// Step 3d: Load the packet payload
for(i = 0; i < buffer_length; i++){
RAMTXDATA = buffer[i];
INC_RAMADDR
}
// Step 3e: Pad short packets //将不足64字节长度的数据填充到64字节
while(ramaddr < 64){
RAMTXDATA = 0;
INC_RAMADDR
}
// Set the TXENDH:TXENDL address to <ramaddr - 1>
ramaddr--;
TXENDH = (ramaddr >> 8);
TXENDL = (ramaddr & 0x00FF);
// Step 4: Set the TXSTARTH:TXSTARTL address back to 0x0000
TXSTARTH = 0x00;
TXSTARTL = 0x00;
// Step 5: Write '1' to TXGO to begin transmission
TXCN = 0x01;
}
//-----------------------------------------------------------------------------
// CP220x_Receive
//-----------------------------------------------------------------------------
//
// This function reads the current packet from the CP220x receive buffer and
// copies it to the passed buffer. The data copied to the buffer includes the
// 14-byte Ethernet Header and the Data Field.
//
// Returns the number of bytes added to the buffer.
//
// (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 by
// CP220x | | CP220x
//-----------------------------------------------------------------------------
unsigned int CP220x_Receive(unsigned char* buffer, unsigned int buffer_length)
{
bit rx_ok;
bit skip = 0;
UINT_struct cplen;
unsigned int i;
// Step 1: Check the RXOK bit to see if packet was received correctly
rx_ok = (CPINFOL & RXOK) && (CPINFOH & RXVALID);
// Step 2: If packet received correctly, read the length, otherwise, skip packet.
if(rx_ok){
// Read the packet length
cplen.Char[0] = CPLENH;
cplen.Char[1] = CPLENL;
} else {
// Set packet length to zero
cplen.Int = 0;
// Skip packet
skip = 1;
}
// Step 3: Read the entire packet from the buffer
// If packet will fit in the buffer
if(buffer_length >= cplen.Int){
// Copy entire packet
for(i = 0; i < cplen.Int; i++){
*buffer++ = RXAUTORD;
}
} else {
// Set packet length to zero
cplen.Int = 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.
if(skip)
{
RXCN |= 0x02; // Skip the packet
}
else
{
RXCN |= 0x04; // Clear the valid bit only
}
// If there are no more packets in the receive buffer, enable reception
if(TLBVALID == 0x00)
{
RXCN = 0x00;
}
// Return the number of bytes added to the buffer
return cplen.Int;
}
/********************************************************************/
//上位机与下位进行交互程序
/********************************************************************/
void LightONOFF(bit b)
{
if (b)
{
led=1;
}
else
{
led=0;
}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -