📄 http_demo.c
字号:
/*----------------------------------------------------------------------------
* R T L T C P N E T E x a m p l e
*----------------------------------------------------------------------------
* Name: HTTP_DEMO.C
* Purpose: HTTP Server demo example
* Rev.: V3.20
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2008 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <RTL.h>
#include <Net_Config.h>
#include <LPC23xx.H> /* LPC23xx definitions */
#include "LCD.h"
#define MCLK 48000000 /* Master Clock 48 MHz */
#define TCLK 10 /* Timer Clock rate 10/s */
#define TCNT (MCLK/TCLK/4) /* Timer Counts */
BOOL LEDrun;
BOOL LCDupdate;
BOOL tick;
U32 dhcp_tout;
U8 lcd_text[2][16+1] = {" RL-ARM", /* Buffer for LCD text */
" HTTP example"};
extern LOCALM localm[]; /* Local Machine Settings */
#define MY_IP localm[NETIF_ETH].IpAdr
#define DHCP_TOUT 50 /* DHCP timeout 5 seconds */
static void init_io (void);
static void init_display (void);
#ifdef RTX_KERNEL
U64 tcp_stack[800/8]; /* A bigger stack for tcp_task */
/* Forward references */
void init (void) __task;
void blink_led (void) __task;
void timer_task (void) __task;
void tcp_task (void) __task;
#endif
/*--------------------------- init ------------------------------------------*/
#ifndef RTX_KERNEL
static void init () {
/* Add System initialisation code here */
init_io ();
init_display ();
init_TcpNet ();
/* Timer 1 as interval timer, reload to 100ms. */
T1TCR = 1;
T1MCR = 3;
T1MR0 = TCNT - 1;
}
#else
void init (void) __task {
/* Add System initialisation code here */
init_io ();
init_display ();
init_TcpNet ();
/* Initialize Tasks */
os_tsk_prio_self (100);
os_tsk_create (blink_led, 20);
os_tsk_create (timer_task, 30);
os_tsk_create_user (tcp_task, 0, &tcp_stack, sizeof(tcp_stack));
os_tsk_delete_self();
}
#endif
/*--------------------------- timer_poll ------------------------------------*/
#ifndef RTX_KERNEL
static void timer_poll () {
/* System tick timer running in poll mode */
if (T1IR & 1) {
T1IR = 1;
/* Timer tick every 100 ms */
timer_tick ();
tick = __TRUE;
}
}
#else
void timer_task (void) __task {
/* System tick timer task */
os_itv_set (10);
while (1) {
timer_tick ();
tick = __TRUE;
os_itv_wait ();
}
}
#endif
/*--------------------------- init_io ---------------------------------------*/
static void init_io () {
/* I/O Ports configured as Output (Push-pull) */
FIO2DIR = 0x000000FF;
FIO2MASK = 0x00000000;
FIO2PIN = 0x00000000;
PINSEL10 = 0;
/* Configure UART1 for 115200 baud. */
PINSEL0 = 0x40000000; /* Enable TxD1 pin */
PINSEL1 = 0x00000001; /* Enable RxD1 pin */
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit*/
U1DLL = 3; /* for 12MHz PCLK Clock */
U1FDR = 0x67; /* Fractional Divider */
U1LCR = 0x03; /* DLAB = 0 */
/* Configure AD0.0 input. */
PCONP |= 0x00001000; /* Enable power to AD block */
PINSEL1 |= 0x00004000; /* AD0.0 pin function select */
}
/*--------------------------- fputc -----------------------------------------*/
int fputc(int ch, FILE *f) {
/* Debug output to serial port. */
if (ch == '\n') {
while (!(U1LSR & 0x20));
U1THR = '\r'; /* output CR */
}
while (!(U1LSR & 0x20));
return (U1THR = ch);
}
/*--------------------------- LED_out ---------------------------------------*/
void LED_out (U32 val) {
FIO2SET = val;
FIO2CLR = ~val;
}
/*--------------------------- AD_in -----------------------------------------*/
U16 AD_in (U32 ch) {
/* Read ARM Analog Input */
U32 val = 0;
U32 adcr;
if (ch < 8) {
adcr = 0x01000000 | (1 << ch);
AD0CR = adcr | 0x00200C00; /* Setup A/D: 10-bit @ 4MHz */
do {
val = AD0GDR; /* Read A/D Data Register */
} while ((val & 0x80000000) == 0); /* Wait for end of A/D Conv. */
AD0CR &= ~adcr; /* Stop A/D Conversion */
val = (val >> 6) & 0x03FF; /* Extract AINx Value */
}
return (val);
}
/*--------------------------- upd_display -----------------------------------*/
static void upd_display () {
/* Update LCD Module display text. */
LCD_cls ();
LCD_puts (lcd_text[0]);
LCD_gotoxy (1,2);
LCD_puts (lcd_text[1]);
LCDupdate =__FALSE;
}
/*--------------------------- init_display ----------------------------------*/
static void init_display () {
/* LCD Module.2x16 init*/
LCD_init ();
LCD_cur_off ();
upd_display ();
}
/*--------------------------- dhcp_check ------------------------------------*/
static void dhcp_check () {
/* Monitor DHCP IP address assignment. */
if (tick == __FALSE || dhcp_tout == 0) {
return;
}
#ifdef RTX_KERNEL
tick = __FALSE;
#endif
if (mem_test (&MY_IP, 0, IP_ADRLEN) == __FALSE && !(dhcp_tout & 0x80000000)) {
/* Success, DHCP has already got the IP address. */
dhcp_tout = 0;
sprintf((S8 *)lcd_text[0],"IP address:");
sprintf((S8 *)lcd_text[1],"%d.%d.%d.%d", MY_IP[0], MY_IP[1],
MY_IP[2], MY_IP[3]);
LCDupdate = __TRUE;
return;
}
if (--dhcp_tout == 0) {
/* A timeout, disable DHCP and use static IP address. */
dhcp_disable ();
sprintf((S8 *)lcd_text[1]," DHCP failed " );
LCDupdate = __TRUE;
dhcp_tout = 30 | 0x80000000;
return;
}
if (dhcp_tout == 0x80000000) {
dhcp_tout = 0;
sprintf((S8 *)lcd_text[0],"IP address:");
sprintf((S8 *)lcd_text[1],"%d.%d.%d.%d", MY_IP[0], MY_IP[1],
MY_IP[2], MY_IP[3]);
LCDupdate = __TRUE;
}
}
/*--------------------------- blink_led -------------------------------------*/
#ifndef RTX_KERNEL
static void blink_led () {
/* Blink the LEDs on MCB2300 board */
const U8 led_val[16] = { 0x48,0x88,0x84,0x44,0x42,0x22,0x21,0x11,
0x12,0x0A,0x0C,0x14,0x18,0x28,0x30,0x50 };
static U32 cnt;
if (tick == __TRUE) {
/* Every 100 ms */
tick = __FALSE;
if (LEDrun == __TRUE) {
LED_out (led_val[cnt]);
if (++cnt >= sizeof(led_val)) {
cnt = 0;
}
}
if (LCDupdate == __TRUE) {
upd_display ();
}
}
}
#else
void blink_led () __task {
/* Blink the LEDs on MCB2300 board */
const U8 led_val[16] = { 0x48,0x88,0x84,0x44,0x42,0x22,0x21,0x11,
0x12,0x0A,0x0C,0x14,0x18,0x28,0x30,0x50 };
static U32 cnt;
LEDrun = __TRUE;
while(1) {
/* Every 100 ms */
if (LEDrun == __TRUE) {
LED_out (led_val[cnt]);
if (++cnt >= sizeof(led_val)) {
cnt = 0;
}
}
if (LCDupdate == __TRUE) {
upd_display ();
}
os_dly_wait(10);
}
}
#endif
/*---------------------------------------------------------------------------*/
#ifndef RTX_KERNEL
int main (void) {
/* Main Thread of the TcpNet */
init ();
LEDrun = __TRUE;
dhcp_tout = DHCP_TOUT;
while (1) {
timer_poll ();
main_TcpNet ();
dhcp_check ();
blink_led ();
}
}
#else
void tcp_task (void) __task {
/* Main Thread of the TcpNet. This task should have */
/* the lowest priority because it is always READY. */
dhcp_tout = DHCP_TOUT;
while (1) {
main_TcpNet();
dhcp_check ();
os_tsk_pass();
}
}
int main (void) {
/* Start with 'init' task. */
os_sys_init(init);
while(1);
}
#endif
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -