The ‘C’ test. Part – XI

Interrupts
11. Interrupts are an important part of embedded systems. Consequently, many compiler vendors offer an extension to standard C to support interrupts. Typically, this new keyword is __interrupt. The following code uses __interrupt to define an interrupt service routine (ISR). Comment on the code.

__interrupt double compute_area
(double
radius)
{
double area = PI * radius *
radius;
printf(“\nArea = %f”, area);
return area;
}

This function has so much wrong with it, it’s hard to know where to start:

  • ISRs cannot return a value. If you don’t understand this, you aren’t hired
  • ISRs cannot be passed parameters. See the first item for your employment prospects if you missed this
  • On many processors/compilers, floating-point operations are not necessarily re-entrant. In some cases one needs to stack additional registers. In other cases, one simply cannot do floating point in an ISR. Furthermore, given that a general rule of thumb is that ISRs should be short and sweet, one wonders about the wisdom of doing floating-point math here
  • In a vein similar to the third point, printf() often has problems with reentrancy and performance. If you missed points three and four, I wouldn’t be too hard on you. Needless to say, if you got these last two points, your employment prospects are looking better and better

…to be continued

Leave a comment