Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 Alternate APIs for RTKernel-32 Preemptive or Cooperative Multitasking? Starting Objects' Methods as Tasks Performance and Interrupt Response Times Task Switches in Cooperative Scheduling Using the FPU in Interrupt Handlers |
Using the FPU in Interrupt HandlersRTKernel-32 maintains an FPU context for each thread when a Floating Point driver such as Fltpii.lib has been linked and the thread was created with flag TF_MATH_CONTEXT. However, no FPU context is maintained for interrupt handlers. Thus, interrupt handlers should not use the FPU. Instead, interrupt handlers should delegate floating point calculations to a thread. If an interrupt handler must nevertheless use the FPU (which is not recommended), it must save and restore the FPU context. This can be done as in this example: #include <Rtk32.h> #include <Rtkflt.h> ... typedef struct { ... } DeviceContext; ... void * FPUContext; // could be part of DeviceContext ... int RTKAPI IntHandler(void * P) // high level interrupt handler { DeviceContext * Context = (DeviceContext*) P; // get device context if (Context->StatusRegister != 0) // any interrupt condition set? { _rtkFLTSave(FPUContext); // save FPU registers _fpreset(); // reset FPU ... // do calculations here _rtkFLTRestore(FPUContext); // restore FPU registers return 1; // done } else return 0; // this int is not for me } ... int main(void) { RTKernelInit(0); // must start the kernel before using the FPU driver FPUContext = calloc(_rtkFLTDataSize(), 1); // must be zero initialized! RTInstallSharedIRQHandlerEx(IRQ, IntHandler, &Context); // program device to start sending interrupts here ... } A separate FPU context for each interrupt handler - which needs the FPU - is required. It is required to zero-initialize the FPU context once before it is used for the first time. Function _rtkFLTDataSize must not be called before RTKernelInit. Please note that the value returned by _rtkFLTDataSize is CPU dependent and will increase with future CPU models, even when the driver Fltpii.lib is not updated. RTKernel-32 will not call the same interrupt handler recursively. It is thus sufficient to maintain exactly one global FPU context per interrupt handler. Saving/resetting/restoring the FPU context in interrupt handlers consumes a lot of CPU time, degrading the overall real-time performance of the application. No ISR of any device driver shipped with On Time RTOS-32 makes use of the FPU.
|