Calling an interrupt from the handling of another.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Calling an interrupt from the handling of another.

Post by egos »

Ring buffer needs only for static implementation FIFO queue. It is not ring queue. And if it has the size defined as power of 2 it can be wraped by "and size-1" operation. Usually in my kernel event queue has the size 16 K and so it can contain queuesize/packagesize-1 event packages. When posting event and queue is full kernel cleans the queue by setting one pointer as other one and posts RESET event.
If you have seen bad English in my words, tell me what's wrong, please.
eXeCuTeR
Member
Member
Posts: 63
Joined: Tue Dec 09, 2008 12:43 pm

Re: Calling an interrupt from the handling of another.

Post by eXeCuTeR »

Something really bazaar just happened..
This code works:

Code: Select all

char getch()
{
 while(1)
 {
   if(flag) break;
   printf("%d", flag); // printing is actually essential!
 }

 return read_from_pipe();
}
but all of these, DON'T!

Code: Select all

char getch()
{
 while(!flag)
   printf("%d", flag); // doesn't even show a number!

 return read_from_pipe();
}

Code: Select all

char getch()
{
 while(!flag);
 return read_from_pipe();
}
(p.s: don't worry about indexes, read_from_pipe() and write_to_pipe() handle them already. and also, excuse me from the lousy names, will be changed in future)
(write_to_pipe() is called from the IRQ and sets the flag to 1, read_from_pipe() from the process sets the flag to 1 if there's absolutely no data to be read which I also check with the indexes)
(also, only write_to_pipe() is external to keyboard.c, what i have shown you now is kbdapi.c)

any explanation to this?
Last edited by eXeCuTeR on Sat Jul 24, 2010 2:40 am, edited 1 time in total.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Calling an interrupt from the handling of another.

Post by gerryg400 »

You need to declare flag as volatile.
If a trainstation is where trains stop, what is a workstation ?
eXeCuTeR
Member
Member
Posts: 63
Joined: Tue Dec 09, 2008 12:43 pm

Re: Calling an interrupt from the handling of another.

Post by eXeCuTeR »

gerryg400 wrote:You need to declare flag as volatile.
Oh that's great! Thanks, all good now!
But I don't see why this actually matters? Because if it was kept into a register, and the IRQ actually change 5 registers (which i remember 3 of them ATM) and if one was it, the results would not be very nice. Just not sure about it, but what the heck.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Calling an interrupt from the handling of another.

Post by gerryg400 »

But I don't see why this actually matters?
If a variable can be modified outside of the normal flow of the program (e.g. by interrupt service routine or by hardware) you need to declare it as volatile.
In this case

Code: Select all

flag=0;
while(!flag)
    ;
gcc might assume that 'flag' will always be zero because it cannot see any code that changes it. It may optimise away the test and replace it with

Code: Select all

flag=0;
while(1)
    ;
If you declare flag as volatile it won't do that.
If a trainstation is where trains stop, what is a workstation ?
Post Reply