This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimal timing slice , Thread qn


I am attaching the .c file. Pls see the thread read and write functions.
thanks


On Wed, 19 Jan 2005 19:47:04 +0100, Andrew Lunn <andrew@lunn.ch> wrote:
> On Wed, Jan 19, 2005 at 09:11:51AM -0800, steve smith wrote:
> > my first question had nothing to do with the second one. I am sorry i
> > didnt make it clear in the email.
> > Regarding your comments: I am sure that the USART is receiving data as
> > I have seen the waveform on the scope. I have configured the serial
> > driver for the board.
> > I dont understand why cyg_io_write should block. it simply write a
> > character to USART without flow control. so if mutex is unlocked on
> > the USART it shall never block.
> > Anyhow the main question is that the cyg_io_write hogs all the cpu and
> > the cyg_io_read inspite of getting data is not running. I print out
> > the values after cyg_io_read and thats how I guess that its not
> > running.
> 
> I think we need to see your code so we have a better idea what is
> going on. Is that possible?
> 
>         Thanks
>                 Andrew
>
//includes
#include <cyg/kernel/kapi.h>
#include <cyg/io/io.h> //cyg_io_write, cyg_io_read
#include <stdio.h>

//#defines
//size of stack for thread
#define THREAD_STACK_SIZE 4096


//variable definitions for this file
int threadStack[2][THREAD_STACK_SIZE]; //stack for the threads
cyg_handle_t threadWriteHandle,threadReadHandle; //handle for thread
cyg_thread threadWriteObj,threadReadObj; //thread info object
cyg_io_handle_t ttyHdl;
int err=0;
char outputString[] = "0xAA\n\r";
cyg_uint32 outputLen = sizeof(outputString);
cyg_uint32 readLen = 36;
cyg_mutex_t printfSerialIoMutex; //mutex to protect serial port from simultaneous read and write and also printf
char readBuffer[36]; //for serial port read buffer
//functions declarations
void threadWriteRoutine(cyg_addrword_t);
void threadreadRoutine(cyg_addrword_t);

//functions definitions
void threadWriteRoutine(cyg_addrword_t data)
{
	while(1)
	{
		cyg_mutex_lock(&printfSerialIoMutex);
		err = cyg_io_write (ttyHdl,outputString,&outputLen);
		cyg_mutex_unlock(&printfSerialIoMutex);
		cyg_thread_yield(); //give execution to thread at same priority level
	}
}
	
void threadReadRoutine(cyg_addrword_t data)
{
	while(1)
	{
		cyg_mutex_lock(&printfSerialIoMutex);
		err = cyg_io_read (ttyHdl,readBuffer,&readLen);
		printf("Read Byte %s\n\r",readBuffer);
		cyg_mutex_unlock(&printfSerialIoMutex);
		cyg_thread_yield(); //give execution to thread at same priority level
	}
}
	
//main function for user application
void cyg_user_start(void)
{
	//initialize mutex
	cyg_mutex_init(&printfSerialIoMutex);
	err = cyg_io_lookup("/dev/tty1", &ttyHdl); 
	if (err)	
	{
		printf("error opening device\n\r");
		printf("cyg_io_lookup returned error code = %u\n\r",err);
		return;
	}
	else
	{
		printf("successfully opened device\n\r");
	}
	//create thread to write and read from the serial port (Serial B) 
	cyg_thread_create(
			0, 	 	         //shed_info: priority 0 = highest; 31 = lowest
			threadWriteRoutine,        //entry: routine that begins execution of thread
			(cyg_addrword_t)0,       //entry_data: data passed to thread entry routine.
			"threadWrite",             //name: string name of thread
			(void *)threadStack[0],  //stack_base: base address of the stack for the thread
			THREAD_STACK_SIZE,    //stack_size: size in bytes of the stack for the thread
			&threadWriteHandle,        //returned handle to the thread
			&threadWriteObj	         //thread information is stored in the thread memory object pointed to by this parameter)
			); //creates thread in suspended mode
	//resume thread when scheduler starts
	cyg_thread_resume(threadWriteHandle);
	cyg_thread_create(
			0, 	 	         //shed_info: priority 0 = highest; 31 = lowest
			threadReadRoutine,        //entry: routine that begins execution of thread
			(cyg_addrword_t)0,       //entry_data: data passed to thread entry routine.
			"threadRead",             //name: string name of thread
			(void *)threadStack[1],  //stack_base: base address of the stack for the thread
			THREAD_STACK_SIZE,    //stack_size: size in bytes of the stack for the thread
			&threadReadHandle,        //returned handle to the thread
			&threadReadObj	         //thread information is stored in the thread memory object pointed to by this parameter)
			); //creates thread in suspended mode
	//resume thread when scheduler starts
	cyg_thread_resume(threadReadHandle);
}




-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]