📄 main.c
字号:
/*************************************************************************
*
* Used with ICCARM and AARM.
*
* (c) Copyright IAR Systems 2007
*
* File name : main.c
* Description : Main module
*
* History :
* 1. Date : February 28, 2007
* Author : Stanimir Bonev
* Description : Create
*
* This example project shows how to use the IAR Embedded Workbench for ARM
* to develop code for the IAR LPC-P2378-SK board.
* It implements WEB server.
* The default IP address is:
* 192.168.0.100
* The physical MAC address is (defined in uipopt.h):
* 00-ff-ff-ff-ff-ff
*
* The UART 0 is used to send debbug messages. It settings are - 115200, 8bits,
* 1 stop, no parity
*
* Jumpers:
* PWR_SEL - depending of power source
* RST_E - unfilled
* ISP_E - unfilled
*
* Note:
* After power-up the controller get clock from internal RC oscillator that
* is unstable and may fail with J-Link auto detect, therefore adaptive clocking
* should always be used. The adaptive clock can be select from menu:
* Project->Options..., section Debugger->J-Link/J-Trace JTAG Speed - Adaptive.
*
**************************************************************************
* Copyright (c) 2001-2003, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the uIP TCP/IP stack.
*
**************************************************************************
*
* $Revision: 1.0 $
*
**************************************************************************/
#include "includes.h"
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
const UartLineCoding_t UartLineCoding =
{
115200,UART_ONE_STOP_BIT,UART_NO_PARITY, UART_WORD_WIDTH_8
};
/*************************************************************************
* Function Name: fiq_handler
* Parameters: none
*
* Return: none
*
* Description: IRQ handler
*
*************************************************************************/
__fiq __arm void FIQ_Handler (void)
{
while(1);
}
/*************************************************************************
* Function Name: irq_handler
* Parameters: none
*
* Return: none
*
* Description: IRQ handler
*
*************************************************************************/
__irq __arm void IRQ_Handler (void)
{
void (*interrupt_function)();
unsigned int vector;
vector = VICADDRESS; // Get interrupt vector.
interrupt_function = (void(*)())vector;
if(interrupt_function != NULL)
{
interrupt_function(); // Call vectored interrupt function.
}
else
{
VICADDRESS = 0; // Clear interrupt in VIC.
}
}
/*************************************************************************
* Function Name: VIC_Init
* Parameters: void
* Return: void
*
* Description: Initialize VIC
*
*************************************************************************/
void VIC_Init(void)
{
volatile unsigned long * pVecAdd, *pVecCntl;
int i;
// Assign all interrupt channels to IRQ
VICINTSELECT = 0;
// Disable all interrupts
VICINTENCLEAR = 0xFFFFFFFF;
// Clear all software interrupts
VICSOFTINTCLEAR = 0xFFFFFFFF;
// VIC registers can be accessed in User or privileged mode
VICPROTECTION = 0;
// Clear interrupt
VICADDRESS = 0;
// Clear address of the Interrupt Service routine (ISR) for vectored IRQs
// and disable all vectored IRQ slots
for(i = 0, pVecCntl = &VICVECTPRIORITY0, pVecAdd = &VICVECTADDR0; i < 32; ++i)
{
*pVecCntl++ = *pVecAdd++ = 0;
}
}
/*************************************************************************
* Function Name: VIC_SetVectoredIRQ
* Parameters: void(*pIRQSub)()
* unsigned int VicIrqSlot
* unsigned int VicIntSouce
*
* Return: void
*
* Description: Init vectored interrupts
*
*************************************************************************/
void VIC_SetVectoredIRQ(void(*pIRQSub)(), unsigned int Priority,
unsigned int VicIntSource)
{
unsigned long volatile *pReg;
// load base address of vectored address registers
pReg = &VICVECTADDR0;
// Set Address of callback function to corresponding Slot
*(pReg+VicIntSource) = (unsigned long)pIRQSub;
// load base address of ctrl registers
pReg = &VICVECTPRIORITY0;
// Set source channel and enable the slot
*(pReg+VicIntSource) = Priority;
// Clear FIQ select bit
VICINTSELECT &= ~(1<<VicIntSource);
}
/*************************************************************************
* Function Name: InitClock
* Parameters: void
* Return: void
*
* Description: Initialize PLL and clocks' dividers. Hclk - 288MHz,
* Usbclk - 48MHz
*
*************************************************************************/
void InitClock(void)
{
// 1. Init OSC
SCS_bit.OSCRANGE = 0;
SCS_bit.OSCEN = 1;
// 2. Wait for OSC ready
while(!SCS_bit.OSCSTAT);
// 3. Disconnect PLL
PLLCON_bit.PLLC = 0;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 4. Disable PLL
PLLCON_bit.PLLE = 0;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 5. Select source clock for PLL
CLKSRCSEL_bit.CLKSRC = 1; // Selects the main oscillator as a PLL clock source.
// 6. Set PLL settings 288 MHz
PLLCFG_bit.MSEL = 24-1;
PLLCFG_bit.NSEL = 2-1;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 7. Enable PLL
PLLCON_bit.PLLE = 1;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// 8. Wait for the PLL to achieve lock
while(!PLLSTAT_bit.PLOCK);
// 9. Set clk divider settings
CCLKCFG = 6-1; // 1/6 Fpll
USBCLKCFG = 6-1; // 1/6 Fpll - 48 MHz
PCLKSEL0 = PCLKSEL1 = 0; // other peripherals
// 10. Connect the PLL
PLLCON_bit.PLLC = 1;
PLLFEED = 0xAA;
PLLFEED = 0x55;
}
/*************************************************************************
* Function Name: GpioInit
* Parameters: void
* Return: void
*
* Description: Reset all GPIO pins to default: primary function
*
*************************************************************************/
void GpioInit(void)
{
// Set to inputs
IO0DIR = \
IO1DIR = \
FIO0DIR = \
FIO1DIR = \
FIO2DIR = \
FIO3DIR = \
FIO4DIR = 0;
// Enable Fast GPIO0,1
SCS_bit.GPIOM = 1;
// clear mask registers
FIO0MASK =\
FIO1MASK =\
FIO2MASK =\
FIO3MASK =\
FIO4MASK = 0;
// Reset all GPIO pins to default primary function
PINSEL0 =\
PINSEL1 =\
PINSEL2 =\
PINSEL3 =\
PINSEL4 =\
PINSEL5 =\
PINSEL6 =\
PINSEL7 =\
PINSEL8 =\
PINSEL9 =\
PINSEL10= 0;
}
/*************************************************************************
* Function Name: SYS_GetFsclk
* Parameters: none
* Return: Int32U
*
* Description: return Sclk [Hz]
*
*************************************************************************/
Int32U SYS_GetFsclk(void)
{
Int32U Mul = 1, Div = 1, Osc, Fsclk;
if(PLLSTAT_bit.PLLC)
{
// when PLL is connected
Mul = PLLSTAT_bit.MSEL + 1;
Div = PLLSTAT_bit.NSEL + 1;
}
// Find clk source
switch(CLKSRCSEL_bit.CLKSRC)
{
case 0:
Osc = I_RC_OSC_FREQ;
break;
case 1:
Osc = MAIN_OSC_FREQ;
break;
case 2:
Osc = RTC_OSC_FREQ;
break;
default:
Osc = 0;
}
// Calculate system frequency
Fsclk = Osc*Mul*2;
Fsclk /= Div*(CCLKCFG+1);
return(Fsclk);
}
/*************************************************************************
* Function Name: SYS_GetFpclk
* Parameters: Int32U Periphery
* Return: Int32U
*
* Description: return Pclk [Hz]
*
*************************************************************************/
Int32U SYS_GetFpclk(Int32U Periphery)
{
Int32U Fpclk;
pInt32U pReg = (pInt32U)((Periphery < 32)?&PCLKSEL0:&PCLKSEL1);
Periphery &= 0x1F; // %32
Fpclk = SYS_GetFsclk();
// find peripheral appropriate periphery divider
switch((*pReg >> Periphery) & 3)
{
case 0:
Fpclk /= 4;
break;
case 1:
break;
case 2:
Fpclk /= 2;
break;
default:
Fpclk /= 8;
}
return(Fpclk);
}
/*************************************************************************
* Function Name: Dly100us
* Parameters: void *arg
* Return: void
*
* Description: Delay [100us]
*
*************************************************************************/
void Dly100us(void *arg)
{
volatile Int32U Dly = (Int32U)arg, Dly100;
for(;Dly;Dly--)
for(Dly100 = 500; Dly100; Dly100--);
}
/*************************************************************************
* Function Name: uip_log
* Parameters: none
*
* Return: none
*
* Description: Events logging
*
*************************************************************************/
void uip_log (char *m)
{
printf("uIP log message: %s\n", m);
}
/*************************************************************************
* Function Name: putchar
* Parameters: int c
* Return: int
*
* Description: put char on UART0
*
*************************************************************************/
int putchar(int c)
{
while(!UartWrite(UART_0,(pInt8U)&c,1));
return(c);
}
/*************************************************************************
* Function Name: IsBoardRevA
* Parameters: none
* Return: Boolean FALSE - revision != "A"
* TRUE - revision == "A"
*
* Description: Return true if the board's revision is "A"
*
*************************************************************************/
Boolean IsBoardRevA()
{
/* Check for board revision.
The board with PHY routing mistake have 4.7k resistor on port P0.21 and
internal pull-up can't set logical 1.
To fix routing mistake U4's pin 9 must be swaped with pin 22. */
BOARD_BUG_CTRL = 0x3;
for(volatile int i = 20; i; --i);
BOARD_BUG_CTRL = 0x0;
for(volatile int i = 20; i; --i);
return(!BOARD_BUG_FDATA);
}
/*************************************************************************
* Function Name: main
* Parameters: none
*
* Return: none
*
* Description: main
*
*************************************************************************/
int main(void)
{
unsigned int i;
uip_ipaddr_t ipaddr;
struct timer periodic_timer, arp_timer;
// MAM init
MAMCR_bit.MODECTRL = 0;
MAMTIM_bit.CYCLES = 3; // FCLK > 40 MHz
MAMCR_bit.MODECTRL = 2; // MAM functions fully enabled
// Init clock
InitClock();
// Init GPIO
GpioInit();
// Init VIC
VIC_Init();
// Sys timer init 1/100 sec tick
clock_init(2);
timer_set(&periodic_timer, CLOCK_SECOND / 2);
timer_set(&arp_timer, CLOCK_SECOND * 10);
// Init UART 0
assert(UartInit(UART_0,4,NORM));
UartSetLineCoding(UART_0,UartLineCoding);
__enable_interrupt();
printf("IAR Embedded Workbench for ARM\r\n"
"IAR-LPC-P2378-SK board\r\n"
"uIP-1.0 WEB server demo\r\n");
if(IsBoardRevA())
{
printf("Board rev. \"A\" (See the board's errata)!\n\r");
}
// Initialize the ethernet device driver
while(!tapdev_init())
{
printf("ENET initialization fault!\r\n");
}
// uIP web server
// Initialize the uIP TCP/IP stack.
uip_init();
uip_ipaddr(ipaddr, 192,168,0,100);
uip_sethostaddr(ipaddr);
uip_ipaddr(ipaddr, 192,168,0,1);
uip_setdraddr(ipaddr);
uip_ipaddr(ipaddr, 255,255,255,0);
uip_setnetmask(ipaddr);
uip_gethostaddr(ipaddr);
printf("WEB server ip address - %d.%d.%d.%d\n\r",
ipaddr[0] & 0xFF,
(ipaddr[0]>>8) & 0xFF,
ipaddr[1] & 0xFF,
(ipaddr[1]>>8) & 0xFF);
// Initialize the HTTP server.
httpd_init();
while(1)
{
uip_len = tapdev_read(uip_buf);
if(uip_len > 0)
{
if(BUF->type == htons(UIP_ETHTYPE_IP))
{
uip_arp_ipin();
uip_input();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0)
{
uip_arp_out();
tapdev_send(uip_buf,uip_len);
}
}
else if(BUF->type == htons(UIP_ETHTYPE_ARP))
{
uip_arp_arpin();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0)
{
tapdev_send(uip_buf,uip_len);
}
}
}
else if(timer_expired(&periodic_timer))
{
timer_reset(&periodic_timer);
for(i = 0; i < UIP_CONNS; i++)
{
uip_periodic(i);
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0)
{
uip_arp_out();
tapdev_send(uip_buf,uip_len);
}
}
#if UIP_UDP
for(i = 0; i < UIP_UDP_CONNS; i++) {
uip_udp_periodic(i);
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
uip_arp_out();
tapdev_send();
}
}
#endif /* UIP_UDP */
/* Call the ARP timer function every 10 seconds. */
if(timer_expired(&arp_timer))
{
timer_reset(&arp_timer);
uip_arp_timer();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -