mbed NXP LPC1768+RTOS+KEIL+HTTP CGI (управление мк через WEB)
 
 

MDK ARM 4.14 & RL-ARM 4.12
main.c
#include "LPC17xx.h"
#include "RTL.h"
#include "stdio.h"
#include "string.h"
#include "Net_Config.h"
#define EVT_RTC_SEC 0x0100
#define EVT_LIFE_LED 0x0101
unsigned long long tcp_stack[1024/8];
const unsigned long led_mask[] = { 1UL<<23, 1UL<<21, 1UL<<20, 1UL<< 18 };
char LED1stat = 0;
char LED2stat = 0;
char LED3stat = 0;
LPC_UART_TypeDef *pUart;
typedef struct {
    uint32_t RTC_Sec;     /* Second value - [0,59] */
    uint32_t RTC_Min;     /* Minute value - [0,59] */
    uint32_t RTC_Hour;    /* Hour value - [0,23] */
    uint32_t RTC_Mday;    /* Day of the month value - [1,31] */
    uint32_t RTC_Mon;     /* Month value - [1,12] */
    uint32_t RTC_Year;    /* Year value - [0,4095] */
    uint32_t RTC_Wday;    /* Day of week value - [0,6] */
    uint32_t RTC_Yday;    /* Day of year value - [1,365] */
} RTCTime;
RTCTime CurrentTime = {0,1,1,1,1,2011};
OS_TID t_DEBUG_PRINT;
OS_TID t_RTC_TIME;
OS_TID t_LIFE_LED;
OS_TID t_TIMER_HTTP;
OS_TID t_HTTP;
void RTCSetTime( RTCTime Time )
{
	LPC_RTC->SEC = Time.RTC_Sec;
	LPC_RTC->MIN = Time.RTC_Min;
	LPC_RTC->HOUR = Time.RTC_Hour;
	LPC_RTC->DOM = Time.RTC_Mday;
	LPC_RTC->DOW = Time.RTC_Wday;
	LPC_RTC->DOY = Time.RTC_Yday;
	LPC_RTC->MONTH = Time.RTC_Mon;
	LPC_RTC->YEAR = Time.RTC_Year;   
}
RTCTime RTCGetTime( void )
{
	RTCTime LocalTime;
 
	LocalTime.RTC_Sec = LPC_RTC->SEC;
	LocalTime.RTC_Min = LPC_RTC->MIN;
	LocalTime.RTC_Hour = LPC_RTC->HOUR;
	LocalTime.RTC_Mday = LPC_RTC->DOM;
	LocalTime.RTC_Wday = LPC_RTC->DOW;
	LocalTime.RTC_Yday = LPC_RTC->DOY;
	LocalTime.RTC_Mon = LPC_RTC->MONTH;
	LocalTime.RTC_Year = LPC_RTC->YEAR;
	return ( LocalTime );   
}
void RTC_IRQHandler (void)
{
	LPC_RTC->ILR |= 0x01;
	isr_evt_set (EVT_RTC_SEC, t_RTC_TIME);
}
void DeviceInit (void)
{
	LPC_GPIO1->FIODIR = (1<<23) | (1<<21) | (1<<20) | (1<<18);
	LPC_PINCON->PINSEL0 |= (1 << 4);             /* Pin P0.2 used as TXD0 (Com0) */
    LPC_PINCON->PINSEL0 |= (1 << 6);             /* Pin P0.3 used as RXD0 (Com0) */
	pUart = (LPC_UART_TypeDef *)LPC_UART0;
	pUart->LCR    = 0x83;                          /* 8 bits, no Parity, 1 Stop bit  */
	pUart->DLL    = 9;                             /* 115200 Baud Rate @ 24.0 MHZ PCLK */
	pUart->FDR    = 0x21;                          /* FR 1,51, DIVADDVAL = 1, MULVAL = 2 */
	pUart->DLM    = 0;                             /* High divisor latch = 0         */
	pUart->LCR    = 0x03;                          /* DLAB = 0                       */
	LPC_SC->PCONP |= (1 << 9);
	if ( LPC_RTC->RTC_AUX & (0x1<<4) )
	{
		LPC_RTC->RTC_AUX |= (0x1<<4);
	}
	LPC_RTC->AMR = 0;
	LPC_RTC->CIIR = 1; 
	LPC_RTC->CCR = 0x10;
	RTCSetTime(CurrentTime);
	LPC_RTC->CCR |= 0x01;
	NVIC_EnableIRQ(RTC_IRQn) ;
}
int SER_putChar (int uart, U8 c) 
{
	LPC_UART_TypeDef *pUart;
	pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1;
	while (!(pUart->LSR & 0x20));
	return (pUart->THR = c);
}
void SER_putString (int uart, U8 *s) 
{	  
	while (*s != 0) 
	{
		SER_putChar(uart, *s++);
	}
}
void LEDon (unsigned int num)
{
	LPC_GPIO1->FIOPIN |= led_mask[num];
	if (num == 1)
	{
		LED1stat = 1;
	}
	if (num == 2)
	{
		LED2stat = 1;
	}
	if (num == 3)
	{
		LED3stat = 1;
	}
}
void LEDoff (unsigned int num)
{
	LPC_GPIO1->FIOPIN &= ~led_mask[num];
	if (num == 1)
	{
		LED1stat = 0;
	}
	if (num == 2)
	{
		LED2stat = 0;
	}
	if (num == 3)
	{
		LED3stat = 0;
	}
}
U16 cgi_func (U8 *env, U8 *buf, U16 buflen, U32 *pcgi) 
{
	U32 len = 0;
	switch (env [0]) 
	{
		case 'a':
		{
		if (LED1stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#FF0000");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#00FF00");
		}
		}
		break;
		case 'b':
		{
		if (LED1stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "0");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "1");
		}
		}
		break;
		case 'c':
		{
		if (LED2stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#FF0000");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#00FF00");
		}
		}
		break;
		case 'd':
		{
		if (LED2stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "0");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "1");
		}
		}
		break;
		case 'e':
		{
		if (LED3stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#FF0000");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#00FF00");
		}
		}
		break;
		case 'f':
		{
		if (LED3stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "0");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "1");
		}
		}
		break;
		case 'g':
		{
		if (LED1stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#FFFFFF");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#0066FF");
		}
		}
		break;
		case 'h':
		{
		if (LED2stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#FFFFFF");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#0066FF");
		}
		}
		break;
		case 'j':
		{
		if (LED3stat ==0)
		{ 
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#FFFFFF");
		}
		else
		{
			len = sprintf ((S8 *) buf, (const S8 *) &env [2], "#0066FF");
		}
		}
		break;
	}
	return ((U16) len);
}
void cgi_process_data (U8 code, U8 *dat, U16 len) 
{
	unsigned char text1 [16];
	unsigned char text2 [16];
	unsigned char text3 [16];
 	U8 *var;
 
	var = (U8 *) alloc_mem (40);
	do 
	{
		dat = http_get_var (dat, var, 40);
		SER_putString (0, var);
		if (var [0] != 0 ) 
		{
			if (str_scomp (var, "textbox1=1") == __TRUE) 
			{
				LEDon (1);				
			}
			if (str_scomp (var, "textbox1=0") == __TRUE) 
			{
				LEDoff (1);				
			}
			if (str_scomp (var, "textbox2=1") == __TRUE) 
			{
				LEDon (2);				
			}
			if (str_scomp (var, "textbox2=0") == __TRUE) 
			{
				LEDoff (2);				
			}
			if (str_scomp (var, "textbox3=1") == __TRUE) 
			{
				LEDon (3);				
			}
			if (str_scomp (var, "textbox3=0") == __TRUE) 
			{
				LEDoff (3);				
			}
			
		}
	} while (dat);
	free_mem ((OS_FRAME *)var);
}
__task void f_LIFE_LED (void) 
{
	char i = 1;
	while (1) 
	{
		os_evt_wait_or (EVT_LIFE_LED, 0xffff); 
		if (i)
		{
			LEDon(0);
			i=!i;
		}
		else
		{
			LEDoff(0);
			i=!i;
		}        
    }
}
__task void f_RTC_TIME (void) 
{
	while (1) 
	{
		os_evt_wait_or (EVT_RTC_SEC, 0xffff);
		os_evt_set (EVT_LIFE_LED, t_LIFE_LED);
	}
}
__task void f_TIMER_HTTP (void) 
{
	os_itv_set (10);
	while (1) 
	{
		timer_tick ();
		os_itv_wait ();
	}
}
__task void f_HTTP (void) 
{
	while (1) 
	{
		main_TcpNet();
		os_tsk_pass();          
    }
}
__task void f_DEBUG_PRINT (void) 
{
	while (1) 
	{
		os_dly_wait (100);         
    }
}
__task void init (void) 
{
	os_tsk_prio_self (100);
	SystemInit ();
	DeviceInit ();
	init_TcpNet ();
	SER_putString (0, "\n\nRTOS Starting ...   \n\n");
	t_DEBUG_PRINT = os_tsk_create (f_DEBUG_PRINT, 3);
	SER_putString (0, "DEBUG_PRINT started\n");
	t_RTC_TIME = os_tsk_create (f_RTC_TIME, 50);
	SER_putString (0, "RTC_TIME started\n");
	t_LIFE_LED = os_tsk_create (f_LIFE_LED, 2);
	SER_putString (0, "LIFE_LED started\n");
	t_TIMER_HTTP = os_tsk_create (f_TIMER_HTTP, 99);
	SER_putString (0, "TIMER_HTTP started\n");
	t_HTTP = os_tsk_create_user (f_HTTP, 1, &tcp_stack, sizeof(tcp_stack));
	SER_putString (0, "HTTP started\n");
	
	SER_putString (0, "\n\nOK!\n\n");
		
	os_tsk_delete_self ();
}
int main (void)
{
	os_sys_init(init);
}
web.inp
index.htm, leds.cgi, mbedMicrocontroller.gif, pinout.gif to Web.c nopr root(Web)
index.htm
<HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> <meta http-equiv="Content-Language" content="ru"> <title>mkWEB Server - Main</title> </head> <body style="text-align: center"> <table border="1" width="100%" id="table1" style="border-width: 0px"> <tr> <td style="border-style: none; border-width: medium"> <div align="center"> <img border="0" src="mbedMicrocontroller.gif" width="172" height="149"><table border="1" width="800" id="table2" style="border-width: 0px"> <tr> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> Ãëàâíàÿ</td> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> <span lang="en-us"><a href="leds.cgi"> <span style="text-decoration: none"><font color="#000000">LEDs</font></span></a></span></td> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> </tr> </table> </div> </td> </tr> </table> <table border="1" width="100%" id="table3" style="border-width: 0px"> <tr> <td style="border-style: none; border-width: medium"> <p align="center"> <img border="0" src="pinout.gif" width="610" height="383"></td> </tr> </table> </body> </HTML>
leds.cgi
t <HTML> t <head> t <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> t <meta http-equiv="Content-Language" content="ru"> t <title>mkWEB Server - LEDs</title> t </head> t <body style="text-align: center"> t <table border="1" width="100%" id="table3" style="border-width: 0px"> t <tr> t <td style="border-style: none; border-width: medium"> t <div align="center"> t <img border="0" src="mbedMicrocontroller.gif" width="172" height="149"> t <table border="1" width="800" id="table4" style="border-width: 0px"> t <tr> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> t <a href="index.htm"><span style="text-decoration: none"> t <font color="#000000">Ãëàâíàÿ</font></span></a></td> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> t <span lang="en-us"><font color="#000000"> t <span style="text-decoration: none">LEDs</span></font></span></td> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> t <td align="center" width="100" style="border-style: none; border-width: medium" bgcolor="#00FF00"> </td> t </tr> t </table> t </div> t </td> t </tr> t </table> t <table border="1" width="100%" id="table7" style="border-width: 0px"> t <tr> t <td style="border-style: none; border-width: medium"> t <p align="center"> t <img border="0" src="pinout.gif" width="610" height="383"></p> t <div align="center"> t <table border="0" width="140" id="table8" style="border-width: 0px" cellspacing="3" cellpadding="0"> t <tr> c g <td style="border-style: none; border-width: medium" bgcolor="%s"> </td> c h <td style="border-style: none; border-width: medium" bgcolor="%s"> </td> c j <td style="border-style: none; border-width: medium" bgcolor="%s"> </td> t <td style="border-style: none; border-width: medium"> </td> t </tr> t </table> t </div> t <p align="center"> </td> t </tr> t </table> t <table border="1" width="100%" id="table5" style="border-width: 0px"> t <tr> t <td style="border-style: none; border-width: medium"> t <div align="center"><a name="leds"></a> t <table border="1" width="800" id="table6" style="border-width: 0px"> t <tr> t <td align="center" width="260" style="border-style: none; c a border-width: medium" bgcolor="%s"> c b <p align="center"><span lang="en-us">LED #1 = %s</span></p> t <p align="center"> t <FORM ACTION="leds.cgi#leds" METHOD="POST" NAME="CGI"> t <TABLE> t <TR> t <TD> c b <INPUT TYPE="TEXT" id="textbox1" name="textbox1" SIZE="16" VALUE="%s"></TD> t <TD ALIGN="right"> t <INPUT TYPE="SUBMIT" id="change1" name="change1" VALUE="SET 1/0"></TD> t </TR> t </TABLE> t </FORM> t </p></td> t <td align="center" width="260" style="border-style: none; c c border-width: medium" bgcolor="%s"> c d <p align="center"><span lang="en-us">LED #2 = %s</span></p> t <p align="center"> t <FORM ACTION="leds.cgi#leds" METHOD="POST" NAME="CGI"> t <TABLE> t <TR> t <TD> c d <INPUT TYPE="TEXT" id="textbox2" name="textbox2" SIZE="16" VALUE="%s"></TD> t <TD ALIGN="right"> t <INPUT TYPE="SUBMIT" id="change2" name="change2" VALUE="SET 1/0"></TD> t </TR> t </TABLE> t </FORM> t </p></td> t </td> t <td align="center" width="260" style="border-style: none; c e border-width: medium" bgcolor="%s"> c f <p align="center"><span lang="en-us">LED #3 = %s</span></p> t <p align="center"> t <FORM ACTION="leds.cgi#leds" METHOD="POST" NAME="CGI"> t <TABLE> t <TR> t <TD> c f <INPUT TYPE="TEXT" id="textbox3" name="textbox3" SIZE="16" VALUE="%s"></TD> t <TD ALIGN="right"> t <INPUT TYPE="SUBMIT" id="change3" name="change3" VALUE="SET 1/0"></TD> t </TR> t </TABLE> t </FORM> t </p></td> t </td> t </tr> t </table> t </div> t </td> t </tr> t </table> t </body> t </HTML> .
4 comments on mbed NXP LPC1768+RTOS+KEIL+HTTP CGI (управление мк через WEB) :
Please log in to post comments.

This does not compile in my uVision4 compiler from Keil or on the mbed online compiler. Just lots of errors.....