📄 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.: $tnVer / $tnDate
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) $CopyR KEIL - An ARM Company. All rights reserved. $end
*---------------------------------------------------------------------------*/
#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 HTTP demo", /* Buffer for LCD text */
" www.keil.com " };
extern LOCALM localm[]; /* Local Machine Settings */
#define MY_IP localm[NETIF_ETH].IpAdr
#define DHCP_TOUT 50 /* DHCP timeout 5 seconds */
#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;
static void init_display (void);
#endif
/*--------------------------- init ------------------------------------------*/
#ifndef RTX_KERNEL
static void init () {
/* Add System initialisation code here */
/* I/O Ports configured as Output (Push-pull) */
FIO2DIR = 0x000000FF;
FIO2MASK = 0x00000000;
FIO2PIN = 0x00000000;
PINSEL10 = 0;
/* Timer 1 as interval timer, reload to 100ms. */
T1TCR = 1;
T1MCR = 3;
T1MR0 = TCNT - 1;
/* 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 */
}
#else
void init (void) __task {
/* Add System initialisation code here */
/* 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 */
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
/*--------------------------- sendchar --------------------------------------*/
int sendchar (int ch) {
/* 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;
}
/*--------------------------- 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 ();
}
/*------------------------------ wait ---------------------------------------*/
static void wait (int wait_ticks) {
/* wait a number of ticks */
#ifndef RTX_KERNEL
int n = 0;
while (n < wait_ticks) {
if (T1IR & 1) {
T1IR = 1;
n++;
}
}
#else
os_dly_wait(wait_ticks*10);
#endif
}
/*--------------------------- 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) {
/* Success, DHCP has already got the IP address. */
dhcp_tout = 0;
sprintf((char*)lcd_text[1]," %3d.%3d.%3d.%3d", MY_IP[0], MY_IP[1], MY_IP[2], MY_IP[3]);
upd_display ();
return;
}
if (--dhcp_tout == 0) {
/* A timeout, disable DHCP and use static IP address. */
dhcp_disable ();
sprintf((char*)lcd_text[1]," DHCP failed " );
upd_display ();
wait (20);
sprintf((char*)lcd_text[1]," %3d.%3d.%3d.%3d", MY_IP[0], MY_IP[1], MY_IP[2], MY_IP[3]);
upd_display ();
}
}
/*--------------------------- 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 ();
init_display ();
init_TcpNet ();
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 + -