📄 main.#1
字号:
//------------------------------------------------------------------------------// main.c//------------------------------------------------------------------------------// Copyright (C) 2005 Silicon Laboratories, Inc.//// Date: 05/19/06 09:38:45// Target: C8051F34x //// Description:// This file contains the main routine, MCU initialization code, and// callback functions used by the TCP/IP Library.//// Generated by TCP/IP Configuration Wizard Version 3//#include "mn_userconst.h" // TCP/IP Library Constants#include "mn_stackconst.h" // TCP/IP Library Constants#include "mn_errs.h" // Library Error Codes#include "mn_defs.h" // Library Type definitions#include "mn_funcs.h" // Library Function Prototypes#include "VFILE_DIR\index.h"#include <c8051F340.h> // Device-specific SFR Definitions//------------------------------------------------------------------------------// Function Prototypes//------------------------------------------------------------------------------// Initialization Routinesvoid PORT_Init (void);void SYSCLK_Init (void);void EMIF_Init(void);int establish_network_connection();
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
// Disable watchdog timer
PCA0MD = 0x00;
// Initialize the MCU
PORT_Init();
SYSCLK_Init();
EMIF_Init();
while(1)
{
// Initialize the TCP/IP stack.
if (mn_init() < 0)
{
// If code execution enters this while(1) loop, the stack failed to initialize.
// Verify that all boards are connected and powered properly.
while(1);
}
// Connect to the network
establish_network_connection();
// Add web page to virtual file system.
// The main page MUST be called index.htm or index.html.
mn_vf_set_entry((byte *)"index.html", INDEX_SIZE, index_html, VF_PTYPE_FLASH);
// Start the Application Layer Services.
mn_server();
}
}
//-----------------------------------------------------------------------------
// establish_network_connection
//-----------------------------------------------------------------------------
//
// This function calls mn_ether_init() to initialize the CP2200 and attach to
// the network.
//
// If there is a network connection, the function returns 1.
//
// If there is no network connection, the function waits until either a
// connection appears or the CP2200 is reset before calling mn_ether_init()
// again. (The application may perform other tasks while polling
// link_status and ether_reset).
//
int establish_network_connection()
{
int retval;
do
{
// mn_ether_init() initializes the Ethernet controller.
// AUTO_NEG indicates that the controller will auto-negotiate.
retval = mn_ether_init(AUTO_NEG, 3, 0);
// If there is no link, poll link_status until it sets or the
// CP2200 resets and then call mn_ether_init() again.
if (retval == LINK_FAIL)
{
while(!link_status && !ether_reset);
}
// If retval is less than zero and is not AUTO_NEG_FAIL, there is a
// hardware error.
else if ((retval < 0) && (retval != AUTO_NEG_FAIL))
{
// Verify that the Ethernet controller is connected and powered properly.
// Verity that the EMIF has been configured at a speed compatible with the
// Ethernet controller.
while(1);
}
}while((retval < 0) && (retval != AUTO_NEG_FAIL));
return (1);
}
//-----------------------------------------------------------------------------// Initialization Routines//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// PORT_Init//-----------------------------------------------------------------------------//// Configure the Interrupts, Crossbar and GPIO ports//void PORT_Init (void){ IT01CF = 0x07; // Enable Interrupt 0 on P0.7 TCON &= ~0x01; // Make /INT0 level triggered XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX) XBR1 = 0x40; // Enable crossbar and enable // weak pull-ups P0MDOUT |= 0x40; // enable UTX as push-pull output P1MDOUT |= 0xC8; // /WR and /RD are push-pull P2MDOUT |= 0xFF; P3MDOUT |= 0xFF; P4MDOUT |= 0xFF;}//-----------------------------------------------------------------------------// EMIF_Init//-----------------------------------------------------------------------------//// Configure the External Memory Interface for both on and off-chip access.//void EMIF_Init (void){ EMI0CF = 0x0B; // Multiplex mode EMI0TC = EMIF_TIMING; // This constant may be modified // according to SYSCLK to meet the // timing requirements for the CP2200 EMI0CN = BASE_ADDRESS; // Page of XRAM accessed by EMIF}//-----------------------------------------------------------------------------// SYSCLK_Init//-----------------------------------------------------------------------------//// This routine initializes the system clock.//void SYSCLK_Init (void){ int i; OSCICN |= 0x03; // Configure internal oscillator for // its maximum frequency CLKMUL = 0x00; // Reset Clock Multiplier and select // internal oscillator as input source CLKMUL |= 0x80; // Enable the Clock Multiplier for(i = 0; i < 256; i++); // Delay at least 5us CLKMUL |= 0xC0; // Initialize the Clock Multiplier while(!(CLKMUL & 0x20)); // Wait for MULRDY => 1 RSTSRC = 0x06; // Enable missing clock detector // and VDD monitor FLSCL |= 0x10; // Set Flash Scale for 48MHz CLKSEL |= 0x03; // Select output of clock multiplier // as the system clock.}//-----------------------------------------------------------------------------// ether_reset_low//-----------------------------------------------------------------------------//// This routine drives the reset pin of the ethernet controller low.//void ether_reset_low(){ P0 &= ~0x40; // Pull reset low}//-----------------------------------------------------------------------------// ether_reset_high//-----------------------------------------------------------------------------//// This routine places the reset pin in High-Z allowing it to be pulled up // using the external pull-up resistor.//// Additionally, this routine waits for the reset pin to read high before// exiting.//void ether_reset_high (void){ P0 |= 0x40; // Allow /RST to rise while(!(P0 & 0x40)); // Wait for /RST to go high}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -