Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Interrupt handling: (6 Items)
   
Interrupt handling  
Hi list!

I read the page http://www.qnx.co.jp/developers/docs/6.3.2/neutrino/prog/inthandler.html about interrupts. 

Then I adapted the given example to handle keyboard irq. 

But my system freezes.

//**********************************************************
#include <stdio.h>
#include <sys/neutrino.h>
#include <sys/syspage.h>
#include <semaphore.h>
#include <pthread.h>

#define IRQ1 1

struct sigevent event;
volatile unsigned counter;
sem_t end;

const struct sigevent *handler(void *area, int id) {
	printf("ID : %d\n", id);
	return(&event);
	
}
void * int_thread(void * cookie){
	int i;
	int id;
	// Request I/O privity
	
	ThreadCtl(_NTO_TCTL_IO, 0);
	// Initialize event structure
	printf("setting notification mode\n");
	event.sigev_notify = SIGEV_INTR;

	id=InterruptAttach(IRQ1, &handler, NULL, 0, 0 );
	for( i = 0; i < 10; ++i ) {
		InterruptWait( 0, NULL );
		printf( "key hit\n" );
		InterruptUnmask(IRQ1, id);
	}
	// Disconnect the ISR handler
	
	InterruptDetach(id);
	sem_post(&end);
	return NULL;
}

int main() {
	printf("application using irq1\n");
	sem_init(&end, NULL, 0);
	pthread_create(NULL, NULL, int_thread, NULL); 

	/*
	
	....
	
	*/

	sem_wait(&end);
	printf("normal termination...\n");

	return 0;
}
//*******************************************************

I also have a few questions.

1. If I need to manage with multiples interrupts source. Should I create one thread and one ISR per source. Or can I use
 one thread which will wait on many interrupts and then call the right ISR? If yes, how can I implement that?

2. In the handler function, the second parameter is "id", can someone explain me how the kernel informs this parameter?

Thanks,

Cédirc
RE: Interrupt handling  
Hi Cedric,
Could you please try it again w/o a printf() in an ISR! 

Peter


-----Original Message-----
From: Cédric Schaffter [mailto:community-noreply@qnx.com] 
Sent: Wednesday, October 22, 2008 2:41 PM
To: general-community
Subject: Interrupt handling

Hi list!

I read the page http://www.qnx.co.jp/developers/docs/6.3.2/neutrino/prog/inthandler.html about interrupts. 

Then I adapted the given example to handle keyboard irq. 

But my system freezes.

//**********************************************************
#include <stdio.h>
#include <sys/neutrino.h>
#include <sys/syspage.h>
#include <semaphore.h>
#include <pthread.h>

#define IRQ1 1

struct sigevent event;
volatile unsigned counter;
sem_t end;

const struct sigevent *handler(void *area, int id) {
	printf("ID : %d\n", id);
	return(&event);
	
}
void * int_thread(void * cookie){
	int i;
	int id;
	// Request I/O privity
	
	ThreadCtl(_NTO_TCTL_IO, 0);
	// Initialize event structure
	printf("setting notification mode\n");
	event.sigev_notify = SIGEV_INTR;

	id=InterruptAttach(IRQ1, &handler, NULL, 0, 0 );
	for( i = 0; i < 10; ++i ) {
		InterruptWait( 0, NULL );
		printf( "key hit\n" );
		InterruptUnmask(IRQ1, id);
	}
	// Disconnect the ISR handler
	
	InterruptDetach(id);
	sem_post(&end);
	return NULL;
}

int main() {
	printf("application using irq1\n");
	sem_init(&end, NULL, 0);
	pthread_create(NULL, NULL, int_thread, NULL); 

	/*
	
	....
	
	*/

	sem_wait(&end);
	printf("normal termination...\n");

	return 0;
}
//*******************************************************

I also have a few questions.

1. If I need to manage with multiples interrupts source. Should I create one thread and one ISR per source. Or can I use
 one thread which will wait on many interrupts and then call the right ISR? If yes, how can I implement that?

2. In the handler function, the second parameter is "id", can someone explain me how the kernel informs this parameter?

Thanks,

Cédirc


_______________________________________________
General
http://community.qnx.com/sf/go/post15391
Re: RE: Interrupt handling  
Thanks Peter, without the printf it works :)! 

Do you know why this instruction made freeze?

Regards,
Cédric
RE: RE: Interrupt handling  
In general, it's not allowed to do kernel calls during an ISR.
Your code in the ISR will be executed with the kernel's stack.
As Thomas said, there are two main different approaches to register for an IRQ event with different flavors, you will 
find many examples in the docs.
If you want to know about the possibility to use a function within an IRQ (kernel call or not), just look at the bottom 
of the documentation for each function.
There you will find a table, explaining the context, the function could be used in.

E.g. For printf()

Classification:
ANSI, POSIX 1003.1

Safety:  
Cancellation point   No 
Interrupt handler    No 
Signal handler       Read the Caveats 
Thread               Yes  

Peter
 


-----Original Message-----
From: Cédric Schaffter [mailto:community-noreply@qnx.com] 
Sent: Wednesday, October 22, 2008 3:14 PM
To: general-community
Subject: Re: RE: Interrupt handling

Thanks Peter, without the printf it works :)! 

Do you know why this instruction made freeze?

Regards,
Cédric

_______________________________________________
General
http://community.qnx.com/sf/go/post15395
AW: Interrupt handling  
Hi Cédric,

besides not doing kernel calls from an isr (better read the /entire/
documentation page for InterruptAttach :-), you also shouldn't need 
to unmask the interrupt after getting the interrupt event - that's 
only required when using  InterruptAttachEvent()  or if you explicitly
masked the IRQ in your interrupt handler. Which leads to your other
questions:

[snip]
> I also have a few questions.
> 
> 1. If I need to manage with multiples interrupts source. 
> Should I create one thread and one ISR per source. Or can I 
> use one thread which will wait on many interrupts and then 
> call the right ISR? If yes, how can I implement that?

You do not need an extra thread to handle interrupts. You'll usually 
dedicate one thread to interrupt handling in so it can quickly react
to hardware events. Just as well you do not need an extra thread per
interrupt source. You could call  InterruptAttach()  multiple times 
for different IRQs, each time providing the same ISR. For handling 
different devices from the same code, you might use the data area 
you can provide to the handler via the "area" argument.
 
> 2. In the handler function, the second parameter is "id", can 
> someone explain me how the kernel informs this parameter?

It is the interrupt id that was returned to your program from 
 InterruptAttach() .  You would, for example, need it if you wanted 
to mask the IRQ in your interrupt handler and had interrupt masking 
tracked (which you don't right now, but usually always should - see 
the  _NTO_INTR_FLAGS_TRK_MSK  flag to  InterruptAttach() )


- Thomas


> Thanks,
> 
> Cédirc
> 
> 
> _______________________________________________
> General
> http://community.qnx.com/sf/go/post15391
> 
Re: AW: Interrupt handling  
Thanks for all this detailled information Peter and Thomas!

These interrupts managements are more clear to my mind now.