[En-Nut-Discussion] Thread Switching Problem
MUHAMMAD AZEEM AZAM
muhammadazeemazam786 at yahoo.com
Thu Apr 5 19:39:41 CEST 2007
I am posting the whole code. I am unable to find thread switching problem. Today i figured out but not sure so please check my code and
------------------
#include <sys/timer.h>
#include <dev/board.h>
#include <dev/hd44780.h>
#include <dev/term.h>
#include <stdio.h>
#include <cfg/os.h>
#include <sys/heap.h>
#include <string.h>
#include <io.h>
#include <sys/confnet.h>
#include <pro/dhcp.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/version.h>
#include <sys/thread.h>
#include <sys/types.h>
#include <netinet/tcp.h>
//--------------------------
#include "shift.h"
#include "lcd.h"
//----------------------------
#define MY_IPADDR "192.168.05.20"
#define MY_IPMASK "255.255.255.0"
#define MY_MAC { 0x00, 0x0A, 0x59, 0x03, 0x00, 0x5F }
#define BUFFSIZE 128
//---------------------------------------
prog_char mem_fmt_P[] = "%u bytes RAM free";
prog_char W_Cmmnd[] = "Wrong Button Pressed";
char text_msg3_str[] = "";
//--------------------------------------------
static char buff[BUFFSIZE];
unsigned int packet_number = 1;
//-------------------------------------------
u_char ShiftLEDOut =0x55;
u_char ShiftLEDIn =0;
u_char LCDText[50] ="\x0";
//-----------------------------------------
/**************************************************************************************
************************* Process Request Function*************************************
**************************************************************************************/
void ProcessRequests(FILE * TCPStream)
{
int got_data;
char *cp;
for (;;)
{
/*
* Flush a stream.
* The calling thread may be suspended until all buffered output data has been written.
*/
fflush(TCPStream);
/*
* Read a line from a stream.
*/
if (fgets(buff, sizeof(buff), TCPStream) == 0) //fread as well
break;
/*
* Chop off EOL.
*/
if ((cp = strchr(buff, '\r')) != 0)
*cp = 0;
if ((cp = strchr(buff, '\n')) != 0)
*cp = 0;
strcpy(text_msg3_str,buff);
LCD_Init();
LCD_Cursor_YX( 0, 0 );
LCD_Puts(text_msg3_str);
LCD_Cursor_YX( 1, 0 );
LCD_Puts("Command");
LCD_SendCmd(0x0C); /* display on, cursor off */
/**************************************************************
* Compute the length of a NULL terminated String.
* Ignoring blank lines.
********************************************************************/
got_data = strlen(buff);
if (got_data == 0)
continue; // Skip other Loop Statements. Got to the start of the Loop.
/************************************************************
* Compare two strings up to a given number of characters.
**************************************************************/
if (strncmp(buff, "Memory", got_data) == 0) //If Match
{
fprintf_P(TCPStream, mem_fmt_P, (u_int)NutHeapAvailable()); //#include <sys/heap.h>
continue;
}
//-------------------------------------------------------------------
if (strncmp(buff, "Data", got_data) == 0) //If Match
{
fprintf(TCPStream, "Muhammad Azeem Azam");
continue;
}
//--------------------
if (strncmp(buff, "Packet", got_data) == 0) //If Match
{
int p_size = 128;
char p_array[p_size];
int index = 0;
p_array[0] = (packet_number & 0xff00) >> 8; //Mask Upper 8-Bits
p_array[1] = packet_number & 0x00ff; // Mask Lower 8-Bits
for(index=2;index<p_size;index++)
{
p_array[index] = rand()%26+ 'a';
}
fwrite(p_array,sizeof(p_array),1,TCPStream);
packet_number ++ ;
NutSleep(500);
continue;
}
//------------------------------------------
if (strncmp("Quit", buff, got_data) == 0)
{
fprintf(TCPStream, "quit");
break; //Exit from Loop
}
//--------------------------------------------------------------------
/*
* For any other Pressed Button
*/
strcpy(text_msg3_str,"Wrong Button");
LCD_Init();
LCD_Cursor_YX(0,0);
LCD_Puts(text_msg3_str);
LCD_SendCmd(0x0C); /* display on, cursor off */
fprintf_P(TCPStream,W_Cmmnd);
}
}
/**************************************************************************************
******************* TCP Service Function **********************************************
**************************************************************************************/
void TCPservice(void)
{
TCPSOCKET *sock;
FILE *TCPStream;
int nTcpError;
if ((sock = NutTcpCreateSocket()) != 0)
{
if (NutTcpAccept(sock, 23) == 0)
{
if ((TCPStream = _fdopen((int) sock, "r+b")) != 0)
{
ProcessRequests(TCPStream);
fclose(TCPStream);
}
else
puts("Assigning a stream failed");
}
else // Accepy
{
nTcpError = NutTcpError(sock); // Error for Accepy
}
NutTcpCloseSocket(sock);
}
else //Creation
{
nTcpError = NutTcpError(sock); // Error for Socket Creation
}
}
/**************************************************************************************
*************************** Lists Threads *********************************************
**************************************************************************************/
THREAD(service_thread, arg)
{
//NutThreadSetPriority(61);
for (;;)
{
PORTD = PORTD & 0xfe; //DDRD = 0xff; //PORTD = 0x00;
//NutSleep(500);
TCPservice();
PORTD = PORTD | 0x01;
//DDRD = 0xff;
//PORTD = 0xff;
}
}
THREAD(compute_thread, arg)
{
//NutThreadSetPriority(65);
for (;;)
{
NutSleep(500);
PORTD = PORTD & 0xfd;
NutSleep(500);
PORTD = PORTD & 0x02;
//DDRD = 0xff;
}
}
/**************************************************************************************
****************************** Initialization Functions *******************************
**************************************************************************************/
void InitRegisterDevice(void)
{
NutRegisterDevice(&devEth0, 0x8300, 5);
}
int InitEthernetDevice(void)
{
u_long ip_addr = inet_addr(MY_IPADDR);
u_long ip_mask = inet_addr(MY_IPMASK);
u_char mac[6] = MY_MAC;
NutNetIfConfig("eth0", mac, ip_addr, ip_mask);
return 0;
}
/**************************************************************************************
*********************************** Main **********************************************
**************************************************************************************/
int main(void)
{
static char txt_msg0_str[] = "TCP Server \x0";
static char txt_msg1_str[] = "Ethernut Project\x0";
/***************************************************
* LED & LCD initialization
*****************************************************/
DevBoardShiftLedOut(ShiftLEDOut);
strcpy(LCDText,txt_msg0_str);
strcat(LCDText,txt_msg1_str);
LCD_Init();
LCD_Cursor_YX( 0, 0 );
LCD_Puts(txt_msg0_str);
LCD_Cursor_YX( 1, 0 );
LCD_Puts(txt_msg1_str);
LCD_SendCmd(0x0C); /* display on, cursor off */
//----------------------------------
InitRegisterDevice();
if (InitEthernetDevice() == 0)
{
NutThreadCreate("NET_Thread", service_thread, 0, 512);
NutThreadCreate("OTH_Thread", compute_thread, 0, 512);
for (;;)
{
DDRD = 0xff;
PORTD = 0xfb;
//PORTD = PORTD & 0xfb;
NutSleep(550); //550
PORTD = PORTD | 0x04;
NutSleep(550); //550
}
}
return 0;
}
------------------
Ralph Mason <ralph.mason at telogis.com> wrote:
I guess its a problem in your TCPservice function.
-----Original Message-----
From: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] On Behalf Of MUHAMMAD AZEEM
AZAM
Sent: 05 April 2007 02:45
To: en-nut-discussion at egnite.de
Subject: [En-Nut-Discussion] Thread Switching Problem
I have the following code and my thread is not switching from main() to
Compute() Thread except for the first time. All threads have same priority.
Service thread is a TCP reuest Thread.
THanks for help in advance
Bye
------------------
THREAD(service_thread, arg)
{
//NutThreadSetPriority(61);
for (;;)
{
PORTD = PORTD & 0xfe;
//DDRD = 0xff;
//PORTD = 0x00;
TCPservice();
PORTD = PORTD | 0x01;
//DDRD = 0xff;
//PORTD = 0xff;
}
}
THREAD(compute_thread, arg)
{
//NutThreadSetPriority(65);
for (;;)
{
NutSleep(500);
PORTD = PORTD & 0xfd;
NutSleep(500);
PORTD = PORTD | 0x02;
//DDRD = 0xff;
//PORTD = 0x0f;
//NutSleep(1000);
}
}
/***************************************************************************
***********
****************************** Initialization Functions
*******************************
****************************************************************************
**********/
void InitRegisterDevice(void)
{
NutRegisterDevice(&devEth0, 0x8300, 5);
}
int InitEthernetDevice(void)
{
u_long ip_addr = inet_addr(MY_IPADDR);
u_long ip_mask = inet_addr(MY_IPMASK);
u_char mac[6] = MY_MAC;
NutNetIfConfig("eth0", mac, ip_addr, ip_mask);
return 0;
}
/***************************************************************************
***********
*********************************** Main
**********************************************
****************************************************************************
**********/
int main(void)
{
static char txt_msg0_str[] = "TCP Server \x0";
static char txt_msg1_str[] = "Ethernut Project\x0";
/***************************************************
* LED & LCD initialization
*****************************************************/
DevBoardShiftLedOut(ShiftLEDOut);
strcpy(LCDText,txt_msg0_str);
strcat(LCDText,txt_msg1_str);
LCD_Init();
LCD_Cursor_YX( 0, 0 );
LCD_Puts(txt_msg0_str);
LCD_Cursor_YX( 1, 0 );
LCD_Puts(txt_msg1_str);
LCD_SendCmd(0x0C); /* display on, cursor off */
//----------------------------------
InitRegisterDevice();
if (InitEthernetDevice() == 0)
{
NutThreadCreate("NET_Thread", service_thread, 0, 512);
NutThreadCreate("OTH_Thread", compute_thread, 0, 512);
//NutThreadSetPriority(254); /*smallest priority on main program.
Default priority is 64*/
for (;;)
{
NutSleep(1000);
PORTD = PORTD & 0xfb;
NutSleep(1000);
PORTD = PORTD | 0x04;
//DDRD = 0xff;
//PORTD = 0xf0;
//NutSleep(500);
}
}
return 0;
}
-----------------------
---------------------------------
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
_______________________________________________
http://lists.egnite.de/mailman/listinfo/en-nut-discussion
--
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.15.9/573 - Release Date: 05/12/2006
16:07
--
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.15.9/573 - Release Date: 05/12/2006
16:07
_______________________________________________
http://lists.egnite.de/mailman/listinfo/en-nut-discussion
---------------------------------
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
More information about the En-Nut-Discussion
mailing list