Problems with Periodic Timer

Hello,

Hooray, I have successfully written my InterruptAttach() routine to replace the posix timer!! Thanks for all of the help.

Right now, I trying to work on the thread problem that I was having. I have rearranged my code, so that I create the thread in my main program, and then I call the myclass::ctrlLoop routine from my thread. However, when I try to create two threads of the ctrlLoop, I found that the first thread completes myclass::ctrlLoop, but then the second thread just hangs. Is it because I’m using the same InterruptWait() routine in both threads? How can I fix my problem? (My ISR generates on interrupt every 400us)

Thanx.


myClass s;
pthread_barrier_t barrier;

main()
{
//Create a barrier for threads
pthread_barrier_init (&barrier, NULL, 2);
pthread_create (NULL, NULL, thread_ctrl, (void *) 2);

// Run P-Ctrller 
s.ctrlLoop(motor1);

//Wait for the other instance of the controller
pthread_barrier_wait (&barrier); 

return 0;

}

//Separate thread for motor 2
void * thread_ctrl(void * iMotor)
{
int i = (int)iMotor;
s.ctrlLoop(i);
pthread_barrier_wait(&barrier);
}

// Control Loop
int myClass::ctrlLoop(int iMotor)
{
int i;
//Process Control Loop
for (i=0;i<= 100;i++)
{
//Wait for ISR to give us a timer
InterruptWait(0,NULL);

//then do stuff
}
return 0;
}

I just realized part of the problem was because I was calling InterruptAttach() only in the main thread, assuming that it would affect both. However, I just read that this is not true. I changed my code so that in myClass::ctrlLoop I call InterruptAttach first (so both threads call it). However, it still doesn’t share the Interrupt # 0 (periodic timer). In order for both threads to share the same interrupt, do I have to write two different interrupt handlers?