How to make an interrupt do a big complicated thing. Solution.

19 Apr 2011

Hi all..

I was trying to get an interrupt to write to a USB drive whilst a load of other stuff was going on, and it just wasn't working. (As I found out yesterday, interrupts don't like doing big complicated things)

So I came up with this construct. I thought it'd be a useful post for anyone who is new to running microprocessors like me.

here's some pseudo code.

...
WRITENOW = 0;
attach writeflagger() to ticker;

writeflagger() {
WRITENOW=1
}
 
writeIt() {
WRITENOW = 0;
BIG SLOW OUTPUT METHOD
}
 
main {
while (1) {
.... doing lots of stuff very quickly...
  if (WRITENOW) {
      writeIt(); 
  }
}
 
19 Apr 2011

Yes, that's generally how you're supposed to do it. Check out this great article by Andy.

19 Apr 2011

I did read that the other day... honest !

Andy's article is for those who know what they are doing though, all that unattaching and attaching.

I have a feeling mine will be less efficient, but I can understand it. The curse of the procedural programmer eh?

I think they are slightly different too.. I notice Andy has nowt going on in his main. My main is prepared to wait while it goes and does the "big" method when it see's the flag set.

I suppose mine is flag driven, Andy's is much more pure interrupt driven ?

19 Apr 2011

The core idea is the same: don't do major work in the interrupt handler, do it in in the user context (i.e. main or functions called from main).