📄 main.#2
字号:
//-----------------------------------------------------------------------------
// main.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This file contains the main routine for the CP220x Send and Receive example.
//
// 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"
#include "arp.h"
#include "tcp.h"
#include "http.h"
#include "ip.h"
#include "Analog.h"
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void main(void);
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
///IPADDRESS xdata MYIP;
MACADDRESS xdata MYMAC;
///MACADDRESS xdata BROADCASTMAC;
MACADDRESS xdata DESTMAC;
// This sets my hardware address to 00:01:02:03:04:05
UCHAR xdata my_hwaddr[6] _at_ 4001;
// Hardware addr to send a broadcast
UCHAR code broadcast_hwaddr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// This sets my IP address to 192.168.0.10
ULONG code my_ipaddr = 0xC0A8000AL;
// This sets my subnet mask to 255.255.255.0
ULONG code my_subnet = 0xFFFFFF00L;
ULONG code gateway_ipaddr = 0L;
unsigned char xdata RX_BUFF[1500];
unsigned char xdata TX_BUFF [256];
UINT volatile event_word;
char xdata text[20];
UCHAR idata debug;
UCHAR idata rcve_buf_allocated;
#define LINK_ERROR 0x20
char xdata inbuf1[1500] _at_ 1000;
char xdata outbuf1[1500] _at_ 2500;
UINT volatile event_word;
UCHAR xdata * rcve_frame(void);
UCHAR idata rcve_buf_allocated;
void eth_rcve(UCHAR xdata * inbuf);
void CP220x_Send( MACADDRESS* pDestAddr, unsigned char* buffer,
unsigned int buffer_length, unsigned int packet_type);
unsigned int CP220x_Receive(unsigned char* buffer, unsigned int buffer_length);
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
unsigned char error_code;
unsigned int i;
unsigned int num_bytes;
UINT event_word_copy;
UCHAR xdata * inbuf;
//----------------------------------
// MCU Initialization
//----------------------------------
// Initialize the MCU for Ethernet
Reset_Init();
init_tcp();
init_http();
/// init_adc();
init_arp();
// Turn off yellow LED.
YELLOW_LED = 0;
//----------------------------------
// Initialize Global Variables
//----------------------------------
/*
// Initialize broadcast MAC address
BROADCASTMAC.Int[0] = 0xFFFF;
BROADCASTMAC.Int[1] = 0xFFFF;
BROADCASTMAC.Int[2] = 0xFFFF;
// Initialize destination MAC address
DESTMAC.Int[0] = 0x0012;
DESTMAC.Int[1] = 0x3F51;
DESTMAC.Int[2] = 0x3589;
// Initialize MYIP address
MYIP.Char[0] = 0;
MYIP.Char[1] = 0;
MYIP.Char[2] = 0;
MYIP.Char[3] = 1;
*/
// Initialize Transmit Buffer
for(i = 0; i < sizeof(TX_BUFF); i++)
{
TX_BUFF[i] = 0;
}
//----------------------------------
// Infinite Execution Loop
// This loop allows the CP220x
// to be re-initialized each time
// it is unplugged from a network.
//----------------------------------
do{
//-------------------------------
// Initialize the CP220x
//-------------------------------
//-----------------------------------------------------------------------
// Part A: Hardware Reset and Reset Initialization
//
// Perform the hardware reset and recommended reset initialization
// procedure listed in the CP220x datasheet.
//
// After completion, check the return code for any of the following
// possible errors:
// OSC_ERROR - oscillator error
// CAL_ERROR - self-initialization error
// MEM_ERROR - external memory bus error
//-----------------------------------------------------------------------
EA = 0; // Disable Interrupts
error_code = CP220x_HW_Reset(); // Reset and Initialize the CP220x
// Check for the three possible error codes
if(error_code & (OSC_ERROR | CAL_ERROR | MEM_ERROR))
{
// Print an Error Message
#if(UART_ENABLED)
printf("\n\n *** Please Reset Device ***\n");
#endif
// Place device in an infinite loop if an error occurs
while(1);
}
//-----------------------------------------------------------------------
// Part B: Physical Layer Initialization
//
// Initialize the physical layer (PHY) for Autonegotiation
//
// After completion, check the return code for any of the following
// possible errors:
// LINK_ERROR - The device is not connected to a network or
// autonegotiation has failed.
//
// If an error occurs, keep re-trying until the device is able to
// complete a successful auto-negotiation or is plugged into a network.
//-----------------------------------------------------------------------
while(1){
error_code = PHY_Init(); // Initialize the physical layer
if(error_code & LINK_ERROR) // Check for the possible error codes
{ // On Failure:
YELLOW_LED = !YELLOW_LED; // Toggle indicator
}
else
{ // On Success:
YELLOW_LED = 1; // Turn on the indicator
break; // Exit initialization loop
}
}
//-----------------------------------------------------------------------
// Part C: MAC Initialization
//
// Initialize the media acccess controller (MAC)
//
// Currently, the MAC initialization routine does not return any errors.
//-----------------------------------------------------------------------
MAC_Init(); // Initialize the MAC
//-----------------------------
// Main Application Loop
//-----------------------------
// Initialize timer to time out in one second
reset_timeout(ONE_SECOND);
// Reset receive buffer and enable interrupts
RXCN = RXCLEAR;
ET3 = 1;
EA = 1;
while(1){
//--------------------------------------------------------------------
// Task #1
// Exit application loop if device is removed from network
//--------------------------------------------------------------------
if(!(PHYCN & LINKSTA)) // Check the link status bit
{ // On detection of bad link:
YELLOW_LED = 0; // Turn off the indicator
break; // Exit application loop
}
//--------------------------------------------------------------------
// Task #2
// Check if a packet has been received
//--------------------------------------------------------------------
if(CPINFOH & RXVALID) // Check if the current packet is valid
{ // On detection of a valid packet:
// Unload packet from the receive buffer and store in <RX_BUFF>
num_bytes = CP220x_Receive(RX_BUFF, sizeof(RX_BUFF));
//-----------------------------------------------------------------
// Application-Specific Code:
// If UART is enabled, print packet statistics to the terminal.
//-----------------------------------------------------------------
#if(UART_ENABLED)
// Print the number of bytes received.
printf("Received Packet (%i bytes)\n", num_bytes);
// Print each byte to the UART terminal
for(i = 0; i < num_bytes; i++)
{
// Create a new row every 16 bytes
if((i & 0x000F) == 0x0000)
{
// Print the row address in the left column
printf("\n%04X ", i);
}
// Print the byte
printf("%02bX ", RX_BUFF[i]);
}
// Print two new-line characters for clarity (whitespace).
printf("\n\n");
#endif
}
//--------------------------------------------------------------------
// Task #3
// Check if timer has expired. Send one packet every second.
//--------------------------------------------------------------------
if(timeout_expired()){
// Reset timer to time out again after another second.
reset_timeout(ONE_SECOND);
// Send one packet.
CP220x_Send(&DESTMAC, TX_BUFF, sizeof(TX_BUFF), IP_PACKET);
}
//////////copy web340 // 下面代码为设置何种事件中断发生,以做出相应的处理
if(CPINFOH & RXVALID)
event_word |= EVENT_ETH_ARRIVED;
event_word_copy = event_word;
EA = 1;
if (event_word_copy & EVENT_ETH_ARRIVED)
{
EA = 0;
event_word &= (~EVENT_ETH_ARRIVED);
EA = 1;
inbuf = rcve_frame();
if (inbuf != NULL)
{
eth_rcve(inbuf);
if (rcve_buf_allocated)
{
rcve_buf_allocated = FALSE;
}
}
}
else if (event_word_copy & EVENT_TCP_RETRANSMIT) //判断TCP传输是否超时
{
event_word &= (~EVENT_TCP_RETRANSMIT);
EA = 1;
tcp_retransmit();
}
else if (event_word_copy & EVENT_TCP_INACTIVITY)//判断TCP休止时间
{
event_word &= (~EVENT_TCP_INACTIVITY);
EA = 1;
tcp_inactivity();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -